Index: LayoutTests/http/tests/navigatorconnect/system-service.html |
diff --git a/LayoutTests/http/tests/navigatorconnect/system-service.html b/LayoutTests/http/tests/navigatorconnect/system-service.html |
new file mode 100644 |
index 0000000000000000000000000000000000000000..94d61af8d4b31add5943647e46b7bddfec642638 |
--- /dev/null |
+++ b/LayoutTests/http/tests/navigatorconnect/system-service.html |
@@ -0,0 +1,121 @@ |
+<!DOCTYPE html> |
+<title> |
+ Tests posting messages over a navigator.connect initiated channel to a system |
+ service. |
+</title> |
+<script src="../../resources/testharness.js"></script> |
+<script src="../../resources/testharnessreport.js"></script> |
+<script src="../../resources/testharness-helpers.js"></script> |
+<script src="../serviceworker/resources/test-helpers.js"></script> |
+<script> |
+var echo_service_url = 'chrome-layout-test:echo'; |
+var annotate_service_url = 'chrome-layout-test:annotate'; |
+ |
+// Helper method that waits for a reply on a port, and resolves a promise with |
+// the reply. |
+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.
|
+ return new Promise(function(resolve) { |
+ var resolved = false; |
+ port.onmessage = t.step_func(function(event) { |
+ assert_false(resolved); |
+ resolved = true; |
+ resolve(event.data); |
+ }); |
+ }); |
+} |
+ |
+// Helper method that applies the same transformations that would happen when |
+// roundtripping some value through the conversions that happen when a service |
+// expects values as base::Value. |
+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.
|
+ // Have to use this[k] here instead of v since v is already stringified. |
+ if (this[k] instanceof Date) { |
+ 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
|
+ } |
+ return v; |
+} |
+ |
+// Convert |actual| and |expected| to json, using |replacer| on |expected|, and |
+// compares the resulting strings. |
+function compare_as_json(replacer, actual, expected) { |
+ assert_equals(JSON.stringify(actual), JSON.stringify(expected, replacer)); |
+} |
+ |
+// Sends various messages to a port, and compares the response using the passed |
+// in |compare| function. |
+function test_sending_messages_with_port(port, compare, t) { |
+ // List of test messages that are send to the service. |
+ var test_messages = ['ping over n.c', |
+ 1234, |
+ ["array", "test"], |
+ {obj: "test"}, |
+ {date: new Date(2015, 2, 20, 13, 10, 42, 0)}, |
+ new Date()]; |
+ var next_message = 0; |
+ |
+ function send_next_message(port) { |
+ if (next_message >= test_messages.length) return; |
+ var message = test_messages[next_message++]; |
+ var result = wait_for_reply(t, port); |
+ port.postMessage(message); |
+ return result.then(t.step_func(function(response) { |
+ compare(response, message); |
+ return send_next_message(port); |
+ })); |
+ } |
+ |
+ return send_next_message(port); |
+} |
+ |
+// Connects to the |target_url|, and then tries sending various messages. |
+function test_sending_messages(target_url, compare, t) { |
+ return navigator.connect(target_url) |
+ .then(function(port) { |
+ return test_sending_messages_with_port(port, compare, t); |
+ }); |
+} |
+ |
+promise_test(test_sending_messages.bind(undefined, echo_service_url + '?as-string', |
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.
|
+ compare_as_json.bind(undefined, undefined)), |
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,
|
+ '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
|
+ |
+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.
|
+ compare_as_json.bind(undefined, convert_dates)), |
+ 'Messages can be sent and received to a system service when sent as values.'); |
+ |
+promise_test(function(t) { |
+ function validate_reply(actual, expected) { |
+ compare_as_json(convert_dates, actual.message_as_value, [expected]); |
+ assert_equals(actual.message_as_string, ''); |
+ } |
+ var port; |
+ return navigator.connect(annotate_service_url + '?as-value') |
+ .then(function(p) { |
+ port = p; |
+ return wait_for_reply(t, port); |
+ }) |
+ .then(t.step_func(function(response) { |
+ assert_equals(response.origin, document.location.origin + '/'); |
+ return test_sending_messages_with_port(port, validate_reply, t); |
+ })); |
+ }, 'Messages are actually understandable by the browser when sent as values.'); |
+ |
+promise_test(function(t) { |
+ function validate_reply(actual, expected) { |
+ compare_as_json(undefined, actual.message_as_value, []); |
+ assert_equals(typeof actual.message_as_string, 'string'); |
+ assert_not_equals(actual.message_as_string, ''); |
+ } |
+ var port; |
+ return navigator.connect(annotate_service_url + '?as-string') |
+ .then(function(p) { |
+ port = p; |
+ return wait_for_reply(t, port); |
+ }) |
+ .then(t.step_func(function(response) { |
+ assert_equals(response.origin, document.location.origin + '/'); |
+ return test_sending_messages_with_port(port, validate_reply, t); |
+ })); |
+ }, '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
|
+ |
+</script> |