| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests.xdp_utils as xdp |
| 6 |
|
| 7 |
import dbus |
| 8 |
import pytest |
| 9 |
import time |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def required_templates(): |
| 14 |
return {"globalshortcuts": {}} |
| 15 |
|
| 16 |
|
| 17 |
class TestGlobalShortcuts: |
| 18 |
def test_version(self, portals, dbus_con): |
| 19 |
xdp.check_version(dbus_con, "GlobalShortcuts", 2) |
| 20 |
|
| 21 |
def test_create_close_session(self, portals, dbus_con, xdp_app_info): |
| 22 |
app_id = xdp_app_info.app_id |
| 23 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 24 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 25 |
|
| 26 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 27 |
options = { |
| 28 |
"session_handle_token": "session_token0", |
| 29 |
} |
| 30 |
response = request.call( |
| 31 |
"CreateSession", |
| 32 |
options=options, |
| 33 |
) |
| 34 |
|
| 35 |
assert response |
| 36 |
assert response.response == 0 |
| 37 |
|
| 38 |
session = xdp.Session.from_response(dbus_con, response) |
| 39 |
# Check the impl portal was called with the right args |
| 40 |
method_calls = mock_intf.GetMethodCalls("CreateSession") |
| 41 |
assert len(method_calls) > 0 |
| 42 |
_, args = method_calls[-1] |
| 43 |
assert args[1] == session.handle |
| 44 |
assert args[2] == app_id |
| 45 |
|
| 46 |
session.close() |
| 47 |
xdp.wait_for(lambda: session.closed) |
| 48 |
|
| 49 |
@pytest.mark.parametrize( |
| 50 |
"template_params", ({"globalshortcuts": {"force-close": 500}},) |
| 51 |
) |
| 52 |
def test_create_session_signal_closed(self, portals, dbus_con, xdp_app_info): |
| 53 |
app_id = xdp_app_info.app_id |
| 54 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 55 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 56 |
|
| 57 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 58 |
options = { |
| 59 |
"session_handle_token": "session_token0", |
| 60 |
} |
| 61 |
response = request.call( |
| 62 |
"CreateSession", |
| 63 |
options=options, |
| 64 |
) |
| 65 |
|
| 66 |
assert response |
| 67 |
assert response.response == 0 |
| 68 |
|
| 69 |
session = xdp.Session.from_response(dbus_con, response) |
| 70 |
# Check the impl portal was called with the right args |
| 71 |
method_calls = mock_intf.GetMethodCalls("CreateSession") |
| 72 |
assert len(method_calls) > 0 |
| 73 |
_, args = method_calls[-1] |
| 74 |
assert args[1] == session.handle |
| 75 |
assert args[2] == app_id |
| 76 |
|
| 77 |
# Now expect the backend to close it |
| 78 |
xdp.wait_for(lambda: session.closed) |
| 79 |
|
| 80 |
def test_bind_list_shortcuts(self, portals, dbus_con): |
| 81 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 82 |
|
| 83 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 84 |
options = { |
| 85 |
"session_handle_token": "session_token0", |
| 86 |
} |
| 87 |
response = request.call( |
| 88 |
"CreateSession", |
| 89 |
options=options, |
| 90 |
) |
| 91 |
|
| 92 |
assert response |
| 93 |
assert response.response == 0 |
| 94 |
|
| 95 |
session = xdp.Session.from_response(dbus_con, response) |
| 96 |
|
| 97 |
shortcuts = [ |
| 98 |
( |
| 99 |
"binding1", |
| 100 |
{ |
| 101 |
"description": dbus.String("Binding #1", variant_level=1), |
| 102 |
"preferred-trigger": dbus.String("CTRL+a", variant_level=1), |
| 103 |
}, |
| 104 |
), |
| 105 |
( |
| 106 |
"binding2", |
| 107 |
{ |
| 108 |
"description": dbus.String("Binding #2", variant_level=1), |
| 109 |
"preferred-trigger": dbus.String("CTRL+b", variant_level=1), |
| 110 |
}, |
| 111 |
), |
| 112 |
] |
| 113 |
|
| 114 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 115 |
response = request.call( |
| 116 |
"BindShortcuts", |
| 117 |
session_handle=session.handle, |
| 118 |
shortcuts=shortcuts, |
| 119 |
parent_window="", |
| 120 |
options={}, |
| 121 |
) |
| 122 |
|
| 123 |
assert response |
| 124 |
assert response.response == 0 |
| 125 |
|
| 126 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 127 |
options = {} |
| 128 |
response = request.call( |
| 129 |
"ListShortcuts", |
| 130 |
session_handle=session.handle, |
| 131 |
options=options, |
| 132 |
) |
| 133 |
|
| 134 |
assert response |
| 135 |
assert response.response == 0 |
| 136 |
|
| 137 |
assert len(list(response.results["shortcuts"])) == len(list(shortcuts)) |
| 138 |
|
| 139 |
session.close() |
| 140 |
xdp.wait_for(lambda: session.closed) |
| 141 |
|
| 142 |
def test_bind_no_shortcuts(self, portals, dbus_con): |
| 143 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 144 |
|
| 145 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 146 |
options = { |
| 147 |
"session_handle_token": "session_token0", |
| 148 |
} |
| 149 |
response = request.call( |
| 150 |
"CreateSession", |
| 151 |
options=options, |
| 152 |
) |
| 153 |
|
| 154 |
assert response |
| 155 |
assert response.response == 0 |
| 156 |
|
| 157 |
session = xdp.Session.from_response(dbus_con, response) |
| 158 |
|
| 159 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 160 |
response = request.call( |
| 161 |
"BindShortcuts", |
| 162 |
session_handle=session.handle, |
| 163 |
shortcuts=[], |
| 164 |
parent_window="", |
| 165 |
options={}, |
| 166 |
) |
| 167 |
|
| 168 |
assert response |
| 169 |
assert response.response == 0 |
| 170 |
|
| 171 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 172 |
options = {} |
| 173 |
response = request.call( |
| 174 |
"ListShortcuts", |
| 175 |
session_handle=session.handle, |
| 176 |
options=options, |
| 177 |
) |
| 178 |
|
| 179 |
assert response |
| 180 |
assert response.response == 0 |
| 181 |
|
| 182 |
assert len(list(response.results["shortcuts"])) == 0 |
| 183 |
|
| 184 |
session.close() |
| 185 |
xdp.wait_for(lambda: session.closed) |
| 186 |
|
| 187 |
def test_trigger(self, portals, dbus_con): |
| 188 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 189 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 190 |
|
| 191 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 192 |
options = { |
| 193 |
"session_handle_token": "session_token0", |
| 194 |
} |
| 195 |
response = request.call( |
| 196 |
"CreateSession", |
| 197 |
options=options, |
| 198 |
) |
| 199 |
|
| 200 |
assert response |
| 201 |
assert response.response == 0 |
| 202 |
|
| 203 |
session = xdp.Session.from_response(dbus_con, response) |
| 204 |
|
| 205 |
shortcuts = [ |
| 206 |
( |
| 207 |
"binding1", |
| 208 |
{ |
| 209 |
"description": dbus.String("Binding #1", variant_level=1), |
| 210 |
"preferred-trigger": dbus.String("CTRL+a", variant_level=1), |
| 211 |
}, |
| 212 |
), |
| 213 |
] |
| 214 |
|
| 215 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 216 |
response = request.call( |
| 217 |
"BindShortcuts", |
| 218 |
session_handle=session.handle, |
| 219 |
shortcuts=shortcuts, |
| 220 |
parent_window="", |
| 221 |
options={}, |
| 222 |
) |
| 223 |
|
| 224 |
assert response |
| 225 |
assert response.response == 0 |
| 226 |
|
| 227 |
activated_count = 0 |
| 228 |
deactivated_count = 0 |
| 229 |
|
| 230 |
def cb_activated(session_handle, shortcut_id, timestamp, options): |
| 231 |
nonlocal activated_count |
| 232 |
now_since_epoch = int(time.time() * 1000000) |
| 233 |
# This assert will race twice a year on systems configured with |
| 234 |
# summer time timezone changes |
| 235 |
assert ( |
| 236 |
now_since_epoch > timestamp |
| 237 |
and (now_since_epoch - 10 * 10001000) < timestamp |
| 238 |
) |
| 239 |
assert shortcut_id == "binding1" |
| 240 |
activated_count += 1 |
| 241 |
|
| 242 |
def cb_deactivated(session_handle, shortcut_id, timestamp, options): |
| 243 |
nonlocal deactivated_count |
| 244 |
now_since_epoch = int(time.time() * 1000000) |
| 245 |
# This assert will race twice a year on systems configured with |
| 246 |
# summer time timezone changes |
| 247 |
assert ( |
| 248 |
now_since_epoch > timestamp |
| 249 |
and (now_since_epoch - 10 * 10001000) < timestamp |
| 250 |
) |
| 251 |
assert shortcut_id == "binding1" |
| 252 |
deactivated_count += 1 |
| 253 |
|
| 254 |
globalshortcuts_intf.connect_to_signal("Activated", cb_activated) |
| 255 |
globalshortcuts_intf.connect_to_signal("Deactivated", cb_deactivated) |
| 256 |
|
| 257 |
mock_intf.Trigger(session.handle, "binding1") |
| 258 |
|
| 259 |
xdp.wait_for(lambda: activated_count == 1 and deactivated_count == 1) |
| 260 |
assert not session.closed |
| 261 |
|
| 262 |
session.close() |
| 263 |
xdp.wait_for(lambda: session.closed) |
| 264 |
|
| 265 |
def test_configure_shortcuts(self, portals, dbus_con): |
| 266 |
globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts") |
| 267 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 268 |
|
| 269 |
request = xdp.Request(dbus_con, globalshortcuts_intf) |
| 270 |
options = { |
| 271 |
"session_handle_token": "session_token0", |
| 272 |
} |
| 273 |
response = request.call( |
| 274 |
"CreateSession", |
| 275 |
options=options, |
| 276 |
) |
| 277 |
|
| 278 |
assert response |
| 279 |
assert response.response == 0 |
| 280 |
|
| 281 |
session = xdp.Session.from_response(dbus_con, response) |
| 282 |
parent_window = "" |
| 283 |
options = {"activation_token": "token_123"} |
| 284 |
|
| 285 |
globalshortcuts_intf.ConfigureShortcuts(session.handle, parent_window, options) |
| 286 |
|
| 287 |
method_calls = mock_intf.GetMethodCalls("ConfigureShortcuts") |
| 288 |
assert len(method_calls) > 0 |
| 289 |
_, args = method_calls[-1] |
| 290 |
|
| 291 |
assert args[0] == session.handle |
| 292 |
assert args[1] == "" |
| 293 |
assert args[2] == options |