| 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 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 |
|
| 57 |
@pytest.fixture |
| 58 |
def required_templates(): |
| 59 |
return { |
| 60 |
"notification": { |
| 61 |
"SupportedOptions": SUPPORTED_OPTIONS, |
| 62 |
}, |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
class NotificationPortal(xdp.GDBusIface): |
| 67 |
def __init__(self): |
| 68 |
super().__init__( |
| 69 |
"org.freedesktop.portal.Desktop", |
| 70 |
"/org/freedesktop/portal/desktop", |
| 71 |
"org.freedesktop.portal.Notification", |
| 72 |
) |
| 73 |
|
| 74 |
def AddNotification(self, id, notification, fds=[]): |
| 75 |
return self._call( |
| 76 |
"AddNotification", |
| 77 |
GLib.Variant("(sa{sv})", (id, notification)), |
| 78 |
fds, |
| 79 |
) |
| 80 |
|
| 81 |
def RemoveNotification(self, id): |
| 82 |
return self._call( |
| 83 |
"RemoveNotification", |
| 84 |
GLib.Variant("(s)", (id,)), |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
class TestNotification: |
| 89 |
def check_notification( |
| 90 |
self, dbus_con, app_id, id, notification_in, notification_expected |
| 91 |
): |
| 92 |
notification_intf = NotificationPortal() |
| 93 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 94 |
|
| 95 |
method_calls = mock_intf.GetMethodCalls("AddNotification") |
| 96 |
backend_calls = len(method_calls) |
| 97 |
|
| 98 |
notification_intf.AddNotification(id, notification_in) |
| 99 |
|
| 100 |
# Check the impl portal was called with the right args |
| 101 |
method_calls = mock_intf.GetMethodCalls("AddNotification") |
| 102 |
assert len(method_calls) == backend_calls + 1 |
| 103 |
_, args = method_calls[-1] |
| 104 |
assert args[0] == app_id |
| 105 |
assert args[1] == id |
| 106 |
|
| 107 |
mock_notification = args[2] |
| 108 |
assert ( |
| 109 |
mock_notification == GLib.Variant("a{sv}", notification_expected).unpack() |
| 110 |
) |
| 111 |
|
| 112 |
def test_version(self, portals, dbus_con): |
| 113 |
xdp.check_version(dbus_con, "Notification", 2) |
| 114 |
|
| 115 |
def test_basic(self, portals, dbus_con, app_id): |
| 116 |
self.check_notification( |
| 117 |
dbus_con, |
| 118 |
app_id, |
| 119 |
"test1", |
| 120 |
NOTIFICATION_BASIC, |
| 121 |
NOTIFICATION_BASIC, |
| 122 |
) |
| 123 |
|
| 124 |
def test_remove(self, portals, dbus_con, app_id): |
| 125 |
notification_intf = NotificationPortal() |
| 126 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 127 |
|
| 128 |
id = "test1" |
| 129 |
|
| 130 |
notification_intf.AddNotification(id, NOTIFICATION_BASIC) |
| 131 |
method_calls = mock_intf.GetMethodCalls("AddNotification") |
| 132 |
assert len(method_calls) == 1 |
| 133 |
_, args = method_calls[-1] |
| 134 |
assert args[0] == app_id |
| 135 |
assert args[1] == id |
| 136 |
|
| 137 |
notification_intf.RemoveNotification(id) |
| 138 |
method_calls = mock_intf.GetMethodCalls("RemoveNotification") |
| 139 |
assert len(method_calls) == 1 |
| 140 |
_, args = method_calls[-1] |
| 141 |
assert args[0] == app_id |
| 142 |
assert args[1] == id |
| 143 |
|
| 144 |
def test_buttons(self, portals, dbus_con, app_id): |
| 145 |
self.check_notification( |
| 146 |
dbus_con, |
| 147 |
app_id, |
| 148 |
"test1", |
| 149 |
NOTIFICATION_BUTTONS, |
| 150 |
NOTIFICATION_BUTTONS, |
| 151 |
) |
| 152 |
|
| 153 |
def test_markup(self, portals, dbus_con, app_id): |
| 154 |
bodies = [ |
| 155 |
( |
| 156 |
"test <b>notification</b> body <i>italic</i>", |
| 157 |
"test <b>notification</b> body <i>italic</i>", |
| 158 |
), |
| 159 |
( |
| 160 |
'test <a href="https://example.com"><b>Some link</b></a>', |
| 161 |
'test <a href="https://example.com"><b>Some link</b></a>', |
| 162 |
), |
| 163 |
( |
| 164 |
"<html>", |
| 165 |
"<html>", |
| 166 |
), |
| 167 |
( |
| 168 |
'<a href="https://xkcd.com/327/#"><html>"></a>', |
| 169 |
'<a href="https://xkcd.com/327/#"><html>"></a>', |
| 170 |
), |
| 171 |
( |
| 172 |
"test \n newline \n\n some more space \n with trailing space ", |
| 173 |
"test newline some more space with trailing space", |
| 174 |
), |
| 175 |
( |
| 176 |
"test <custom> tag </custom>", |
| 177 |
"test tag", |
| 178 |
), |
| 179 |
( |
| 180 |
"test <b>notification<b> body", |
| 181 |
False, |
| 182 |
), |
| 183 |
( |
| 184 |
"<b>foo<i>bar</b></i>", |
| 185 |
False, |
| 186 |
), |
| 187 |
( |
| 188 |
"test <markup><i>notification</i><markup> body", |
| 189 |
False, |
| 190 |
), |
| 191 |
] |
| 192 |
|
| 193 |
i = 0 |
| 194 |
for body_in, body_expected in bodies: |
| 195 |
notification_in = NOTIFICATION_BASIC.copy() |
| 196 |
notification_in["markup-body"] = GLib.Variant("s", body_in) |
| 197 |
|
| 198 |
notification_expected = NOTIFICATION_BASIC.copy() |
| 199 |
if body_expected: |
| 200 |
notification_expected["markup-body"] = GLib.Variant("s", body_expected) |
| 201 |
|
| 202 |
try: |
| 203 |
self.check_notification( |
| 204 |
dbus_con, |
| 205 |
app_id, |
| 206 |
f"test{i}", |
| 207 |
notification_in, |
| 208 |
notification_expected, |
| 209 |
) |
| 210 |
assert body_expected |
| 211 |
except GLib.GError as e: |
| 212 |
assert "invalid markup-body" in e.message |
| 213 |
|
| 214 |
i += 1 |
| 215 |
|
| 216 |
def test_bad_arg(self, portals, dbus_con, app_id): |
| 217 |
notification = NOTIFICATION_BASIC.copy() |
| 218 |
notification["bodx"] = GLib.Variant("s", "Xtest") |
| 219 |
|
| 220 |
self.check_notification( |
| 221 |
dbus_con, |
| 222 |
app_id, |
| 223 |
"test1", |
| 224 |
notification, |
| 225 |
NOTIFICATION_BASIC, |
| 226 |
) |
| 227 |
|
| 228 |
def test_bad_priority(self, portals, dbus_con, app_id): |
| 229 |
notification = NOTIFICATION_BASIC.copy() |
| 230 |
notification["priority"] = GLib.Variant("s", "invalid") |
| 231 |
|
| 232 |
try: |
| 233 |
self.check_notification( |
| 234 |
dbus_con, |
| 235 |
app_id, |
| 236 |
"test1", |
| 237 |
notification, |
| 238 |
notification, |
| 239 |
) |
| 240 |
assert False, "This statement should not be reached" |
| 241 |
except GLib.GError as e: |
| 242 |
assert "invalid not a priority" in e.message |
| 243 |
|
| 244 |
def test_bad_button(self, portals, dbus_con, app_id): |
| 245 |
notification = NOTIFICATION_BUTTONS.copy() |
| 246 |
notification["buttons"] = GLib.Variant( |
| 247 |
"aa{sv}", |
| 248 |
[ |
| 249 |
{ |
| 250 |
"labex": GLib.Variant("s", "button1"), |
| 251 |
"action": GLib.Variant("s", "action1"), |
| 252 |
}, |
| 253 |
], |
| 254 |
) |
| 255 |
|
| 256 |
try: |
| 257 |
self.check_notification( |
| 258 |
dbus_con, |
| 259 |
app_id, |
| 260 |
"test1", |
| 261 |
notification, |
| 262 |
notification, |
| 263 |
) |
| 264 |
assert False, "This statement should not be reached" |
| 265 |
except GLib.GError as e: |
| 266 |
assert "invalid button" in e.message |
| 267 |
|
| 268 |
def test_display_hint(self, portals, dbus_con, app_id): |
| 269 |
notification = NOTIFICATION_BASIC.copy() |
| 270 |
notification["display-hint"] = GLib.Variant( |
| 271 |
"as", |
| 272 |
[ |
| 273 |
"transient", |
| 274 |
"show-as-new", |
| 275 |
], |
| 276 |
) |
| 277 |
|
| 278 |
self.check_notification( |
| 279 |
dbus_con, |
| 280 |
app_id, |
| 281 |
"test1", |
| 282 |
notification, |
| 283 |
notification, |
| 284 |
) |
| 285 |
|
| 286 |
notification = NOTIFICATION_BASIC.copy() |
| 287 |
notification["display-hint"] = GLib.Variant( |
| 288 |
"as", |
| 289 |
[ |
| 290 |
"unsupported-hint", |
| 291 |
], |
| 292 |
) |
| 293 |
|
| 294 |
try: |
| 295 |
self.check_notification( |
| 296 |
dbus_con, |
| 297 |
app_id, |
| 298 |
"test1", |
| 299 |
notification, |
| 300 |
notification, |
| 301 |
) |
| 302 |
assert False, "This statement should not be reached" |
| 303 |
except GLib.GError as e: |
| 304 |
assert "not a display-hint" in e.message |
| 305 |
|
| 306 |
def test_category(self, portals, dbus_con, app_id): |
| 307 |
notification = NOTIFICATION_BASIC.copy() |
| 308 |
notification["category"] = GLib.Variant("s", "im.received") |
| 309 |
|
| 310 |
self.check_notification( |
| 311 |
dbus_con, |
| 312 |
app_id, |
| 313 |
"test1", |
| 314 |
notification, |
| 315 |
notification, |
| 316 |
) |
| 317 |
|
| 318 |
notification = NOTIFICATION_BASIC.copy() |
| 319 |
notification["category"] = GLib.Variant("s", "x-vendor.custom") |
| 320 |
|
| 321 |
self.check_notification( |
| 322 |
dbus_con, |
| 323 |
app_id, |
| 324 |
"test1", |
| 325 |
notification, |
| 326 |
notification, |
| 327 |
) |
| 328 |
|
| 329 |
notification = NOTIFICATION_BASIC.copy() |
| 330 |
notification["category"] = GLib.Variant("s", "unsupported-type") |
| 331 |
|
| 332 |
try: |
| 333 |
self.check_notification( |
| 334 |
dbus_con, |
| 335 |
app_id, |
| 336 |
"test1", |
| 337 |
notification, |
| 338 |
notification, |
| 339 |
) |
| 340 |
assert False, "This statement should not be reached" |
| 341 |
except GLib.GError as e: |
| 342 |
assert "not a supported category" in e.message |
| 343 |
|
| 344 |
def test_supported_options(self, portals, dbus_con, app_id): |
| 345 |
properties_intf = xdp.get_iface(dbus_con, "org.freedesktop.DBus.Properties") |
| 346 |
|
| 347 |
options = properties_intf.Get( |
| 348 |
"org.freedesktop.portal.Notification", "SupportedOptions" |
| 349 |
) |
| 350 |
|
| 351 |
assert options == SUPPORTED_OPTIONS |
| 352 |
|
| 353 |
def test_icon_themed(self, portals, dbus_con, app_id): |
| 354 |
notification_intf = NotificationPortal() |
| 355 |
icon = Gio.ThemedIcon.new("test-icon-symbolic") |
| 356 |
|
| 357 |
notification = NOTIFICATION_BASIC.copy() |
| 358 |
notification["icon"] = icon.serialize() |
| 359 |
|
| 360 |
notification_intf.AddNotification("test1", notification) |
| 361 |
|
| 362 |
def test_icon_bytes(self, portals, dbus_con, app_id): |
| 363 |
notification_intf = NotificationPortal() |
| 364 |
bytes = GLib.Bytes.new(SVG_IMAGE_DATA.encode("utf-8")) |
| 365 |
icon = Gio.BytesIcon.new(bytes) |
| 366 |
|
| 367 |
notification = NOTIFICATION_BASIC.copy() |
| 368 |
notification["icon"] = icon.serialize() |
| 369 |
|
| 370 |
notification_intf.AddNotification("test1", notification) |
| 371 |
|
| 372 |
def test_icon_file(self, portals, dbus_con, app_id): |
| 373 |
notification_intf = NotificationPortal() |
| 374 |
fd, file_path = tempfile.mkstemp(prefix="notification_icon_", dir=Path.home()) |
| 375 |
os.write(fd, SVG_IMAGE_DATA.encode("utf-8")) |
| 376 |
|
| 377 |
file = Gio.File.new_for_path(file_path) |
| 378 |
icon = Gio.FileIcon.new(file) |
| 379 |
|
| 380 |
notification = NOTIFICATION_BASIC.copy() |
| 381 |
notification["icon"] = icon.serialize() |
| 382 |
|
| 383 |
notification = { |
| 384 |
"title": GLib.Variant("s", "title"), |
| 385 |
"icon": icon.serialize(), |
| 386 |
} |
| 387 |
|
| 388 |
notification_intf.AddNotification("test1", notification) |
| 389 |
|
| 390 |
def test_icon_bad(self, portals, dbus_con, app_id): |
| 391 |
notification_intf = NotificationPortal() |
| 392 |
|
| 393 |
notification = NOTIFICATION_BASIC.copy() |
| 394 |
|
| 395 |
bad_icons = [ |
| 396 |
GLib.Variant("(sv)", ["themed", GLib.Variant("s", "test-icon-symbolic")]), |
| 397 |
GLib.Variant( |
| 398 |
"(sv)", |
| 399 |
["bytes", GLib.Variant("as", ["test-icon-symbolic", "test-icon"])], |
| 400 |
), |
| 401 |
GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("s", "")]), |
| 402 |
GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("h", 0)]), |
| 403 |
] |
| 404 |
|
| 405 |
for icon in bad_icons: |
| 406 |
notification["icon"] = icon |
| 407 |
try: |
| 408 |
notification_intf.AddNotification("test1", notification) |
| 409 |
assert False, "This statement should not be reached" |
| 410 |
except GLib.GError as e: |
| 411 |
assert e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR) |
| 412 |
|
| 413 |
def test_sound_simple(self, portals, dbus_con, app_id): |
| 414 |
notification = NOTIFICATION_BASIC.copy() |
| 415 |
notification["sound"] = GLib.Variant("s", "default") |
| 416 |
|
| 417 |
self.check_notification( |
| 418 |
dbus_con, |
| 419 |
app_id, |
| 420 |
"test1", |
| 421 |
notification, |
| 422 |
notification, |
| 423 |
) |
| 424 |
|
| 425 |
notification = NOTIFICATION_BASIC.copy() |
| 426 |
notification["sound"] = GLib.Variant("s", "silent") |
| 427 |
|
| 428 |
self.check_notification( |
| 429 |
dbus_con, |
| 430 |
app_id, |
| 431 |
"test1", |
| 432 |
notification, |
| 433 |
notification, |
| 434 |
) |
| 435 |
|
| 436 |
notification = NOTIFICATION_BASIC.copy() |
| 437 |
notification["sound"] = GLib.Variant("s", "bad") |
| 438 |
|
| 439 |
try: |
| 440 |
self.check_notification( |
| 441 |
dbus_con, |
| 442 |
app_id, |
| 443 |
"test1", |
| 444 |
notification, |
| 445 |
notification, |
| 446 |
) |
| 447 |
assert False, "This statement should not be reached" |
| 448 |
except GLib.GError as e: |
| 449 |
assert "invalid sound: invalid option" in e.message |
| 450 |
|
| 451 |
def test_sound_file(self, portals, dbus_con, app_id): |
| 452 |
notification_intf = NotificationPortal() |
| 453 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 454 |
|
| 455 |
fd, file_path = tempfile.mkstemp(prefix="notification_sound_", dir=Path.home()) |
| 456 |
os.write(fd, SOUND_DATA) |
| 457 |
|
| 458 |
file = Gio.File.new_for_path(file_path) |
| 459 |
|
| 460 |
notification = NOTIFICATION_BASIC.copy() |
| 461 |
notification["sound"] = GLib.Variant( |
| 462 |
"(sv)", |
| 463 |
( |
| 464 |
"file", |
| 465 |
GLib.Variant("s", file.get_uri()), |
| 466 |
), |
| 467 |
) |
| 468 |
|
| 469 |
notification_intf.AddNotification("test1", notification) |
| 470 |
|
| 471 |
method_calls = mock_intf.GetMethodCalls("AddNotification") |
| 472 |
assert len(method_calls) == 1 |
| 473 |
_, args = method_calls[-1] |
| 474 |
mock_notification = args[2] |
| 475 |
|
| 476 |
assert "sound" not in mock_notification |
| 477 |
|
| 478 |
def test_sound_fd(self, portals, dbus_con, app_id): |
| 479 |
notification_intf = NotificationPortal() |
| 480 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 481 |
|
| 482 |
fd = os.memfd_create("notification_sound_test", os.MFD_ALLOW_SEALING) |
| 483 |
os.write(fd, SOUND_DATA) |
| 484 |
|
| 485 |
notification = NOTIFICATION_BASIC.copy() |
| 486 |
notification["sound"] = GLib.Variant( |
| 487 |
"(sv)", |
| 488 |
( |
| 489 |
"file-descriptor", |
| 490 |
GLib.Variant("h", 0), |
| 491 |
), |
| 492 |
) |
| 493 |
|
| 494 |
notification_intf.AddNotification("test1", notification, [fd]) |
| 495 |
|
| 496 |
method_calls = mock_intf.GetMethodCalls("AddNotification") |
| 497 |
assert len(method_calls) == 1 |
| 498 |
_, args = method_calls[-1] |
| 499 |
mock_notification = args[2] |
| 500 |
|
| 501 |
assert mock_notification["sound"][0] == "file-descriptor" |
| 502 |
mock_fd = mock_notification["sound"][1] |
| 503 |
mock_fd = mock_fd.take() |
| 504 |
|
| 505 |
os.lseek(fd, 0, os.SEEK_SET) |
| 506 |
fd_contents = os.read(mock_fd, 1000) |
| 507 |
assert fd_contents == SOUND_DATA |
| 508 |
|
| 509 |
os.close(mock_fd) |
| 510 |
os.close(fd) |
| 511 |
|
| 512 |
def test_sound_bad(self, portals, dbus_con, app_id): |
| 513 |
notification_intf = NotificationPortal() |
| 514 |
|
| 515 |
notification = NOTIFICATION_BASIC.copy() |
| 516 |
|
| 517 |
bad_sounds = [ |
| 518 |
# bad type |
| 519 |
GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("s", "")]), |
| 520 |
# not sending the FD for the handle |
| 521 |
GLib.Variant("(sv)", ["file-descriptor", GLib.Variant("h", 13)]), |
| 522 |
] |
| 523 |
|
| 524 |
for sound in bad_sounds: |
| 525 |
notification["sound"] = sound |
| 526 |
try: |
| 527 |
notification_intf.AddNotification("test1", notification) |
| 528 |
assert False, "This statement should not be reached" |
| 529 |
except GLib.GError as e: |
| 530 |
assert e.matches(Gio.io_error_quark(), Gio.IOErrorEnum.DBUS_ERROR) |
| 531 |
pass |