| 1 |
# Unit tests for profile_manager.py methods. |
| 2 |
# |
| 3 |
# Copyright 2025-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=wrong-import-position |
| 22 |
# pylint: disable=import-outside-toplevel |
| 23 |
# pylint: disable=protected-access |
| 24 |
|
| 25 |
"""Unit tests for profile_manager.py methods.""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
from typing import TYPE_CHECKING |
| 30 |
|
| 31 |
import gi |
| 32 |
|
| 33 |
gi.require_version("Gtk", "3.0") |
| 34 |
|
| 35 |
import pytest |
| 36 |
|
| 37 |
if TYPE_CHECKING: |
| 38 |
from unittest.mock import MagicMock |
| 39 |
|
| 40 |
from .orca_test_context import OrcaTestContext |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.unit |
| 44 |
class TestProfileManager: |
| 45 |
"""Test ProfileManager methods.""" |
| 46 |
|
| 47 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 48 |
"""Set up mocks for profile_manager dependencies.""" |
| 49 |
|
| 50 |
additional_modules = [ |
| 51 |
"orca.braille", |
| 52 |
"orca.orca", |
| 53 |
"orca.orca_modifier_manager", |
| 54 |
"orca.speech_manager", |
| 55 |
"orca.braille_presenter", |
| 56 |
"orca.presentation_manager", |
| 57 |
] |
| 58 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 59 |
|
| 60 |
from orca import gsettings_registry |
| 61 |
|
| 62 |
registry = gsettings_registry.get_registry() |
| 63 |
registry.set_active_profile("default") |
| 64 |
|
| 65 |
test_context.patch( |
| 66 |
"orca.profile_manager.ProfileManager._get_stored_profiles", |
| 67 |
return_value=[ |
| 68 |
["Default", "default"], |
| 69 |
["Spanish", "spanish"], |
| 70 |
["Work", "work"], |
| 71 |
], |
| 72 |
) |
| 73 |
|
| 74 |
speech_manager_mock = essential_modules["orca.speech_manager"] |
| 75 |
speech_manager_mock.get_manager.return_value.refresh_speech.return_value = None |
| 76 |
|
| 77 |
braille_mock = essential_modules["orca.braille"] |
| 78 |
braille_mock.check_braille_setting.return_value = None |
| 79 |
|
| 80 |
orca_mock = essential_modules["orca.orca"] |
| 81 |
orca_mock.load_user_settings.return_value = None |
| 82 |
|
| 83 |
messages_mock = essential_modules["orca.messages"] |
| 84 |
messages_mock.PROFILE_NOT_FOUND = "No profiles found." |
| 85 |
messages_mock.PROFILE_CHANGED = "Profile set to %s." |
| 86 |
messages_mock.PROFILE_CURRENT = "Current profile is %s." |
| 87 |
|
| 88 |
guilabels_mock = essential_modules["orca.guilabels"] |
| 89 |
guilabels_mock.PROFILE_DEFAULT = "Default" |
| 90 |
|
| 91 |
return essential_modules |
| 92 |
|
| 93 |
def test_get_active_profile(self, test_context: OrcaTestContext) -> None: |
| 94 |
"""Test getting active profile.""" |
| 95 |
|
| 96 |
self._setup_dependencies(test_context) |
| 97 |
from orca.profile_manager import ProfileManager |
| 98 |
|
| 99 |
manager = ProfileManager() |
| 100 |
profile = manager.get_active_profile() |
| 101 |
|
| 102 |
assert profile == "default" |
| 103 |
|
| 104 |
def test_set_active_profile(self, test_context: OrcaTestContext) -> None: |
| 105 |
"""Test setting active profile.""" |
| 106 |
|
| 107 |
self._setup_dependencies(test_context) |
| 108 |
from orca import gsettings_registry |
| 109 |
from orca.profile_manager import ProfileManager |
| 110 |
|
| 111 |
manager = ProfileManager() |
| 112 |
manager.set_active_profile("spanish") |
| 113 |
|
| 114 |
assert gsettings_registry.get_registry().get_active_profile() == "spanish" |
| 115 |
|
| 116 |
def test_load_profile(self, test_context: OrcaTestContext) -> None: |
| 117 |
"""Test loading a profile calls set_active_profile and load_user_settings.""" |
| 118 |
|
| 119 |
essential_modules = self._setup_dependencies(test_context) |
| 120 |
from orca import gsettings_registry |
| 121 |
from orca.profile_manager import ProfileManager |
| 122 |
|
| 123 |
manager = ProfileManager() |
| 124 |
manager.load_profile("spanish") |
| 125 |
|
| 126 |
assert gsettings_registry.get_registry().get_active_profile() == "spanish" |
| 127 |
essential_modules["orca.orca"].load_user_settings.assert_called_once_with( |
| 128 |
skip_reload_message=True, |
| 129 |
) |
| 130 |
|
| 131 |
def test_remove_profile(self, test_context: OrcaTestContext) -> None: |
| 132 |
"""Test removing a profile.""" |
| 133 |
|
| 134 |
self._setup_dependencies(test_context) |
| 135 |
from orca.profile_manager import ProfileManager |
| 136 |
|
| 137 |
mock_run = test_context.patch("subprocess.run") |
| 138 |
manager = ProfileManager() |
| 139 |
manager.remove_profile("spanish") |
| 140 |
|
| 141 |
mock_run.assert_called_once_with( |
| 142 |
["dconf", "reset", "-f", "/org/gnome/orca/spanish/"], |
| 143 |
check=True, |
| 144 |
) |
| 145 |
|
| 146 |
def test_remove_profile_dconf_failure(self, test_context: OrcaTestContext) -> None: |
| 147 |
"""Test removing a profile when dconf reset fails.""" |
| 148 |
|
| 149 |
import subprocess |
| 150 |
|
| 151 |
self._setup_dependencies(test_context) |
| 152 |
from orca.profile_manager import ProfileManager |
| 153 |
|
| 154 |
mock_run = test_context.patch("subprocess.run") |
| 155 |
mock_run.side_effect = subprocess.CalledProcessError(1, "dconf") |
| 156 |
manager = ProfileManager() |
| 157 |
manager.remove_profile("spanish") |
| 158 |
|
| 159 |
mock_run.assert_called_once() |
| 160 |
|
| 161 |
def test_remove_profile_dconf_not_found(self, test_context: OrcaTestContext) -> None: |
| 162 |
"""Test removing a profile when dconf is not installed.""" |
| 163 |
|
| 164 |
self._setup_dependencies(test_context) |
| 165 |
from orca.profile_manager import ProfileManager |
| 166 |
|
| 167 |
mock_run = test_context.patch("subprocess.run") |
| 168 |
mock_run.side_effect = FileNotFoundError("dconf not found") |
| 169 |
manager = ProfileManager() |
| 170 |
manager.remove_profile("spanish") |
| 171 |
|
| 172 |
mock_run.assert_called_once() |
| 173 |
|
| 174 |
def test_remove_profile_sanitizes_name(self, test_context: OrcaTestContext) -> None: |
| 175 |
"""Test removing a profile sanitizes the name for the dconf path.""" |
| 176 |
|
| 177 |
self._setup_dependencies(test_context) |
| 178 |
from orca.profile_manager import ProfileManager |
| 179 |
|
| 180 |
mock_run = test_context.patch("subprocess.run") |
| 181 |
manager = ProfileManager() |
| 182 |
manager.remove_profile("My Profile") |
| 183 |
|
| 184 |
mock_run.assert_called_once_with( |
| 185 |
["dconf", "reset", "-f", "/org/gnome/orca/my-profile/"], |
| 186 |
check=True, |
| 187 |
) |
| 188 |
|
| 189 |
def test_rename_profile(self, test_context: OrcaTestContext) -> None: |
| 190 |
"""Test renaming a profile.""" |
| 191 |
|
| 192 |
self._setup_dependencies(test_context) |
| 193 |
from orca import gsettings_registry |
| 194 |
from orca.profile_manager import ProfileManager |
| 195 |
|
| 196 |
registry = gsettings_registry.get_registry() |
| 197 |
mock_rename = test_context.patch_object(registry, "rename_profile") |
| 198 |
|
| 199 |
manager = ProfileManager() |
| 200 |
manager.rename_profile("spanish", ["Espanol", "espanol"]) |
| 201 |
|
| 202 |
mock_rename.assert_called_once_with("spanish", "Espanol", "espanol") |
| 203 |
|
| 204 |
def test_commands_registered(self, test_context: OrcaTestContext) -> None: |
| 205 |
"""Test that profile manager commands are registered with CommandManager.""" |
| 206 |
|
| 207 |
self._setup_dependencies(test_context) |
| 208 |
from orca import command_manager |
| 209 |
from orca.profile_manager import ProfileManager |
| 210 |
|
| 211 |
manager = ProfileManager() |
| 212 |
manager.set_up_commands() |
| 213 |
cmd_manager = command_manager.get_manager() |
| 214 |
|
| 215 |
assert cmd_manager.get_keyboard_command("cycleSettingsProfileHandler") is not None |
| 216 |
assert cmd_manager.get_keyboard_command("presentCurrentProfileHandler") is not None |
| 217 |
|
| 218 |
def test_cycle_settings_profile_cycles_to_next(self, test_context: OrcaTestContext) -> None: |
| 219 |
"""Test cycle_settings_profile cycles to next profile.""" |
| 220 |
|
| 221 |
essential_modules = self._setup_dependencies(test_context) |
| 222 |
from unittest.mock import MagicMock |
| 223 |
|
| 224 |
from orca import gsettings_registry |
| 225 |
from orca.profile_manager import ProfileManager |
| 226 |
|
| 227 |
manager = ProfileManager() |
| 228 |
mock_script = MagicMock() |
| 229 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 230 |
pres_manager.present_message.reset_mock() |
| 231 |
|
| 232 |
result = manager.cycle_settings_profile(script=mock_script) |
| 233 |
|
| 234 |
assert result is True |
| 235 |
assert gsettings_registry.get_registry().get_active_profile() == "spanish" |
| 236 |
pres_manager.present_message.assert_called() |
| 237 |
|
| 238 |
def test_cycle_settings_profile_wraps_around(self, test_context: OrcaTestContext) -> None: |
| 239 |
"""Test cycle_settings_profile wraps to first profile at end.""" |
| 240 |
|
| 241 |
self._setup_dependencies(test_context) |
| 242 |
from unittest.mock import MagicMock |
| 243 |
|
| 244 |
from orca import gsettings_registry |
| 245 |
from orca.profile_manager import ProfileManager |
| 246 |
|
| 247 |
gsettings_registry.get_registry().set_active_profile("work") |
| 248 |
|
| 249 |
manager = ProfileManager() |
| 250 |
mock_script = MagicMock() |
| 251 |
|
| 252 |
result = manager.cycle_settings_profile(script=mock_script) |
| 253 |
|
| 254 |
assert result is True |
| 255 |
assert gsettings_registry.get_registry().get_active_profile() == "default" |
| 256 |
|
| 257 |
def test_cycle_settings_profile_no_profiles(self, test_context: OrcaTestContext) -> None: |
| 258 |
"""Test cycle_settings_profile handles no profiles.""" |
| 259 |
|
| 260 |
essential_modules = self._setup_dependencies(test_context) |
| 261 |
from unittest.mock import MagicMock |
| 262 |
|
| 263 |
from orca.profile_manager import ProfileManager |
| 264 |
|
| 265 |
manager = ProfileManager() |
| 266 |
mock_script = MagicMock() |
| 267 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 268 |
pres_manager.present_message.reset_mock() |
| 269 |
|
| 270 |
result = manager.cycle_settings_profile(script=mock_script) |
| 271 |
|
| 272 |
assert result is True |
| 273 |
pres_manager.present_message.assert_called() |
| 274 |
|
| 275 |
def test_present_current_profile(self, test_context: OrcaTestContext) -> None: |
| 276 |
"""Test present_current_profile presents the current profile name.""" |
| 277 |
|
| 278 |
essential_modules = self._setup_dependencies(test_context) |
| 279 |
from unittest.mock import MagicMock |
| 280 |
|
| 281 |
from orca.profile_manager import ProfileManager |
| 282 |
|
| 283 |
manager = ProfileManager() |
| 284 |
mock_script = MagicMock() |
| 285 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 286 |
pres_manager.present_message.reset_mock() |
| 287 |
|
| 288 |
result = manager.present_current_profile(script=mock_script) |
| 289 |
|
| 290 |
assert result is True |
| 291 |
pres_manager.present_message.assert_called() |
| 292 |
call_args = pres_manager.present_message.call_args |
| 293 |
assert "Default" in call_args[0][0] |
| 294 |
|
| 295 |
|
| 296 |
@pytest.mark.unit |
| 297 |
class TestProfilePreferencesGridUI: |
| 298 |
"""Test ProfilePreferencesGrid UI creation.""" |
| 299 |
|
| 300 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 301 |
"""Set up mocks for ProfilePreferencesGrid dependencies.""" |
| 302 |
|
| 303 |
additional_modules = [ |
| 304 |
"orca.braille", |
| 305 |
"orca.orca", |
| 306 |
"orca.speech_manager", |
| 307 |
"orca.braille_presenter", |
| 308 |
"orca.presentation_manager", |
| 309 |
] |
| 310 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 311 |
|
| 312 |
from orca import gsettings_registry |
| 313 |
|
| 314 |
registry = gsettings_registry.get_registry() |
| 315 |
registry.set_active_profile("default") |
| 316 |
|
| 317 |
test_context.patch( |
| 318 |
"orca.profile_manager.ProfileManager._get_stored_profiles", |
| 319 |
return_value=[ |
| 320 |
["Default", "default"], |
| 321 |
["Spanish", "spanish"], |
| 322 |
], |
| 323 |
) |
| 324 |
|
| 325 |
guilabels_mock = essential_modules["orca.guilabels"] |
| 326 |
guilabels_mock.GENERAL_PROFILES = "Profiles" |
| 327 |
guilabels_mock.GENERAL_START_UP_PROFILE = "Start-up profile" |
| 328 |
guilabels_mock.PROFILE_DEFAULT = "Default" |
| 329 |
guilabels_mock.PROFILE_CONFLICT_MESSAGE = "Profile %s already exists" |
| 330 |
guilabels_mock.PROFILE_REMOVE_LABEL = "Remove Profile" |
| 331 |
guilabels_mock.PROFILE_REMOVE_MESSAGE = "Remove profile %s?" |
| 332 |
guilabels_mock.MENU_REMOVE_PROFILE = "Remove" |
| 333 |
guilabels_mock.MENU_RENAME = "Rename" |
| 334 |
guilabels_mock.PROFILE_SAVE_AS_TITLE = "Save Profile As" |
| 335 |
guilabels_mock.PROFILE_NAME_LABEL = "Profile name:" |
| 336 |
guilabels_mock.DIALOG_CANCEL = "Cancel" |
| 337 |
guilabels_mock.DIALOG_APPLY = "Apply" |
| 338 |
guilabels_mock.DIALOG_ADD = "Add" |
| 339 |
guilabels_mock.PROFILES_INFO = "Select a profile to edit or create a new one." |
| 340 |
guilabels_mock.CURRENT_PROFILE = "Current Profile" |
| 341 |
guilabels_mock.PROFILE_CREATE_NEW = "_Create New Profile" |
| 342 |
|
| 343 |
return essential_modules |
| 344 |
|
| 345 |
def test_grid_creates_successfully(self, test_context: OrcaTestContext) -> None: |
| 346 |
"""Test ProfilePreferencesGrid creates without error.""" |
| 347 |
|
| 348 |
from gi.repository import Gtk # pylint: disable=no-name-in-module |
| 349 |
|
| 350 |
self._setup_dependencies(test_context) |
| 351 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 352 |
|
| 353 |
manager = ProfileManager() |
| 354 |
|
| 355 |
def callback(_profile): |
| 356 |
return None |
| 357 |
|
| 358 |
grid = ProfilePreferencesGrid(manager, callback) |
| 359 |
|
| 360 |
assert isinstance(grid, Gtk.Grid) |
| 361 |
|
| 362 |
def test_grid_has_auto_grid(self, test_context: OrcaTestContext) -> None: |
| 363 |
"""Test ProfilePreferencesGrid has auto_grid with controls.""" |
| 364 |
|
| 365 |
self._setup_dependencies(test_context) |
| 366 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 367 |
|
| 368 |
manager = ProfileManager() |
| 369 |
|
| 370 |
def callback(_profile): |
| 371 |
return None |
| 372 |
|
| 373 |
grid = ProfilePreferencesGrid(manager, callback) |
| 374 |
|
| 375 |
assert grid._auto_grid is not None |
| 376 |
|
| 377 |
def test_grid_save_settings_returns_dict(self, test_context: OrcaTestContext) -> None: |
| 378 |
"""Test save_settings returns a dictionary.""" |
| 379 |
|
| 380 |
self._setup_dependencies(test_context) |
| 381 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 382 |
|
| 383 |
manager = ProfileManager() |
| 384 |
|
| 385 |
def callback(_profile): |
| 386 |
return None |
| 387 |
|
| 388 |
grid = ProfilePreferencesGrid(manager, callback) |
| 389 |
|
| 390 |
result = grid.save_settings() |
| 391 |
|
| 392 |
assert isinstance(result, dict) |
| 393 |
|
| 394 |
def test_grid_has_changes_initially_false(self, test_context: OrcaTestContext) -> None: |
| 395 |
"""Test has_changes returns False initially.""" |
| 396 |
|
| 397 |
self._setup_dependencies(test_context) |
| 398 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 399 |
|
| 400 |
manager = ProfileManager() |
| 401 |
|
| 402 |
def callback(_profile): |
| 403 |
return None |
| 404 |
|
| 405 |
grid = ProfilePreferencesGrid(manager, callback) |
| 406 |
|
| 407 |
assert grid.has_changes() is False |
| 408 |
|
| 409 |
def test_grid_reload_clears_pending_renames(self, test_context: OrcaTestContext) -> None: |
| 410 |
"""Test reload clears pending renames.""" |
| 411 |
|
| 412 |
self._setup_dependencies(test_context) |
| 413 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 414 |
|
| 415 |
manager = ProfileManager() |
| 416 |
|
| 417 |
def callback(_profile): |
| 418 |
return None |
| 419 |
|
| 420 |
grid = ProfilePreferencesGrid(manager, callback) |
| 421 |
|
| 422 |
grid._pending_renames["old"] = ["New", "new"] |
| 423 |
assert len(grid._pending_renames) == 1 |
| 424 |
|
| 425 |
grid.reload() |
| 426 |
|
| 427 |
assert len(grid._pending_renames) == 0 |
| 428 |
|
| 429 |
def test_grid_app_specific_disables_startup_setter(self, test_context: OrcaTestContext) -> None: |
| 430 |
"""Test app-specific grid disables startup profile setter.""" |
| 431 |
|
| 432 |
self._setup_dependencies(test_context) |
| 433 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 434 |
|
| 435 |
manager = ProfileManager() |
| 436 |
|
| 437 |
def callback(_profile): |
| 438 |
return None |
| 439 |
|
| 440 |
grid = ProfilePreferencesGrid(manager, callback, is_app_specific=True) |
| 441 |
|
| 442 |
assert grid._is_app_specific is True |
| 443 |
|
| 444 |
def test_validate_profile_name_detects_conflict(self, test_context: OrcaTestContext) -> None: |
| 445 |
"""Test _validate_profile_name detects existing profile names.""" |
| 446 |
|
| 447 |
self._setup_dependencies(test_context) |
| 448 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 449 |
|
| 450 |
manager = ProfileManager() |
| 451 |
|
| 452 |
def callback(_profile): |
| 453 |
return None |
| 454 |
|
| 455 |
grid = ProfilePreferencesGrid(manager, callback) |
| 456 |
|
| 457 |
is_valid, error_msg = grid._validate_profile_name("Default") |
| 458 |
|
| 459 |
assert is_valid is False |
| 460 |
assert "Default" in error_msg |
| 461 |
|
| 462 |
def test_validate_profile_name_allows_unique(self, test_context: OrcaTestContext) -> None: |
| 463 |
"""Test _validate_profile_name allows unique names.""" |
| 464 |
|
| 465 |
self._setup_dependencies(test_context) |
| 466 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 467 |
|
| 468 |
manager = ProfileManager() |
| 469 |
|
| 470 |
def callback(_profile): |
| 471 |
return None |
| 472 |
|
| 473 |
grid = ProfilePreferencesGrid(manager, callback) |
| 474 |
|
| 475 |
is_valid, error_msg = grid._validate_profile_name("NewProfile") |
| 476 |
|
| 477 |
assert is_valid is True |
| 478 |
assert error_msg == "" |
| 479 |
|
| 480 |
def test_get_available_profiles_includes_pending_renames( |
| 481 |
self, |
| 482 |
test_context: OrcaTestContext, |
| 483 |
) -> None: |
| 484 |
"""Test _get_available_profiles includes pending renames.""" |
| 485 |
|
| 486 |
self._setup_dependencies(test_context) |
| 487 |
from orca.profile_manager import ProfileManager, ProfilePreferencesGrid |
| 488 |
|
| 489 |
manager = ProfileManager() |
| 490 |
|
| 491 |
def callback(_profile): |
| 492 |
return None |
| 493 |
|
| 494 |
grid = ProfilePreferencesGrid(manager, callback) |
| 495 |
|
| 496 |
grid._pending_renames["spanish"] = ["Espanol", "spanish"] |
| 497 |
profiles = grid._get_available_profiles() |
| 498 |
|
| 499 |
profile_names = [p[0] for p in profiles] |
| 500 |
assert "Espanol" in profile_names |
| 501 |
assert "Spanish" not in profile_names |