Index: LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js |
diff --git a/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js b/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js |
index 9c40755d8c447f05997677e1b3a6d9b88f5f926a..29f26da74df6fae488740144d00b9962971af752 100644 |
--- a/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js |
+++ b/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js |
@@ -17,9 +17,11 @@ function reply_as_promise(t, port) { |
}); |
} |
-// Method that behaves similarly to navigator.connect, but the actual connect |
-// call is made from a cross origin iframe. |
-function cross_origin_connect(t, service) { |
+// Method that behaves similarly to navigator.services.connect, but the actual |
+// connect call is made from a cross origin iframe. Also the returned port is a |
+// MessagePort instead of a ServicePort, but with targetURL, name and data |
+// attributes set. |
+function cross_origin_connect(t, service, options) { |
// |service| could be a relative URL, but for this to work from the iframe it |
// needs an absolute URL. |
var target_url = new URL(service, location.origin + base_path()); |
@@ -28,52 +30,43 @@ function cross_origin_connect(t, service) { |
.then(function(iframe) { |
var channel = new MessageChannel(); |
iframe.contentWindow.postMessage( |
- {connect: target_url.href, port: channel.port2}, '*', [channel.port2]); |
+ {connect: target_url.href, port: channel.port2, options: options}, '*', [channel.port2]); |
return reply_as_promise(t, channel.port1); |
- }); |
+ }) |
+ .then(function(result) { |
+ var port = result.port; |
+ port.targetURL = result.targetURL; |
+ port.name = result.name; |
+ port.data = result.data; |
+ return port; |
+ }); |
} |
// Method that behaves similarly to navigator.connect, but the actual connect |
-// call is made from a worker. |
-function connect_from_worker(t, service) { |
+// call is made from a worker. Also the returned port is a MessagePort instead |
+// of a ServicePort, but with targetURL, name and data attributes set. |
+function connect_from_worker(t, service, options) { |
// |service| is a relative URL, but for this to work from the worker it needs |
// an absolute URL. |
var target_url = location.origin + base_path() + service; |
var worker = new Worker('resources/connect-helper.js'); |
var channel = new MessageChannel(); |
worker.postMessage |
- ({connect: target_url, port: channel.port2}, [channel.port2]); |
- return reply_as_promise(t, channel.port1); |
-} |
- |
-// Similar to Promise.race, except that returned promise only rejects if all |
-// passed promises reject. Used temporarily to support both old and new client |
-// side APIs. |
-function first_to_resolve(promises) { |
- return new Promise(function(resolve, reject) { |
- var remaining = promises.length; |
- var resolved = false; |
- for (var i = 0; i < promises.length; ++i) { |
- Promise.resolve(promises[i]) |
- .then(function(result) { |
- if (!resolved) { |
- resolve(result); |
- resolved = true; |
- } |
- }) |
- .catch(function(result) { |
- remaining--; |
- if (remaining === 0) { |
- reject(result); |
- } |
- }); |
- } |
- }); |
+ ({connect: target_url, port: channel.port2, options: options}, [channel.port2]); |
+ return reply_as_promise(t, channel.port1) |
+ .then(function(result) { |
+ var port = result.port; |
+ port.targetURL = result.targetURL; |
+ port.name = result.name; |
+ port.data = result.data; |
+ return port; |
+ }); |
} |
// Takes (a promise resolving to) a ServicePort instance, and returns a Promise |
-// that resolves to a MessagePort wrapping that ServicePort. Used to support |
-// both old and new APIs at the same time. |
+// that resolves to a MessagePort wrapping that ServicePort. Used to simplify |
+// testing code and to allow forwarding a connection from a cross origin iframe |
+// or worker to the main test runner. |
function wrap_in_port(maybe_port) { |
return Promise.resolve(maybe_port).then( |
function(port) { |
@@ -86,6 +79,9 @@ function wrap_in_port(maybe_port) { |
navigator.services.onmessage = function(event) { |
channel.port2.postMessage(event.data, event.ports); |
}; |
+ channel.port1.targetURL = port.targetURL; |
+ channel.port1.name = port.name; |
+ channel.port1.data = port.data; |
return channel.port1; |
} |
); |