Debian Codegraph @

tests/test_screenshot.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 Flag
10 from typing import Any
11 import os
12 from pathlib import Path
13
14
15 SCREENSHOT_DATA = dbus.Dictionary(
16 {
17 "uri": "file:///screenshot.png",
18 "color": (0.0, 1.0, 0.331),
19 },
20 signature="sv",
21 )
22
23
24 class ScreenshotTarget(Flag):
25 SCREEN = 1
26 WINDOW = 2
27 AREA = 4
28 ACTIVE_WINDOW = 8
29
30
31 SCREENSHOT_TARGETS_GOOD = tuple(target.value for target in ScreenshotTarget)
32 SCREENSHOT_TARGETS_BAD = (
33 0,
34 (ScreenshotTarget.SCREEN | ScreenshotTarget.WINDOW).value,
35 16,
36 )
37 SCREENSHOT_TARGETS_ALL = (
38 ScreenshotTarget.SCREEN
39 | ScreenshotTarget.WINDOW
40 | ScreenshotTarget.AREA
41 | ScreenshotTarget.ACTIVE_WINDOW
42 ).value
43
44
45 @pytest.fixture
46 def required_templates():
47 image = Path(os.environ["XDG_DATA_HOME"]) / "screenshot-image.png"
48 image.write_text("image contents")
49 SCREENSHOT_DATA["uri"] = f"file://{image.absolute().as_posix()}"
50
51 return {
52 "access": {},
53 "screenshot": {
54 "results": SCREENSHOT_DATA,
55 },
56 }
57
58
59 class TestScreenshot:
60 def set_permission(self, dbus_con, appid, permission):
61 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
62 perm_store_intf.SetPermission(
63 "screenshot",
64 True,
65 "screenshot",
66 appid,
67 [permission],
68 )
69
70 def test_version(self, portals, dbus_con):
71 xdp.check_version(dbus_con, "Screenshot", 3)
72
73 def test_available_targets(self, portals, dbus_con):
74 properties_intf = dbus.Interface(
75 xdp.get_xdp_dbus_object(dbus_con), "org.freedesktop.DBus.Properties"
76 )
77 available_targets = properties_intf.Get(
78 "org.freedesktop.portal.Screenshot",
79 "AvailableTargets",
80 )
81 assert int(available_targets) == SCREENSHOT_TARGETS_ALL
82
83 @pytest.mark.parametrize("target", SCREENSHOT_TARGETS_GOOD)
84 def test_screenshot_target(
85 self, xdg_document_portal, portals, dbus_con, xdp_app_info, target
86 ):
87 app_id = xdp_app_info.app_id
88 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
89 mock_intf = xdp.get_mock_iface(dbus_con)
90
91 request = xdp.Request(dbus_con, screenshot_intf)
92 response = request.call(
93 "Screenshot",
94 parent_window="",
95 options={
96 "interactive": True,
97 "target": dbus.UInt32(target),
98 },
99 )
100
101 assert response
102 assert response.response == 0
103
104 method_calls = mock_intf.GetMethodCalls("Screenshot")
105 assert len(method_calls) > 0
106 _, args = method_calls[-1]
107 assert args[1] == app_id
108 assert args[2] == ""
109 assert args[3]["target"] == target
110
111 @pytest.mark.parametrize("target", SCREENSHOT_TARGETS_BAD)
112 def test_screenshot_invalid_target(self, portals, dbus_con, target):
113 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
114
115 request = xdp.Request(dbus_con, screenshot_intf)
116 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
117 request.call(
118 "Screenshot",
119 parent_window="",
120 options={
121 "interactive": True,
122 "target": dbus.UInt32(target),
123 },
124 )
125
126 e = excinfo.value
127 assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument"
128 assert "Invalid screenshot target" in e.get_dbus_message()
129
130 @pytest.mark.parametrize(
131 "template_params",
132 (
133 {
134 "screenshot": {
135 "available-targets": ScreenshotTarget.SCREEN.value,
136 "results": SCREENSHOT_DATA,
137 },
138 },
139 ),
140 )
141 def test_screenshot_unavailable_target(self, portals, dbus_con):
142 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
143
144 request = xdp.Request(dbus_con, screenshot_intf)
145 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
146 request.call(
147 "Screenshot",
148 parent_window="",
149 options={
150 "interactive": True,
151 "target": dbus.UInt32(ScreenshotTarget.WINDOW.value),
152 },
153 )
154
155 e = excinfo.value
156 assert e.get_dbus_name() == "org.freedesktop.portal.Error.InvalidArgument"
157 assert "Unavailable screenshot target" in e.get_dbus_message()
158
159 @pytest.mark.parametrize(
160 "template_params",
161 (
162 {
163 "screenshot": {
164 "version": 2,
165 "results": SCREENSHOT_DATA,
166 },
167 },
168 ),
169 )
170 def test_screenshot_options_forwarded_to_v2_backend(
171 self, xdg_document_portal, portals, dbus_con
172 ):
173 xdp.check_version(dbus_con, "Screenshot", 2)
174
175 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
176 mock_intf = xdp.get_mock_iface(dbus_con)
177
178 request = xdp.Request(dbus_con, screenshot_intf)
179 response = request.call(
180 "Screenshot",
181 parent_window="",
182 options={
183 "modal": False,
184 "interactive": True,
185 },
186 )
187
188 assert response
189 assert response.response == 0
190
191 method_calls = mock_intf.GetMethodCalls("Screenshot")
192 assert len(method_calls) > 0
193 _, args = method_calls[-1]
194 assert not args[3]["modal"]
195 assert args[3]["interactive"]
196 assert "target" not in args[3]
197
198 @pytest.mark.parametrize("modal", [True, False])
199 @pytest.mark.parametrize("interactive", [True, False])
200 def test_screenshot_basic(
201 self, xdg_document_portal, portals, dbus_con, xdp_app_info, modal, interactive
202 ):
203 app_id = xdp_app_info.app_id
204 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
205 mock_intf = xdp.get_mock_iface(dbus_con)
206
207 request = xdp.Request(dbus_con, screenshot_intf)
208 options = {
209 "modal": modal,
210 "interactive": interactive,
211 }
212 response = request.call(
213 "Screenshot",
214 parent_window="",
215 options=options,
216 )
217
218 assert response
219 assert response.response == 0
220
221 assert xdp.uri_same_file(SCREENSHOT_DATA["uri"], response.results["uri"])
222
223 # Check the impl portal was called with the right args
224 method_calls = mock_intf.GetMethodCalls("Screenshot")
225 assert len(method_calls) > 0
226 _, args = method_calls[-1]
227 assert args[1] == app_id
228 assert args[2] == "" # parent window
229 assert args[3]["modal"] == modal
230 assert args[3]["interactive"] == interactive
231
232 # check that args were forwarded to access portal correctly
233 if not interactive:
234 method_calls = mock_intf.GetMethodCalls("AccessDialog")
235 assert len(method_calls) > 0
236 _, args = method_calls[-1]
237 assert args[1] == app_id
238 assert args[2] == "" # parent window
239 assert args[6]["modal"] == modal
240
241 @pytest.mark.parametrize(
242 "template_params", ({"screenshot": {"expect-close": True}},)
243 )
244 def test_screenshot_close(self, portals, dbus_con):
245 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
246
247 request = xdp.Request(dbus_con, screenshot_intf)
248 request.schedule_close(1000)
249 options = {
250 "interactive": True,
251 }
252 request.call(
253 "Screenshot",
254 parent_window="",
255 options=options,
256 )
257
258 # Only true if the impl.Request was closed too
259 assert request.closed
260
261 @pytest.mark.parametrize("template_params", ({"screenshot": {"response": 1}},))
262 def test_screenshot_cancel(self, portals, dbus_con, xdp_app_info):
263 app_id = xdp_app_info.app_id
264 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
265 mock_intf = xdp.get_mock_iface(dbus_con)
266
267 modal = True
268 interactive = True
269
270 request = xdp.Request(dbus_con, screenshot_intf)
271 options = {
272 "modal": modal,
273 "interactive": interactive,
274 }
275 response = request.call(
276 "Screenshot",
277 parent_window="",
278 options=options,
279 )
280
281 assert response
282 assert response.response == 1
283
284 # Check the impl portal was called with the right args
285 method_calls = mock_intf.GetMethodCalls("Screenshot")
286 assert len(method_calls) > 0
287 _, args = method_calls[-1]
288 assert args[1] == app_id
289 assert args[2] == "" # parent window
290 assert args[3]["modal"] == modal
291 assert args[3]["interactive"] == interactive
292
293 @pytest.mark.parametrize("permission", ["", "yes", "no", "ask"])
294 def test_screenshot_permissions(
295 self, xdg_document_portal, portals, dbus_con, xdp_app_info, permission
296 ):
297 app_id = xdp_app_info.app_id
298 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
299 mock_intf = xdp.get_mock_iface(dbus_con)
300
301 self.set_permission(dbus_con, app_id, permission)
302
303 request = xdp.Request(dbus_con, screenshot_intf)
304 options = {
305 "modal": True,
306 "interactive": False,
307 "handle_token": request.handle_token,
308 }
309 response = request.call(
310 "Screenshot",
311 parent_window="",
312 options=options,
313 )
314
315 assert response
316 if permission == "no":
317 assert response.response != 0
318 return
319
320 assert response.response == 0
321 method_calls = mock_intf.GetMethodCalls("AccessDialog")
322 if permission == "yes":
323 assert len(method_calls) == 0
324 else:
325 assert len(method_calls) > 0
326
327 request = xdp.Request(dbus_con, screenshot_intf)
328 options["handle_token"] = request.handle_token
329 response = request.call(
330 "Screenshot",
331 parent_window="",
332 options=options,
333 )
334
335 assert response
336 assert response.response == 0
337 method_calls_2 = mock_intf.GetMethodCalls("AccessDialog")
338 if permission == "ask":
339 assert len(method_calls_2) > len(method_calls)
340 else:
341 assert len(method_calls_2) == len(method_calls)
342
343 def test_pick_color_basic(self, portals, dbus_con, xdp_app_info):
344 app_id = xdp_app_info.app_id
345 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
346 mock_intf = xdp.get_mock_iface(dbus_con)
347
348 request = xdp.Request(dbus_con, screenshot_intf)
349 options: Any = {}
350 response = request.call(
351 "PickColor",
352 parent_window="",
353 options=options,
354 )
355
356 assert response
357 assert response.response == 0
358 assert response.results["color"] == SCREENSHOT_DATA["color"]
359
360 # Check the impl portal was called with the right args
361 method_calls = mock_intf.GetMethodCalls("PickColor")
362 assert len(method_calls) > 0
363 _, args = method_calls[-1]
364 assert args[1] == app_id
365 assert args[2] == "" # parent window
366
367 @pytest.mark.parametrize(
368 "template_params", ({"screenshot": {"expect-close": True}},)
369 )
370 def test_pick_color_close(self, portals, dbus_con):
371 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
372
373 request = xdp.Request(dbus_con, screenshot_intf)
374 request.schedule_close(1000)
375 options: Any = {}
376 request.call(
377 "PickColor",
378 parent_window="",
379 options=options,
380 )
381
382 # Only true if the impl.Request was closed too
383 assert request.closed
384
385 @pytest.mark.parametrize("template_params", ({"screenshot": {"response": 1}},))
386 def test_pick_color_cancel(self, portals, dbus_con, xdp_app_info):
387 app_id = xdp_app_info.app_id
388 screenshot_intf = xdp.get_portal_iface(dbus_con, "Screenshot")
389 mock_intf = xdp.get_mock_iface(dbus_con)
390
391 request = xdp.Request(dbus_con, screenshot_intf)
392 options: Any = {}
393 response = request.call(
394 "PickColor",
395 parent_window="",
396 options=options,
397 )
398
399 assert response
400 assert response.response == 1
401
402 # Check the impl portal was called with the right args
403 method_calls = mock_intf.GetMethodCalls("PickColor")
404 assert len(method_calls) > 0
405 _, args = method_calls[-1]
406 assert args[1] == app_id
407 assert args[2] == "" # parent window