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

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: no find copies 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..14786f3510890f433201be9929b2d049cb9a76c8
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/messaging/externally_connectable/sites/assertions.js
@@ -0,0 +1,100 @@
+// 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: 5
+};
+
+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;
+ }
+
+ // 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) {
+ var responseJson = JSON.stringify(response);
+ var messageJson = JSON.stringify(message);
+ if (responseJson == messageJson)
+ return true;
+ console.warn('Expected ' + messageJson + ' got ' + responseJson);
+ reply(results.INCORRECT_RESPONSE);
+ return false;
+ }
+
+ 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;
+ port.onMessage.addListener(function(response) {
Jeffrey Yasskin 2013/06/08 00:28:09 Do you need to add this listener before posting yo
not at google - send to devlin 2013/06/08 02:02:33 JS is defined by its single threadedness.
+ pendingResponses--;
+ if (checkLastError(reply) &&
Jeffrey Yasskin 2013/06/08 00:28:09 If the first response produces a lastError, but th
not at google - send to devlin 2013/06/08 02:02:33 Done.
+ checkResponse(response, reply) &&
+ pendingResponses == 0) {
+ 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