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