summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/performance-timeline/back-forward-cache-restoration.tentative.html
blob: 733642fd03225a0877b93e8dcc09ec24e097935a (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
<!doctype html>
<html>

<head>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="/common/utils.js"></script>
  <script src="/common/dispatcher/dispatcher.js"></script>
  <script src="/html/browsers/browsing-the-web/back-forward-cache/resources/helper.sub.js"></script>
</head>

<body>
  <script>
    const BackForwardCacheRestorationName = '';
    const BackForwardCacheRestorationType = 'back-forward-cache-restoration';

    let getNumberofBackForwardCacheRestorationEntries = (BackForwardCacheRestorationType) => {
      return window.performance.getEntriesByType(BackForwardCacheRestorationType).length;
    }

    let getBackForwardCacheRestorationByType = (BackForwardCacheRestorationType) => {
      let entries = window.performance.getEntriesByType(BackForwardCacheRestorationType);
      return entries[entries.length - 1];
    }

    let getBackForwardCacheRestorationByGetAllAndFilter = (BackForwardCacheRestorationType) => {
      let entries = window.performance.getEntries().filter(e => e.entryType == BackForwardCacheRestorationType);
      return entries[entries.length - 1];
    }

    let getBackForwardCacheRestorationByPerformanceObserverBuffered = async (BackForwardCacheRestorationType) => {
      let p = new Promise(resolve => {
        new PerformanceObserver((list) => {
          const entries = list.getEntries().filter(e => e.entryType == BackForwardCacheRestorationType);
          if (entries.length > 0) {
            resolve(entries[entries.length - 1]);
          }
        }).observe({ type: BackForwardCacheRestorationType, buffered: true });
      });
      return await p;
    }

    let checkEntry = (entry, expectedNavigationId) => {
      assert_equals(entry.name, BackForwardCacheRestorationName);
      assert_equals(entry.entryType, BackForwardCacheRestorationType);
      assert_equals(entry.navigationId, expectedNavigationId);
      assert_true(entry.pageshowEventStart > entry.startTime);
      assert_true(entry.pageshowEventEnd >= entry.pageshowEventStart);
    }

    promise_test(async t => {
      const pageA = new RemoteContext(token());
      const pageB = new RemoteContext(token());

      const urlA = executorPath + pageA.context_id;
      const urlB = originCrossSite + executorPath + pageB.context_id;
      // Open url A.
      window.open(urlA, '_blank', 'noopener');
      await pageA.execute_script(waitForPageShow);

      // Assert no instance of BackForwardCacheRestoration exists without back forward cache navigatoin.
      let size = await pageA.execute_script(getNumberofBackForwardCacheRestorationEntries);
      assert_equals(0, size);

      let entry;
      for (i = 0; i < 2; i++) {
        // Navigate away to url B and back.
        await navigateAndThenBack(pageA, pageB, urlB);

        // Assert Performance Observer API supports BackForwardCacheRestoration.
        entry = await pageA.execute_script(getBackForwardCacheRestorationByPerformanceObserverBuffered, [BackForwardCacheRestorationType]);
        checkEntry(entry, i + 2); // The expected navigation id of the entry created at i-th navigating away and back is i+2 because navigation id starts from 1 and increments before an instance of BackForwardRestoration is created.
        // Assert Performance Timeline API supports BackForwardCacheRestoration.
        entry = await pageA.execute_script(getBackForwardCacheRestorationByType, [BackForwardCacheRestorationType]);
        checkEntry(entry, i + 2);

        entry = await pageA.execute_script(getBackForwardCacheRestorationByGetAllAndFilter, [BackForwardCacheRestorationType]);
        checkEntry(entry, i + 2);
      }
    }, 'Performance API for the back forward cache restoration entry.');
  </script>
</body>

</html>