unregister-immediately.https.html (5152B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Use Clear-Site-Data to immediately unregister service workers</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="resources/test-helpers.sub.js"></script> 7 <script src="resources/unregister-immediately-helpers.js"></script> 8 <body> 9 <script> 10 'use strict'; 11 12 // These tests use the Clear-Site-Data network response header to immediately 13 // unregister a service worker registration with a worker whose state is 14 // 'installed', 'waiting', 'activating' or 'activated'. Immediately 15 // unregistering runs the "Clear Registration" algorithm without waiting for the 16 // active worker's controlled clients to unload. 17 18 promise_test(async test => { 19 // This test keeps the service worker in the 'activating' state by using a 20 // script with an activate event waitUntil() promise that never resolves. 21 const script_url = 'resources/onactivate-waituntil-forever.js'; 22 const scope_url = 23 'resources/scope-for-unregister-immediately-with-waiting-worker'; 24 25 const registration = await service_worker_unregister_and_register( 26 test, script_url, scope_url); 27 const service_worker = registration.installing; 28 29 await wait_for_state(test, service_worker, 'activating'); 30 31 // Clear-Site-Data must cause activation to fail. 32 await Promise.all([ 33 clear_site_data(), 34 wait_for_state(test, service_worker, 'redundant')]); 35 36 await assert_no_registrations_exist(); 37 }, 'Clear-Site-Data must unregister a registration with a worker ' 38 + 'in the "activating" state.'); 39 40 promise_test(async test => { 41 // Create an registration with two service workers: one activated and one 42 // installed. 43 const script_url = 'resources/update_shell.py'; 44 const scope_url = 45 'resources/scope-for-unregister-immediately-with-with-update'; 46 47 const registration = await service_worker_unregister_and_register( 48 test, script_url, scope_url); 49 const first_service_worker = registration.installing; 50 51 await wait_for_state(test, first_service_worker, 'activated'); 52 registration.update(); 53 54 const event_watcher = new EventWatcher(test, registration, 'updatefound'); 55 await event_watcher.wait_for('updatefound'); 56 57 const second_service_worker = registration.installing; 58 await wait_for_state(test, second_service_worker, 'installed'); 59 60 // Clear-Site-Data must clear both workers from the registration. 61 await Promise.all([ 62 clear_site_data(), 63 wait_for_state(test, first_service_worker, 'redundant'), 64 wait_for_state(test, second_service_worker, 'redundant')]); 65 66 await assert_no_registrations_exist(); 67 }, 'Clear-Site-Data must unregister an activated registration with ' 68 + 'an update waiting.'); 69 70 promise_test(async test => { 71 const script_url = 'resources/empty.js'; 72 const scope_url = 73 'resources/blank.html?unregister-immediately-with-controlled-client'; 74 75 const registration = await service_worker_unregister_and_register( 76 test, script_url, scope_url); 77 const service_worker = registration.installing; 78 79 await wait_for_state(test, service_worker, 'activated'); 80 const frame = await add_controlled_iframe(test, scope_url); 81 const frame_registration = 82 await frame.contentWindow.navigator.serviceWorker.ready; 83 84 const event_watcher = new EventWatcher( 85 test, frame.contentWindow.navigator.serviceWorker, 'controllerchange'); 86 87 // Clear-Site-Data must remove the iframe's controller. 88 await Promise.all([ 89 clear_site_data(), 90 event_watcher.wait_for('controllerchange'), 91 wait_for_state(test, service_worker, 'redundant')]); 92 93 assert_equals(frame.contentWindow.navigator.serviceWorker.controller, null); 94 await assert_no_registrations_exist(); 95 96 // The ready promise must continue to resolve with the unregistered 97 // registration. 98 assert_equals(frame_registration, 99 await frame.contentWindow.navigator.serviceWorker.ready); 100 }, 'Clear-Site-Data must unregister an activated registration with controlled ' 101 + 'clients.'); 102 103 promise_test(async test => { 104 const script_url = 'resources/empty.js'; 105 const scope_url = 106 'resources/blank.html?unregister-immediately-while-waiting-to-clear'; 107 108 const registration = await service_worker_unregister_and_register( 109 test, script_url, scope_url); 110 const service_worker = registration.installing; 111 112 await wait_for_state(test, service_worker, 'activated'); 113 const frame = await add_controlled_iframe(test, scope_url); 114 115 const event_watcher = new EventWatcher( 116 test, frame.contentWindow.navigator.serviceWorker, 'controllerchange'); 117 118 // Unregister waits to clear the registration until no controlled clients 119 // exist. 120 await registration.unregister(); 121 122 // Clear-Site-Data must clear the unregistered registration immediately. 123 await Promise.all([ 124 clear_site_data(), 125 event_watcher.wait_for('controllerchange'), 126 wait_for_state(test, service_worker, 'redundant')]); 127 128 assert_equals(frame.contentWindow.navigator.serviceWorker.controller, null); 129 await assert_no_registrations_exist(); 130 }, 'Clear-Site-Data must clear an unregistered registration waiting for ' 131 + ' controlled clients to unload.'); 132 133 </script> 134 </body>