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 port; | |
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 port = event.data.port; | |
109 }); | |
110 } | |
111 | |
112 var cleanup = function() { | |
113 if (client_frame && client_frame) { | |
114 client_frame.remove(); | |
115 } | |
116 | |
117 if (port) { | |
118 port.postMessage(''); | |
falken
2017/05/29 04:54:08
why's this needed in cleanup? could we comment or
mike3
2017/05/29 17:00:50
Done.
| |
119 } | |
120 | |
121 if (registration) { | |
122 return registration.unregister(); | |
123 } | |
124 }; | |
125 | |
126 var body = with_iframe(parameters.src_url) | |
falken
2017/05/29 04:54:08
Could we name this "test_promise" or something. "b
mike3
2017/05/29 17:00:50
Done.
| |
104 .then(function(frame) { | 127 .then(function(frame) { |
105 client_frame = frame; | 128 client_frame = frame; |
106 return service_worker_unregister_and_register( | 129 return service_worker_unregister_and_register( |
107 test, script_url, parameters.scope); | 130 test, script_url, parameters.scope); |
108 }) | 131 }) |
109 .then(function(registration) { | 132 .then(function(r) { |
133 registration = r; | |
110 service_worker = registration.installing; | 134 service_worker = registration.installing; |
111 return wait_for_state(test, service_worker, parameters.wait_state); | 135 return wait_for_state(test, service_worker, parameters.wait_state); |
112 }) | 136 }) |
113 .then(function(state) { | 137 .then(function() { |
114 if (parameters.should_be_reload) { | 138 if (parameters.should_be_reload) { |
115 client_frame.remove(); | 139 client_frame.remove(); |
116 return with_iframe(parameters.src_url); | 140 return with_iframe(parameters.src_url); |
117 } | 141 } |
118 return client_frame; | 142 return client_frame; |
119 }) | 143 }) |
120 .then(function(frame) { | 144 .then(function(frame) { |
121 client_frame = frame; | 145 client_frame = frame; |
122 return new Promise(function(resolve) { | 146 return new Promise(function(resolve) { |
123 var channel = new MessageChannel(); | 147 var channel = new MessageChannel(); |
124 channel.port1.onmessage = test.step_func(resolve); | 148 channel.port1.onmessage = test.step_func(resolve); |
125 service_worker.postMessage({ | 149 service_worker.postMessage({ |
126 port: channel.port2, | 150 port: channel.port2, |
127 url: parameters.dest_url | 151 url: parameters.dest_url |
128 }, [channel.port2]); | 152 }, [channel.port2]); |
129 }); | 153 }); |
130 }) | 154 }) |
131 .then(function(response) { | 155 .then(function(response) { |
132 client_frame && client_frame.remove() | |
133 assert_equals(response.data, parameters.expected); | 156 assert_equals(response.data, parameters.expected); |
134 return service_worker_unregister_and_done(test, parameters.scope); | 157 }); |
135 }) | 158 |
159 // Ensure that test "clean up" is deferred until after the test body | |
160 // executes. `Test#add_cleanup` cannot be used for this purpose because the | |
161 // operation is asynchronous, and `add_cleanup` does not support | |
162 // asynchronous operations at the time of this writing. Ensure also that | |
falken
2017/05/29 04:54:08
If there's a bug to link to about add_cleanup, it'
mike3
2017/05/29 17:00:50
There wasn't but there ought to have been.
https:/
| |
163 // test failure is not hidden by successful cleanup operation. | |
164 return body | |
165 .then(cleanup, cleanup) | |
166 .then(function() { return body; }); | |
136 }, parameters.description); | 167 }, parameters.description); |
137 } | 168 } |
138 | |
139 </script> | 169 </script> |
140 </body> | 170 </body> |
OLD | NEW |