summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/unregister-then-register-new-script.https.html
blob: 385430c2d8e6c875a1dbbc3ff14c80500f049a39 (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
<!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';

async_test(function(t) {
    var scope = 'resources/scope/unregister-then-register-new-script-that-exists';
    var new_worker_url = worker_url + '?new';
    var iframe;
    var registration;
    var new_registration;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          iframe = frame;
          return registration.unregister();
        })
      .then(function() {
          return navigator.serviceWorker.register(new_worker_url,
                                                  { scope: scope });
        })
      .then(function(r) {
          new_registration = r;
          assert_equals(registration.installing.scriptURL,
                        normalizeURL(new_worker_url),
                        'before activated registration.installing');
          assert_equals(registration.waiting, null,
                        'before activated registration.waiting');
          assert_equals(registration.active.scriptURL, normalizeURL(worker_url),
                        'before activated registration.active');
          assert_equals(new_registration.installing.scriptURL,
                        normalizeURL(new_worker_url),
                        'before activated new_registration.installing');
          assert_equals(new_registration.waiting, null,
                        'before activated new_registration.waiting');
          assert_equals(new_registration.active.scriptURL,
                        normalizeURL(worker_url),
                        'before activated new_registration.active');
          iframe.remove();
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() {
          assert_equals(new_registration.installing, null,
                        'after activated new_registration.installing');
          assert_equals(new_registration.waiting, null,
                        'after activated new_registration.waiting');
          assert_equals(new_registration.active.scriptURL,
                        normalizeURL(new_worker_url),
                        'after activated new_registration.active');
          return with_iframe(scope);
        })
      .then(function(frame) {
          assert_equals(
              frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
              normalizeURL(new_worker_url),
              'the new worker should control a new document');
          frame.remove();
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
}, 'Registering a new script URL while an unregistered registration is in use');

async_test(function(t) {
    var scope = 'resources/scope/unregister-then-register-new-script-that-404s';
    var iframe;
    var registration;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          iframe = frame;
          return registration.unregister();
        })
      .then(function() {
          var promise = navigator.serviceWorker.register('this-will-404',
                                                         { scope: scope });
          iframe.remove();
          return promise;
        })
      .then(
        function() {
          assert_unreached('register should reject the promise');
        },
        function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
                        null,
                        'document should not load with a controller');
          frame.remove();
          t.done();
        })
      .catch(unreached_rejection(t));
}, 'Registering a new script URL that 404s does not resurrect an ' +
       'unregistered registration');

async_test(function(t) {
    var scope = 'resources/scope/unregister-then-register-reject-install-worker';
    var iframe;
    var registration;

    service_worker_unregister_and_register(t, worker_url, scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          iframe = frame;
          return registration.unregister();
        })
      .then(function() {
          var promise = navigator.serviceWorker.register(
              'resources/reject-install-worker.js', { scope: scope });
          iframe.remove();
          return promise;
        })
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'redundant');
        })
      .then(function() {
          return with_iframe(scope);
        })
      .then(function(frame) {
          assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
                        null,
                        'document should not load with a controller');
          frame.remove();
          return registration.unregister();
        })
      .then(function() {
          t.done();
        })
      .catch(unreached_rejection(t));
  }, 'Registering a new script URL that fails to install does not resurrect ' +
       'an unregistered registration');
</script>