| 1 |
import importlib.util |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from fenrirscreenreader.core.inputManager import InputManager |
| 8 |
|
| 9 |
|
| 10 |
PROJECT_ROOT = Path(__file__).resolve().parents[2] |
| 11 |
COMMANDS_DIR = ( |
| •12 |
PROJECT_ROOT / "fenrirscreenreader" / "commands" / "commands" |
| 13 |
) |
| •14 |
KEYBOARD_DIR = PROJECT_ROOT / ".." / ".." / ".." / "config" / "keyboard" |
| 15 |
|
| 16 |
|
| 17 |
def load_command(name): |
| 18 |
spec = importlib.util.spec_from_file_location( |
| 19 |
f"fenrir_{name}", COMMANDS_DIR / f"{name}.py" |
| 20 |
) |
| 21 |
module = importlib.util.module_from_spec(spec) |
| 22 |
spec.loader.exec_module(module) |
| 23 |
return module.command() |
| 24 |
|
| 25 |
|
| 26 |
def run_command(name, content, cursor_x=0, cursor_y=0): |
| 27 |
output_manager = Mock() |
| 28 |
settings_manager = Mock() |
| 29 |
settings_manager.get_setting_as_bool.return_value = True |
| 30 |
env = { |
| 31 |
"punctuation": {"PUNCTDICT": {" ": "space"}}, |
| 32 |
"screen": { |
| 33 |
"newCursorReview": {"x": cursor_x, "y": cursor_y}, |
| 34 |
"new_content_text": content, |
| 35 |
}, |
| 36 |
"runtime": { |
| 37 |
"CursorManager": Mock(), |
| 38 |
"OutputManager": output_manager, |
| 39 |
"SettingsManager": settings_manager, |
| 40 |
}, |
| 41 |
} |
| 42 |
command = load_command(name) |
| 43 |
command.initialize(env) |
| 44 |
command.run() |
| 45 |
return output_manager |
| 46 |
|
| 47 |
|
| 48 |
def load_bindings(layout): |
| 49 |
manager = InputManager() |
| 50 |
manager.env = { |
| 51 |
"bindings": {}, |
| 52 |
"rawBindings": {}, |
| 53 |
"runtime": {"DebugManager": Mock()}, |
| 54 |
} |
| 55 |
manager.load_shortcuts(KEYBOARD_DIR / f"{layout}.conf") |
| 56 |
return manager.env["bindings"] |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.unit |
| 60 |
def test_current_word_is_spelled_one_character_at_a_time(): |
| 61 |
output_manager = run_command( |
| 62 |
"review_curr_word_spell", "hello world", cursor_x=2 |
| 63 |
) |
| 64 |
|
| 65 |
calls = output_manager.present_text.call_args_list |
| 66 |
assert [call.args[0] for call in calls] == list("hello") |
| 67 |
assert calls[0].kwargs == { |
| 68 |
"interrupt": True, |
| 69 |
"ignore_punctuation": True, |
| 70 |
"announce_capital": True, |
| 71 |
"flush": False, |
| 72 |
} |
| 73 |
assert all(call.kwargs["interrupt"] is False for call in calls[1:]) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.unit |
| 77 |
def test_current_word_spelling_preserves_capitals_and_punctuation(): |
| 78 |
output_manager = run_command("review_curr_word_spell", "Hi!", cursor_x=1) |
| 79 |
|
| 80 |
assert [ |
| 81 |
call.args[0] for call in output_manager.present_text.call_args_list |
| 82 |
] == ["H", "i", "!"] |
| 83 |
|
| 84 |
|
| 85 |
@pytest.mark.unit |
| 86 |
@pytest.mark.parametrize( |
| 87 |
"command_name", |
| 88 |
["review_curr_word_spell", "review_curr_word_phonetic"], |
| 89 |
) |
| 90 |
def test_current_word_spelling_announces_blank_when_no_word_exists( |
| 91 |
command_name, |
| 92 |
): |
| 93 |
output_manager = run_command(command_name, " ") |
| 94 |
|
| 95 |
output_manager.present_text.assert_called_once_with( |
| 96 |
"blank", interrupt=True, flush=False |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
@pytest.mark.unit |
| 101 |
def test_current_word_phonetic_spelling_uses_phonetic_names(): |
| 102 |
output_manager = run_command( |
| 103 |
"review_curr_word_phonetic", "Az!", cursor_x=1 |
| 104 |
) |
| 105 |
|
| 106 |
calls = output_manager.present_text.call_args_list |
| 107 |
assert [call.args[0] for call in calls] == ["Alpha", "zulu", "!"] |
| 108 |
assert calls[0].kwargs["interrupt"] is True |
| 109 |
assert all(call.kwargs["interrupt"] is False for call in calls[1:]) |
| 110 |
|
| 111 |
|
| 112 |
@pytest.mark.unit |
| 113 |
@pytest.mark.parametrize( |
| 114 |
("layout", "character_keys", "word_keys"), |
| 115 |
[ |
| 116 |
("desktop", ["KEY_KP2"], ["KEY_KP5"]), |
| 117 |
("laptop", ["KEY_COMMA", "KEY_FENRIR"], ["KEY_FENRIR", "KEY_K"]), |
| 118 |
], |
| 119 |
) |
| 120 |
def test_progressive_review_bindings(layout, character_keys, word_keys): |
| 121 |
bindings = load_bindings(layout) |
| 122 |
|
| 123 |
assert bindings[str([1, character_keys])] == "REVIEW_CURR_CHAR" |
| 124 |
assert bindings[str([2, character_keys])] == "REVIEW_CURR_CHAR_PHONETIC" |
| 125 |
assert bindings[str([1, word_keys])] == "REVIEW_CURR_WORD" |
| 126 |
assert bindings[str([2, word_keys])] == "REVIEW_CURR_WORD_SPELL" |
| 127 |
assert bindings[str([3, word_keys])] == "REVIEW_CURR_WORD_PHONETIC" |
| 128 |
|
| 129 |
|
| 130 |
@pytest.mark.unit |
| 131 |
@pytest.mark.parametrize("layout", ["desktop", "laptop"]) |
| 132 |
def test_layouts_do_not_bind_removed_keyboard_layout_cycle(layout): |
| 133 |
bindings = load_bindings(layout) |
| 134 |
|
| 135 |
assert "CYCLE_KEYBOARD_LAYOUT" not in bindings.values() |
| 136 |
|
| 137 |
removed_commands = { |
| 138 |
"REVIEW_PREV_CHAR_PHONETIC", |
| 139 |
"REVIEW_NEXT_CHAR_PHONETIC", |
| 140 |
"REVIEW_PREV_WORD_PHONETIC", |
| 141 |
"REVIEW_NEXT_WORD_PHONETIC", |
| 142 |
} |
| 143 |
assert removed_commands.isdisjoint(bindings.values()) |
| 144 |
|
| 145 |
|
| 146 |
@pytest.mark.unit |
| 147 |
def test_obsolete_phonetic_navigation_commands_are_removed(): |
| 148 |
removed_commands = [ |
| 149 |
"review_prev_char_phonetic.py", |
| 150 |
"review_next_char_phonetic.py", |
| 151 |
"review_prev_word_phonetic.py", |
| 152 |
"review_next_word_phonetic.py", |
| 153 |
] |
| 154 |
|
| 155 |
assert all(not (COMMANDS_DIR / name).exists() for name in removed_commands) |