Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8126)

Unified Diff: chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js

Issue 16174005: Implement externally_connectable! Web pages can now communicate directly with (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: absolute path... Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js
diff --git a/chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js b/chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js
new file mode 100644
index 0000000000000000000000000000000000000000..5a8b2692ea2d0d173f65d443357fe3451263408e
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js
@@ -0,0 +1,129 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+window.assertions = (function() {
+
+// Codes for test results. Must match ExternallyConnectableMessagingTest::Result
+// in c/b/extensions/extension_messages_apitest.cc.
+var results = {
+ OK: 0,
+ NAMESPACE_NOT_DEFINED: 1,
+ FUNCTION_NOT_DEFINED: 2,
+ COULD_NOT_ESTABLISH_CONNECTION_ERROR: 3,
+ OTHER_ERROR: 4,
+ INCORRECT_RESPONSE_SENDER: 5,
+ INCORRECT_RESPONSE_MESSAGE: 6
+};
+
+// Make the messages sent vaguely complex, but unambiguously JSON-ifiable.
+var message = [{'a': {'b': 10}}, 20, 'c\x10\x11'];
+
+function checkLastError(reply) {
+ if (!chrome.runtime.lastError)
+ return true;
+ var kCouldNotEstablishConnection =
+ 'Could not establish connection. Receiving end does not exist.';
+ if (chrome.runtime.lastError.message == kCouldNotEstablishConnection)
+ reply(results.COULD_NOT_ESTABLISH_CONNECTION_ERROR);
+ else
+ reply(results.OTHER_ERROR);
+ return false;
+}
+
+function checkResponse(response, reply) {
+ // The response will be an echo of both the original message *and* the
+ // MessageSender (with the tab field stripped down).
+ //
+ // First check the sender was correct.
+ var incorrectSender = false;
+ if (!response.sender.hasOwnProperty('tab')) {
+ console.warn('Expected a tab, got none');
+ incorrectSender = true;
+ }
+ if (response.sender.tab.url != document.location.href) {
+ console.warn('Expected tab url ' + document.location.href + ' got ' +
+ response.sender.tab.url);
+ incorrectSender = true;
+ }
+ if (response.sender.hasOwnProperty('id')) {
+ console.warn('Expected no id, got "' + response.sender.id + '"');
+ incorrectSender = true;
+ }
+ if (response.sender.url != document.location.href) {
+ console.warn('Expected url ' + document.location.href + ' got ' +
+ response.sender.url);
+ incorrectSender = true;
+ }
+ if (incorrectSender) {
+ reply(results.INCORRECT_RESPONSE_SENDER);
+ return false;
+ }
+
+ // Check the correct content was echoed.
+ var expectedJson = JSON.stringify(message);
+ var actualJson = JSON.stringify(response.message);
+ if (actualJson == expectedJson)
+ return true;
+ console.warn('Expected message ' + expectedJson + ' got ' + actualJson);
+ reply(results.INCORRECT_RESPONSE_MESSAGE);
+ return false;
+}
+
+return {
+ canConnectAndSendMessages: function(extensionId) {
+ if (!chrome.runtime) {
+ reply(results.NAMESPACE_NOT_DEFINED);
+ return;
+ }
+
+ if (!chrome.runtime.sendMessage || !chrome.runtime.connect) {
+ reply(results.FUNCTION_NOT_DEFINED);
+ return;
+ }
+
+ function canSendMessage(reply) {
+ chrome.runtime.sendMessage(extensionId, message, function(response) {
+ if (checkLastError(reply) && checkResponse(response, reply))
+ reply(results.OK);
+ });
+ }
+
+ function canConnectAndSendMessages(reply) {
+ var port = chrome.runtime.connect(extensionId);
+ port.postMessage(message, function() {
+ checkLastError(reply);
+ });
+ port.postMessage(message, function() {
+ checkLastError(reply);
+ });
+ var pendingResponses = 2;
+ var ok = true;
+ port.onMessage.addListener(function(response) {
+ pendingResponses--;
+ ok = ok && checkLastError(reply) && checkResponse(response, reply);
+ if (pendingResponses == 0 && ok)
+ reply(results.OK);
+ });
+ }
+
+ var done = domAutomationController.send.bind(domAutomationController);
+ canSendMessage(function(result) {
+ if (result != results.OK)
+ done(result);
+ else
+ canConnectAndSendMessages(done);
+ });
+ },
+
+ isDefined: function(name) {
+ var result = results.OK;
+ if (!chrome.runtime)
+ result = results.NAMESPACE_NOT_DEFINED;
+ else if (!chrome.runtime[name])
+ result = results.FUNCTION_NOT_DEFINED;
+ domAutomationController.send(result);
+ }
+};
+
+}()); // window.assertions

Powered by Google App Engine
This is Rietveld 408576698