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

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

Issue 812853002: Basic tests for sending messages over a navigator.connect initiated channel. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@navigator-connect_connect-tests
Patch Set: address Jeffrey's comments Created 6 years 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
new file mode 100644
index 0000000000000000000000000000000000000000..7a0a3a08f3ef7b2a50f99c02ef047be6a08b6d72
--- /dev/null
+++ b/LayoutTests/http/tests/navigatorconnect/resources/test-helpers.js
@@ -0,0 +1,47 @@
+// Helper method that waits for a {success: <boolean>, result: any} reply on
+// a port and returns a promise that resolves (if success is true) or rejects
+// the promise with the value of the result attribute.
+function reply_as_promise(t, port) {
+ return new Promise(function(resolve, reject) {
+ var got_reply = false;
+ port.onmessage = t.step_func(function(event) {
+ assert_false(got_reply);
+ assert_true('success' in event.data);
+ assert_true('result' in event.data);
+ got_reply = true;
+ if (event.data.success)
+ resolve(event.data.result);
+ else
+ reject(event.data.result);
+ });
+ });
+}
+
+// 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) {
+ // |service| is a relative URL, but for this to work from the iframe it needs
+ // an absolute URL.
+ var target_url = location.origin + base_path() + service;
+ return with_iframe(
+ cross_origin + base_path() + 'resources/connect-helper.html')
+ .then(function(iframe) {
+ var channel = new MessageChannel();
+ iframe.contentWindow.postMessage(
+ {connect: target_url, port: channel.port2}, '*', [channel.port2]);
+ return reply_as_promise(channel.port1);
+ });
+}
+
+// Method that behaves similarly to navigator.connect, but the actual connect
+// call is made from a worker.
+function connect_from_worker(t, service) {
+ // |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(channel.port1);
+}

Powered by Google App Engine
This is Rietveld 408576698