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