| 1 |
# Orca |
| 2 |
# |
| 3 |
# Copyright 2026 Igalia, S.L. |
| 4 |
# Author: Joanmarie Diggs <[email protected]> |
| 5 |
# |
| 6 |
# This library is free software; you can redistribute it and/or |
| 7 |
# modify it under the terms of the GNU Lesser General Public |
| 8 |
# License as published by the Free Software Foundation; either |
| 9 |
# version 2.1 of the License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# This library is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with this library; if not, write to the |
| 18 |
# Free Software Foundation, Inc., Franklin Street, Fifth Floor, |
| 19 |
# Boston MA 02110-1301 USA. |
| 20 |
|
| 21 |
# pylint: disable=import-outside-toplevel,protected-access |
| 22 |
|
| 23 |
"""Integration tests for GSettings support. |
| 24 |
|
| 25 |
These tests exercise Orca's GSettings infrastructure against real Gio.Settings |
| 26 |
objects backed by the in-memory backend. Unlike the unit tests which mock the |
| 27 |
GSettings layer, these verify that schemas compile correctly, the layered lookup |
| 28 |
fallback chain works end-to-end, and save/load round-trips preserve data. |
| 29 |
""" |
| 30 |
|
| 31 |
from __future__ import annotations |
| 32 |
|
| 33 |
import pytest |
| 34 |
from gi.repository import Gio |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.gsettings |
| 38 |
class TestSchemaRegistration: |
| 39 |
"""Verifies @gsettings_schema decorators produce usable compiled schemas.""" |
| 40 |
|
| 41 |
def test_all_expected_schemas_registered(self, gsettings_registry) -> None: |
| 42 |
"""All decorated schemas should be present in the registry.""" |
| 43 |
|
| 44 |
expected = { |
| 45 |
"braille", |
| 46 |
"caret-navigation", |
| 47 |
"chat", |
| 48 |
"document", |
| 49 |
"flat-review", |
| 50 |
"keybindings", |
| 51 |
"live-regions", |
| 52 |
"metadata", |
| 53 |
"mouse-review", |
| 54 |
"pronunciations", |
| 55 |
"say-all", |
| 56 |
"sound", |
| 57 |
"speech", |
| 58 |
"spellcheck", |
| 59 |
"structural-navigation", |
| 60 |
"system-information", |
| 61 |
"table-navigation", |
| 62 |
"text-attributes", |
| 63 |
"typing-echo", |
| 64 |
"voice", |
| 65 |
} |
| 66 |
registered = set(gsettings_registry.get_schema_names()) |
| 67 |
assert expected == registered |
| 68 |
|
| 69 |
def test_each_schema_resolves_to_compiled_gio_schema(self, gsettings_registry) -> None: |
| 70 |
"""Every registered schema ID should be findable in the compiled schema source.""" |
| 71 |
|
| 72 |
source = Gio.SettingsSchemaSource.get_default() |
| 73 |
for name, schema_id in gsettings_registry._schemas.items(): |
| 74 |
schema = source.lookup(schema_id, True) |
| 75 |
assert schema is not None, f"Schema {schema_id!r} ({name}) not compiled" |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.gsettings |
| 79 |
class TestLayeredLookupFallback: |
| 80 |
"""Verifies the 3-tier fallback: app override -> active profile -> default profile.""" |
| 81 |
|
| 82 |
def test_returns_none_when_no_user_values_exist( |
| 83 |
self, gsettings_handle, gsettings_profile |
| 84 |
) -> None: |
| 85 |
"""With nothing set at any layer, layered lookup returns None.""" |
| 86 |
|
| 87 |
handle = gsettings_handle("speech") |
| 88 |
assert handle.get_boolean("enable") is None |
| 89 |
|
| 90 |
def test_finds_value_in_default_profile(self, gsettings_handle, gsettings_profile) -> None: |
| 91 |
"""A value set in the default profile should be returned.""" |
| 92 |
|
| 93 |
handle = gsettings_handle("speech") |
| 94 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 95 |
assert handle.get_boolean("enable") is False |
| 96 |
|
| 97 |
def test_active_profile_overrides_default( |
| 98 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 99 |
) -> None: |
| 100 |
"""When both profiles have values, the active profile wins.""" |
| 101 |
|
| 102 |
handle = gsettings_handle("speech") |
| 103 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 104 |
handle.get_for_profile("spanish").set_boolean("enable", True) |
| 105 |
|
| 106 |
gsettings_registry.set_active_profile("spanish") |
| 107 |
assert handle.get_boolean("enable") is True |
| 108 |
|
| 109 |
def test_active_profile_falls_back_to_default( |
| 110 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 111 |
) -> None: |
| 112 |
"""When the active profile has no value, the default profile's value is used.""" |
| 113 |
|
| 114 |
handle = gsettings_handle("speech") |
| 115 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 116 |
|
| 117 |
gsettings_registry.set_active_profile("spanish") |
| 118 |
assert handle.get_boolean("enable") is False |
| 119 |
|
| 120 |
def test_app_override_takes_precedence( |
| 121 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 122 |
) -> None: |
| 123 |
"""An app-specific value should win over both profile layers.""" |
| 124 |
|
| 125 |
handle = gsettings_handle("speech") |
| 126 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 127 |
handle.get_for_app("firefox").set_boolean("enable", True) |
| 128 |
|
| 129 |
gsettings_registry.set_active_app("Firefox") |
| 130 |
assert handle.get_boolean("enable") is True |
| 131 |
|
| 132 |
def test_no_app_override_falls_through_to_profile( |
| 133 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 134 |
) -> None: |
| 135 |
"""When an app is active but has no override, the profile value is used.""" |
| 136 |
|
| 137 |
handle = gsettings_handle("speech") |
| 138 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 139 |
|
| 140 |
gsettings_registry.set_active_app("Firefox") |
| 141 |
assert handle.get_boolean("enable") is False |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.gsettings |
| 145 |
class TestRegistryLookup: |
| 146 |
"""Tests registry-level lookup: runtime overrides, defaults, voice routing, enums.""" |
| 147 |
|
| 148 |
def test_default_parameter_used_when_no_value_exists( |
| 149 |
self, gsettings_registry, gsettings_profile |
| 150 |
) -> None: |
| 151 |
"""The default= parameter should be returned when nothing is set in dconf.""" |
| 152 |
|
| 153 |
result = gsettings_registry.layered_lookup("speech", "enable", "b", default=True) |
| 154 |
assert result is True |
| 155 |
|
| 156 |
def test_runtime_override_takes_precedence_over_dconf( |
| 157 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 158 |
) -> None: |
| 159 |
"""A runtime override should win over a value stored in dconf.""" |
| 160 |
|
| 161 |
handle = gsettings_handle("speech") |
| 162 |
handle.get_for_profile("default").set_boolean("enable", True) |
| 163 |
|
| 164 |
gsettings_registry.set_runtime_value("speech", "enable", False) |
| 165 |
try: |
| 166 |
assert gsettings_registry.layered_lookup("speech", "enable", "b") is False |
| 167 |
finally: |
| 168 |
gsettings_registry.remove_runtime_value("speech", "enable") |
| 169 |
|
| 170 |
def test_voice_type_routes_to_correct_sub_path( |
| 171 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 172 |
) -> None: |
| 173 |
"""voice_type='uppercase' should read from voices/uppercase, not voices/default.""" |
| 174 |
|
| 175 |
handle = gsettings_handle("voice") |
| 176 |
handle.get_for_profile("default", sub_path="voices/default").set_double("pitch", 1.0) |
| 177 |
handle.get_for_profile("default", sub_path="voices/uppercase").set_double("pitch", 9.0) |
| 178 |
|
| 179 |
default_pitch = gsettings_registry.layered_lookup( |
| 180 |
"voice", "pitch", "d", voice_type="default" |
| 181 |
) |
| 182 |
uppercase_pitch = gsettings_registry.layered_lookup( |
| 183 |
"voice", "pitch", "d", voice_type="uppercase" |
| 184 |
) |
| 185 |
assert default_pitch == 1.0 |
| 186 |
assert uppercase_pitch == 9.0 |
| 187 |
|
| 188 |
def test_enum_lookup_returns_string_nick( |
| 189 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 190 |
) -> None: |
| 191 |
"""Enum settings should be returned as their string nick, not an int.""" |
| 192 |
|
| 193 |
handle = gsettings_handle("speech") |
| 194 |
handle.get_for_profile("default").set_string("verbosity-level", "brief") |
| 195 |
|
| 196 |
result = gsettings_registry.layered_lookup( |
| 197 |
"speech", |
| 198 |
"verbosity-level", |
| 199 |
"", |
| 200 |
genum="org.gnome.Orca.VerbosityLevel", |
| 201 |
) |
| 202 |
assert result == "brief" |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.gsettings |
| 206 |
class TestSaveSchema: |
| 207 |
"""Tests save_schema, the preferences UI path for writing settings to dconf.""" |
| 208 |
|
| 209 |
def test_round_trip_multiple_types( |
| 210 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 211 |
) -> None: |
| 212 |
"""Boolean, string, and int values should survive a save_schema round-trip.""" |
| 213 |
|
| 214 |
gsettings_registry.save_schema( |
| 215 |
"speech", |
| 216 |
{"enable": False, "synthesizer": "espeak-ng", "repeated-character-limit": 8}, |
| 217 |
"default", |
| 218 |
) |
| 219 |
|
| 220 |
handle = gsettings_handle("speech") |
| 221 |
assert handle.get_boolean("enable") is False |
| 222 |
assert handle.get_string("synthesizer") == "espeak-ng" |
| 223 |
assert handle.get_int("repeated-character-limit") == 8 |
| 224 |
|
| 225 |
def test_round_trip_strv(self, gsettings_registry, gsettings_handle, gsettings_profile) -> None: |
| 226 |
"""String-array values should survive a save_schema round-trip.""" |
| 227 |
|
| 228 |
gsettings_registry.save_schema( |
| 229 |
"keybindings", |
| 230 |
{"desktop-modifier-keys": ["Insert"], "laptop-modifier-keys": ["Caps_Lock"]}, |
| 231 |
"default", |
| 232 |
) |
| 233 |
|
| 234 |
handle = gsettings_handle("keybindings") |
| 235 |
assert handle.get_strv("desktop-modifier-keys") == ["Insert"] |
| 236 |
assert handle.get_strv("laptop-modifier-keys") == ["Caps_Lock"] |
| 237 |
|
| 238 |
def test_app_override_isolation( |
| 239 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 240 |
) -> None: |
| 241 |
"""Settings saved with an app_name should not be visible without that app active.""" |
| 242 |
|
| 243 |
gsettings_registry.save_schema("speech", {"enable": False}, "default", app_name="firefox") |
| 244 |
|
| 245 |
handle = gsettings_handle("speech") |
| 246 |
assert handle.get_boolean("enable") is None |
| 247 |
|
| 248 |
gsettings_registry.set_active_app("firefox") |
| 249 |
assert handle.get_boolean("enable") is False |
| 250 |
|
| 251 |
def test_skip_defaults_resets_default_valued_key( |
| 252 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 253 |
) -> None: |
| 254 |
"""skip_defaults should reset a key whose value matches the descriptor default.""" |
| 255 |
|
| 256 |
handle = gsettings_handle("speech") |
| 257 |
handle.get_for_profile("default").set_boolean("enable", False) |
| 258 |
assert handle.get_boolean("enable") is False |
| 259 |
|
| 260 |
gsettings_registry.save_schema("speech", {"enable": True}, "default", skip_defaults=True) |
| 261 |
assert handle.get_boolean("enable") is None |
| 262 |
|
| 263 |
def test_enum_saved_by_nick( |
| 264 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 265 |
) -> None: |
| 266 |
"""Enum values passed as string nicks should be written correctly.""" |
| 267 |
|
| 268 |
gsettings_registry.save_schema("speech", {"verbosity-level": "brief"}, "default") |
| 269 |
|
| 270 |
handle = gsettings_handle("speech") |
| 271 |
assert handle.get_string("verbosity-level") == "brief" |
| 272 |
|
| 273 |
def test_enum_saved_by_int( |
| 274 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 275 |
) -> None: |
| 276 |
"""Enum values passed as ints should be resolved to their nick before writing.""" |
| 277 |
|
| 278 |
gsettings_registry.save_schema( |
| 279 |
"speech", |
| 280 |
{"progress-bar-speech-verbosity": 2}, |
| 281 |
"default", |
| 282 |
) |
| 283 |
|
| 284 |
handle = gsettings_handle("speech") |
| 285 |
assert handle.get_string("progress-bar-speech-verbosity") == "window" |
| 286 |
|
| 287 |
|
| 288 |
@pytest.mark.gsettings |
| 289 |
class TestProfileOperations: |
| 290 |
"""Tests profile reset, rename, and copy operations across all schemas.""" |
| 291 |
|
| 292 |
def test_reset_profile_clears_all_user_keys( |
| 293 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 294 |
) -> None: |
| 295 |
"""reset_profile should remove all user-set values for that profile.""" |
| 296 |
|
| 297 |
handle = gsettings_handle("speech") |
| 298 |
gs = handle.get_for_profile("doomed") |
| 299 |
gs.set_boolean("enable", False) |
| 300 |
gs.set_string("synthesizer", "espeak-ng") |
| 301 |
|
| 302 |
gsettings_registry.reset_profile("doomed") |
| 303 |
|
| 304 |
gs_after = Gio.Settings.new_with_path( |
| 305 |
handle.get_schema_id(), "/org/gnome/orca/doomed/speech/" |
| 306 |
) |
| 307 |
assert gs_after.get_user_value("enable") is None |
| 308 |
assert gs_after.get_user_value("synthesizer") is None |
| 309 |
|
| 310 |
def test_rename_profile_moves_keys( |
| 311 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 312 |
) -> None: |
| 313 |
"""rename_profile should copy keys to the new path and clear the old.""" |
| 314 |
|
| 315 |
handle = gsettings_handle("speech") |
| 316 |
handle.get_for_profile("old-name").set_boolean("enable", False) |
| 317 |
|
| 318 |
gsettings_registry.rename_profile("old-name", "New Label", "new-name") |
| 319 |
|
| 320 |
gs_new = Gio.Settings.new_with_path( |
| 321 |
handle.get_schema_id(), "/org/gnome/orca/new-name/speech/" |
| 322 |
) |
| 323 |
assert gs_new.get_boolean("enable") is False |
| 324 |
|
| 325 |
gs_old = Gio.Settings.new_with_path( |
| 326 |
handle.get_schema_id(), "/org/gnome/orca/old-name/speech/" |
| 327 |
) |
| 328 |
assert gs_old.get_user_value("enable") is None |
| 329 |
|
| 330 |
def test_rename_profile_updates_metadata(self, gsettings_registry, gsettings_profile) -> None: |
| 331 |
"""rename_profile should set display-name and internal-name on the new path.""" |
| 332 |
|
| 333 |
gs_old = gsettings_registry.get_settings("metadata", "src-profile") |
| 334 |
gs_old.set_string("display-name", "Old Label") |
| 335 |
|
| 336 |
gsettings_registry.rename_profile("src-profile", "Shiny New", "dest-profile") |
| 337 |
|
| 338 |
gs_new = gsettings_registry.get_settings("metadata", "dest-profile") |
| 339 |
assert gs_new.get_string("display-name") == "Shiny New" |
| 340 |
assert gs_new.get_string("internal-name") == "dest-profile" |
| 341 |
|
| 342 |
def test_copy_user_keys(self, gsettings_registry, gsettings_handle, gsettings_profile) -> None: |
| 343 |
"""copy_user_keys should duplicate user-set values without affecting the source.""" |
| 344 |
|
| 345 |
handle = gsettings_handle("speech") |
| 346 |
gs_src = handle.get_for_profile("default") |
| 347 |
gs_src.set_boolean("enable", False) |
| 348 |
gs_src.set_string("synthesizer", "espeak-ng") |
| 349 |
|
| 350 |
gs_dest = handle.get_for_profile("cloned") |
| 351 |
gsettings_registry.copy_user_keys(gs_src, gs_dest) |
| 352 |
|
| 353 |
assert gs_dest.get_boolean("enable") is False |
| 354 |
assert gs_dest.get_string("synthesizer") == "espeak-ng" |
| 355 |
assert gs_src.get_user_value("enable") is not None |
| 356 |
|
| 357 |
|
| 358 |
@pytest.mark.gsettings |
| 359 |
class TestDictSchemas: |
| 360 |
"""Tests pronunciation and keybinding schemas (dict serialization).""" |
| 361 |
|
| 362 |
def test_pronunciation_round_trip(self, gsettings_registry, gsettings_profile) -> None: |
| 363 |
"""Pronunciations should survive import -> get_pronunciations.""" |
| 364 |
|
| 365 |
from orca import gsettings_migrator |
| 366 |
|
| 367 |
gs = gsettings_registry.get_settings("pronunciations", "default", "pronunciations") |
| 368 |
gsettings_migrator.import_pronunciations( |
| 369 |
gs, {"orca": ["orca", "or-kah"], "GNOME": ["GNOME", "guh-nome"]} |
| 370 |
) |
| 371 |
|
| 372 |
result = gsettings_registry.get_pronunciations("default") |
| 373 |
assert result == {"orca": "or-kah", "GNOME": "guh-nome"} |
| 374 |
|
| 375 |
def test_keybinding_round_trip(self, gsettings_registry, gsettings_profile) -> None: |
| 376 |
"""Keybindings should survive import -> get_keybindings.""" |
| 377 |
|
| 378 |
from orca import gsettings_migrator |
| 379 |
|
| 380 |
gs = gsettings_registry.get_settings("keybindings", "default", "keybindings") |
| 381 |
gsettings_migrator.import_keybindings(gs, {"doAction": [["65", "0", "1", "1"]]}) |
| 382 |
|
| 383 |
result = gsettings_registry.get_keybindings("default") |
| 384 |
assert result["doAction"] == [["65", "0", "1", "1"]] |
| 385 |
|
| 386 |
def test_pronunciations_empty_for_unset_profile( |
| 387 |
self, gsettings_registry, gsettings_profile |
| 388 |
) -> None: |
| 389 |
"""get_pronunciations should return {} for a profile with no data.""" |
| 390 |
|
| 391 |
assert gsettings_registry.get_pronunciations("nonexistent") == {} |
| 392 |
|
| 393 |
def test_keybindings_empty_for_unset_profile( |
| 394 |
self, gsettings_registry, gsettings_profile |
| 395 |
) -> None: |
| 396 |
"""get_keybindings should return {} for a profile with no data.""" |
| 397 |
|
| 398 |
assert gsettings_registry.get_keybindings("nonexistent") == {} |
| 399 |
|
| 400 |
|
| 401 |
@pytest.mark.gsettings |
| 402 |
class TestMigrationRoundTrip: |
| 403 |
"""Tests JSON-to-dconf migration via _write_profile_settings.""" |
| 404 |
|
| 405 |
def test_legacy_keys_land_in_correct_schemas( |
| 406 |
self, gsettings_registry, gsettings_handle, gsettings_profile |
| 407 |
) -> None: |
| 408 |
"""Legacy prefs should be mapped to their correct GSettings schema and key.""" |
| 409 |
|
| 410 |
general = { |
| 411 |
"enableSpeech": False, |
| 412 |
"enableBraille": False, |
| 413 |
} |
| 414 |
gsettings_registry._write_profile_settings("migrated", general, {}, {}) |
| 415 |
|
| 416 |
gsettings_registry.set_active_profile("migrated") |
| 417 |
speech = gsettings_handle("speech") |
| 418 |
braille = gsettings_handle("braille") |
| 419 |
|
| 420 |
assert speech.get_boolean("enable") is False |
| 421 |
assert braille.get_boolean("enabled") is False |
| 422 |
|
| 423 |
def test_migration_includes_pronunciations(self, gsettings_registry, gsettings_profile) -> None: |
| 424 |
"""_write_profile_settings should also persist pronunciations.""" |
| 425 |
|
| 426 |
gsettings_registry._write_profile_settings( |
| 427 |
"with-pron", {"enableSpeech": True}, {"orca": ["orca", "or-kah"]}, {} |
| 428 |
) |
| 429 |
assert gsettings_registry.get_pronunciations("with-pron") == {"orca": "or-kah"} |
| 430 |
|
| 431 |
def test_migration_includes_keybindings(self, gsettings_registry, gsettings_profile) -> None: |
| 432 |
"""_write_profile_settings should also persist keybindings.""" |
| 433 |
|
| 434 |
gsettings_registry._write_profile_settings( |
| 435 |
"with-kb", {"enableSpeech": True}, {}, {"doAction": [["65", "0", "1", "1"]]} |
| 436 |
) |
| 437 |
result = gsettings_registry.get_keybindings("with-kb") |
| 438 |
assert "doAction" in result |
| 439 |
|
| 440 |
def test_migration_sets_profile_metadata(self, gsettings_registry, gsettings_profile) -> None: |
| 441 |
"""_write_profile_settings should write display-name and internal-name.""" |
| 442 |
|
| 443 |
general = {"enableSpeech": True, "profile": ["My Profile", "my-profile"]} |
| 444 |
gsettings_registry._write_profile_settings("my-profile", general, {}, {}) |
| 445 |
|
| 446 |
gs = gsettings_registry.get_settings("metadata", "my-profile") |
| 447 |
assert gs.get_string("display-name") == "My Profile" |
| 448 |
assert gs.get_string("internal-name") == "my-profile" |