summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/clients-matchall.https.html
blob: bbd9ea2327921b010e9dc6d8caca2697f0ecae8e (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
<!DOCTYPE html>
<title>Service Worker: Clients.matchAll with a prerender page</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="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
<script src="/speculation-rules/prerender/resources/utils.js"></script>
<body>
<script>

setup(() => assertSpeculationRulesIsSupported());

promise_test(async t => {
  const uid = token();
  const workerUrl = 'resources/clients-matchall-service-worker.js';
  const pageUrl = `resources/prerendered-page.html?uid=${uid}`;

  // Start a service worker.
  const registration =
      await service_worker_unregister_and_register(t, workerUrl, workerUrl);
  t.add_cleanup(_ => registration.unregister());

  // Observe the message from a prerendered page.
  const bc = new PrerenderChannel('prerender-channel', uid);
  t.add_cleanup(_ => bc.close());
  const messagePromise = new Promise(resolve => {
    bc.addEventListener('message', e => {
      resolve(e.data);
    }, {once:true});
  });

  startPrerendering(pageUrl);

  const result = await messagePromise;
  assert_equals(result, 'prerender success');

  // The service worker returns the list of client urls exposed by
  // Clients#matchAll().
  const message = await new Promise(resolve => {
    navigator.serviceWorker.onmessage = resolve;
    get_newest_worker(registration).postMessage('invoke clients.matchAll()');
  });

  assert_array_equals(
    message.data,
    [ window.location.href, new URL(pageUrl, location).toString() ]);
}, 'The client urls (including a prerender page) are exposed by ' +
   'Clients#matchAll()');

</script>
</body>