|
23 | 23 | from io import StringIO |
24 | 24 |
|
25 | 25 | from . import UtilityMixin |
26 | | - |
| 26 | +from simpleline.render.screen import UIScreen |
| 27 | +from simpleline.input.input_handler import InputHandler |
27 | 28 | from simpleline.render.adv_widgets import GetInputScreen, GetPasswordInputScreen |
28 | 29 |
|
29 | 30 |
|
| 31 | +class InputHandlerMock(InputHandler): |
| 32 | + """Dummy InputHandler instance. |
| 33 | +
|
| 34 | + This class based on InputHandler has all threading logic disabled for simpler testing. |
| 35 | + """ |
| 36 | + def _register_input_ready_signal(self): |
| 37 | + pass |
| 38 | + |
| 39 | + def get_input(self, prompt): |
| 40 | + pass |
| 41 | + |
| 42 | + def wait_on_input(self): |
| 43 | + pass |
| 44 | + |
| 45 | + |
| 46 | +class UIScreen_TestCase(unittest.TestCase): |
| 47 | + @patch('simpleline.render.screen.input_manager.InputHandler') |
| 48 | + def test_uiscreen_disable_concurrency_check(self, input_handler_class_mock): |
| 49 | + # Replace default InputHandler instance created by InputManager by our mock to have |
| 50 | + # it stored after use. |
| 51 | + input_handler_instance_mock = InputHandlerMock() |
| 52 | + input_handler_class_mock.return_value = input_handler_instance_mock |
| 53 | + |
| 54 | + screen = UIScreen() |
| 55 | + input_manager = screen.input_manager |
| 56 | + input_manager.skip_concurrency_check = True |
| 57 | + screen.get_user_input("test") |
| 58 | + |
| 59 | + self.assertTrue(screen.input_manager.skip_concurrency_check) |
| 60 | + # Check that the created InputHandler instance has the flag correctly set. |
| 61 | + # We don't need to check the concurrency check functionality, it is tested |
| 62 | + # elsewhere already. |
| 63 | + self.assertTrue(input_handler_instance_mock.skip_concurrency_check) |
| 64 | + |
| 65 | + @patch('simpleline.render.screen.input_manager.InputHandler') |
| 66 | + def test_uiscreen_password_disable_concurrency_check(self, input_handler_class_mock): |
| 67 | + # Replace default InputHandler instance created by InputManager by our mock to have |
| 68 | + # it stored after use. |
| 69 | + input_handler_instance_mock = InputHandlerMock() |
| 70 | + input_handler_class_mock.return_value = input_handler_instance_mock |
| 71 | + |
| 72 | + screen = GetPasswordInputScreen(message="Test prompt") |
| 73 | + input_manager = screen.input_manager |
| 74 | + input_manager.skip_concurrency_check = True |
| 75 | + screen.get_user_input("test") |
| 76 | + |
| 77 | + self.assertTrue(screen.input_manager.skip_concurrency_check) |
| 78 | + # Check that the created InputHandler instance has the flag correctly set. |
| 79 | + # We don't need to check the concurrency check functionality, it is tested |
| 80 | + # elsewhere already. |
| 81 | + self.assertTrue(input_handler_instance_mock.skip_concurrency_check) |
| 82 | + |
| 83 | + @patch('simpleline.render.screen.input_manager.InputHandler') |
| 84 | + def test_uiscreen_non_blocking_input_disable_concurrency_check(self, input_handler_class_mock): |
| 85 | + # Replace default InputHandler instance created by InputManager by our mock to have |
| 86 | + # it stored after use. |
| 87 | + input_handler_instance_mock = InputHandlerMock() |
| 88 | + input_handler_class_mock.return_value = input_handler_instance_mock |
| 89 | + |
| 90 | + screen = UIScreen() |
| 91 | + input_manager = screen.input_manager |
| 92 | + input_manager.skip_concurrency_check = True |
| 93 | + screen.get_input_with_error_check("test") |
| 94 | + |
| 95 | + self.assertTrue(screen.input_manager.skip_concurrency_check) |
| 96 | + # Check that the created InputHandler instance has the flag correctly set. |
| 97 | + # We don't need to check the concurrency check functionality, it is tested |
| 98 | + # elsewhere already. |
| 99 | + self.assertTrue(input_handler_instance_mock.skip_concurrency_check) |
| 100 | + |
| 101 | + @patch('simpleline.render.screen.input_manager.InputHandler') |
| 102 | + def test_uiscreen_default_concurrency_check(self, input_handler_class_mock): |
| 103 | + # Replace default InputHandler instance created by InputManager by our mock to have |
| 104 | + # it stored after use. |
| 105 | + input_handler_instance_mock = InputHandlerMock() |
| 106 | + input_handler_class_mock.return_value = input_handler_instance_mock |
| 107 | + |
| 108 | + screen = UIScreen() |
| 109 | + screen.get_user_input("test") |
| 110 | + |
| 111 | + self.assertFalse(screen.input_manager.skip_concurrency_check) |
| 112 | + # Check that the created InputHandler instance has the flag correctly set. |
| 113 | + # We don't need to check the concurrency check functionality, it is tested |
| 114 | + # elsewhere already. |
| 115 | + self.assertFalse(input_handler_instance_mock.skip_concurrency_check) |
| 116 | + |
| 117 | + |
30 | 118 | @patch('simpleline.input.input_handler.InputHandlerRequest._get_input') |
31 | 119 | @patch('sys.stdout', new_callable=StringIO) |
32 | 120 | class AdvWidgets_TestCase(unittest.TestCase, UtilityMixin): |
|
0 commit comments