summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/service-workers.https.html
blob: 6a45276057d75b8504e00b214d903c98b4173755 (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
<!DOCTYPE html>
<title>Same-origin prerendering can access Indexed Database</title>
<meta name="timeout" content="long">
<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="resources/utils.js"></script>
<script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<body>
<script>

setup(() => assertSpeculationRulesIsSupported());

// This delay is to prevent a race condition which can cause false passes -
// a service worker might take some time to install, and if activation is too quick it might
// end up occuring after activation by chance.
const ACTIVATION_DELAY = 500;

promise_test(async t => {
  const reg = await service_worker_unregister_and_register(
    t, "./resources/service-worker.js", `resources/`);

  t.add_cleanup(() => reg.unregister());
  const {exec} = await create_prerendered_page(t);
  const {text, prerendering} = await exec(async () => {
    const text = await (await fetch(`ping`)).text();
    return {text, prerendering: document.prerendering};
  });

  assert_true(prerendering);
  assert_equals(text, 'pong');
}, 'A prerendered page should be able to access an existing Service Worker');

promise_test(async t => {
  const {exec, activate} = await create_prerendered_page(t);
  const scope = `./${token()}/`;
  await exec(async scope => {
    window.serviceWorkerInstalled = new Promise(resolve => {
      navigator.serviceWorker.register('./service-worker.js', {scope})
        .then(reg => {
          reg.unregister();
          resolve({prerendering: document.prerendering});
        });
    });
  }, scope);

  await new Promise(resolve => t.step_timeout(resolve, ACTIVATION_DELAY));

  await activate();

  const {prerendering} = await exec(async () => { return await window.serviceWorkerInstalled});
  assert_false(prerendering, 'Service Worker Installation should occur after activation');
}, 'Registering a new service worker from a prerendered page should be delayed');

promise_test(async t => {
  const uid = token();
  const reg = await service_worker_unregister_and_register(
    t, "./resources/service-worker.js", `./resources/${uid}/`);
  t.add_cleanup(() => reg.unregister());

  const {exec, activate} = await create_prerendered_page(t);
  await exec(async uid => {
    window.serviceWorkerUnregistered = (async () => {
      const regs = await navigator.serviceWorker.getRegistrations();
      const reg = regs.find(r => r.scope.includes(uid));
      await reg.unregister();
      return {prerendering: document.prerendering};
    })();
  }, uid);

  await new Promise(resolve => t.step_timeout(resolve, ACTIVATION_DELAY));

  await activate();

  const {prerendering} = await exec(() => window.serviceWorkerUnregistered);
  assert_false(prerendering, 'Service Worker deregistration should occur after activation');
}, 'Unregistering an exsiting service worker from a prerendered page should be delayed');

promise_test(async t => {
  const uid = token();
  const reg = await service_worker_unregister_and_register(
    t, "./resources/service-worker.js", `./resources/${uid}/`);
  t.add_cleanup(() => reg.unregister());

  const {exec, activate} = await create_prerendered_page(t);
  await exec(async uid => {
    window.serviceWorkerUpdated = (async () => {
      const regs = await navigator.serviceWorker.getRegistrations();
      const reg = regs.find(r => r.scope.includes(uid));
      await reg.update();
      return {prerendering: document.prerendering};
    })();
  }, uid);

  await new Promise(resolve => t.step_timeout(resolve, ACTIVATION_DELAY));

  await activate();

  const {prerendering} = await exec(() => window.serviceWorkerUpdated);
  assert_false(prerendering, 'Service Worker updates should occur after activation');
}, 'Updating an exsiting service worker from a prerendered page should be delayed');

promise_test(async t => {
  const reg = await service_worker_unregister_and_register(
    t, "./resources/service-worker.js", 'resources/');

  t.add_cleanup(() => reg.unregister());
  const {exec} = await create_prerendered_page(t);
  const {clientInfo} = await exec(async () => (await fetch(`client`)).json());
  assert_not_equals(clientInfo.id, null);
  assert_equals(clientInfo.visibilityState, 'hidden');
  assert_equals(clientInfo.focused, false);
}, 'A prerendered page should be accessible as a hidden & unfocused SW client');

</script>
</body>