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

Side by Side Diff: LayoutTests/http/tests/navigatorconnect/resources/postmessage-tests.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: 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Runs various navigator.connect tests, verifying that message are sent and
2 // received correctly.
3 // |connect_method| is a function taking a test object as well as the regular
4 // navigator.connect parameters. This makes it possible to run the same tests
5 // while wrapping all navigator.connect calls in a helper making the actual
6 // connection from a cross origin iframe, or a web worker.
7 function run_postmessage_tests(source_origin, connect_method) {
8 // Helper method that waits for a reply on a port, and resolves a promise with
9 // the reply.
10 function waitForReply(t, port) {
11 return new Promise(function(resolve) {
12 var resolved = false;
13 port.onmessage = t.step_func(function(event) {
14 assert_false(resolved);
15 resolved = true;
16 resolve(event.data);
17 });
18 });
19 }
20
21 // FIXME: Current navigator.connect implementation returns origins with a
22 // trailing slash, which is probably not correct.
23 if (source_origin.charAt(source_origin.length - 1) != '/')
24 source_origin += '/';
25
26 promise_test(function(t) {
27 var scope = sw_scope + '/echo';
28 var sw_url = 'resources/echo-worker.js';
29 return service_worker_unregister_and_register(t, sw_url, scope)
30 .then(function(registration) {
31 return wait_for_state(t, registration.installing, 'activated');
32 })
33 .then(function() {
34 return connect_method(t, scope + '/service');
35 })
36 .then(function(port) {
37 port.postMessage('hello world');
38 return waitForReply(t, port);
39 })
40 .then(function(response) {
41 assert_equals(response, 'hello world');
42 return service_worker_unregister(t, scope);
43 });
44 }, 'Messages can be sent and received.');
45
46 promise_test(function(t) {
47 var scope = sw_scope + '/echo-port';
48 var sw_url = 'resources/echo-worker.js';
49 var channel = new MessageChannel();
50 return service_worker_unregister_and_register(t, sw_url, scope)
51 .then(function(registration) {
52 return wait_for_state(t, registration.installing, 'activated');
53 })
54 .then(function() {
55 return connect_method(t, scope + '/service');
56 })
57 .then(function(port) {
58 channel.port1.postMessage('hello world');
Jeffrey Yasskin 2014/12/20 00:31:51 I'd be happier with a more unique string here.
Marijn Kruisselbrink 2014/12/20 02:06:15 Done.
59 port.postMessage({port: channel.port2}, [channel.port2]);
60 return waitForReply(t, port);
61 })
62 .then(function(response) {
63 assert_true('port' in response);
64 assert_class_string(response.port, 'MessagePort');
65 return waitForReply(t, response.port);
66 })
67 .then(function(response) {
68 assert_equals(response, 'hello world');
69 return service_worker_unregister(t, scope);
70 });
71 }, 'Ports can be sent and received.');
72
73 promise_test(function(t) {
74 var scope = sw_scope + '/reply-client-info';
75 var sw_url = 'resources/reply-client-info-worker.js';
76 var full_target_url = location.origin + base_path() + scope + '/service';
Jeffrey Yasskin 2014/12/20 00:31:51 You're not re-using full_target_url, so I'd probab
Marijn Kruisselbrink 2014/12/20 02:06:15 Done.
77 var port;
78 return service_worker_unregister_and_register(t, sw_url, scope)
79 .then(function(registration) {
80 return wait_for_state(t, registration.installing, 'activated');
81 })
82 .then(function() {
83 return connect_method(t, scope + '/service');
84 })
85 .then(function(p) {
86 port = p;
87 return waitForReply(t, port)
88 })
89 .then(function(response) {
90 assert_equals(response.targetUrl, full_target_url);
91 assert_equals(response.origin, source_origin);
92 var r = waitForReply(t, port);
93 port.postMessage('ping');
94 return r;
95 })
96 .then(function(response) {
97 assert_equals(response.origin, source_origin);
98 return service_worker_unregister(t, scope);
99 });
100 }, 'Connect and message events include expected origin and targetUrl.');
101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698