OLD | NEW |
1 // Runs various navigator.connect tests, verifying that message are sent and | 1 // Runs various navigator.connect tests, verifying that message are sent and |
2 // received correctly. | 2 // received correctly. |
3 // |connect_method| is a function taking a test object as well as the regular | 3 // |connect_method| is a function taking a test object as well as the regular |
4 // navigator.connect parameters. This makes it possible to run the same tests | 4 // navigator.connect parameters. This makes it possible to run the same tests |
5 // while wrapping all navigator.connect calls in a helper making the actual | 5 // while wrapping all navigator.connect calls in a helper making the actual |
6 // connection from a cross origin iframe, or a web worker. | 6 // connection from a cross origin iframe, or a web worker. |
7 function run_postmessage_tests(source_origin, connect_method) { | 7 function run_postmessage_tests(source_origin, connect_method) { |
8 // Helper method that waits for a reply on a port, and resolves a promise with | 8 // Helper method that waits for a reply on a port, and resolves a promise with |
9 // the reply. | 9 // the reply. |
10 function wait_for_reply(t, port) { | 10 function wait_for_reply(t, port) { |
11 return new Promise(function(resolve) { | 11 return new Promise(function(resolve) { |
12 var resolved = false; | 12 var resolved = false; |
13 port.onmessage = t.step_func(function(event) { | 13 port.onmessage = t.step_func(function(event) { |
14 assert_false(resolved); | 14 assert_false(resolved); |
15 resolved = true; | 15 resolved = true; |
16 resolve(event.data); | 16 resolve(event.data); |
17 }); | 17 }); |
18 }); | 18 }); |
19 } | 19 } |
20 | 20 |
21 // FIXME: Current navigator.connect implementation returns origins with a | 21 // FIXME: Current navigator.connect implementation returns origins with a |
22 // trailing slash, which is probably not correct. | 22 // trailing slash, which is probably not correct. |
23 if (source_origin.charAt(source_origin.length - 1) != '/') | 23 if (source_origin.charAt(source_origin.length - 1) != '/') |
24 source_origin += '/'; | 24 source_origin += '/'; |
25 | 25 |
26 promise_test(function(t) { | 26 sequential_promise_test(function(t) { |
27 var scope = sw_scope + '/echo'; | 27 var scope = sw_scope + '/echo'; |
28 var sw_url = 'resources/echo-worker.js'; | 28 var sw_url = 'resources/echo-worker.js'; |
29 var test_message = 'ping over navigator.connect'; | 29 var test_message = 'ping over navigator.connect'; |
30 return service_worker_unregister_and_register(t, sw_url, scope) | 30 return service_worker_unregister_and_register(t, sw_url, scope) |
31 .then(function(registration) { | 31 .then(function(registration) { |
32 return wait_for_state(t, registration.installing, 'activated'); | 32 return wait_for_state(t, registration.installing, 'activated'); |
33 }) | 33 }) |
34 .then(function() { | 34 .then(function() { |
35 return connect_method(t, scope + '/service'); | 35 return connect_method(t, scope + '/service'); |
36 }) | 36 }) |
37 .then(function(port) { | 37 .then(function(port) { |
38 port.postMessage(test_message); | 38 port.postMessage(test_message); |
39 return wait_for_reply(t, port); | 39 return wait_for_reply(t, port); |
40 }) | 40 }) |
41 .then(function(response) { | 41 .then(function(response) { |
42 assert_equals(response, test_message); | 42 assert_equals(response, test_message); |
43 return service_worker_unregister(t, scope); | 43 return service_worker_unregister(t, scope); |
44 }); | 44 }); |
45 }, 'Messages can be sent and received.'); | 45 }, 'Messages can be sent and received.'); |
46 | 46 |
47 promise_test(function(t) { | 47 sequential_promise_test(function(t) { |
48 var scope = sw_scope + '/echo-port'; | 48 var scope = sw_scope + '/echo-port'; |
49 var sw_url = 'resources/echo-worker.js'; | 49 var sw_url = 'resources/echo-worker.js'; |
50 var test_message = 'ping over navigator.connect'; | 50 var test_message = 'ping over navigator.connect'; |
51 var channel = new MessageChannel(); | 51 var channel = new MessageChannel(); |
52 return service_worker_unregister_and_register(t, sw_url, scope) | 52 return service_worker_unregister_and_register(t, sw_url, scope) |
53 .then(function(registration) { | 53 .then(function(registration) { |
54 return wait_for_state(t, registration.installing, 'activated'); | 54 return wait_for_state(t, registration.installing, 'activated'); |
55 }) | 55 }) |
56 .then(function() { | 56 .then(function() { |
57 return connect_method(t, scope + '/service'); | 57 return connect_method(t, scope + '/service'); |
58 }) | 58 }) |
59 .then(function(port) { | 59 .then(function(port) { |
60 channel.port1.postMessage(test_message); | 60 channel.port1.postMessage(test_message); |
61 port.postMessage({port: channel.port2}, [channel.port2]); | 61 port.postMessage({port: channel.port2}, [channel.port2]); |
62 return wait_for_reply(t, port); | 62 return wait_for_reply(t, port); |
63 }) | 63 }) |
64 .then(function(response) { | 64 .then(function(response) { |
65 assert_true('port' in response); | 65 assert_true('port' in response); |
66 assert_class_string(response.port, 'MessagePort'); | 66 assert_class_string(response.port, 'MessagePort'); |
67 return wait_for_reply(t, response.port); | 67 return wait_for_reply(t, response.port); |
68 }) | 68 }) |
69 .then(function(response) { | 69 .then(function(response) { |
70 assert_equals(response, test_message); | 70 assert_equals(response, test_message); |
71 return service_worker_unregister(t, scope); | 71 return service_worker_unregister(t, scope); |
72 }); | 72 }); |
73 }, 'Ports can be sent and received.'); | 73 }, 'Ports can be sent and received.'); |
74 | 74 |
75 promise_test(function(t) { | 75 sequential_promise_test(function(t) { |
76 var scope = sw_scope + '/reply-client-info'; | 76 var scope = sw_scope + '/reply-client-info'; |
77 var sw_url = 'resources/reply-client-info-worker.js'; | 77 var sw_url = 'resources/reply-client-info-worker.js'; |
78 var target_url = scope + '/service'; | 78 var target_url = scope + '/service'; |
79 var port; | 79 var port; |
80 return service_worker_unregister_and_register(t, sw_url, scope) | 80 return service_worker_unregister_and_register(t, sw_url, scope) |
81 .then(function(registration) { | 81 .then(function(registration) { |
82 return wait_for_state(t, registration.installing, 'activated'); | 82 return wait_for_state(t, registration.installing, 'activated'); |
83 }) | 83 }) |
84 .then(function() { | 84 .then(function() { |
85 return connect_method(t, target_url); | 85 return connect_method(t, target_url); |
86 }) | 86 }) |
87 .then(function(p) { | 87 .then(function(p) { |
88 port = p; | 88 port = p; |
89 return wait_for_reply(t, port) | 89 return wait_for_reply(t, port) |
90 }) | 90 }) |
91 .then(function(response) { | 91 .then(function(response) { |
92 assert_equals(response.targetUrl, | 92 assert_equals(response.targetUrl, |
93 location.origin + base_path() + target_url); | 93 location.origin + base_path() + target_url); |
94 assert_equals(response.origin, source_origin); | 94 assert_equals(response.origin, source_origin); |
95 var r = wait_for_reply(t, port); | 95 var r = wait_for_reply(t, port); |
96 port.postMessage('ping'); | 96 port.postMessage('ping'); |
97 return r; | 97 return r; |
98 }) | 98 }) |
99 .then(function(response) { | 99 .then(function(response) { |
100 assert_equals(response.origin, source_origin); | 100 assert_equals(response.origin, source_origin); |
101 return service_worker_unregister(t, scope); | 101 return service_worker_unregister(t, scope); |
102 }); | 102 }); |
103 }, 'Connect and message events include expected origin and targetUrl.'); | 103 }, 'Connect and message events include expected origin and targetUrl.'); |
104 } | 104 } |
OLD | NEW |