Debian Codegraph @

tests/unit_tests/test_action_presenter.py

raw
1 # Unit tests for action_presenter.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=import-outside-toplevel
22 # pylint: disable=protected-access
23 # pylint: disable=too-many-public-methods
24
25 """Unit tests for action_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 LOCATION_NOT_FOUND_FULL_MSG = "Location not found (full)"
39 LOCATION_NOT_FOUND_BRIEF_MSG = "Location not found (brief)"
40 LOCATION_NOT_FOUND_MSG = "Location not found"
41 SHOW_ACTIONS_LIST_CMD = "showActionsList"
42
43
44 @pytest.mark.unit
45 class TestActionPresenter:
46 """Test ActionPresenter class methods."""
47
48 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]:
49 """Returns all dependencies needed for ActionPresenter testing."""
50
51 additional_modules = [
52 "orca.brltablenames",
53 "orca.speech",
54 "orca.braille_presenter",
55 "orca.presentation_manager",
56 ]
57 essential_modules = test_context.setup_shared_dependencies(additional_modules)
58
59 essential_modules["orca.cmdnames"].SHOW_ACTIONS_LIST = SHOW_ACTIONS_LIST_CMD
60 focus_manager_instance = test_context.Mock()
61 focus_manager_instance.get_active_mode_and_object_of_interest = test_context.Mock(
62 return_value=(None, None),
63 )
64 focus_manager_instance.get_locus_of_focus = test_context.Mock(return_value=None)
65 focus_manager_instance.get_active_window = test_context.Mock(return_value=None)
66 focus_manager_instance.clear_state = test_context.Mock()
67 focus_manager_instance.set_active_window = test_context.Mock()
68 focus_manager_instance.set_locus_of_focus = test_context.Mock()
69 essential_modules["orca.focus_manager"].get_manager = test_context.Mock(
70 return_value=focus_manager_instance,
71 )
72
73 input_event_handler_mock = test_context.Mock()
74 essential_modules["orca.input_event"].InputEventHandler = test_context.Mock(
75 return_value=input_event_handler_mock,
76 )
77 essential_modules["orca.input_event"].InputEvent = test_context.mocker.MagicMock
78
79 essential_modules["orca.messages"].LOCATION_NOT_FOUND_FULL = LOCATION_NOT_FOUND_FULL_MSG
80 essential_modules["orca.messages"].LOCATION_NOT_FOUND_BRIEF = LOCATION_NOT_FOUND_BRIEF_MSG
81 essential_modules["orca.messages"].NO_ACTIONS_FOUND_ON = "No actions found on %s"
82 essential_modules["orca.guilabels"].ACTIONS_LIST = "Actions List"
83 return essential_modules
84
85 # pylint: disable-next=too-many-locals
86 @pytest.mark.parametrize(
87 "test_scenario,n_actions,action_descriptions,object_source,"
88 "expected_message,expects_gui_creation",
89 [
90 pytest.param(
91 "with_actions",
92 2,
93 ["Click button", "Activate button"],
94 "locus_of_focus",
95 None,
96 True,
97 id="actions_from_locus",
98 ),
99 pytest.param(
100 "no_object",
101 0,
102 [],
103 None,
104 LOCATION_NOT_FOUND_FULL_MSG,
105 False,
106 id="no_object_available",
107 ),
108 pytest.param(
109 "empty_descriptions",
110 2,
111 ["", ""],
112 "locus_of_focus",
113 None,
114 True,
115 id="empty_action_descriptions",
116 ),
117 pytest.param(
118 "from_active_mode",
119 2,
120 ["Click button", "Activate button"],
121 "active_mode",
122 None,
123 True,
124 id="object_from_active_mode",
125 ),
126 pytest.param(
127 "no_actions_found",
128 0,
129 [],
130 "locus_of_focus",
131 "No actions found on Test Button",
132 False,
133 id="object_has_no_actions",
134 ),
135 ],
136 )
137 def test_show_actions_list( # pylint: disable=too-many-arguments,too-many-positional-arguments,too-many-locals,too-many-statements
138 self,
139 test_context: OrcaTestContext,
140 test_scenario: str,
141 n_actions: int,
142 action_descriptions: list[str],
143 object_source: str | None,
144 expected_message: str | None,
145 expects_gui_creation: bool,
146 ) -> None:
147 """Test ActionPresenter.show_actions_list with various scenarios."""
148
149 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
150
151 if test_scenario != "no_object":
152 ax_object_mock = test_context.Mock()
153 ax_object_mock.get_n_actions = test_context.Mock(return_value=n_actions)
154 ax_object_mock.get_action_name = test_context.Mock(
155 side_effect=lambda obj, i: ["click", "activate"][i]
156 if i < len(["click", "activate"])
157 else f"action{i}",
158 )
159 ax_object_mock.get_action_localized_name = test_context.Mock(
160 side_effect=lambda obj, i: ["click", "activate"][i]
161 if i < len(["click", "activate"])
162 else f"action{i}",
163 )
164 ax_object_mock.get_action_description = test_context.Mock(
165 side_effect=lambda obj, i: action_descriptions[i]
166 if i < len(action_descriptions)
167 else f"desc{i}",
168 )
169 ax_object_mock.get_name = test_context.Mock(return_value="Test Button")
170
171 ax_utilities_mock = test_context.Mock()
172 ax_utilities_mock.get_application = test_context.Mock(return_value=test_context.Mock())
173
174 ax_action_mock = test_context.Mock()
175 ax_action_mock.get_n_actions = ax_object_mock.get_n_actions
176 ax_action_mock.get_action_name = ax_object_mock.get_action_name
177 ax_action_mock.get_action_localized_name = ax_object_mock.get_action_localized_name
178 ax_action_mock.get_action_description = ax_object_mock.get_action_description
179
180 ax_action_module_mock = test_context.Mock()
181 ax_action_module_mock.AXAction = ax_action_mock
182 test_context.patch_module("orca.ax_action", ax_action_module_mock)
183
184 ax_object_module_mock = test_context.Mock()
185 ax_object_module_mock.AXObject = ax_object_mock
186 test_context.patch_module("orca.ax_object", ax_object_module_mock)
187 ax_utilities_module_mock = test_context.Mock()
188 ax_utilities_module_mock.AXUtilities = ax_utilities_mock
189 test_context.patch_module("orca.ax_utilities", ax_utilities_module_mock)
190
191 from orca.action_presenter import ActionPresenter
192
193 presenter = ActionPresenter()
194 mock_script = essential_modules["orca.script_manager"].get_manager().get_script()
195 mock_event = test_context.Mock()
196 focus_manager = essential_modules["orca.focus_manager"].get_manager()
197
198 if object_source == "locus_of_focus":
199 mock_accessible_object = test_context.Mock()
200 mock_accessible_object.get_name = test_context.Mock(return_value="Test Button")
201 focus_manager.get_active_mode_and_object_of_interest.return_value = (None, None)
202 focus_manager.get_locus_of_focus.return_value = mock_accessible_object
203 elif object_source == "active_mode":
204 mock_accessible_object = test_context.Mock()
205 mock_accessible_object.get_name = test_context.Mock(return_value="Test Button")
206 focus_manager.get_active_mode_and_object_of_interest.return_value = (
207 test_context.Mock(),
208 mock_accessible_object,
209 )
210 focus_manager.get_locus_of_focus.return_value = None
211 else:
212 focus_manager.get_active_mode_and_object_of_interest.return_value = (None, None)
213 focus_manager.get_locus_of_focus.return_value = None
214 mock_accessible_object = None
215
216 mock_action_list_class = None
217 if expects_gui_creation:
218 mock_window = test_context.Mock()
219 focus_manager.get_active_window.return_value = mock_window
220 mock_action_list = test_context.Mock()
221 mock_action_list_class = test_context.patch(
222 "orca.action_presenter.ActionList",
223 return_value=mock_action_list,
224 )
225
226 result = presenter.show_actions_list(mock_script, mock_event, True)
227 assert result is True
228
229 if expected_message:
230 pres_manager = essential_modules["orca.presentation_manager"].get_manager()
231 if LOCATION_NOT_FOUND_MSG in expected_message:
232 pres_manager.present_message.assert_called_once_with(
233 LOCATION_NOT_FOUND_FULL_MSG,
234 LOCATION_NOT_FOUND_BRIEF_MSG,
235 )
236 else:
237 pres_manager.present_message.assert_called_once_with(expected_message)
238
239 if expects_gui_creation and n_actions > 0:
240 assert mock_action_list_class is not None
241 mock_action_list_class.assert_called_once()
242 mock_action_list.show_gui.assert_called_once()
243 assert presenter._obj == mock_accessible_object
244 assert presenter._gui == mock_action_list
245
246 if test_scenario == "empty_descriptions":
247 call_args = mock_action_list_class.call_args[0][0]
248 assert "click" in call_args
249 assert call_args["click"] == "click"
250
251 @pytest.mark.parametrize(
252 "action_success,gui_state",
253 [
254 pytest.param(True, "present", id="successful_action"),
255 pytest.param(False, "present", id="failed_action"),
256 pytest.param(True, None, id="no_gui_early_return"),
257 ],
258 )
259 def test_perform_action(
260 self,
261 test_context: OrcaTestContext,
262 action_success: bool,
263 gui_state: str | None,
264 ) -> None:
265 """Test ActionPresenter._perform_action with various scenarios."""
266
267 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
268 from orca.action_presenter import ActionPresenter
269
270 presenter = ActionPresenter()
271 mock_accessible_object = test_context.Mock()
272 mock_accessible_object.get_name = test_context.Mock(return_value="Test Button")
273 presenter._obj = mock_accessible_object
274
275 if gui_state == "present":
276 presenter._gui = test_context.Mock()
277 else:
278 presenter._gui = None
279
280 mock_idle_add = test_context.patch("orca.action_presenter.GLib.idle_add")
281 mock_debug_tokens = test_context.patch("orca.action_presenter.debug.print_tokens")
282 mock_do_action = test_context.patch("orca.action_presenter.AXUtilities.do_named_action")
283 mock_do_action.return_value = action_success
284
285 presenter._perform_action("click")
286
287 if gui_state is None:
288 essential_modules["orca.debug"].print_message.assert_called()
289 debug_call = essential_modules["orca.debug"].print_message.call_args[0][1]
290 assert "_perform_action called when self._gui is None" in debug_call
291 mock_do_action.assert_not_called()
292 else:
293 assert presenter._gui is not None
294 presenter._gui.hide.assert_called_once()
295 mock_do_action.assert_called_with(mock_accessible_object, "click")
296 mock_idle_add.assert_called_with(presenter._gui.destroy)
297 mock_debug_tokens.assert_called()
298
299 if not action_success:
300 debug_calls = mock_debug_tokens.call_args_list
301 assert len(debug_calls) > 0
302
303 def test_get_presenter_singleton(self, test_context: OrcaTestContext) -> None:
304 """Test action_presenter.get_presenter singleton functionality."""
305
306 self._setup_dependencies(test_context)
307 from orca.action_presenter import get_presenter
308
309 presenter1 = get_presenter()
310 presenter2 = get_presenter()
311 assert presenter1 is presenter2
312 assert presenter1.__class__.__name__ == "ActionPresenter"
313
314 def test_restore_focus_sets_script_and_focus(self, test_context: OrcaTestContext) -> None:
315 """Test ActionPresenter._restore_focus properly restores script and focus."""
316
317 essential_modules: dict[str, MagicMock] = self._setup_dependencies(test_context)
318 from orca.action_presenter import ActionPresenter
319
320 presenter = ActionPresenter()
321 mock_obj = test_context.Mock()
322 mock_window = test_context.Mock()
323 mock_app = test_context.Mock()
324 mock_script = test_context.Mock()
325
326 presenter._obj = mock_obj
327 presenter._window = mock_window
328
329 ax_utilities_mock = test_context.Mock()
330 ax_utilities_mock.get_application = test_context.Mock(return_value=mock_app)
331 test_context.patch("orca.action_presenter.AXUtilities", new=ax_utilities_mock)
332
333 script_manager_instance = essential_modules["orca.script_manager"].get_manager()
334 script_manager_instance.get_script.return_value = mock_script
335
336 focus_manager_instance = essential_modules["orca.focus_manager"].get_manager()
337
338 presenter._restore_focus()
339
340 ax_utilities_mock.get_application.assert_called_once_with(mock_obj)
341 script_manager_instance.get_script.assert_called_once_with(mock_app, mock_obj)
342 script_manager_instance.set_active_script.assert_called_once_with(
343 mock_script,
344 "Action Presenter list is being destroyed",
345 )
346 focus_manager_instance.clear_state.assert_called_once_with(
347 "Action Presenter list is being destroyed",
348 )
349 focus_manager_instance.set_active_window.assert_called_once_with(mock_window)
350 focus_manager_instance.set_locus_of_focus.assert_called_once_with(None, mock_obj)
351
352 def test_clear_gui_and_restore_focus(self, test_context: OrcaTestContext) -> None:
353 """Test ActionPresenter._clear_gui_and_restore_focus clears gui and calls restore."""
354
355 self._setup_dependencies(test_context)
356 from orca.action_presenter import ActionPresenter
357
358 presenter = ActionPresenter()
359 presenter._gui = test_context.Mock()
360
361 mock_restore_focus = test_context.patch_object(
362 presenter,
363 "_restore_focus",
364 return_value=None,
365 )
366
367 presenter._clear_gui_and_restore_focus()
368
369 assert presenter._gui is None
370 mock_restore_focus.assert_called_once()