summaryrefslogtreecommitdiffstats
path: root/test/wpt/tests/service-workers/service-worker/registration-schedule-job.https.html
blob: 25d758ee8f0e26459218c7359faf7d3866b313cf (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
<!DOCTYPE html>
<meta charset="utf-8">
<meta name=timeout content=long>
<title>Service Worker: Schedule Job algorithm</title>
<script src="/resources/testharness.js"></script>
<script src="resources/testharness-helpers.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// Tests for https://w3c.github.io/ServiceWorker/#schedule-job-algorithm
// Non-equivalent register jobs should not be coalesced.
const scope = 'resources/';
const script1 = 'resources/empty.js';
const script2 = 'resources/empty.js?change';

async function cleanup() {
  const registration = await navigator.serviceWorker.getRegistration(scope);
  if (registration)
    await registration.unregister();
}

function absolute_url(url) {
  return new URL(url, self.location).toString();
}

// Test that a change to `script` starts a new register job.
promise_test(async t => {
  await cleanup();
  t.add_cleanup(cleanup);

  // Make a registration.
  const registration = await
      navigator.serviceWorker.register(script1, {scope});

  // Schedule two more register jobs.
  navigator.serviceWorker.register(script1, {scope});
  await navigator.serviceWorker.register(script2, {scope});

  // The jobs should not have been coalesced.
  const worker = get_newest_worker(registration);
  assert_equals(worker.scriptURL, absolute_url(script2));
}, 'different scriptURL');

// Test that a change to `updateViaCache` starts a new register job.
promise_test(async t => {
  await cleanup();
  t.add_cleanup(cleanup);

  // Check defaults.
  const registration = await
      navigator.serviceWorker.register(script1, {scope});
  assert_equals(registration.updateViaCache, 'imports');

  // Schedule two more register jobs.
  navigator.serviceWorker.register(script1, {scope});
  await navigator.serviceWorker.register(script1, {scope,
      updateViaCache: 'none'});

  // The jobs should not have been coalesced.
  assert_equals(registration.updateViaCache, 'none');
}, 'different updateViaCache');

// Test that a change to `type` starts a new register job.
promise_test(async t => {
  await cleanup();
  t.add_cleanup(cleanup);

  const scriptForTypeCheck = 'resources/type-check-worker.js';
  // Check defaults.
  const registration = await
      navigator.serviceWorker.register(scriptForTypeCheck, {scope});

  let worker_type = await new Promise((resolve) => {
    navigator.serviceWorker.onmessage = (event) => {
      resolve(event.data);
    };
    // The jobs should not have been coalesced. get_newest_worker() helps the
    // test fail with stable output on browers that incorrectly coalesce
    // register jobs, since then sometimes registration is not a new worker as
    // expected.
    const worker = get_newest_worker(registration);
    // The argument of postMessage doesn't matter for this case.
    worker.postMessage('');
  });

  assert_equals(worker_type, 'classic');

  // Schedule two more register jobs.
  navigator.serviceWorker.register(scriptForTypeCheck, {scope});
  await navigator.serviceWorker.register(scriptForTypeCheck, {scope, type: 'module'});

  worker_type = await new Promise((resolve) => {
    navigator.serviceWorker.onmessage = (event) => {
      resolve(event.data);
    };
    // The jobs should not have been coalesced. get_newest_worker() helps the
    // test fail with stable output on browers that incorrectly coalesce
    // register jobs, since then sometimes registration is not a new worker as
    // expected.
    const worker = get_newest_worker(registration);
    // The argument of postMessage doesn't matter for this case.
    worker.postMessage('');
  });

  assert_equals(worker_type, 'module');
}, 'different type');
</script>