Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions src/startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ beforeEach(() => {
vi.doMock("./main", () => mocks.loadMain());
});
afterEach(() => {
vi.useRealTimers();
document.body.innerHTML = "";
if (originalFonts) Object.defineProperty(document, "fonts", originalFonts);
else Reflect.deleteProperty(document, "fonts");
Expand Down Expand Up @@ -152,7 +153,7 @@ it("offers retry when the application module fails instead of spinning forever",
expect(document.getElementById("app")?.hasAttribute("inert")).toBe(true);
});

it("waits for reported contentful paint when the browser supports paint timing", async () => {
function mockPaintTiming() {
let report!: (entries: { getEntries: () => { name: string }[] }) => void;
const observe = vi.fn();
const disconnect = vi.fn();
Expand All @@ -167,14 +168,25 @@ it("waits for reported contentful paint when the browser supports paint timing",
disconnect = disconnect;
}
);
return {
observe,
disconnect,
report: (name: string) => report({ getEntries: () => [{ name }] }),
};
}

it("waits for reported contentful paint when the browser supports paint timing", async () => {
const { observe, disconnect, report } = mockPaintTiming();
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const { startup } = await import("./startup");
await vi.waitFor(() => expect(observe).toHaveBeenCalledWith({ type: "paint", buffered: true }));
expect(observe).toHaveBeenCalledWith({ type: "paint", buffered: true });
expect(mocks.loadMain).not.toHaveBeenCalled();
report({ getEntries: () => [{ name: "first-paint" }] });
report("first-paint");
await Promise.resolve();
expect(mocks.loadMain).not.toHaveBeenCalled();
report({ getEntries: () => [{ name: "first-contentful-paint" }] });
report("first-contentful-paint");
await vi.waitFor(() => expect(mocks.loadMain).toHaveBeenCalledOnce());
await vi.advanceTimersByTimeAsync(500);
expect(disconnect).toHaveBeenCalledOnce();
content.resolve();
fonts.resolve();
Expand All @@ -185,6 +197,27 @@ it("waits for reported contentful paint when the browser supports paint timing",
expect(mocks.focus).toHaveBeenCalledOnce();
});

it("starts after 500 ms when paint timing never reports contentful paint", async () => {
const { disconnect, report } = mockPaintTiming();
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
const { startup } = await import("./startup");
report("first-paint");

await vi.advanceTimersByTimeAsync(499);
expect(mocks.loadMain).not.toHaveBeenCalled();
await vi.advanceTimersByTimeAsync(1);
expect(disconnect).toHaveBeenCalledOnce();
await vi.waitFor(() => expect(mocks.loadMain).toHaveBeenCalledOnce());

content.resolve();
fonts.resolve();
await vi.waitFor(() => expect(frames).toHaveLength(1));
frame();
frame();
await startup;
expect(mocks.focus).toHaveBeenCalledOnce();
});

it("starts hidden QA without waiting for visible animation frames", async () => {
window.__KINDLING_QA_BACKGROUND__ = true;
const { startup } = await import("./startup");
Expand Down
13 changes: 11 additions & 2 deletions src/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import { isTauri } from "@tauri-apps/api/core";
import { backgroundQA, installBackgroundFrames } from "./lib/qaMode";

const PAINT_TIMEOUT_MS = 500;

installBackgroundFrames();

function paintOpportunity(): Promise<void> {
Expand All @@ -27,11 +29,18 @@ async function loadingPaint() {
await new Promise<void>((resolve) => {
const observer = new PerformanceObserver((entries) => {
if (entries.getEntries().some((entry) => entry.name === "first-contentful-paint")) {
observer.disconnect();
resolve();
finish();
}
});
observer.observe({ type: "paint", buffered: true });
// Some webviews may never deliver a contentful paint entry.
const timeout = window.setTimeout(finish, PAINT_TIMEOUT_MS);

function finish() {
window.clearTimeout(timeout);
observer.disconnect();
resolve();
}
});
} else {
// Without Paint Timing, let initial layout request its fonts,
Expand Down
Loading