2 <title>Service Worker: claim client using registration
</title>
3 <script src=
"../resources/testharness.js"></script>
4 <script src=
"../resources/testharness-helpers.js"></script>
5 <script src=
"../resources/testharnessreport.js"></script>
6 <script src=
"resources/test-helpers.js"></script>
9 promise_test(function(t
) {
10 var scope
= 'resources/';
11 var frame_url
= 'resources/blank.html?using-different-registration';
12 var url1
= 'resources/empty.js';
13 var url2
= 'resources/claim-worker.js';
14 var worker
, sw_registration
, frame
;
15 return service_worker_unregister_and_register(t
, url1
, scope
)
16 .then(function(registration
) {
17 return wait_for_state(t
, registration
.installing
, 'activated');
20 return with_iframe(frame_url
);
24 return navigator
.serviceWorker
.register(url2
, {scope
: frame_url
});
26 .then(function(registration
) {
27 worker
= registration
.installing
;
28 sw_registration
= registration
;
29 return wait_for_state(t
, registration
.installing
, 'activated');
32 var saw_controllerchanged
= new Promise(function(resolve
) {
33 frame
.contentWindow
.navigator
.serviceWorker
.oncontrollerchange
=
34 function() { resolve(); }
36 var channel
= new MessageChannel();
37 var saw_message
= new Promise(function(resolve
) {
38 channel
.port1
.onmessage
= t
.step_func(function(e
) {
39 assert_equals(e
.data
, 'PASS',
40 'Worker call to claim() should fulfill.');
44 worker
.postMessage({port
: channel
.port2
}, [channel
.port2
]);
45 return Promise
.all([saw_controllerchanged
, saw_message
]);
49 frame
.contentWindow
.navigator
.serviceWorker
.controller
.scriptURL
,
51 'Frame1 controller scriptURL should be changed to url2');
53 return sw_registration
.unregister();
56 return service_worker_unregister_and_done(t
, scope
);
58 }, 'Test worker claims client which is using another registration');
60 promise_test(function(t
) {
61 var scope
= 'resources/blank.html?using-same-registration';
62 var url1
= 'resources/empty.js';
63 var url2
= 'resources/claim-worker.js';
65 return service_worker_unregister_and_register(t
, url1
, scope
)
66 .then(function(registration
) {
67 return wait_for_state(t
, registration
.installing
, 'activated');
70 return with_iframe(scope
);
74 return navigator
.serviceWorker
.register(url2
, {scope
: scope
});
76 .then(function(registration
) {
77 worker
= registration
.installing
;
78 return wait_for_state(t
, registration
.installing
, 'installed');
81 var channel
= new MessageChannel();
82 var saw_message
= new Promise(function(resolve
) {
83 channel
.port1
.onmessage
= t
.step_func(function(e
) {
84 assert_equals(e
.data
, 'FAIL: exception: InvalidStateError',
85 'Worker call to claim() should reject with ' +
90 worker
.postMessage({port
: channel
.port2
}, [channel
.port2
]);
95 return service_worker_unregister_and_done(t
, scope
);
97 }, 'Test for the waiting worker claims a client which is using the the ' +