summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/ready.https.html
blob: ee6a97ca8fc96c449734814b5c3a767c34c43a9d (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
<!DOCTYPE html>
<title>Service Worker: navigator.serviceWorker.ready</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
test(function() {
    var promise = navigator.serviceWorker.ready;
    assert_equals(promise, navigator.serviceWorker.ready,
                  'repeated access to ready without intervening ' +
                  'registrations should return the same Promise object');
  }, 'ready returns the same Promise object');

async_test(function(t) {
    with_iframe('resources/blank.html?uncontrolled')
      .then(t.step_func(function(frame) {
          var promise = frame.contentWindow.navigator.serviceWorker.ready;
          assert_equals(Object.getPrototypeOf(promise),
                        frame.contentWindow.Promise.prototype,
                        'the Promise should be in the context of the ' +
                        'related document');
          frame.remove();
          t.done();
        }));
  }, 'ready returns a Promise object in the context of the related document');

async_test(function(t) {
    var url = 'resources/empty-worker.js';
    var scope = 'resources/blank.html?ready-controlled';
    var expected_url = normalizeURL(url);
    var frame;

    service_worker_unregister_and_register(t, url, scope)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(scope); })
      .then(function(f) {
          frame = f;
          return frame.contentWindow.navigator.serviceWorker.ready;
        })
      .then(function(registration) {
          assert_equals(registration.installing, null,
                        'installing should be null');
          assert_equals(registration.waiting, null,
                        'waiting should be null');
          assert_equals(registration.active.scriptURL, expected_url,
                        'active after ready should not be null');
          assert_equals(
              frame.contentWindow.navigator.serviceWorker.controller.scriptURL,
              expected_url,
              'controlled document should have a controller');

          frame.remove();
          service_worker_unregister_and_done(t, scope);
        })
      .catch(unreached_rejection(t));
  }, 'ready on a controlled document');

async_test(function(t) {
    var url = 'resources/empty-worker.js';
    var scope = 'resources/blank.html?ready-potential-controlled';
    var expected_url = normalizeURL(url);
    var frame;

    with_iframe(scope)
      .then(function(f) {
          frame = f;
          return navigator.serviceWorker.register(url, {scope:scope});
        })
      .then(function() {
          return frame.contentWindow.navigator.serviceWorker.ready;
        })
      .then(function(registration) {
          assert_equals(registration.installing, null,
                        'installing should be null');
          assert_equals(registration.waiting, null,
                        'waiting should be null.')
          assert_equals(registration.active.scriptURL, expected_url,
                        'active after ready should not be null');
          assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
                        null,
                        'uncontrolled document should not have a controller');

          frame.remove();
          service_worker_unregister_and_done(t, scope);
        })
      .catch(unreached_rejection(t));
  }, 'ready on a potential controlled document');

async_test(function(t) {
    var url = 'resources/empty-worker.js';
    var matched_scope = 'resources/blank.html?ready-after-match';
    var longer_matched_scope = 'resources/blank.html?ready-after-match-longer';
    var frame, registration;

    Promise.all([service_worker_unregister(t, matched_scope),
                 service_worker_unregister(t, longer_matched_scope)])
      .then(function() {
          return with_iframe(longer_matched_scope);
        })
      .then(function(f) {
          frame = f;
          return navigator.serviceWorker.register(url, {scope: matched_scope});
        })
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return navigator.serviceWorker.register(
              url, {scope: longer_matched_scope});
        })
      .then(function() {
          return frame.contentWindow.navigator.serviceWorker.ready;
        })
      .then(function(r) {
          assert_equals(r.scope, normalizeURL(longer_matched_scope),
                        'longer matched registration should be returned');
          assert_equals(frame.contentWindow.navigator.serviceWorker.controller,
                        null, 'controller should be null');
          return registration.unregister();
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, longer_matched_scope);
        })
      .catch(unreached_rejection(t));
  }, 'ready after a longer matched registration registered');

async_test(function(t) {
    var url = 'resources/empty-worker.js';
    var matched_scope = 'resources/blank.html?ready-after-resolve';
    var longer_matched_scope =
        'resources/blank.html?ready-after-resolve-longer';
    var frame, registration;

    service_worker_unregister_and_register(t, url, matched_scope)
      .then(function(r) {
          registration = r;
          return wait_for_state(t, r.installing, 'activated');
        })
      .then(function() {
          return with_iframe(longer_matched_scope);
        })
      .then(function(f) {
          frame = f;
          return f.contentWindow.navigator.serviceWorker.ready;
        })
      .then(function(r) {
          assert_equals(r.scope, normalizeURL(matched_scope),
                        'matched registration should be returned');
          return navigator.serviceWorker.register(
              url, {scope: longer_matched_scope});
        })
      .then(function() {
          return frame.contentWindow.navigator.serviceWorker.ready;
        })
      .then(function(r) {
          assert_equals(r.scope, normalizeURL(matched_scope),
                        'ready should only be resolved once');
          return registration.unregister();
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, longer_matched_scope);
        })
      .catch(unreached_rejection(t));
  }, 'access ready after it has been resolved');

</script>