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 src="resources/test-helpers.js"></script> |
| 11 <body> |
| 12 <script> |
| 13 var echo_service_url = 'chrome-layout-test:echo'; |
| 14 var annotate_service_url = 'chrome-layout-test:annotate'; |
| 15 var cross_origin = get_host_info().UNAUTHENTICATED_ORIGIN; |
| 16 |
| 17 // Helper method that waits for a reply on a port, and resolves a promise with |
| 18 // the reply. |
| 19 function wait_for_reply(test, port) { |
| 20 return new Promise(function(resolve) { |
| 21 var resolved = false; |
| 22 port.onmessage = test.step_func(function(event) { |
| 23 assert_false(resolved); |
| 24 resolved = true; |
| 25 resolve(event.data); |
| 26 }); |
| 27 }); |
| 28 } |
| 29 |
| 30 // Helper method that applies the same transformations that would happen when |
| 31 // roundtripping some value through the conversions that happen when a service |
| 32 // expects values as base::Value. |
| 33 // In particular since base::Value can't represent a date directly, converting a |
| 34 // Date to a base::Value converts it to the number of seconds since |
| 35 // 1 January 1970 00:00:00 UTC. Converting it back to javascript will then keep |
| 36 // it as a number. |
| 37 function convert_dates(key, value) { |
| 38 // Have to use |this[key]| here instead of |value| since |value| is already |
| 39 // stringified. |
| 40 if (this[key] instanceof Date) { |
| 41 return this[key].getTime() / 1000; |
| 42 } |
| 43 return value; |
| 44 } |
| 45 |
| 46 // Convert |actual| and |expected| to json, using |replacer| on |expected|, and |
| 47 // compares the resulting strings. |
| 48 function compare_as_json(replacer, actual, expected) { |
| 49 assert_equals(JSON.stringify(actual), JSON.stringify(expected, replacer)); |
| 50 } |
| 51 |
| 52 // Sends various messages to a port, and compares the response using the passed |
| 53 // in |compare| function. |
| 54 function test_sending_messages_with_port(port, compare, t) { |
| 55 // List of test messages that are send to the service. |
| 56 var test_messages = ['ping over n.c', |
| 57 1234, |
| 58 ["array", "test"], |
| 59 {obj: "test"}, |
| 60 {date: new Date(2015, 2, 20, 13, 10, 42, 0)}, |
| 61 new Date()]; |
| 62 var next_message = 0; |
| 63 |
| 64 function send_next_message(port) { |
| 65 if (next_message >= test_messages.length) return; |
| 66 var message = test_messages[next_message++]; |
| 67 var result = wait_for_reply(t, port); |
| 68 port.postMessage(message); |
| 69 return result.then(t.step_func(function(response) { |
| 70 compare(response, message); |
| 71 return send_next_message(port); |
| 72 })); |
| 73 } |
| 74 |
| 75 return send_next_message(port); |
| 76 } |
| 77 |
| 78 // Connects to the |target_url|, and then tries sending various messages. |
| 79 function test_sending_messages(target_url, compare, t) { |
| 80 return navigator.connect(target_url) |
| 81 .then(function(port) { |
| 82 return test_sending_messages_with_port(port, compare, t); |
| 83 }); |
| 84 } |
| 85 |
| 86 promise_test(function(t) { |
| 87 var target_url = echo_service_url + '?as-string'; |
| 88 // No special replacer needed, so bind undefined to the replacer parameter. |
| 89 var compare = compare_as_json.bind(undefined, undefined); |
| 90 return navigator.connect(target_url) |
| 91 .then(function(port) { |
| 92 return test_sending_messages_with_port(port, compare, t); |
| 93 }); |
| 94 }, 'Messages can be sent and received to a system service when sent as strings
.'); |
| 95 |
| 96 promise_test(function(t) { |
| 97 var target_url = echo_service_url + '?as-value'; |
| 98 var compare = compare_as_json.bind(undefined, convert_dates); |
| 99 return navigator.connect(target_url) |
| 100 .then(function(port) { |
| 101 return test_sending_messages_with_port(port, compare, t); |
| 102 }); |
| 103 }, 'Messages can be sent and received to a system service when sent as values.
'); |
| 104 |
| 105 promise_test(function(t) { |
| 106 function validate_reply(actual, expected) { |
| 107 compare_as_json(convert_dates, actual.message_as_value, [expected]); |
| 108 assert_equals(actual.message_as_string, ''); |
| 109 } |
| 110 var port; |
| 111 return navigator.connect(annotate_service_url + '?as-value') |
| 112 .then(function(p) { |
| 113 port = p; |
| 114 return wait_for_reply(t, port); |
| 115 }) |
| 116 .then(t.step_func(function(response) { |
| 117 assert_equals(response.origin, document.location.origin + '/'); |
| 118 return test_sending_messages_with_port(port, validate_reply, t); |
| 119 })); |
| 120 }, 'Messages are actually understandable by the browser when sent as values.')
; |
| 121 |
| 122 promise_test(function(t) { |
| 123 function validate_reply(actual, expected) { |
| 124 compare_as_json(undefined, actual.message_as_value, []); |
| 125 assert_equals(typeof actual.message_as_string, 'string'); |
| 126 assert_not_equals(actual.message_as_string, ''); |
| 127 } |
| 128 var port; |
| 129 return navigator.connect(annotate_service_url + '?as-string') |
| 130 .then(function(p) { |
| 131 port = p; |
| 132 return wait_for_reply(t, port); |
| 133 }) |
| 134 .then(t.step_func(function(response) { |
| 135 assert_equals(response.origin, document.location.origin + '/'); |
| 136 return test_sending_messages_with_port(port, validate_reply, t); |
| 137 })); |
| 138 }, 'Messages are only sent as values when the service asks for it.'); |
| 139 |
| 140 |
| 141 promise_test(function(t) { |
| 142 function validate_reply(actual, expected) { |
| 143 compare_as_json(convert_dates, actual.message_as_value, [expected]); |
| 144 assert_equals(actual.message_as_string, ''); |
| 145 } |
| 146 var port; |
| 147 return cross_origin_connect(t, annotate_service_url + '?as-value') |
| 148 .then(function(p) { |
| 149 port = p; |
| 150 return wait_for_reply(t, port); |
| 151 }) |
| 152 .then(t.step_func(function(response) { |
| 153 assert_equals(response.origin, cross_origin + '/'); |
| 154 return test_sending_messages_with_port(port, validate_reply, t); |
| 155 })); |
| 156 }, 'Messages are sent as values, even if port is transferred from iframe.'); |
| 157 |
| 158 </script> |
| 159 </body> |
OLD | NEW |