Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const storybook = withStorybook(test);
const OMEGA = (2 * Math.PI) / 0.3;
/** Its starting speed, widths per second. */
const PUSH_VELOCITY = 8.773;
/** How long a heavy push commit holds the main thread, in ms. */
const HEAVY_COMMIT_MS = 300;

interface Stack {
driver: TouchDriver;
Expand Down Expand Up @@ -1166,7 +1168,7 @@ test('a push whose commit blocks the main thread starts from its first frame', a
// check reads the time since the block with the position, because an
// engine's first frames after a long task can take far longer than 16 ms.
const {driver, width} = await openStack(page, 'settings');
await page.evaluate(() => {
await page.evaluate(blockMs => {
const observer = new MutationObserver(() => {
if (
document.querySelector('[data-navigation-screen="screen-General"]') ==
Expand All @@ -1175,14 +1177,14 @@ test('a push whose commit blocks the main thread starts from its first frame', a
return;
}
observer.disconnect();
const until = performance.now() + 300;
const until = performance.now() + blockMs;
while (performance.now() < until) {
// Hold the main thread, as a heavy commit does.
}
(window as Window & {blockEnd?: number}).blockEnd = performance.now();
});
observer.observe(document.body, {childList: true, subtree: true});
});
}, HEAVY_COMMIT_MS);
await driver.tap(await rowCenter(page, 'General'));
// The second frame after the block, and how long after the block it ran.
const {x, sinceBlock} = await page.evaluate(
Expand Down Expand Up @@ -1218,6 +1220,97 @@ test('a push whose commit blocks the main thread starts from its first frame', a
expect(Math.abs(x) / width).toBeGreaterThan(0.5 * leftAt(sinceBlock));
});

test('a push from rest after a heavy commit never skips into its curve', async ({
page,
}) => {
// The first frame that moves shows no more of the spring than the time
// since the frame before it (or since the block ended) allows, plus one
// frame, so the push never skips into its curve. A mouse click is an input
// task in every engine, as a tap is, so the test needs no touch path. The
// frames are read from the task that blocks, so the count starts there.
await openStory(page, storybook(), 'lab-navigationstack--settings');
const stackBox = await page.getByTestId('navigation-stack').boundingBox();
if (stackBox == null) {
throw new Error('no stack');
}
const width = stackBox.width;
await page.evaluate(blockMs => {
type Frame = {offset: number; t: number; isFound: boolean};
const trace = window as Window & {
blockEnd?: number;
pushFrames?: Frame[];
};
const selector = '[data-navigation-screen="screen-General"]';
const observer = new MutationObserver(() => {
if (document.querySelector(selector) == null) {
return;
}
observer.disconnect();
const until = performance.now() + blockMs;
while (performance.now() < until) {
// Hold the main thread, as a heavy commit does.
}
trace.blockEnd = performance.now();
// The pushed card's offset and the frame time, in the first six
// frames after the block.
const read: Frame[] = [];
const step = (t: number) => {
const pushed = document.querySelector(selector);
const value =
pushed == null ? 'none' : getComputedStyle(pushed).translate;
read.push({
offset: value === 'none' ? 0 : Math.abs(parseFloat(value)),
t,
isFound: pushed != null,
});
if (read.length < 6) {
requestAnimationFrame(step);
} else {
trace.pushFrames = read;
}
};
requestAnimationFrame(step);
});
observer.observe(document.body, {childList: true, subtree: true});
}, HEAVY_COMMIT_MS);
const row = await rowCenter(page, 'General');
await page.mouse.click(row.x, row.y);
await page.waitForFunction(
() =>
(window as Window & {pushFrames?: unknown[]}).pushFrames?.length === 6,
);
const {frames, blockEnd} = await page.evaluate(() => {
const trace = window as Window & {
blockEnd?: number;
pushFrames?: {offset: number; t: number; isFound: boolean}[];
};
return {frames: trace.pushFrames ?? [], blockEnd: trace.blockEnd ?? 0};
});
// The push happened: the block ran, and every frame found the pushed card.
expect(blockEnd).toBeGreaterThan(0);
expect(frames.map(frame => frame.isFound)).toEqual(Array(6).fill(true));
const travelled = frames.map(frame => 1 - frame.offset / width);
const firstMoving = travelled.findIndex(share => share > 0.005);
expect(firstMoving).toBeGreaterThanOrEqual(0);
// The push starts on the first frame after the task. Chromium can run the
// first two frames after a long task on one frame time, so the card can
// still read at rest on the second read.
expect(firstMoving).toBeLessThanOrEqual(2);
// The spring's share after `ms` from rest (OMEGA, starting at
// PUSH_VELOCITY), and the most the first moving frame may show: the time
// since the frame before it (or since the block ended), plus one frame.
const shareAt = (ms: number) => {
const s = ms / 1000;
return 1 - (1 + (OMEGA - PUSH_VELOCITY) * s) * Math.exp(-OMEGA * s);
};
const sincePrevious =
frames[firstMoving].t -
(firstMoving > 0 ? frames[firstMoving - 1].t : blockEnd);
expect(travelled[firstMoving]).toBeLessThanOrEqual(
shareAt(sincePrevious + 17) + 0.02,
);
});

test('a push keeps advancing every frame while the main thread is blocked', async ({
page,
browserName,
Expand Down
Loading