|
| 1 | +import threading |
| 2 | +import unittest |
| 3 | +from types import SimpleNamespace |
| 4 | +from unittest.mock import Mock, patch |
| 5 | + |
| 6 | +import ok.gui.StartController as start_controller_module |
| 7 | +from ok.gui.StartController import StartController |
| 8 | + |
| 9 | + |
| 10 | +class FakeClock: |
| 11 | + def __init__(self): |
| 12 | + self.value = 0 |
| 13 | + |
| 14 | + def monotonic(self): |
| 15 | + return self.value |
| 16 | + |
| 17 | + def sleep(self, seconds): |
| 18 | + self.value += seconds |
| 19 | + |
| 20 | + |
| 21 | +class FakeWindow: |
| 22 | + def __init__(self, sizes): |
| 23 | + self.sizes = iter(sizes) |
| 24 | + self.hwnd = 0 |
| 25 | + self.width = 0 |
| 26 | + self.height = 0 |
| 27 | + self.updates = 0 |
| 28 | + |
| 29 | + def do_update_window_size(self): |
| 30 | + self.width, self.height = next(self.sizes) |
| 31 | + self.hwnd = 1 |
| 32 | + self.updates += 1 |
| 33 | + |
| 34 | + |
| 35 | +class TestStartController(unittest.TestCase): |
| 36 | + def make_controller(self): |
| 37 | + controller = StartController.__new__(StartController) |
| 38 | + controller.exit_event = threading.Event() |
| 39 | + controller.start_timeout = 20 |
| 40 | + controller.STARTED_WINDOW_STABLE_SECONDS = 2 |
| 41 | + controller.STARTED_WINDOW_POLL_INTERVAL = 1 |
| 42 | + return controller |
| 43 | + |
| 44 | + def test_started_window_must_be_usable_and_stable_before_continuing(self): |
| 45 | + controller = self.make_controller() |
| 46 | + window = FakeWindow([(80, 80), (120, 120), (140, 120), (140, 120), (140, 120)]) |
| 47 | + clock = FakeClock() |
| 48 | + fake_og = SimpleNamespace(device_manager=SimpleNamespace(hwnd_window=window)) |
| 49 | + |
| 50 | + with patch.object(start_controller_module, 'og', fake_og), \ |
| 51 | + patch.object(start_controller_module.time, 'monotonic', clock.monotonic), \ |
| 52 | + patch.object(start_controller_module.time, 'sleep', clock.sleep): |
| 53 | + self.assertTrue(controller._wait_until_started_window_stable()) |
| 54 | + |
| 55 | + self.assertEqual(5, window.updates) |
| 56 | + |
| 57 | + def test_started_window_wait_does_not_restart_countdown(self): |
| 58 | + controller = self.make_controller() |
| 59 | + controller.STARTED_WINDOW_STABLE_SECONDS = 1 |
| 60 | + controller.STARTED_WINDOW_POLL_INTERVAL = 0.2 |
| 61 | + window = FakeWindow([(120, 120)] * 6) |
| 62 | + clock = FakeClock() |
| 63 | + fake_og = SimpleNamespace(device_manager=SimpleNamespace(hwnd_window=window)) |
| 64 | + emit = Mock() |
| 65 | + fake_communicate = SimpleNamespace(starting_emulator=SimpleNamespace(emit=emit)) |
| 66 | + |
| 67 | + with patch.object(start_controller_module, 'og', fake_og), \ |
| 68 | + patch.object(start_controller_module, 'communicate', fake_communicate), \ |
| 69 | + patch.object(start_controller_module.time, 'monotonic', clock.monotonic), \ |
| 70 | + patch.object(start_controller_module.time, 'sleep', clock.sleep): |
| 71 | + self.assertTrue(controller._wait_until_started_window_stable()) |
| 72 | + |
| 73 | + emit.assert_not_called() |
| 74 | + |
| 75 | + def test_started_windows_wait_for_stability_before_capture_readiness(self): |
| 76 | + controller = self.make_controller() |
| 77 | + device_manager = Mock() |
| 78 | + device_manager.get_preferred_device.return_value = { |
| 79 | + 'connected': False, |
| 80 | + 'device': 'windows', |
| 81 | + } |
| 82 | + device_manager.get_exe_path.return_value = r'C:\game.exe' |
| 83 | + fake_og = SimpleNamespace( |
| 84 | + device_manager=device_manager, |
| 85 | + global_config=Mock(), |
| 86 | + ) |
| 87 | + fake_og.global_config.get_config.return_value = None |
| 88 | + |
| 89 | + call_order = [] |
| 90 | + controller._wait_until_started_window_stable = Mock( |
| 91 | + side_effect=lambda: call_order.append('stable') or True) |
| 92 | + controller._wait_until_device_ready = Mock( |
| 93 | + side_effect=lambda: call_order.append('ready') or True) |
| 94 | + |
| 95 | + with patch.object(start_controller_module, 'og', fake_og), \ |
| 96 | + patch.object(start_controller_module, 'is_admin', return_value=True), \ |
| 97 | + patch.object(start_controller_module, 'execute', |
| 98 | + side_effect=lambda *args, **kwargs: call_order.append('execute') or True): |
| 99 | + self.assertTrue(controller.start_device()) |
| 100 | + |
| 101 | + self.assertEqual(['execute', 'stable', 'ready'], call_order) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + unittest.main() |
0 commit comments