| 1 |
# Unit tests for spellcheck_presenter.py methods. |
| 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 |
| 22 |
# pylint: disable=protected-access |
| 23 |
# pylint: disable=no-member |
| 24 |
|
| 25 |
"""Unit tests for spellcheck_presenter.py methods.""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
from typing import TYPE_CHECKING |
| 30 |
|
| 31 |
import pytest |
| 32 |
|
| 33 |
if TYPE_CHECKING: |
| 34 |
from unittest.mock import MagicMock |
| 35 |
|
| 36 |
from .orca_test_context import OrcaTestContext |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.unit |
| 40 |
class TestSpellCheckPresenter: |
| 41 |
"""Test SpellCheckPresenter class methods.""" |
| 42 |
|
| 43 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 44 |
"""Returns dependencies for spellcheck_presenter module testing.""" |
| 45 |
|
| 46 |
additional_modules: list[str] = [ |
| 47 |
"orca.ax_utilities", |
| 48 |
"orca.ax_text", |
| 49 |
"orca.ax_object", |
| 50 |
"orca.braille", |
| 51 |
"orca.focus_manager", |
| 52 |
"orca.input_event_manager", |
| 53 |
"orca.messages", |
| 54 |
"orca.object_properties", |
| 55 |
"orca.preferences_grid_base", |
| 56 |
"orca.presentation_manager", |
| 57 |
"orca.speech_presenter", |
| 58 |
] |
| 59 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 60 |
|
| 61 |
debug_mock = essential_modules["orca.debug"] |
| 62 |
debug_mock.print_message = test_context.Mock() |
| 63 |
debug_mock.LEVEL_INFO = 800 |
| 64 |
|
| 65 |
from orca import gsettings_registry |
| 66 |
|
| 67 |
gsettings_registry.get_registry().clear_runtime_values() |
| 68 |
|
| 69 |
return essential_modules |
| 70 |
|
| 71 |
def test_get_presenter(self, test_context: OrcaTestContext) -> None: |
| 72 |
"""Test get_presenter returns a SpellCheckPresenter instance.""" |
| 73 |
|
| 74 |
essential_modules = self._setup_dependencies(test_context) |
| 75 |
from orca.spellcheck_presenter import SpellCheckPresenter, get_presenter |
| 76 |
|
| 77 |
presenter = get_presenter() |
| 78 |
assert presenter is not None |
| 79 |
assert isinstance(presenter, SpellCheckPresenter) |
| 80 |
|
| 81 |
dbus_service_mock = essential_modules["orca.dbus_service"] |
| 82 |
controller = dbus_service_mock.get_remote_controller.return_value |
| 83 |
controller.register_decorated_module.assert_called_with("SpellCheckPresenter", presenter) |
| 84 |
|
| 85 |
def test_get_spell_error_true(self, test_context: OrcaTestContext) -> None: |
| 86 |
"""Test get_spell_error returns True when setting is True.""" |
| 87 |
|
| 88 |
self._setup_dependencies(test_context) |
| 89 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 90 |
|
| 91 |
presenter = SpellCheckPresenter() |
| 92 |
result = presenter.get_spell_error() |
| 93 |
assert result is True |
| 94 |
|
| 95 |
def test_get_spell_error_false(self, test_context: OrcaTestContext) -> None: |
| 96 |
"""Test get_spell_error returns False when setting is False.""" |
| 97 |
|
| 98 |
self._setup_dependencies(test_context) |
| 99 |
|
| 100 |
from orca import gsettings_registry |
| 101 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 102 |
|
| 103 |
presenter = SpellCheckPresenter() |
| 104 |
gsettings_registry.get_registry().set_runtime_value("spellcheck", "spell-error", False) |
| 105 |
result = presenter.get_spell_error() |
| 106 |
assert result is False |
| 107 |
|
| 108 |
def test_set_spell_error(self, test_context: OrcaTestContext) -> None: |
| 109 |
"""Test set_spell_error updates settings.""" |
| 110 |
|
| 111 |
essential_modules = self._setup_dependencies(test_context) |
| 112 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 113 |
|
| 114 |
presenter = SpellCheckPresenter() |
| 115 |
result = presenter.set_spell_error(False) |
| 116 |
|
| 117 |
assert result is True |
| 118 |
assert presenter.get_spell_error() is False |
| 119 |
essential_modules["orca.debug"].print_message.assert_called() |
| 120 |
|
| 121 |
def test_set_spell_error_same_value(self, test_context: OrcaTestContext) -> None: |
| 122 |
"""Test set_spell_error returns early when value unchanged.""" |
| 123 |
|
| 124 |
essential_modules = self._setup_dependencies(test_context) |
| 125 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 126 |
|
| 127 |
presenter = SpellCheckPresenter() |
| 128 |
essential_modules["orca.debug"].print_message.reset_mock() |
| 129 |
|
| 130 |
result = presenter.set_spell_error(True) |
| 131 |
assert result is True |
| 132 |
# Debug message for setting change should NOT be called |
| 133 |
calls = essential_modules["orca.debug"].print_message.call_args_list |
| 134 |
setting_calls = [c for c in calls if "Setting spell error" in str(c)] |
| 135 |
assert len(setting_calls) == 0 |
| 136 |
|
| 137 |
def test_get_spell_suggestion_true(self, test_context: OrcaTestContext) -> None: |
| 138 |
"""Test get_spell_suggestion returns True when setting is True.""" |
| 139 |
|
| 140 |
self._setup_dependencies(test_context) |
| 141 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 142 |
|
| 143 |
presenter = SpellCheckPresenter() |
| 144 |
result = presenter.get_spell_suggestion() |
| 145 |
assert result is True |
| 146 |
|
| 147 |
def test_get_spell_suggestion_false(self, test_context: OrcaTestContext) -> None: |
| 148 |
"""Test get_spell_suggestion returns False when setting is False.""" |
| 149 |
|
| 150 |
self._setup_dependencies(test_context) |
| 151 |
|
| 152 |
from orca import gsettings_registry |
| 153 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 154 |
|
| 155 |
presenter = SpellCheckPresenter() |
| 156 |
gsettings_registry.get_registry().set_runtime_value("spellcheck", "spell-suggestion", False) |
| 157 |
result = presenter.get_spell_suggestion() |
| 158 |
assert result is False |
| 159 |
|
| 160 |
def test_set_spell_suggestion(self, test_context: OrcaTestContext) -> None: |
| 161 |
"""Test set_spell_suggestion updates settings.""" |
| 162 |
|
| 163 |
essential_modules = self._setup_dependencies(test_context) |
| 164 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 165 |
|
| 166 |
presenter = SpellCheckPresenter() |
| 167 |
result = presenter.set_spell_suggestion(False) |
| 168 |
|
| 169 |
assert result is True |
| 170 |
assert presenter.get_spell_suggestion() is False |
| 171 |
essential_modules["orca.debug"].print_message.assert_called() |
| 172 |
|
| 173 |
def test_set_spell_suggestion_same_value(self, test_context: OrcaTestContext) -> None: |
| 174 |
"""Test set_spell_suggestion returns early when value unchanged.""" |
| 175 |
|
| 176 |
essential_modules = self._setup_dependencies(test_context) |
| 177 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 178 |
|
| 179 |
presenter = SpellCheckPresenter() |
| 180 |
essential_modules["orca.debug"].print_message.reset_mock() |
| 181 |
|
| 182 |
result = presenter.set_spell_suggestion(True) |
| 183 |
assert result is True |
| 184 |
# Debug message for setting change should NOT be called |
| 185 |
calls = essential_modules["orca.debug"].print_message.call_args_list |
| 186 |
setting_calls = [c for c in calls if "Setting spell suggestion" in str(c)] |
| 187 |
assert len(setting_calls) == 0 |
| 188 |
|
| 189 |
def test_get_present_context_true(self, test_context: OrcaTestContext) -> None: |
| 190 |
"""Test get_present_context returns True when setting is True.""" |
| 191 |
|
| 192 |
self._setup_dependencies(test_context) |
| 193 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 194 |
|
| 195 |
presenter = SpellCheckPresenter() |
| 196 |
result = presenter.get_present_context() |
| 197 |
assert result is True |
| 198 |
|
| 199 |
def test_get_present_context_false(self, test_context: OrcaTestContext) -> None: |
| 200 |
"""Test get_present_context returns False when setting is False.""" |
| 201 |
|
| 202 |
self._setup_dependencies(test_context) |
| 203 |
|
| 204 |
from orca import gsettings_registry |
| 205 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 206 |
|
| 207 |
presenter = SpellCheckPresenter() |
| 208 |
gsettings_registry.get_registry().set_runtime_value("spellcheck", "present-context", False) |
| 209 |
result = presenter.get_present_context() |
| 210 |
assert result is False |
| 211 |
|
| 212 |
def test_set_present_context(self, test_context: OrcaTestContext) -> None: |
| 213 |
"""Test set_present_context updates settings.""" |
| 214 |
|
| 215 |
essential_modules = self._setup_dependencies(test_context) |
| 216 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 217 |
|
| 218 |
presenter = SpellCheckPresenter() |
| 219 |
result = presenter.set_present_context(False) |
| 220 |
|
| 221 |
assert result is True |
| 222 |
assert presenter.get_present_context() is False |
| 223 |
essential_modules["orca.debug"].print_message.assert_called() |
| 224 |
|
| 225 |
def test_set_present_context_same_value(self, test_context: OrcaTestContext) -> None: |
| 226 |
"""Test set_present_context returns early when value unchanged.""" |
| 227 |
|
| 228 |
essential_modules = self._setup_dependencies(test_context) |
| 229 |
from orca.spellcheck_presenter import SpellCheckPresenter |
| 230 |
|
| 231 |
presenter = SpellCheckPresenter() |
| 232 |
essential_modules["orca.debug"].print_message.reset_mock() |
| 233 |
|
| 234 |
result = presenter.set_present_context(True) |
| 235 |
assert result is True |
| 236 |
# Debug message for setting change should NOT be called |
| 237 |
calls = essential_modules["orca.debug"].print_message.call_args_list |
| 238 |
setting_calls = [c for c in calls if "Setting present context" in str(c)] |
| 239 |
assert len(setting_calls) == 0 |