diff options
author | jstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-12 20:08:19 +0000 |
---|---|---|
committer | jstritar@chromium.org <jstritar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-12 20:08:19 +0000 |
commit | a3e99c05dea01a4fde57b3f6665a8465857e3284 (patch) | |
tree | ca177714f2ea97b74cd119762b6d27721ed9694f /chrome/test | |
parent | 4f4de7e6e1393bf1b068337cdf8895e91addfa72 (diff) | |
download | chromium_src-a3e99c05dea01a4fde57b3f6665a8465857e3284.zip chromium_src-a3e99c05dea01a4fde57b3f6665a8465857e3284.tar.gz chromium_src-a3e99c05dea01a4fde57b3f6665a8465857e3284.tar.bz2 |
Sets lastError when trying to connect or send a request to an invalid extension.
BUG=61998
TEST=ExtensionApiTest.Messaging
Review URL: http://codereview.chromium.org/4708006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@65977 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
3 files changed, 69 insertions, 11 deletions
diff --git a/chrome/test/data/extensions/api_test/messaging/connect/page.js b/chrome/test/data/extensions/api_test/messaging/connect/page.js index 9d45f64..d6c6950 100644 --- a/chrome/test/data/extensions/api_test/messaging/connect/page.js +++ b/chrome/test/data/extensions/api_test/messaging/connect/page.js @@ -1,18 +1,18 @@ JSON.parse = function() { return "JSON.parse clobbered by content script."; -} +}; JSON.stringify = function() { return "JSON.stringify clobbered by content script."; -} +}; Array.prototype.toJSON = function() { return "Array.prototype.toJSON clobbered by content script."; -} +}; Object.prototype.toJSON = function() { return "Object.prototype.toJSON clobbered by content script."; -} +}; // For complex connect tests. chrome.extension.onConnect.addListener(function(port) { @@ -31,6 +31,10 @@ chrome.extension.onConnect.addListener(function(port) { window.location = "about:blank"; } else if (msg.testPortName) { port.postMessage({portName:port.name}); + } else if (msg.testSendRequestFromTabError) { + testSendRequestFromTabError(); + } else if (msg.testConnectFromTabError) { + testConnectFromTabError(); } }); }); @@ -57,6 +61,24 @@ function testSendRequestFromTab() { }); } +// Tests sendRequest to an invalid extension. +function testSendRequestFromTabError() { + // try sending a request to a bad extension id + chrome.extension.sendRequest("bad-extension-id", {m: 1}, function(response) { + var success = (response === undefined && chrome.extension.lastError); + chrome.extension.sendRequest({success: success}); + }); +} + +// Tests connecting to an invalid extension. +function testConnectFromTabError() { + var port = chrome.extension.connect("bad-extension-id"); + port.onDisconnect.addListener(function() { + var success = (chrome.extension.lastError ? true : false); + chrome.extension.sendRequest({success: success}); + }); +} + // For test sendRequest. chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { sendResponse({success: (request.step2 == 1)}); diff --git a/chrome/test/data/extensions/api_test/messaging/connect/test.html b/chrome/test/data/extensions/api_test/messaging/connect/test.html index 9802697..2136d5c 100644 --- a/chrome/test/data/extensions/api_test/messaging/connect/test.html +++ b/chrome/test/data/extensions/api_test/messaging/connect/test.html @@ -1,19 +1,19 @@ <script> JSON.parse = function() { return "JSON.parse clobbered by extension."; -} +}; JSON.stringify = function() { return "JSON.stringify clobbered by extension."; -} +}; Array.prototype.toJSON = function() { return "Array.prototype.toJSON clobbered by extension."; -} +}; Object.prototype.toJSON = function() { return "Object.prototype.toJSON clobbered by extension."; -} +}; // Keep track of the tab that we're running tests in, for simplicity. var testTab = null; @@ -80,7 +80,8 @@ chrome.test.getConfig(function(config) { // Tests receiving a request from a content script and responding. function sendRequestFromTab() { - chrome.extension.onRequest.addListener( + var doneListening = chrome.test.listenForever( + chrome.extension.onRequest, function(request, sender, sendResponse) { chrome.test.assertTrue("url" in sender.tab, "no tab available."); chrome.test.assertEq(sender.id, location.host); @@ -92,7 +93,7 @@ chrome.test.getConfig(function(config) { // Step 2. chrome.test.assertEq(request.step, 2); sendResponse(); - chrome.test.succeed(); + doneListening(); } }); @@ -102,6 +103,40 @@ chrome.test.getConfig(function(config) { chrome.test.log("sendRequestFromTab: sent first message to tab"); }, + // Tests error handling when sending a request from a content script to an + // invalid extension. + function sendRequestFromTabError() { + chrome.test.listenOnce( + chrome.extension.onRequest, + function(request, sender, sendResponse) { + if (!request.success) + chrome.test.fail(); + } + ); + + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testSendRequestFromTabError: true}); + port.disconnect(); + chrome.test.log("testSendRequestFromTabError: send 1st message to tab"); + }, + + // Tests error handling when connecting to an invalid extension from a + // content script. + function connectFromTabError() { + chrome.test.listenOnce( + chrome.extension.onRequest, + function(request, sender, sendResponse) { + if (!request.success) + chrome.test.fail(); + } + ); + + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testConnectFromTabError: true}); + port.disconnect(); + chrome.test.log("testConnectFromTabError: sent 1st message to tab"); + }, + // Tests sending a request to a tab and receiving a response. function sendRequest() { chrome.tabs.sendRequest(testTab.id, {step2: 1}, function(response) { diff --git a/chrome/test/data/extensions/api_test/stubs/content_script.js b/chrome/test/data/extensions/api_test/stubs/content_script.js index cc34c8d..fde6fa8 100644 --- a/chrome/test/data/extensions/api_test/stubs/content_script.js +++ b/chrome/test/data/extensions/api_test/stubs/content_script.js @@ -73,7 +73,8 @@ function testPath(path, expectError) { // This is the last component - we expect it to either be defined or // to throw an error on access. try { - if (typeof(module[parts[i]]) == "undefined") { + if (typeof(module[parts[i]]) == "undefined" && + path != "extension.lastError") { logToConsoleAndStdout(" fail (undefined and not throwing error): " + path); return false; |