Skip to content

Commit

Permalink
tests/notification: Add more tests
Browse files Browse the repository at this point in the history
This includes tests which get skipped because I can't figure out how to
send (av).
  • Loading branch information
swick committed Oct 30, 2024
1 parent 7319c37 commit a5e294f
Showing 1 changed file with 262 additions and 1 deletion.
263 changes: 262 additions & 1 deletion tests/test_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@

import dbus
import pytest
import tempfile
import os
from pathlib import Path
from gi.repository import GLib, Gio

SVG_IMAGE_DATA = """<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" height="16px" width="16px"/>
"""

SOUND_DATA = b"0x52, 0x49, 0x46, 0x46, 0x24, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45,"

SUPPORTED_OPTIONS = {
"foo": "bar",
}


@pytest.fixture
def required_templates():
return {"notification": {}}
return {
"notification": {
"SupportedOptions": SUPPORTED_OPTIONS,
},
}


class TestNotification:
Expand Down Expand Up @@ -231,3 +249,246 @@ def test_notification_bad_button(self, portals, dbus_con, app_id):
assert False, "This statement should not be reached"
except dbus.exceptions.DBusException:
pass

def test_notification_display_hint(self, portals, dbus_con, app_id):
notification = {
"title": "title",
"body": "test notification body",
"display-hint": ["transient", "show-as-new"],
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

notification = {
"title": "title",
"body": "test notification body",
"display-hint": ["unsupported-hint"],
}
try:
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)
assert False, "This statement should not be reached"
except dbus.exceptions.DBusException:
pass

def test_notification_category(self, portals, dbus_con, app_id):
notification = {
"title": "title",
"body": "test notification body",
"category": "im.message",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

notification = {
"title": "title",
"body": "test notification body",
"category": "x-vendor.custom",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

notification = {
"title": "title",
"body": "test notification body",
"category": "unsupported-type",
}
try:
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)
assert False, "This statement should not be reached"
except dbus.exceptions.DBusException:
pass

def test_supported_options(self, portals, dbus_con, app_id):
properties_intf = xdp.get_iface(dbus_con, "org.freedesktop.DBus.Properties")

options = properties_intf.Get(
"org.freedesktop.portal.Notification", "SupportedOptions"
)

assert options == SUPPORTED_OPTIONS

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_icon_themed(self, portals, dbus_con, app_id):
icon = Gio.ThemedIcon.new("test-icon-symbolic")
icon = icon.serialize()

# icon = dbus.Struct(
# ("themed", ["test-icon-symbolic", "test-icon"]),
# signature="sv",
# )

notification = {
"title": "title",
"body": "test notification body",
"icon": dbus.Struct(icon, signature="sv"),
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_icon_bytes(self, portals, dbus_con, app_id):
bytes = GLib.Bytes.new(SVG_IMAGE_DATA.encode("utf-8"))
icon = Gio.BytesIcon.new(bytes)
icon = icon.serialize()

notification = {
"title": "title",
"body": "test notification body",
"icon": dbus.Struct(icon, signature="sv"),
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_icon_file(self, portals, dbus_con, app_id):
fd, file_path = tempfile.mkstemp(prefix="notification_icon_", dir=Path.home())
os.write(fd, SVG_IMAGE_DATA.encode("utf-8"))

file = Gio.File.new_for_path(file_path)
icon = Gio.FileIcon.new(file)
icon = icon.serialize()

notification = {
"title": "title",
"body": "test notification body",
"icon": dbus.Struct(icon, signature="sv"),
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_icon_bad(self, portals, dbus_con, app_id):
pass
# invalid icons:
# test_icon ("('themed', <'test-icon-symbolic'>)", NULL, TRUE);
# test_icon ("('bytes', <['test-icon-symbolic', 'test-icon']>)", NULL, TRUE);
# test_icon ("('file-descriptor', <''>)", NULL, TRUE);
# test_icon ("('file-descriptor', <handle 0>)", NULL, TRUE);

def test_sound_simple(self, portals, dbus_con, app_id):
notification = {
"title": "title",
"body": "test notification body",
"sound": "default",
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

notification = {
"title": "title",
"body": "test notification body",
"sound": "silent",
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)

notification = {
"title": "title",
"body": "test notification body",
"sound": "bad",
"default-action": "test-action",
}
try:
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification,
)
assert False, "This statement should not be reached"
except dbus.exceptions.DBusException:
pass

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_sound_file(self, portals, dbus_con, app_id):
fd, file_path = tempfile.mkstemp(prefix="notification_sound_", dir=Path.home())
os.write(fd, SOUND_DATA)

file = Gio.File.new_for_path(file_path)
icon = dbus.Struct(("file", file.get_uri()), signature="sv")

notification = {
"title": "title",
"body": "test notification body",
"icon": icon,
"default-action": "test-action",
}
# FIXME: we expect any numbered fd here
notification_expected = {
"title": "title",
"body": "test notification body",
"icon": ("file-descriptor", 0),
"default-action": "test-action",
}
self.check_notification(
dbus_con,
app_id,
"test1",
notification,
notification_expected,
)

@pytest.mark.skip(reason="Cannot send 'sv' with python")
def test_sound_bad(self, portals, dbus_con, app_id):
pass
# invalid sounds:
# test_sound ("('file-descriptor', <''>)", NULL, TRUE);
# test_sound ("('file-descriptor', <handle 0>)", NULL, TRUE);

0 comments on commit a5e294f

Please sign in to comment.