| 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 |
SETTINGS_DATA_TEST1 = { |
| 12 |
"org.freedesktop.appearance": dbus.Dictionary( |
| 13 |
{ |
| 14 |
"color-scheme": dbus.UInt32(1), |
| 15 |
"accent-color": dbus.Struct((0.0, 0.1, 0.33), signature="ddd"), |
| 16 |
}, |
| 17 |
signature="sv", |
| 18 |
), |
| 19 |
} |
| 20 |
|
| 21 |
SETTINGS_DATA_TEST2 = { |
| 22 |
"org.freedesktop.appearance": dbus.Dictionary( |
| 23 |
{ |
| 24 |
"color-scheme": dbus.UInt32(2), |
| 25 |
"contrast": dbus.UInt32(0), |
| 26 |
}, |
| 27 |
signature="sv", |
| 28 |
), |
| 29 |
"org.example.custom": dbus.Dictionary( |
| 30 |
{ |
| 31 |
"foo": "bar", |
| 32 |
}, |
| 33 |
signature="sv", |
| 34 |
), |
| 35 |
} |
| 36 |
|
| 37 |
SETTINGS_DATA_BAD = { |
| 38 |
"org.freedesktop.appearance": dbus.Dictionary( |
| 39 |
{ |
| 40 |
"color-scheme": dbus.UInt32(99), |
| 41 |
"accent-color": dbus.Struct((11.11, 22.22, 33.33), signature="ddd"), |
| 42 |
}, |
| 43 |
signature="sv", |
| 44 |
), |
| 45 |
"org.example.custom": dbus.Dictionary( |
| 46 |
{ |
| 47 |
"foo": "baz", |
| 48 |
}, |
| 49 |
signature="sv", |
| 50 |
), |
| 51 |
"org.example.custom.bad": dbus.Dictionary( |
| 52 |
{ |
| 53 |
"bad": "bad", |
| 54 |
}, |
| 55 |
signature="sv", |
| 56 |
), |
| 57 |
} |
| 58 |
|
| 59 |
# This is the expected data, merged SETTINGS_DATA_TEST1 and SETTINGS_DATA_TEST2 |
| 60 |
SETTINGS_DATA = { |
| 61 |
"org.freedesktop.appearance": dbus.Dictionary( |
| 62 |
{ |
| 63 |
"color-scheme": dbus.UInt32(1), |
| 64 |
"accent-color": dbus.Struct((0.0, 0.1, 0.33), signature="ddd"), |
| 65 |
"contrast": dbus.UInt32(0), |
| 66 |
}, |
| 67 |
signature="sv", |
| 68 |
), |
| 69 |
"org.example.custom": dbus.Dictionary( |
| 70 |
{ |
| 71 |
"foo": "bar", |
| 72 |
}, |
| 73 |
signature="sv", |
| 74 |
), |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
@pytest.fixture |
| 79 |
def required_templates(): |
| 80 |
return { |
| 81 |
"settings:org.freedesktop.impl.portal.Test1": { |
| 82 |
"settings": SETTINGS_DATA_TEST1, |
| 83 |
}, |
| 84 |
"settings:org.freedesktop.impl.portal.Test2": { |
| 85 |
"settings": SETTINGS_DATA_TEST2, |
| 86 |
}, |
| 87 |
"settings:org.freedesktop.impl.portal.TestBad": { |
| 88 |
"settings": SETTINGS_DATA_BAD, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
PORTAL_CONFIG_FILES = { |
| 94 |
"test1.portal": b""" |
| 95 |
[portal] |
| 96 |
DBusName=org.freedesktop.impl.portal.Test1 |
| 97 |
Interfaces=org.freedesktop.impl.portal.Settings; |
| 98 |
""", |
| 99 |
"test2.portal": b""" |
| 100 |
[portal] |
| 101 |
DBusName=org.freedesktop.impl.portal.Test2 |
| 102 |
Interfaces=org.freedesktop.impl.portal.Settings; |
| 103 |
""", |
| 104 |
"test_bad.portal": b""" |
| 105 |
[portal] |
| 106 |
DBusName=org.freedesktop.impl.portal.TestBad |
| 107 |
Interfaces=org.freedesktop.impl.portal.Settings; |
| 108 |
""", |
| 109 |
"test_noimpl.portal": b""" |
| 110 |
[portal] |
| 111 |
DBusName=org.freedesktop.impl.portal.TestBad |
| 112 |
Interfaces=org.freedesktop.impl.portal.NonExistant; |
| 113 |
""", |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
def portal_config_good(): |
| 118 |
# test1 merged with test2 should result in the correct output |
| 119 |
files = PORTAL_CONFIG_FILES.copy() |
| 120 |
files["test-portals.conf"] = b""" |
| 121 |
[preferred] |
| 122 |
default=test1;test2; |
| 123 |
""" |
| 124 |
yield files |
| 125 |
|
| 126 |
# a portal without the settings impl does not affect the result |
| 127 |
files = PORTAL_CONFIG_FILES.copy() |
| 128 |
files["test-portals.conf"] = b""" |
| 129 |
[preferred] |
| 130 |
default=test1;test_noimpl;test2; |
| 131 |
""" |
| 132 |
yield files |
| 133 |
|
| 134 |
# the default should be ignored when the interface is configured |
| 135 |
files = PORTAL_CONFIG_FILES.copy() |
| 136 |
files["test-portals.conf"] = b""" |
| 137 |
[preferred] |
| 138 |
default=test_bad; |
| 139 |
org.freedesktop.impl.portal.Settings=test1;test2 |
| 140 |
""" |
| 141 |
yield files |
| 142 |
|
| 143 |
# use * which should expand to test1;test2;test_noimpl |
| 144 |
files = PORTAL_CONFIG_FILES.copy() |
| 145 |
del files["test_bad.portal"] |
| 146 |
files["test-portals.conf"] = b""" |
| 147 |
[preferred] |
| 148 |
default=test_noimpl; |
| 149 |
org.freedesktop.impl.portal.Settings=*; |
| 150 |
""" |
| 151 |
yield files |
| 152 |
|
| 153 |
|
| 154 |
def portal_config_bad(): |
| 155 |
# test1 alone should result in bad output |
| 156 |
files = PORTAL_CONFIG_FILES.copy() |
| 157 |
files["test-portals.conf"] = b""" |
| 158 |
[preferred] |
| 159 |
default=test1; |
| 160 |
""" |
| 161 |
yield files |
| 162 |
|
| 163 |
# test2 merged with test1 is the wrong order |
| 164 |
files = PORTAL_CONFIG_FILES.copy() |
| 165 |
files["test-portals.conf"] = b""" |
| 166 |
[preferred] |
| 167 |
default=test2;test1; |
| 168 |
""" |
| 169 |
yield files |
| 170 |
|
| 171 |
# test_noimpl does not affect anything |
| 172 |
files = PORTAL_CONFIG_FILES.copy() |
| 173 |
files["test-portals.conf"] = b""" |
| 174 |
[preferred] |
| 175 |
default=test_noimpl;test2;test1; |
| 176 |
""" |
| 177 |
yield files |
| 178 |
|
| 179 |
# default should get ignored, test2 alone should result in bad output |
| 180 |
files = PORTAL_CONFIG_FILES.copy() |
| 181 |
files["test-portals.conf"] = b""" |
| 182 |
[preferred] |
| 183 |
default=test1;test2 |
| 184 |
org.freedesktop.impl.portal.Settings=test2;test_noimpl |
| 185 |
""" |
| 186 |
yield files |
| 187 |
|
| 188 |
# test_bad anywhere in the active config should result in bad output |
| 189 |
files = PORTAL_CONFIG_FILES.copy() |
| 190 |
files["test-portals.conf"] = b""" |
| 191 |
[preferred] |
| 192 |
default=test1;test2 |
| 193 |
org.freedesktop.impl.portal.Settings=test_bad;test1;test2 |
| 194 |
""" |
| 195 |
yield files |
| 196 |
|
| 197 |
# use * which expands to test1;test2;test_bad;test_no_impl |
| 198 |
# contains test_bad which should result in bad output |
| 199 |
files = PORTAL_CONFIG_FILES.copy() |
| 200 |
files["test-portals.conf"] = b""" |
| 201 |
[preferred] |
| 202 |
default=test_noimpl; |
| 203 |
org.freedesktop.impl.portal.Settings=*; |
| 204 |
""" |
| 205 |
yield files |
| 206 |
|
| 207 |
|
| 208 |
def portal_config_twice(): |
| 209 |
# check that test1 gets picked up only once |
| 210 |
files = PORTAL_CONFIG_FILES.copy() |
| 211 |
del files["test_bad.portal"] |
| 212 |
files["test-portals.conf"] = b""" |
| 213 |
[preferred] |
| 214 |
default=test_noimpl; |
| 215 |
org.freedesktop.impl.portal.Settings=test1;*; |
| 216 |
""" |
| 217 |
yield files |
| 218 |
|
| 219 |
|
| 220 |
@pytest.fixture |
| 221 |
def xdg_desktop_portal_dir_default_files(): |
| 222 |
return next(portal_config_good()) |
| 223 |
|
| 224 |
|
| 225 |
class TestSettings: |
| 226 |
def test_version(self, portals, dbus_con): |
| 227 |
xdp.check_version(dbus_con, "Settings", 2) |
| 228 |
|
| 229 |
@pytest.mark.parametrize( |
| 230 |
"xdg_desktop_portal_dir_default_files", |
| 231 |
portal_config_good(), |
| 232 |
) |
| 233 |
def test_read_all(self, portals, dbus_con): |
| 234 |
settings_intf = xdp.get_portal_iface(dbus_con, "Settings") |
| 235 |
|
| 236 |
value = settings_intf.ReadAll([]) |
| 237 |
assert value == SETTINGS_DATA |
| 238 |
|
| 239 |
value = settings_intf.ReadAll([""]) |
| 240 |
assert value == SETTINGS_DATA |
| 241 |
|
| 242 |
value = settings_intf.ReadAll(["does-not-exist"]) |
| 243 |
assert value == {} |
| 244 |
|
| 245 |
value = settings_intf.ReadAll(["org."]) |
| 246 |
assert value == {} |
| 247 |
|
| 248 |
value = settings_intf.ReadAll(["org.*"]) |
| 249 |
assert value == SETTINGS_DATA |
| 250 |
|
| 251 |
value = settings_intf.ReadAll( |
| 252 |
["org.freedesktop.appearance", "org.example.custom"] |
| 253 |
) |
| 254 |
assert value == SETTINGS_DATA |
| 255 |
|
| 256 |
value = settings_intf.ReadAll(["org.freedesktop.appearance"]) |
| 257 |
assert len(value) == 1 |
| 258 |
assert "org.freedesktop.appearance" in value |
| 259 |
assert ( |
| 260 |
value["org.freedesktop.appearance"] |
| 261 |
== SETTINGS_DATA["org.freedesktop.appearance"] |
| 262 |
) |
| 263 |
|
| 264 |
@pytest.mark.parametrize( |
| 265 |
"xdg_desktop_portal_dir_default_files", |
| 266 |
portal_config_bad(), |
| 267 |
) |
| 268 |
def test_read_all_bad_config(self, portals, dbus_con): |
| 269 |
settings_intf = xdp.get_portal_iface(dbus_con, "Settings") |
| 270 |
|
| 271 |
value = settings_intf.ReadAll([]) |
| 272 |
assert value != SETTINGS_DATA |
| 273 |
|
| 274 |
@pytest.mark.parametrize( |
| 275 |
"xdg_desktop_portal_dir_default_files", |
| 276 |
portal_config_twice(), |
| 277 |
) |
| 278 |
def test_config_twice(self, portals, dbus_con): |
| 279 |
settings_intf = xdp.get_portal_iface(dbus_con, "Settings") |
| 280 |
mock_intf = xdp.get_mock_iface(dbus_con, "org.freedesktop.impl.portal.Test1") |
| 281 |
|
| 282 |
value = settings_intf.ReadAll([]) |
| 283 |
assert value == SETTINGS_DATA |
| 284 |
|
| 285 |
# The config is `test1;*`, make sure we only get a single call to Test1 |
| 286 |
method_calls = mock_intf.GetMethodCalls("ReadAll") |
| 287 |
assert len(method_calls) == 1 |
| 288 |
|
| 289 |
def test_read(self, portals, dbus_con): |
| 290 |
settings_intf = xdp.get_portal_iface(dbus_con, "Settings") |
| 291 |
|
| 292 |
color_scheme = SETTINGS_DATA["org.freedesktop.appearance"]["color-scheme"] |
| 293 |
|
| 294 |
value = settings_intf.ReadOne("org.freedesktop.appearance", "color-scheme") |
| 295 |
assert isinstance(value, dbus.UInt32) |
| 296 |
assert value.variant_level == 1 |
| 297 |
assert value == color_scheme |
| 298 |
|
| 299 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 300 |
settings_intf.ReadOne("org.does.not.exist", "color-scheme") |
| 301 |
assert excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotFound" |
| 302 |
|
| 303 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 304 |
settings_intf.ReadOne("org.freedesktop.appearance", "xcolor-scheme") |
| 305 |
assert excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotFound" |
| 306 |
|
| 307 |
# deprecated but should still check that it works |
| 308 |
# the crucial detail here is that the variant_level is 2 |
| 309 |
value = settings_intf.Read("org.freedesktop.appearance", "color-scheme") |
| 310 |
assert isinstance(value, dbus.UInt32) |
| 311 |
assert value.variant_level == 2 |
| 312 |
assert value == color_scheme |
| 313 |
|
| 314 |
def test_changed(self, portals, dbus_con): |
| 315 |
settings_intf = xdp.get_portal_iface(dbus_con, "Settings") |
| 316 |
mock_intf = xdp.get_mock_iface(dbus_con, "org.freedesktop.impl.portal.Test1") |
| 317 |
changed_count = 0 |
| 318 |
|
| 319 |
ns = "org.freedesktop.appearance" |
| 320 |
key = "color-scheme" |
| 321 |
current_value = SETTINGS_DATA[ns][key] |
| 322 |
new_value = 2 |
| 323 |
assert current_value != new_value |
| 324 |
|
| 325 |
value = settings_intf.ReadOne(ns, key) |
| 326 |
assert value == current_value |
| 327 |
|
| 328 |
def cb_settings_changed(changed_ns, changed_key, changed_value): |
| 329 |
nonlocal changed_count |
| 330 |
changed_count += 1 |
| 331 |
assert changed_ns == ns |
| 332 |
assert changed_key == key |
| 333 |
assert changed_value == new_value |
| 334 |
|
| 335 |
settings_intf.connect_to_signal("SettingChanged", cb_settings_changed) |
| 336 |
mock_intf.SetSetting(ns, key, new_value) |
| 337 |
|
| 338 |
xdp.wait_for(lambda: changed_count == 1) |