summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/performance-timeline/navigation-id.helper.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /testing/web-platform/tests/performance-timeline/navigation-id.helper.js
parentInitial commit. (diff)
downloadfirefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz
firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/performance-timeline/navigation-id.helper.js')
-rw-r--r--testing/web-platform/tests/performance-timeline/navigation-id.helper.js130
1 files changed, 130 insertions, 0 deletions
diff --git a/testing/web-platform/tests/performance-timeline/navigation-id.helper.js b/testing/web-platform/tests/performance-timeline/navigation-id.helper.js
new file mode 100644
index 0000000000..53099cadb2
--- /dev/null
+++ b/testing/web-platform/tests/performance-timeline/navigation-id.helper.js
@@ -0,0 +1,130 @@
+// The test functions called in the navigation-counter test. They rely on
+// artifacts defined in
+// '/html/browsers/browsing-the-web/back-forward-cache/resources/helper.sub.js'
+// which should be included before this file to use these functions.
+
+// This function is to obtain navigation ids of all performance entries to
+// verify.
+let testInitial = () => {
+ return window.performance.getEntries().map(e => e.navigationId);
+}
+
+let testMarkMeasure = (expectedNavigationId, markName, MeasureName) => {
+ const markName1 = 'test-mark';
+ const markName2 = 'test-mark' + expectedNavigationId;
+ const measureName = 'test-measure' + expectedNavigationId;
+
+ window.performance.mark(markName1);
+ window.performance.mark(markName2);
+ window.performance.measure(measureName, markName1, markName2);
+ return window.performance.getEntriesByName(markName2).concat(
+ window.performance.getEntriesByName(measureName)).map(e => e.navigationId);
+}
+
+let testResourceTiming = async (expectedNavigationId) => {
+ let navigationId = -1;
+
+ let p = new Promise(resolve => {
+ new PerformanceObserver((list) => {
+ const entry = list.getEntries().find(e => e.name.includes('json_resource') && e.navigationId == expectedNavigationId);
+ if (entry) {
+ navigationId = entry.navigationId;
+ resolve();
+ }
+ }).observe({ type: 'resource' });
+ });
+
+ const resp = await fetch('/performance-timeline/resources/json_resource.json');
+ await p;
+ return [navigationId];
+}
+
+let testElementTiming = async (expectedNavigationId) => {
+ let navigationId = -1;
+ let p = new Promise(resolve => {
+ new PerformanceObserver((list) => {
+ const entry = list.getEntries().find(e => e.entryType === 'element' && e.identifier === 'test-element-timing' + expectedNavigationId);
+ if (entry) {
+ navigationId = entry.navigationId;
+ resolve();
+ }
+ }).observe({ type: 'element' });
+ });
+
+ let el = document.createElement('p');
+ el.setAttribute('elementtiming', 'test-element-timing' + expectedNavigationId);
+ el.textContent = 'test element timing text';
+ document.body.appendChild(el);
+ await p;
+ return [navigationId];
+}
+
+let testLongTask = async () => {
+ let navigationIds = [];
+
+ let p = new Promise(resolve => {
+ new PerformanceObserver((list) => {
+ const entry = list.getEntries().find(e => e.entryType === 'longtask')
+ if (entry) {
+ navigationIds.push(entry.navigationId);
+ navigationIds = navigationIds.concat(
+ entry.attribution.map(a => a.navigationId));
+ resolve();
+ }
+ }).observe({ type: 'longtask' });
+ });
+
+ const script = document.createElement('script');
+ script.src = '/performance-timeline/resources/make_long_task.js';
+ document.body.appendChild(script);
+ await p;
+ document.body.removeChild(script);
+ return navigationIds;
+}
+
+const testFunctionMap = {
+ 'mark_measure': testMarkMeasure,
+ 'resource_timing': testResourceTiming,
+ 'element_timing': testElementTiming,
+ 'long_task_task_attribution': testLongTask,
+};
+
+function runNavigationIdTest(params, description) {
+ const defaultParams = {
+ openFunc: url => window.open(url, '_blank', 'noopener'),
+ scripts: [],
+ funcBeforeNavigation: () => { },
+ targetOrigin: originCrossSite,
+ navigationTimes: 4,
+ funcAfterAssertion: () => { },
+ } // Apply defaults.
+ params = { ...defaultParams, ...params };
+
+ promise_test(async t => {
+ const pageA = new RemoteContext(token());
+ const pageB = new RemoteContext(token());
+
+ const urlA = executorPath + pageA.context_id;
+ const urlB = params.targetOrigin + executorPath + pageB.context_id;
+ // Open url A.
+ params.openFunc(urlA);
+ await pageA.execute_script(waitForPageShow);
+
+ // Assert navigation id is 1 when the document is loaded first time.
+ let navigationIds = await pageA.execute_script(testInitial);
+ assert_true(
+ navigationIds.every(t => t === 1), 'All Navigation Ids should be 1.');
+
+ for (i = 1; i <= params.navigationTimes; i++) {
+ // Navigate away to url B and back.
+ await navigateAndThenBack(pageA, pageB, urlB);
+
+ // Assert navigation id increments when the document is load from bfcache.
+ navigationIds = await pageA.execute_script(
+ testFunctionMap[params.testName], [i + 1]);
+ assert_true(
+ navigationIds.every(t => t === (i + 1)),
+ params.testName + ' Navigation Id should all be ' + (i + 1) + '.');
+ }
+ }, description);
+}