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="../resources/get-host-info.js"></script> |
6 <script src="resources/test-helpers.js"></script> | 6 <script src="resources/test-helpers.js"></script> |
7 <body> | |
7 <script> | 8 <script> |
8 | 9 |
9 var scope = 'resources/blank.html?windowclient-navigate'; | 10 const SCOPE = 'resources/blank.html'; |
10 var script_url = 'resources/windowclient-navigate-worker.js'; | 11 const SCRIPT_URL = 'resources/windowclient-navigate-worker.js'; |
11 var client_frame; | 12 const URL_PREFIX = location.origin + '/serviceworker/'; |
12 var worker; | 13 const FULL_SCOPE_URL = URL_PREFIX + SCOPE; |
13 var test; | 14 const FULL_SCRIPT_URL = URL_PREFIX + SCRIPT_URL; |
15 const CROSS_ORIGIN_URL = get_host_info()['HTTP_REMOTE_ORIGIN'] + | |
16 '/serviceworker/resources/blank.html'; | |
14 | 17 |
15 var current_index = 0; | 18 navigate_test({ |
16 var test_list = [ | 19 description: 'normal test', |
17 { url : 'blank.html' }, | 20 dest_url: 'blank.html?navigate', |
18 { url : '' }, | 21 expected: FULL_SCOPE_URL + '?navigate', |
19 { url : 'blank.html', init : 'uncontrolled/blank.html' }, | 22 }); |
20 { url : get_host_info()['HTTP_REMOTE_ORIGIN'] + | |
21 '/serviceworker/resources/blank.html' }, | |
22 { url : 'http://[example].com' }, | |
23 { url : 'view-source://example.com' }, | |
24 { url : 'file:///' }, | |
25 { url : 'about:blank' }, | |
26 { url : 'about:crash' } | |
27 ]; | |
28 | 23 |
29 var expected = [ | 24 navigate_test({ |
30 location.origin + '/serviceworker/resources/blank.html', | 25 description: 'blank url test', |
31 location.origin + '/serviceworker/' + script_url, | 26 dest_url: '', |
32 'TypeError', | 27 expected: FULL_SCRIPT_URL |
33 null, | 28 }); |
34 'TypeError', | |
35 'TypeError', | |
36 'TypeError', | |
37 'TypeError', | |
38 'TypeError' | |
39 ]; | |
40 | 29 |
41 var actual = []; | 30 navigate_test({ |
31 description: 'in scope but not controlled test', | |
32 dest_url: 'blank.html?navigate', | |
33 expected: 'TypeError', | |
34 uncontrolled: true, | |
35 wait_state: 'installing' | |
36 }); | |
42 | 37 |
43 async_test(function(t) { | 38 navigate_test({ |
44 test = t; | 39 description: 'out scope test', |
45 return service_worker_unregister_and_register(test, script_url, scope) | 40 src_url: 'out_scope/blank.html', |
46 .then(function(registration) { | 41 dest_url: 'blank.html?navigate', |
47 worker = registration.installing; | 42 expected: 'TypeError', |
48 return wait_for_state(test, worker, 'activated'); | 43 }); |
49 }) | |
50 .then(function() { | |
51 var channel = new MessageChannel(); | |
52 channel.port1.onmessage = test.step_func(function(e) { | |
53 on_message(e, channel.port1); | |
54 }); | |
55 worker.postMessage({ port : channel.port2 }, [channel.port2]); | |
56 }) | |
57 .catch(unreached_rejection(t)); | |
58 }, 'WindowClient.navigate() test'); | |
59 | 44 |
60 function on_message(e, port) { | 45 navigate_test({ |
61 var message = e.data; | 46 description : 'cross orgin url test', |
47 dest_url: CROSS_ORIGIN_URL, | |
48 expected: null | |
49 }); | |
62 | 50 |
63 message == 'ready' || actual.push(message); | 51 navigate_test({ |
64 if (expected.length == actual.length) { | 52 description: 'invalid url(http://[eaxmple.com]) test', |
65 assert_array_equals(actual, expected); | 53 dest_url: 'http://[example].com', |
66 service_worker_unregister_and_done(test, scope); | 54 expected: 'TypeError' |
67 } else { | 55 }); |
68 client_frame && client_frame.remove(); | 56 |
69 var init_url = test_list[current_index].init || scope; | 57 navigate_test({ |
70 with_iframe(init_url).then(function(f) { | 58 description: 'invalid url(view-source://example.com) test', |
71 client_frame = f; | 59 dest_url: 'view-source://example.com', |
72 port.postMessage(test_list[current_index++].url); | 60 expected: 'TypeError' |
73 }); | 61 }); |
74 } | 62 |
63 navigate_test({ | |
64 description: 'invalid url(file:///) test', | |
65 dest_url: 'file:///', | |
66 expected: 'TypeError' | |
67 }); | |
68 | |
69 navigate_test({ | |
70 description: 'invalid url(about:blank) test', | |
71 dest_url: 'about:blank', | |
72 expected: 'TypeError' | |
73 }); | |
74 | |
75 function navigate_test(override_parameters) { | |
76 // default parameters | |
77 var parameters = { | |
78 description : null, | |
79 src_url : SCOPE, | |
80 dest_url : null, | |
81 expected : null, | |
82 }; | |
83 | |
84 for (key in override_parameters) | |
85 parameters[key] = override_parameters[key]; | |
86 | |
87 promise_test(function(test) { | |
88 var service_worker; | |
89 var client_frame; | |
90 return service_worker_unregister(test, SCOPE) | |
91 .then(function() { | |
92 return with_iframe(parameters.src_url); | |
93 }) | |
94 .then(function(frame) { | |
95 client_frame = frame; | |
96 return navigator.serviceWorker.register(SCRIPT_URL, { scope: SCOPE }); | |
zino
2016/01/22 02:29:35
On second thoughts, this seems strange.
The iframe
| |
97 }) | |
98 .then(function(registration) { | |
99 service_worker = registration.installing; | |
100 return wait_for_state(test, service_worker, 'activated'); | |
101 }) | |
102 .then(function(state) { | |
103 var w = client_frame.contentWindow; | |
104 if (parameters.uncontrolled) { | |
105 assert_equals(w.navigator.serviceWorker.controller, null); | |
106 return Promise.resolve(); | |
107 } | |
108 return new Promise(function(resolve) { | |
109 client_frame.onload = function() { resolve(); } | |
110 w.location.reload(); | |
111 }); | |
112 }) | |
113 .then(function() { | |
114 return new Promise(function(resolve) { | |
115 var channel = new MessageChannel(); | |
116 channel.port1.onmessage = test.step_func(resolve); | |
117 service_worker.postMessage({ | |
118 port: channel.port2, | |
119 url: parameters.dest_url | |
120 }, [channel.port2]); | |
121 }); | |
122 }) | |
123 .then(function(response) { | |
124 client_frame && client_frame.remove() | |
125 assert_equals(response.data, parameters.expected); | |
126 return service_worker_unregister_and_done(test, SCOPE); | |
127 }) | |
128 .catch(unreached_rejection(test)); | |
129 }, parameters.description); | |
75 } | 130 } |
76 | 131 |
77 </script> | 132 </script> |
133 </body> | |
OLD | NEW |