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