Source code
Revision control
Copy as Markdown
Other Tools
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<script src="/tests/SimpleTest/paint_listener.js"></script>
<script src="apz_test_utils.js"></script>
<script src="apz_test_native_event_utils.js"></script>
<title>ScrollLinkedEffectDetector tests</title>
<style>
html, body { margin: 0; }
body {
height: 1000vh;
}
#target {
position: absolute;
height: 800px;
background-color: #cc00cc;
top: 0;
left: 0;
right: 0;
}
</style>
<div id="target"></div>
<script>
async function test() {
let eventTimeStamp;
// Utility function to synthesize a scroll and call the given function
// in the event listener.
async function promiseScrollAndEvent(fn) {
let scrollEventPromise = new Promise(resolve => {
window.addEventListener("scroll", () => {
fn();
resolve();
}, { once: true });
});
await scrollEventPromise;
// Wait a rAF to make sure we are outside of the micro tasks for the scroll
// event callback so that we can ensure our stack based
// ScrollLinkedEffectDetector has been scoped out from the function firing
// scroll events.
await promiseFrame();
}
let intervalId = setInterval(async () => {
await synthesizeNativeWheel(window, 50, 50, 0, -10);
}, 0);
await promiseScrollAndEvent(() => {
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(!SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"No scroll-linked effect found yet");
// Setup a scroll-linked effect callback.
await promiseScrollAndEvent(() => {
isnot(window.scrollY, 0, "we've already scrolled some amount");
target.style.top = window.scrollY + "px";
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"Scroll-linked effect found");
// A no-op again.
await promiseScrollAndEvent(() => {
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(!SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"No scroll-linked effect found");
// Setup a non-effective scroll-linked effect callback.
await promiseScrollAndEvent(() => {
target.style.top = getComputedStyle(target).top;
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(!SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"No scroll-linked effect found");
// Setup a callback to remove the style.
await promiseScrollAndEvent(() => {
target.style.top = "";
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"Scroll-linked effect found");
// Setup a no-op callback.
await promiseScrollAndEvent(() => {
eventTimeStamp = document.timeline.currentTime;
});
is(eventTimeStamp, document.timeline.currentTime,
`We are in same time frame where we got a scroll event at ${eventTimeStamp}`);
ok(!SpecialPowers.DOMWindowUtils.hasScrollLinkedEffect,
"No scroll-linked effect found this time");
clearInterval(intervalId);
}
waitUntilApzStable()
.then(test)
.then(subtestDone, subtestFailed);
</script>