Debian Codegraph @

tests/test_inhibit.py

raw
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 from enum import Enum, Flag
10
11
12 class InhibitFlags(Flag):
13 LOGOUT = 1
14 USER_SWITCH = 2
15 SUSPEND = 4
16 IDLE = 8
17 ALL = 16 - 1
18
19
20 class SessionState(Enum):
21 RUNNING = 1
22 QUERY_END = 2
23 ENDING = 3
24
25
26 @pytest.fixture
27 def required_templates():
28 return {"inhibit": {}}
29
30
31 class TestInhibit:
32 def set_permissions(self, dbus_con, app_id, permissions):
33 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
34 perm_store_intf.SetPermission(
35 "inhibit",
36 True,
37 "inhibit",
38 app_id,
39 permissions,
40 )
41
42 def test_version(self, portals, dbus_con):
43 xdp.check_version(dbus_con, "Inhibit", 3)
44
45 def test_basic(self, portals, dbus_con, xdp_app_info):
46 app_id = xdp_app_info.app_id
47 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
48 mock_intf = xdp.get_mock_iface(dbus_con)
49
50 reason = "reason"
51 flags = InhibitFlags.ALL
52
53 request = xdp.Request(dbus_con, inhibit_intf)
54 options = {
55 "reason": reason,
56 }
57 response = request.call(
58 "Inhibit",
59 window="",
60 flags=flags.value,
61 options=options,
62 )
63
64 assert response
65 assert response.response == 0
66
67 # Check the impl portal was called with the right args
68 method_calls = mock_intf.GetMethodCalls("Inhibit")
69 assert len(method_calls) > 0
70 _, args = method_calls[-1]
71 assert args[1] == app_id
72 assert args[2] == "" # parent window
73 assert args[3] == flags.value
74 assert args[4]["reason"] == reason
75
76 @pytest.mark.parametrize(
77 "token", ("Invalid-Token&", "", "/foo", "something-else", "😄")
78 )
79 def test_inhibit_invalid_handle_token(self, portals, dbus_con, token):
80 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
81
82 request = xdp.Request(dbus_con, inhibit_intf)
83 options = {"handle_token": token}
84
85 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
86 request.call("Inhibit", window="", flags=0, options=options)
87
88 e = excinfo.value
89 assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument"
90 assert "Invalid token" in e.get_dbus_message()
91
92 @pytest.mark.parametrize("template_params", ({"inhibit": {"response": 1}},))
93 def test_cancel(self, portals, dbus_con, xdp_app_info):
94 app_id = xdp_app_info.app_id
95 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
96 mock_intf = xdp.get_mock_iface(dbus_con)
97
98 reason = "reason"
99 flags = InhibitFlags.ALL
100
101 request = xdp.Request(dbus_con, inhibit_intf)
102 options = {
103 "reason": reason,
104 }
105 response = request.call(
106 "Inhibit",
107 window="",
108 flags=flags.value,
109 options=options,
110 )
111
112 # for some reason, the backend failing is still considered a success
113 assert response
114 assert response.response == 0
115
116 # Check the impl portal was called with the right args
117 method_calls = mock_intf.GetMethodCalls("Inhibit")
118 assert len(method_calls) > 0
119 _, args = method_calls[-1]
120 assert args[1] == app_id
121 assert args[2] == "" # parent window
122 assert args[3] == flags.value
123 assert args[4]["reason"] == reason
124
125 @pytest.mark.parametrize("template_params", ({"inhibit": {"expect-close": True}},))
126 def test_close(self, portals, dbus_con, xdp_app_info):
127 app_id = xdp_app_info.app_id
128 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
129 mock_intf = xdp.get_mock_iface(dbus_con)
130
131 reason = "reason"
132 flags = InhibitFlags.ALL
133
134 request = xdp.Request(dbus_con, inhibit_intf)
135 request.schedule_close(1000)
136 options = {
137 "reason": reason,
138 }
139 request.call(
140 "Inhibit",
141 window="",
142 flags=flags.value,
143 options=options,
144 )
145
146 # Only true if the impl.Request was closed too
147 assert request.closed
148
149 # Check the impl portal was called with the right args
150 method_calls = mock_intf.GetMethodCalls("Inhibit")
151 assert len(method_calls) > 0
152 _, args = method_calls[-1]
153 assert args[1] == app_id
154 assert args[2] == "" # parent window
155 assert args[3] == flags.value
156 assert args[4]["reason"] == reason
157
158 def test_permission(self, portals, dbus_con, xdp_app_info):
159 app_id = xdp_app_info.app_id
160 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
161 mock_intf = xdp.get_mock_iface(dbus_con)
162
163 self.set_permissions(dbus_con, app_id, ["logout", "suspend"])
164
165 reason = "reason"
166 flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND | InhibitFlags.IDLE
167 allowed_flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND
168
169 request = xdp.Request(dbus_con, inhibit_intf)
170 options = {
171 "reason": reason,
172 }
173 response = request.call(
174 "Inhibit",
175 window="",
176 flags=flags.value,
177 options=options,
178 )
179
180 assert response
181 assert response.response == 0
182
183 method_calls = mock_intf.GetMethodCalls("Inhibit")
184 _, args = method_calls[-1]
185 assert args[3] == allowed_flags.value
186
187 self.set_permissions(dbus_con, app_id, ["suspend"])
188
189 flags = InhibitFlags.LOGOUT | InhibitFlags.SUSPEND | InhibitFlags.IDLE
190 allowed_flags = InhibitFlags.SUSPEND
191
192 request = xdp.Request(dbus_con, inhibit_intf)
193 options = {
194 "reason": reason,
195 }
196 response = request.call(
197 "Inhibit",
198 window="",
199 flags=flags.value,
200 options=options,
201 )
202
203 assert response
204 assert response.response == 0
205
206 method_calls = mock_intf.GetMethodCalls("Inhibit")
207 _, args = method_calls[-1]
208 assert args[3] == allowed_flags.value
209
210 def test_monitor(self, portals, dbus_con, xdp_app_info):
211 app_id = xdp_app_info.app_id
212 inhibit_intf = xdp.get_portal_iface(dbus_con, "Inhibit")
213 mock_intf = xdp.get_mock_iface(dbus_con)
214
215 changed_count = 0
216
217 request = xdp.Request(dbus_con, inhibit_intf)
218 options = {
219 "session_handle_token": "session_token0",
220 }
221 response = request.call(
222 "CreateMonitor",
223 window="",
224 options=options,
225 )
226
227 assert response
228 assert response.response == 0
229
230 session = xdp.Session.from_response(dbus_con, response)
231
232 # Check the impl portal was called with the right args
233 method_calls = mock_intf.GetMethodCalls("CreateMonitor")
234 assert len(method_calls) > 0
235 _, args = method_calls[-1]
236 assert args[1] == session.handle
237 assert args[2] == app_id
238 assert args[3] == "" # parent window
239
240 def state_changed_cb(session_handle, state):
241 nonlocal changed_count
242
243 assert not state["screensaver-active"]
244 assert state["session-state"] == SessionState.QUERY_END.value
245
246 changed_count += 1
247
248 inhibit_intf.connect_to_signal("StateChanged", state_changed_cb)
249
250 # wait for a Query End state change
251 xdp.wait_for(lambda: changed_count == 1)
252 assert not session.closed
253 # and respond with QueryEndResponse
254 inhibit_intf.QueryEndResponse(session.handle)
255
256 # wait for another Query End state change
257 xdp.wait_for(lambda: changed_count == 2)
258 assert not session.closed
259
260 # do not respond with QueryEndResponse and instead wait for >1s
261 xdp.wait(1500)
262
263 # the session should have gotten closed by now
264 assert session.closed