Debian Codegraph @

tests/unit_tests/test_sleep_mode_manager.py

raw
1 # Unit tests for sleep_mode_manager.py methods.
2 #
3 # Copyright 2025 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 # pylint: disable=too-many-locals
25
26 """Unit tests for sleep_mode_manager.py methods."""
27
28 from __future__ import annotations
29
30 from typing import TYPE_CHECKING
31
32 import gi
33 import pytest
34
35 gi.require_version("Atspi", "2.0")
36 from gi.repository import Atspi
37
38 if TYPE_CHECKING:
39 from unittest.mock import MagicMock
40
41 from .orca_test_context import OrcaTestContext
42
43
44 @pytest.mark.unit
45 class TestSleepModeManager:
46 """Test SleepModeManager class methods."""
47
48 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
49 """Returns dependencies for sleep_mode_manager module testing."""
50
51 essential_modules = test_context.setup_shared_dependencies(
52 ["orca.braille_presenter", "orca.presentation_manager"],
53 )
54
55 debug_mock = essential_modules["orca.debug"]
56 debug_mock.print_message = test_context.Mock()
57 debug_mock.print_tokens = test_context.Mock()
58 debug_mock.debugFile = None
59 debug_mock.LEVEL_INFO = 800
60
61 ax_object_mock = essential_modules["orca.ax_object"]
62 ax_object_mock.get_name = test_context.Mock(return_value="TestApp")
63
64 braille_presenter_mock = essential_modules["orca.braille_presenter"]
65 braille_presenter_instance = test_context.Mock()
66 braille_presenter_instance.use_braille = test_context.Mock(return_value=True)
67 braille_presenter_instance.present_message = test_context.Mock()
68 braille_presenter_mock.get_presenter = test_context.Mock(
69 return_value=braille_presenter_instance,
70 )
71
72 cmdnames_mock = essential_modules["orca.cmdnames"]
73 cmdnames_mock.TOGGLE_SLEEP_MODE = "Toggle sleep mode for the current application"
74
75 dbus_service_mock = essential_modules["orca.dbus_service"]
76 controller_mock = test_context.Mock()
77 controller_mock.register_decorated_module = test_context.Mock()
78 dbus_service_mock.get_remote_controller = test_context.Mock(return_value=controller_mock)
79 dbus_service_mock.command = lambda func: func
80
81 input_event_mock = essential_modules["orca.input_event"]
82 input_event_handler_mock = test_context.Mock()
83 input_event_mock.InputEventHandler = test_context.Mock(
84 return_value=input_event_handler_mock,
85 )
86
87 keybindings_mock = essential_modules["orca.keybindings"]
88 key_bindings_instance = test_context.Mock()
89 key_bindings_instance.is_empty = test_context.Mock(return_value=False)
90 key_bindings_instance.remove_key_grabs = test_context.Mock(return_value=None)
91 key_bindings_instance.add = test_context.Mock(return_value=None)
92 keybindings_mock.KeyBindings = test_context.Mock(return_value=key_bindings_instance)
93 keybindings_mock.KeyBinding = test_context.Mock()
94 keybindings_mock.DEFAULT_MODIFIER_MASK = 1
95 keybindings_mock.SHIFT_ALT_CTRL_MODIFIER_MASK = 15
96
97 messages_mock = essential_modules["orca.messages"]
98 messages_mock.SLEEP_MODE_ENABLED_FOR = "Sleep mode enabled for %s."
99 messages_mock.SLEEP_MODE_DISABLED_FOR = "Sleep mode disabled for %s."
100
101 script_manager_mock = essential_modules["orca.script_manager"]
102 script_manager_instance = test_context.Mock()
103 script_manager_instance.get_or_create_sleep_mode_script = test_context.Mock()
104 script_manager_instance.set_active_script = test_context.Mock()
105 script_manager_instance.get_script = test_context.Mock()
106 script_manager_mock.get_manager = test_context.Mock(return_value=script_manager_instance)
107
108 essential_modules["controller"] = controller_mock
109 essential_modules["input_event_handler"] = input_event_handler_mock
110 essential_modules["key_bindings_instance"] = key_bindings_instance
111 essential_modules["script_manager_instance"] = script_manager_instance
112
113 return essential_modules
114
115 def test_init(self, test_context: OrcaTestContext) -> None:
116 """Test SleepModeManager.__init__ with default parameters."""
117
118 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
119 from orca import command_manager
120 from orca.sleep_mode_manager import SleepModeManager
121
122 manager = SleepModeManager()
123 assert not manager._apps
124 # Commands are registered during setup(), not __init__()
125 manager.set_up_commands()
126 cmd_manager = command_manager.get_manager()
127 assert cmd_manager.get_command("toggle_sleep_mode") is not None
128 # Bindings are now stored on Commands, not created via KeyBindings class
129 assert essential_modules["orca.dbus_service"] is not None
130 assert essential_modules["controller"] is not None
131
132 def test_commands_registered(self, test_context: OrcaTestContext) -> None:
133 """Test that commands are registered with CommandManager during setup."""
134
135 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
136 from orca import command_manager
137 from orca.sleep_mode_manager import SleepModeManager
138
139 manager = SleepModeManager()
140 manager.set_up_commands()
141 # Verify command is in CommandManager
142 cmd_manager = command_manager.get_manager()
143 assert cmd_manager.get_keyboard_command("toggle_sleep_mode") is not None
144 essential_modules["orca.debug"].print_message.assert_called()
145
146 def test_is_active_for_app_with_active_app(self, test_context: OrcaTestContext) -> None:
147 """Test SleepModeManager.is_active_for_app returns True for active app."""
148
149 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
150 from orca.sleep_mode_manager import SleepModeManager
151
152 manager = SleepModeManager()
153 mock_app = test_context.Mock(spec=Atspi.Accessible)
154 app_hash = hash(mock_app)
155 manager._apps.append(app_hash)
156
157 essential_modules["orca.debug"].print_tokens.reset_mock()
158 result = manager.is_active_for_app(mock_app)
159 assert result is True
160 essential_modules["orca.debug"].print_tokens.assert_called_with(
161 essential_modules["orca.debug"].LEVEL_INFO,
162 ["SLEEP MODE MANAGER: Is active for", mock_app],
163 True,
164 )
165
166 def test_setup_handlers(self, test_context: OrcaTestContext) -> None:
167 """Test that commands are registered with CommandManager during setup."""
168
169 self._setup_dependencies(test_context)
170 from orca import command_manager
171 from orca.sleep_mode_manager import SleepModeManager
172
173 manager = SleepModeManager()
174 manager.set_up_commands()
175 # Commands are now registered with CommandManager
176 cmd_manager = command_manager.get_manager()
177 cmd = cmd_manager.get_command("toggle_sleep_mode")
178 assert cmd is not None
179
180 def test_setup_bindings(self, test_context: OrcaTestContext) -> None:
181 """Test that keybindings are created via Command.set_keybinding."""
182
183 self._setup_dependencies(test_context)
184 from orca import command_manager
185 from orca.sleep_mode_manager import SleepModeManager
186
187 manager = SleepModeManager()
188 manager.set_up_commands()
189 # Verify the command has a keybinding
190 cmd_manager = command_manager.get_manager()
191 cmd = cmd_manager.get_keyboard_command("toggle_sleep_mode")
192 assert cmd is not None
193 keybinding = cmd.get_keybinding()
194 assert keybinding is not None
195
196 def test_toggle_sleep_mode_no_script(self, test_context: OrcaTestContext) -> None:
197 """Test SleepModeManager.toggle_sleep_mode with no script."""
198
199 self._setup_dependencies(test_context)
200 from orca.sleep_mode_manager import SleepModeManager
201
202 manager = SleepModeManager()
203 result = manager.toggle_sleep_mode(None)
204 assert result is True
205
206 def test_toggle_sleep_mode_script_no_app(self, test_context: OrcaTestContext) -> None:
207 """Test SleepModeManager.toggle_sleep_mode with script but no app."""
208
209 self._setup_dependencies(test_context)
210 from orca.sleep_mode_manager import SleepModeManager
211
212 manager = SleepModeManager()
213 mock_script = test_context.Mock()
214 mock_script.app = None
215 result = manager.toggle_sleep_mode(mock_script)
216 assert result is True
217
218 @pytest.mark.parametrize(
219 "initially_active,notify_user,expected_message_contains",
220 [
221 pytest.param(False, True, "Sleep mode enabled for", id="enable_with_notification"),
222 pytest.param(False, False, None, id="enable_without_notification"),
223 pytest.param(True, True, "Sleep mode disabled for", id="disable_with_notification"),
224 pytest.param(True, False, None, id="disable_without_notification"),
225 ],
226 )
227 def test_toggle_sleep_mode_with_script_and_app( # pylint: disable=too-many-branches
228 self,
229 test_context: OrcaTestContext,
230 initially_active: bool,
231 notify_user: bool,
232 expected_message_contains: str | None,
233 ) -> None:
234 """Test SleepModeManager.toggle_sleep_mode with various scenarios."""
235
236 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
237 from orca.sleep_mode_manager import SleepModeManager
238
239 manager = SleepModeManager()
240 mock_script = test_context.Mock()
241 mock_app = test_context.Mock()
242 mock_script.app = mock_app
243 mock_event = test_context.Mock()
244 app_hash = hash(mock_app)
245
246 if initially_active:
247 manager._apps.append(app_hash)
248
249 script_manager_instance = essential_modules["script_manager_instance"]
250
251 if initially_active:
252 new_script = test_context.Mock()
253 script_manager_instance.get_script = test_context.Mock(return_value=new_script)
254 else:
255 sleep_script = test_context.Mock()
256 script_manager_instance.get_or_create_sleep_mode_script = test_context.Mock(
257 return_value=sleep_script,
258 )
259
260 test_context.patch(
261 "orca.sleep_mode_manager.script_manager.get_manager",
262 return_value=script_manager_instance,
263 )
264
265 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
266 pres_manager.present_message.reset_mock()
267 result = manager.toggle_sleep_mode(mock_script, mock_event, notify_user=notify_user)
268 assert result is True
269
270 if initially_active:
271 assert app_hash not in manager._apps
272 else:
273 assert app_hash in manager._apps
274
275 if notify_user and expected_message_contains:
276 if initially_active:
277 pres_manager.present_message.assert_called_once()
278 call_args = pres_manager.present_message.call_args[0][0]
279 assert expected_message_contains in call_args
280 else:
281 pres_manager.speak_message.assert_called_once()
282 call_args = pres_manager.speak_message.call_args[0][0]
283 assert expected_message_contains in call_args
284 elif not notify_user:
285 pres_manager.present_message.assert_not_called()
286 pres_manager.speak_message.assert_not_called()
287
288 if initially_active:
289 script_manager_instance.get_script.assert_called_with(mock_app)
290 script_manager_instance.set_active_script.assert_called_with(
291 new_script,
292 "Sleep mode toggled off",
293 )
294 else:
295 script_manager_instance.get_or_create_sleep_mode_script.assert_called_with(mock_app)
296 script_manager_instance.set_active_script.assert_called_with(
297 sleep_script,
298 "Sleep mode toggled on",
299 )