OLD | NEW |
(Empty) | |
| 1 importScripts('worker-testharness.js'); |
| 2 |
| 3 // Helper method that waits for a reply on a port, and resolves a promise with |
| 4 // the reply. |
| 5 function wait_for_message_to_port(test, port) { |
| 6 return new Promise(function(resolve) { |
| 7 var resolved = false; |
| 8 port.onmessage = test.step_func(function(event) { |
| 9 assert_false(resolved); |
| 10 resolved = true; |
| 11 resolve(event.data); |
| 12 }); |
| 13 }); |
| 14 } |
| 15 |
| 16 // Similar helper method, but waits for a reply to a stashed port with a |
| 17 // particular name. |
| 18 function wait_for_message_to_stashed_port(test, name) { |
| 19 return new Promise(function(resolve) { |
| 20 var event_handler = function(e) { |
| 21 if (e.source.name === name) { |
| 22 self.ports.removeEventListener('message', event_handler); |
| 23 resolve(e); |
| 24 } |
| 25 }; |
| 26 self.ports.addEventListener('message', event_handler); |
| 27 }); |
| 28 } |
| 29 |
| 30 test(function(test) { |
| 31 var channel = new MessageChannel(); |
| 32 var name = 'somename'; |
| 33 var stashed_port = self.ports.add(name, channel.port1); |
| 34 assert_equals(stashed_port.name, name); |
| 35 }, 'Name set when adding port is set correctly.'); |
| 36 |
| 37 promise_test(function(test) { |
| 38 var channel = new MessageChannel(); |
| 39 var stashed_port = self.ports.add('', channel.port1); |
| 40 stashed_port.postMessage('pingy ping'); |
| 41 return wait_for_message_to_port(test, channel.port2) |
| 42 .then(test.step_func(function(reply) { |
| 43 assert_equals(reply, 'pingy ping'); |
| 44 })); |
| 45 }, 'Messages posted into stashed port arrive on other side.'); |
| 46 |
| 47 promise_test(function(test) { |
| 48 var channel = new MessageChannel(); |
| 49 var name = 'test_events'; |
| 50 var stashed_port = self.ports.add(name, channel.port1); |
| 51 channel.port2.postMessage('ping blah'); |
| 52 return wait_for_message_to_stashed_port(test, name) |
| 53 .then(test.step_func(function(reply) { |
| 54 assert_equals(reply.data, 'ping blah'); |
| 55 assert_equals(reply.source, stashed_port); |
| 56 })); |
| 57 }, 'Messages sent to stashed port arrive as global events.'); |
OLD | NEW |