OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: FetchEvent for resources</title> | 2 <title>Service Worker: FetchEvent for resources</title> |
3 <script src="../resources/testharness.js"></script> | 3 <script src="../resources/testharness.js"></script> |
4 <script src="../resources/testharnessreport.js"></script> | 4 <script src="../resources/testharnessreport.js"></script> |
5 <script src="../resources/get-host-info.js?pipe=sub"></script> | 5 <script src="../resources/get-host-info.js?pipe=sub"></script> |
6 <script src="resources/test-helpers.js"></script> | 6 <script src="resources/test-helpers.js"></script> |
7 <script> | 7 <script> |
8 | 8 |
9 function assert_resolves(promise, description) { | 9 function assert_resolves(promise, description) { |
10 return promise.catch(function() { throw description; }); | 10 return promise.catch(function(reason) { |
| 11 throw new Error(description + ' - ' + reason.message); |
| 12 }); |
11 } | 13 } |
12 | 14 |
13 function assert_rejects(promise, description) { | 15 function assert_rejects(promise, description) { |
14 return promise.then( | 16 return promise.then( |
15 function() { throw description; }, | 17 function() { throw new Error(description); }, |
16 function() {}); | 18 function() {}); |
17 } | 19 } |
18 | 20 |
19 function iframe_test(url) { | 21 function iframe_test(url) { |
20 return new Promise(function(resolve, reject) { | 22 return new Promise(function(resolve, reject) { |
21 var frame = document.createElement('iframe'); | 23 var frame = document.createElement('iframe'); |
22 // We can't catch the network error on iframe. So we use the timer. | |
23 var timer = setTimeout(function() { | |
24 reject(); | |
25 frame.remove(); | |
26 }, 500); | |
27 frame.src = url; | 24 frame.src = url; |
28 frame.onload = function() { | 25 frame.onload = function() { |
29 clearTimeout(timer); | |
30 if (frame.contentDocument.body.textContent == 'Hello world\n') | 26 if (frame.contentDocument.body.textContent == 'Hello world\n') |
31 resolve(); | 27 resolve(); |
32 else | 28 else |
33 reject(); | 29 reject(new Error('content mismatch')); |
34 frame.remove(); | 30 frame.remove(); |
35 }; | 31 }; |
36 document.body.appendChild(frame); | 32 document.body.appendChild(frame); |
37 }); | 33 }); |
38 } | 34 } |
39 | 35 |
40 async_test(function(t) { | 36 promise_test(function(t) { |
41 var SCOPE = 'resources/fetch-request-redirect-iframe.html'; | 37 var SCOPE = 'resources/fetch-request-redirect-iframe.html'; |
42 var SCRIPT = 'resources/fetch-rewrite-worker.js'; | 38 var SCRIPT = 'resources/fetch-rewrite-worker.js'; |
43 var host_info = get_host_info(); | 39 var host_info = get_host_info(); |
44 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + | 40 var REDIRECT_URL = host_info['HTTP_ORIGIN'] + |
45 '/serviceworker/resources/redirect.php?Redirect='; | 41 '/serviceworker/resources/redirect.php?Redirect='; |
46 var IMAGE_URL = host_info['HTTP_ORIGIN'] + '/resources/square.png'; | 42 var IMAGE_URL = host_info['HTTP_ORIGIN'] + '/resources/square.png'; |
47 var AUDIO_URL = | 43 var AUDIO_URL = |
48 host_info['HTTP_ORIGIN'] + '/media/resources/load-video.php?' + | 44 host_info['HTTP_ORIGIN'] + '/media/resources/load-video.php?' + |
49 'name=../../../../media/content/silence.oga&type=audio/ogg'; | 45 'name=../../../../media/content/silence.oga&type=audio/ogg'; |
50 var XHR_URL = host_info['HTTP_ORIGIN'] + | 46 var XHR_URL = host_info['HTTP_ORIGIN'] + |
51 '/serviceworker/resources/simple.txt'; | 47 '/serviceworker/resources/simple.txt'; |
52 var HTML_URL = host_info['HTTP_ORIGIN'] + '/resources/dummy.html'; | 48 var HTML_URL = host_info['HTTP_ORIGIN'] + '/resources/dummy.html'; |
53 | 49 |
54 var REDIRECT_TO_IMAGE_URL = REDIRECT_URL + encodeURIComponent(IMAGE_URL); | 50 var REDIRECT_TO_IMAGE_URL = REDIRECT_URL + encodeURIComponent(IMAGE_URL); |
55 var REDIRECT_TO_AUDIO_URL = REDIRECT_URL + encodeURIComponent(AUDIO_URL); | 51 var REDIRECT_TO_AUDIO_URL = REDIRECT_URL + encodeURIComponent(AUDIO_URL); |
56 var REDIRECT_TO_XHR_URL = REDIRECT_URL + encodeURIComponent(XHR_URL); | 52 var REDIRECT_TO_XHR_URL = REDIRECT_URL + encodeURIComponent(XHR_URL); |
57 var REDIRECT_TO_HTML_URL = REDIRECT_URL + encodeURIComponent(HTML_URL); | 53 var REDIRECT_TO_HTML_URL = REDIRECT_URL + encodeURIComponent(HTML_URL); |
58 | 54 |
59 var worker; | 55 var worker; |
60 var frame; | 56 var frame; |
61 service_worker_unregister_and_register(t, SCRIPT, SCOPE) | 57 return service_worker_unregister_and_register(t, SCRIPT, SCOPE) |
62 .then(function(registration) { | 58 .then(function(registration) { |
63 worker = registration.installing; | 59 worker = registration.installing; |
64 return wait_for_state(t, worker, 'activated'); | 60 return wait_for_state(t, worker, 'activated'); |
65 }) | 61 }) |
66 .then(function() { return with_iframe(SCOPE); }) | 62 .then(function() { return with_iframe(SCOPE); }) |
67 .then(function(f) { | 63 .then(function(f) { |
68 frame = f; | 64 frame = f; |
69 return Promise.all([ | 65 return Promise.all([ |
70 // XMLHttpRequest tests. | 66 // XMLHttpRequest tests. |
71 assert_resolves(frame.contentWindow.xhr(XHR_URL), | 67 assert_resolves(frame.contentWindow.xhr(XHR_URL), |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 'Normal iframe loading should succeed.'), | 139 'Normal iframe loading should succeed.'), |
144 assert_resolves( | 140 assert_resolves( |
145 iframe_test(REDIRECT_TO_HTML_URL), | 141 iframe_test(REDIRECT_TO_HTML_URL), |
146 'Normal redirected iframe loading should succeed.'), | 142 'Normal redirected iframe loading should succeed.'), |
147 assert_resolves( | 143 assert_resolves( |
148 iframe_test(SCOPE + '?url=' + | 144 iframe_test(SCOPE + '?url=' + |
149 encodeURIComponent(REDIRECT_TO_HTML_URL) + | 145 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
150 '&redirect-mode=follow'), | 146 '&redirect-mode=follow'), |
151 'Redirected iframe loading with Request.redirect=follow should'+ | 147 'Redirected iframe loading with Request.redirect=follow should'+ |
152 ' succeed.'), | 148 ' succeed.'), |
| 149 |
| 150 /* |
| 151 TODO(horo): iframe load failure detection is unreliable. |
| 152 Rework these. crbug.com/522587 |
153 assert_rejects( | 153 assert_rejects( |
154 iframe_test(SCOPE + '?url=' + | 154 iframe_test(SCOPE + '?url=' + |
155 encodeURIComponent(REDIRECT_TO_HTML_URL) + | 155 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
156 '&redirect-mode=error'), | 156 '&redirect-mode=error'), |
157 'Redirected iframe loading with Request.redirect=error should '+ | 157 'Redirected iframe loading with Request.redirect=error should '+ |
158 'fail.'), | 158 'fail.'), |
159 assert_resolves( | 159 assert_resolves( |
160 iframe_test(SCOPE + '?url=' + | 160 iframe_test(SCOPE + '?url=' + |
161 encodeURIComponent(REDIRECT_TO_HTML_URL) + | 161 encodeURIComponent(REDIRECT_TO_HTML_URL) + |
162 '&redirect-mode=manual'), | 162 '&redirect-mode=manual'), |
163 'Redirected iframe loading with Request.redirect=manual should'+ | 163 'Redirected iframe loading with Request.redirect=manual should'+ |
164 ' succeed.'), | 164 ' succeed.'), |
| 165 */ |
165 ]); | 166 ]); |
166 }) | 167 }) |
167 .then(function() { | 168 .then(function() { |
168 frame.remove(); | 169 frame.remove(); |
169 service_worker_unregister_and_done(t, SCOPE); | 170 service_worker_unregister_and_done(t, SCOPE); |
170 }) | 171 }); |
171 .catch(unreached_rejection(t)); | |
172 }, 'Verify redirect mode of Fetch API and ServiceWorker FetchEvent.'); | 172 }, 'Verify redirect mode of Fetch API and ServiceWorker FetchEvent.'); |
173 </script> | 173 </script> |
OLD | NEW |