| 1 |
# Unit tests for notification_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=wrong-import-position |
| 22 |
# pylint: disable=import-outside-toplevel |
| 23 |
# pylint: disable=too-many-public-methods |
| 24 |
# pylint: disable=too-many-statements |
| 25 |
# pylint: disable=protected-access |
| 26 |
# pylint: disable=too-many-arguments |
| 27 |
# pylint: disable=too-many-positional-arguments |
| 28 |
# pylint: disable=too-many-locals |
| 29 |
# pylint: disable=too-many-lines |
| 30 |
|
| 31 |
"""Unit tests for notification_presenter.py methods.""" |
| 32 |
|
| 33 |
from __future__ import annotations |
| 34 |
|
| 35 |
from typing import TYPE_CHECKING |
| 36 |
|
| 37 |
import pytest |
| 38 |
|
| 39 |
if TYPE_CHECKING: |
| 40 |
from unittest.mock import MagicMock |
| 41 |
|
| 42 |
from .orca_test_context import OrcaTestContext |
| 43 |
|
| 44 |
|
| 45 |
class Fake: |
| 46 |
"""Stub used as a stand-in for objects that need an identity.""" |
| 47 |
|
| 48 |
|
| 49 |
@pytest.mark.unit |
| 50 |
class TestNotificationPresenter: |
| 51 |
"""Test NotificationPresenter class methods.""" |
| 52 |
|
| 53 |
def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, MagicMock]: |
| 54 |
"""Returns all dependencies needed for NotificationPresenter testing.""" |
| 55 |
|
| 56 |
additional_modules = [ |
| 57 |
"gi", |
| 58 |
"gi.repository", |
| 59 |
"gi.repository.Atspi", |
| 60 |
"gi.repository.GObject", |
| 61 |
"gi.repository.Gtk", |
| 62 |
"orca.braille_presenter", |
| 63 |
"orca.presentation_manager", |
| 64 |
] |
| 65 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 66 |
|
| 67 |
gi_mock = essential_modules["gi"] |
| 68 |
gi_mock.require_version = test_context.Mock() |
| 69 |
|
| 70 |
gi_repository_mock = essential_modules["gi.repository"] |
| 71 |
atspi_mock = essential_modules["gi.repository.Atspi"] |
| 72 |
atspi_mock.Role = Fake |
| 73 |
atspi_mock.Accessible = Fake |
| 74 |
atspi_mock.MatchRule = Fake |
| 75 |
atspi_mock.Relation = Fake |
| 76 |
gi_repository_mock.Atspi = atspi_mock |
| 77 |
|
| 78 |
gobject_mock = essential_modules["gi.repository.GObject"] |
| 79 |
gobject_mock.TYPE_STRING = str |
| 80 |
|
| 81 |
gtk_mock = essential_modules["gi.repository.Gtk"] |
| 82 |
|
| 83 |
gtk_mock.DialogFlags = test_context.Mock() |
| 84 |
gtk_mock.DialogFlags.MODAL = 1 |
| 85 |
gtk_mock.ResponseType = test_context.Mock() |
| 86 |
gtk_mock.ResponseType.APPLY = -10 |
| 87 |
gtk_mock.ResponseType.CLOSE = -7 |
| 88 |
gtk_mock.STOCK_CLEAR = "gtk-clear" |
| 89 |
gtk_mock.STOCK_CLOSE = "gtk-close" |
| 90 |
|
| 91 |
dialog_mock = test_context.Mock() |
| 92 |
dialog_mock.set_default_size = test_context.Mock() |
| 93 |
dialog_mock.get_content_area = test_context.Mock() |
| 94 |
dialog_mock.connect = test_context.Mock() |
| 95 |
dialog_mock.show_all = test_context.Mock() |
| 96 |
dialog_mock.present_with_time = test_context.Mock() |
| 97 |
dialog_mock.destroy = test_context.Mock() |
| 98 |
|
| 99 |
content_area_mock = test_context.Mock() |
| 100 |
content_area_mock.add = test_context.Mock() |
| 101 |
dialog_mock.get_content_area.return_value = content_area_mock |
| 102 |
|
| 103 |
grid_mock = test_context.Mock() |
| 104 |
scrolled_window_mock = test_context.Mock() |
| 105 |
scrolled_window_mock.add = test_context.Mock() |
| 106 |
|
| 107 |
tree_view_mock = test_context.Mock() |
| 108 |
tree_view_mock.set_hexpand = test_context.Mock() |
| 109 |
tree_view_mock.set_vexpand = test_context.Mock() |
| 110 |
tree_view_mock.append_column = test_context.Mock() |
| 111 |
tree_view_mock.set_model = test_context.Mock() |
| 112 |
|
| 113 |
cell_renderer_mock = test_context.Mock() |
| 114 |
tree_column_mock = test_context.Mock() |
| 115 |
tree_column_mock.set_sort_column_id = test_context.Mock() |
| 116 |
|
| 117 |
list_store_mock = test_context.Mock() |
| 118 |
list_store_mock.append = test_context.Mock(return_value="mock_iter") |
| 119 |
list_store_mock.set_value = test_context.Mock() |
| 120 |
list_store_mock.clear = test_context.Mock() |
| 121 |
|
| 122 |
gtk_mock.Dialog = test_context.Mock(return_value=dialog_mock) |
| 123 |
gtk_mock.Grid = test_context.Mock(return_value=grid_mock) |
| 124 |
gtk_mock.ScrolledWindow = test_context.Mock(return_value=scrolled_window_mock) |
| 125 |
gtk_mock.TreeView = test_context.Mock(return_value=tree_view_mock) |
| 126 |
gtk_mock.CellRendererText = test_context.Mock(return_value=cell_renderer_mock) |
| 127 |
gtk_mock.TreeViewColumn = test_context.Mock(return_value=tree_column_mock) |
| 128 |
gtk_mock.ListStore = test_context.Mock(return_value=list_store_mock) |
| 129 |
|
| 130 |
cmdnames_mock = essential_modules["orca.cmdnames"] |
| 131 |
cmdnames_mock.NOTIFICATION_MESSAGES_LAST = "presentLastNotification" |
| 132 |
cmdnames_mock.NOTIFICATION_MESSAGES_NEXT = "presentNextNotification" |
| 133 |
cmdnames_mock.NOTIFICATION_MESSAGES_PREVIOUS = "presentPreviousNotification" |
| 134 |
cmdnames_mock.NOTIFICATION_MESSAGES_LIST = "showNotificationsList" |
| 135 |
|
| 136 |
dbus_service_mock = essential_modules["orca.dbus_service"] |
| 137 |
controller_mock = test_context.Mock() |
| 138 |
controller_mock.register_decorated_module = test_context.Mock() |
| 139 |
dbus_service_mock.get_remote_controller = test_context.Mock(return_value=controller_mock) |
| 140 |
dbus_service_mock.command = lambda func: func |
| 141 |
|
| 142 |
debug_mock = essential_modules["orca.debug"] |
| 143 |
debug_mock.print_message = test_context.Mock() |
| 144 |
debug_mock.print_tokens = test_context.Mock() |
| 145 |
debug_mock.LEVEL_INFO = 800 |
| 146 |
|
| 147 |
guilabels_mock = essential_modules["orca.guilabels"] |
| 148 |
guilabels_mock.notifications_count = test_context.Mock(return_value="Notifications (2)") |
| 149 |
guilabels_mock.NOTIFICATIONS_COLUMN_HEADER = "Message" |
| 150 |
guilabels_mock.NOTIFICATIONS_RECEIVED_TIME = "Time" |
| 151 |
|
| 152 |
input_event_mock = essential_modules["orca.input_event"] |
| 153 |
input_event_handler_class = test_context.Mock() |
| 154 |
input_event_handler_instance = test_context.Mock() |
| 155 |
input_event_handler_class.return_value = input_event_handler_instance |
| 156 |
input_event_mock.InputEventHandler = input_event_handler_class |
| 157 |
input_event_mock.InputEvent = test_context.mocker.MagicMock |
| 158 |
|
| 159 |
keybindings_mock = essential_modules["orca.keybindings"] |
| 160 |
|
| 161 |
def create_keybindings_instance(): |
| 162 |
instance = test_context.Mock() |
| 163 |
instance.is_empty = test_context.Mock(return_value=True) |
| 164 |
instance.remove_key_grabs = test_context.Mock() |
| 165 |
instance.add = test_context.Mock() |
| 166 |
return instance |
| 167 |
|
| 168 |
keybindings_mock.KeyBindings = test_context.Mock(side_effect=create_keybindings_instance) |
| 169 |
keybinding_instance = test_context.Mock() |
| 170 |
keybindings_mock.KeyBinding = test_context.Mock(return_value=keybinding_instance) |
| 171 |
keybindings_mock.DEFAULT_MODIFIER_MASK = 1 |
| 172 |
keybindings_mock.NO_MODIFIER_MASK = 0 |
| 173 |
|
| 174 |
messages_mock = essential_modules["orca.messages"] |
| 175 |
messages_mock.NOTIFICATION_NO_MESSAGES = "No notification messages" |
| 176 |
messages_mock.NOTIFICATION_LIST_TOP = "Beginning of notification list" |
| 177 |
messages_mock.NOTIFICATION_LIST_BOTTOM = "End of notification list" |
| 178 |
messages_mock.seconds_ago = lambda x: f"{int(x)} seconds ago" |
| 179 |
messages_mock.minutes_ago = lambda x: f"{x} minutes ago" |
| 180 |
messages_mock.hours_ago = lambda x: f"{x} hours ago" |
| 181 |
messages_mock.days_ago = lambda x: f"{x} days ago" |
| 182 |
|
| 183 |
essential_modules["dialog"] = dialog_mock |
| 184 |
essential_modules["list_store"] = list_store_mock |
| 185 |
essential_modules["tree_view"] = tree_view_mock |
| 186 |
|
| 187 |
return essential_modules |
| 188 |
|
| 189 |
def test_init(self, test_context: OrcaTestContext) -> None: |
| 190 |
"""Test NotificationPresenter.__init__.""" |
| 191 |
|
| 192 |
self._setup_dependencies(test_context) |
| 193 |
from orca.notification_presenter import NotificationPresenter |
| 194 |
|
| 195 |
mock_controller = test_context.Mock() |
| 196 |
mock_get_controller = test_context.patch( |
| 197 |
"orca.notification_presenter.dbus_service.get_remote_controller", |
| 198 |
return_value=mock_controller, |
| 199 |
) |
| 200 |
presenter = NotificationPresenter() |
| 201 |
|
| 202 |
assert presenter._gui is None |
| 203 |
assert presenter._max_size == 55 |
| 204 |
assert presenter._notifications == [] |
| 205 |
assert presenter._current_index == -1 |
| 206 |
|
| 207 |
# D-Bus registration happens during setup() |
| 208 |
presenter.set_up_commands() |
| 209 |
mock_get_controller.assert_called() |
| 210 |
mock_controller.register_decorated_module.assert_called() |
| 211 |
|
| 212 |
@pytest.mark.parametrize( |
| 213 |
"_refresh,check_handler_names", |
| 214 |
[ |
| 215 |
pytest.param(True, True, id="refresh_true"), |
| 216 |
pytest.param(False, False, id="no_refresh"), |
| 217 |
], |
| 218 |
) |
| 219 |
def test_get_handlers( |
| 220 |
self, |
| 221 |
test_context: OrcaTestContext, |
| 222 |
_refresh: bool, |
| 223 |
check_handler_names: bool, |
| 224 |
) -> None: |
| 225 |
"""Test NotificationPresenter.get_handlers returns empty dict. |
| 226 |
|
| 227 |
Note: Commands are now registered directly with CommandManager, |
| 228 |
so get_handlers() returns an empty dict. |
| 229 |
""" |
| 230 |
|
| 231 |
self._setup_dependencies(test_context) |
| 232 |
from orca import command_manager |
| 233 |
from orca.notification_presenter import NotificationPresenter |
| 234 |
|
| 235 |
presenter = NotificationPresenter() |
| 236 |
presenter.set_up_commands() |
| 237 |
|
| 238 |
if check_handler_names: |
| 239 |
manager = command_manager.get_manager() |
| 240 |
expected_commands = [ |
| 241 |
"present_last_notification", |
| 242 |
"present_next_notification", |
| 243 |
"present_previous_notification", |
| 244 |
"show_notification_list", |
| 245 |
] |
| 246 |
for command_name in expected_commands: |
| 247 |
assert manager.get_keyboard_command(command_name) is not None |
| 248 |
|
| 249 |
def test_save_notification_basic(self, test_context: OrcaTestContext) -> None: |
| 250 |
"""Test NotificationPresenter.save_notification.""" |
| 251 |
|
| 252 |
self._setup_dependencies(test_context) |
| 253 |
from orca.notification_presenter import NotificationPresenter |
| 254 |
|
| 255 |
presenter = NotificationPresenter() |
| 256 |
test_message = "Test notification message" |
| 257 |
test_context.patch("time.time", return_value=1234567890) |
| 258 |
presenter.save_notification(test_message) |
| 259 |
|
| 260 |
assert len(presenter._notifications) == 1 |
| 261 |
message, timestamp = presenter._notifications[0] |
| 262 |
assert message == test_message |
| 263 |
assert timestamp == 1234567890 |
| 264 |
|
| 265 |
def test_save_notification_max_size_limit(self, test_context: OrcaTestContext) -> None: |
| 266 |
"""Test NotificationPresenter.save_notification respects max size limit.""" |
| 267 |
|
| 268 |
self._setup_dependencies(test_context) |
| 269 |
from orca.notification_presenter import NotificationPresenter |
| 270 |
|
| 271 |
presenter = NotificationPresenter() |
| 272 |
presenter._max_size = 3 |
| 273 |
|
| 274 |
test_context.patch("time.time", return_value=1234567890) |
| 275 |
presenter.save_notification("Message 1") |
| 276 |
presenter.save_notification("Message 2") |
| 277 |
presenter.save_notification("Message 3") |
| 278 |
presenter.save_notification("Message 4") |
| 279 |
|
| 280 |
assert len(presenter._notifications) == 3 |
| 281 |
messages = [msg for msg, _ in presenter._notifications] |
| 282 |
assert messages == ["Message 2", "Message 3", "Message 4"] |
| 283 |
|
| 284 |
def test_clear_list(self, test_context: OrcaTestContext) -> None: |
| 285 |
"""Test NotificationPresenter.clear_list.""" |
| 286 |
|
| 287 |
self._setup_dependencies(test_context) |
| 288 |
from orca.notification_presenter import NotificationPresenter |
| 289 |
|
| 290 |
presenter = NotificationPresenter() |
| 291 |
presenter._notifications = [("Test 1", 123.0), ("Test 2", 456.0)] |
| 292 |
presenter._current_index = -2 |
| 293 |
presenter.clear_list() |
| 294 |
|
| 295 |
assert presenter._notifications == [] |
| 296 |
assert presenter._current_index == -1 |
| 297 |
|
| 298 |
def test_setup_commands(self, test_context: OrcaTestContext) -> None: |
| 299 |
"""Test that commands are registered with CommandManager during setup.""" |
| 300 |
|
| 301 |
self._setup_dependencies(test_context) |
| 302 |
from orca import command_manager |
| 303 |
from orca.notification_presenter import NotificationPresenter |
| 304 |
|
| 305 |
presenter = NotificationPresenter() |
| 306 |
presenter.set_up_commands() |
| 307 |
cmd_manager = command_manager.get_manager() |
| 308 |
|
| 309 |
expected_commands = [ |
| 310 |
"present_last_notification", |
| 311 |
"present_next_notification", |
| 312 |
"present_previous_notification", |
| 313 |
"show_notification_list", |
| 314 |
] |
| 315 |
for command_name in expected_commands: |
| 316 |
assert cmd_manager.get_keyboard_command(command_name) is not None |
| 317 |
|
| 318 |
@pytest.mark.parametrize( |
| 319 |
"time_diff, expected_result", |
| 320 |
[ |
| 321 |
pytest.param(30, "30 seconds ago", id="thirty_seconds"), |
| 322 |
pytest.param(90, "2 minutes ago", id="ninety_seconds"), |
| 323 |
pytest.param(1800, "30 minutes ago", id="thirty_minutes"), |
| 324 |
pytest.param(7200, "2 hours ago", id="two_hours"), |
| 325 |
pytest.param(90000, "1 days ago", id="one_day"), |
| 326 |
], |
| 327 |
) |
| 328 |
def test_timestamp_to_string(self, test_context, time_diff, expected_result) -> None: |
| 329 |
"""Test NotificationPresenter._timestamp_to_string.""" |
| 330 |
|
| 331 |
self._setup_dependencies(test_context) |
| 332 |
from orca.notification_presenter import NotificationPresenter |
| 333 |
|
| 334 |
presenter = NotificationPresenter() |
| 335 |
current_time = 1234567890 |
| 336 |
test_timestamp = current_time - time_diff |
| 337 |
test_context.patch("time.time", return_value=current_time) |
| 338 |
result = presenter._timestamp_to_string(test_timestamp) |
| 339 |
assert result == expected_result |
| 340 |
|
| 341 |
def test_present_last_notification_no_messages(self, test_context: OrcaTestContext) -> None: |
| 342 |
"""Test NotificationPresenter.present_last_notification with no messages.""" |
| 343 |
|
| 344 |
deps = self._setup_dependencies(test_context) |
| 345 |
from orca.notification_presenter import NotificationPresenter |
| 346 |
|
| 347 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 348 |
pres_manager.present_message.reset_mock() |
| 349 |
mock_script = test_context.Mock() |
| 350 |
presenter = NotificationPresenter() |
| 351 |
result = presenter.present_last_notification(mock_script, None, True) |
| 352 |
|
| 353 |
assert result is True |
| 354 |
pres_manager.present_message.assert_called_once_with("No notification messages") |
| 355 |
|
| 356 |
def test_present_last_notification_with_messages(self, test_context: OrcaTestContext) -> None: |
| 357 |
"""Test NotificationPresenter.present_last_notification with messages.""" |
| 358 |
|
| 359 |
essential_modules = self._setup_dependencies(test_context) |
| 360 |
from orca.notification_presenter import NotificationPresenter |
| 361 |
|
| 362 |
mock_script = test_context.Mock() |
| 363 |
presenter = NotificationPresenter() |
| 364 |
current_time = 1234567890 |
| 365 |
test_message = "Last notification" |
| 366 |
|
| 367 |
test_context.patch("time.time", return_value=current_time - 60) |
| 368 |
presenter.save_notification(test_message) |
| 369 |
|
| 370 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 371 |
pres_manager.present_message.reset_mock() |
| 372 |
test_context.patch("time.time", return_value=current_time) |
| 373 |
result = presenter.present_last_notification(mock_script, None, True) |
| 374 |
|
| 375 |
assert result is True |
| 376 |
pres_manager.present_message.assert_called_once_with(f"{test_message} 1 minutes ago") |
| 377 |
assert presenter._current_index == -1 |
| 378 |
|
| 379 |
def test_present_last_notification_no_notify(self, test_context: OrcaTestContext) -> None: |
| 380 |
"""Test NotificationPresenter.present_last_notification with notify_user=False.""" |
| 381 |
|
| 382 |
essential_modules = self._setup_dependencies(test_context) |
| 383 |
from orca.notification_presenter import NotificationPresenter |
| 384 |
|
| 385 |
mock_script = test_context.Mock() |
| 386 |
presenter = NotificationPresenter() |
| 387 |
pres_manager = essential_modules["orca.presentation_manager"].get_manager() |
| 388 |
pres_manager.present_message.reset_mock() |
| 389 |
result = presenter.present_last_notification(mock_script, None, False) |
| 390 |
|
| 391 |
assert result is True |
| 392 |
pres_manager.present_message.assert_not_called() |
| 393 |
|
| 394 |
def test_present_previous_notification_no_messages(self, test_context: OrcaTestContext) -> None: |
| 395 |
"""Test NotificationPresenter.present_previous_notification with no messages.""" |
| 396 |
|
| 397 |
deps = self._setup_dependencies(test_context) |
| 398 |
from orca.notification_presenter import NotificationPresenter |
| 399 |
|
| 400 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 401 |
pres_manager.present_message.reset_mock() |
| 402 |
mock_script = test_context.Mock() |
| 403 |
presenter = NotificationPresenter() |
| 404 |
result = presenter.present_previous_notification(mock_script, None, True) |
| 405 |
|
| 406 |
assert result is True |
| 407 |
pres_manager.present_message.assert_called_once_with("No notification messages") |
| 408 |
|
| 409 |
def test_present_previous_notification_at_top(self, test_context: OrcaTestContext) -> None: |
| 410 |
"""Test NotificationPresenter.present_previous_notification at list top.""" |
| 411 |
|
| 412 |
deps = self._setup_dependencies(test_context) |
| 413 |
from orca.notification_presenter import NotificationPresenter |
| 414 |
|
| 415 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 416 |
mock_script = test_context.Mock() |
| 417 |
presenter = NotificationPresenter() |
| 418 |
current_time = 1234567890 |
| 419 |
|
| 420 |
test_context.patch("time.time", return_value=current_time - 60) |
| 421 |
presenter.save_notification("Message 1") |
| 422 |
presenter.save_notification("Message 2") |
| 423 |
presenter._current_index = 0 |
| 424 |
test_context.patch("time.time", return_value=current_time) |
| 425 |
pres_manager.present_message.reset_mock() |
| 426 |
result = presenter.present_previous_notification(mock_script, None, True) |
| 427 |
|
| 428 |
assert result is True |
| 429 |
assert pres_manager.present_message.call_count == 2 |
| 430 |
pres_manager.present_message.assert_any_call("Beginning of notification list") |
| 431 |
pres_manager.present_message.assert_any_call("Message 1 1 minutes ago") |
| 432 |
|
| 433 |
def test_present_previous_notification_normal_navigation( |
| 434 |
self, |
| 435 |
test_context: OrcaTestContext, |
| 436 |
) -> None: |
| 437 |
"""Test NotificationPresenter.present_previous_notification normal navigation.""" |
| 438 |
|
| 439 |
deps = self._setup_dependencies(test_context) |
| 440 |
from orca.notification_presenter import NotificationPresenter |
| 441 |
|
| 442 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 443 |
mock_script = test_context.Mock() |
| 444 |
presenter = NotificationPresenter() |
| 445 |
current_time = 1234567890 |
| 446 |
|
| 447 |
test_context.patch("time.time", return_value=current_time - 60) |
| 448 |
presenter.save_notification("Message 1") |
| 449 |
presenter.save_notification("Message 2") |
| 450 |
presenter._current_index = -1 |
| 451 |
test_context.patch("time.time", return_value=current_time) |
| 452 |
pres_manager.present_message.reset_mock() |
| 453 |
result = presenter.present_previous_notification(mock_script, None, True) |
| 454 |
|
| 455 |
assert result is True |
| 456 |
assert presenter._current_index == -2 |
| 457 |
pres_manager.present_message.assert_called_once_with("Message 1 1 minutes ago") |
| 458 |
|
| 459 |
@pytest.mark.parametrize( |
| 460 |
"method_name,start_index", |
| 461 |
[ |
| 462 |
pytest.param("present_previous_notification", -1, id="previous_index_error"), |
| 463 |
pytest.param("present_next_notification", 0, id="next_index_error"), |
| 464 |
], |
| 465 |
) |
| 466 |
def test_navigation_index_error( |
| 467 |
self, |
| 468 |
test_context: OrcaTestContext, |
| 469 |
method_name, |
| 470 |
start_index, |
| 471 |
) -> None: |
| 472 |
"""Test notification navigation methods handle IndexError scenarios.""" |
| 473 |
deps = self._setup_dependencies(test_context) |
| 474 |
from orca.notification_presenter import NotificationPresenter |
| 475 |
|
| 476 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 477 |
mock_script = test_context.Mock() |
| 478 |
presenter = NotificationPresenter() |
| 479 |
current_time = 1234567890 |
| 480 |
|
| 481 |
test_context.patch("time.time", return_value=current_time - 60) |
| 482 |
presenter.save_notification("Message 1") |
| 483 |
presenter._current_index = start_index |
| 484 |
|
| 485 |
original_notifications = presenter._notifications.copy() |
| 486 |
presenter._notifications = [] |
| 487 |
test_context.patch("time.time", return_value=current_time) |
| 488 |
pres_manager.present_message.reset_mock() |
| 489 |
|
| 490 |
method = getattr(presenter, method_name) |
| 491 |
result = method(mock_script, None, True) |
| 492 |
|
| 493 |
presenter._notifications = original_notifications |
| 494 |
assert result is True |
| 495 |
pres_manager.present_message.assert_called_with("No notification messages") |
| 496 |
|
| 497 |
def test_present_next_notification_no_messages(self, test_context: OrcaTestContext) -> None: |
| 498 |
"""Test NotificationPresenter.present_next_notification with no messages.""" |
| 499 |
|
| 500 |
deps = self._setup_dependencies(test_context) |
| 501 |
from orca.notification_presenter import NotificationPresenter |
| 502 |
|
| 503 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 504 |
pres_manager.present_message.reset_mock() |
| 505 |
mock_script = test_context.Mock() |
| 506 |
presenter = NotificationPresenter() |
| 507 |
result = presenter.present_next_notification(mock_script, None, True) |
| 508 |
|
| 509 |
assert result is True |
| 510 |
pres_manager.present_message.assert_called_once_with("No notification messages") |
| 511 |
|
| 512 |
def test_present_next_notification_at_bottom(self, test_context: OrcaTestContext) -> None: |
| 513 |
"""Test NotificationPresenter.present_next_notification at list bottom.""" |
| 514 |
|
| 515 |
deps = self._setup_dependencies(test_context) |
| 516 |
from orca.notification_presenter import NotificationPresenter |
| 517 |
|
| 518 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 519 |
mock_script = test_context.Mock() |
| 520 |
presenter = NotificationPresenter() |
| 521 |
current_time = 1234567890 |
| 522 |
|
| 523 |
test_context.patch("time.time", return_value=current_time - 60) |
| 524 |
presenter.save_notification("Message 1") |
| 525 |
presenter.save_notification("Message 2") |
| 526 |
presenter._current_index = -1 |
| 527 |
test_context.patch("time.time", return_value=current_time) |
| 528 |
pres_manager.present_message.reset_mock() |
| 529 |
result = presenter.present_next_notification(mock_script, None, True) |
| 530 |
|
| 531 |
assert result is True |
| 532 |
assert pres_manager.present_message.call_count == 2 |
| 533 |
pres_manager.present_message.assert_any_call("End of notification list") |
| 534 |
pres_manager.present_message.assert_any_call("Message 2 1 minutes ago") |
| 535 |
|
| 536 |
def test_present_next_notification_normal_navigation( |
| 537 |
self, |
| 538 |
test_context: OrcaTestContext, |
| 539 |
) -> None: |
| 540 |
"""Test NotificationPresenter.present_next_notification normal navigation.""" |
| 541 |
|
| 542 |
deps = self._setup_dependencies(test_context) |
| 543 |
from orca.notification_presenter import NotificationPresenter |
| 544 |
|
| 545 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 546 |
mock_script = test_context.Mock() |
| 547 |
presenter = NotificationPresenter() |
| 548 |
current_time = 1234567890 |
| 549 |
|
| 550 |
test_context.patch("time.time", return_value=current_time - 60) |
| 551 |
presenter.save_notification("Message 1") |
| 552 |
presenter.save_notification("Message 2") |
| 553 |
presenter._current_index = -2 |
| 554 |
test_context.patch("time.time", return_value=current_time) |
| 555 |
pres_manager.present_message.reset_mock() |
| 556 |
result = presenter.present_next_notification(mock_script, None, True) |
| 557 |
|
| 558 |
assert result is True |
| 559 |
assert presenter._current_index == -1 |
| 560 |
pres_manager.present_message.assert_called_once_with("Message 2 1 minutes ago") |
| 561 |
|
| 562 |
def test_show_notification_list_no_messages(self, test_context: OrcaTestContext) -> None: |
| 563 |
"""Test NotificationPresenter.show_notification_list with no messages.""" |
| 564 |
|
| 565 |
deps = self._setup_dependencies(test_context) |
| 566 |
from orca.notification_presenter import NotificationPresenter |
| 567 |
|
| 568 |
pres_manager = deps["orca.presentation_manager"].get_manager() |
| 569 |
pres_manager.present_message.reset_mock() |
| 570 |
mock_script = test_context.Mock() |
| 571 |
presenter = NotificationPresenter() |
| 572 |
result = presenter.show_notification_list(mock_script, None, True) |
| 573 |
|
| 574 |
assert result is True |
| 575 |
pres_manager.present_message.assert_called_once_with("No notification messages") |
| 576 |
|
| 577 |
def test_show_notification_list_create_gui(self, test_context: OrcaTestContext) -> None: |
| 578 |
"""Test NotificationPresenter.show_notification_list creates GUI.""" |
| 579 |
|
| 580 |
self._setup_dependencies(test_context) |
| 581 |
from orca.notification_presenter import NotificationPresenter |
| 582 |
|
| 583 |
mock_script = test_context.Mock() |
| 584 |
mock_script.present_message = test_context.Mock() |
| 585 |
presenter = NotificationPresenter() |
| 586 |
current_time = 1234567890 |
| 587 |
|
| 588 |
test_context.patch("time.time", return_value=current_time - 60) |
| 589 |
presenter.save_notification("Message 1") |
| 590 |
presenter.save_notification("Message 2") |
| 591 |
test_context.patch("time.time", return_value=current_time) |
| 592 |
result = presenter.show_notification_list(mock_script, None, True) |
| 593 |
|
| 594 |
assert result is True |
| 595 |
assert presenter._gui is not None |
| 596 |
|
| 597 |
def test_show_notification_list_existing_gui(self, test_context: OrcaTestContext) -> None: |
| 598 |
"""Test NotificationPresenter.show_notification_list with existing GUI.""" |
| 599 |
|
| 600 |
self._setup_dependencies(test_context) |
| 601 |
from orca.notification_presenter import NotificationPresenter |
| 602 |
|
| 603 |
mock_script = test_context.Mock() |
| 604 |
mock_script.present_message = test_context.Mock() |
| 605 |
presenter = NotificationPresenter() |
| 606 |
current_time = 1234567890 |
| 607 |
|
| 608 |
test_context.patch("time.time", return_value=current_time - 60) |
| 609 |
presenter.save_notification("Message 1") |
| 610 |
|
| 611 |
mock_gui = test_context.Mock() |
| 612 |
mock_gui.show_gui = test_context.Mock() |
| 613 |
presenter._gui = mock_gui |
| 614 |
test_context.patch("time.time", return_value=current_time) |
| 615 |
result = presenter.show_notification_list(mock_script, None, True) |
| 616 |
|
| 617 |
assert result is True |
| 618 |
assert presenter._gui is mock_gui |
| 619 |
mock_gui.show_gui.assert_called_once() |
| 620 |
|
| 621 |
def test_on_dialog_destroyed(self, test_context: OrcaTestContext) -> None: |
| 622 |
"""Test NotificationPresenter.on_dialog_destroyed.""" |
| 623 |
|
| 624 |
self._setup_dependencies(test_context) |
| 625 |
from orca.notification_presenter import NotificationPresenter |
| 626 |
|
| 627 |
presenter = NotificationPresenter() |
| 628 |
mock_dialog = test_context.Mock() |
| 629 |
presenter._gui = test_context.Mock() |
| 630 |
presenter.on_dialog_destroyed(mock_dialog) |
| 631 |
|
| 632 |
assert presenter._gui is None |
| 633 |
|
| 634 |
|
| 635 |
@pytest.mark.unit |
| 636 |
class TestNotificationListGUI: |
| 637 |
"""Test NotificationListGUI class methods.""" |
| 638 |
|
| 639 |
def _setup_dependencies(self, test_context: OrcaTestContext): |
| 640 |
"""Returns all dependencies needed for NotificationPresenter testing.""" |
| 641 |
|
| 642 |
additional_modules = [ |
| 643 |
"gi", |
| 644 |
"gi.repository", |
| 645 |
"gi.repository.Atspi", |
| 646 |
"gi.repository.GObject", |
| 647 |
"gi.repository.Gtk", |
| 648 |
"orca.braille_presenter", |
| 649 |
"orca.presentation_manager", |
| 650 |
] |
| 651 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 652 |
|
| 653 |
gi_mock = essential_modules["gi"] |
| 654 |
gi_mock.require_version = test_context.Mock() |
| 655 |
|
| 656 |
gi_repository_mock = essential_modules["gi.repository"] |
| 657 |
atspi_mock = essential_modules["gi.repository.Atspi"] |
| 658 |
atspi_mock.Role = Fake |
| 659 |
atspi_mock.Accessible = Fake |
| 660 |
atspi_mock.MatchRule = Fake |
| 661 |
atspi_mock.Relation = Fake |
| 662 |
gi_repository_mock.Atspi = atspi_mock |
| 663 |
|
| 664 |
gobject_mock = essential_modules["gi.repository.GObject"] |
| 665 |
gobject_mock.TYPE_STRING = str |
| 666 |
|
| 667 |
gtk_mock = essential_modules["gi.repository.Gtk"] |
| 668 |
|
| 669 |
gtk_mock.DialogFlags = test_context.Mock() |
| 670 |
gtk_mock.DialogFlags.MODAL = 1 |
| 671 |
gtk_mock.ResponseType = test_context.Mock() |
| 672 |
gtk_mock.ResponseType.APPLY = -10 |
| 673 |
gtk_mock.ResponseType.CLOSE = -7 |
| 674 |
gtk_mock.STOCK_CLEAR = "gtk-clear" |
| 675 |
gtk_mock.STOCK_CLOSE = "gtk-close" |
| 676 |
|
| 677 |
dialog_mock = test_context.Mock() |
| 678 |
dialog_mock.set_default_size = test_context.Mock() |
| 679 |
dialog_mock.get_content_area = test_context.Mock() |
| 680 |
dialog_mock.connect = test_context.Mock() |
| 681 |
dialog_mock.show_all = test_context.Mock() |
| 682 |
dialog_mock.present_with_time = test_context.Mock() |
| 683 |
dialog_mock.destroy = test_context.Mock() |
| 684 |
|
| 685 |
content_area_mock = test_context.Mock() |
| 686 |
content_area_mock.add = test_context.Mock() |
| 687 |
dialog_mock.get_content_area.return_value = content_area_mock |
| 688 |
|
| 689 |
grid_mock = test_context.Mock() |
| 690 |
scrolled_window_mock = test_context.Mock() |
| 691 |
scrolled_window_mock.add = test_context.Mock() |
| 692 |
|
| 693 |
tree_view_mock = test_context.Mock() |
| 694 |
tree_view_mock.set_hexpand = test_context.Mock() |
| 695 |
tree_view_mock.set_vexpand = test_context.Mock() |
| 696 |
tree_view_mock.append_column = test_context.Mock() |
| 697 |
tree_view_mock.set_model = test_context.Mock() |
| 698 |
|
| 699 |
list_store_mock = test_context.Mock() |
| 700 |
list_store_mock.clear = test_context.Mock() |
| 701 |
list_store_mock.append = test_context.Mock() |
| 702 |
|
| 703 |
tree_view_column_mock = test_context.Mock() |
| 704 |
cell_renderer_text_mock = test_context.Mock() |
| 705 |
|
| 706 |
gtk_mock.Dialog = test_context.Mock(return_value=dialog_mock) |
| 707 |
gtk_mock.Grid = test_context.Mock(return_value=grid_mock) |
| 708 |
gtk_mock.ScrolledWindow = test_context.Mock(return_value=scrolled_window_mock) |
| 709 |
gtk_mock.TreeView = test_context.Mock(return_value=tree_view_mock) |
| 710 |
gtk_mock.ListStore = test_context.Mock(return_value=list_store_mock) |
| 711 |
gtk_mock.TreeViewColumn = test_context.Mock(return_value=tree_view_column_mock) |
| 712 |
gtk_mock.CellRendererText = test_context.Mock(return_value=cell_renderer_text_mock) |
| 713 |
|
| 714 |
focus_manager_instance = test_context.Mock() |
| 715 |
focus_manager_instance.get_locus_of_focus = test_context.Mock(return_value=None) |
| 716 |
essential_modules["orca.focus_manager"].get_manager = test_context.Mock( |
| 717 |
return_value=focus_manager_instance, |
| 718 |
) |
| 719 |
|
| 720 |
script_manager_instance = test_context.Mock() |
| 721 |
script_instance = test_context.Mock() |
| 722 |
script_manager_instance.get_active_script = test_context.Mock(return_value=script_instance) |
| 723 |
essential_modules["orca.script_manager"].get_manager = test_context.Mock( |
| 724 |
return_value=script_manager_instance, |
| 725 |
) |
| 726 |
|
| 727 |
input_event_handler_mock = test_context.Mock() |
| 728 |
essential_modules["orca.input_event"].InputEventHandler = test_context.Mock( |
| 729 |
return_value=input_event_handler_mock, |
| 730 |
) |
| 731 |
|
| 732 |
essential_modules[ |
| 733 |
"orca.messages" |
| 734 |
].NOTIFICATION_PRESENTER_MESSAGE_NOT_FOUND = "No notification message found" |
| 735 |
essential_modules[ |
| 736 |
"orca.messages" |
| 737 |
].NOTIFICATION_PRESENTER_MESSAGE_DUPLICATE = "Duplicate notification" |
| 738 |
|
| 739 |
essential_modules["orca.guilabels"].NOTIFICATION_LIST_TITLE = "Notification List" |
| 740 |
essential_modules["dialog"] = dialog_mock |
| 741 |
essential_modules["list_store"] = list_store_mock |
| 742 |
essential_modules["tree_view"] = tree_view_mock |
| 743 |
|
| 744 |
return essential_modules |
| 745 |
|
| 746 |
def test_notification_list_gui_constructor_validation( |
| 747 |
self, |
| 748 |
test_context: OrcaTestContext, |
| 749 |
) -> None: |
| 750 |
"""Test NotificationListGUI class can be imported and instantiated.""" |
| 751 |
|
| 752 |
self._setup_dependencies(test_context) |
| 753 |
orca_i18n_mock = test_context.Mock() |
| 754 |
orca_i18n_mock._ = lambda x: x |
| 755 |
test_context.patch_module("orca.orca_i18n", orca_i18n_mock) |
| 756 |
|
| 757 |
from orca.notification_presenter import NotificationListGUI |
| 758 |
|
| 759 |
assert NotificationListGUI is not None |
| 760 |
|
| 761 |
|
| 762 |
@pytest.mark.unit |
| 763 |
class TestNotificationPresenterModule: |
| 764 |
"""Test module-level functions.""" |
| 765 |
|
| 766 |
def _setup_dependencies(self, test_context: OrcaTestContext): |
| 767 |
"""Returns all dependencies needed for NotificationPresenter testing.""" |
| 768 |
|
| 769 |
additional_modules = [ |
| 770 |
"gi", |
| 771 |
"gi.repository", |
| 772 |
"gi.repository.Atspi", |
| 773 |
"gi.repository.GObject", |
| 774 |
"gi.repository.Gtk", |
| 775 |
"orca.braille_presenter", |
| 776 |
"orca.presentation_manager", |
| 777 |
] |
| 778 |
essential_modules = test_context.setup_shared_dependencies(additional_modules) |
| 779 |
|
| 780 |
gi_mock = essential_modules["gi"] |
| 781 |
gi_mock.require_version = test_context.Mock() |
| 782 |
|
| 783 |
gi_repository_mock = essential_modules["gi.repository"] |
| 784 |
atspi_mock = essential_modules["gi.repository.Atspi"] |
| 785 |
atspi_mock.Role = Fake |
| 786 |
atspi_mock.Accessible = Fake |
| 787 |
atspi_mock.MatchRule = Fake |
| 788 |
atspi_mock.Relation = Fake |
| 789 |
gi_repository_mock.Atspi = atspi_mock |
| 790 |
|
| 791 |
gobject_mock = essential_modules["gi.repository.GObject"] |
| 792 |
gobject_mock.TYPE_STRING = str |
| 793 |
|
| 794 |
gtk_mock = essential_modules["gi.repository.Gtk"] |
| 795 |
|
| 796 |
gtk_mock.DialogFlags = test_context.Mock() |
| 797 |
gtk_mock.DialogFlags.MODAL = 1 |
| 798 |
gtk_mock.ResponseType = test_context.Mock() |
| 799 |
gtk_mock.ResponseType.APPLY = -10 |
| 800 |
gtk_mock.ResponseType.CLOSE = -7 |
| 801 |
gtk_mock.STOCK_CLEAR = "gtk-clear" |
| 802 |
gtk_mock.STOCK_CLOSE = "gtk-close" |
| 803 |
|
| 804 |
dialog_mock = test_context.Mock() |
| 805 |
dialog_mock.set_default_size = test_context.Mock() |
| 806 |
dialog_mock.get_content_area = test_context.Mock() |
| 807 |
dialog_mock.connect = test_context.Mock() |
| 808 |
dialog_mock.show_all = test_context.Mock() |
| 809 |
dialog_mock.present_with_time = test_context.Mock() |
| 810 |
dialog_mock.destroy = test_context.Mock() |
| 811 |
|
| 812 |
content_area_mock = test_context.Mock() |
| 813 |
content_area_mock.add = test_context.Mock() |
| 814 |
dialog_mock.get_content_area.return_value = content_area_mock |
| 815 |
|
| 816 |
grid_mock = test_context.Mock() |
| 817 |
scrolled_window_mock = test_context.Mock() |
| 818 |
scrolled_window_mock.add = test_context.Mock() |
| 819 |
|
| 820 |
tree_view_mock = test_context.Mock() |
| 821 |
tree_view_mock.set_hexpand = test_context.Mock() |
| 822 |
tree_view_mock.set_vexpand = test_context.Mock() |
| 823 |
tree_view_mock.append_column = test_context.Mock() |
| 824 |
tree_view_mock.set_model = test_context.Mock() |
| 825 |
|
| 826 |
list_store_mock = test_context.Mock() |
| 827 |
list_store_mock.clear = test_context.Mock() |
| 828 |
list_store_mock.append = test_context.Mock() |
| 829 |
|
| 830 |
tree_view_column_mock = test_context.Mock() |
| 831 |
cell_renderer_text_mock = test_context.Mock() |
| 832 |
|
| 833 |
gtk_mock.Dialog = test_context.Mock(return_value=dialog_mock) |
| 834 |
gtk_mock.Grid = test_context.Mock(return_value=grid_mock) |
| 835 |
gtk_mock.ScrolledWindow = test_context.Mock(return_value=scrolled_window_mock) |
| 836 |
gtk_mock.TreeView = test_context.Mock(return_value=tree_view_mock) |
| 837 |
gtk_mock.ListStore = test_context.Mock(return_value=list_store_mock) |
| 838 |
gtk_mock.TreeViewColumn = test_context.Mock(return_value=tree_view_column_mock) |
| 839 |
gtk_mock.CellRendererText = test_context.Mock(return_value=cell_renderer_text_mock) |
| 840 |
|
| 841 |
focus_manager_instance = test_context.Mock() |
| 842 |
focus_manager_instance.get_locus_of_focus = test_context.Mock(return_value=None) |
| 843 |
essential_modules["orca.focus_manager"].get_manager = test_context.Mock( |
| 844 |
return_value=focus_manager_instance, |
| 845 |
) |
| 846 |
|
| 847 |
script_manager_instance = test_context.Mock() |
| 848 |
script_instance = test_context.Mock() |
| 849 |
script_manager_instance.get_active_script = test_context.Mock(return_value=script_instance) |
| 850 |
essential_modules["orca.script_manager"].get_manager = test_context.Mock( |
| 851 |
return_value=script_manager_instance, |
| 852 |
) |
| 853 |
|
| 854 |
input_event_handler_mock = test_context.Mock() |
| 855 |
essential_modules["orca.input_event"].InputEventHandler = test_context.Mock( |
| 856 |
return_value=input_event_handler_mock, |
| 857 |
) |
| 858 |
|
| 859 |
essential_modules[ |
| 860 |
"orca.messages" |
| 861 |
].NOTIFICATION_PRESENTER_MESSAGE_NOT_FOUND = "No notification message found" |
| 862 |
essential_modules[ |
| 863 |
"orca.messages" |
| 864 |
].NOTIFICATION_PRESENTER_MESSAGE_DUPLICATE = "Duplicate notification" |
| 865 |
|
| 866 |
essential_modules["orca.guilabels"].NOTIFICATION_LIST_TITLE = "Notification List" |
| 867 |
essential_modules["dialog"] = dialog_mock |
| 868 |
essential_modules["list_store"] = list_store_mock |
| 869 |
essential_modules["tree_view"] = tree_view_mock |
| 870 |
|
| 871 |
return essential_modules |
| 872 |
|
| 873 |
def test_get_presenter(self, test_context: OrcaTestContext) -> None: |
| 874 |
"""Test get_presenter function.""" |
| 875 |
|
| 876 |
self._setup_dependencies(test_context) |
| 877 |
from orca.notification_presenter import get_presenter |
| 878 |
|
| 879 |
presenter = get_presenter() |
| 880 |
assert presenter is not None |
| 881 |
assert presenter._notifications is not None |
| 882 |
assert presenter._current_index is not None |
| 883 |
|
| 884 |
presenter2 = get_presenter() |
| 885 |
assert presenter is presenter2 |