1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width,minimum-scale=1">
<title>Test soft navigation UKM collection</title>
<script src="resources/util.js"></script>
</head>
<body>
<style>
#shifter {
position: relative;
width: 300px;
height: 200px;
background: blue;
}
</style>
<div id="shifter">shifter</div>
<main id=main>
<div>
<a id=link><img src="/images/lcp-256x256.png"></a>
</div>
</main>
<div id="div">content</div>
<script>
const addImage = async (element) => {
const img = new Image();
img.src = '/images/lcp-133x106.png' + "?" + Math.random();
await img.decode();
element.appendChild(img);
};
const addImageToMain = async () => {
await addImage(document.body);
};
const waitOnPaintEntriesPromise = () => {
return new Promise((resolve, reject) => {
if (!performance.softNavPaintMetricsSupported) {
// If we don't have paint entries, fall back to a timer instead.
setTimeout(resolve, 200);
}
const paint_entries = []
new PerformanceObserver(list => {
paint_entries.push(...list.getEntries());
if (paint_entries.length == 2) {
resolve();
} else if (paint_entries.length > 2) {
reject();
}
}).observe({ type: 'paint', includeSoftNavigationObservations: true });
});
};
const waitForRegularLcpEntries = () => {
return new Promise(resolve => {
new PerformanceObserver(resolve).observe(
{
type: 'largest-contentful-paint', buffered: true
});
})
}
const waitForSoftNavigationLcpEntries = () => {
return new Promise(resolve => {
new PerformanceObserver(resolve).observe(
{
type: 'largest-contentful-paint', buffered: true,
includeSoftNavigationObservations: true
});
})
}
const GetSoftNavigationLCPEntries = async () => {
const soft_nav_entry_navigation_ids = await new Promise(resolve => {
(new PerformanceObserver(list => {
if (list.getEntries().length >= 2) {
resolve(list.getEntries().map(e => e.navigationId));
}
})).observe({
type: 'soft-navigation', buffered: true
});
});
const soft_nav_lcp_entries = await new Promise(resolve => {
(new PerformanceObserver(list => {
let soft_nav_entries = list.getEntries().filter(
e => soft_nav_entry_navigation_ids.includes(e.navigationId));
if (soft_nav_entries.length >= 2) {
resolve(soft_nav_entries);
}
})).observe({
type: 'largest-contentful-paint', buffered: true,
includeSoftNavigationObservations: true
});
});
return soft_nav_lcp_entries.map(e => JSON.stringify(e.toJSON()));
}
let counter = 0;
const setEvent = (button, pushState, addContent, pushUrl, eventType) => {
const URL = "foobar.html";
button.addEventListener(eventType, async e => {
// Jump through a task, to ensure task tracking is working properly.
await new Promise(r => setTimeout(r, 0));
const url = URL + "?" + counter;
if (pushState) {
if (pushUrl) {
pushState(url);
} else {
pushState();
}
}
await new Promise(r => setTimeout(r, 10));
await addContent(url);
++counter;
});
};
const setEventAndWait = async () => {
await waitForRegularLcpEntries();
const link = document.getElementById("link");
setEvent(link, /*pushState=*/url => history.pushState({}, '', url),
/*addContent=*/async () => await addImageToMain(), /*pushUrl=*/true,
/*eventType=*/"click");
}
const waitForSoftNavigationEntry = async () => {
if (!PerformanceObserver.supportedEntryTypes.includes('soft-navigation')) {
throw new Error("Soft Navigations API not supported");
}
await new Promise(resolve => {
(new PerformanceObserver(resolve)).observe({
type: 'soft-navigation', buffered: true
});
});
await waitOnPaintEntriesPromise();;
await waitForSoftNavigationLcpEntries();
}
const waitForSoftNavigationEntry2 = async () => {
await new Promise(resolve => {
(new PerformanceObserver(() => resolve())).observe({
type: 'soft-navigation'
});
});
await waitOnPaintEntriesPromise();
await waitForSoftNavigationLcpEntries();
}
let eventPromises = [];
const registerEventListeners = () => {
eventPromises = [];
const element = document.getElementById('div');
for (const event of ['mouseup', 'pointerup', 'click']) {
eventPromises.push(new Promise(resolve => {
element.addEventListener(event, resolve, { once: true });
}));
}
}
const waitForClick = async () => {
await Promise.all(eventPromises);
}
const addChangeColorEventListener = () => {
const element = document.getElementById('div');
element.addEventListener("pointerdown", () => {
element.style = "color:red";
});
}
if (PerformanceObserver.supportedEntryTypes.indexOf("layout-shift") == -1)
throw new Error("Layout Instability API not supported");
// An list to record all the entries with startTime and score.
let element = document.querySelector("#shifter");
(async () => {
await waitUntilAfterNextLayout();
document.querySelector("#shifter").style = "top: 160px";
})();
const triggerLayoutShift = async (multiplier = 1) => {
// Wait for 50 milliseconds that the click simulation that caused the
// soft navigation would not be counted as recent input.
await new Promise(resolve => setTimeout(resolve, 500));
let element = document.querySelector("#shifter")
let val = parseInt(getComputedStyle(element).top, 10);
element.style.top = (val + 40) * multiplier + "px";
}
const GetLayoutShift = async (expected_soft_nav_count = 0) => {
let soft_nav_entry_navigation_id;
if (expected_soft_nav_count > 0) {
soft_nav_entry_navigation_id = await new Promise(resolve => {
const observer = new PerformanceObserver(list => {
let soft_nav_entries = list.getEntries();
if (soft_nav_entries.length >= expected_soft_nav_count) {
observer.disconnect();
resolve(list.getEntries()[soft_nav_entries.length - 1].navigationId);
}
});
observer.observe({
type: 'soft-navigation', buffered: true
});
});
}
return await new Promise((resolve) => {
const layout_shifts = [];
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
layout_shifts.push(entry);
}
if (expected_soft_nav_count > 0) {
let soft_nav_layout_shifts = layout_shifts.filter(
e => soft_nav_entry_navigation_id == e.navigationId);
if (soft_nav_layout_shifts.length >= 1) {
observer.disconnect();
resolve(soft_nav_layout_shifts.map((entry) => ({
startTime: entry.startTime,
score: entry.value,
hadRecentInput: entry.hadRecentInput,
})));
}
} else {
if (layout_shifts.length >= 1) {
observer.disconnect();
resolve(layout_shifts.map((entry) => ({
startTime: entry.startTime,
score: entry.value,
hadRecentInput: entry.hadRecentInput,
})));
}
}
});
observer.observe({
type: 'layout-shift', buffered: true
});
});
}
</script>
</body>
</html>
|