OLD | NEW |
1 // Adapter for testharness.js-style tests with Service Workers | 1 // Adapter for testharness.js-style tests with Service Workers |
2 | 2 |
3 function service_worker_unregister_and_register(test, url, scope) { | 3 function service_worker_unregister_and_register(test, url, scope) { |
4 var options = scope ? { scope: scope } : {}; | 4 var options = scope ? { scope: scope } : {}; |
5 return navigator.serviceWorker.unregister(scope).then( | 5 return navigator.serviceWorker.unregister(scope).then( |
6 test.step_func(function() { | 6 test.step_func(function() { |
7 return navigator.serviceWorker.register(url, options); | 7 return navigator.serviceWorker.register(url, options); |
8 }), | 8 }), |
9 unreached_rejection(test, 'Unregister should not fail') | 9 unreached_rejection(test, 'Unregister should not fail') |
10 ).then(test.step_func(function(worker) { | 10 ).then(test.step_func(function(worker) { |
(...skipping 30 matching lines...) Expand all Loading... |
41 resolve(frame); | 41 resolve(frame); |
42 }; | 42 }; |
43 document.body.appendChild(frame); | 43 document.body.appendChild(frame); |
44 }); | 44 }); |
45 } | 45 } |
46 | 46 |
47 function normalizeURL(url) { | 47 function normalizeURL(url) { |
48 return new URL(url, document.location).toString().replace(/#.*$/, ''); | 48 return new URL(url, document.location).toString().replace(/#.*$/, ''); |
49 } | 49 } |
50 | 50 |
| 51 function wait_for_update(test, registration) { |
| 52 return new Promise(test.step_func(function(resolve, reject) { |
| 53 registration.addEventListener('updatefound', test.step_func(function() { |
| 54 resolve(registration.installing); |
| 55 })); |
| 56 })); |
| 57 } |
| 58 |
51 function wait_for_state(test, worker, state) { | 59 function wait_for_state(test, worker, state) { |
52 return new Promise(test.step_func(function(resolve, reject) { | 60 return new Promise(test.step_func(function(resolve, reject) { |
53 worker.addEventListener('statechange', test.step_func(function() { | 61 worker.addEventListener('statechange', test.step_func(function() { |
54 if (worker.state === state) | 62 if (worker.state === state) |
55 resolve(state); | 63 resolve(state); |
56 })); | 64 })); |
57 })); | 65 })); |
58 } | 66 } |
59 | 67 |
60 (function() { | 68 (function() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 102 } |
95 } | 103 } |
96 }; | 104 }; |
97 | 105 |
98 function service_worker_test(url, description) { | 106 function service_worker_test(url, description) { |
99 var scope = window.location.origin + '/service-worker-scope/' + | 107 var scope = window.location.origin + '/service-worker-scope/' + |
100 window.location.pathname; | 108 window.location.pathname; |
101 | 109 |
102 var test = async_test(description); | 110 var test = async_test(description); |
103 service_worker_unregister_and_register(test, url, scope) | 111 service_worker_unregister_and_register(test, url, scope) |
| 112 .then(function(registration) { |
| 113 return wait_for_update(test, registration); |
| 114 }) |
104 .then(function(worker) { return fetch_tests_from_worker(worker); }) | 115 .then(function(worker) { return fetch_tests_from_worker(worker); }) |
105 .then(function() { return navigator.serviceWorker.unregister(scope); }) | 116 .then(function() { return navigator.serviceWorker.unregister(scope); }) |
106 .then(function() { test.done(); }) | 117 .then(function() { test.done(); }) |
107 .catch(test.step_func(function(e) { throw e; })); | 118 .catch(test.step_func(function(e) { throw e; })); |
108 }; | 119 }; |
109 | 120 |
110 self.service_worker_test = service_worker_test; | 121 self.service_worker_test = service_worker_test; |
111 })(); | 122 })(); |
112 | 123 |
113 function get_host_info() { | 124 function get_host_info() { |
(...skipping 15 matching lines...) Expand all Loading... |
129 HTTP_ORIGIN: 'http://' + ORIGINAL_HOST + ':' + HTTP_PORT, | 140 HTTP_ORIGIN: 'http://' + ORIGINAL_HOST + ':' + HTTP_PORT, |
130 HTTPS_ORIGIN: 'https://' + ORIGINAL_HOST + ':' + HTTPS_PORT, | 141 HTTPS_ORIGIN: 'https://' + ORIGINAL_HOST + ':' + HTTPS_PORT, |
131 HTTP_REMOTE_ORIGIN: 'http://' + REMOTE_HOST + ':' + HTTP_PORT, | 142 HTTP_REMOTE_ORIGIN: 'http://' + REMOTE_HOST + ':' + HTTP_PORT, |
132 HTTPS_REMOTE_ORIGIN: 'https://' + REMOTE_HOST + ':' + HTTPS_PORT | 143 HTTPS_REMOTE_ORIGIN: 'https://' + REMOTE_HOST + ':' + HTTPS_PORT |
133 }; | 144 }; |
134 } | 145 } |
135 | 146 |
136 function base_path() { | 147 function base_path() { |
137 return location.pathname.replace(/\/[^\/]*$/, '/'); | 148 return location.pathname.replace(/\/[^\/]*$/, '/'); |
138 } | 149 } |
OLD | NEW |