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