summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/update-registration-with-type.https.html
blob: 269e61b390bdb1b96c151e214f8a58109a7db3b4 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<meta charset="utf-8">
<title>Service Worker: Update the registration with a different script type.</title>
<!-- common.js is for guid() -->
<script src="/common/security-features/resources/common.sub.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
// The following two tests check that a registration is updated correctly
// with different script type. At first Service Worker is registered as
// classic script type, then it is re-registered as module script type,
// and vice versa. A main script is also updated at the same time.
promise_test(async t => {
  const key = guid();
  const script = `resources/update-registration-with-type.py?classic_first=1&key=${key}`;
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with classic script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  });
  const firstWorker = firstRegistration.installing;
  await wait_for_state(t, firstWorker, 'activated');
  firstWorker.postMessage(' ');
  let msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r);
  assert_equals(msgEvent.data, 'A classic script.');

  // Re-register with module script type.
  const secondRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  const secondWorker = secondRegistration.installing;
  secondWorker.postMessage(' ');
  msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r);
  assert_equals(msgEvent.data, 'A module script.');

  assert_not_equals(firstWorker, secondWorker);
  assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (classic => module).');

promise_test(async t => {
  const key = guid();
  const script = `resources/update-registration-with-type.py?classic_first=0&key=${key}`;
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with module script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  const firstWorker = firstRegistration.installing;
  await wait_for_state(t, firstWorker, 'activated');
  firstWorker.postMessage(' ');
  let msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r);
  assert_equals(msgEvent.data, 'A module script.');

  // Re-register with classic script type.
  const secondRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  });
  const secondWorker = secondRegistration.installing;
  secondWorker.postMessage(' ');
  msgEvent = await new Promise(r => navigator.serviceWorker.onmessage = r);
  assert_equals(msgEvent.data, 'A classic script.');

  assert_not_equals(firstWorker, secondWorker);
  assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (module => classic).');

// The following two tests change the script type while keeping
// the script identical.
promise_test(async t => {
  const script = 'resources/empty-worker.js';
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with classic script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  });
  const firstWorker = firstRegistration.installing;
  await wait_for_state(t, firstWorker, 'activated');

  // Re-register with module script type.
  const secondRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  const secondWorker = secondRegistration.installing;

  assert_not_equals(firstWorker, secondWorker);
  assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (classic => module) '
    + 'and with a same main script.');

promise_test(async t => {
  const script = 'resources/empty-worker.js';
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with module script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  const firstWorker = firstRegistration.installing;
  await wait_for_state(t, firstWorker, 'activated');

  // Re-register with classic script type.
  const secondRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  });
  const secondWorker = secondRegistration.installing;

  assert_not_equals(firstWorker, secondWorker);
  assert_equals(firstRegistration, secondRegistration);
}, 'Update the registration with a different script type (module => classic) '
    + 'and with a same main script.');

// This test checks that a registration is not updated with the same script
// type and the same main script.
promise_test(async t => {
  const script = 'resources/empty-worker.js';
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with module script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  await wait_for_state(t, firstRegistration.installing, 'activated');

  // Re-register with module script type.
  const secondRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  assert_equals(secondRegistration.installing, null);

  assert_equals(firstRegistration, secondRegistration);
}, 'Does not update the registration with the same script type and '
    + 'the same main script.');

// In the case (classic => module), a worker script contains importScripts()
// that is disallowed on module scripts, so the second registration is
// expected to fail script evaluation.
promise_test(async t => {
  const script = 'resources/classic-worker.js';
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with classic script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  });
  assert_not_equals(firstRegistration.installing, null);
  await wait_for_state(t, firstRegistration.installing, 'activated');

  // Re-register with module script type and expect TypeError.
  return promise_rejects_js(t, TypeError, navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  }), 'Registering with invalid evaluation should be failed.');
}, 'Update the registration with a different script type (classic => module) '
    + 'and with a same main script. Expect evaluation failed.');

// In the case (module => classic), a worker script contains static-import
// that is disallowed on classic scripts, so the second registration is
// expected to fail script evaluation.
promise_test(async t => {
  const script = 'resources/module-worker.js';
  const scope = 'resources/update-registration-with-type';
  await service_worker_unregister(t, scope);
  t.add_cleanup(() => service_worker_unregister(t, scope));

  // Register with module script type.
  const firstRegistration = await navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'module'
  });
  assert_not_equals(firstRegistration.installing, null);
  await wait_for_state(t, firstRegistration.installing, 'activated');

  // Re-register with classic script type and expect TypeError.
  return promise_rejects_js(t, TypeError, navigator.serviceWorker.register(script, {
    scope: scope,
    type: 'classic'
  }), 'Registering with invalid evaluation should be failed.');
}, 'Update the registration with a different script type (module => classic) '
    + 'and with a same main script. Expect evaluation failed.');
</script>
</body>