Skip to content

Truly independent web browser

8.6 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="../work-test.js"></script>
<style>
    @keyframes fade-in {
        from { opacity: 0; }
        to { opacity: 1; }
    }

    @keyframes slide-up {
        from { opacity: 0; transform: translateY(100px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .azure-pair {
        animation: fade-in 100s, slide-up 100s ease-in-out;
    }

    .double-fade {
        animation: fade-in 100s, fade-in 100s;
    }

    .split-winners {
        animation: slide-up 100s ease-in-out, fade-in 100s;
    }

    .finite-pair {
        opacity: 0.35;
        animation: fade-in 100s linear, slide-up 100s linear;
    }

    .infinite-pair {
        animation: fade-in 100s linear infinite, slide-up 100s linear infinite;
    }

    .finite-loser-infinite-winner {
        animation: fade-in 100s linear, slide-up 100s linear infinite;
    }

    .css-fade {
        animation: fade-in 100s;
    }

    #pseudo.enabled::before {
        content: "";
        display: block;
        width: 10px;
        height: 10px;
        animation: fade-in 100s, slide-up 100s ease-in-out;
    }
</style>
<div id="target"></div>
<div id="pseudo"></div>
<script>
    function compositorAnimationDump() {
        const lines = internals.dumpCompositorAnimations().split("\n").filter(Boolean);
        const animations = lines.filter(line => /^(opacity|background-color|filter|transform) /.test(line)).sort();
        return [...animations, ...lines.filter(line => !animations.includes(line))];
    }

    asyncTest(async done => {
        internals.updateStyle();
        const target = document.querySelector("#target");
        const nextFrames = async (count = 3) => {
            for (let i = 0; i < count; ++i)
                await new Promise(requestAnimationFrame);
        };
        const compositorAnimations = () => `[${compositorAnimationDump().filter(line => !line.endsWith("timer armed")).join(", ")}]`;
        const armedTimers = () => `[${internals.dumpCompositorAnimations().split("\n").filter(line => line.endsWith("timer armed")).join(", ")}]`;
        const renderingTraceEvents = event => {
            const lines = internals.takeRenderingTrace().split("\n").filter(line => line.startsWith(event));
            return `[${[...new Set(lines.map(line => line.replace(/ x\d+$/, "")))].join(", ")}]`;
        };
        const clearTarget = async () => {
            for (const animation of target.getAnimations())
                animation.cancel();
            target.className = "";
            target.removeAttribute("style");
            await nextFrames(2);
            internals.fireCompositorAnimationWakeupForTesting(document.timeline.currentTime);
        };

        target.className = "azure-pair";
        await nextFrames();
        internals.beginStyleTrace();
        await nextFrames(4);
        const sampled = parseStyleTrace(internals.takeStyleTrace()).filter(event => event.outcome?.startsWith("sampled"));
        println(`Azure pair publishes opacity and transform: ${compositorAnimations()}`);
        println(`Azure pair main-thread samples: [${sampled.map(event => event.element).join(", ")}]`);

        await clearTarget();
        target.className = "double-fade";
        await nextFrames();
        println(`later duplicate fade wins opacity: ${compositorAnimations()}`);

        await clearTarget();
        const loser = target.animate([{ opacity: 0.1 }, { opacity: 0.9 }], { duration: 100000 });
        const winner = target.animate([{ opacity: 0.2 }, { opacity: 0.4 }], { duration: 1000 });
        loser.currentTime = 500;
        winner.currentTime = 500;
        await nextFrames();
        const winnerOpacity = parseFloat(getComputedStyle(target).opacity);
        internals.beginRenderingTrace();
        winner.currentTime = 1001;
        await nextFrames();
        const loserOpacity = parseFloat(getComputedStyle(target).opacity);
        println(`short winner controls the boundary sample: ${Math.abs(winnerOpacity - 0.3) < 0.02}`);
        println(`loser is published when winner ends: ${loserOpacity < 0.2}, compositor animations ${compositorAnimations()}`);
        println(`winner change republishes descriptors: ${renderingTraceEvents("compositor animations updated")}`);

        await clearTarget();
        target.className = "css-fade";
        await nextFrames();
        const cssAnimationCompositorAnimations = compositorAnimations();
        const scriptWinner = target.animate([{ opacity: 0.25 }, { opacity: 0.75 }], { duration: 100000 });
        scriptWinner.currentTime = 50000;
        await nextFrames();
        println(`CSS animation on the compositor: ${cssAnimationCompositorAnimations}`);
        println(`later script animation wins over CSS animation: ${Math.abs(parseFloat(getComputedStyle(target).opacity) - 0.5) < 0.02}, compositor animations ${compositorAnimations()}`);
        scriptWinner.pause();
        await nextFrames();
        println(`paused winner is sampled on the main thread: ${Math.abs(parseFloat(getComputedStyle(target).opacity) - 0.5) < 0.02}, compositor animations ${compositorAnimations()}`);
        scriptWinner.cancel();
        await nextFrames();
        println(`cancelling winner republishes CSS animation: ${compositorAnimations()}`);

        await clearTarget();
        target.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 100000 });
        target.animate([{ opacity: 0 }, { opacity: 0.5 }], { duration: 100000, composite: "add" });
        await nextFrames();
        println(`add competitor stays on the main thread: ${compositorAnimations()}`);

        await clearTarget();
        target.className = "split-winners";
        await nextFrames();
        println(`opacity loser still hands off transform: ${compositorAnimations()}`);

        await clearTarget();
        document.querySelector("#pseudo").className = "enabled";
        await nextFrames();
        println(`pseudo-element pair publishes opacity and transform: ${compositorAnimations()}`);
        for (const animation of document.getAnimations())
            animation.cancel();

        await clearTarget();
        target.className = "finite-loser-infinite-winner";
        await nextFrames(2);
        const finiteLoser = target.getAnimations().find(animation => animation.animationName === "fade-in");
        println(`finite loser receives an end wake-up: ${armedTimers()}`);
        finiteLoser.currentTime = 100001;
        await finiteLoser.finished;
        println(`finite loser finishes after deterministic advance: ${finiteLoser.playState === "finished"}`);

        await clearTarget();
        let finiteAnimationEndEvents = 0;
        target.addEventListener("animationend", () => ++finiteAnimationEndEvents);
        target.className = "finite-pair";
        await nextFrames(2);
        const finiteAnimations = target.getAnimations();
        const losingFiniteAnimation = finiteAnimations.find(animation => animation.animationName === "fade-in");
        println(`competing finite effects register an end wake-up: ${armedTimers()}`);
        for (const animation of finiteAnimations)
            animation.currentTime = 100001;
        await losingFiniteAnimation.finished;
        await nextFrames(2);
        println(`losing finite effect finishes after deterministic advance: ${losingFiniteAnimation.playState === "finished"}`);
        println(`both competing finite effects dispatch animationend: ${finiteAnimationEndEvents === 2}`);
        println(`losing finite effect restores base opacity: ${getComputedStyle(target).opacity === "0.35"}`);

        await clearTarget();
        let losingInfiniteIterations = 0;
        const losingInfiniteIterationsObserved = new Promise(resolve => {
            target.addEventListener("animationiteration", event => {
                if (event.animationName !== "fade-in" || ++losingInfiniteIterations !== 2)
                    return;
                resolve(true);
            });
        });
        target.className = "infinite-pair";
        await nextFrames(2);
        const losingInfiniteAnimation = target.getAnimations().find(animation => animation.animationName === "fade-in");
        internals.beginRenderingTrace();
        losingInfiniteAnimation.currentTime = 150000;
        await nextFrames(2);
        losingInfiniteAnimation.currentTime = 250000;
        await losingInfiniteIterationsObserved;
        println(`losing infinite effect dispatches iteration events: ${losingInfiniteIterations === 2}`);
        println(`losing infinite listener uses iteration wake-ups: frame requests ${renderingTraceEvents("animation frame pump request")}, armed timers ${armedTimers()}`);
        for (const animation of target.getAnimations())
            animation.cancel();
        done();
    });
</script>