Truly independent web browser
<!doctype html>
<script src="include.js"></script>
<script src="work-test.js"></script>
<script>
test(() => {
const expected = `
layout FULL
@viewport/block RUN (cache=bypass)
#target/block RUN (cache=miss)
#sibling/block REUSE SUBTREE
`;
const cases = [
["template literal indentation", expected, expected, false],
["no layout work", "", "\n", false],
["unexpected layout", "", expected, true],
["missing layout", expected, "", true],
["unrelated sibling work", expected, expected.replace("REUSE SUBTREE", "RUN (cache=miss)"), true],
["fragment replay is different from subtree reuse", expected, expected.replace("REUSE SUBTREE", "REPLAY FRAGMENTS"), true],
["bypass is different from a miss", expected, expected.replace("cache=miss", "cache=bypass"), true],
["extra pass", expected, expected + expected, true],
["nested work", expected, expected.replace(" #sibling", " #sibling"), true],
["repeated run", expected, expected + " #sibling/block REUSE SUBTREE\n", true],
];
for (const [name, expected, actual, differs] of cases) {
const passed = (traceDifference(expected, actual) !== null) === differs;
println(`${passed ? "PASS" : "FAIL"}: ${name}`);
}
function exercise(name, step, expectedCalls, errorText) {
const calls = [];
const fakeInternals = {
beginLayoutTrace: () => calls.push("begin"),
updateLayoutForTesting: () => calls.push("flush"),
takeLayoutTrace: () => { calls.push("take"); return ""; },
};
let error = "";
try {
runLayoutTestStep(step(calls), fakeInternals);
} catch (caught) {
error = String(caught);
}
const passed = calls.join(",") === expectedCalls && (errorText ? error.includes(errorText) : !error);
println(`${passed ? "PASS" : "FAIL"}: ${name}`);
}
exercise("traces mutation, flush and checks", calls => ({
name: "interval", expect: "",
mutate: () => { calls.push("mutate"); },
check: equal => { calls.push("check"); equal(1, 1, "equality"); },
}), "begin,mutate,flush,check,take");
exercise("stops tracing after a throwing mutation", () => ({
name: "throw", expect: "", mutate: () => { throw new Error("mutation failed"); },
}), "begin,take", "mutation failed");
exercise("stops tracing after a failed check", () => ({
name: "check", expect: "", check: equal => equal(1, 2, "geometry"),
}), "begin,flush,take", "geometry: expected 2, got 1");
exercise("requires expectations before mutating", calls => ({
name: "missing", mutate: () => { calls.push("mutate"); },
}), "", "missing exact layout work expectation");
exercise("rejects asynchronous mutations", () => ({
name: "async", expect: "", mutate: () => Promise.resolve(),
}), "begin,take", "mutate must be synchronous");
exercise("rejects asynchronous checks", () => ({
name: "async", expect: "", check: () => Promise.resolve(),
}), "begin,flush,take", "check must be synchronous");
{
const calls = [];
const fakeInternals = {
beginLayoutTrace: () => calls.push("begin layout"),
takeLayoutTrace: () => { calls.push("take layout"); return ""; },
updateLayoutForTesting: () => calls.push("flush"),
beginDisplayListTrace: () => calls.push("begin display list"),
takeDisplayListTrace: () => { calls.push("take display list"); return "recording (overlay=true)\n"; },
recordDisplayListForTesting: (paintOverlay, cold) => calls.push(`record overlay=${paintOverlay} cold=${cold}`),
};
let error = "";
try {
runWorkStep({
name: "stages", paintOverlay: true, expect: { displayList: "", layout: "" },
mutate: () => { calls.push("mutate"); },
}, "layout", fakeInternals);
} catch (caught) {
error = String(caught);
}
const expectedCalls = "begin layout,begin display list,mutate,record overlay=true cold=false,take layout,take display list";
println(`${calls.join(",") === expectedCalls && !error ? "PASS" : "FAIL"}: traces every stage a step expects and settles the last`);
}
const trace = [
"layout FULL",
" @viewport/block RUN (cache=bypass)",
" Box<DIV>#flex/flex RUN (cache=miss)",
" BlockContainer(anonymous) MEASURE max-content inline (cache=miss) FROM ITEMS",
" BlockContainer(anonymous) MEASURE min-content inline (cache=hit)",
" BlockContainer<DIV>#column MEASURE min-content inline (cache=miss) FROM CHILDREN",
" Box<DIV>#item MEASURE max-content block (cache=miss)",
" Box<DIV>#item/flex RUN (cache=bypass)",
" Box<TABLE>#table/table RUN (cache=bypass)",
" BlockContainer<TD>.cell/block RUN (cache=miss)",
" BlockContainer<TD>.cell/block READ RESULT",
" BlockContainer<DIV> REUSE IN-FLOW SUBTREE",
"layout PARTIAL Box<DIV>#flex",
" Box<DIV>#flex/flex REPLAY FRAGMENTS",
"",
].join("\n");
const expectedEvents = [
{ depth: 0, event: "layout", partial: false },
{ depth: 1, event: "run", box: "@viewport", formattingContext: "block", action: "RUN", cache: "bypass" },
{ depth: 2, event: "run", box: "Box<DIV>#flex", formattingContext: "flex", action: "RUN", cache: "miss" },
{ depth: 3, event: "measure", box: "BlockContainer(anonymous)", size: "max-content inline", cache: "miss", from: "items" },
{ depth: 3, event: "measure", box: "BlockContainer(anonymous)", size: "min-content inline", cache: "hit" },
{ depth: 3, event: "measure", box: "BlockContainer<DIV>#column", size: "min-content inline", cache: "miss", from: "children" },
{ depth: 3, event: "measure", box: "Box<DIV>#item", size: "max-content block", cache: "miss" },
{ depth: 4, event: "run", box: "Box<DIV>#item", formattingContext: "flex", action: "RUN", cache: "bypass" },
{ depth: 3, event: "run", box: "Box<TABLE>#table", formattingContext: "table", action: "RUN", cache: "bypass" },
{ depth: 4, event: "run", box: "BlockContainer<TD>.cell", formattingContext: "block", action: "RUN", cache: "miss" },
{ depth: 4, event: "run", box: "BlockContainer<TD>.cell", formattingContext: "block", action: "READ RESULT" },
{ depth: 4, event: "reuse in-flow subtree", box: "BlockContainer<DIV>" },
{ depth: 0, event: "layout", partial: true, box: "Box<DIV>#flex" },
{ depth: 1, event: "run", box: "Box<DIV>#flex", formattingContext: "flex", action: "REPLAY FRAGMENTS" },
];
// JSON leaves out the properties a line does not have.
const parsedEvents = parseLayoutTrace(trace);
println(`${JSON.stringify(parsedEvents) === JSON.stringify(expectedEvents) ? "PASS" : "FAIL"}: parses every kind of trace line`);
try {
parseLayoutTrace(" Box<DIV>#item SOMETHING NEW");
println("FAIL: rejects unknown trace lines");
} catch (error) {
println(`${String(error).includes("unrecognized layout trace line") ? "PASS" : "FAIL"}: rejects unknown trace lines`);
}
function exerciseWork(name, mutate, expectedCalls, errorText, inspect) {
const calls = [];
const fakeInternals = {
beginLayoutTrace: () => calls.push("begin"),
updateLayoutForTesting: () => calls.push("flush"),
takeLayoutTrace: () => { calls.push("take"); return trace; },
};
let error = "";
let work;
try {
work = layoutWork(() => mutate(calls), fakeInternals);
} catch (caught) {
error = String(caught);
}
const passed = calls.join(",") === expectedCalls
&& (errorText ? error.includes(errorText) : !error)
&& (!inspect || inspect(work));
println(`${passed ? "PASS" : "FAIL"}: ${name}`);
}
exerciseWork("work lays out pending changes before tracing the mutation", calls => { calls.push("mutate"); },
"flush,begin,mutate,flush,take");
exerciseWork("work stops tracing after a throwing mutation", () => { throw new Error("mutation failed"); },
"flush,begin,take", "mutation failed");
exerciseWork("work rejects asynchronous mutations", () => Promise.resolve(),
"flush,begin,take", "mutate must be synchronous");
exerciseWork("work counts the events a filter object matches", () => {}, "flush,begin,flush,take", null,
work => work.count({ event: "measure", cache: "miss" }) === 3 && work.count({ event: "run", box: "BlockContainer<TD>.cell" }) === 2);
exerciseWork("work counts the events a filter function matches", () => {}, "flush,begin,flush,take", null,
work => work.count(event => event.depth === 0) === 2);
exerciseWork("work names each matching box once, in trace order", () => {}, "flush,begin,flush,take", null,
work => work.boxes({ event: "measure" }).join() === "BlockContainer(anonymous),BlockContainer<DIV>#column,Box<DIV>#item");
exerciseWork("work keeps the trace text", () => {}, "flush,begin,flush,take", null, work => work.text === trace);
});
</script>