Skip to content

Truly independent web browser

3.9 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="../work-test.js"></script>
<style>
    @keyframes loading {
        0%, 40%, 90% { background-color: rgb(35, 35, 35); }
        70% { background-color: rgb(65, 65, 65); }
    }
    @keyframes finite-color {
        from { background-color: rgb(0, 0, 0); }
        to { background-color: rgb(200, 0, 0); }
    }
    .target { position: absolute; top: 200vh; width: 20px; height: 20px; }
    #infinite { animation: loading 1s linear infinite; }
    #finite { animation: finite-color 3s linear forwards; }
    #repeated { animation: finite-color 1s linear 3 forwards; }
</style>
<div id="infinite" class="target"></div>
<div id="finite" class="target"></div>
<div id="repeated" class="target"></div>
<script>
    function printCompositorAnimations(title) {
        println(title);
        for (const line of internals.dumpCompositorAnimations().split("\n").filter(Boolean))
            println(`  ${line}`);
    }

    function samples() {
        const events = parseStyleTrace(internals.takeStyleTrace()).filter(event => event.outcome?.startsWith("sampled"));
        return `[${events.map(event => event.element).join(", ")}]`;
    }

    function frameRequests() {
        return `[${internals.takeRenderingTrace().split("\n").filter(line => line.startsWith("animation frame pump request")).join(", ")}]`;
    }

    asyncTest(async done => {
        internals.updateStyle();
        const events = [];
        document.addEventListener("animationiteration", event => events.push(`iteration ${event.elapsedTime}`));
        document.addEventListener("animationend", event => events.push(`end ${event.elapsedTime}`));
        document.addEventListener("animationcancel", () => events.push("cancel"));
        const timeline = internals.createInternalAnimationTimeline();
        const animations = document.getAnimations();
        for (const animation of animations) {
            animation.timeline = timeline;
            animation.startTime = 0;
        }
        timeline.setTime(0);
        timeline.setTimeForObservation(0);
        const nextFrame = () => new Promise(requestAnimationFrame);
        for (let i = 0; i < 4; ++i)
            await nextFrame();
        internals.beginStyleTrace();
        internals.beginRenderingTrace();
        for (let i = 0; i < 4; ++i)
            await nextFrame();
        println(`finite and infinite offscreen paint animations sample: ${samples()}`);
        println(`frame requests with animation listeners: ${frameRequests()}`);
        printCompositorAnimations("offscreen paint animations on the compositor:");

        internals.beginStyleTrace();
        internals.beginRenderingTrace();
        timeline.setTimeForObservation(999);
        internals.fireCompositorAnimationWakeupForTesting(999);
        println(`early wake-up frame requests: ${frameRequests()}`);
        timeline.setTimeForObservation(1000);
        internals.fireCompositorAnimationWakeupForTesting(1000);
        timeline.setTime(1000);
        await nextFrame();
        println(`finite and infinite offscreen animations dispatch iteration: ${events.filter(event => event === "iteration 1").length === 2}`);
        println(`iteration samples: ${samples()}`);

        timeline.setTimeForObservation(3001);
        internals.fireCompositorAnimationWakeupForTesting(3001);
        timeline.setTime(3001);
        await nextFrame();
        println(`finite offscreen animations dispatch their end events: ${events.filter(event => event === "end 3").length === 2}`);
        println(`finite offscreen animation finishes: ${finite.getAnimations()[0].playState === "finished"}`);
        println(`finite offscreen animation applies its final color: ${getComputedStyle(finite).backgroundColor === "rgb(200, 0, 0)"}`);
        for (const animation of animations)
            animation.cancel();
        await nextFrame();
        println(`offscreen animation dispatches cancellation: ${events.includes("cancel")}`);
        done();
    });
</script>