2 <title>Service Worker: navigator.serviceWorker.ready
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharnessreport.js"></script>
5 <script src=
"resources/test-helpers.js"></script>
9 var promise
= navigator
.serviceWorker
.ready
;
10 assert_equals(promise
, navigator
.serviceWorker
.ready
,
11 'repeated access to ready without intervening ' +
12 'registrations should return the same Promise object');
13 }, 'ready returns the same Promise object');
15 async_test(function(t
) {
16 with_iframe('resources/blank.html?uncontrolled')
17 .then(t
.step_func(function(frame
) {
18 var promise
= frame
.contentWindow
.navigator
.serviceWorker
.ready
;
19 assert_equals(Object
.getPrototypeOf(promise
),
20 frame
.contentWindow
.Promise
.prototype,
21 'the Promise should be in the context of the ' +
26 }, 'ready returns a Promise object in the context of the related document');
28 async_test(function(t
) {
29 var url
= 'resources/empty-worker.js';
30 var scope
= 'resources/blank.html?ready-controlled';
31 var expected_url
= normalizeURL(url
);
34 service_worker_unregister_and_register(t
, url
, scope
)
35 .then(function(registration
) {
36 return wait_for_state(t
, registration
.installing
, 'activated');
38 .then(function() { return with_iframe(scope
); })
41 return frame
.contentWindow
.navigator
.serviceWorker
.ready
;
43 .then(function(registration
) {
44 assert_equals(registration
.installing
, null,
45 'installing should be null');
46 assert_equals(registration
.waiting
, null,
47 'waiting should be null');
48 assert_equals(registration
.active
.scriptURL
, expected_url
,
49 'active after ready should not be null');
51 frame
.contentWindow
.navigator
.serviceWorker
.controller
.scriptURL
,
53 'controlled document should have a controller');
56 service_worker_unregister_and_done(t
, scope
);
58 .catch(unreached_rejection(t
));
59 }, 'ready on a controlled document');
61 async_test(function(t
) {
62 var url
= 'resources/empty-worker.js';
63 var scope
= 'resources/blank.html?ready-potential-controlled';
64 var expected_url
= normalizeURL(url
);
70 return navigator
.serviceWorker
.register(url
, {scope
:scope
});
73 return frame
.contentWindow
.navigator
.serviceWorker
.ready
;
75 .then(function(registration
) {
76 assert_equals(registration
.installing
, null,
77 'installing should be null');
78 assert_equals(registration
.waiting
, null,
79 'waiting should be null.')
80 assert_equals(registration
.active
.scriptURL
, expected_url
,
81 'active after ready should not be null');
82 assert_equals(frame
.contentWindow
.navigator
.serviceWorker
.controller
,
84 'uncontrolled document should not have a controller');
87 service_worker_unregister_and_done(t
, scope
);
89 .catch(unreached_rejection(t
));
90 }, 'ready on a potential controlled document');
92 async_test(function(t
) {
93 var url
= 'resources/empty-worker.js';
94 var matched_scope
= 'resources/blank.html?ready-after-match';
95 var longer_matched_scope
= 'resources/blank.html?ready-after-match-longer';
96 var frame
, registration
;
98 Promise
.all([service_worker_unregister(t
, matched_scope
),
99 service_worker_unregister(t
, longer_matched_scope
)])
101 return with_iframe(longer_matched_scope
);
105 return navigator
.serviceWorker
.register(url
, {scope
: matched_scope
});
109 return wait_for_state(t
, r
.installing
, 'activated');
112 return navigator
.serviceWorker
.register(
113 url
, {scope
: longer_matched_scope
});
116 return frame
.contentWindow
.navigator
.serviceWorker
.ready
;
119 assert_equals(r
.scope
, normalizeURL(longer_matched_scope
),
120 'longer matched registration should be returned');
121 assert_equals(frame
.contentWindow
.navigator
.serviceWorker
.controller
,
122 null, 'controller should be null');
123 return registration
.unregister();
127 return service_worker_unregister_and_done(t
, longer_matched_scope
);
129 .catch(unreached_rejection(t
));
130 }, 'ready after a longer matched registration registered');
132 async_test(function(t
) {
133 var url
= 'resources/empty-worker.js';
134 var matched_scope
= 'resources/blank.html?ready-after-resolve';
135 var longer_matched_scope
=
136 'resources/blank.html?ready-after-resolve-longer';
137 var frame
, registration
;
139 service_worker_unregister_and_register(t
, url
, matched_scope
)
142 return wait_for_state(t
, r
.installing
, 'activated');
145 return with_iframe(longer_matched_scope
);
149 return f
.contentWindow
.navigator
.serviceWorker
.ready
;
152 assert_equals(r
.scope
, normalizeURL(matched_scope
),
153 'matched registration should be returned');
154 return navigator
.serviceWorker
.register(
155 url
, {scope
: longer_matched_scope
});
158 return frame
.contentWindow
.navigator
.serviceWorker
.ready
;
161 assert_equals(r
.scope
, normalizeURL(matched_scope
),
162 'ready should only be resolved once');
163 return registration
.unregister();
167 return service_worker_unregister_and_done(t
, longer_matched_scope
);
169 .catch(unreached_rejection(t
));
170 }, 'access ready after it has been resolved');