OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 window.assertions = (function() { |
| 6 |
| 7 // Codes for test results. Must match ExternallyConnectableMessagingTest::Result |
| 8 // in c/b/extensions/extension_messages_apitest.cc. |
| 9 var results = { |
| 10 OK: 0, |
| 11 NAMESPACE_NOT_DEFINED: 1, |
| 12 FUNCTION_NOT_DEFINED: 2, |
| 13 COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3, |
| 14 OTHER_ERROR: 4, |
| 15 INCORRECT_RESPONSE_SENDER: 5, |
| 16 INCORRECT_RESPONSE_MESSAGE: 6 |
| 17 }; |
| 18 |
| 19 // Make the messages sent vaguely complex, but unambiguously JSON-ifiable. |
| 20 var message = [{'a': {'b': 10}}, 20, 'c\x10\x11']; |
| 21 |
| 22 function checkLastError(reply) { |
| 23 if (!chrome.runtime.lastError) |
| 24 return true; |
| 25 var kCouldNotEstablishConnection = |
| 26 'Could not establish connection. Receiving end does not exist.'; |
| 27 if (chrome.runtime.lastError.message == kCouldNotEstablishConnection) |
| 28 reply(results.COULD_NOT_ESTABLISH_CONNECTION_ERROR); |
| 29 else |
| 30 reply(results.OTHER_ERROR); |
| 31 return false; |
| 32 } |
| 33 |
| 34 function checkResponse(response, reply) { |
| 35 // The response will be an echo of both the original message *and* the |
| 36 // MessageSender (with the tab field stripped down). |
| 37 // |
| 38 // First check the sender was correct. |
| 39 var incorrectSender = false; |
| 40 if (!response.sender.hasOwnProperty('tab')) { |
| 41 console.warn('Expected a tab, got none'); |
| 42 incorrectSender = true; |
| 43 } |
| 44 if (response.sender.tab.url != document.location.href) { |
| 45 console.warn('Expected tab url ' + document.location.href + ' got ' + |
| 46 response.sender.tab.url); |
| 47 incorrectSender = true; |
| 48 } |
| 49 if (response.sender.hasOwnProperty('id')) { |
| 50 console.warn('Expected no id, got "' + response.sender.id + '"'); |
| 51 incorrectSender = true; |
| 52 } |
| 53 if (response.sender.url != document.location.href) { |
| 54 console.warn('Expected url ' + document.location.href + ' got ' + |
| 55 response.sender.url); |
| 56 incorrectSender = true; |
| 57 } |
| 58 if (incorrectSender) { |
| 59 reply(results.INCORRECT_RESPONSE_SENDER); |
| 60 return false; |
| 61 } |
| 62 |
| 63 // Check the correct content was echoed. |
| 64 var expectedJson = JSON.stringify(message); |
| 65 var actualJson = JSON.stringify(response.message); |
| 66 if (actualJson == expectedJson) |
| 67 return true; |
| 68 console.warn('Expected message ' + expectedJson + ' got ' + actualJson); |
| 69 reply(results.INCORRECT_RESPONSE_MESSAGE); |
| 70 return false; |
| 71 } |
| 72 |
| 73 return { |
| 74 canConnectAndSendMessages: function(extensionId) { |
| 75 if (!chrome.runtime) { |
| 76 reply(results.NAMESPACE_NOT_DEFINED); |
| 77 return; |
| 78 } |
| 79 |
| 80 if (!chrome.runtime.sendMessage || !chrome.runtime.connect) { |
| 81 reply(results.FUNCTION_NOT_DEFINED); |
| 82 return; |
| 83 } |
| 84 |
| 85 function canSendMessage(reply) { |
| 86 chrome.runtime.sendMessage(extensionId, message, function(response) { |
| 87 if (checkLastError(reply) && checkResponse(response, reply)) |
| 88 reply(results.OK); |
| 89 }); |
| 90 } |
| 91 |
| 92 function canConnectAndSendMessages(reply) { |
| 93 var port = chrome.runtime.connect(extensionId); |
| 94 port.postMessage(message, function() { |
| 95 checkLastError(reply); |
| 96 }); |
| 97 port.postMessage(message, function() { |
| 98 checkLastError(reply); |
| 99 }); |
| 100 var pendingResponses = 2; |
| 101 var ok = true; |
| 102 port.onMessage.addListener(function(response) { |
| 103 pendingResponses--; |
| 104 ok = ok && checkLastError(reply) && checkResponse(response, reply); |
| 105 if (pendingResponses == 0 && ok) |
| 106 reply(results.OK); |
| 107 }); |
| 108 } |
| 109 |
| 110 var done = domAutomationController.send.bind(domAutomationController); |
| 111 canSendMessage(function(result) { |
| 112 if (result != results.OK) |
| 113 done(result); |
| 114 else |
| 115 canConnectAndSendMessages(done); |
| 116 }); |
| 117 }, |
| 118 |
| 119 isDefined: function(name) { |
| 120 var result = results.OK; |
| 121 if (!chrome.runtime) |
| 122 result = results.NAMESPACE_NOT_DEFINED; |
| 123 else if (!chrome.runtime[name]) |
| 124 result = results.FUNCTION_NOT_DEFINED; |
| 125 domAutomationController.send(result); |
| 126 } |
| 127 }; |
| 128 |
| 129 }()); // window.assertions |
OLD | NEW |