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