Debian Codegraph @

tests/test_globalshortcuts.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests 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", 1)
20
21 def test_create_close_session(self, portals, dbus_con, app_id):
22 globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts")
23 mock_intf = xdp.get_mock_iface(dbus_con)
24
25 request = xdp.Request(dbus_con, globalshortcuts_intf)
26 options = {
27 "session_handle_token": "session_token0",
28 }
29 response = request.call(
30 "CreateSession",
31 options=options,
32 )
33
34 assert response
35 assert response.response == 0
36
37 session = xdp.Session.from_response(dbus_con, response)
38 # Check the impl portal was called with the right args
39 method_calls = mock_intf.GetMethodCalls("CreateSession")
40 assert len(method_calls) > 0
41 _, args = method_calls[-1]
42 assert args[1] == session.handle
43 assert args[2] == app_id
44
45 session.close()
46 xdp.wait_for(lambda: session.closed)
47
48 @pytest.mark.parametrize(
49 "template_params", ({"globalshortcuts": {"force-close": 500}},)
50 )
51 def test_create_session_signal_closed(self, portals, dbus_con, app_id):
52 globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts")
53 mock_intf = xdp.get_mock_iface(dbus_con)
54
55 request = xdp.Request(dbus_con, globalshortcuts_intf)
56 options = {
57 "session_handle_token": "session_token0",
58 }
59 response = request.call(
60 "CreateSession",
61 options=options,
62 )
63
64 assert response
65 assert response.response == 0
66
67 session = xdp.Session.from_response(dbus_con, response)
68 # Check the impl portal was called with the right args
69 method_calls = mock_intf.GetMethodCalls("CreateSession")
70 assert len(method_calls) > 0
71 _, args = method_calls[-1]
72 assert args[1] == session.handle
73 assert args[2] == app_id
74
75 # Now expect the backend to close it
76 xdp.wait_for(lambda: session.closed)
77
78 def test_bind_list_shortcuts(self, portals, dbus_con):
79 globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts")
80
81 request = xdp.Request(dbus_con, globalshortcuts_intf)
82 options = {
83 "session_handle_token": "session_token0",
84 }
85 response = request.call(
86 "CreateSession",
87 options=options,
88 )
89
90 assert response
91 assert response.response == 0
92
93 session = xdp.Session.from_response(dbus_con, response)
94
95 shortcuts = [
96 (
97 "binding1",
98 {
99 "description": dbus.String("Binding #1", variant_level=1),
100 "preferred-trigger": dbus.String("CTRL+a", variant_level=1),
101 },
102 ),
103 (
104 "binding2",
105 {
106 "description": dbus.String("Binding #2", variant_level=1),
107 "preferred-trigger": dbus.String("CTRL+b", variant_level=1),
108 },
109 ),
110 ]
111
112 request = xdp.Request(dbus_con, globalshortcuts_intf)
113 response = request.call(
114 "BindShortcuts",
115 session_handle=session.handle,
116 shortcuts=shortcuts,
117 parent_window="",
118 options={},
119 )
120
121 assert response
122 assert response.response == 0
123
124 request = xdp.Request(dbus_con, globalshortcuts_intf)
125 options = {}
126 response = request.call(
127 "ListShortcuts",
128 session_handle=session.handle,
129 options=options,
130 )
131
132 assert response
133 assert response.response == 0
134
135 assert len(list(response.results["shortcuts"])) == len(list(shortcuts))
136
137 session.close()
138 xdp.wait_for(lambda: session.closed)
139
140 def test_trigger(self, portals, dbus_con):
141 globalshortcuts_intf = xdp.get_portal_iface(dbus_con, "GlobalShortcuts")
142 mock_intf = xdp.get_mock_iface(dbus_con)
143
144 request = xdp.Request(dbus_con, globalshortcuts_intf)
145 options = {
146 "session_handle_token": "session_token0",
147 }
148 response = request.call(
149 "CreateSession",
150 options=options,
151 )
152
153 assert response
154 assert response.response == 0
155
156 session = xdp.Session.from_response(dbus_con, response)
157
158 shortcuts = [
159 (
160 "binding1",
161 {
162 "description": dbus.String("Binding #1", variant_level=1),
163 "preferred-trigger": dbus.String("CTRL+a", variant_level=1),
164 },
165 ),
166 ]
167
168 request = xdp.Request(dbus_con, globalshortcuts_intf)
169 response = request.call(
170 "BindShortcuts",
171 session_handle=session.handle,
172 shortcuts=shortcuts,
173 parent_window="",
174 options={},
175 )
176
177 assert response
178 assert response.response == 0
179
180 activated_count = 0
181 deactivated_count = 0
182
183 def cb_activated(session_handle, shortcut_id, timestamp, options):
184 nonlocal activated_count
185 now_since_epoch = int(time.time() * 1000000)
186 # This assert will race twice a year on systems configured with
187 # summer time timezone changes
188 assert (
189 now_since_epoch > timestamp
190 and (now_since_epoch - 10 * 10001000) < timestamp
191 )
192 assert shortcut_id == "binding1"
193 activated_count += 1
194
195 def cb_deactivated(session_handle, shortcut_id, timestamp, options):
196 nonlocal deactivated_count
197 now_since_epoch = int(time.time() * 1000000)
198 # This assert will race twice a year on systems configured with
199 # summer time timezone changes
200 assert (
201 now_since_epoch > timestamp
202 and (now_since_epoch - 10 * 10001000) < timestamp
203 )
204 assert shortcut_id == "binding1"
205 deactivated_count += 1
206
207 globalshortcuts_intf.connect_to_signal("Activated", cb_activated)
208 globalshortcuts_intf.connect_to_signal("Deactivated", cb_deactivated)
209
210 mock_intf.Trigger(session.handle, "binding1")
211
212 xdp.wait_for(lambda: activated_count == 1 and deactivated_count == 1)
213 assert not session.closed
214
215 session.close()
216 xdp.wait_for(lambda: session.closed)