Debian Codegraph @

tests/test_notification.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 pytest
8 import tempfile
9 import os
10 from pathlib import Path
11 from gi.repository import GLib, Gio
12
13 SVG_IMAGE_DATA = """<?xml version="1.0" encoding="UTF-8"?>
14 <svg xmlns="http://www.w3.org/2000/svg" height="16px" width="16px"/>
15 """
16
17 SOUND_DATA = (
18 b"\x52\x49\x46\x46\x24\x00\x00\x00\x57\x41\x56\x45" +
19 b"\x66\x6d\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00" +
20 b"\x44\xac\x00\x00\x88\x58\x01\x00\x02\x00\x10\x00" +
21 b"\x64\x61\x74\x61\x00\x00\x00\x00"
22 ) # fmt: skip
23
24
25 SUPPORTED_OPTIONS = {
26 "foo": "bar",
27 }
28
29 NOTIFICATION_BASIC = {
30 "title": GLib.Variant("s", "title"),
31 "body": GLib.Variant("s", "test notification body"),
32 "priority": GLib.Variant("s", "normal"),
33 "default-action": GLib.Variant("s", "test-action"),
34 }
35
36 NOTIFICATION_BUTTONS = {
37 "title": GLib.Variant("s", "test notification 2"),
38 "body": GLib.Variant("s", "test notification body 2"),
39 "priority": GLib.Variant("s", "low"),
40 "default-action": GLib.Variant("s", "test-action"),
41 "buttons": GLib.Variant(
42 "aa{sv}",
43 [
44 {
45 "label": GLib.Variant("s", "button1"),
46 "action": GLib.Variant("s", "action1"),
47 },
48 {
49 "label": GLib.Variant("s", "button2"),
50 "action": GLib.Variant("s", "action2"),
51 },
52 ],
53 ),
54 }
55
56 ALL_VERSIONS_PARAMS = (
57 {"notification": {"version": 1}},
58 {"notification": {"version": 2}},
59 )
60
61
62 @pytest.fixture
63 def required_templates():
64 return {
65 "notification": {
66 "SupportedOptions": SUPPORTED_OPTIONS,
67 },
68 }
69
70
71 class NotificationPortal(xdp.GDBusIface):
72 def __init__(self):
73 super().__init__(
74 "org.freedesktop.portal.Desktop",
75 "/org/freedesktop/portal/desktop",
76 "org.freedesktop.portal.Notification",
77 )
78
79 def AddNotification(self, id, notification, fds=[]):
80 return self._call(
81 "AddNotification",
82 GLib.Variant("(sa{sv})", (id, notification)),
83 fds,
84 )
85
86 def RemoveNotification(self, id):
87 return self._call(
88 "RemoveNotification",
89 GLib.Variant("(s)", (id,)),
90 )
91
92
93 class TestNotification:
94 def add_notification(self, dbus_con, app_id, id, notification, fds=[]):
95 notification_intf = NotificationPortal()
96 mock_intf = xdp.get_mock_iface(dbus_con)
97
98 method_calls = mock_intf.GetMethodCalls("AddNotification")
99 backend_calls = len(method_calls)
100
101 notification_intf.AddNotification(id, notification, fds)
102
103 # Check the impl portal was called with the right args
104 method_calls = mock_intf.GetMethodCalls("AddNotification")
105 assert len(method_calls) == backend_calls + 1
106 _, args = method_calls[-1]
107 assert args[0] == app_id
108 assert args[1] == id
109
110 return args[2]
111
112 def check_notification(
113 self, dbus_con, app_id, id, notification_in, notification_expected
114 ):
115 mock_notification = self.add_notification(dbus_con, app_id, id, notification_in)
116 self.compare_notification(mock_notification, notification_expected)
117
118 def compare_notification(self, mock_notification, notification_expected):
119 assert (
120 mock_notification == GLib.Variant("a{sv}", notification_expected).unpack()
121 )
122
123 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
124 def test_version(self, portals, dbus_con, template_params):
125 xdp.check_version(
126 dbus_con, "Notification", template_params["notification"]["version"]
127 )
128
129 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
130 def test_basic(self, portals, dbus_con, xdp_app_info):
131 app_id = xdp_app_info.app_id
132
133 self.check_notification(
134 dbus_con,
135 app_id,
136 "test1",
137 NOTIFICATION_BASIC,
138 NOTIFICATION_BASIC,
139 )
140
141 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
142 def test_remove(self, portals, dbus_con, xdp_app_info):
143 app_id = xdp_app_info.app_id
144
145 notification_intf = NotificationPortal()
146 mock_intf = xdp.get_mock_iface(dbus_con)
147
148 id = "test1"
149
150 notification_intf.AddNotification(id, NOTIFICATION_BASIC)
151 method_calls = mock_intf.GetMethodCalls("AddNotification")
152 assert len(method_calls) == 1
153 _, args = method_calls[-1]
154 assert args[0] == app_id
155 assert args[1] == id
156
157 notification_intf.RemoveNotification(id)
158 method_calls = mock_intf.GetMethodCalls("RemoveNotification")
159 assert len(method_calls) == 1
160 _, args = method_calls[-1]
161 assert args[0] == app_id
162 assert args[1] == id
163
164 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
165 def test_buttons(self, portals, dbus_con, xdp_app_info):
166 app_id = xdp_app_info.app_id
167
168 self.check_notification(
169 dbus_con,
170 app_id,
171 "test1",
172 NOTIFICATION_BUTTONS,
173 NOTIFICATION_BUTTONS,
174 )
175
176 def test_markup(self, portals, dbus_con, xdp_app_info):
177 app_id = xdp_app_info.app_id
178
179 bodies = [
180 (
181 "test <b>notification</b> body <i>italic</i>",
182 "test <b>notification</b> body <i>italic</i>",
183 ),
184 (
185 'test <a href="https://example.com"><b>Some link</b></a>',
186 'test <a href="https://example.com"><b>Some link</b></a>',
187 ),
188 (
189 "&lt;html&gt;",
190 "&lt;html&gt;",
191 ),
192 (
193 '<a href="https://xkcd.com/327/#&quot;&gt;&lt;html&gt;"></a>',
194 '<a href="https://xkcd.com/327/#&quot;&gt;&lt;html&gt;"></a>',
195 ),
196 (
197 "test \n newline \n\n some more space \n with trailing space ",
198 "test newline some more space with trailing space",
199 ),
200 (
201 "test <custom> tag </custom>",
202 "test tag",
203 ),
204 (
205 "test <b>notification<b> body",
206 False,
207 ),
208 (
209 "<b>foo<i>bar</b></i>",
210 False,
211 ),
212 (
213 "test <markup><i>notification</i><markup> body",
214 False,
215 ),
216 ]
217
218 i = 0
219 for body_in, body_expected in bodies:
220 notification_in = NOTIFICATION_BASIC.copy()
221 notification_in["markup-body"] = GLib.Variant("s", body_in)
222
223 notification_expected = NOTIFICATION_BASIC.copy()
224 if body_expected:
225 notification_expected["markup-body"] = GLib.Variant("s", body_expected)
226
227 try:
228 self.check_notification(
229 dbus_con,
230 app_id,
231 f"test{i}",
232 notification_in,
233 notification_expected,
234 )
235 assert body_expected
236 except GLib.GError as e:
237 assert "invalid markup-body" in e.message
238
239 i += 1
240
241 def test_bad_arg(self, portals, dbus_con, xdp_app_info):
242 app_id = xdp_app_info.app_id
243 notification = NOTIFICATION_BASIC.copy()
244 notification["bodx"] = GLib.Variant("s", "Xtest")
245
246 self.check_notification(
247 dbus_con,
248 app_id,
249 "test1",
250 notification,
251 NOTIFICATION_BASIC,
252 )
253
254 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
255 def test_bad_priority(self, portals, dbus_con, xdp_app_info):
256 app_id = xdp_app_info.app_id
257 notification = NOTIFICATION_BASIC.copy()
258 notification["priority"] = GLib.Variant("s", "invalid")
259
260 try:
261 self.check_notification(
262 dbus_con,
263 app_id,
264 "test1",
265 notification,
266 notification,
267 )
268 assert False, "This statement should not be reached"
269 except GLib.GError as e:
270 assert "invalid not a priority" in e.message
271
272 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
273 def test_bad_button(self, portals, dbus_con, xdp_app_info):
274 app_id = xdp_app_info.app_id
275 notification = NOTIFICATION_BUTTONS.copy()
276 notification["buttons"] = GLib.Variant(
277 "aa{sv}",
278 [
279 {
280 "labex": GLib.Variant("s", "button1"),
281 "action": GLib.Variant("s", "action1"),
282 },
283 ],
284 )
285
286 try:
287 self.check_notification(
288 dbus_con,
289 app_id,
290 "test1",
291 notification,
292 notification,
293 )
294 assert False, "This statement should not be reached"
295 except GLib.GError as e:
296 assert "invalid button" in e.message
297
298 def test_display_hint(self, portals, dbus_con, xdp_app_info):
299 app_id = xdp_app_info.app_id
300 notification = NOTIFICATION_BASIC.copy()
301 notification["display-hint"] = GLib.Variant(
302 "as",
303 [
304 "transient",
305 "show-as-new",
306 ],
307 )
308
309 self.check_notification(
310 dbus_con,
311 app_id,
312 "test1",
313 notification,
314 notification,
315 )
316
317 notification = NOTIFICATION_BASIC.copy()
318 notification["display-hint"] = GLib.Variant(
319 "as",
320 [
321 "unsupported-hint",
322 ],
323 )
324
325 try:
326 self.check_notification(
327 dbus_con,
328 app_id,
329 "test1",
330 notification,
331 notification,
332 )
333 assert False, "This statement should not be reached"
334 except GLib.GError as e:
335 assert "not a display-hint" in e.message
336
337 def test_category(self, portals, dbus_con, xdp_app_info):
338 app_id = xdp_app_info.app_id
339 notification = NOTIFICATION_BASIC.copy()
340 notification["category"] = GLib.Variant("s", "im.received")
341
342 self.check_notification(
343 dbus_con,
344 app_id,
345 "test1",
346 notification,
347 notification,
348 )
349
350 notification = NOTIFICATION_BASIC.copy()
351 notification["category"] = GLib.Variant("s", "x-vendor.custom")
352
353 self.check_notification(
354 dbus_con,
355 app_id,
356 "test1",
357 notification,
358 notification,
359 )
360
361 notification = NOTIFICATION_BASIC.copy()
362 notification["category"] = GLib.Variant("s", "unsupported-type")
363
364 try:
365 self.check_notification(
366 dbus_con,
367 app_id,
368 "test1",
369 notification,
370 notification,
371 )
372 assert False, "This statement should not be reached"
373 except GLib.GError as e:
374 assert "not a supported category" in e.message
375
376 def test_supported_options(self, portals, dbus_con):
377 properties_intf = xdp.get_iface(dbus_con, "org.freedesktop.DBus.Properties")
378
379 options = properties_intf.Get(
380 "org.freedesktop.portal.Notification", "SupportedOptions"
381 )
382
383 assert options == SUPPORTED_OPTIONS
384
385 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
386 def test_icon_themed(self, portals, dbus_con, xdp_app_info):
387 notification = NOTIFICATION_BASIC.copy()
388 notification["icon"] = GLib.Variant(
389 "(sv)",
390 (
391 "themed",
392 GLib.Variant("as", ["test-icon-symbolic"]),
393 ),
394 )
395
396 self.check_notification(
397 dbus_con,
398 xdp_app_info.app_id,
399 "test1",
400 notification,
401 notification,
402 )
403
404 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
405 def test_icon_themed_string(self, portals, dbus_con, xdp_app_info):
406 notification = NOTIFICATION_BASIC.copy()
407 icon = Gio.ThemedIcon.new("test-icon-symbolic")
408 notification["icon"] = GLib.Variant("s", icon.get_names()[0])
409
410 expected = notification.copy()
411 expected["icon"] = GLib.Variant(
412 "(sv)",
413 (
414 "themed",
415 GLib.Variant("as", icon.get_names()),
416 ),
417 )
418
419 self.check_notification(
420 dbus_con,
421 xdp_app_info.app_id,
422 "test1",
423 notification,
424 expected,
425 )
426
427 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
428 def test_icon_bytes(self, portals, dbus_con, xdp_app_info, template_params):
429 image_bytes = SVG_IMAGE_DATA.encode("utf-8")
430 notification = NOTIFICATION_BASIC.copy()
431 notification["icon"] = GLib.Variant(
432 "(sv)",
433 (
434 "bytes",
435 GLib.Variant("ay", image_bytes),
436 ),
437 )
438
439 added_notification = self.add_notification(
440 dbus_con,
441 xdp_app_info.app_id,
442 "test1",
443 notification,
444 )
445
446 if template_params["notification"]["version"] == 1:
447 self.compare_notification(added_notification, notification)
448 return
449
450 assert "icon" in added_notification
451 assert added_notification["icon"][0] == "file-descriptor"
452 assert added_notification["icon"][1] is not None
453
454 mock_fd = added_notification["icon"][1]
455 mock_fd = mock_fd.take()
456
457 fd_contents = os.read(mock_fd, 1000)
458 assert fd_contents == image_bytes
459
460 os.close(mock_fd)
461
462 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
463 def test_icon_file(self, portals, dbus_con, xdp_app_info):
464 fd, file_path = tempfile.mkstemp(prefix="notification_icon_", dir=Path.home())
465 os.write(fd, SVG_IMAGE_DATA.encode("utf-8"))
466
467 file = Gio.File.new_for_path(file_path)
468 icon = Gio.FileIcon.new(file)
469
470 notification = NOTIFICATION_BASIC.copy()
471 notification["icon"] = icon.serialize()
472
473 added_notification = self.add_notification(
474 dbus_con,
475 xdp_app_info.app_id,
476 "test1",
477 notification,
478 )
479
480 assert "icon" not in added_notification
481
482 def test_icon_fd(self, portals, dbus_con, xdp_app_info):
483 svg_image_bytes = SVG_IMAGE_DATA.encode("utf-8")
484
485 fd = os.memfd_create("notification_sound_test", os.MFD_ALLOW_SEALING)
486 os.write(fd, svg_image_bytes)
487
488 notification = NOTIFICATION_BASIC.copy()
489 notification["icon"] = GLib.Variant(
490 "(sv)",
491 (
492 "file-descriptor",
493 GLib.Variant("h", 0),
494 ),
495 )
496
497 added_notification = self.add_notification(
498 dbus_con,
499 xdp_app_info.app_id,
500 "test1",
501 notification,
502 [fd],
503 )
504
505 assert added_notification["icon"][0] == "file-descriptor"
506 mock_fd = added_notification["icon"][1]
507 mock_fd = mock_fd.take()
508
509 os.lseek(fd, 0, os.SEEK_SET)
510 fd_contents = os.read(mock_fd, 1000)
511 assert fd_contents == svg_image_bytes
512
513 os.close(mock_fd)
514 os.close(fd)
515
516 @pytest.mark.parametrize("template_params", ALL_VERSIONS_PARAMS)
517 def test_icon_bad(self, portals, dbus_con):
518 notification_intf = NotificationPortal()
519
520 notification = NOTIFICATION_BASIC.copy()
521
522 bad_icons = [
523 GLib.Variant("(sv)", ["themed", GLib.Variant("s", "test-icon-symbolic")]),
524 GLib.Variant(
525 "(sv)",
526 ["bytes", GLib.Variant("as", ["test-icon-symbolic", "test-icon"])],
527 ),
528 GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("s", "")]),
529 GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("h", 0)]),
530 ]
531
532 for icon in bad_icons:
533 notification["icon"] = icon
534 try:
535 notification_intf.AddNotification("test1", notification)
536 assert False, "This statement should not be reached"
537 except GLib.GError as e:
538 assert e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR)
539
540 def test_sound_simple(self, portals, dbus_con, xdp_app_info):
541 app_id = xdp_app_info.app_id
542 notification = NOTIFICATION_BASIC.copy()
543 notification["sound"] = GLib.Variant("s", "default")
544
545 self.check_notification(
546 dbus_con,
547 app_id,
548 "test1",
549 notification,
550 notification,
551 )
552
553 notification = NOTIFICATION_BASIC.copy()
554 notification["sound"] = GLib.Variant("s", "silent")
555
556 self.check_notification(
557 dbus_con,
558 app_id,
559 "test1",
560 notification,
561 notification,
562 )
563
564 notification = NOTIFICATION_BASIC.copy()
565 notification["sound"] = GLib.Variant("s", "bad")
566
567 try:
568 self.check_notification(
569 dbus_con,
570 app_id,
571 "test1",
572 notification,
573 notification,
574 )
575 assert False, "This statement should not be reached"
576 except GLib.GError as e:
577 assert "invalid sound: invalid option" in e.message
578
579 def test_sound_file(self, portals, dbus_con, xdp_app_info):
580 fd, file_path = tempfile.mkstemp(prefix="notification_sound_", dir=Path.home())
581 os.write(fd, SOUND_DATA)
582
583 file = Gio.File.new_for_path(file_path)
584
585 notification = NOTIFICATION_BASIC.copy()
586 notification["sound"] = GLib.Variant(
587 "(sv)",
588 (
589 "file",
590 GLib.Variant("s", file.get_uri()),
591 ),
592 )
593
594 mock_notification = self.add_notification(
595 dbus_con,
596 xdp_app_info.app_id,
597 "test1",
598 notification,
599 )
600
601 assert "sound" not in mock_notification
602
603 def test_sound_fd(self, portals, dbus_con, xdp_app_info):
604 fd = os.memfd_create("notification_sound_test", os.MFD_ALLOW_SEALING)
605 os.write(fd, SOUND_DATA)
606
607 notification = NOTIFICATION_BASIC.copy()
608 notification["sound"] = GLib.Variant(
609 "(sv)",
610 (
611 "file-descriptor",
612 GLib.Variant("h", 0),
613 ),
614 )
615
616 mock_notification = self.add_notification(
617 dbus_con,
618 xdp_app_info.app_id,
619 "test1",
620 notification,
621 [fd],
622 )
623
624 assert mock_notification["sound"][0] == "file-descriptor"
625 mock_fd = mock_notification["sound"][1]
626 mock_fd = mock_fd.take()
627
628 os.lseek(fd, 0, os.SEEK_SET)
629 fd_contents = os.read(mock_fd, 1000)
630 assert fd_contents == SOUND_DATA
631
632 os.close(mock_fd)
633 os.close(fd)
634
635 def test_sound_bad(self, portals, dbus_con):
636 notification_intf = NotificationPortal()
637
638 notification = NOTIFICATION_BASIC.copy()
639
640 bad_sounds = [
641 # bad type
642 GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("s", "")]),
643 # not sending the FD for the handle
644 GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("h", 13)]),
645 ]
646
647 for sound in bad_sounds:
648 notification["sound"] = sound
649 try:
650 notification_intf.AddNotification("test1", notification)
651 assert False, "This statement should not be reached"
652 except GLib.GError as e:
653 assert e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR)
654 pass