| OLD | NEW |
| (Empty) | |
| 1 <!DOCTYPE html> |
| 2 <title>Service Worker: Registration-useCache</title> |
| 3 <script src="/resources/testharness.js"></script> |
| 4 <script src="resources/testharness-helpers.js"></script> |
| 5 <script src="/resources/testharnessreport.js"></script> |
| 6 <script src="resources/test-helpers.sub.js"></script> |
| 7 <script> |
| 8 |
| 9 function registerFirstServiceWorker(test, script, scope, useCache) { |
| 10 var swr, sw; |
| 11 |
| 12 var setting = {scope: scope}; |
| 13 if (useCache !== undefined) { |
| 14 setting['useCache'] = useCache; |
| 15 } |
| 16 |
| 17 return Promise.resolve() |
| 18 .then(() => navigator.serviceWorker.register(script, setting)) |
| 19 .then(registration => swr = registration) |
| 20 |
| 21 .then(() => wait_for_update(test, swr)) |
| 22 .then(worker => sw = worker) |
| 23 |
| 24 .then(() => getMessageFromServiceWorker(sw, 'normal')) |
| 25 .then(() => wait_for_state(test, sw, 'activated')) |
| 26 .then(() => assert_true((!!swr.active && |
| 27 !swr.waiting && |
| 28 !swr.installing), |
| 29 ('The active SW should be the only SW. ' + |
| 30 '(a: ' + !!swr.active + |
| 31 ',w: ' + !swr.waiting + |
| 32 ',i: ' + !swr.installing + ')'))) |
| 33 .then(() => swr); |
| 34 } |
| 35 |
| 36 function getMessageFromServiceWorker(sw, httpRequestType, oldValues) { |
| 37 var mainResolveFunction, importedResolveFunction; |
| 38 var promises = [ |
| 39 new Promise(function(resolve) {mainResolveFunction = resolve}), |
| 40 new Promise(function(resolve) {importedResolveFunction = resolve}) |
| 41 ]; |
| 42 |
| 43 var messageChannel = new MessageChannel(); |
| 44 messageChannel.port1.onmessage = e => { |
| 45 if (httpRequestType) { |
| 46 assert_equals(e.data.type, httpRequestType, |
| 47 "HTTP request type check."); |
| 48 } |
| 49 |
| 50 var compareOldValue = (httpRequestType === 'revalidate') && oldValues; |
| 51 switch(e.data.from) { |
| 52 case 'main': |
| 53 if (compareOldValue) { |
| 54 assert_not_equals(e.data.value, oldValues[0], |
| 55 'Values shouldn\'t be the same'); |
| 56 } |
| 57 mainResolveFunction(e.data.value); |
| 58 break; |
| 59 case 'imported': |
| 60 if (compareOldValue) { |
| 61 assert_not_equals(e.data.value, oldValues[1], |
| 62 'Values shouldn\'t be the same'); |
| 63 } |
| 64 importedResolveFunction(e.data.value); |
| 65 break; |
| 66 } |
| 67 }; |
| 68 |
| 69 sw.postMessage({port: messageChannel.port2}, [messageChannel.port2]); |
| 70 return Promise.all(promises); |
| 71 } |
| 72 |
| 73 function testAction(test, registration, action, shouldCreateSW) { |
| 74 var testFunction = shouldCreateSW ? testActionDoesCreateSW |
| 75 : testActionDoesNotCreateSW; |
| 76 |
| 77 return Promise.resolve() |
| 78 .then(() => testFunction(registration, action, test)) |
| 79 .then(() => registration); |
| 80 } |
| 81 |
| 82 function testActionDoesCreateSW(registration, action, test) { |
| 83 var oldValues; |
| 84 |
| 85 return Promise.resolve() |
| 86 .then(() => getMessageFromServiceWorker(registration.active)) |
| 87 .then(values => oldValues = values) |
| 88 |
| 89 .then(() => action()) |
| 90 .then(() => wait_for_update(test, registration)) |
| 91 .then(worker => getMessageFromServiceWorker(worker, |
| 92 'revalidate', |
| 93 oldValues)); |
| 94 } |
| 95 |
| 96 function testActionDoesNotCreateSW(registration, action) { |
| 97 return Promise.resolve() |
| 98 .then(() => action()) |
| 99 .then(() => assert_true((!!registration.active && |
| 100 !registration.waiting && |
| 101 !registration.installing), |
| 102 ('The active SW should still be the only SW. ' + |
| 103 '(a: ' + !!registration.active + |
| 104 ',w: ' + !registration.waiting + |
| 105 ',i: ' + !registration.installing + ')'))); |
| 106 } |
| 107 |
| 108 promise_test(function(t) { |
| 109 var script = 'resources/update-max-aged-worker.py' + |
| 110 '?Test=Test_with_useCache_default'; |
| 111 var scope = 'resources/blank.html'; |
| 112 |
| 113 return Promise.resolve() |
| 114 .then(() => registerFirstServiceWorker(t, script, scope)) |
| 115 .then(r => testAction(t, r, r.update.bind(r), true)) |
| 116 |
| 117 // Tear down |
| 118 .then(() => service_worker_unregister_and_done(t, scope)); |
| 119 }, 'Test with useCache: default'); |
| 120 |
| 121 promise_test(function(t) { |
| 122 var script = 'resources/update-max-aged-worker.py' + |
| 123 '?Test=Test_with_useCache_true'; |
| 124 var scope = 'resources/blank.html'; |
| 125 |
| 126 return Promise.resolve() |
| 127 .then(() => registerFirstServiceWorker(t, script, scope, true)) |
| 128 .then(r => testAction(t, r, r.update.bind(r), false)) |
| 129 |
| 130 // Tear down |
| 131 .then(() => service_worker_unregister_and_done(t, scope)); |
| 132 }, 'Test with useCache: true'); |
| 133 |
| 134 promise_test(function(t) { |
| 135 var script = 'resources/update-max-aged-worker.py' + |
| 136 '?Test=Test_with_useCache_false'; |
| 137 var scope = 'resources/blank.html'; |
| 138 |
| 139 return Promise.resolve() |
| 140 .then(() => registerFirstServiceWorker(t, script, scope, false)) |
| 141 .then(r => testAction(t, r, r.update.bind(r), true)) |
| 142 |
| 143 // Tear down |
| 144 .then(() => service_worker_unregister_and_done(t, scope)); |
| 145 }, 'Test with useCache: false'); |
| 146 |
| 147 promise_test(function(t) { |
| 148 var script = 'resources/update-max-aged-worker.py' + |
| 149 '?Test=Consecutive_registrations_with_useCache_settings_false_f
alse'; |
| 150 var scope = 'resources/blank.html'; |
| 151 |
| 152 return Promise.resolve() |
| 153 .then(() => registerFirstServiceWorker(t, script, scope, false)) |
| 154 .then(r => { |
| 155 var action = navigator.serviceWorker.register.bind( |
| 156 navigator.serviceWorker, script, {scope: scope, |
| 157 useCache: false}); |
| 158 return testAction(t, r, action, false); |
| 159 }) |
| 160 |
| 161 // Tear down |
| 162 .then(() => service_worker_unregister_and_done(t, scope)) |
| 163 }, "Consecutive registrations with useCache settings(false, false)"); |
| 164 |
| 165 promise_test(function(t) { |
| 166 var script = 'resources/update-max-aged-worker.py' + |
| 167 '?Test=Consecutive_registrations_with_useCache_settings_false_t
rue'; |
| 168 var scope = 'resources/blank.html'; |
| 169 |
| 170 return Promise.resolve() |
| 171 .then(() => registerFirstServiceWorker(t, script, scope, false)) |
| 172 .then(r => testAction(t, r, r.update.bind(r), true)) |
| 173 .then(r => { |
| 174 var action = navigator.serviceWorker.register.bind( |
| 175 navigator.serviceWorker, script, {scope: scope, |
| 176 useCache: true}); |
| 177 return testAction(t, r, action, false); |
| 178 }) |
| 179 .then(r => testAction(t, r, r.update.bind(r), false)) |
| 180 |
| 181 // Tear down |
| 182 .then(() => service_worker_unregister_and_done(t, scope)) |
| 183 }, "Consecutive registrations with useCache settings(false, true)"); |
| 184 |
| 185 promise_test(function(t) { |
| 186 var script = 'resources/update-max-aged-worker.py' + |
| 187 '?Test=Consecutive_registrations_with_useCache_settings_true_fa
lse'; |
| 188 var scope = 'resources/blank.html'; |
| 189 |
| 190 return Promise.resolve() |
| 191 .then(() => registerFirstServiceWorker(t, script, scope, true)) |
| 192 .then(r => testAction(t, r, r.update.bind(r), false)) |
| 193 .then(r => { |
| 194 var action = navigator.serviceWorker.register.bind( |
| 195 navigator.serviceWorker, script, {scope: scope, |
| 196 useCache: false}); |
| 197 return testAction(t, r, action, true); |
| 198 }) |
| 199 .then(r => testAction(t, r, r.update.bind(r), true)) |
| 200 |
| 201 // Tear down |
| 202 .then(() => service_worker_unregister_and_done(t, scope)); |
| 203 }, "Consecutive registrations with useCache settings(true, false)"); |
| 204 |
| 205 promise_test(function(t) { |
| 206 var script = 'resources/update-max-aged-worker.py' + |
| 207 '?Test=Consecutive_registrations_with_useCache_settings_true_tr
ue'; |
| 208 var scope = 'resources/blank.html'; |
| 209 |
| 210 return Promise.resolve() |
| 211 .then(() => registerFirstServiceWorker(t, script, scope, true)) |
| 212 .then(r => { |
| 213 var action = navigator.serviceWorker.register.bind( |
| 214 navigator.serviceWorker, script, {scope: scope, |
| 215 useCache: true}); |
| 216 return testAction(t, r, action, false); |
| 217 }) |
| 218 |
| 219 // Tear down |
| 220 .then(() => service_worker_unregister_and_done(t, scope)) |
| 221 }, "Consecutive registrations with useCache settings(true, true)"); |
| 222 |
| 223 </script> |
| OLD | NEW |