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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Helper method that waits for a {success: <boolean>, result: any} reply on 1 // Helper method that waits for a {success: <boolean>, result: any} reply on
2 // a port and returns a promise that resolves (if success is true) or rejects 2 // a port and returns a promise that resolves (if success is true) or rejects
3 // the promise with the value of the result attribute. 3 // the promise with the value of the result attribute.
4 function reply_as_promise(t, port) { 4 function reply_as_promise(t, port) {
5 return new Promise(function(resolve, reject) { 5 return new Promise(function(resolve, reject) {
6 var got_reply = false; 6 var got_reply = false;
7 port.onmessage = t.step_func(function(event) { 7 port.onmessage = t.step_func(function(event) {
8 assert_false(got_reply); 8 assert_false(got_reply);
9 assert_true('success' in event.data); 9 assert_true('success' in event.data);
10 assert_true('result' in event.data); 10 assert_true('result' in event.data);
(...skipping 27 matching lines...) Expand all
38 function connect_from_worker(t, service) { 38 function connect_from_worker(t, service) {
39 // |service| is a relative URL, but for this to work from the worker it needs 39 // |service| is a relative URL, but for this to work from the worker it needs
40 // an absolute URL. 40 // an absolute URL.
41 var target_url = location.origin + base_path() + service; 41 var target_url = location.origin + base_path() + service;
42 var worker = new Worker('resources/connect-helper.js'); 42 var worker = new Worker('resources/connect-helper.js');
43 var channel = new MessageChannel(); 43 var channel = new MessageChannel();
44 worker.postMessage 44 worker.postMessage
45 ({connect: target_url, port: channel.port2}, [channel.port2]); 45 ({connect: target_url, port: channel.port2}, [channel.port2]);
46 return reply_as_promise(t, channel.port1); 46 return reply_as_promise(t, channel.port1);
47 } 47 }
48
49 // Similar to Promise.race, except that returned promise only rejects if all
50 // passed promises reject. Used temporarily to support both old and new client
51 // side APIs.
52 function first_to_resolve(promises) {
53 return new Promise(function(resolve, reject) {
54 var remaining = promises.length;
55 var resolved = false;
56 for (var i = 0; i < promises.length; ++i) {
57 Promise.resolve(promises[i])
58 .then(function(result) {
59 if (!resolved) {
60 resolve(result);
61 resolved = true;
62 }
63 })
64 .catch(function(result) {
65 remaining--;
66 if (remaining === 0) {
67 reject(result);
68 }
69 });
70 }
71 });
72 }
73
74 // Takes (a promise resolving to) a ServicePort instance, and returns a Promise
75 // that resolves to a MessagePort wrapping that ServicePort. Used to support
76 // both old and new APIs at the same time.
77 function wrap_in_port(maybe_port) {
78 return Promise.resolve(maybe_port).then(
79 function(port) {
80 var channel = new MessageChannel();
81 channel.port2.onmessage = function(event) {
82 port.postMessage(event.data, event.ports);
83 };
84 // Should use addEventListener and check source of event, but source isn't
85 // set yet, so for now just assume only one wrapped port is used at a time .
86 navigator.services.onmessage = function(event) {
87 channel.port2.postMessage(event.data, event.ports);
88 };
89 return channel.port1;
90 }
91 );
92 }
93
94 var promise_tests = Promise.resolve();
95 // Helper function to run promise tests one after the other.
96 // TODO(ortuno): Remove once https://github.com/w3c/testharness.js/pull/115/file s
97 // gets through.
98 function sequential_promise_test(func, name) {
99 var test = async_test(name);
100 promise_tests = promise_tests.then(function() {
101 return test.step(func, test, test);
102 }).then(function() {
103 test.done();
104 }).catch(test.step_func(function(value) {
105 // step_func catches the error again so the error doesn't propagate.
106 throw value;
107 }));
108 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698