Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Unified Diff: LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js

Issue 1191393003: Update client side navigator.connect API to use ServicePortCollection [1/3] (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@serviceport
Patch Set: address more comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 fe4a83276406e2e60b59e5f4029fcc0074f956c2..9c40755d8c447f05997677e1b3a6d9b88f5f926a 100644
--- a/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js
+++ b/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js
@@ -45,3 +45,64 @@ function connect_from_worker(t, service) {
({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);
+ }
+ });
+ }
+ });
+}
+
+// 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.
+function wrap_in_port(maybe_port) {
+ return Promise.resolve(maybe_port).then(
+ function(port) {
+ var channel = new MessageChannel();
+ channel.port2.onmessage = function(event) {
+ port.postMessage(event.data, event.ports);
+ };
+ // Should use addEventListener and check source of event, but source isn't
+ // set yet, so for now just assume only one wrapped port is used at a time.
+ navigator.services.onmessage = function(event) {
+ channel.port2.postMessage(event.data, event.ports);
+ };
+ return channel.port1;
+ }
+ );
+}
+
+var promise_tests = Promise.resolve();
+// Helper function to run promise tests one after the other.
+// TODO(ortuno): Remove once https://github.com/w3c/testharness.js/pull/115/files
+// gets through.
+function sequential_promise_test(func, name) {
+ var test = async_test(name);
+ promise_tests = promise_tests.then(function() {
+ return test.step(func, test, test);
+ }).then(function() {
+ test.done();
+ }).catch(test.step_func(function(value) {
+ // step_func catches the error again so the error doesn't propagate.
+ throw value;
+ }));
+}

Powered by Google App Engine
This is Rietveld 408576698