claim-using-registration.https.html (3979B)
1 <!DOCTYPE html> 2 <title>Service Worker: claim client using registration</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script src="resources/test-helpers.sub.js"></script> 6 <script> 7 8 promise_test(function(t) { 9 var scope = 'resources/'; 10 var frame_url = 'resources/blank.html?using-different-registration'; 11 var url1 = 'resources/empty.js'; 12 var url2 = 'resources/claim-worker.js'; 13 var worker, sw_registration, frame; 14 return service_worker_unregister_and_register(t, url1, scope) 15 .then(function(registration) { 16 t.add_cleanup(function() { 17 return service_worker_unregister(t, scope); 18 }); 19 20 return wait_for_state(t, registration.installing, 'activated'); 21 }) 22 .then(function() { 23 return with_iframe(frame_url); 24 }) 25 .then(function(f) { 26 frame = f; 27 return navigator.serviceWorker.register(url2, {scope: frame_url}); 28 }) 29 .then(function(registration) { 30 worker = registration.installing; 31 sw_registration = registration; 32 return wait_for_state(t, registration.installing, 'activated'); 33 }) 34 .then(function() { 35 var saw_controllerchanged = new Promise(function(resolve) { 36 frame.contentWindow.navigator.serviceWorker.oncontrollerchange = 37 function() { resolve(); } 38 }); 39 var channel = new MessageChannel(); 40 var saw_message = new Promise(function(resolve) { 41 channel.port1.onmessage = t.step_func(function(e) { 42 assert_equals(e.data, 'PASS', 43 'Worker call to claim() should fulfill.'); 44 resolve(); 45 }); 46 }); 47 worker.postMessage({port: channel.port2}, [channel.port2]); 48 return Promise.all([saw_controllerchanged, saw_message]); 49 }) 50 .then(function() { 51 assert_equals( 52 frame.contentWindow.navigator.serviceWorker.controller.scriptURL, 53 normalizeURL(url2), 54 'Frame1 controller scriptURL should be changed to url2'); 55 frame.remove(); 56 return sw_registration.unregister(); 57 }); 58 }, 'Test worker claims client which is using another registration'); 59 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'; 64 var frame, worker; 65 return service_worker_unregister_and_register(t, url1, scope) 66 .then(function(registration) { 67 t.add_cleanup(function() { 68 return service_worker_unregister(t, scope); 69 }); 70 71 return wait_for_state(t, registration.installing, 'activated'); 72 }) 73 .then(function() { 74 return with_iframe(scope); 75 }) 76 .then(function(f) { 77 frame = f; 78 return navigator.serviceWorker.register(url2, {scope: scope}); 79 }) 80 .then(function(registration) { 81 worker = registration.installing; 82 return wait_for_state(t, registration.installing, 'installed'); 83 }) 84 .then(function() { 85 var channel = new MessageChannel(); 86 var saw_message = new Promise(function(resolve) { 87 channel.port1.onmessage = t.step_func(function(e) { 88 assert_equals(e.data, 'FAIL: exception: InvalidStateError', 89 'Worker call to claim() should reject with ' + 90 'InvalidStateError'); 91 resolve(); 92 }); 93 }); 94 worker.postMessage({port: channel.port2}, [channel.port2]); 95 return saw_message; 96 }) 97 .then(function() { 98 frame.remove(); 99 }); 100 }, 'Test for the waiting worker claims a client which is using the ' + 101 'same registration'); 102 103 </script>