| 1 |
import importlib.util |
| 2 |
from pathlib import Path |
| 3 |
from unittest.mock import Mock |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
|
| 8 |
def load_lock_command(command_file): |
| 9 |
command_path = ( |
| 10 |
Path(__file__).resolve().parents[2] |
| 11 |
/ "fenrirscreenreader" |
| 12 |
/ "commands" |
| 13 |
/ "onKeyInput" |
| 14 |
/ command_file |
| 15 |
) |
| 16 |
spec = importlib.util.spec_from_file_location( |
| 17 |
command_file.replace("-", "_"), command_path |
| 18 |
) |
| 19 |
module = importlib.util.module_from_spec(spec) |
| 20 |
spec.loader.exec_module(module) |
| 21 |
return module.command |
| 22 |
|
| 23 |
|
| 24 |
def build_env(input_data): |
| 25 |
output_manager = Mock() |
| 26 |
return { |
| 27 |
"input": input_data, |
| 28 |
"runtime": { |
| 29 |
"OutputManager": output_manager, |
| 30 |
}, |
| 31 |
}, output_manager |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.unit |
| 35 |
def test_capslock_command_ignores_non_capslock_state_change(): |
| 36 |
command = load_lock_command("80000-capslock.py")() |
| 37 |
env, output_manager = build_env( |
| 38 |
{ |
| 39 |
"old_caps_lock": False, |
| 40 |
"new_caps_lock": True, |
| 41 |
"curr_input": ["KEY_KP8"], |
| 42 |
"prev_input": [], |
| 43 |
} |
| 44 |
) |
| 45 |
command.initialize(env) |
| 46 |
|
| 47 |
command.run() |
| 48 |
|
| 49 |
output_manager.present_text.assert_not_called() |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.unit |
| 53 |
def test_capslock_command_announces_capslock_release_change(): |
| 54 |
command = load_lock_command("80000-capslock.py")() |
| 55 |
env, output_manager = build_env( |
| 56 |
{ |
| 57 |
"old_caps_lock": True, |
| 58 |
"new_caps_lock": False, |
| 59 |
"curr_input": [], |
| 60 |
"prev_input": ["KEY_CAPSLOCK"], |
| 61 |
} |
| 62 |
) |
| 63 |
command.initialize(env) |
| 64 |
|
| 65 |
command.run() |
| 66 |
|
| 67 |
output_manager.present_text.assert_called_once() |
| 68 |
assert output_manager.present_text.call_args.args[0] == "Capslock off" |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.unit |
| 72 |
def test_scrolllock_command_ignores_non_scrolllock_state_change(): |
| 73 |
command = load_lock_command("80300-scrolllock.py")() |
| 74 |
env, output_manager = build_env( |
| 75 |
{ |
| 76 |
"old_scroll_lock": False, |
| 77 |
"new_scroll_lock": True, |
| 78 |
"curr_input": ["KEY_KP8"], |
| 79 |
"prev_input": [], |
| 80 |
} |
| 81 |
) |
| 82 |
command.initialize(env) |
| 83 |
|
| 84 |
command.run() |
| 85 |
|
| 86 |
output_manager.present_text.assert_not_called() |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.unit |
| 90 |
def test_scrolllock_command_announces_scrolllock_release_change(): |
| 91 |
command = load_lock_command("80300-scrolllock.py")() |
| 92 |
env, output_manager = build_env( |
| 93 |
{ |
| 94 |
"old_scroll_lock": True, |
| 95 |
"new_scroll_lock": False, |
| 96 |
"curr_input": [], |
| 97 |
"prev_input": ["KEY_SCROLLLOCK"], |
| 98 |
} |
| 99 |
) |
| 100 |
command.initialize(env) |
| 101 |
|
| 102 |
command.run() |
| 103 |
|
| 104 |
output_manager.present_text.assert_called_once() |
| 105 |
assert output_manager.present_text.call_args.args[0] == "Scrolllock off" |