From d59a3df5a376fca629f54ee51167f8c8e1d013d4 Mon Sep 17 00:00:00 2001 From: yang you <119789235+REDrighthand05@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:55:42 +0800 Subject: [PATCH 1/2] Reuse desktop preview and WebGL state; respect ONNX batch shape --- .github/workflows/desktop-runtime.yml | 29 +++++++++++++ echo_scene/depth_batch.py | 43 +++++++++++++------ tests/test_depth_dispatch.py | 59 +++++++++++++++++++++++++++ tests/test_web_bridge.py | 19 +++++++-- tests/test_window_resize.py | 43 +++++++++++++++++++ web_app.py | 30 ++++++++------ web_ui/app.js | 54 +++++++++++++++++------- 7 files changed, 234 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/desktop-runtime.yml create mode 100644 tests/test_depth_dispatch.py create mode 100644 tests/test_window_resize.py diff --git a/.github/workflows/desktop-runtime.yml b/.github/workflows/desktop-runtime.yml new file mode 100644 index 00000000..e734313f --- /dev/null +++ b/.github/workflows/desktop-runtime.yml @@ -0,0 +1,29 @@ +name: Desktop runtime regression +on: + pull_request: + branches: [product/echo-scene] + paths: + - 'web_ui/**' + - 'web_app.py' + - 'echo_scene/depth_batch.py' + - 'tests/test_web_bridge.py' + - 'tests/test_window_resize.py' + - 'tests/test_depth_dispatch.py' + - '.github/workflows/desktop-runtime.yml' +permissions: + contents: read +jobs: + windows: + runs-on: windows-latest + timeout-minutes: 12 + env: + PYTHONUTF8: "1" + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - run: python -m pip install numpy pillow onnxruntime-directml playwright + - run: python tests/test_web_bridge.py + - run: python tests/test_depth_dispatch.py + - run: python tests/test_window_resize.py diff --git a/echo_scene/depth_batch.py b/echo_scene/depth_batch.py index 8ad67df4..8d4865e5 100644 --- a/echo_scene/depth_batch.py +++ b/echo_scene/depth_batch.py @@ -1,7 +1,6 @@ """Batch depth estimation utilities for a single-frame ONNX depth model.""" import numpy as np -import onnxruntime as ort from PIL import Image @@ -41,9 +40,8 @@ def _extract_frame_output(output, index, batch_len): raise ValueError("unexpected batch output shape") -def _infer_single(path, session): +def _infer_prepared(tensor, width, height, session): try: - tensor, width, height = _preprocess_image(path) input_array = np.ascontiguousarray(tensor[np.newaxis, :, :, :], dtype=np.float32) outputs = session.run(["predicted_depth"], {"pixel_values": input_array}) frame = _extract_frame_output(outputs[0], 0, 1) @@ -52,12 +50,29 @@ def _infer_single(path, session): return None +def _infer_single(path, session): + try: + tensor, width, height = _preprocess_image(path) + except Exception: + return None + return _infer_prepared(tensor, width, height, session) + + def make_session(model_path, providers=None): + import onnxruntime as ort + if providers is not None: return ort.InferenceSession(str(model_path), providers=providers) return ort.InferenceSession(str(model_path)) +def _pixel_batch_dimension(session): + for model_input in session.get_inputs(): + if model_input.name == "pixel_values" and model_input.shape: + return model_input.shape[0] + return None + + def iter_depth_estimates(image_paths, session, batch_size: int = 4, chunk_size: int = 16): """Yield (index, depth_or_None) streaming over bounded chunks. @@ -73,6 +88,10 @@ def iter_depth_estimates(image_paths, session, batch_size: int = 4, estimate_depth_batch call over the whole list would produce. """ paths = list(image_paths) + if paths: + print(f"Depth runtime: providers={session.get_providers()}, " + f"model batch dimension={_pixel_batch_dimension(session)}, " + f"requested batch={batch_size}", flush=True) if batch_size > 1: chunk_size = max(batch_size, chunk_size // batch_size * batch_size) @@ -90,7 +109,9 @@ def estimate_depth_batch(image_paths, session, batch_size: int = 4) -> list: if not paths: return [] - if batch_size <= 1: + # ONNX models exported with batch dimension 1 cannot accept a stack. + # A symbolic/unknown dimension keeps the existing batched path. + if batch_size <= 1 or _pixel_batch_dimension(session) == 1: return [_infer_single(path, session) for path in paths] results = [None] * len(paths) @@ -100,21 +121,19 @@ def estimate_depth_batch(image_paths, session, batch_size: int = 4) -> list: chunk_tensors = [] chunk_meta = [] chunk_indices = [] - preprocess_ok = True for offset, path in enumerate(chunk_paths): try: tensor, width, height = _preprocess_image(path) except Exception: - preprocess_ok = False - break + continue chunk_tensors.append(tensor) chunk_meta.append((width, height)) chunk_indices.append(start + offset) - if not preprocess_ok: - for offset, path in enumerate(chunk_paths): - results[start + offset] = _infer_single(path, session) + if len(chunk_tensors) != len(chunk_paths): + for idx, tensor, (width, height) in zip(chunk_indices, chunk_tensors, chunk_meta): + results[idx] = _infer_prepared(tensor, width, height, session) continue try: @@ -126,8 +145,8 @@ def estimate_depth_batch(image_paths, session, batch_size: int = 4) -> list: for i in range(len(chunk_tensors)) ] except Exception: - for offset, path in enumerate(chunk_paths): - results[start + offset] = _infer_single(path, session) + for idx, tensor, (width, height) in zip(chunk_indices, chunk_tensors, chunk_meta): + results[idx] = _infer_prepared(tensor, width, height, session) continue for i, (width, height) in enumerate(chunk_meta): diff --git a/tests/test_depth_dispatch.py b/tests/test_depth_dispatch.py new file mode 100644 index 00000000..e41279e1 --- /dev/null +++ b/tests/test_depth_dispatch.py @@ -0,0 +1,59 @@ +"""Check batch dispatch, without downloading models or requiring a GPU.""" +import tempfile +import unittest +from pathlib import Path +import sys +from types import SimpleNamespace +from unittest.mock import patch + +from PIL import Image +import numpy as np + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from echo_scene import depth_batch + + +class Session: + def __init__(self, dimension, reject_batches=False): + self.dimension = dimension + self.reject_batches = reject_batches + self.calls = [] + + def get_inputs(self): + return [SimpleNamespace(name="pixel_values", shape=[self.dimension, 3, 518, 518])] + + def run(self, outputs, inputs): + tensor = inputs["pixel_values"] + self.calls.append(len(tensor)) + if self.reject_batches and len(tensor) > 1: + raise ValueError("batch unsupported") + return [tensor[:, 0]] + + +class DepthDispatch(unittest.TestCase): + def test_fixed_dynamic_and_fallback(self): + with tempfile.TemporaryDirectory() as temp: + paths = [] + for i in range(5): + path = Path(temp) / f"{i}.png" + Image.new("RGB", (12 + i, 20), (40 * i, 0, 0)).save(path) + paths.append(path) + fixed = Session(1) + golden = depth_batch.estimate_depth_batch(paths, fixed) + self.assertEqual(fixed.calls, [1] * 5) + for reject, expected in [(False, [4, 1]), (True, [4, 1, 1, 1, 1, 1])]: + session = Session("N", reject) + with patch.object(depth_batch, "_preprocess_image", + wraps=depth_batch._preprocess_image) as prep: + result = depth_batch.estimate_depth_batch(paths, session) + self.assertEqual(session.calls, expected) + self.assertEqual(prep.call_count, len(paths)) + for wanted, got in zip(golden, result): + np.testing.assert_array_equal(wanted, got) + mixed = depth_batch.estimate_depth_batch([paths[0], Path(temp)/"missing", paths[2]], Session("N")) + self.assertIsNone(mixed[1]) + np.testing.assert_array_equal(golden[2], mixed[2]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_web_bridge.py b/tests/test_web_bridge.py index 5073b5c5..06cc7bc5 100644 --- a/tests/test_web_bridge.py +++ b/tests/test_web_bridge.py @@ -9,6 +9,7 @@ import sys import tempfile import time +from unittest.mock import patch from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) @@ -74,6 +75,18 @@ def main(): check("poll reports idle state", poll["busy"] is False and poll["hasJob"] is False) + bridge._publish_preview(b"frame one") + first = bridge.poll(-1) + check("first poll delivers the current preview", bool(first.get("preview"))) + check("acknowledged preview is omitted", "preview" not in bridge.poll(first["previewSeq"])) + bridge._publish_preview(b"frame two") + hidden = bridge.poll(first["previewSeq"], False) + restored = bridge.poll(first["previewSeq"]) + check("hidden poll skips image and restore catches up", + "preview" not in hidden and restored["preview"] != first["preview"]) + check("legacy callers still receive complete previews", + bridge.poll()["preview"] == bridge.get_preview_frame()["data"] == restored["preview"]) + # a job with no outputs cannot open a scene bridge.job = running check("open_scene on unfinished job is a no-op", @@ -113,9 +126,9 @@ def rel(tag, draft=False): and hop["tag_name"] == "v0.5.1") check("hop at the top is None", updater.pick_hop(releases, "0.5.1") is None) - check("manual check never raises and answers honestly", - bridge.manual_check_update()["status"] in - ("update", "latest", "error")) + with patch.object(updater, "_urlopen_with", side_effect=OSError("offline")): + check("manual check reports an offline network", + bridge.manual_check_update()["status"] == "error") # Route cascade: direct dead, first candidate dead, second # answers -> it wins, is cached, and is reported as auto-found. diff --git a/tests/test_window_resize.py b/tests/test_window_resize.py new file mode 100644 index 00000000..8eabdacd --- /dev/null +++ b/tests/test_window_resize.py @@ -0,0 +1,43 @@ +"""Exercise the shipped desktop page in Edge, with no desktop bridge required.""" +from pathlib import Path + +from playwright.sync_api import sync_playwright + + +def main(): + url = (Path(__file__).resolve().parents[1] / "web_ui/index.html").as_uri() + with sync_playwright() as playwright: + browser = playwright.chromium.launch(channel="msedge", args=[ + "--enable-unsafe-swiftshader", "--use-angle=swiftshader", + ]) + page = browser.new_page(viewport={"width": 900, "height": 600}) + errors = [] + page.on("pageerror", lambda error: errors.append(str(error))) + page.goto(url) + page.wait_for_function("typeof gpuOK !== 'undefined' && gpuOK && !!gpu.fbos") + page.evaluate("""() => { + window.originalDocument = document; + window.originalResources = [...gpu.texState, ...gpu.fbos, ...gpu.texStatics]; + window.resizeCalls = 0; + const original = rescaleField; + rescaleField = () => { resizeCalls++; original(); }; + }""") + for width, height in [(1200, 800), (900, 600)] * 5: + page.set_viewport_size({"width": width, "height": height}) + page.wait_for_function("([w,h]) => W === w && H === h", arg=[width, height]) + page.evaluate("""() => { + window.beforeBurst = resizeCalls; + for (let i = 0; i < 100; i++) window.dispatchEvent(new Event('resize')); + }""") + page.wait_for_function("resizeCalls > beforeBurst") + assert page.evaluate("resizeCalls === beforeBurst + 1") + assert page.evaluate("document === originalDocument") + assert page.evaluate("""originalResources.every((resource, index) => + resource === [...gpu.texState, ...gpu.fbos, ...gpu.texStatics][index])""") + assert page.evaluate("gl.getError() === gl.NO_ERROR") + assert not errors, errors + browser.close() + + +if __name__ == "__main__": + main() diff --git a/web_app.py b/web_app.py index d697660e..e65a74f7 100644 --- a/web_app.py +++ b/web_app.py @@ -95,6 +95,7 @@ def __init__(self): self._hist_sig = None self._inbox_sig = None self._cli_cursor = cli_events.load_cursor() + self._preview_lock = threading.Lock() self._preview_seq = 0 self._preview_data = None self._preview_stop = threading.Event() @@ -305,7 +306,9 @@ def get_snapshot(self): "cliEvents": self._drain_cli_events(recent_only=True), } - def poll(self): + def poll(self, known_preview_seq=None, include_preview=True): + with self._preview_lock: + seq, preview = self._preview_seq, self._preview_data out = { "status": self.status, "frac": self.frac, @@ -314,7 +317,7 @@ def poll(self): "hasJob": self.job is not None, "depthReady": self.depth_out is not None, "logs": self._pending_log[:], - "previewSeq": self._preview_seq, + "previewSeq": seq, "update": self.update, } self._pending_log = [] @@ -331,8 +334,8 @@ def poll(self): if isig != self._inbox_sig: self._inbox_sig = isig out["inbox"] = inbox - if self._preview_data is not None: - out["preview"] = self._preview_data + if include_preview and known_preview_seq != seq and preview is not None: + out["preview"] = preview if self.depth_out is not None: out["depthUrl"] = Path(self.depth_out).as_uri() if self.job is not None: @@ -351,7 +354,14 @@ def poll(self): def get_preview_frame(self): """The latest preview frame (data URL) + its sequence.""" - return {"seq": self._preview_seq, "data": self._preview_data} + with self._preview_lock: + return {"seq": self._preview_seq, "data": self._preview_data} + + def _publish_preview(self, png): + data = "data:image/png;base64," + base64.b64encode(png).decode("ascii") + with self._preview_lock: + self._preview_data = data + self._preview_seq += 1 def get_toolbox(self): """The toolbox page's payload: every registered echo_super @@ -469,10 +479,7 @@ def _depth_stills_loop(self, stills_dir, stop): if size and (newest.name, size) != seen: seen = (newest.name, size) data = newest.read_bytes() - self._preview_data = ( - "data:image/png;base64," - + base64.b64encode(data).decode()) - self._preview_seq += 1 + self._publish_preview(data) except Exception: pass stop.wait(0.8) @@ -609,10 +616,7 @@ def _preview_loop(self, job, stop): frame = Image.fromarray(img) buf = io.BytesIO() frame.save(buf, format="PNG") - self._preview_data = ( - "data:image/png;base64," - + base64.b64encode(buf.getvalue()).decode()) - self._preview_seq += 1 + self._publish_preview(buf.getvalue()) last_render, last_signature = now, signature stop.wait(0.7) except Exception: diff --git a/web_ui/app.js b/web_ui/app.js index 8a59700a..b4f80dbe 100644 --- a/web_ui/app.js +++ b/web_ui/app.js @@ -333,7 +333,7 @@ function buildField() { /* plus one render draw. No buffer is re-uploaded per frame. */ /* ------------------------------------------------------------------ */ let gl = null, gpuOK = false, gpuTried = false; -const gpu = {}; +const gpu = { scaleX: 1, scaleY: 1 }; const GPU_VS = `#version 300 es void main() { vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2)); @@ -344,6 +344,7 @@ precision highp float; uniform sampler2D uS0, uS1, uA0, uA1, uA2; uniform float uDt, uAmbT, uAmp; uniform vec3 uMouse; +uniform vec2 uResizeScale; layout(location = 0) out vec4 o0; layout(location = 1) out vec4 o1; void main() { @@ -353,6 +354,8 @@ void main() { vec4 a0 = texelFetch(uA0, ij, 0); vec4 a1 = texelFetch(uA1, ij, 0); vec4 a2 = texelFetch(uA2, ij, 0); + st0.xy *= uResizeScale; + st1.xy *= uResizeScale; float x = st0.x, y = st0.y, z = st0.z; float vx = st1.x, vy = st1.y, vz = st1.z; float driftX = sin(uAmbT * 0.045 + a1.x) * 15.0 * uAmp; @@ -500,7 +503,7 @@ function gpuEnsure() { } gpu.u = {}; for (const name of ["uS0", "uS1", "uA0", "uA1", "uA2", - "uDt", "uAmbT", "uAmp", "uMouse"]) + "uDt", "uAmbT", "uAmp", "uMouse", "uResizeScale"]) gpu.u["p_" + name] = gl.getUniformLocation(gpu.phys, name); for (const name of ["uS0", "uA1", "uA2", "uA3", "uViewport", "uAmbT", "uAmp", "uMistSize", "uCount"]) @@ -513,7 +516,7 @@ function gpuEnsure() { gpuOK = true; return true; } -function gpuPackStatics() { +function gpuPackStatics(homesOnly = false) { const n = beads.length, side = gpu.side, out = []; const packs = [ b => [b.hx, b.hy, b.hz, b.phase], @@ -526,6 +529,7 @@ function gpuPackStatics() { const arr = new Float32Array(side * side * 4); beads.forEach((b, i) => arr.set(pack(b), i * 4)); out.push(arr); + if (homesOnly) break; } return out; } @@ -533,6 +537,7 @@ function gpuUploadField() { const n = beads.length; gpu.side = Math.max(1, Math.ceil(Math.sqrt(n))); gpu.count = n; + gpu.scaleX = gpu.scaleY = 1; const side = gpu.side; for (const t of gpu.texStatics || []) gl.deleteTexture(t); for (const t of gpu.texState || []) gl.deleteTexture(t); @@ -555,15 +560,15 @@ function gpuUploadStatics() { textures only -- the live state keeps its momentum */ if (!gpuOK || !gpu.texStatics) return; const side = gpu.side; - gpuPackStatics().forEach((arr, k) => { + gpuPackStatics(true).forEach((arr, k) => { gl.bindTexture(gl.TEXTURE_2D, gpu.texStatics[k]); if (gpu.half) { const h = new Uint16Array(arr.length); for (let i = 0; i < arr.length; i++) h[i] = f32ToF16(arr[i]); - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, side, side, 0, + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, side, side, gl.RGBA, gl.HALF_FLOAT, h); } else { - gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, side, side, 0, + gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, side, side, gl.RGBA, gl.FLOAT, arr); } }); @@ -597,6 +602,8 @@ function gpuStep(dt) { gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, gpu.texState[r * 2 + 1]); gl.uniform1i(u.p_uS1, 1); + gl.uniform2f(u.p_uResizeScale, gpu.scaleX, gpu.scaleY); + gpu.scaleX = gpu.scaleY = 1; gl.uniform1f(u.p_uDt, sdt); gl.drawArrays(gl.TRIANGLES, 0, 3); gpu.read = w; @@ -653,10 +660,11 @@ function rescaleField() { b.rx *= sx; b.ry *= sy; b.x *= sx; b.y *= sy; } applyCalmZones(); - /* the GPU state restarts from the (scaled) homes on a resize; - the field re-settles in a breath -- during a window drag - this keeps the wall from smearing */ - if (gpuOK && gpu.texState) gpuUploadField(); + // Apply the resize to the live positions on the next physics step. + if (gpuOK) { + gpu.scaleX *= sx; + gpu.scaleY *= sy; + } } function calmZones() { @@ -801,7 +809,15 @@ window.addEventListener("focus", () => { pageFocused = true; wakeWall(); }); window.addEventListener("blur", () => { pageFocused = false; }); window.addEventListener("mousemove", e => { mouse = { x: e.clientX, y: e.clientY }; }); window.addEventListener("mouseout", () => { mouse = null; }); -window.addEventListener("resize", rescaleField); +let resizePending = false; +window.addEventListener("resize", () => { + if (resizePending) return; + resizePending = true; + requestAnimationFrame(() => { + resizePending = false; + rescaleField(); + }); +}); /* working state: the graffiti recedes, the scene takes the wall */ let holdTimer = null; @@ -1459,9 +1475,18 @@ async function initBridge() { setStatus(snap.status, snap.frac); bridge.check_update(); let wasBusy = false; + let polling = false; setInterval(async () => { let out; - try { out = await bridge.poll(); } catch (e) { return; } + if (polling) return; + polling = true; + try { + out = await bridge.poll(pvSeq, !document.hidden); + } catch (e) { + return; + } finally { + polling = false; + } state.busy = !!out.busy; state.depthReady = !!out.depthReady; if (out.busy && !wasBusy) setWorking(true); @@ -1486,10 +1511,9 @@ async function initBridge() { : `有新版本 v${out.update.version}`; } if (out.depthUrl) showDepthVideo(out.depthUrl); - if (out.previewSeq !== pvSeq) { + if (out.preview && out.previewSeq !== pvSeq) { pvSeq = out.previewSeq; - const fr = await bridge.get_preview_frame(); - if (fr && fr.data) drawPreview(fr.data); + drawPreview(out.preview); } }, 450); } From bee4a509b567c9f325e706b60cad1f35ff7a1d01 Mon Sep 17 00:00:00 2001 From: yang you <119789235+REDrighthand05@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:59:23 +0800 Subject: [PATCH 2/2] Stream standalone scene exports and exercise native Windows caption controls --- .github/workflows/desktop-runtime.yml | 7 ++- echo_scene/pipeline.py | 48 +++++++++++++------ tests/test_native_window.py | 67 +++++++++++++++++++++++++++ tests/test_scene_export_stream.py | 39 ++++++++++++++++ 4 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 tests/test_native_window.py create mode 100644 tests/test_scene_export_stream.py diff --git a/.github/workflows/desktop-runtime.yml b/.github/workflows/desktop-runtime.yml index e734313f..c05055ff 100644 --- a/.github/workflows/desktop-runtime.yml +++ b/.github/workflows/desktop-runtime.yml @@ -6,8 +6,11 @@ on: - 'web_ui/**' - 'web_app.py' - 'echo_scene/depth_batch.py' + - 'echo_scene/pipeline.py' + - 'tests/test_scene_export_stream.py' - 'tests/test_web_bridge.py' - 'tests/test_window_resize.py' + - 'tests/test_native_window.py' - 'tests/test_depth_dispatch.py' - '.github/workflows/desktop-runtime.yml' permissions: @@ -23,7 +26,9 @@ jobs: - uses: actions/setup-python@v5 with: python-version: '3.12' - - run: python -m pip install numpy pillow onnxruntime-directml playwright + - run: python -m pip install numpy pillow onnxruntime-directml playwright pywebview - run: python tests/test_web_bridge.py - run: python tests/test_depth_dispatch.py + - run: python tests/test_scene_export_stream.py - run: python tests/test_window_resize.py + - run: python tests/test_native_window.py diff --git a/echo_scene/pipeline.py b/echo_scene/pipeline.py index a71fc980..5a5755a9 100644 --- a/echo_scene/pipeline.py +++ b/echo_scene/pipeline.py @@ -31,9 +31,11 @@ import base64 import json import math +import re import shutil import subprocess import sys +import tempfile from pathlib import Path import numpy as np @@ -1150,6 +1152,13 @@ def stage_scale(job_dir: Path) -> dict: return info +def _write_embedded_buffer(output, path): + # Every non-final block must encode without Base64 padding. + with path.open("rb") as source: + while block := source.read(3 * 256 * 1024): + output.write(base64.b64encode(block).decode("ascii")) + + def stage_present(job_dir: Path) -> dict: """Write the standalone scene page into the job folder. @@ -1168,25 +1177,36 @@ def stage_present(job_dir: Path) -> dict: return info template = Path(__file__).resolve().parent.parent / "viewer.html" html = template.read_text(encoding="utf-8") - points_b64 = base64.b64encode(points_bin.read_bytes()).decode() - html = html.replace( - "\n\n' + script) + cursor = 0 + for match in re.finditer(r"fetch\('(mesh_vertices\.bin|mesh_indices\.bin)'\)", after): + buf = job_dir / match[1] + if buf.exists(): + output.write(after[cursor:match.start()] + "fetch('data:application/octet-stream;base64,") + _write_embedded_buffer(output, buf) + output.write("')") + cursor = match.end() + output.write(after[cursor:]) + temp_path.replace(scene_path) + finally: + if temp_path is not None: + temp_path.unlink(missing_ok=True) files = {"scene_html": str(scene_path)} for key, name in (("voxel_glb", "voxel_model.glb"), ("voxel_ply", "voxel_model.ply"), diff --git a/tests/test_native_window.py b/tests/test_native_window.py new file mode 100644 index 00000000..70fdc4e7 --- /dev/null +++ b/tests/test_native_window.py @@ -0,0 +1,67 @@ +"""Exercise the desktop caption handlers on a real Windows WebView2 window.""" +import ctypes +from ctypes import wintypes +from pathlib import Path +import sys +import threading +import time +import uuid + +import webview + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from web_app import Bridge + + +def wait_until(predicate, operation): + deadline = time.monotonic() + 10 + while not predicate(): + if time.monotonic() >= deadline: + raise AssertionError(f"timed out during {operation}") + time.sleep(0.05) + + +def main(): + user32 = ctypes.windll.user32 + user32.FindWindowW.argtypes = [wintypes.LPCWSTR, wintypes.LPCWSTR] + user32.FindWindowW.restype = wintypes.HWND + for check in (user32.IsZoomed, user32.IsIconic): + check.argtypes = [wintypes.HWND] + check.restype = wintypes.BOOL + title = "Echo window regression " + uuid.uuid4().hex + url = (Path(__file__).resolve().parents[1] / "web_ui/index.html").as_uri() + window = webview.create_window(title, url, width=800, height=600, frameless=True) + ready = threading.Event() + window.events.loaded += ready.set + bridge = Bridge() + bridge._window = window + failures = [] + + def exercise(): + try: + assert ready.wait(30), "page did not load" + hwnd = user32.FindWindowW(None, title) + assert hwnd, "native window not found" + window.evaluate_js("window.testDocument = document; true") + for _ in range(10): + bridge.win_toggle_max() + wait_until(lambda: user32.IsZoomed(hwnd), "maximize") + bridge.win_toggle_max() + wait_until(lambda: not user32.IsZoomed(hwnd), "restore size") + bridge.win_minimize() + wait_until(lambda: user32.IsIconic(hwnd), "minimize") + window.restore() + wait_until(lambda: not user32.IsIconic(hwnd), "restore from minimize") + assert window.evaluate_js("window.testDocument === document"), "page reloaded" + except Exception as exc: + failures.append(exc) + finally: + window.destroy() + + webview.start(exercise, gui="edgechromium") + if failures: + raise failures[0] + + +if __name__ == "__main__": + main() diff --git a/tests/test_scene_export_stream.py b/tests/test_scene_export_stream.py new file mode 100644 index 00000000..204d167b --- /dev/null +++ b/tests/test_scene_export_stream.py @@ -0,0 +1,39 @@ +"""Check the standalone scene payload and atomic replacement on write failure.""" +import base64 +import tempfile +from pathlib import Path +import sys +import unittest +from unittest.mock import patch + +sys.path.insert(0, str(Path(__file__).resolve().parents[1])) +from echo_scene import pipeline + + +class SceneExport(unittest.TestCase): + def test_buffers_and_failed_reexport(self): + template = (Path(pipeline.__file__).resolve().parents[1] / "viewer.html").read_text(encoding="utf-8") + with tempfile.TemporaryDirectory() as folder: + root = Path(folder) + points = bytes(range(253)) * 4001 + (root / "points.bin").write_bytes(points) + expected = template.replace("\n