Debian Codegraph @

tests/test_filechooser.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 import os
10 from pathlib import Path
11
12
13 FILECHOOSER_RESULTS = {
14 "uris": ["FILLED OUT LATER"],
15 "choices": [("encoding", "utf8"), ("reencode", "true"), ("third", "a")],
16 }
17
18
19 @pytest.fixture
20 def required_templates():
21 test_file1 = Path(os.environ["XDG_DATA_HOME"]) / "test1.txt"
22 test_file1.write_text("test1")
23 test_file2 = Path(os.environ["XDG_DATA_HOME"]) / "test2.txt"
24 test_file2.write_text("test2")
25
26 FILECHOOSER_RESULTS["uris"] = [
27 f"file://{test_file1.absolute().as_posix()}",
28 f"file://{test_file2.absolute().as_posix()}",
29 ]
30
31 return {
32 "filechooser": {
33 "results": dbus.Dictionary(FILECHOOSER_RESULTS, signature="sv"),
34 },
35 "lockdown": {},
36 }
37
38
39 class TestFilechooser:
40 def test_version(self, portals, dbus_con):
41 xdp.check_version(dbus_con, "FileChooser", 4)
42
43 def test_open_file_basic(self, portals, dbus_con, xdp_app_info):
44 app_id = xdp_app_info.app_id
45 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
46 mock_intf = xdp.get_mock_iface(dbus_con)
47
48 title = "Test"
49 accept_label = "Accept"
50 multiple = True
51 options = {
52 "accept_label": accept_label,
53 "multiple": multiple,
54 }
55 request = xdp.Request(dbus_con, filechooser_intf)
56 response = request.call(
57 "OpenFile",
58 parent_window="",
59 title=title,
60 options=options,
61 )
62
63 assert response
64 assert response.response == 0
65 assert xdp.uris_same_files(
66 FILECHOOSER_RESULTS["uris"], response.results["uris"]
67 )
68
69 # Check the impl portal was called with the right args
70 method_calls = mock_intf.GetMethodCalls("OpenFile")
71 assert len(method_calls) == 1
72 _, args = method_calls.pop()
73 assert args[1] == app_id
74 assert args[2] == "" # parent window
75 assert args[3] == title
76 assert args[4]["accept_label"] == accept_label
77 assert args[4]["multiple"] == multiple
78
79 @pytest.mark.parametrize("template_params", ({"filechooser": {"response": 1}},))
80 def test_open_file_cancel(self, portals, dbus_con, xdp_app_info):
81 app_id = xdp_app_info.app_id
82 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
83 mock_intf = xdp.get_mock_iface(dbus_con)
84
85 title = "Test"
86 accept_label = "Accept"
87 multiple = True
88 options = {
89 "accept_label": accept_label,
90 "multiple": multiple,
91 }
92 request = xdp.Request(dbus_con, filechooser_intf)
93 response = request.call(
94 "OpenFile",
95 parent_window="",
96 title=title,
97 options=options,
98 )
99
100 assert response
101 assert response.response == 1
102
103 # Check the impl portal was called with the right args
104 method_calls = mock_intf.GetMethodCalls("OpenFile")
105 assert len(method_calls) == 1
106 _, args = method_calls.pop()
107 assert args[1] == app_id
108 assert args[2] == "" # parent window
109 assert args[3] == title
110 assert args[4]["accept_label"] == accept_label
111 assert args[4]["multiple"] == multiple
112
113 @pytest.mark.parametrize(
114 "template_params", ({"filechooser": {"expect-close": True}},)
115 )
116 def test_open_file_close(self, portals, dbus_con, xdp_app_info):
117 app_id = xdp_app_info.app_id
118 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
119 mock_intf = xdp.get_mock_iface(dbus_con)
120
121 title = "Test"
122 accept_label = "Accept"
123 multiple = True
124 options = {
125 "accept_label": accept_label,
126 "multiple": multiple,
127 }
128 request = xdp.Request(dbus_con, filechooser_intf)
129 request.schedule_close(1000)
130 request.call(
131 "OpenFile",
132 parent_window="",
133 title=title,
134 options=options,
135 )
136
137 # Only true if the impl.Request was closed too
138 assert request.closed
139
140 # Check the impl portal was called with the right args
141 method_calls = mock_intf.GetMethodCalls("OpenFile")
142 assert len(method_calls) == 1
143 _, args = method_calls.pop()
144 assert args[1] == app_id
145 assert args[2] == "" # parent window
146 assert args[3] == title
147 assert args[4]["accept_label"] == accept_label
148 assert args[4]["multiple"] == multiple
149
150 def test_open_file_filter1(self, portals, dbus_con):
151 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
152 mock_intf = xdp.get_mock_iface(dbus_con)
153
154 options = {
155 "filters": [
156 (
157 "Images",
158 [
159 (dbus.UInt32(0), "*ico"),
160 (dbus.UInt32(1), "image/png"),
161 ],
162 ),
163 (
164 "Text",
165 [
166 (dbus.UInt32(0), "*.txt"),
167 ],
168 ),
169 ],
170 }
171 request = xdp.Request(dbus_con, filechooser_intf)
172 response = request.call(
173 "OpenFile",
174 parent_window="",
175 title="Test",
176 options=options,
177 )
178
179 assert response
180 assert response.response == 0
181 assert xdp.uris_same_files(
182 FILECHOOSER_RESULTS["uris"], response.results["uris"]
183 )
184
185 method_calls = mock_intf.GetMethodCalls("OpenFile")
186 assert len(method_calls) == 1
187
188 def test_open_file_filter2(self, portals, dbus_con):
189 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
190
191 options = {
192 "filters": [
193 (
194 "Text",
195 [
196 # Invalid filter type
197 (dbus.UInt32(4), "*.txt"),
198 ],
199 ),
200 ],
201 }
202 request = xdp.Request(dbus_con, filechooser_intf)
203 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
204 request.call(
205 "OpenFile",
206 parent_window="",
207 title="Test",
208 options=options,
209 )
210 assert (
211 excinfo.value.get_dbus_name()
212 == "org.freedesktop.portal.Error.InvalidArgument"
213 )
214
215 def test_open_file_current_filter1(self, portals, dbus_con):
216 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
217 mock_intf = xdp.get_mock_iface(dbus_con)
218
219 options = {
220 "filters": [
221 (
222 "Images",
223 [
224 (dbus.UInt32(0), "*ico"),
225 (dbus.UInt32(1), "image/png"),
226 ],
227 ),
228 (
229 "Text",
230 [
231 (dbus.UInt32(0), "*.txt"),
232 ],
233 ),
234 ],
235 "current_filter": (
236 "Text",
237 [
238 (dbus.UInt32(0), "*.txt"),
239 ],
240 ),
241 }
242 request = xdp.Request(dbus_con, filechooser_intf)
243 response = request.call(
244 "OpenFile",
245 parent_window="",
246 title="Test",
247 options=options,
248 )
249
250 assert response
251 assert response.response == 0
252 assert xdp.uris_same_files(
253 FILECHOOSER_RESULTS["uris"], response.results["uris"]
254 )
255
256 method_calls = mock_intf.GetMethodCalls("OpenFile")
257 assert len(method_calls) == 1
258 _, args = method_calls.pop()
259 assert args[4]["current_filter"] == options["current_filter"]
260
261 def test_open_file_current_filter2(self, portals, dbus_con):
262 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
263 mock_intf = xdp.get_mock_iface(dbus_con)
264
265 options = {
266 "current_filter": (
267 "Text",
268 [
269 (dbus.UInt32(0), "*.txt"),
270 ],
271 ),
272 }
273 request = xdp.Request(dbus_con, filechooser_intf)
274 response = request.call(
275 "OpenFile",
276 parent_window="",
277 title="Test",
278 options=options,
279 )
280
281 assert response
282 assert response.response == 0
283 assert xdp.uris_same_files(
284 FILECHOOSER_RESULTS["uris"], response.results["uris"]
285 )
286
287 method_calls = mock_intf.GetMethodCalls("OpenFile")
288 assert len(method_calls) == 1
289 _, args = method_calls.pop()
290 assert args[4]["current_filter"] == options["current_filter"]
291
292 def test_open_file_current_filter3(self, portals, dbus_con):
293 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
294
295 options = {
296 "current_filter": (
297 "Text",
298 [
299 # Invalid filter type
300 (dbus.UInt32(6), "*.txt"),
301 ],
302 ),
303 }
304 request = xdp.Request(dbus_con, filechooser_intf)
305 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
306 request.call(
307 "OpenFile",
308 parent_window="",
309 title="Test",
310 options=options,
311 )
312 assert (
313 excinfo.value.get_dbus_name()
314 == "org.freedesktop.portal.Error.InvalidArgument"
315 )
316
317 def test_open_file_current_filter4(self, portals, dbus_con):
318 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
319
320 options = {
321 "filters": [
322 (
323 "Images",
324 [
325 (dbus.UInt32(0), "*ico"),
326 (dbus.UInt32(1), "image/png"),
327 ],
328 ),
329 (
330 "Text",
331 [
332 (dbus.UInt32(0), "*.txt"),
333 ],
334 ),
335 ],
336 "current_filter": (
337 "Something else",
338 [
339 (dbus.UInt32(0), "*.sth.else"),
340 ],
341 ),
342 }
343 request = xdp.Request(dbus_con, filechooser_intf)
344 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
345 request.call(
346 "OpenFile",
347 parent_window="",
348 title="Test",
349 options=options,
350 )
351 assert (
352 excinfo.value.get_dbus_name()
353 == "org.freedesktop.portal.Error.InvalidArgument"
354 )
355
356 def test_open_file_choices1(self, portals, dbus_con):
357 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
358 mock_intf = xdp.get_mock_iface(dbus_con)
359
360 options = {
361 "choices": [
362 (
363 "encoding",
364 "Encoding",
365 [
366 ("utf8", "Unicode"),
367 ("latin15", "Western"),
368 ],
369 "latin15",
370 ),
371 (
372 "reencode",
373 "Reencode",
374 [],
375 "false",
376 ),
377 (
378 "third",
379 "Third",
380 [("a", "A"), ("b", "B")],
381 "",
382 ),
383 ],
384 }
385 request = xdp.Request(dbus_con, filechooser_intf)
386 response = request.call(
387 "OpenFile",
388 parent_window="",
389 title="Test",
390 options=options,
391 )
392
393 assert response
394 assert response.response == 0
395 assert xdp.uris_same_files(
396 FILECHOOSER_RESULTS["uris"], response.results["uris"]
397 )
398
399 method_calls = mock_intf.GetMethodCalls("OpenFile")
400 assert len(method_calls) == 1
401 _, args = method_calls.pop()
402 assert args[4]["choices"] == options["choices"]
403
404 def test_open_file_choices_invalid(self, portals, dbus_con):
405 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
406
407 invalid_choices = [
408 (
409 "encoding",
410 "Encoding",
411 [
412 ("utf8", ""),
413 ("latin15", "Western"),
414 ],
415 "latin15",
416 ),
417 (
418 "encoding",
419 "Encoding",
420 [
421 ("", "Unicode"),
422 ("latin15", "Western"),
423 ],
424 "latin15",
425 ),
426 (
427 "",
428 "Encoding",
429 [
430 ("utf8", "Unicode"),
431 ("latin15", "Western"),
432 ],
433 "latin15",
434 ),
435 ]
436
437 for choice in invalid_choices:
438 request = xdp.Request(dbus_con, filechooser_intf)
439 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
440 options = {
441 "choices": [choice],
442 }
443 request.call(
444 "OpenFile",
445 parent_window="",
446 title="Test",
447 options=options,
448 )
449 assert (
450 excinfo.value.get_dbus_name()
451 == "org.freedesktop.portal.Error.InvalidArgument"
452 )
453
454 def test_save_file_basic(self, portals, dbus_con, xdp_app_info):
455 app_id = xdp_app_info.app_id
456 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
457 mock_intf = xdp.get_mock_iface(dbus_con)
458
459 title = "Test"
460 accept_label = "Accept"
461 current_name = "File Name"
462 options = {
463 "accept_label": accept_label,
464 "current_name": current_name,
465 }
466 request = xdp.Request(dbus_con, filechooser_intf)
467 response = request.call(
468 "SaveFile",
469 parent_window="",
470 title=title,
471 options=options,
472 )
473
474 assert response
475 assert response.response == 0
476 assert xdp.uris_same_files(
477 FILECHOOSER_RESULTS["uris"], response.results["uris"]
478 )
479
480 # Check the impl portal was called with the right args
481 method_calls = mock_intf.GetMethodCalls("SaveFile")
482 assert len(method_calls) == 1
483 _, args = method_calls.pop()
484 assert args[1] == app_id
485 assert args[2] == "" # parent window
486 assert args[3] == title
487 assert args[4]["accept_label"] == accept_label
488 assert args[4]["current_name"] == current_name
489
490 @pytest.mark.parametrize("template_params", ({"filechooser": {"response": 1}},))
491 def test_save_file_cancel(self, portals, dbus_con, xdp_app_info):
492 app_id = xdp_app_info.app_id
493 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
494 mock_intf = xdp.get_mock_iface(dbus_con)
495
496 title = "Test"
497 accept_label = "Accept"
498 current_name = "File Name"
499 options = {
500 "accept_label": accept_label,
501 "current_name": current_name,
502 }
503 request = xdp.Request(dbus_con, filechooser_intf)
504 response = request.call(
505 "SaveFile",
506 parent_window="",
507 title=title,
508 options=options,
509 )
510
511 assert response
512 assert response.response == 1
513
514 # Check the impl portal was called with the right args
515 method_calls = mock_intf.GetMethodCalls("SaveFile")
516 assert len(method_calls) == 1
517 _, args = method_calls.pop()
518 assert args[1] == app_id
519 assert args[2] == "" # parent window
520 assert args[3] == title
521 assert args[4]["accept_label"] == accept_label
522 assert args[4]["current_name"] == current_name
523
524 @pytest.mark.parametrize(
525 "template_params", ({"filechooser": {"expect-close": True}},)
526 )
527 def test_save_file_close(self, portals, dbus_con, xdp_app_info):
528 app_id = xdp_app_info.app_id
529 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
530 mock_intf = xdp.get_mock_iface(dbus_con)
531
532 title = "Test"
533 accept_label = "Accept"
534 current_name = "File Name"
535 options = {
536 "accept_label": accept_label,
537 "current_name": current_name,
538 }
539 request = xdp.Request(dbus_con, filechooser_intf)
540 request.schedule_close(1000)
541 request.call(
542 "SaveFile",
543 parent_window="",
544 title=title,
545 options=options,
546 )
547
548 # Only true if the impl.Request was closed too
549 assert request.closed
550
551 # Check the impl portal was called with the right args
552 method_calls = mock_intf.GetMethodCalls("SaveFile")
553 assert len(method_calls) == 1
554 _, args = method_calls.pop()
555 assert args[1] == app_id
556 assert args[2] == "" # parent window
557 assert args[3] == title
558 assert args[4]["accept_label"] == accept_label
559 assert args[4]["current_name"] == current_name
560
561 def test_save_file_filters(self, portals, dbus_con):
562 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
563 mock_intf = xdp.get_mock_iface(dbus_con)
564
565 options = {
566 "filters": [
567 (
568 "Images",
569 [
570 (dbus.UInt32(0), "*ico"),
571 (dbus.UInt32(1), "image/png"),
572 ],
573 ),
574 (
575 "Text",
576 [
577 (dbus.UInt32(0), "*.txt"),
578 ],
579 ),
580 ],
581 }
582 request = xdp.Request(dbus_con, filechooser_intf)
583 response = request.call(
584 "SaveFile",
585 parent_window="",
586 title="Title",
587 options=options,
588 )
589
590 assert response
591 assert response.response == 0
592 assert xdp.uris_same_files(
593 FILECHOOSER_RESULTS["uris"], response.results["uris"]
594 )
595
596 # Check the impl portal was called with the right args
597 method_calls = mock_intf.GetMethodCalls("SaveFile")
598 assert len(method_calls) == 1
599 _, args = method_calls.pop()
600 assert args[4]["filters"] == options["filters"]
601
602 @pytest.mark.parametrize(
603 "template_params", ({"lockdown": {"disable-save-to-disk": True}},)
604 )
605 def test_save_file_lockdown(self, portals, dbus_con):
606 filechooser_intf = xdp.get_portal_iface(dbus_con, "FileChooser")
607 mock_intf = xdp.get_mock_iface(dbus_con)
608
609 request = xdp.Request(dbus_con, filechooser_intf)
610 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
611 request.call(
612 "SaveFile",
613 parent_window="",
614 title="Title",
615 options={},
616 )
617 assert (
618 excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed"
619 )
620
621 # Check the impl portal was not called
622 method_calls = mock_intf.GetMethodCalls("FileChooser")
623 assert len(method_calls) == 0