| 1 |
import ast |
| 2 |
from unittest.mock import Mock |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from fenrirscreenreader.core.commandManager import CommandManager |
| 7 |
from fenrirscreenreader.core.helpManager import HelpManager |
| 8 |
|
| 9 |
|
| 10 |
def create_help_manager(capture_succeeds=True): |
| 11 |
manager = HelpManager() |
| 12 |
bindings = { |
| 13 |
str([1, ["KEY_F1", "KEY_FENRIR"]]): "TOGGLE_TUTORIAL_MODE", |
| 14 |
str([1, ["KEY_FENRIR", "KEY_S"]]): "SPELL_CHECK", |
| 15 |
str([1, ["KEY_FENRIR", "KEY_W"]]): "READ_WORD", |
| 16 |
str([2, ["KEY_FENRIR", "KEY_W"]]): "READ_WORD", |
| 17 |
str([1, ["KEY_SCRIPT", "KEY_U"]]): ( |
| 18 |
"MUSICPLAYER__-__KEY_U" |
| 19 |
), |
| 20 |
} |
| 21 |
raw_bindings = { |
| 22 |
shortcut: ast.literal_eval(shortcut) for shortcut in bindings |
| 23 |
} |
| 24 |
command_manager = Mock() |
| 25 |
descriptions = { |
| 26 |
"SPELL_CHECK": "spell check", |
| 27 |
"READ_WORD": "read current word", |
| 28 |
"UNBOUND_ACTION": "an action without a shortcut", |
| 29 |
"MUSICPLAYER__-__KEY_U": "music player plugin", |
| 30 |
} |
| 31 |
command_manager.get_command_description.side_effect = ( |
| 32 |
lambda command, section="commands": descriptions.get(command, "") |
| 33 |
) |
| 34 |
input_manager = Mock() |
| 35 |
input_manager.set_help_capture.return_value = capture_succeeds |
| 36 |
internal_command = Mock() |
| 37 |
internal_command.help_visible = False |
| 38 |
plugin_command = Mock() |
| 39 |
plugin_command.help_category = "plugins" |
| 40 |
manager.initialize( |
| 41 |
{ |
| 42 |
"general": {"tutorialMode": False}, |
| 43 |
"bindings": bindings.copy(), |
| 44 |
"rawBindings": { |
| 45 |
shortcut: [raw[0], raw[1].copy()] |
| 46 |
for shortcut, raw in raw_bindings.items() |
| 47 |
}, |
| 48 |
"commands": { |
| 49 |
"commands": { |
| 50 |
"READ_WORD": Mock(), |
| 51 |
"SPELL_CHECK": Mock(), |
| 52 |
"TOGGLE_TUTORIAL_MODE": Mock(), |
| 53 |
"UNBOUND_ACTION": Mock(), |
| 54 |
"00_INIT_COMMANDS": internal_command, |
| 55 |
"MUSICPLAYER__-__KEY_U": plugin_command, |
| 56 |
} |
| 57 |
}, |
| 58 |
"runtime": { |
| 59 |
"CommandManager": command_manager, |
| 60 |
"DebugManager": Mock(), |
| 61 |
"InputDriver": Mock(refresh_grabs=Mock()), |
| 62 |
"InputManager": input_manager, |
| 63 |
"VmenuManager": Mock(get_active=Mock(return_value=False)), |
| 64 |
}, |
| 65 |
} |
| 66 |
) |
| 67 |
return manager, bindings, raw_bindings |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.unit |
| 71 |
def test_help_lists_bound_actions_once_with_all_shortcuts(): |
| 72 |
manager, _bindings, raw_bindings = create_help_manager() |
| 73 |
|
| 74 |
manager.set_tutorial_mode(True) |
| 75 |
|
| 76 |
active_text = " ".join(manager.help_lists["active"]) |
| 77 |
assert active_text.count("read word") == 1 |
| 78 |
assert "fenrir, w" in active_text |
| 79 |
assert "2 times fenrir, w" in active_text |
| 80 |
assert "unbound action" not in active_text |
| 81 |
assert "musicplayer" not in active_text |
| 82 |
assert "00 init commands" not in active_text |
| 83 |
assert all( |
| 84 |
"00 init commands" not in entry |
| 85 |
for entry in manager.help_lists["unbound"] |
| 86 |
) |
| 87 |
assert manager.help_lists["unbound"] == [ |
| 88 |
"unbound action, Shortcuts unbound, Description an action without a shortcut" |
| 89 |
] |
| 90 |
assert manager.help_lists["plugins"] == [ |
| 91 |
"musicplayer, Shortcuts script, u, Description music player plugin" |
| 92 |
] |
| 93 |
assert raw_bindings[str([1, ["KEY_FENRIR", "KEY_S"]])] == [ |
| 94 |
1, |
| 95 |
["KEY_FENRIR", "KEY_S"], |
| 96 |
] |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.unit |
| 100 |
def test_help_navigation_defaults_to_active_and_starts_before_first_item(): |
| 101 |
manager, _bindings, _raw_bindings = create_help_manager() |
| 102 |
manager.set_tutorial_mode(True) |
| 103 |
|
| 104 |
assert manager.get_help_section_name() == "Active actions" |
| 105 |
assert manager.get_help_for_current_index() == "Active actions" |
| 106 |
|
| 107 |
manager.next_index() |
| 108 |
assert manager.get_help_for_current_index() == manager.help_lists["active"][0] |
| 109 |
|
| 110 |
assert manager.select_help_section("unbound") == "Unbound actions" |
| 111 |
assert manager.get_help_for_current_index() == "Unbound actions" |
| 112 |
manager.prev_index() |
| 113 |
assert manager.get_help_for_current_index() == manager.help_lists["unbound"][-1] |
| 114 |
|
| 115 |
assert manager.next_help_section() == "Plugins" |
| 116 |
assert manager.next_help_section() == "Active actions" |
| 117 |
assert manager.prev_help_section() == "Plugins" |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.unit |
| 121 |
def test_help_installs_and_restores_modal_bindings_exactly(): |
| 122 |
manager, bindings, raw_bindings = create_help_manager() |
| 123 |
|
| 124 |
manager.set_tutorial_mode(True) |
| 125 |
|
| 126 |
assert ( |
| 127 |
manager.env["bindings"][str([1, ["KEY_LEFT"]])] |
| 128 |
== "PREV_HELP_SECTION" |
| 129 |
) |
| 130 |
assert ( |
| 131 |
manager.env["bindings"][str([1, ["KEY_RIGHT"]])] |
| 132 |
== "NEXT_HELP_SECTION" |
| 133 |
) |
| 134 |
assert manager.env["bindings"][str([1, ["KEY_ESC"]])] == "TOGGLE_TUTORIAL_MODE" |
| 135 |
assert manager.env["rawBindings"][str([1, ["KEY_RIGHT"]])] == [ |
| 136 |
1, |
| 137 |
["KEY_RIGHT"], |
| 138 |
] |
| 139 |
manager.env["runtime"]["InputManager"].set_help_capture.assert_called_once_with( |
| 140 |
True |
| 141 |
) |
| 142 |
|
| 143 |
manager.set_tutorial_mode(False) |
| 144 |
|
| 145 |
assert manager.env["bindings"] == bindings |
| 146 |
assert manager.env["rawBindings"] == raw_bindings |
| 147 |
manager.env["runtime"]["InputManager"].set_help_capture.assert_called_with( |
| 148 |
False |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
@pytest.mark.unit |
| 153 |
def test_help_records_degraded_capture_without_refusing_mode(): |
| 154 |
manager, _bindings, _raw_bindings = create_help_manager( |
| 155 |
capture_succeeds=False |
| 156 |
) |
| 157 |
|
| 158 |
manager.set_tutorial_mode(True) |
| 159 |
|
| 160 |
assert manager.is_tutorial_mode() is True |
| 161 |
assert manager.is_capture_degraded() is True |
| 162 |
|
| 163 |
|
| 164 |
@pytest.mark.unit |
| 165 |
def test_help_does_not_claim_exit_when_capture_release_fails(): |
| 166 |
manager, bindings, raw_bindings = create_help_manager() |
| 167 |
manager.set_tutorial_mode(True) |
| 168 |
manager.env["runtime"]["InputManager"].set_help_capture.return_value = False |
| 169 |
|
| 170 |
assert manager.set_tutorial_mode(False) is False |
| 171 |
|
| 172 |
assert manager.is_tutorial_mode() is True |
| 173 |
assert manager.env["bindings"] != bindings |
| 174 |
assert manager.env["rawBindings"] != raw_bindings |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.unit |
| 178 |
def test_command_description_is_presented_on_ignored_screen_in_help(): |
| 179 |
command_manager = CommandManager() |
| 180 |
output_manager = Mock() |
| 181 |
command_manager.env = { |
| 182 |
"runtime": { |
| 183 |
"ScreenManager": Mock( |
| 184 |
is_ignored_screen=Mock(return_value=True) |
| 185 |
), |
| 186 |
"HelpManager": Mock( |
| 187 |
is_tutorial_mode=Mock(return_value=True) |
| 188 |
), |
| 189 |
"DebugManager": Mock(), |
| 190 |
"OutputManager": output_manager, |
| 191 |
} |
| 192 |
} |
| 193 |
command_manager.command_exists = Mock(return_value=True) |
| 194 |
command_manager.get_command_description = Mock( |
| 195 |
return_value="spell check" |
| 196 |
) |
| 197 |
|
| 198 |
command_manager.execute_command("SPELL_CHECK", "commands") |
| 199 |
|
| 200 |
output_manager.present_text.assert_called_once_with( |
| 201 |
"spell check", interrupt=False |
| 202 |
) |