Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <title>Service Worker: WindowClient.navigate() tests</title> | 2 <title>Service Worker: WindowClient.navigate() tests</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"></script> | 5 <script src="/common/get-host-info.sub.js"></script> |
| 6 <script src="resources/test-helpers.js"></script> | 6 <script src="resources/test-helpers.sub.js"></script> |
| 7 <body> | 7 <body> |
| 8 <script> | 8 <script> |
| 9 'use strict'; | |
| 9 | 10 |
| 10 const SCOPE = 'resources/blank.html'; | 11 const SCOPE = 'resources/blank.html'; |
| 11 const SCRIPT_URL = 'resources/windowclient-navigate-worker.js'; | 12 const SCRIPT_URL = 'resources/windowclient-navigate-worker.js'; |
| 12 const CROSS_ORIGIN_URL = get_host_info()['HTTP_REMOTE_ORIGIN'] + | 13 const CROSS_ORIGIN_URL = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() + |
| 13 '/serviceworker/resources/blank.html'; | 14 'resources/blank.html'; |
| 14 | 15 |
| 15 navigate_test({ | 16 navigate_test({ |
| 16 description: 'normal test', | 17 description: 'normal', |
| 17 dest_url: 'blank.html?navigate', | 18 dest_url: 'blank.html?navigate', |
| 18 expected: normalizeURL(SCOPE) + '?navigate', | 19 expected: normalizeURL(SCOPE) + '?navigate', |
| 19 }); | 20 }); |
| 20 | 21 |
| 21 navigate_test({ | 22 navigate_test({ |
| 22 description: 'blank url test', | 23 description: 'blank url', |
| 23 dest_url: '', | 24 dest_url: '', |
| 24 expected: normalizeURL(SCRIPT_URL) | 25 expected: normalizeURL(SCRIPT_URL) |
| 25 }); | 26 }); |
| 26 | 27 |
| 27 navigate_test({ | 28 navigate_test({ |
| 28 description: 'in scope but not controlled test on installing worker', | 29 description: 'in scope but not controlled test on installing worker', |
| 29 dest_url: 'blank.html?navigate', | 30 dest_url: 'blank.html?navigate', |
| 30 expected: 'TypeError', | 31 expected: 'TypeError', |
| 31 wait_state: 'installing', | 32 wait_state: 'installing', |
| 32 }); | 33 }); |
| 33 | 34 |
| 34 navigate_test({ | 35 navigate_test({ |
| 35 description: 'in scope but not controlled test on active worker', | 36 description: 'in scope but not controlled test on active worker', |
| 36 dest_url: 'blank.html?navigate', | 37 dest_url: 'blank.html?navigate', |
| 37 expected: 'TypeError', | 38 expected: 'TypeError', |
| 38 should_be_reload: false, | 39 should_be_reload: false, |
| 39 }); | 40 }); |
| 40 | 41 |
| 41 navigate_test({ | 42 navigate_test({ |
| 42 description: 'out scope test', | 43 description: 'out of scope', |
| 43 src_url: 'out_scope/blank.html', | 44 src_url: '/common/blank.html', |
| 44 dest_url: 'blank.html?navigate', | 45 dest_url: 'blank.html?navigate', |
| 45 expected: 'TypeError', | 46 expected: 'TypeError', |
| 46 }); | 47 }); |
| 47 | 48 |
| 48 navigate_test({ | 49 navigate_test({ |
| 49 description: 'cross orgin url test', | 50 description: 'cross orgin url', |
| 50 dest_url: CROSS_ORIGIN_URL, | 51 dest_url: CROSS_ORIGIN_URL, |
| 51 expected: null | 52 expected: null |
| 52 }); | 53 }); |
| 53 | 54 |
| 54 navigate_test({ | 55 navigate_test({ |
| 55 description: 'invalid url(http://[example.com]) test', | 56 description: 'invalid url (http://[example.com])', |
| 56 dest_url: 'http://[example].com', | 57 dest_url: 'http://[example].com', |
| 57 expected: 'TypeError' | 58 expected: 'TypeError' |
| 58 }); | 59 }); |
| 59 | 60 |
| 60 navigate_test({ | 61 navigate_test({ |
| 61 description: 'invalid url(view-source://example.com) test', | 62 description: 'invalid url (view-source://example.com)', |
| 62 dest_url: 'view-source://example.com', | 63 dest_url: 'view-source://example.com', |
| 63 expected: 'TypeError' | 64 expected: 'TypeError' |
| 64 }); | 65 }); |
| 65 | 66 |
| 66 navigate_test({ | 67 navigate_test({ |
| 67 description: 'invalid url(file:///) test', | 68 description: 'invalid url (file:///)', |
| 68 dest_url: 'file:///', | 69 dest_url: 'file:///', |
| 69 expected: 'TypeError' | 70 expected: 'TypeError' |
| 70 }); | 71 }); |
| 71 | 72 |
| 72 navigate_test({ | 73 navigate_test({ |
| 73 description: 'invalid url(about:blank) test', | 74 description: 'invalid url (about:blank)', |
| 74 dest_url: 'about:blank', | 75 dest_url: 'about:blank', |
| 75 expected: 'TypeError' | 76 expected: 'TypeError' |
| 76 }); | 77 }); |
| 77 | 78 |
| 78 function navigate_test(override_parameters) { | 79 function navigate_test(override_parameters) { |
| 79 // default parameters | 80 // default parameters |
| 80 var parameters = { | 81 var parameters = { |
| 81 description: null, | 82 description: null, |
| 82 src_url: SCOPE, | 83 src_url: SCOPE, |
| 83 dest_url: null, | 84 dest_url: null, |
| 84 expected: null, | 85 expected: null, |
| 85 wait_state: 'activated', | 86 wait_state: 'activated', |
| 86 scope: SCOPE, | 87 scope: SCOPE, |
| 87 should_be_reload: true | 88 should_be_reload: true |
| 88 }; | 89 }; |
| 90 var key; | |
| 89 | 91 |
| 90 for (key in override_parameters) | 92 for (key in override_parameters) |
| 91 parameters[key] = override_parameters[key]; | 93 parameters[key] = override_parameters[key]; |
| 92 | 94 |
| 93 promise_test(function(test) { | 95 promise_test(function(test) { |
| 94 var service_worker; | 96 var service_worker; |
| 95 var client_frame; | 97 var client_frame; |
| 98 var registration; | |
| 99 var pausedLifecyclePort; | |
| 96 var script_url = SCRIPT_URL; | 100 var script_url = SCRIPT_URL; |
| 97 | 101 |
| 98 // For in-scope-but-not-controlled test on installing worker, | 102 // For in-scope-but-not-controlled test on installing worker, |
| 99 // if the wait_state is "installing", then append the query to script_url. | 103 // if the wait_state is "installing", then append the query to script_url. |
| 100 if (parameters.wait_state == 'installing') | 104 if (parameters.wait_state == 'installing') { |
| 101 script_url += '?' + parameters.wait_state; | 105 script_url += '?' + parameters.wait_state; |
| 102 | 106 |
| 103 return with_iframe(parameters.src_url) | 107 navigator.serviceWorker.addEventListener('message', function(event) { |
| 108 pausedLifecyclePort = event.data.port; | |
| 109 }); | |
| 110 } | |
| 111 | |
| 112 var cleanup = function() { | |
| 113 if (client_frame && client_frame) { | |
| 114 client_frame.remove(); | |
| 115 } | |
| 116 | |
| 117 // Some tests require that the worker remain in a given lifecycle phase. | |
| 118 // "Clean up" logic for these tests requires signaling the worker to | |
| 119 // release the hold; this allows the worker to be properly discarded | |
| 120 // prior to the execution of additional tests. | |
| 121 if (pausedLifecyclePort) { | |
| 122 // The value of the posted message is inconsequential. A string is | |
| 123 // specified here solely to aid in test debugging. | |
| 124 pausedLifecyclePort.postMessage('continue lifecycle'); | |
|
falken
2017/05/30 01:11:01
Thanks for the comments and explanation!
| |
| 125 } | |
| 126 | |
| 127 if (registration) { | |
| 128 return registration.unregister(); | |
| 129 } | |
| 130 }; | |
| 131 | |
| 132 var test_body = with_iframe(parameters.src_url) | |
| 104 .then(function(frame) { | 133 .then(function(frame) { |
| 105 client_frame = frame; | 134 client_frame = frame; |
| 106 return service_worker_unregister_and_register( | 135 return service_worker_unregister_and_register( |
| 107 test, script_url, parameters.scope); | 136 test, script_url, parameters.scope); |
| 108 }) | 137 }) |
| 109 .then(function(registration) { | 138 .then(function(r) { |
| 139 registration = r; | |
| 110 service_worker = registration.installing; | 140 service_worker = registration.installing; |
| 111 return wait_for_state(test, service_worker, parameters.wait_state); | 141 return wait_for_state(test, service_worker, parameters.wait_state); |
| 112 }) | 142 }) |
| 113 .then(function(state) { | 143 .then(function() { |
| 114 if (parameters.should_be_reload) { | 144 if (parameters.should_be_reload) { |
| 115 client_frame.remove(); | 145 client_frame.remove(); |
| 116 return with_iframe(parameters.src_url); | 146 return with_iframe(parameters.src_url); |
| 117 } | 147 } |
| 118 return client_frame; | 148 return client_frame; |
| 119 }) | 149 }) |
| 120 .then(function(frame) { | 150 .then(function(frame) { |
| 121 client_frame = frame; | 151 client_frame = frame; |
| 122 return new Promise(function(resolve) { | 152 return new Promise(function(resolve) { |
| 123 var channel = new MessageChannel(); | 153 var channel = new MessageChannel(); |
| 124 channel.port1.onmessage = test.step_func(resolve); | 154 channel.port1.onmessage = test.step_func(resolve); |
| 125 service_worker.postMessage({ | 155 service_worker.postMessage({ |
| 126 port: channel.port2, | 156 port: channel.port2, |
| 127 url: parameters.dest_url | 157 url: parameters.dest_url |
| 128 }, [channel.port2]); | 158 }, [channel.port2]); |
| 129 }); | 159 }); |
| 130 }) | 160 }) |
| 131 .then(function(response) { | 161 .then(function(response) { |
| 132 client_frame && client_frame.remove() | |
| 133 assert_equals(response.data, parameters.expected); | 162 assert_equals(response.data, parameters.expected); |
| 134 return service_worker_unregister_and_done(test, parameters.scope); | 163 }); |
| 135 }) | 164 |
| 165 // Ensure that test "clean up" is deferred until after the test body | |
| 166 // executes. `Test#add_cleanup` cannot be used for this purpose because the | |
| 167 // operation is asynchronous, and `add_cleanup` does not support | |
| 168 // asynchronous operations at the time of this writing. See | |
| 169 // https://github.com/w3c/web-platform-tests/issues/6075 | |
| 170 // Ensure also that test failure is not hidden by successful cleanup | |
| 171 // operation. | |
| 172 return test_body | |
| 173 .then(cleanup, cleanup) | |
| 174 .then(function() { return test_body; }); | |
| 136 }, parameters.description); | 175 }, parameters.description); |
| 137 } | 176 } |
| 138 | |
| 139 </script> | 177 </script> |
| 140 </body> | 178 </body> |
| OLD | NEW |