|
| 1 | +"""Qt integration tests for the Remote Desktop GUI tab. |
| 2 | +
|
| 3 | +Runs against an offscreen QApplication so it stays headless. Verifies |
| 4 | +the viewer's FrameDisplay actually receives and decodes JPEG frames |
| 5 | +end-to-end, and that the host preview pane mirrors what is being sent. |
| 6 | +""" |
| 7 | +import os |
| 8 | +import time |
| 9 | +from io import BytesIO |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +# Force Qt to use the offscreen platform plugin so the test runs without a |
| 14 | +# display server (and without flashing windows on a real desktop). |
| 15 | +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") |
| 16 | + |
| 17 | +PIL = pytest.importorskip("PIL.Image") |
| 18 | +pyside = pytest.importorskip("PySide6.QtWidgets") |
| 19 | + |
| 20 | +from PySide6.QtCore import Qt # noqa: E402 |
| 21 | +from PySide6.QtWidgets import QApplication # noqa: E402 |
| 22 | + |
| 23 | +from je_auto_control.utils.remote_desktop.registry import registry # noqa: E402 |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture(scope="module") |
| 27 | +def qapp(): |
| 28 | + app = QApplication.instance() or QApplication([]) |
| 29 | + yield app |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +def reset_registry(): |
| 34 | + registry.disconnect_viewer() |
| 35 | + registry.stop_host() |
| 36 | + yield |
| 37 | + registry.disconnect_viewer() |
| 38 | + registry.stop_host() |
| 39 | + |
| 40 | + |
| 41 | +def _make_jpeg(width: int = 64, height: int = 48) -> bytes: |
| 42 | + """Encode a small solid-color image to JPEG.""" |
| 43 | + from PIL import Image |
| 44 | + img = Image.new("RGB", (width, height), color=(255, 0, 0)) |
| 45 | + buf = BytesIO() |
| 46 | + img.save(buf, format="JPEG", quality=70) |
| 47 | + return buf.getvalue() |
| 48 | + |
| 49 | + |
| 50 | +def _process_until(app: QApplication, predicate, timeout: float = 3.0, |
| 51 | + interval_ms: int = 20) -> bool: |
| 52 | + deadline = time.monotonic() + timeout |
| 53 | + while time.monotonic() < deadline: |
| 54 | + app.processEvents() |
| 55 | + if predicate(): |
| 56 | + return True |
| 57 | + time.sleep(interval_ms / 1000.0) |
| 58 | + app.processEvents() |
| 59 | + return predicate() |
| 60 | + |
| 61 | + |
| 62 | +def test_viewer_panel_renders_frame_from_host(qapp): |
| 63 | + from je_auto_control.gui.remote_desktop_tab import _ViewerPanel |
| 64 | + from je_auto_control.utils.remote_desktop.host import RemoteDesktopHost |
| 65 | + |
| 66 | + jpeg = _make_jpeg() |
| 67 | + captured_input = [] |
| 68 | + |
| 69 | + host = RemoteDesktopHost( |
| 70 | + token="t", bind="127.0.0.1", port=0, fps=30.0, |
| 71 | + frame_provider=lambda: jpeg, |
| 72 | + input_dispatcher=captured_input.append, |
| 73 | + ) |
| 74 | + host.start() |
| 75 | + registry._host = host # noqa: SLF001 # test-only injection |
| 76 | + try: |
| 77 | + panel = _ViewerPanel() |
| 78 | + panel._host_field.setText("127.0.0.1") # noqa: SLF001 |
| 79 | + panel._port.setValue(host.port) # noqa: SLF001 |
| 80 | + panel._token.setText("t") # noqa: SLF001 |
| 81 | + panel._connect() # noqa: SLF001 |
| 82 | + assert _process_until(qapp, panel._display.has_image) # noqa: SLF001 |
| 83 | + # Display image must match the encoded frame size. |
| 84 | + assert panel._display._image.width() == 64 # noqa: SLF001 |
| 85 | + assert panel._display._image.height() == 48 # noqa: SLF001 |
| 86 | + finally: |
| 87 | + registry.disconnect_viewer() |
| 88 | + host.stop(timeout=1.0) |
| 89 | + registry._host = None # noqa: SLF001 |
| 90 | + |
| 91 | + |
| 92 | +def test_host_preview_shows_streamed_frame(qapp): |
| 93 | + from je_auto_control.gui.remote_desktop_tab import _HostPanel |
| 94 | + from je_auto_control.utils.remote_desktop.host import RemoteDesktopHost |
| 95 | + |
| 96 | + jpeg = _make_jpeg(80, 60) |
| 97 | + host = RemoteDesktopHost( |
| 98 | + token="t", bind="127.0.0.1", port=0, fps=30.0, |
| 99 | + frame_provider=lambda: jpeg, |
| 100 | + ) |
| 101 | + host.start() |
| 102 | + registry._host = host # noqa: SLF001 |
| 103 | + try: |
| 104 | + panel = _HostPanel() |
| 105 | + # Speed the preview poll up so the test does not need to wait 250ms+. |
| 106 | + panel._preview_timer.setInterval(20) # noqa: SLF001 |
| 107 | + assert _process_until(qapp, panel._preview.has_image) # noqa: SLF001 |
| 108 | + assert panel._preview._image.width() == 80 # noqa: SLF001 |
| 109 | + assert panel._preview._image.height() == 60 # noqa: SLF001 |
| 110 | + finally: |
| 111 | + host.stop(timeout=1.0) |
| 112 | + registry._host = None # noqa: SLF001 |
| 113 | + |
| 114 | + |
| 115 | +def test_viewer_input_round_trips_to_dispatcher(qapp): |
| 116 | + from je_auto_control.gui.remote_desktop_tab import _ViewerPanel |
| 117 | + from je_auto_control.utils.remote_desktop.host import RemoteDesktopHost |
| 118 | + |
| 119 | + jpeg = _make_jpeg() |
| 120 | + captured = [] |
| 121 | + host = RemoteDesktopHost( |
| 122 | + token="t", bind="127.0.0.1", port=0, fps=30.0, |
| 123 | + frame_provider=lambda: jpeg, |
| 124 | + input_dispatcher=captured.append, |
| 125 | + ) |
| 126 | + host.start() |
| 127 | + registry._host = host # noqa: SLF001 |
| 128 | + try: |
| 129 | + panel = _ViewerPanel() |
| 130 | + panel._host_field.setText("127.0.0.1") # noqa: SLF001 |
| 131 | + panel._port.setValue(host.port) # noqa: SLF001 |
| 132 | + panel._token.setText("t") # noqa: SLF001 |
| 133 | + panel._connect() # noqa: SLF001 |
| 134 | + assert _process_until(qapp, panel._display.has_image) # noqa: SLF001 |
| 135 | + |
| 136 | + panel._send_mouse_move(11, 13) # noqa: SLF001 |
| 137 | + panel._send_mouse_press(11, 13, "mouse_left") # noqa: SLF001 |
| 138 | + |
| 139 | + assert _process_until( |
| 140 | + qapp, |
| 141 | + lambda: any(c.get("action") == "mouse_press" for c in captured), |
| 142 | + ) |
| 143 | + moves = [c for c in captured if c.get("action") == "mouse_move"] |
| 144 | + assert any(c == {"action": "mouse_move", "x": 11, "y": 13} |
| 145 | + for c in moves) |
| 146 | + finally: |
| 147 | + registry.disconnect_viewer() |
| 148 | + host.stop(timeout=1.0) |
| 149 | + registry._host = None # noqa: SLF001 |
0 commit comments