OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <title> | |
3 Tests posting messages over a navigator.connect initiated channel to a system | |
4 service. | |
5 </title> | |
6 <script src="../../resources/testharness.js"></script> | |
7 <script src="../../resources/testharnessreport.js"></script> | |
8 <script src="../../resources/testharness-helpers.js"></script> | |
9 <script src="../serviceworker/resources/test-helpers.js"></script> | |
10 <script> | |
11 var echo_service_url = 'chrome-layout-test:echo'; | |
12 var annotate_service_url = 'chrome-layout-test:annotate'; | |
13 | |
14 // Helper method that waits for a reply on a port, and resolves a promise with | |
15 // the reply. | |
16 function wait_for_reply(t, port) { | |
scheib
2015/02/26 23:02:32
Consider using 'test' instead of 't' for helper pa
Marijn Kruisselbrink
2015/02/27 23:04:58
Done.
| |
17 return new Promise(function(resolve) { | |
18 var resolved = false; | |
19 port.onmessage = t.step_func(function(event) { | |
20 assert_false(resolved); | |
21 resolved = true; | |
22 resolve(event.data); | |
23 }); | |
24 }); | |
25 } | |
26 | |
27 // Helper method that applies the same transformations that would happen when | |
28 // roundtripping some value through the conversions that happen when a service | |
29 // expects values as base::Value. | |
30 function convert_dates(k, v) { | |
scheib
2015/02/26 23:02:32
Use more descriptive parameter names than k, v.
Marijn Kruisselbrink
2015/02/27 23:04:58
Done.
| |
31 // Have to use this[k] here instead of v since v is already stringified. | |
32 if (this[k] instanceof Date) { | |
33 return this[k].getTime() / 1000; | |
scheib
2015/02/26 23:02:32
I think document more clearly, a Date object conve
Marijn Kruisselbrink
2015/02/27 23:04:58
I made this a bit more explicit. Not sure if direc
| |
34 } | |
35 return v; | |
36 } | |
37 | |
38 // Convert |actual| and |expected| to json, using |replacer| on |expected|, and | |
39 // compares the resulting strings. | |
40 function compare_as_json(replacer, actual, expected) { | |
41 assert_equals(JSON.stringify(actual), JSON.stringify(expected, replacer)); | |
42 } | |
43 | |
44 // Sends various messages to a port, and compares the response using the passed | |
45 // in |compare| function. | |
46 function test_sending_messages_with_port(port, compare, t) { | |
47 // List of test messages that are send to the service. | |
48 var test_messages = ['ping over n.c', | |
49 1234, | |
50 ["array", "test"], | |
51 {obj: "test"}, | |
52 {date: new Date(2015, 2, 20, 13, 10, 42, 0)}, | |
53 new Date()]; | |
54 var next_message = 0; | |
55 | |
56 function send_next_message(port) { | |
57 if (next_message >= test_messages.length) return; | |
58 var message = test_messages[next_message++]; | |
59 var result = wait_for_reply(t, port); | |
60 port.postMessage(message); | |
61 return result.then(t.step_func(function(response) { | |
62 compare(response, message); | |
63 return send_next_message(port); | |
64 })); | |
65 } | |
66 | |
67 return send_next_message(port); | |
68 } | |
69 | |
70 // Connects to the |target_url|, and then tries sending various messages. | |
71 function test_sending_messages(target_url, compare, t) { | |
72 return navigator.connect(target_url) | |
73 .then(function(port) { | |
74 return test_sending_messages_with_port(port, compare, t); | |
75 }); | |
76 } | |
77 | |
78 promise_test(test_sending_messages.bind(undefined, echo_service_url + '?as-strin g', | |
scheib
2015/02/26 23:02:32
The query strings '?as-string' '?as-value' don't s
Marijn Kruisselbrink
2015/02/27 23:04:58
The test wouldn't pass if it wouldn't be implement
scheib
2015/02/27 23:53:04
Doh, I had searched for as-string.
| |
79 compare_as_json.bind(undefined, undefine d)), | |
scheib
2015/02/26 23:02:32
Isn't the second undefined bound to compare_as_jso
Marijn Kruisselbrink
2015/02/27 23:04:58
the first undefined is the |this| for compare_as_j
scheib
2015/02/27 23:53:04
:P I was lost in line wrapped ,)(),,0()). Thanks,
| |
80 'Messages can be sent and received to a system service when sent as strings. '); | |
scheib
2015/02/26 23:02:32
Did 't' need to be passed in to test_sending_messa
Marijn Kruisselbrink
2015/02/27 23:04:58
promise_test calls its argument with 't', but as y
| |
81 | |
82 promise_test(test_sending_messages.bind(undefined, echo_service_url + '?as-value ', | |
scheib
2015/02/26 23:02:32
These may be cleaner to read by just using duplica
Marijn Kruisselbrink
2015/02/27 23:04:58
Done.
| |
83 compare_as_json.bind(undefined, convert_ dates)), | |
84 'Messages can be sent and received to a system service when sent as values.' ); | |
85 | |
86 promise_test(function(t) { | |
87 function validate_reply(actual, expected) { | |
88 compare_as_json(convert_dates, actual.message_as_value, [expected]); | |
89 assert_equals(actual.message_as_string, ''); | |
90 } | |
91 var port; | |
92 return navigator.connect(annotate_service_url + '?as-value') | |
93 .then(function(p) { | |
94 port = p; | |
95 return wait_for_reply(t, port); | |
96 }) | |
97 .then(t.step_func(function(response) { | |
98 assert_equals(response.origin, document.location.origin + '/'); | |
99 return test_sending_messages_with_port(port, validate_reply, t); | |
100 })); | |
101 }, 'Messages are actually understandable by the browser when sent as values.') ; | |
102 | |
103 promise_test(function(t) { | |
104 function validate_reply(actual, expected) { | |
105 compare_as_json(undefined, actual.message_as_value, []); | |
106 assert_equals(typeof actual.message_as_string, 'string'); | |
107 assert_not_equals(actual.message_as_string, ''); | |
108 } | |
109 var port; | |
110 return navigator.connect(annotate_service_url + '?as-string') | |
111 .then(function(p) { | |
112 port = p; | |
113 return wait_for_reply(t, port); | |
114 }) | |
115 .then(t.step_func(function(response) { | |
116 assert_equals(response.origin, document.location.origin + '/'); | |
117 return test_sending_messages_with_port(port, validate_reply, t); | |
118 })); | |
119 }, 'Messages are only send as values when the service asks for it.'); | |
scheib
2015/02/26 23:02:32
s/send/sent/
Marijn Kruisselbrink
2015/02/27 23:04:58
You'd think after so many times making the same mi
| |
120 | |
121 </script> | |
OLD | NEW |