summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/unregister-then-register.https.html
blob: b61608c8419fd4f362dee5330404e2743a2f05f6 (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>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var worker_url = 'resources/empty-worker.js';

promise_test(async function(t) {
    const scope = 'resources/scope/re-register-resolves-to-new-value';
    const registration = await service_worker_unregister_and_register(t, worker_url, scope);
    t.add_cleanup(() => registration.unregister());

    await wait_for_state(t, registration.installing, 'activated');
    await registration.unregister();
    const newRegistration = await navigator.serviceWorker.register(worker_url, { scope });
    t.add_cleanup(() => newRegistration.unregister());

    assert_not_equals(
      registration, newRegistration,
      'register should resolve to a new value'
    );
  }, 'Unregister then register resolves to a new value');

promise_test(async function(t) {
  const scope = 'resources/scope/re-register-while-old-registration-in-use';
  const registration = await service_worker_unregister_and_register(t, worker_url, scope);
  t.add_cleanup(() => registration.unregister());

  await wait_for_state(t, registration.installing, 'activated');
  const frame = await with_iframe(scope);
  t.add_cleanup(() => frame.remove());

  await registration.unregister();
  const newRegistration = await navigator.serviceWorker.register(worker_url, { scope });
  t.add_cleanup(() => newRegistration.unregister());

  assert_not_equals(
    registration, newRegistration,
    'Unregister and register should always create a new registration'
  );
}, 'Unregister then register does not resolve to the original value even if the registration is in use.');

promise_test(function(t) {
    var scope = 'resources/scope/re-register-does-not-affect-existing-controllee';
    var iframe;
    var registration;
    var controller;

    return service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          t.add_cleanup(function() {
            return service_worker_unregister(t, scope);
          });

          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          iframe = frame;
          controller = iframe.contentWindow.navigator.serviceWorker.controller;
          return registration.unregister();
        })
      .then(function() {
          return navigator.serviceWorker.register(worker_url, { scope: scope });
        })
      .then(function(newRegistration) {
          assert_equals(registration.installing, null,
                        'installing version is null');
          assert_equals(registration.waiting, null, 'waiting version is null');
          assert_equals(
              iframe.contentWindow.navigator.serviceWorker.controller,
              controller,
              'the worker from the first registration is the controller');
          iframe.remove();
        });
  }, 'Unregister then register does not affect existing controllee');

promise_test(async function(t) {
  const scope = 'resources/scope/resurrection';
  const altWorkerURL = worker_url + '?alt';
  const registration = await service_worker_unregister_and_register(t, worker_url, scope);
  t.add_cleanup(() => registration.unregister());

  await wait_for_state(t, registration.installing, 'activating');
  const iframe = await with_iframe(scope);
  t.add_cleanup(() => iframe.remove());

  await registration.unregister();
  const newRegistration = await navigator.serviceWorker.register(altWorkerURL, { scope });
  t.add_cleanup(() => newRegistration.unregister());

  assert_equals(newRegistration.active, null, 'Registration is new');

  await wait_for_state(t, newRegistration.installing, 'activating');

  const newIframe = await with_iframe(scope);
  t.add_cleanup(() => newIframe.remove());

  const iframeController = iframe.contentWindow.navigator.serviceWorker.controller;
  const newIframeController = newIframe.contentWindow.navigator.serviceWorker.controller;

  assert_not_equals(iframeController, newIframeController, 'iframes have different controllers');
}, 'Unregister then register does not resurrect the registration');
</script>