summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/performance-timeline/navigation-id.helper.js
blob: 1b72fe9908d573c902e8457aad24615431b5e719 (plain)
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
// 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 = (markId, markName, MeasureName) => {
  const markName1 = 'test-mark';
  const markName2 = 'test-mark' + markId;
  const measureName = 'test-measure' + markId;

  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 (resourceTimingEntryId) => {
  let navigationId;

  let p = new Promise(resolve => {
    new PerformanceObserver((list) => {
      const entry = list.getEntries().find(
        e => e.name.includes('json_resource' + resourceTimingEntryId));
      if (entry) {
        navigationId = entry.navigationId;
        resolve();
      }
    }).observe({ type: 'resource' });
  });

  const resp = await fetch(
    '/performance-timeline/resources/json_resource' + resourceTimingEntryId + '.json');
  await p;
  return [navigationId];
}

let testElementTiming = async (elementTimingEntryId) => {
  let navigationId;
  let p = new Promise(resolve => {
    new PerformanceObserver((list) => {
      const entry = list.getEntries().find(
        e => e.entryType === 'element' && e.identifier === 'test-element-timing' + elementTimingEntryId);
      if (entry) {
        navigationId = entry.navigationId;
        resolve();
      }
    }).observe({ type: 'element' });
  });

  let el = document.createElement('p');
  el.setAttribute('elementtiming', 'test-element-timing' + elementTimingEntryId);
  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 ids of all performance entries are the same.
    let navigationIds = await pageA.execute_script(testInitial);
    assert_true(
      navigationIds.every(t => t === navigationIds[0]),
      'Navigation Ids should be the same as the initial load.');

    for (i = 1; i <= params.navigationTimes; i++) {
      // Navigate away to url B and back.
      await navigateAndThenBack(pageA, pageB, urlB);

      // Assert new navigation ids are generated when the document is load from bfcache.
      let nextNavigationIds = await pageA.execute_script(
        testFunctionMap[params.testName], [i + 1]);

      // Assert navigation ids of all performance entries are the same.
      assert_true(
        nextNavigationIds.every(t => t === nextNavigationIds[0]),
        'All Navigation Ids should be same after bfcache navigation.');

      // Assert navigation ids after bfcache navigation are different from those before.
      assert_true(
        navigationIds[0] !== nextNavigationIds[0],
        params.testName +
        ' Navigation Ids should be re-generated and different from the previous ones.');

      navigationIds = nextNavigationIds;
    }
  }, description);
}