Skip to content

Truly independent web browser

8.5 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="../work-test.js"></script>
<style>
    @keyframes label-sweep {
        from { background-position: calc(-1 * var(--offset)) 50%, 0 50%; }
        to { background-position: calc(100px - var(--offset)) 50%, 0 50%; }
    }
    body { margin: 0; }
    .target { position: absolute; top: 200vh; width: 100px; height: 100px; border-top: 1px solid; }
</style>
<script>
    asyncTest(async done => {
        internals.updateStyle();
        const nextFrame = () => new Promise(requestAnimationFrame);
        async function settle() {
            for (let i = 0; i < 4; ++i)
                await nextFrame();
        }
        async function traceOverFrames() {
            internals.beginStyleTrace();
            internals.beginRenderingTrace();
            await settle();
            const sampled = parseStyleTrace(internals.takeStyleTrace()).filter(event => event.outcome?.startsWith("sampled"));
            const pumpRequests = internals.takeRenderingTrace().split("\n").filter(line => line.startsWith("animation frame pump request"));
            return {
                sampled: `[${[...new Set(sampled.map(event => event.element))].join(", ")}]`,
                pumpRequests: `[${[...new Set(pumpRequests.map(line => line.replace(/ x\d+$/, "")))].join(", ")}]`,
            };
        }
        const timeline = internals.createInternalAnimationTimeline();
        timeline.setTime(0);
        timeline.setTimeForObservation(0);
        const target = document.createElement("div");
        target.className = "target";
        document.body.append(target);
        const animation = new Animation(new KeyframeEffect(target, [
            { borderTopColor: "rgb(0, 0, 0)", offset: 0 },
            { borderTopColor: "rgb(200, 0, 0)", offset: 0.8 },
        ], { duration: 1000, iterations: Infinity }), timeline);
        animation.play();
        animation.startTime = 0;
        await settle();
        let trace = await traceOverFrames();
        println(`offscreen background samples: ${trace.sampled}`);
        println(`offscreen background frame requests: ${trace.pumpRequests}`);

        timeline.setTimeForObservation(400);
        println(`computed style samples the current color: ${getComputedStyle(target).borderTopColor === "rgb(100, 0, 0)"}`);
        timeline.setTime(600);
        timeline.setTimeForObservation(600);
        target.scrollIntoView();
        await settle();
        println(`scrolling into view samples the current color: ${getComputedStyle(target).borderTopColor === "rgb(150, 0, 0)"}`);
        trace = await traceOverFrames();
        println(`visible background frame requests: ${trace.pumpRequests}`);

        scrollTo(0, 0);
        await settle();
        trace = await traceOverFrames();
        println(`frame requests after scrolling out of view: ${trace.pumpRequests}`);

        const scroller = document.createElement("div");
        scroller.style.cssText = "width: 100px; height: 20px; overflow: auto";
        target.replaceWith(scroller);
        scroller.append(target);
        target.style.cssText = "position: relative; top: 40px";
        await settle();
        trace = await traceOverFrames();
        println(`paint outside a nested scrollport frame requests: ${trace.pumpRequests}`);
        scroller.scrollTop = 40;
        await settle();
        trace = await traceOverFrames();
        println(`frame requests after scrolling a nested scrollport to the paint: ${trace.pumpRequests}`);
        scroller.replaceWith(target);
        target.style.cssText = "";

        const child = document.createElement("div");
        child.style.cssText = "position: fixed; top: 0; width: 20px; height: 20px; border-top: 1px solid; border-top-color: inherit";
        target.append(child);
        await settle();
        trace = await traceOverFrames();
        println(`visible fixed descendant inheriting the paint frame requests: ${trace.pumpRequests}`);
        child.remove();

        const wrapper = document.createElement("div");
        target.replaceWith(wrapper);
        wrapper.append(target);
        const filterAnimation = new Animation(new KeyframeEffect(wrapper, [
            { filter: "none" },
            { filter: "blur(100px)" },
        ], { duration: 1000, iterations: Infinity }), timeline);
        filterAnimation.play();
        filterAnimation.startTime = 600;
        await settle();
        trace = await traceOverFrames();
        println(`ancestor filter animation before it expands frame requests: ${trace.pumpRequests}`);
        filterAnimation.cancel();
        wrapper.replaceWith(target);

        animation.effect.setKeyframes([
            { backgroundColor: "rgb(0, 0, 0)", width: "100px" },
            { backgroundColor: "rgb(200, 0, 0)", width: "200px" },
        ]);
        await settle();
        trace = await traceOverFrames();
        println(`mixed layout and paint animation frame requests: ${trace.pumpRequests}`);
        animation.cancel();
        target.remove();

        const clip = document.createElement("div");
        clip.className = "target";
        clip.style.overflow = "hidden";
        const shadow = document.createElement("div");
        shadow.style.cssText = "width: 20px; height: 20px";
        clip.append(shadow);
        document.body.append(clip);
        const shadowAnimation = new Animation(new KeyframeEffect(shadow, [
            { boxShadow: "0 0 0 0 red" },
            { boxShadow: "0 -200vh 0 0 blue" },
        ], { duration: 1000, iterations: Infinity }), timeline);
        shadowAnimation.play();
        shadowAnimation.startTime = 0;
        await settle();
        trace = await traceOverFrames();
        println(`offscreen clipped shadow frame requests: ${trace.pumpRequests}`);
        clip.style.cssText += "; overflow: clip; overflow-clip-margin: 300vh";
        await settle();
        trace = await traceOverFrames();
        println(`potentially visible shadow in an expanded overflow clip frame requests: ${trace.pumpRequests}`);
        clip.style.cssText += "; overflow-clip-margin: 0px; overflow-x: clip; overflow-y: visible";
        await settle();
        trace = await traceOverFrames();
        println(`vertical shadow overflow past a horizontal clip frame requests: ${trace.pumpRequests}`);
        clip.style.overflow = "visible";
        await settle();
        trace = await traceOverFrames();
        println(`unclipped shadow that can reach the viewport frame requests: ${trace.pumpRequests}`);
        shadowAnimation.cancel();
        clip.style.overflow = "hidden";
        const label = document.createElement("div");
        label.textContent = "Animated label";
        shadow.replaceWith(label);
        label.style.cssText = "--offset: 10px; animation: label-sweep 1s linear infinite";
        const labelAnimation = label.getAnimations()[0];
        labelAnimation.timeline = timeline;
        labelAnimation.startTime = 0;
        await settle();
        trace = await traceOverFrames();
        println(`offscreen shorthand with variable substitution frame requests: ${trace.pumpRequests}`);
        println(`offscreen shorthand with variable substitution samples: ${trace.sampled}`);
        clip.scrollIntoView();
        await settle();
        trace = await traceOverFrames();
        println(`visible shorthand with variable substitution frame requests: ${trace.pumpRequests}`);
        labelAnimation.cancel();
        scrollTo(0, 0);
        label.style.cssText = "transform: translateY(10px)";
        label.innerHTML = '<svg width="20" height="20"><path d="M0 0L20 20" stroke="black" /></svg> Animated label';
        const fadeAnimation = new Animation(new KeyframeEffect(label, [
            { opacity: 0, filter: "blur(2px)" },
            { opacity: 1, filter: "blur(0px)" },
        ], { duration: 1000, fill: "both" }), timeline);
        fadeAnimation.play();
        fadeAnimation.startTime = 0;
        await settle();
        trace = await traceOverFrames();
        println(`offscreen clipped fade and blur frame requests: ${trace.pumpRequests}`);
        println(`offscreen clipped fade and blur samples: ${trace.sampled}`);
        timeline.setTimeForObservation(1001);
        internals.fireCompositorAnimationWakeupForTesting(1001);
        timeline.setTime(1001);
        await fadeAnimation.finished;
        println(`offscreen clipped fade and blur finishes: ${fadeAnimation.playState === "finished"}`);
        println(`offscreen clipped blur applies its final value: ${getComputedStyle(label).filter === "blur(0px)"}`);
        fadeAnimation.cancel();
        clip.remove();
        done();
    });
</script>