···11+<!DOCTYPE html>
22+<html>
33+<head><title>tessera-viz-jsoo test</title></head>
44+<body>
55+ <h1>tessera-viz-jsoo Browser Tests</h1>
66+ <p>PNG: <span id="png-result">pending...</span></p>
77+ <p>URL: <span id="url-result">pending...</span></p>
88+ <p>Status: <span id="status">running...</span></p>
99+ <p>Image: <img id="test-image" alt="test image" /></p>
1010+ <script>
1111+ var worker = new Worker('test_browser.bc.js');
1212+ worker.onmessage = function(e) {
1313+ var idx = e.data.indexOf(':');
1414+ if (idx === -1) return;
1515+ var id = e.data.substring(0, idx);
1616+ var text = e.data.substring(idx + 1);
1717+ if (id === 'img-src') {
1818+ document.getElementById('test-image').src = text;
1919+ } else {
2020+ var el = document.getElementById(id);
2121+ if (el) el.textContent = text;
2222+ }
2323+ };
2424+ worker.onerror = function(e) {
2525+ document.getElementById('status').textContent = 'FAILED: ' + e.message;
2626+ };
2727+ </script>
2828+</body>
2929+</html>
+48
tessera-viz-jsoo/test/test_browser.ml
···11+open Js_of_ocaml
22+33+let post_result id text =
44+ ignore (Js.Unsafe.global##postMessage (Js.string (id ^ ":" ^ text)))
55+66+let () =
77+ try
88+ (* Create a 4x4 test image: red/green/blue/white quadrants *)
99+ let w = 4 and h = 4 in
1010+ let data = Bigarray.Array1.create Bigarray.int8_unsigned Bigarray.c_layout (w * h * 4) in
1111+ for y = 0 to h - 1 do
1212+ for x = 0 to w - 1 do
1313+ let off = (y * w + x) * 4 in
1414+ let r, g, b =
1515+ if y < 2 && x < 2 then (255, 0, 0)
1616+ else if y < 2 then (0, 255, 0)
1717+ else if x < 2 then (0, 0, 255)
1818+ else (255, 255, 255)
1919+ in
2020+ Bigarray.Array1.set data off r;
2121+ Bigarray.Array1.set data (off + 1) g;
2222+ Bigarray.Array1.set data (off + 2) b;
2323+ Bigarray.Array1.set data (off + 3) 255
2424+ done
2525+ done;
2626+ let img = Viz.{ data; width = w; height = h } in
2727+2828+ (* Test 1: PNG encoding produces valid data *)
2929+ let png = Viz.png_of_rgba img in
3030+ post_result "png-result"
3131+ (Printf.sprintf "OK: %d bytes, magic=0x%02x%c%c%c"
3232+ (String.length png) (Char.code png.[0]) png.[1] png.[2] png.[3]);
3333+3434+ (* Test 2: data URL generation *)
3535+ let url = Viz_jsoo.to_data_url img in
3636+ let prefix = "data:image/png;base64," in
3737+ let has_prefix = String.length url > String.length prefix &&
3838+ String.sub url 0 (String.length prefix) = prefix in
3939+ post_result "url-result"
4040+ (if has_prefix then Printf.sprintf "OK: %d chars" (String.length url)
4141+ else "FAIL: bad prefix");
4242+4343+ (* Post the data URL for the page to display as an image *)
4444+ post_result "img-src" url;
4545+4646+ post_result "status" "ALL PASSED"
4747+ with exn ->
4848+ post_result "status" (Printf.sprintf "FAILED: %s" (Printexc.to_string exn))