Skip to content

Commit

Permalink
tests/settings: Spawn two backends providing different settings
Browse files Browse the repository at this point in the history
  • Loading branch information
swick committed Dec 19, 2024
1 parent 4896c2f commit 251cddd
Showing 1 changed file with 219 additions and 3 deletions.
222 changes: 219 additions & 3 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,55 @@
import pytest


SETTINGS_DATA_TEST1 = {
"org.freedesktop.appearance": dbus.Dictionary(
{
"color-scheme": dbus.UInt32(1),
"accent-color": dbus.Struct((0.0, 0.1, 0.33), signature="ddd"),
},
signature="sv",
),
}

SETTINGS_DATA_TEST2 = {
"org.freedesktop.appearance": dbus.Dictionary(
{
"color-scheme": dbus.UInt32(2),
"contrast": dbus.UInt32(0),
},
signature="sv",
),
"org.example.custom": dbus.Dictionary(
{
"foo": "bar",
},
signature="sv",
),
}

SETTINGS_DATA_BAD = {
"org.freedesktop.appearance": dbus.Dictionary(
{
"color-scheme": dbus.UInt32(99),
"accent-color": dbus.Struct((11.11, 22.22, 33.33), signature="ddd"),
},
signature="sv",
),
"org.example.custom": dbus.Dictionary(
{
"foo": "baz",
},
signature="sv",
),
"org.example.custom.bad": dbus.Dictionary(
{
"bad": "bad",
},
signature="sv",
),
}

# This is the expected data, merged SETTINGS_DATA_TEST1 and SETTINGS_DATA_TEST2
SETTINGS_DATA = {
"org.freedesktop.appearance": dbus.Dictionary(
{
Expand All @@ -29,16 +78,158 @@
@pytest.fixture
def required_templates():
return {
"settings": {
"settings": SETTINGS_DATA,
"settings:org.freedesktop.impl.portal.Test1": {
"settings": SETTINGS_DATA_TEST1,
},
"settings:org.freedesktop.impl.portal.Test2": {
"settings": SETTINGS_DATA_TEST2,
},
"settings:org.freedesktop.impl.portal.TestBad": {
"settings": SETTINGS_DATA_BAD,
},
}


PORTAL_CONFIG_FILES = {
"test1.portal": b"""
[portal]
DBusName=org.freedesktop.impl.portal.Test1
Interfaces=org.freedesktop.impl.portal.Settings;
""",
"test2.portal": b"""
[portal]
DBusName=org.freedesktop.impl.portal.Test2
Interfaces=org.freedesktop.impl.portal.Settings;
""",
"test_bad.portal": b"""
[portal]
DBusName=org.freedesktop.impl.portal.TestBad
Interfaces=org.freedesktop.impl.portal.Settings;
""",
"test_noimpl.portal": b"""
[portal]
DBusName=org.freedesktop.impl.portal.TestBad
Interfaces=org.freedesktop.impl.portal.NonExistant;
""",
}


def portal_config_good():
# test1 merged with test2 should result in the correct output
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test1;test2;
"""
yield files

# a portal without the settings impl does not affect the result
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test1;test_noimpl;test2;
"""
yield files

# the default should be ignored when the interface is configured
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test_bad;
org.freedesktop.impl.portal.Settings=test1;test2
"""
yield files

# use * which should expand to test1;test2;test_noimpl
files = PORTAL_CONFIG_FILES.copy()
del files["test_bad.portal"]
files["test-portals.conf"] = b"""
[preferred]
default=test_noimpl;
org.freedesktop.impl.portal.Settings=*;
"""
yield files


def portal_config_bad():
# test1 alone should result in bad output
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test1;
"""
yield files

# test2 merged with test1 is the wrong order
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test2;test1;
"""
yield files

# test_noimpl does not affect anything
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test_noimpl;test2;test1;
"""
yield files

# default should get ignored, test2 alone should result in bad output
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test1;test2
org.freedesktop.impl.portal.Settings=test2;test_noimpl
"""
yield files

# test_bad anywhere in the active config should result in bad output
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test1;test2
org.freedesktop.impl.portal.Settings=test_bad;test1;test2
"""
yield files

# use * which expands to test1;test2;test_bad;test_no_impl
# contains test_bad which should result in bad output
files = PORTAL_CONFIG_FILES.copy()
files["test-portals.conf"] = b"""
[preferred]
default=test_noimpl;
org.freedesktop.impl.portal.Settings=*;
"""
yield files


def portal_config_twice():
# check that test1 gets picked up only once
files = PORTAL_CONFIG_FILES.copy()
del files["test_bad.portal"]
files["test-portals.conf"] = b"""
[preferred]
default=test_noimpl;
org.freedesktop.impl.portal.Settings=test1;*;
"""
yield files


@pytest.fixture
def xdg_desktop_portal_dir_default_files():
return next(portal_config_good())


class TestSettings:
def test_version(self, portals, dbus_con):
xdp.check_version(dbus_con, "Settings", 2)

@pytest.mark.parametrize(
"xdg_desktop_portal_dir_default_files",
portal_config_good(),
)
def test_settings_read_all(self, portals, dbus_con):
settings_intf = xdp.get_portal_iface(dbus_con, "Settings")

Expand Down Expand Up @@ -70,6 +261,31 @@ def test_settings_read_all(self, portals, dbus_con):
== SETTINGS_DATA["org.freedesktop.appearance"]
)

@pytest.mark.parametrize(
"xdg_desktop_portal_dir_default_files",
portal_config_bad(),
)
def test_settings_read_all_bad_config(self, portals, dbus_con):
settings_intf = xdp.get_portal_iface(dbus_con, "Settings")

value = settings_intf.ReadAll([])
assert value != SETTINGS_DATA

@pytest.mark.parametrize(
"xdg_desktop_portal_dir_default_files",
portal_config_twice(),
)
def test_settings_config_twice(self, portals, dbus_con):
settings_intf = xdp.get_portal_iface(dbus_con, "Settings")
mock_intf = xdp.get_mock_iface(dbus_con, "org.freedesktop.impl.portal.Test1")

value = settings_intf.ReadAll([])
assert value == SETTINGS_DATA

# The config is `test1;*`, make sure we only get a single call to Test1
method_calls = mock_intf.GetMethodCalls("ReadAll")
assert len(method_calls) == 1

def test_settings_read(self, portals, dbus_con):
settings_intf = xdp.get_portal_iface(dbus_con, "Settings")

Expand Down Expand Up @@ -101,7 +317,7 @@ def test_settings_read(self, portals, dbus_con):

def test_settings_changed(self, portals, dbus_con):
settings_intf = xdp.get_portal_iface(dbus_con, "Settings")
mock_intf = xdp.get_mock_iface(dbus_con)
mock_intf = xdp.get_mock_iface(dbus_con, "org.freedesktop.impl.portal.Test1")
changed_count = 0

ns = "org.freedesktop.appearance"
Expand Down

0 comments on commit 251cddd

Please sign in to comment.