Skip to content

Truly independent web browser

5.3 KB
Raw Blame
<!DOCTYPE html>
<script src="include.js"></script>
<script src="layout-optimization-test-matrix.js"></script>
<script src="work-test.js"></script>
<iframe id="frame" width="300" height="240" srcdoc="<style>html, body { margin: 0 }</style>"></iframe>
<script>
    const viewportFormattingContexts = {
        block: "display: block",
        inlineBlock: "display: inline-block",
        flex: "display: flex",
        grid: "display: grid",
    };

    promiseTest(async () => {
        if (document.readyState !== "complete")
            await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
        __outputElement.style.display = "none";
        const contentDocument = frame.contentDocument;
        const contentInternals = frame.contentWindow.internals;
        const matrix = new LayoutTestMatrix("viewport resize scenarios");

        function makeElement(style = "") {
            const element = contentDocument.createElement("div");
            element.style.cssText = style;
            return element;
        }

        function makeCacheChain(format, depth, leaf) {
            let inner = leaf;
            for (let level = depth - 1; level >= 0; --level) {
                const root = makeElement(
                    `${viewportFormattingContexts[format]}; width: ${100 - level * 10}px; `
                    + `height: ${50 - level * 5}px; overflow: hidden`);
                root.id = `chain${level}`;
                root.appendChild(inner);
                inner = root;
            }
            const shell = makeElement("width: 110px; height: 60px; overflow: hidden");
            shell.id = "shell";
            shell.appendChild(inner);
            return shell;
        }

        function resizeFrame(width, height) {
            frame.width = width;
            frame.height = height;
            // Committing the parent updates the local child navigable's viewport.
            internals.updateLayoutForTesting();
            contentInternals.updateLayoutForTesting();
        }

        function viewportWork(testCase) {
            const context = testCase.format === "block" || testCase.format === "inlineBlock" ? "block" : testCase.format;
            const kind = context === "block" ? "BlockContainer" : "Box";
            const escaped = testCase.dependency === "absolute" || testCase.dependency === "fixed";
            const work = escaped
                ? "REPLAY FRAGMENTS\n    BlockContainer<DIV>#target/block RUN (cache=miss)"
                : "REUSE SUBTREE";
            return `layout FULL
  @viewport/block RUN (cache=bypass)
    BlockContainer<HTML>/block RUN (cache=miss)
      BlockContainer<DIV>#shell/block RUN (cache=miss)
        ${kind}<DIV>#chain0/${context} ${work}`;
        }

        const cases = matrixCases({
            format: Object.keys(viewportFormattingContexts),
            depth: [1, 2],
            dependency: ["vw", "vh", "absolute", "fixed"],
        });
        for (const testCase of cases) {
            const label = `viewport ${JSON.stringify(testCase)}`;
            matrix.run(label, () => {
                resizeFrame(300, 240);
                contentDocument.body.textContent = "";
                let target;
                if (testCase.dependency === "absolute" || testCase.dependency === "fixed") {
                    target = makeElement("position: fixed; right: 0; top: 0; width: 10px; height: 10px");
                    target.style.position = testCase.dependency;
                    contentDocument.body.appendChild(makeCacheChain(testCase.format, testCase.depth, target));
                } else {
                    const stable = makeElement("width: 10px; height: 10px");
                    contentDocument.body.appendChild(makeCacheChain(testCase.format, testCase.depth, stable));
                    target = testCase.dependency === "vw"
                        ? makeElement("width: 50vw; height: 10px")
                        : makeElement("width: 10px; height: 50vh");
                    contentDocument.body.appendChild(target);
                }
                target.id = "target";
                contentInternals.updateLayoutForTesting();
                const before = target.getBoundingClientRect();
                runLayoutTestStep({
                    name: label,
                    mutate: () => { resizeFrame(200, 160); },
                    expect: viewportWork(testCase),
                    check: () => {
                        const after = target.getBoundingClientRect();
                        if (testCase.dependency === "vw") {
                            matrix.expect("width before", rounded(before.width), 150);
                            matrix.expect("width after", rounded(after.width), 100);
                        } else if (testCase.dependency === "vh") {
                            matrix.expect("height before", rounded(before.height), 120);
                            matrix.expect("height after", rounded(after.height), 80);
                        } else {
                            matrix.expect("left before", rounded(before.left), 290);
                            matrix.expect("left after", rounded(after.left), 190);
                        }
                    },
                }, contentInternals);
            });
        }
        __outputElement.style.display = "";
        matrix.print();
    });
</script>