summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/resources/prerender-state.html
blob: 914db2a9ed900ece42982c12913576086f7db928 (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
<!DOCTYPE html>
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="utils.js"></script>
<script>

// TODO(https://crbug.com/1174978): Make sure this page is being prerendered.

const params = new URLSearchParams(location.search);

// Take a key used for storing a test result in the server.
const key = params.get('key');

// The main test page (state-and-event.html in the parent directory) will load
// this page only with the "key" parameter. This page will then prerender
// itself with the "run-test" parameter. When "run-test" is in the URL we'll
// actually start the test process and record the results to send back to the
// main test page. We do this because the main test page cannot navigate itself
// but it also cannot open a popup to a prerendered browsing context so the
// prerender triggering and activation must both happen in this popup.
const run_test = params.has('run-test');
if (!run_test) {
  // Generate a new stash key so we can communicate with the prerendered page
  // about when to activate it.
  const activate_key = token();
  const url = new URL(document.URL);
  url.searchParams.append('run-test', '');
  url.searchParams.append('activate-key', activate_key);
  startPrerendering(url.toString());

  // Wait until the prerendered page signals us it's time to activate, then
  // navigate to it.
  nextValueFromServer(activate_key).then(() => {
    window.location = url.toString();
  });
} else {
  const activate_key = params.get('activate-key');
  const result = {
    // Check the types of the members on document.
    prerenderingTypeOf: typeof(document.prerendering),
    onprerenderingChangeTypeOf: typeof(document.onprerenderingchange),

    // Check the value of document.prerendering now and after activation.
    prerenderingValueBeforeActivate: document.prerendering,
    prerenderingValueAfterActivate: null,

    // Track when the prerenderingchange event is fired.
    onprerenderingchangeCalledBeforeActivate: false,
    onprerenderingchangeCalledAfterActivate: false,

    // Tracks the properties on the prerenderingchange event.
    eventBubbles: null,
    eventCancelable: null
  };

  let did_load = false;

  addEventListener('load', () => {
    did_load = true;

    // Tell the harness we've finished loading so we can proceed to activation.
    writeValueToServer(activate_key, 'did_load');
  });

  document.addEventListener('prerenderingchange', (e) => {
    result.eventBubbles = e.bubbles;
    result.eventCancelable = e.cancelable;

    if (did_load) {
      result.onprerenderingchangeCalledAfterActivate = true;
      result.prerenderingValueAfterActivate = document.prerendering;
      writeValueToServer(key, JSON.stringify(result)).then(() => {
        window.close();
      });
    } else {
      result.onprerenderingchangeCalledBeforeActivate = true;
    }
  });
}

</script>