Lightpanda: the headless browser designed for AI and automation
more timing fixes
Karl Seguinda414c1dde35
diff --git a/src/browser/tests/intersection_observer/basic.html b/src/browser/tests/intersection_observer/basic.htmlindex fdba924c1..0e69e1368 100644--- a/src/browser/tests/intersection_observer/basic.html+++ b/src/browser/tests/intersection_observer/basic.html@@ -2,7 +2,8 @@<script src="../testing.js"></script><div id="target" style="width: 100px; height: 100px;">Target Element</div>-<script id="basic">+<script id="basic" type=module>+ const state = await testing.async();const target = document.getElementById('target');let callbackCalled = false;let entries = null;@@ -13,8 +14,11 @@});observer.observe(target);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(true, callbackCalled);testing.expectEqual(1, entries.length);@@ -27,63 +31,52 @@testing.expectEqual('object', typeof entry.intersectionRect);testing.expectEqual('number', typeof entry.time);testing.expectEqual(true, entry.time > 0);-- observer.disconnect();});</script>-<script id=detached>- {- // never attached- let count = 0;- const div = document.createElement('div');- new IntersectionObserver((entries) => {- count += 1;- }).observe(div);-- testing.onload(() => {- testing.expectEqual(0, count);- });- }-- {- // has a parent, but never connected- let count = 0;- const div1 = document.createElement('div');- const div2 = document.createElement('div');- new IntersectionObserver((entries) => {- count += 1;- }).observe(div1);-- div2.appendChild(div1);- testing.onload(() => {- testing.expectEqual(0, count);- });- }-- {- // observed inside a detached subtree, reported once the subtree is connected- const seen = [];- const div1 = document.createElement('div');- const div2 = document.createElement('div');- div2.appendChild(div1);- new IntersectionObserver((entries) => {- for (const entry of entries) seen.push(entry.isIntersecting);- }).observe(div1);-- document.body.appendChild(div2);- testing.onload(() => {- testing.expectEqual([true], seen);- });- }+<script id=detached type=module>+ const state = await testing.async();++ // never attached+ let neverAttached = 0;+ const div = document.createElement('div');+ new IntersectionObserver((entries) => {+ neverAttached += 1;+ }).observe(div);++ // has a parent, but never connected+ let neverConnected = 0;+ const div1 = document.createElement('div');+ const div2 = document.createElement('div');+ new IntersectionObserver((entries) => {+ neverConnected += 1;+ }).observe(div1);+ div2.appendChild(div1);++ // observed inside a detached subtree, reported once the subtree is connected+ const seen = [];+ const div3 = document.createElement('div');+ const div4 = document.createElement('div');+ div4.appendChild(div3);+ new IntersectionObserver((entries) => {+ for (const entry of entries) seen.push(entry.isIntersecting);+ }).observe(div3);+ document.body.appendChild(div4);++ await testing.nextFrame();++ state.resolve();+ await state.done(() => {+ testing.expectEqual(0, neverAttached);+ testing.expectEqual(0, neverConnected);+ testing.expectEqual([true], seen);+ });</script><script id=detached_document type=module>// connected to a document that never renders, reported once adopted into// this one (WPT intersection-observer/target-in-detached-document.html)const state = await testing.async();- // WPT's waitForNotification: two frames, then a task- const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve))));const seen = [];const doc = document.implementation.createHTMLDocument('');@@ -92,10 +85,10 @@for (const entry of entries) seen.push(entry.isIntersecting);}).observe(div);- await nextFrame();+ await testing.nextFrame();const before = seen.slice();document.body.appendChild(div);- await nextFrame();+ await testing.nextFrame();state.resolve();await state.done(() => {@@ -103,3 +96,23 @@testing.expectEqual([true], seen);});</script>++<script id=delivered_in_microtask type=module>+ // The spec delivers from the next rendering update, in a task.+ const state = await testing.async();+ const target = document.getElementById('target');+ let count = 0;+ const observer = new IntersectionObserver(() => { count += 1; });+ observer.observe(target);+ await Promise.resolve();+ testing.diverges('Lightpanda delivers in a microtask, not at the next rendering update', () => {+ testing.expectEqual(1, count);+ });+ await testing.nextFrame();+ observer.disconnect();++ state.resolve();+ await state.done(() => {+ testing.expectEqual(1, count);+ });+</script>diff --git a/src/browser/tests/intersection_observer/callback_throws.html b/src/browser/tests/intersection_observer/callback_throws.htmlindex d29f71beb..5b1a69f86 100644--- a/src/browser/tests/intersection_observer/callback_throws.html+++ b/src/browser/tests/intersection_observer/callback_throws.html@@ -12,21 +12,24 @@testing.expectEqual(0, reported);</script>-<script id=callbackThrowsDOMException>+<script id=callbackThrowsDOMException type=module>{// Reporting reads the exception's message through a native accessor. The// delivered entries must survive that call.+ const state = await testing.async();let calls = 0;const io = new IntersectionObserver(() => {calls += 1;throw new DOMException('nope', 'SyntaxError');});io.observe(document.getElementById('target'));+ await testing.nextFrame();+ io.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(1, calls);testing.expectEqual(1, reported);- io.disconnect();});}</script>diff --git a/src/browser/tests/intersection_observer/disconnect.html b/src/browser/tests/intersection_observer/disconnect.htmlindex c15a7549b..0ca62a58c 100644--- a/src/browser/tests/intersection_observer/disconnect.html+++ b/src/browser/tests/intersection_observer/disconnect.html@@ -3,7 +3,8 @@<script src="../testing.js"></script><div id="target" style="width: 100px; height: 100px;">Target Element</div>-<script id="disconnect">+<script id="disconnect" type=module>+ const state = await testing.async();const target = document.getElementById('target');let callCount = 0;@@ -12,20 +13,21 @@});observer.observe(target);+ await testing.nextFrame();+ const afterFirst = callCount;- testing.onload(() => {- testing.expectEqual(1, callCount);-- observer.disconnect();+ observer.disconnect();- // Create a new observer to trigger another check- // If disconnect worked, the old observer won't fire- const observer2 = new IntersectionObserver(() => {});- observer2.observe(target);+ // Create a new observer to trigger another check+ // If disconnect worked, the old observer won't fire+ const observer2 = new IntersectionObserver(() => {});+ observer2.observe(target);+ await testing.nextFrame();+ observer2.disconnect();- testing.onload(() => {- observer2.disconnect();- testing.expectEqual(1, callCount);- });+ state.resolve();+ await state.done(() => {+ testing.expectEqual(1, afterFirst);+ testing.expectEqual(1, callCount);});</script>diff --git a/src/browser/tests/intersection_observer/disconnect_during_delivery.html b/src/browser/tests/intersection_observer/disconnect_during_delivery.htmlindex dcf5e3bb2..4426c068f 100644--- a/src/browser/tests/intersection_observer/disconnect_during_delivery.html+++ b/src/browser/tests/intersection_observer/disconnect_during_delivery.html@@ -2,10 +2,11 @@<script src="../testing.js"></script><div id="target" style="width: 100px; height: 60px;">Target Element</div>-<script id="disconnectEarlierObserver">+<script id="disconnectEarlierObserver" type=module>{// b disconnects a while the delivery round is in progress. Each observer// must still be called at most once, in registration order.+ const state = await testing.async();const target = document.getElementById('target');const calls = [];const a = new IntersectionObserver(() => calls.push('a'));@@ -14,17 +15,20 @@a.observe(target);b.observe(target);c.observe(target);+ await testing.nextFrame();+ b.disconnect();+ c.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual('a,b,c', calls.join(','));- b.disconnect();- c.disconnect();});}</script>-<script id="disconnectLaterObserver">+<script id="disconnectLaterObserver" type=module>{+ const state = await testing.async();const target = document.getElementById('target');const calls = [];let c = null;@@ -34,11 +38,13 @@a.observe(target);b.observe(target);c.observe(target);+ await testing.nextFrame();+ a.disconnect();+ b.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual('a,b', calls.join(','));- a.disconnect();- b.disconnect();});}</script>diff --git a/src/browser/tests/intersection_observer/multiple_targets.html b/src/browser/tests/intersection_observer/multiple_targets.htmlindex 52296220a..936ff6783 100644--- a/src/browser/tests/intersection_observer/multiple_targets.html+++ b/src/browser/tests/intersection_observer/multiple_targets.html@@ -4,7 +4,8 @@<div id="target1" style="width: 100px; height: 100px;">Target 1</div><div id="target2" style="width: 100px; height: 100px;">Target 2</div>-<script id="multiple">+<script id="multiple" type=module>+ const state = await testing.async();const target1 = document.getElementById('target1');const target2 = document.getElementById('target2');let entryCount = 0;@@ -19,12 +20,13 @@observer.observe(target1);observer.observe(target2);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(2, entryCount);testing.expectTrue(seenTargets.has(target1));testing.expectTrue(seenTargets.has(target2));-- observer.disconnect();});</script>diff --git a/src/browser/tests/intersection_observer/unobserve.html b/src/browser/tests/intersection_observer/unobserve.htmlindex 7998b9e7b..baadad973 100644--- a/src/browser/tests/intersection_observer/unobserve.html+++ b/src/browser/tests/intersection_observer/unobserve.html@@ -4,7 +4,8 @@<div id="target1" style="width: 100px; height: 100px;">Target 1</div><div id="target2" style="width: 100px; height: 100px;">Target 2</div>-<script id="unobserve">+<script id="unobserve" type=module>+ const state = await testing.async();const target1 = document.getElementById('target1');const target2 = document.getElementById('target2');const seenTargets = [];@@ -20,12 +21,13 @@observer.observe(target1);observer.unobserve(target1);observer.observe(target2);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {// Should only see target2, not target1testing.expectEqual(1, seenTargets.length);testing.expectEqual(target2, seenTargets[0]);-- observer.disconnect();});</script>diff --git a/src/browser/tests/resize_observer/basic.html b/src/browser/tests/resize_observer/basic.htmlindex 4d8d8a2c1..1209c2074 100644--- a/src/browser/tests/resize_observer/basic.html+++ b/src/browser/tests/resize_observer/basic.html@@ -3,8 +3,9 @@<div id="target" style="width: 100px; height: 60px;">Target Element</div><div id="hidden" style="width: 50px; display: none;">Hidden</div>-<script id="initial">+<script id="initial" type=module>{+ const state = await testing.async();const target = document.getElementById('target');let count = 0;let entries = null;@@ -14,8 +15,11 @@entries = observerEntries;});observer.observe(target);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {// observe() delivers a single initial entrytesting.expectEqual(1, count);testing.expectEqual(1, entries.length);@@ -32,23 +36,47 @@testing.expectEqual(60, entry.contentBoxSize[0].blockSize);testing.expectEqual(100, entry.borderBoxSize[0].inlineSize);testing.expectEqual(100, entry.devicePixelContentBoxSize[0].inlineSize);-- observer.disconnect();});}</script>-<script id="no_initial_for_zero_size">+<script id="no_initial_for_zero_size" type=module>{// A display:none element has no box, so observe() delivers nothing.+ const state = await testing.async();const hidden = document.getElementById('hidden');let hiddenCount = 0;const hiddenObserver = new ResizeObserver(() => { hiddenCount += 1; });hiddenObserver.observe(hidden);+ await testing.nextFrame();+ hiddenObserver.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(0, hiddenCount);- hiddenObserver.disconnect();+ });+ }+</script>++<script id="delivered_in_microtask" type=module>+ {+ // The spec delivers during the next rendering update, after its+ // animation frame callbacks.+ const state = await testing.async();+ const target = document.getElementById('target');+ let count = 0;+ const observer = new ResizeObserver(() => { count += 1; });+ observer.observe(target);+ await Promise.resolve();+ testing.diverges('Lightpanda delivers in a microtask, not at the next rendering update', () => {+ testing.expectEqual(1, count);+ });+ await testing.nextFrame();+ observer.disconnect();++ state.resolve();+ await state.done(() => {+ testing.expectEqual(1, count);});}</script>diff --git a/src/browser/tests/resize_observer/disconnect_during_delivery.html b/src/browser/tests/resize_observer/disconnect_during_delivery.htmlindex 8fdf7a696..2fc6b1f56 100644--- a/src/browser/tests/resize_observer/disconnect_during_delivery.html+++ b/src/browser/tests/resize_observer/disconnect_during_delivery.html@@ -3,10 +3,11 @@<script src="../testing.js"></script><div id="target" style="width: 100px; height: 60px;">Target Element</div>-<script id="disconnectEarlierObserver">+<script id="disconnectEarlierObserver" type=module>{// b disconnects a while the delivery round is in progress. Each observer// must still be called at most once, in registration order.+ const state = await testing.async();const target = document.getElementById('target');const calls = [];const a = new ResizeObserver(() => calls.push('a'));@@ -15,17 +16,20 @@a.observe(target);b.observe(target);c.observe(target);+ await testing.nextFrame();+ b.disconnect();+ c.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual('a,b,c', calls.join(','));- b.disconnect();- c.disconnect();});}</script>-<script id="disconnectLaterObserver">+<script id="disconnectLaterObserver" type=module>{+ const state = await testing.async();const target = document.getElementById('target');const calls = [];let c = null;@@ -35,11 +39,13 @@a.observe(target);b.observe(target);c.observe(target);+ await testing.nextFrame();+ a.disconnect();+ b.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual('a,b', calls.join(','));- a.disconnect();- b.disconnect();});}</script>diff --git a/src/browser/tests/resize_observer/unobserve.html b/src/browser/tests/resize_observer/unobserve.htmlindex 6c4155a07..368518db9 100644--- a/src/browser/tests/resize_observer/unobserve.html+++ b/src/browser/tests/resize_observer/unobserve.html@@ -1,9 +1,11 @@<!DOCTYPE html>+<meta name=lp-mode content=test><script src="../testing.js"></script><div id="anchor">anchor</div>-<script id="unobserve_before_delivery">+<script id="unobserve_before_delivery" type=module>{+ const state = await testing.async();const t = document.createElement('div');t.style.width = '100px';document.body.appendChild(t);@@ -14,16 +16,19 @@observer.unobserve(t);// unobserving a target we never observed is a no-opobserver.unobserve(document.body);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(0, count);- observer.disconnect();});}</script>-<script id="disconnect_stops_notifications">+<script id="disconnect_stops_notifications" type=module>{+ const state = await testing.async();const t1 = document.createElement('div');const t2 = document.createElement('div');t1.style.width = '100px';@@ -37,15 +42,18 @@observer.observe(t2);observer.disconnect();t1.style.width = '30px';+ await testing.nextFrame();- testing.onload(() => {+ state.resolve();+ await state.done(() => {testing.expectEqual(0, count);});}</script>-<script id="observe_twice_is_one">+<script id="observe_twice_is_one" type=module>{+ const state = await testing.async();const t = document.createElement('div');t.style.width = '100px';document.body.appendChild(t);@@ -54,11 +62,13 @@const observer = new ResizeObserver((entries) => { entryCount += entries.length; });observer.observe(t);observer.observe(t);+ await testing.nextFrame();+ observer.disconnect();- testing.onload(() => {+ state.resolve();+ await state.done(() => {// observing the same target twice still yields a single entrytesting.expectEqual(1, entryCount);- observer.disconnect();});}</script>diff --git a/src/browser/tests/testing.js b/src/browser/tests/testing.jsindex cebeafbf9..d9003f781 100644--- a/src/browser/tests/testing.js+++ b/src/browser/tests/testing.js@@ -126,6 +126,15 @@throw new Error('no error');}+ // Resolves once observer notifications queued so far have been delivered:+ // WPT's waitForNotification, two frames then a task. Page load isn't+ // ordered against them.+ function nextFrame() {+ return new Promise((resolve) => {+ requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(resolve)));+ });+ }+function onload(cb) {const script_id = _currentScriptId();if (!script_id) {@@ -337,6 +346,7 @@beginBrowserLog: beginBrowserLog,endBrowserLog: endBrowserLog,onload: onload,+ nextFrame: nextFrame,IS_TEST_RUNNER: IS_TEST_RUNNER,HOST: '127.0.0.1',ORIGIN: 'http://127.0.0.1:9582',