Skip to content

Truly independent web browser

1.9 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    function nextTask() {
        const { promise, resolve } = Promise.withResolvers();
        const channel = new MessageChannel();
        channel.port1.onmessage = resolve;
        channel.port2.postMessage(null);
        return promise;
    }

    function printRenderingSchedule(title, trace) {
        println(title);
        // A request made while rendering comes from the rendering work itself, never from the test, and the work an
        // opportunity starts can make one before the trace is taken.
        for (const line of trace.split("\n")) {
            if (/^(request(?! while rendering)|opportunity|update$)/.test(line))
                println(`  ${line}`);
        }
    }

    asyncTest(async done => {
        internals.setManualRenderingOpportunities(true);
        await nextTask();
        await setSystemVisibilityState("hidden");
        internals.beginRenderingTrace();

        let callbackCount = 0;
        requestAnimationFrame(() => ++callbackCount);
        internals.injectRenderingOpportunity(performance.now());
        await nextTask();
        const whileHidden = internals.takeRenderingTrace();
        const callbackCountWhileHidden = callbackCount;

        await setSystemVisibilityState("visible");
        internals.beginRenderingTrace();
        internals.injectRenderingOpportunity(performance.now());
        await nextTask();
        const afterVisibleOpportunity = internals.takeRenderingTrace();
        internals.setManualRenderingOpportunities(false);

        println(`callback count while hidden: ${callbackCountWhileHidden}`);
        printRenderingSchedule("rendering while hidden:", whileHidden);
        println(`callback count after visible opportunity: ${callbackCount}`);
        printRenderingSchedule("rendering after the visible opportunity:", afterVisibleOpportunity);
        done();
    });
</script>