update.https.html (6801B)
1 <!DOCTYPE html> 2 <title>Service Worker: Registration update()</title> 3 <meta name="timeout" content="long"> 4 <script src="/common/utils.js"></script> 5 <script src="/resources/testharness.js"></script> 6 <script src="/resources/testharnessreport.js"></script> 7 <script src="resources/testharness-helpers.js"></script> 8 <script src="resources/test-helpers.sub.js"></script> 9 <script> 10 'use strict'; 11 12 const SCOPE = 'resources/simple.txt'; 13 14 // Create a service worker (update-worker.py). The response to update() will be 15 // different based on the mode. 16 async function prepare_ready_registration_with_mode(t, mode) { 17 const key = token(); 18 const worker_url = `resources/update-worker.py?Key=${key}&Mode=${mode}`; 19 const expected_url = normalizeURL(worker_url); 20 const registration = await service_worker_unregister_and_register( 21 t, worker_url, SCOPE); 22 await wait_for_state(t, registration.installing, 'activated'); 23 assert_equals(registration.installing, null, 24 'prepare_ready: installing'); 25 assert_equals(registration.waiting, null, 26 'prepare_ready: waiting'); 27 assert_equals(registration.active.scriptURL, expected_url, 28 'prepare_ready: active'); 29 return [registration, expected_url]; 30 } 31 32 // Create a service worker (update-worker-from-file.py), which is initially 33 // |initial_worker| and |updated_worker| later. 34 async function prepare_ready_registration_with_file( 35 t, initial_worker, updated_worker) { 36 const key = token(); 37 const worker_url = `resources/update-worker-from-file.py?` + 38 `First=${initial_worker}&Second=${updated_worker}&Key=${key}`; 39 const expected_url = normalizeURL(worker_url); 40 41 const registration = await service_worker_unregister_and_register( 42 t, worker_url, SCOPE); 43 await wait_for_state(t, registration.installing, 'activated'); 44 assert_equals(registration.installing, null, 45 'prepare_ready: installing'); 46 assert_equals(registration.waiting, null, 47 'prepare_ready: waiting'); 48 assert_equals(registration.active.scriptURL, expected_url, 49 'prepare_ready: active'); 50 return [registration, expected_url]; 51 } 52 53 function assert_installing_and_active(registration, expected_url) { 54 assert_equals(registration.installing.scriptURL, expected_url, 55 'assert_installing_and_active: installing'); 56 assert_equals(registration.waiting, null, 57 'assert_installing_and_active: waiting'); 58 assert_equals(registration.active.scriptURL, expected_url, 59 'assert_installing_and_active: active'); 60 } 61 62 function assert_waiting_and_active(registration, expected_url) { 63 assert_equals(registration.installing, null, 64 'assert_waiting_and_active: installing'); 65 assert_equals(registration.waiting.scriptURL, expected_url, 66 'assert_waiting_and_active: waiting'); 67 assert_equals(registration.active.scriptURL, expected_url, 68 'assert_waiting_and_active: active'); 69 } 70 71 function assert_active_only(registration, expected_url) { 72 assert_equals(registration.installing, null, 73 'assert_active_only: installing'); 74 assert_equals(registration.waiting, null, 75 'assert_active_only: waiting'); 76 assert_equals(registration.active.scriptURL, expected_url, 77 'assert_active_only: active'); 78 } 79 80 promise_test(async t => { 81 const [registration, expected_url] = 82 await prepare_ready_registration_with_mode(t, 'normal'); 83 t.add_cleanup(() => registration.unregister()); 84 85 await Promise.all([registration.update(), wait_for_update(t, registration)]); 86 assert_installing_and_active(registration, expected_url); 87 88 await wait_for_state(t, registration.installing, 'installed'); 89 assert_waiting_and_active(registration, expected_url); 90 91 await wait_for_state(t, registration.waiting, 'activated'); 92 assert_active_only(registration, expected_url); 93 }, 'update() should succeed when new script is available.'); 94 95 promise_test(async t => { 96 const [registration, expected_url] = 97 await prepare_ready_registration_with_mode(t, 'bad_mime_type'); 98 t.add_cleanup(() => registration.unregister()); 99 100 await promise_rejects_dom(t, 'SecurityError', registration.update()); 101 assert_active_only(registration, expected_url); 102 }, 'update() should fail when mime type is invalid.'); 103 104 promise_test(async t => { 105 const [registration, expected_url] = 106 await prepare_ready_registration_with_mode(t, 'redirect'); 107 t.add_cleanup(() => registration.unregister()); 108 109 await promise_rejects_js(t, TypeError, registration.update()); 110 assert_active_only(registration, expected_url); 111 }, 'update() should fail when a response for the main script is redirect.'); 112 113 promise_test(async t => { 114 const [registration, expected_url] = 115 await prepare_ready_registration_with_mode(t, 'syntax_error'); 116 t.add_cleanup(() => registration.unregister()); 117 118 await promise_rejects_js(t, TypeError, registration.update()); 119 assert_active_only(registration, expected_url); 120 }, 'update() should fail when a new script contains a syntax error.'); 121 122 promise_test(async t => { 123 const [registration, expected_url] = 124 await prepare_ready_registration_with_mode(t, 'throw_install'); 125 t.add_cleanup(() => registration.unregister()); 126 127 await Promise.all([registration.update(), wait_for_update(t, registration)]); 128 assert_installing_and_active(registration, expected_url); 129 }, 'update() should resolve when the install event throws.'); 130 131 promise_test(async t => { 132 const [registration, expected_url] = 133 await prepare_ready_registration_with_mode(t, 'normal'); 134 t.add_cleanup(() => registration.unregister()); 135 136 // We need to hold a client alive so that unregister() below doesn't remove 137 // the registration before update() has had a chance to look at the pending 138 // uninstall flag. 139 const frame = await with_iframe(SCOPE); 140 t.add_cleanup(() => frame.remove()); 141 142 await promise_rejects_js( 143 t, TypeError, 144 Promise.all([registration.unregister(), registration.update()])); 145 }, 'update() should fail when the pending uninstall flag is set.'); 146 147 promise_test(async t => { 148 const [registration, expected_url] = 149 await prepare_ready_registration_with_file( 150 t, 151 'update-smaller-body-before-update-worker.js', 152 'update-smaller-body-after-update-worker.js'); 153 t.add_cleanup(() => registration.unregister()); 154 155 await Promise.all([registration.update(), wait_for_update(t, registration)]); 156 assert_installing_and_active(registration, expected_url); 157 158 await wait_for_state(t, registration.installing, 'installed'); 159 assert_waiting_and_active(registration, expected_url); 160 161 await wait_for_state(t, registration.waiting, 'activated'); 162 assert_active_only(registration, expected_url); 163 }, 'update() should succeed when the script shrinks.'); 164 </script>