Debian Codegraph @

tests/unit_tests/test_text_attribute_manager.py

raw
1 # Unit tests for text_attribute_manager.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 text_attribute_manager.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 TestTextAttributeManager:
41 """Test TextAttributeManager class methods."""
42
43 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
44 """Returns dependencies for text_attribute_manager module testing."""
45
46 additional_modules: list[str] = []
47 essential_modules = test_context.setup_shared_dependencies(additional_modules)
48
49 debug_mock = essential_modules["orca.debug"]
50 debug_mock.print_message = test_context.Mock()
51 debug_mock.LEVEL_INFO = 800
52
53 from orca import gsettings_registry
54
55 registry = gsettings_registry.get_registry()
56 registry.clear_runtime_values()
57
58 return essential_modules
59
60 def test_init(self, test_context: OrcaTestContext) -> None:
61 """Test TextAttributeManager initialization."""
62
63 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
64 from orca.text_attribute_manager import TextAttributeManager
65
66 manager = TextAttributeManager()
67 assert manager is not None
68
69 dbus_service_mock = essential_modules["orca.dbus_service"]
70 controller = dbus_service_mock.get_remote_controller.return_value
71 controller.register_decorated_module.assert_called_with("TextAttributeManager", manager)
72
73 def test_get_attributes_to_speak_empty(self, test_context: OrcaTestContext) -> None:
74 """Test get_attributes_to_speak returns empty list by default."""
75
76 self._setup_dependencies(test_context)
77 from orca.text_attribute_manager import TextAttributeManager
78
79 manager = TextAttributeManager()
80 result = manager.get_attributes_to_speak()
81 assert result == []
82
83 def test_get_attributes_to_speak_with_values(self, test_context: OrcaTestContext) -> None:
84 """Test get_attributes_to_speak returns configured attributes."""
85
86 self._setup_dependencies(test_context)
87 expected = ["bold", "italic", "underline"]
88 from orca.text_attribute_manager import TextAttributeManager
89
90 manager = TextAttributeManager()
91 manager.set_attributes_to_speak(expected)
92 result = manager.get_attributes_to_speak()
93 assert result == expected
94
95 def test_set_attributes_to_speak(self, test_context: OrcaTestContext) -> None:
96 """Test set_attributes_to_speak updates settings."""
97
98 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
99 from orca.text_attribute_manager import TextAttributeManager
100
101 manager = TextAttributeManager()
102 new_value = ["bold", "italic"]
103 result = manager.set_attributes_to_speak(new_value)
104
105 assert result is True
106 assert manager.get_attributes_to_speak() == new_value
107 essential_modules["orca.debug"].print_message.assert_called()
108
109 def test_set_attributes_to_speak_same_value(self, test_context: OrcaTestContext) -> None:
110 """Test set_attributes_to_speak returns early when value unchanged."""
111
112 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
113 existing = ["bold", "italic"]
114 from orca.text_attribute_manager import TextAttributeManager
115
116 manager = TextAttributeManager()
117 manager.set_attributes_to_speak(existing)
118 essential_modules["orca.debug"].print_message.reset_mock()
119
120 result = manager.set_attributes_to_speak(existing)
121 assert result is True
122 calls = essential_modules["orca.debug"].print_message.call_args_list
123 setting_calls = [c for c in calls if "Setting attributes to speak" in str(c)]
124 assert len(setting_calls) == 0
125
126 def test_get_attributes_to_braille_empty(self, test_context: OrcaTestContext) -> None:
127 """Test get_attributes_to_braille returns empty list by default."""
128
129 self._setup_dependencies(test_context)
130 from orca.text_attribute_manager import TextAttributeManager
131
132 manager = TextAttributeManager()
133 result = manager.get_attributes_to_braille()
134 assert result == []
135
136 def test_get_attributes_to_braille_with_values(self, test_context: OrcaTestContext) -> None:
137 """Test get_attributes_to_braille returns configured attributes."""
138
139 self._setup_dependencies(test_context)
140 expected = ["bold", "strikethrough"]
141 from orca.text_attribute_manager import TextAttributeManager
142
143 manager = TextAttributeManager()
144 manager.set_attributes_to_braille(expected)
145 result = manager.get_attributes_to_braille()
146 assert result == expected
147
148 def test_set_attributes_to_braille(self, test_context: OrcaTestContext) -> None:
149 """Test set_attributes_to_braille updates settings."""
150
151 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
152 from orca.text_attribute_manager import TextAttributeManager
153
154 manager = TextAttributeManager()
155 new_value = ["bold", "underline"]
156 result = manager.set_attributes_to_braille(new_value)
157
158 assert result is True
159 assert manager.get_attributes_to_braille() == new_value
160 essential_modules["orca.debug"].print_message.assert_called()
161
162 def test_set_attributes_to_braille_same_value(self, test_context: OrcaTestContext) -> None:
163 """Test set_attributes_to_braille returns early when value unchanged."""
164
165 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
166 existing = ["bold"]
167 from orca.text_attribute_manager import TextAttributeManager
168
169 manager = TextAttributeManager()
170 manager.set_attributes_to_braille(existing)
171 essential_modules["orca.debug"].print_message.reset_mock()
172
173 result = manager.set_attributes_to_braille(existing)
174 assert result is True
175 calls = essential_modules["orca.debug"].print_message.call_args_list
176 setting_calls = [c for c in calls if "Setting attributes to braille" in str(c)]
177 assert len(setting_calls) == 0