claim-not-using-registration.https.html (5252B)
1 <!DOCTYPE html> 2 <title>Service Worker: claim client not 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 <body> 7 <script> 8 9 promise_test(function(t) { 10 var init_scope = 'resources/blank.html?not-using-init'; 11 var claim_scope = 'resources/blank.html?not-using'; 12 var init_worker_url = 'resources/empty.js'; 13 var claim_worker_url = 'resources/claim-worker.js'; 14 var claim_worker, claim_registration, frame1, frame2; 15 return service_worker_unregister_and_register( 16 t, init_worker_url, init_scope) 17 .then(function(registration) { 18 t.add_cleanup(function() { 19 return service_worker_unregister(t, init_scope); 20 }); 21 22 return wait_for_state(t, registration.installing, 'activated'); 23 }) 24 .then(function() { 25 return Promise.all( 26 [with_iframe(init_scope), with_iframe(claim_scope)]); 27 }) 28 .then(function(frames) { 29 frame1 = frames[0]; 30 frame2 = frames[1]; 31 assert_equals( 32 frame1.contentWindow.navigator.serviceWorker.controller.scriptURL, 33 normalizeURL(init_worker_url), 34 'Frame1 controller should not be null'); 35 assert_equals( 36 frame2.contentWindow.navigator.serviceWorker.controller, null, 37 'Frame2 controller should be null'); 38 return navigator.serviceWorker.register(claim_worker_url, 39 {scope: claim_scope}); 40 }) 41 .then(function(registration) { 42 t.add_cleanup(function() { 43 return service_worker_unregister(t, claim_scope); 44 }); 45 46 claim_worker = registration.installing; 47 claim_registration = registration; 48 return wait_for_state(t, registration.installing, 'activated'); 49 }) 50 .then(function() { 51 var saw_controllerchanged = new Promise(function(resolve) { 52 frame2.contentWindow.navigator.serviceWorker.oncontrollerchange = 53 function() { resolve(); } 54 }); 55 var channel = new MessageChannel(); 56 var saw_message = new Promise(function(resolve) { 57 channel.port1.onmessage = t.step_func(function(e) { 58 assert_equals(e.data, 'PASS', 59 'Worker call to claim() should fulfill.'); 60 resolve(); 61 }); 62 }); 63 claim_worker.postMessage({port: channel.port2}, [channel.port2]); 64 return Promise.all([saw_controllerchanged, saw_message]); 65 }) 66 .then(function() { 67 assert_equals( 68 frame1.contentWindow.navigator.serviceWorker.controller.scriptURL, 69 normalizeURL(init_worker_url), 70 'Frame1 should not be influenced'); 71 assert_equals( 72 frame2.contentWindow.navigator.serviceWorker.controller.scriptURL, 73 normalizeURL(claim_worker_url), 74 'Frame2 should be controlled by the new registration'); 75 frame1.remove(); 76 frame2.remove(); 77 return claim_registration.unregister(); 78 }); 79 }, 'Test claim client which is not using registration'); 80 81 promise_test(function(t) { 82 var scope = 'resources/blank.html?longer-matched'; 83 var claim_scope = 'resources/blank.html?longer'; 84 var claim_worker_url = 'resources/claim-worker.js'; 85 var installing_worker_url = 'resources/empty-worker.js'; 86 var frame, claim_worker; 87 return with_iframe(scope) 88 .then(function(f) { 89 frame = f; 90 return navigator.serviceWorker.register( 91 claim_worker_url, {scope: claim_scope}); 92 }) 93 .then(function(registration) { 94 t.add_cleanup(function() { 95 return service_worker_unregister(t, claim_scope); 96 }); 97 98 claim_worker = registration.installing; 99 return wait_for_state(t, registration.installing, 'activated'); 100 }) 101 .then(function() { 102 return navigator.serviceWorker.register( 103 installing_worker_url, {scope: scope}); 104 }) 105 .then(function() { 106 t.add_cleanup(function() { 107 return service_worker_unregister(t, scope); 108 }); 109 110 var channel = new MessageChannel(); 111 var saw_message = new Promise(function(resolve) { 112 channel.port1.onmessage = t.step_func(function(e) { 113 assert_equals(e.data, 'PASS', 114 'Worker call to claim() should fulfill.'); 115 resolve(); 116 }); 117 }); 118 claim_worker.postMessage({port: channel.port2}, [channel.port2]); 119 return saw_message; 120 }) 121 .then(function() { 122 assert_equals( 123 frame.contentWindow.navigator.serviceWorker.controller, null, 124 'Frame should not be claimed when a longer-matched ' + 125 'registration exists'); 126 frame.remove(); 127 }); 128 }, 'Test claim client when there\'s a longer-matched registration not ' + 129 'already used by the page'); 130 131 </script>