Skip to content

Truly independent web browser

4.0 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<style>
    @keyframes finite-opacity {
        from { opacity: 0.2; }
        to { opacity: 0.8; }
    }

    @keyframes finite-transform {
        from { transform: translateX(0px); }
        to { transform: translateX(100px); }
    }

    #opacity {
        opacity: 0.3;
        animation: finite-opacity 300ms linear forwards;
    }

    #transform {
        transform: translateX(7px);
        animation: finite-transform 300ms linear none;
    }

    #opacity-without-fill {
        opacity: 0.3;
        animation: finite-opacity 300ms linear none;
    }
</style>
<div id="opacity"></div>
<div id="transform"></div>
<div id="opacity-without-fill"></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))];
    }

    function animationUpdatesOutsideRenderingUpdates(trace) {
        const outside = [];
        let updatesRun = 0;
        for (const line of trace.split("\n")) {
            const [, event, times] = line.match(/^(.*?)(?: x(\d+))?$/);
            if (event === "update")
                updatesRun += Number(times ?? 1);
            if (event === "animation timeline updated its animations") {
                updatesRun -= Number(times ?? 1);
                if (updatesRun < 0)
                    outside.push(line);
                updatesRun = 0;
            }
        }
        return outside;
    }

    asyncTest(async done => {
        let animationEndEvents = 0;
        let eventHandlerSawTerminalStyle = false;
        let traceUntilEndEvent = "";
        opacity.addEventListener("animationend", () => {
            ++animationEndEvents;
            traceUntilEndEvent = internals.takeRenderingTrace();
            internals.beginRenderingTrace();
            eventHandlerSawTerminalStyle = getComputedStyle(opacity).opacity === "0.8";
        });
        transform.addEventListener("animationend", () => ++animationEndEvents);

        await new Promise(requestAnimationFrame);
        await new Promise(requestAnimationFrame);
        const opacityWithoutFill = document.querySelector("#opacity-without-fill");
        const animations = [...opacity.getAnimations(), ...transform.getAnimations(), ...opacityWithoutFill.getAnimations()];
        println("finite effects use the compositor:");
        for (const line of compositorAnimationDump())
            println(`  ${line}`);

        internals.beginRenderingTrace();
        let finishedPromises = 0;
        await Promise.all(animations.map(animation => animation.finished.then(() => ++finishedPromises)));
        await new Promise(requestAnimationFrame);
        const trace = traceUntilEndEvent + internals.takeRenderingTrace();

        println(`finished promises resolve: ${finishedPromises === 3}`);
        println(`animationend events fire: ${animationEndEvents === 2}`);
        println(`animationend sees terminal style: ${eventHandlerSawTerminalStyle}`);
        println(`finite effects request one end frame: [${trace.split("\n").filter(line => line.startsWith("animation frame pump request")).join(", ")}]`);
        println(`animation updates outside rendering updates before the end event: [${animationUpdatesOutsideRenderingUpdates(traceUntilEndEvent).join(", ")}]`);
        println(`forwards fill retains opacity: ${getComputedStyle(opacity).opacity === "0.8"}`);
        println(`no fill restores transform: ${getComputedStyle(transform).transform === "matrix(1, 0, 0, 1, 7, 0)"}`);
        println(`no fill restores opacity: ${getComputedStyle(opacityWithoutFill).opacity === "0.3"}`);
        await Promise.resolve();
        println(`terminal values remain stable: ${getComputedStyle(opacity).opacity === "0.8" && getComputedStyle(transform).transform === "matrix(1, 0, 0, 1, 7, 0)" && getComputedStyle(opacityWithoutFill).opacity === "0.3"}`);
        done();
    });
</script>