| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <meta charset="utf-8"> | 2 <meta charset="utf-8"> |
| 3 <title>NavigationPreloadManager.getState</title> | 3 <title>NavigationPreloadManager.getState</title> |
| 4 <script src="../../resources/testharness.js"></script> | 4 <script src="../../resources/testharness.js"></script> |
| 5 <script src="../../resources/testharnessreport.js"></script> | 5 <script src="../../resources/testharnessreport.js"></script> |
| 6 <script src="../resources/test-helpers.js"></script> | 6 <script src="../resources/test-helpers.js"></script> |
| 7 <script src="resources/helpers.js"></script> |
| 7 <body> | 8 <body> |
| 8 <script> | 9 <script> |
| 9 function post_and_wait_for_reply(worker, message) { | 10 function post_and_wait_for_reply(worker, message) { |
| 10 return new Promise(resolve => { | 11 return new Promise(resolve => { |
| 11 navigator.serviceWorker.onmessage = e => { resolve(e.data); }; | 12 navigator.serviceWorker.onmessage = e => { resolve(e.data); }; |
| 12 worker.postMessage(message); | 13 worker.postMessage(message); |
| 13 }); | 14 }); |
| 14 } | 15 } |
| 15 | 16 |
| 16 function expect_state(state, enabled, header, desc) { | |
| 17 assert_equals(Object.keys(state).length, 2, desc + ': # of keys'); | |
| 18 assert_equals(state.enabled, enabled, desc + ': enabled'); | |
| 19 assert_equals(state.headerValue, header, desc + ': header'); | |
| 20 } | |
| 21 | |
| 22 promise_test(t => { | 17 promise_test(t => { |
| 23 const scope = '../resources/get-state'; | 18 const scope = '../resources/get-state'; |
| 24 const script = '../resources/empty-worker.js'; | 19 const script = '../resources/empty-worker.js'; |
| 25 var np; | 20 var np; |
| 26 | 21 |
| 27 return service_worker_unregister_and_register(t, script, scope) | 22 return service_worker_unregister_and_register(t, script, scope) |
| 28 .then(r => { | 23 .then(r => { |
| 29 np = r.navigationPreload; | 24 np = r.navigationPreload; |
| 30 add_completion_callback(() => r.unregister()); | 25 add_completion_callback(() => r.unregister()); |
| 31 return np.getState(); | 26 return wait_for_state(t, r.installing, 'activated'); |
| 32 }) | 27 }) |
| 28 .then(() => np.getState()) |
| 33 .then(state => { | 29 .then(state => { |
| 34 expect_state(state, false, 'true', 'default state'); | 30 expect_state(state, false, 'true', 'default state'); |
| 35 return np.enable(); | 31 return np.enable(); |
| 36 }) | 32 }) |
| 37 .then(result => { | 33 .then(result => { |
| 38 assert_equals(result, undefined, | 34 assert_equals(result, undefined, |
| 39 'enable() should resolve to undefined'); | 35 'enable() should resolve to undefined'); |
| 40 return np.getState(); | 36 return np.getState(); |
| 41 }) | 37 }) |
| 42 .then(state => { | 38 .then(state => { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 new TypeError, | 80 new TypeError, |
| 85 np.setHeaderValue('\rcarriage'), | 81 np.setHeaderValue('\rcarriage'), |
| 86 'setHeaderValue() should throw if passed \\r'); | 82 'setHeaderValue() should throw if passed \\r'); |
| 87 }) | 83 }) |
| 88 .then(() => { | 84 .then(() => { |
| 89 return promise_rejects(t, | 85 return promise_rejects(t, |
| 90 new TypeError, | 86 new TypeError, |
| 91 np.setHeaderValue('newline\n'), | 87 np.setHeaderValue('newline\n'), |
| 92 'setHeaderValue() should throw if passed \\n'); | 88 'setHeaderValue() should throw if passed \\n'); |
| 93 }) | 89 }) |
| 90 .then(() => { |
| 91 return promise_rejects(t, |
| 92 new TypeError, |
| 93 np.setHeaderValue(), |
| 94 'setHeaderValue() should throw if passed undefined'); |
| 95 }) |
| 94 .then(() => np.enable().then(() => np.getState())) | 96 .then(() => np.enable().then(() => np.getState())) |
| 95 .then(state => { | 97 .then(state => { |
| 96 expect_state(state, true, 'null', 'enable() should not change header'); | 98 expect_state(state, true, 'null', 'enable() should not change header'); |
| 97 }); | 99 }); |
| 98 }, 'getState'); | 100 }, 'getState'); |
| 99 | 101 |
| 100 // This test sends commands to a worker to call enable()/disable()/getState(). | 102 // This test sends commands to a worker to call enable()/disable()/getState(). |
| 101 // It checks the results from the worker and verifies that they match the | 103 // It checks the results from the worker and verifies that they match the |
| 102 // navigation preload state accessible from the page. | 104 // navigation preload state accessible from the page. |
| 103 promise_test(t => { | 105 promise_test(t => { |
| 104 const scope = 'resources/get-state-worker'; | 106 const scope = 'resources/get-state-worker'; |
| 105 const script = 'resources/get-state-worker.js'; | 107 const script = 'resources/get-state-worker.js'; |
| 106 var worker; | 108 var worker; |
| 107 var registration; | 109 var registration; |
| 108 | 110 |
| 109 return service_worker_unregister_and_register(t, script, scope) | 111 return service_worker_unregister_and_register(t, script, scope) |
| 110 .then(r => { | 112 .then(r => { |
| 111 registration = r; | 113 registration = r; |
| 112 add_completion_callback(() => registration.unregister()); | 114 add_completion_callback(() => registration.unregister()); |
| 113 worker = registration.installing; | 115 worker = registration.installing; |
| 116 return wait_for_state(t, worker, 'activated'); |
| 117 }) |
| 118 .then(() => { |
| 114 // Call getState(). | 119 // Call getState(). |
| 115 return post_and_wait_for_reply(worker, 'getState'); | 120 return post_and_wait_for_reply(worker, 'getState'); |
| 116 }) | 121 }) |
| 117 .then(data => { | 122 .then(data => { |
| 118 return Promise.all([data, registration.navigationPreload.getState()]); | 123 return Promise.all([data, registration.navigationPreload.getState()]); |
| 119 }) | 124 }) |
| 120 .then(states => { | 125 .then(states => { |
| 121 expect_state(states[0], false, 'true', | 126 expect_state(states[0], false, 'true', |
| 122 'default state (from worker)'); | 127 'default state (from worker)'); |
| 123 expect_state(states[1], false, 'true', | 128 expect_state(states[1], false, 'true', |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 post_and_wait_for_reply(worker, 'getState'), | 167 post_and_wait_for_reply(worker, 'getState'), |
| 163 registration.navigationPreload.getState()]); | 168 registration.navigationPreload.getState()]); |
| 164 }) | 169 }) |
| 165 .then(states => { | 170 .then(states => { |
| 166 expect_state(states[0], false, 'insightful', | 171 expect_state(states[0], false, 'insightful', |
| 167 'state after setHeaderValue() (from worker)'); | 172 'state after setHeaderValue() (from worker)'); |
| 168 expect_state(states[1], false, 'insightful', | 173 expect_state(states[1], false, 'insightful', |
| 169 'state after setHeaderValue() (from page)'); | 174 'state after setHeaderValue() (from page)'); |
| 170 }); | 175 }); |
| 171 }, 'getState from a worker'); | 176 }, 'getState from a worker'); |
| 177 |
| 178 // This tests navigation preload API when there is no active worker. It calls |
| 179 // the API from the main page and then from the worker itself. |
| 180 promise_test(t => { |
| 181 const scope = 'resources/wait-for-activate-worker'; |
| 182 const script = 'resources/wait-for-activate-worker.js'; |
| 183 var registration; |
| 184 var np; |
| 185 return service_worker_unregister_and_register(t, script, scope) |
| 186 .then(r => { |
| 187 registration = r; |
| 188 np = registration.navigationPreload; |
| 189 add_completion_callback(() => registration.unregister()); |
| 190 return Promise.all([ |
| 191 promise_rejects( |
| 192 t, 'InvalidStateError', np.enable(), |
| 193 'enable should reject if there is no active worker'), |
| 194 promise_rejects( |
| 195 t, 'InvalidStateError', np.disable(), |
| 196 'disable should reject if there is no active worker'), |
| 197 promise_rejects( |
| 198 t, 'InvalidStateError', np.setHeaderValue('umm'), |
| 199 'setHeaderValue should reject if there is no active worker')]); |
| 200 }) |
| 201 .then(() => np.getState()) |
| 202 .then(state => { |
| 203 expect_state(state, false, 'true', 'state before activation'); |
| 204 return post_and_wait_for_reply(registration.installing, 'ping'); |
| 205 }) |
| 206 .then(result => assert_equals(result, 'PASS')); |
| 207 }, 'no active worker'); |
| 172 </script> | 208 </script> |
| 173 </body> | 209 </body> |
| OLD | NEW |