Debian Codegraph @

tests/unit_tests/test_command_manager.py

raw
1 # Unit tests for command_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-lines
25 # pylint: disable=too-many-arguments
26 # pylint: disable=too-many-positional-arguments
27
28 """Unit tests for command_manager.py methods."""
29
30 from __future__ import annotations
31
32 from typing import TYPE_CHECKING
33
34 import pytest
35
36 if TYPE_CHECKING:
37 from unittest.mock import Mock
38
39 from .orca_test_context import OrcaTestContext
40
41
42 @pytest.mark.unit
43 class TestCommand:
44 """Test Command base class methods."""
45
46 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
47 """Returns dependencies for command_manager module testing."""
48
49 essential_modules = test_context.setup_shared_dependencies([])
50
51 input_event_mock = essential_modules["orca.input_event"]
52 input_event_mock.InputEventHandler = test_context.Mock
53
54 keybindings_mock = essential_modules["orca.keybindings"]
55 keybindings_mock.KeyBinding = test_context.Mock
56 keybindings_mock.KeyBindings = test_context.Mock
57
58 return essential_modules
59
60 def _create_mock_function(self, test_context: OrcaTestContext) -> Mock:
61 """Creates a mock function for a Command."""
62
63 function = test_context.Mock()
64 function.return_value = True
65 return function
66
67 def test_init_minimal(self, test_context: OrcaTestContext) -> None:
68 """Test Command.__init__ with minimal arguments."""
69
70 self._setup_dependencies(test_context)
71 from orca.command_manager import Command
72
73 function = self._create_mock_function(test_context)
74 command = Command("testCommand", function, "Test Group")
75
76 assert command.get_name() == "testCommand"
77 assert command.get_function() == function
78 assert command.get_group_label() == "Test Group"
79 assert command.get_description() == ""
80
81 def test_init_full(self, test_context: OrcaTestContext) -> None:
82 """Test Command.__init__ with all arguments."""
83
84 self._setup_dependencies(test_context)
85 from orca.command_manager import Command
86
87 function = self._create_mock_function(test_context)
88 command = Command(
89 "fullCommand",
90 function,
91 "Full Group",
92 "Full description",
93 enabled=False,
94 suspended=True,
95 )
96
97 assert command.get_name() == "fullCommand"
98 assert command.get_function() == function
99 assert command.get_group_label() == "Full Group"
100 assert command.get_description() == "Full description"
101 assert command.is_enabled() is False
102 assert command.is_suspended() is True
103
104 def test_set_group_label(self, test_context: OrcaTestContext) -> None:
105 """Test Command.set_group_label."""
106
107 self._setup_dependencies(test_context)
108 from orca.command_manager import Command
109
110 function = self._create_mock_function(test_context)
111 command = Command("testCommand", function, "Original Group")
112
113 assert command.get_group_label() == "Original Group"
114
115 command.set_group_label("New Group")
116 assert command.get_group_label() == "New Group"
117
118 def test_init_enabled_suspended_defaults(self, test_context: OrcaTestContext) -> None:
119 """Test that enabled defaults to True and suspended defaults to False."""
120
121 self._setup_dependencies(test_context)
122 from orca.command_manager import Command
123
124 function = self._create_mock_function(test_context)
125 command = Command("testCommand", function, "Test Group")
126
127 assert command.is_enabled() is True
128 assert command.is_suspended() is False
129
130 def test_init_enabled_suspended_explicit(self, test_context: OrcaTestContext) -> None:
131 """Test Command.__init__ with explicit enabled and suspended values."""
132
133 self._setup_dependencies(test_context)
134 from orca.command_manager import Command
135
136 function = self._create_mock_function(test_context)
137 command = Command("testCommand", function, "Test Group", enabled=False, suspended=True)
138
139 assert command.is_enabled() is False
140 assert command.is_suspended() is True
141
142 def test_set_enabled(self, test_context: OrcaTestContext) -> None:
143 """Test Command.set_enabled."""
144
145 self._setup_dependencies(test_context)
146 from orca.command_manager import Command
147
148 function = self._create_mock_function(test_context)
149 command = Command("testCommand", function, "Test Group")
150
151 assert command.is_enabled() is True
152
153 command.set_enabled(False)
154 assert command.is_enabled() is False
155
156 command.set_enabled(True)
157 assert command.is_enabled() is True
158
159 def test_set_suspended(self, test_context: OrcaTestContext) -> None:
160 """Test Command.set_suspended."""
161
162 self._setup_dependencies(test_context)
163 from orca.command_manager import Command
164
165 function = self._create_mock_function(test_context)
166 command = Command("testCommand", function, "Test Group")
167
168 assert command.is_suspended() is False
169
170 command.set_suspended(True)
171 assert command.is_suspended() is True
172
173 command.set_suspended(False)
174 assert command.is_suspended() is False
175
176 def test_execute_calls_function(self, test_context: OrcaTestContext) -> None:
177 """Test that execute calls the command's function."""
178
179 self._setup_dependencies(test_context)
180 from orca.command_manager import Command
181
182 function = self._create_mock_function(test_context)
183 function.return_value = True
184 command = Command("testCommand", function, "Test Group")
185
186 mock_script = test_context.Mock()
187 mock_event = test_context.Mock()
188
189 result = command.execute(mock_script, mock_event)
190
191 function.assert_called_once_with(mock_script, mock_event)
192 assert result is True
193
194 def test_execute_returns_function_result(self, test_context: OrcaTestContext) -> None:
195 """Test that execute returns the function's return value."""
196
197 self._setup_dependencies(test_context)
198 from orca.command_manager import Command
199
200 function = self._create_mock_function(test_context)
201 function.return_value = False
202 command = Command("testCommand", function, "Test Group")
203
204 mock_script = test_context.Mock()
205
206 result = command.execute(mock_script, None)
207
208 assert result is False
209
210
211 @pytest.mark.unit
212 class TestKeyboardCommand:
213 """Test KeyboardCommand class methods."""
214
215 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
216 """Returns dependencies for command_manager module testing."""
217
218 essential_modules = test_context.setup_shared_dependencies([])
219
220 input_event_mock = essential_modules["orca.input_event"]
221 input_event_mock.InputEventHandler = test_context.Mock
222
223 keybindings_mock = essential_modules["orca.keybindings"]
224 keybindings_mock.KeyBinding = test_context.Mock
225 keybindings_mock.KeyBindings = test_context.Mock
226
227 return essential_modules
228
229 def _create_mock_function(self, test_context: OrcaTestContext) -> Mock:
230 """Creates a mock function for a Command."""
231
232 function = test_context.Mock()
233 function.return_value = True
234 return function
235
236 def _create_mock_keybinding(
237 self,
238 test_context: OrcaTestContext,
239 keyval: int = 65,
240 keycode: int = 38,
241 ) -> Mock:
242 """Creates a mock KeyBinding with default keyval/keycode for indexing."""
243
244 kb = test_context.Mock()
245 kb.keyval = keyval
246 kb.keycode = keycode
247 return kb
248
249 def test_init_minimal(self, test_context: OrcaTestContext) -> None:
250 """Test KeyboardCommand.__init__ with minimal arguments."""
251
252 self._setup_dependencies(test_context)
253 from orca.command_manager import KeyboardCommand
254
255 function = self._create_mock_function(test_context)
256 command = KeyboardCommand("testCommand", function, "Test Group")
257
258 assert command.get_name() == "testCommand"
259 assert command.get_function() == function
260 assert command.get_group_label() == "Test Group"
261 assert command.get_description() == ""
262 assert command.get_keybinding() is None
263
264 def test_init_with_keybindings(self, test_context: OrcaTestContext) -> None:
265 """Test KeyboardCommand.__init__ with keybindings."""
266
267 self._setup_dependencies(test_context)
268 from orca.command_manager import KeyboardCommand
269
270 function = self._create_mock_function(test_context)
271 desktop_kb = self._create_mock_keybinding(test_context)
272 laptop_kb = self._create_mock_keybinding(test_context)
273
274 command = KeyboardCommand(
275 "fullCommand",
276 function,
277 "Full Group",
278 "Full description",
279 desktop_keybinding=desktop_kb,
280 laptop_keybinding=laptop_kb,
281 )
282
283 assert command.get_name() == "fullCommand"
284 assert command.get_default_keybinding(is_desktop=True) == desktop_kb
285 assert command.get_default_keybinding(is_desktop=False) == laptop_kb
286
287 def test_set_keybinding(self, test_context: OrcaTestContext) -> None:
288 """Test KeyboardCommand.set_keybinding."""
289
290 self._setup_dependencies(test_context)
291 from orca.command_manager import KeyboardCommand
292
293 function = self._create_mock_function(test_context)
294 command = KeyboardCommand("testCommand", function, "Test Group")
295
296 assert command.get_keybinding() is None
297
298 new_kb = self._create_mock_keybinding(test_context)
299 command.set_keybinding(new_kb)
300 assert command.get_keybinding() == new_kb
301
302 command.set_keybinding(None)
303 assert command.get_keybinding() is None
304
305 def test_is_active_enabled_not_suspended_with_keybinding(
306 self,
307 test_context: OrcaTestContext,
308 ) -> None:
309 """Test is_active returns True when enabled, not suspended, and has keybinding."""
310
311 self._setup_dependencies(test_context)
312 from orca.command_manager import KeyboardCommand
313
314 function = self._create_mock_function(test_context)
315 keybinding = self._create_mock_keybinding(test_context)
316 command = KeyboardCommand(
317 "testCommand",
318 function,
319 "Test Group",
320 desktop_keybinding=keybinding,
321 )
322 command.set_keybinding(keybinding)
323
324 assert command.is_active() is True
325
326 def test_is_active_disabled(self, test_context: OrcaTestContext) -> None:
327 """Test is_active returns False when disabled."""
328
329 self._setup_dependencies(test_context)
330 from orca.command_manager import KeyboardCommand
331
332 function = self._create_mock_function(test_context)
333 keybinding = self._create_mock_keybinding(test_context)
334 command = KeyboardCommand(
335 "testCommand",
336 function,
337 "Test Group",
338 desktop_keybinding=keybinding,
339 enabled=False,
340 )
341 command.set_keybinding(keybinding)
342
343 assert command.is_active() is False
344
345 def test_is_active_suspended(self, test_context: OrcaTestContext) -> None:
346 """Test is_active returns False when suspended."""
347
348 self._setup_dependencies(test_context)
349 from orca.command_manager import KeyboardCommand
350
351 function = self._create_mock_function(test_context)
352 keybinding = self._create_mock_keybinding(test_context)
353 command = KeyboardCommand(
354 "testCommand",
355 function,
356 "Test Group",
357 desktop_keybinding=keybinding,
358 suspended=True,
359 )
360 command.set_keybinding(keybinding)
361
362 assert command.is_active() is False
363
364 def test_is_active_no_keybinding(self, test_context: OrcaTestContext) -> None:
365 """Test is_active returns False when no keybinding is assigned."""
366
367 self._setup_dependencies(test_context)
368 from orca.command_manager import KeyboardCommand
369
370 function = self._create_mock_function(test_context)
371 command = KeyboardCommand("testCommand", function, "Test Group")
372
373 assert command.is_active() is False
374
375 def test_is_group_toggle_init(self, test_context: OrcaTestContext) -> None:
376 """Test KeyboardCommand initializes is_group_toggle correctly."""
377
378 self._setup_dependencies(test_context)
379 from orca.command_manager import KeyboardCommand
380
381 function = self._create_mock_function(test_context)
382
383 # Default is False
384 cmd_default = KeyboardCommand("cmd1", function, "Test Group")
385 assert cmd_default.is_group_toggle() is False
386
387 # Explicit True
388 cmd_toggle = KeyboardCommand("cmd2", function, "Test Group", is_group_toggle=True)
389 assert cmd_toggle.is_group_toggle() is True
390
391
392 @pytest.mark.unit
393 class TestBrailleCommand:
394 """Test BrailleCommand class methods."""
395
396 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
397 """Returns dependencies for command_manager module testing."""
398
399 essential_modules = test_context.setup_shared_dependencies([])
400
401 input_event_mock = essential_modules["orca.input_event"]
402 input_event_mock.InputEventHandler = test_context.Mock
403
404 keybindings_mock = essential_modules["orca.keybindings"]
405 keybindings_mock.KeyBinding = test_context.Mock
406 keybindings_mock.KeyBindings = test_context.Mock
407
408 return essential_modules
409
410 def _create_mock_function(self, test_context: OrcaTestContext) -> Mock:
411 """Creates a mock function for a Command."""
412
413 function = test_context.Mock()
414 function.return_value = True
415 return function
416
417 def test_braille_bindings_default_empty(self, test_context: OrcaTestContext) -> None:
418 """Test that braille bindings default to empty tuple."""
419
420 self._setup_dependencies(test_context)
421 from orca.command_manager import BrailleCommand
422
423 function = self._create_mock_function(test_context)
424 command = BrailleCommand("testCommand", function, "Test Group")
425
426 assert command.get_braille_bindings() == ()
427
428 def test_init_with_braille_bindings(self, test_context: OrcaTestContext) -> None:
429 """Test BrailleCommand.__init__ with braille bindings."""
430
431 self._setup_dependencies(test_context)
432 from orca.command_manager import BrailleCommand
433
434 function = self._create_mock_function(test_context)
435 command = BrailleCommand(
436 "testCommand",
437 function,
438 "Test Group",
439 "Test description",
440 braille_bindings=(1, 2, 3),
441 )
442
443 assert command.get_braille_bindings() == (1, 2, 3)
444
445
446 @pytest.mark.unit
447 class TestCommandManager:
448 """Test CommandManager class methods."""
449
450 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
451 """Returns dependencies for command_manager module testing."""
452
453 essential_modules = test_context.setup_shared_dependencies([])
454
455 input_event_mock = essential_modules["orca.input_event"]
456 input_event_mock.InputEventHandler = test_context.Mock
457
458 keybindings_mock = essential_modules["orca.keybindings"]
459 keybindings_mock.KeyBinding = test_context.Mock
460 keybindings_mock.KeyBindings = test_context.Mock
461
462 from orca import gsettings_registry
463
464 gsettings_registry.get_registry().clear_runtime_values()
465
466 return essential_modules
467
468 def _create_mock_function(
469 self,
470 test_context: OrcaTestContext,
471 _description: str = "Test function",
472 ) -> Mock:
473 """Creates a mock function for a Command."""
474
475 function = test_context.Mock()
476 function.return_value = True
477 return function
478
479 def _create_mock_keybinding(
480 self,
481 test_context: OrcaTestContext,
482 keyval: int = 65,
483 keycode: int = 38,
484 ) -> Mock:
485 """Creates a mock KeyBinding with default keyval/keycode for indexing."""
486
487 kb = test_context.Mock()
488 kb.keyval = keyval
489 kb.keycode = keycode
490 kb.keysymstring = "a"
491 return kb
492
493 def test_init(self, test_context: OrcaTestContext) -> None:
494 """Test CommandManager.__init__."""
495
496 self._setup_dependencies(test_context)
497 from orca.command_manager import CommandManager
498
499 manager = CommandManager()
500 assert not manager.get_all_keyboard_commands()
501 assert not manager.get_all_braille_commands()
502
503 def test_add_and_get_keyboard_command(self, test_context: OrcaTestContext) -> None:
504 """Test CommandManager.add_command and get_keyboard_command."""
505
506 self._setup_dependencies(test_context)
507 from orca.command_manager import CommandManager, KeyboardCommand
508
509 manager = CommandManager()
510 function = self._create_mock_function(test_context)
511 command = KeyboardCommand("testCommand", function, "Test Group")
512
513 manager.add_command(command)
514
515 retrieved = manager.get_keyboard_command("testCommand")
516 assert retrieved == command
517
518 assert manager.get_keyboard_command("nonexistent") is None
519
520 def test_get_all_keyboard_commands(self, test_context: OrcaTestContext) -> None:
521 """Test CommandManager.get_all_keyboard_commands."""
522
523 self._setup_dependencies(test_context)
524 from orca.command_manager import CommandManager, KeyboardCommand
525
526 manager = CommandManager()
527
528 function1 = self._create_mock_function(test_context)
529 function2 = self._create_mock_function(test_context)
530 function3 = self._create_mock_function(test_context)
531
532 cmd1 = KeyboardCommand("cmd1", function1, "Group A")
533 cmd2 = KeyboardCommand("cmd2", function2, "Group B")
534 cmd3 = KeyboardCommand("cmd3", function3, "Group A")
535
536 manager.add_command(cmd1)
537 manager.add_command(cmd2)
538 manager.add_command(cmd3)
539
540 all_commands = manager.get_all_keyboard_commands()
541 assert len(all_commands) == 3
542 assert cmd1 in all_commands
543 assert cmd2 in all_commands
544 assert cmd3 in all_commands
545
546 def test_get_command_for_event_finds_match(self, test_context: OrcaTestContext) -> None:
547 """Test get_command_for_event returns matching command."""
548
549 self._setup_dependencies(test_context)
550 from orca.command_manager import CommandManager, KeyboardCommand
551
552 manager = CommandManager()
553
554 function = self._create_mock_function(test_context)
555 kb = self._create_mock_keybinding(test_context)
556 kb.matches.return_value = True
557 kb.modifiers = 4
558 kb.click_count = 1
559
560 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb)
561 cmd.set_keybinding(kb)
562 manager.add_command(cmd)
563
564 event = test_context.Mock()
565 event.get_click_count.return_value = 1
566 event.id = 65
567 event.hw_code = 38
568 event.modifiers = 4
569 event.is_keypad_key_with_numlock_on.return_value = False
570
571 result = manager.get_command_for_event(event)
572 assert result == cmd
573
574 def test_get_command_for_event_no_match(self, test_context: OrcaTestContext) -> None:
575 """Test get_command_for_event returns None when no match."""
576
577 self._setup_dependencies(test_context)
578 from orca.command_manager import CommandManager, KeyboardCommand
579
580 manager = CommandManager()
581
582 function = self._create_mock_function(test_context)
583 kb = self._create_mock_keybinding(test_context)
584 kb.matches.return_value = False
585
586 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb)
587 cmd.set_keybinding(kb)
588 manager.add_command(cmd)
589
590 event = test_context.Mock()
591 event.get_click_count.return_value = 1
592 event.id = 65
593 event.hw_code = 38
594 event.modifiers = 4
595
596 result = manager.get_command_for_event(event)
597 assert result is None
598
599 def test_get_command_for_event_skips_inactive(self, test_context: OrcaTestContext) -> None:
600 """Test get_command_for_event skips inactive commands by default."""
601
602 self._setup_dependencies(test_context)
603 from orca.command_manager import CommandManager, KeyboardCommand
604
605 manager = CommandManager()
606
607 function = self._create_mock_function(test_context)
608 kb = self._create_mock_keybinding(test_context)
609 kb.matches.return_value = True
610 kb.modifiers = 4
611 kb.click_count = 1
612
613 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb, suspended=True)
614 cmd.set_keybinding(kb)
615 manager.add_command(cmd)
616
617 event = test_context.Mock()
618 event.get_click_count.return_value = 1
619 event.id = 65
620 event.hw_code = 38
621 event.modifiers = 4
622
623 result = manager.get_command_for_event(event)
624 assert result is None
625
626 def test_get_command_for_event_includes_inactive(self, test_context: OrcaTestContext) -> None:
627 """Test get_command_for_event can include inactive commands."""
628
629 self._setup_dependencies(test_context)
630 from orca.command_manager import CommandManager, KeyboardCommand
631
632 manager = CommandManager()
633
634 function = self._create_mock_function(test_context)
635 kb = self._create_mock_keybinding(test_context)
636 kb.matches.return_value = True
637 kb.modifiers = 4
638 kb.click_count = 1
639
640 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb, suspended=True)
641 cmd.set_keybinding(kb)
642 manager.add_command(cmd)
643
644 event = test_context.Mock()
645 event.get_click_count.return_value = 1
646 event.id = 65
647 event.hw_code = 38
648 event.modifiers = 4
649 event.is_keypad_key_with_numlock_on.return_value = False
650
651 result = manager.get_command_for_event(event, active_only=False)
652 assert result == cmd
653
654 def test_get_command_for_braille_event_finds_match(self, test_context: OrcaTestContext) -> None:
655 """Test get_command_for_braille_event returns matching command."""
656
657 self._setup_dependencies(test_context)
658 from orca.command_manager import BrailleCommand, CommandManager
659
660 manager = CommandManager()
661
662 function = self._create_mock_function(test_context)
663 cmd = BrailleCommand("cmd", function, "Test Group", braille_bindings=(100, 200, 300))
664 manager.add_command(cmd)
665
666 result = manager.get_command_for_braille_event(200)
667 assert result == cmd
668
669 def test_get_command_for_braille_event_no_match(self, test_context: OrcaTestContext) -> None:
670 """Test get_command_for_braille_event returns None when no match."""
671
672 self._setup_dependencies(test_context)
673 from orca.command_manager import BrailleCommand, CommandManager
674
675 manager = CommandManager()
676
677 function = self._create_mock_function(test_context)
678 cmd = BrailleCommand("cmd", function, "Test Group", braille_bindings=(100, 200, 300))
679 manager.add_command(cmd)
680
681 result = manager.get_command_for_braille_event(999)
682 assert result is None
683
684 def test_get_command_for_braille_event_empty_bindings(
685 self,
686 test_context: OrcaTestContext,
687 ) -> None:
688 """Test get_command_for_braille_event returns None when command has no braille bindings."""
689
690 self._setup_dependencies(test_context)
691 from orca.command_manager import BrailleCommand, CommandManager
692
693 manager = CommandManager()
694
695 function = self._create_mock_function(test_context)
696 cmd = BrailleCommand("cmd", function, "Test Group")
697 manager.add_command(cmd)
698
699 result = manager.get_command_for_braille_event(100)
700 assert result is None
701
702 def test_set_group_enabled(self, test_context: OrcaTestContext) -> None:
703 """Test set_group_enabled sets enabled state for all commands in group."""
704
705 self._setup_dependencies(test_context)
706 from orca.command_manager import CommandManager, KeyboardCommand
707
708 manager = CommandManager()
709
710 function1 = self._create_mock_function(test_context)
711 function2 = self._create_mock_function(test_context)
712 function3 = self._create_mock_function(test_context)
713
714 cmd1 = KeyboardCommand("cmd1", function1, "Group A")
715 cmd2 = KeyboardCommand("cmd2", function2, "Group A")
716 cmd3 = KeyboardCommand("cmd3", function3, "Group B")
717
718 manager.add_command(cmd1)
719 manager.add_command(cmd2)
720 manager.add_command(cmd3)
721
722 manager.set_group_enabled("Group A", False)
723
724 assert cmd1.is_enabled() is False
725 assert cmd2.is_enabled() is False
726 assert cmd3.is_enabled() is True
727
728 def test_set_group_suspended(self, test_context: OrcaTestContext) -> None:
729 """Test set_group_suspended sets suspended state for all commands in group."""
730
731 self._setup_dependencies(test_context)
732 from orca.command_manager import CommandManager, KeyboardCommand
733
734 manager = CommandManager()
735
736 function1 = self._create_mock_function(test_context)
737 function2 = self._create_mock_function(test_context)
738 function3 = self._create_mock_function(test_context)
739
740 cmd1 = KeyboardCommand("cmd1", function1, "Group A")
741 cmd2 = KeyboardCommand("cmd2", function2, "Group A")
742 cmd3 = KeyboardCommand("cmd3", function3, "Group B")
743
744 manager.add_command(cmd1)
745 manager.add_command(cmd2)
746 manager.add_command(cmd3)
747
748 manager.set_group_suspended("Group A", True)
749
750 assert cmd1.is_suspended() is True
751 assert cmd2.is_suspended() is True
752 assert cmd3.is_suspended() is False
753
754 def test_has_multi_click_bindings_true(self, test_context: OrcaTestContext) -> None:
755 """Test has_multi_click_bindings returns True when multi-click binding exists."""
756
757 self._setup_dependencies(test_context)
758 from orca.command_manager import CommandManager, KeyboardCommand
759
760 manager = CommandManager()
761
762 function = self._create_mock_function(test_context)
763
764 # Single-click binding (KP_Up keyval = 65431, keycode = 80)
765 kb1 = self._create_mock_keybinding(test_context, keyval=65431, keycode=80)
766 kb1.matches.return_value = True
767 kb1.click_count = 1
768 cmd1 = KeyboardCommand("readLine", function, "Flat Review", desktop_keybinding=kb1)
769 cmd1.set_keybinding(kb1)
770 manager.add_command(cmd1)
771
772 # Double-click binding for same key
773 kb2 = self._create_mock_keybinding(test_context, keyval=65431, keycode=80)
774 kb2.matches.return_value = True
775 kb2.click_count = 2
776 cmd2 = KeyboardCommand("spellLine", function, "Flat Review", desktop_keybinding=kb2)
777 cmd2.set_keybinding(kb2)
778 manager.add_command(cmd2)
779
780 assert manager.has_multi_click_bindings(65431, 80, 0) is True
781
782 def test_has_multi_click_bindings_false_single_only(
783 self,
784 test_context: OrcaTestContext,
785 ) -> None:
786 """Test has_multi_click_bindings returns False when only single-click binding exists."""
787
788 self._setup_dependencies(test_context)
789 from orca.command_manager import CommandManager, KeyboardCommand
790
791 manager = CommandManager()
792
793 function = self._create_mock_function(test_context)
794
795 # Only single-click binding (KP_Home keyval = 65429, keycode = 79)
796 kb = self._create_mock_keybinding(test_context, keyval=65429, keycode=79)
797 kb.matches.return_value = True
798 kb.click_count = 1
799 cmd = KeyboardCommand("previousLine", function, "Flat Review", desktop_keybinding=kb)
800 cmd.set_keybinding(kb)
801 manager.add_command(cmd)
802
803 assert manager.has_multi_click_bindings(65429, 79, 0) is False
804
805 def test_has_multi_click_bindings_false_no_bindings(
806 self,
807 test_context: OrcaTestContext,
808 ) -> None:
809 """Test has_multi_click_bindings returns False when no bindings exist for key."""
810
811 self._setup_dependencies(test_context)
812 from orca.command_manager import CommandManager
813
814 manager = CommandManager()
815
816 # KP_Home keyval = 65429, keycode = 79
817 assert manager.has_multi_click_bindings(65429, 79, 0) is False
818
819 def test_has_multi_click_bindings_respects_modifiers(
820 self,
821 test_context: OrcaTestContext,
822 ) -> None:
823 """Test has_multi_click_bindings distinguishes by modifiers."""
824
825 self._setup_dependencies(test_context)
826 from orca.command_manager import CommandManager, KeyboardCommand
827
828 manager = CommandManager()
829
830 function = self._create_mock_function(test_context)
831
832 # Double-click binding with Orca modifier (KP_Up keyval = 65431, keycode = 80)
833 kb = self._create_mock_keybinding(test_context, keyval=65431, keycode=80)
834 # matches() returns True only when modifiers=256
835 kb.matches.side_effect = lambda kv, kc, mods: mods == 256
836 kb.click_count = 2
837 cmd = KeyboardCommand("someCommand", function, "Test", desktop_keybinding=kb)
838 cmd.set_keybinding(kb)
839 manager.add_command(cmd)
840
841 # With modifier: has multi-click
842 assert manager.has_multi_click_bindings(65431, 80, 256) is True
843 # Without modifier: no multi-click bindings
844 assert manager.has_multi_click_bindings(65431, 80, 0) is False
845
846 def test_get_command_for_event_shifted_key(self, test_context: OrcaTestContext) -> None:
847 """Test get_command_for_event finds command via keycode when keyval differs (shifted)."""
848
849 self._setup_dependencies(test_context)
850 from orca.command_manager import CommandManager, KeyboardCommand
851
852 manager = CommandManager()
853
854 function = self._create_mock_function(test_context)
855 # Command bound to 'h' (keyval=104, keycode=38)
856 kb = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
857 kb.matches.return_value = True
858 kb.click_count = 1
859 cmd = KeyboardCommand("prevHeading", function, "Structural Nav", desktop_keybinding=kb)
860 cmd.set_keybinding(kb)
861 manager.add_command(cmd)
862
863 # Event has 'H' keyval (72) due to Shift, but same keycode (38)
864 event = test_context.Mock()
865 event.get_click_count.return_value = 1
866 event.id = 72 # 'H' keyval (shifted)
867 event.hw_code = 38 # Same keycode as 'h'
868 event.modifiers = 1 # Shift
869 event.is_keypad_key_with_numlock_on.return_value = False
870
871 result = manager.get_command_for_event(event)
872 assert result == cmd
873
874 def test_get_command_for_event_unshifted_key(self, test_context: OrcaTestContext) -> None:
875 """Test get_command_for_event finds command via keyval for unshifted key."""
876
877 self._setup_dependencies(test_context)
878 from orca.command_manager import CommandManager, KeyboardCommand
879
880 manager = CommandManager()
881
882 function = self._create_mock_function(test_context)
883 # Command bound to 'h' (keyval=104, keycode=38)
884 kb = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
885 kb.matches.return_value = True
886 kb.click_count = 1
887 cmd = KeyboardCommand("nextHeading", function, "Structural Nav", desktop_keybinding=kb)
888 cmd.set_keybinding(kb)
889 manager.add_command(cmd)
890
891 # Event has 'h' keyval (104), no shift
892 event = test_context.Mock()
893 event.get_click_count.return_value = 1
894 event.id = 104 # 'h' keyval (unshifted)
895 event.hw_code = 38
896 event.modifiers = 0
897 event.is_keypad_key_with_numlock_on.return_value = False
898
899 result = manager.get_command_for_event(event)
900 assert result == cmd
901
902 def test_has_multi_click_bindings_shifted_key(self, test_context: OrcaTestContext) -> None:
903 """Test has_multi_click_bindings finds binding via keycode when keyval differs."""
904
905 self._setup_dependencies(test_context)
906 from orca.command_manager import CommandManager, KeyboardCommand
907
908 manager = CommandManager()
909
910 function = self._create_mock_function(test_context)
911 # Single-click binding for 'h' (keyval=104, keycode=38)
912 kb1 = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
913 kb1.matches.return_value = True
914 kb1.click_count = 1
915 cmd1 = KeyboardCommand("nextHeading", function, "Structural Nav", desktop_keybinding=kb1)
916 cmd1.set_keybinding(kb1)
917 manager.add_command(cmd1)
918
919 # Double-click binding for same key
920 kb2 = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
921 kb2.matches.return_value = True
922 kb2.click_count = 2
923 cmd2 = KeyboardCommand("listHeadings", function, "Structural Nav", desktop_keybinding=kb2)
924 cmd2.set_keybinding(kb2)
925 manager.add_command(cmd2)
926
927 # Query with 'H' keyval (72) due to Shift, but same keycode (38)
928 # Should find multi-click binding via keycode
929 assert manager.has_multi_click_bindings(72, 38, 1) is True
930
931 def test_has_multi_click_bindings_unshifted_key(self, test_context: OrcaTestContext) -> None:
932 """Test has_multi_click_bindings finds binding via keyval for unshifted key."""
933
934 self._setup_dependencies(test_context)
935 from orca.command_manager import CommandManager, KeyboardCommand
936
937 manager = CommandManager()
938
939 function = self._create_mock_function(test_context)
940 # Single-click binding for 'h' (keyval=104, keycode=38)
941 kb1 = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
942 kb1.matches.return_value = True
943 kb1.click_count = 1
944 cmd1 = KeyboardCommand("nextHeading", function, "Structural Nav", desktop_keybinding=kb1)
945 cmd1.set_keybinding(kb1)
946 manager.add_command(cmd1)
947
948 # Double-click binding for same key
949 kb2 = self._create_mock_keybinding(test_context, keyval=104, keycode=38)
950 kb2.matches.return_value = True
951 kb2.click_count = 2
952 cmd2 = KeyboardCommand("listHeadings", function, "Structural Nav", desktop_keybinding=kb2)
953 cmd2.set_keybinding(kb2)
954 manager.add_command(cmd2)
955
956 # Query with 'h' keyval (104), unshifted - should find via keyval
957 assert manager.has_multi_click_bindings(104, 38, 0) is True
958
959
960 @pytest.mark.unit
961 class TestGetManager:
962 """Test the get_manager singleton function."""
963
964 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
965 """Returns dependencies for command_manager module testing."""
966
967 essential_modules = test_context.setup_shared_dependencies([])
968
969 input_event_mock = essential_modules["orca.input_event"]
970 input_event_mock.InputEventHandler = test_context.Mock
971
972 keybindings_mock = essential_modules["orca.keybindings"]
973 keybindings_mock.KeyBinding = test_context.Mock
974 keybindings_mock.KeyBindings = test_context.Mock
975
976 from orca import gsettings_registry
977
978 gsettings_registry.get_registry().clear_runtime_values()
979
980 return essential_modules
981
982 def test_get_manager_returns_singleton(self, test_context: OrcaTestContext) -> None:
983 """Test that get_manager returns the same instance."""
984
985 self._setup_dependencies(test_context)
986 from orca.command_manager import get_manager
987
988 manager1 = get_manager()
989 manager2 = get_manager()
990
991 assert manager1 is manager2
992
993 def test_get_manager_returns_command_manager(self, test_context: OrcaTestContext) -> None:
994 """Test that get_manager returns a CommandManager instance."""
995
996 self._setup_dependencies(test_context)
997 from orca.command_manager import CommandManager, get_manager
998
999 manager = get_manager()
1000 assert isinstance(manager, CommandManager)
1001
1002
1003 @pytest.mark.unit
1004 class TestDiffBasedGrabUpdates:
1005 """Test diff-based grab updates in CommandManager."""
1006
1007 def _setup_dependencies(self, test_context: OrcaTestContext) -> dict[str, Mock]:
1008 """Returns dependencies for command_manager module testing."""
1009
1010 essential_modules = test_context.setup_shared_dependencies(["orca.orca_modifier_manager"])
1011
1012 input_event_mock = essential_modules["orca.input_event"]
1013 input_event_mock.InputEventHandler = test_context.Mock
1014
1015 keybindings_mock = essential_modules["orca.keybindings"]
1016 keybindings_mock.KeyBinding = test_context.Mock
1017 keybindings_mock.KeyBindings = test_context.Mock
1018
1019 orca_modifier_manager_mock = essential_modules["orca.orca_modifier_manager"]
1020 modifier_manager_instance = test_context.Mock()
1021 modifier_manager_instance.refresh_orca_modifiers = test_context.Mock()
1022 orca_modifier_manager_mock.get_manager = test_context.Mock(
1023 return_value=modifier_manager_instance,
1024 )
1025
1026 from orca import gsettings_registry
1027
1028 gsettings_registry.get_registry().clear_runtime_values()
1029
1030 essential_modules["modifier_manager_instance"] = modifier_manager_instance
1031 return essential_modules
1032
1033 def _create_mock_function(self, test_context: OrcaTestContext) -> Mock:
1034 """Creates a mock function for a Command."""
1035
1036 function = test_context.Mock()
1037 function.return_value = True
1038 return function
1039
1040 def _create_mock_keybinding(
1041 self,
1042 test_context: OrcaTestContext,
1043 keysymstring: str = "a",
1044 modifiers: int = 0,
1045 click_count: int = 1,
1046 keyval: int = 65,
1047 keycode: int = 38,
1048 ) -> Mock:
1049 """Creates a mock KeyBinding with specified properties."""
1050
1051 kb = test_context.Mock()
1052 kb.keysymstring = keysymstring
1053 kb.modifiers = modifiers
1054 kb.click_count = click_count
1055 kb.keyval = keyval
1056 kb.keycode = keycode
1057 kb.has_grabs = test_context.Mock(return_value=False)
1058 kb.add_grabs = test_context.Mock()
1059 kb.remove_grabs = test_context.Mock()
1060 kb.get_grab_ids = test_context.Mock(return_value=[])
1061 kb.set_grab_ids = test_context.Mock()
1062 return kb
1063
1064 def test_binding_key_returns_tuple(self, test_context: OrcaTestContext) -> None:
1065 """Test _binding_key returns correct tuple."""
1066
1067 self._setup_dependencies(test_context)
1068 from orca.command_manager import CommandManager
1069
1070 manager = CommandManager()
1071 kb = self._create_mock_keybinding(
1072 test_context,
1073 keysymstring="h",
1074 modifiers=256,
1075 click_count=2,
1076 )
1077
1078 result = manager._binding_key(kb)
1079 assert result == ("h", 256, 2)
1080
1081 def test_binding_key_returns_none_for_none(self, test_context: OrcaTestContext) -> None:
1082 """Test _binding_key returns None for None keybinding."""
1083
1084 self._setup_dependencies(test_context)
1085 from orca.command_manager import CommandManager
1086
1087 manager = CommandManager()
1088 assert manager._binding_key(None) is None
1089
1090 def test_binding_key_returns_none_for_empty_keysymstring(
1091 self,
1092 test_context: OrcaTestContext,
1093 ) -> None:
1094 """Test _binding_key returns None when keysymstring is empty."""
1095
1096 self._setup_dependencies(test_context)
1097 from orca.command_manager import CommandManager
1098
1099 manager = CommandManager()
1100 kb = self._create_mock_keybinding(test_context, keysymstring="")
1101 assert manager._binding_key(kb) is None
1102
1103 def test_diff_transfers_grab_ids_for_matching_bindings(
1104 self,
1105 test_context: OrcaTestContext,
1106 ) -> None:
1107 """Test that grab IDs are transferred when bindings match."""
1108
1109 self._setup_dependencies(test_context)
1110 from orca.command_manager import CommandManager, KeyboardCommand
1111
1112 manager = CommandManager()
1113 function = self._create_mock_function(test_context)
1114
1115 # Old command with grabs
1116 old_kb = self._create_mock_keybinding(test_context, keysymstring="h", modifiers=256)
1117 old_kb.has_grabs.return_value = True
1118 old_kb.get_grab_ids.return_value = [42, 43]
1119 old_cmd = KeyboardCommand("cmd", function, "Group", desktop_keybinding=old_kb)
1120 old_cmd.set_keybinding(old_kb)
1121 old_commands = {"cmd": old_cmd}
1122
1123 # New command with same binding but no grabs yet
1124 new_kb = self._create_mock_keybinding(test_context, keysymstring="h", modifiers=256)
1125 new_kb.has_grabs.return_value = False
1126 new_cmd = KeyboardCommand("cmd", function, "Group", desktop_keybinding=new_kb)
1127 new_cmd.set_keybinding(new_kb)
1128 new_commands = {"cmd": new_cmd}
1129
1130 manager._keyboard_commands = old_commands
1131 manager._diff_and_update_grabs(new_commands, "test")
1132
1133 # Verify grab IDs were transferred
1134 new_kb.set_grab_ids.assert_called_once_with([42, 43])
1135 old_kb.set_grab_ids.assert_called_once_with([])
1136 # Neither add nor remove should be called
1137 new_kb.add_grabs.assert_not_called()
1138 old_kb.remove_grabs.assert_not_called()
1139
1140 def test_diff_removes_grabs_for_old_only_bindings(self, test_context: OrcaTestContext) -> None:
1141 """Test that grabs are removed for bindings only in old commands."""
1142
1143 self._setup_dependencies(test_context)
1144 from orca.command_manager import CommandManager, KeyboardCommand
1145
1146 manager = CommandManager()
1147 function = self._create_mock_function(test_context)
1148
1149 # Old command with grabs
1150 old_kb = self._create_mock_keybinding(test_context, keysymstring="h", modifiers=256)
1151 old_kb.has_grabs.return_value = True
1152 old_cmd = KeyboardCommand("old_cmd", function, "Group", desktop_keybinding=old_kb)
1153 old_cmd.set_keybinding(old_kb)
1154 old_commands = {"old_cmd": old_cmd}
1155
1156 # Empty new commands
1157 new_commands: dict = {}
1158
1159 manager._keyboard_commands = old_commands
1160 manager._diff_and_update_grabs(new_commands, "test")
1161
1162 # Verify grabs were removed
1163 old_kb.remove_grabs.assert_called_once()
1164
1165 def test_diff_adds_grabs_for_new_only_bindings(self, test_context: OrcaTestContext) -> None:
1166 """Test that grabs are added for bindings only in new commands."""
1167
1168 self._setup_dependencies(test_context)
1169 from orca.command_manager import CommandManager, KeyboardCommand
1170
1171 manager = CommandManager()
1172 function = self._create_mock_function(test_context)
1173
1174 # Empty old commands
1175 old_commands: dict = {}
1176
1177 # New command without grabs
1178 new_kb = self._create_mock_keybinding(test_context, keysymstring="h", modifiers=256)
1179 new_kb.has_grabs.return_value = False
1180 new_cmd = KeyboardCommand("new_cmd", function, "Group", desktop_keybinding=new_kb)
1181 new_cmd.set_keybinding(new_kb)
1182 new_commands = {"new_cmd": new_cmd}
1183
1184 manager._keyboard_commands = old_commands
1185 manager._diff_and_update_grabs(new_commands, "test")
1186
1187 # Verify grabs were added
1188 new_kb.add_grabs.assert_called_once()
1189
1190 def test_set_active_commands_uses_diff(self, test_context: OrcaTestContext) -> None:
1191 """Test set_active_commands uses diff-based updates."""
1192
1193 self._setup_dependencies(test_context)
1194 from orca.command_manager import CommandManager, KeyboardCommand
1195
1196 manager = CommandManager()
1197 function = self._create_mock_function(test_context)
1198
1199 # Add initial command with grabs
1200 old_kb = self._create_mock_keybinding(test_context, keysymstring="a")
1201 old_kb.has_grabs.return_value = True
1202 old_cmd = KeyboardCommand("old_cmd", function, "Group", desktop_keybinding=old_kb)
1203 old_cmd.set_keybinding(old_kb)
1204 manager.add_command(old_cmd)
1205
1206 # Set new commands
1207 new_kb = self._create_mock_keybinding(test_context, keysymstring="b")
1208 new_kb.has_grabs.return_value = False
1209 new_cmd = KeyboardCommand("new_cmd", function, "Group", desktop_keybinding=new_kb)
1210 new_cmd.set_keybinding(new_kb)
1211
1212 manager.set_active_commands({"new_cmd": new_cmd}, "test")
1213
1214 # Old binding should have grabs removed, new binding should have grabs added
1215 old_kb.remove_grabs.assert_called_once()
1216 new_kb.add_grabs.assert_called_once()
1217
1218 def test_activate_commands_applies_overrides(self, test_context: OrcaTestContext) -> None:
1219 """Test activate_commands applies user overrides."""
1220
1221 self._setup_dependencies(test_context)
1222 from orca.command_manager import CommandManager, KeyboardCommand
1223
1224 manager = CommandManager()
1225 function = self._create_mock_function(test_context)
1226
1227 # Add a command
1228 kb = self._create_mock_keybinding(test_context, keysymstring="a")
1229 cmd = KeyboardCommand("test_cmd", function, "Group", desktop_keybinding=kb)
1230 cmd.set_keybinding(kb)
1231 manager.add_command(cmd)
1232
1233 # activate_commands reads overrides via layered_lookup on the registry.
1234 # With no user values in the memory backend, no overrides are applied.
1235 manager.activate_commands("test")
1236
1237 # The command should still have its original keybinding unchanged.
1238 assert cmd.get_keybinding() is kb
1239
1240 def test_diff_skips_inactive_commands(self, test_context: OrcaTestContext) -> None:
1241 """Test that diff skips inactive commands."""
1242
1243 self._setup_dependencies(test_context)
1244 from orca.command_manager import CommandManager, KeyboardCommand
1245
1246 manager = CommandManager()
1247 function = self._create_mock_function(test_context)
1248
1249 # Old command that is disabled (inactive)
1250 old_kb = self._create_mock_keybinding(test_context, keysymstring="h")
1251 old_kb.has_grabs.return_value = True
1252 old_cmd = KeyboardCommand(
1253 "cmd",
1254 function,
1255 "Group",
1256 desktop_keybinding=old_kb,
1257 enabled=False,
1258 )
1259 old_cmd.set_keybinding(old_kb)
1260 old_commands = {"cmd": old_cmd}
1261
1262 # New command that is also disabled
1263 new_kb = self._create_mock_keybinding(test_context, keysymstring="h")
1264 new_cmd = KeyboardCommand(
1265 "cmd",
1266 function,
1267 "Group",
1268 desktop_keybinding=new_kb,
1269 enabled=False,
1270 )
1271 new_cmd.set_keybinding(new_kb)
1272 new_commands = {"cmd": new_cmd}
1273
1274 manager._keyboard_commands = old_commands
1275 manager._diff_and_update_grabs(new_commands, "test")
1276
1277 # Neither should have grabs modified since both are inactive
1278 old_kb.remove_grabs.assert_not_called()
1279 new_kb.add_grabs.assert_not_called()
1280 new_kb.set_grab_ids.assert_not_called()
1281
1282 def test_set_group_suspended_removes_grabs_when_suspending(
1283 self,
1284 test_context: OrcaTestContext,
1285 ) -> None:
1286 """Test set_group_suspended removes grabs when suspending active commands."""
1287
1288 self._setup_dependencies(test_context)
1289 from orca.command_manager import CommandManager, KeyboardCommand
1290
1291 manager = CommandManager()
1292 function = self._create_mock_function(test_context)
1293
1294 # Active command with grabs
1295 kb = self._create_mock_keybinding(test_context, keysymstring="h")
1296 kb.has_grabs.return_value = True
1297 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb)
1298 cmd.set_keybinding(kb)
1299 manager.add_command(cmd)
1300
1301 # Suspend the group
1302 manager.set_group_suspended("Test Group", True)
1303
1304 # Grabs should be removed
1305 kb.remove_grabs.assert_called_once()
1306 kb.add_grabs.assert_not_called()
1307
1308 def test_set_group_suspended_adds_grabs_when_unsuspending(
1309 self,
1310 test_context: OrcaTestContext,
1311 ) -> None:
1312 """Test set_group_suspended adds grabs when unsuspending commands."""
1313
1314 self._setup_dependencies(test_context)
1315 from orca.command_manager import CommandManager, KeyboardCommand
1316
1317 manager = CommandManager()
1318 function = self._create_mock_function(test_context)
1319
1320 # Suspended command without grabs
1321 kb = self._create_mock_keybinding(test_context, keysymstring="h")
1322 kb.has_grabs.return_value = False
1323 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb, suspended=True)
1324 cmd.set_keybinding(kb)
1325 manager.add_command(cmd)
1326
1327 # Unsuspend the group
1328 manager.set_group_suspended("Test Group", False)
1329
1330 # Grabs should be added
1331 kb.add_grabs.assert_called_once()
1332 kb.remove_grabs.assert_not_called()
1333
1334 def test_set_group_suspended_no_change_when_already_in_state(
1335 self,
1336 test_context: OrcaTestContext,
1337 ) -> None:
1338 """Test set_group_suspended does nothing when commands already in desired state."""
1339
1340 self._setup_dependencies(test_context)
1341 from orca.command_manager import CommandManager, KeyboardCommand
1342
1343 manager = CommandManager()
1344 function = self._create_mock_function(test_context)
1345
1346 # Already suspended command
1347 kb = self._create_mock_keybinding(test_context, keysymstring="h")
1348 kb.has_grabs.return_value = False
1349 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb, suspended=True)
1350 cmd.set_keybinding(kb)
1351 manager.add_command(cmd)
1352
1353 # Suspend again (no change)
1354 manager.set_group_suspended("Test Group", True)
1355
1356 # No grabs should be modified
1357 kb.add_grabs.assert_not_called()
1358 kb.remove_grabs.assert_not_called()
1359
1360 def test_set_group_enabled_removes_grabs_when_disabling(
1361 self,
1362 test_context: OrcaTestContext,
1363 ) -> None:
1364 """Test set_group_enabled removes grabs when disabling active commands."""
1365
1366 self._setup_dependencies(test_context)
1367 from orca.command_manager import CommandManager, KeyboardCommand
1368
1369 manager = CommandManager()
1370 function = self._create_mock_function(test_context)
1371
1372 # Active command with grabs
1373 kb = self._create_mock_keybinding(test_context, keysymstring="h")
1374 kb.has_grabs.return_value = True
1375 cmd = KeyboardCommand("cmd", function, "Test Group", desktop_keybinding=kb)
1376 cmd.set_keybinding(kb)
1377 manager.add_command(cmd)
1378
1379 # Disable the group
1380 manager.set_group_enabled("Test Group", False)
1381
1382 # Grabs should be removed
1383 kb.remove_grabs.assert_called_once()
1384 kb.add_grabs.assert_not_called()
1385
1386 def test_set_group_enabled_skips_group_toggle_commands(
1387 self,
1388 test_context: OrcaTestContext,
1389 ) -> None:
1390 """Test set_group_enabled skips group toggle commands."""
1391
1392 self._setup_dependencies(test_context)
1393 from orca.command_manager import CommandManager, KeyboardCommand
1394
1395 manager = CommandManager()
1396 function = self._create_mock_function(test_context)
1397
1398 # Group toggle command with grabs
1399 kb = self._create_mock_keybinding(test_context, keysymstring="z")
1400 kb.has_grabs.return_value = True
1401 cmd = KeyboardCommand(
1402 "toggle_cmd",
1403 function,
1404 "Test Group",
1405 desktop_keybinding=kb,
1406 is_group_toggle=True,
1407 )
1408 cmd.set_keybinding(kb)
1409 manager.add_command(cmd)
1410
1411 # Disable the group
1412 manager.set_group_enabled("Test Group", False)
1413
1414 # Group toggle should not have grabs removed (it stays active)
1415 kb.remove_grabs.assert_not_called()
1416 # Command should still be enabled
1417 assert cmd.is_enabled() is True