diff options
author | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-08 23:14:12 +0000 |
---|---|---|
committer | mpcomplete@chromium.org <mpcomplete@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-08 23:14:12 +0000 |
commit | 7a379f0eec9bbaeeb0d68c06de5d3473b18ea50d (patch) | |
tree | 6cd83a8c4f7dabeab1eb44afd612a3ec264302d1 | |
parent | a96af9d3a9feb46c463399438d9687c7c10a9451 (diff) | |
download | chromium_src-7a379f0eec9bbaeeb0d68c06de5d3473b18ea50d.zip chromium_src-7a379f0eec9bbaeeb0d68c06de5d3473b18ea50d.tar.gz chromium_src-7a379f0eec9bbaeeb0d68c06de5d3473b18ea50d.tar.bz2 |
Move the extension messaging tests to be ExtensionApiTests. Remove the old
tests.
Review URL: http://codereview.chromium.org/519087
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35843 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 154 insertions, 234 deletions
diff --git a/chrome/browser/extensions/extension_browsertests_misc.cc b/chrome/browser/extensions/extension_browsertests_misc.cc index 39fca8261..ee5935c 100644 --- a/chrome/browser/extensions/extension_browsertests_misc.cc +++ b/chrome/browser/extensions/extension_browsertests_misc.cc @@ -493,48 +493,6 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, ParseFeedValidFeedNoLinks) { "No error"); } -// Tests that message passing between extensions and tabs works. -IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_MessagingExtensionTab) { - ASSERT_TRUE(LoadExtension( - test_data_dir_.AppendASCII("good").AppendASCII("Extensions") - .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") - .AppendASCII("1.0"))); - - // Get the ExtensionHost that is hosting our toolstrip page. - ExtensionProcessManager* manager = - browser()->profile()->GetExtensionProcessManager(); - ExtensionHost* host = FindHostWithPath(manager, "/toolstrip.html", 1); - - // Load the tab that will communicate with our toolstrip. - ui_test_utils::NavigateToURL( - browser(), - GURL("chrome-extension://bjafgdebaacbbbecmhlhpofkepfkgcpa/page.html")); - - // Test extension->tab messaging. - bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testPostMessage()", &result); - EXPECT_TRUE(result); - - // Test tab->extension messaging. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testPostMessageFromTab()", &result); - EXPECT_TRUE(result); - - // Test disconnect event dispatch. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testDisconnect()", &result); - EXPECT_TRUE(result); - - // Test disconnect is fired on tab close. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testDisconnectOnClose()", &result); - EXPECT_TRUE(result); -} - // Tests that an error raised during an async function still fires // the callback, but sets chrome.extension.lastError. IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) { @@ -552,65 +510,6 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, LastError) { EXPECT_TRUE(result); } -// Tests that message passing between extensions and content scripts works. -IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DISABLED_MessagingContentScript) { - HTTPTestServer* server = StartHTTPServer(); - - ASSERT_TRUE(LoadExtension( - test_data_dir_.AppendASCII("good").AppendASCII("Extensions") - .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") - .AppendASCII("1.0"))); - - UserScriptMaster* master = browser()->profile()->GetUserScriptMaster(); - if (!master->ScriptsReady()) { - // Wait for UserScriptMaster to finish its scan. - NotificationRegistrar registrar; - registrar.Add(this, NotificationType::USER_SCRIPTS_UPDATED, - NotificationService::AllSources()); - ui_test_utils::RunMessageLoop(); - } - ASSERT_TRUE(master->ScriptsReady()); - - // Get the ExtensionHost that is hosting our toolstrip page. - ExtensionProcessManager* manager = - browser()->profile()->GetExtensionProcessManager(); - ExtensionHost* host = FindHostWithPath(manager, "/toolstrip.html", 1); - - // Load the tab whose content script will communicate with our toolstrip. - GURL url = server->TestServerPageW(kTestFile); - ui_test_utils::NavigateToURL(browser(), url); - - // Test extension->tab messaging. - bool result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testPostMessage()", &result); - EXPECT_TRUE(result); - - // Test port naming. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testPortName()", &result); - EXPECT_TRUE(result); - - // Test tab->extension messaging. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testPostMessageFromTab()", &result); - EXPECT_TRUE(result); - - // Test disconnect event dispatch. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testDisconnect()", &result); - EXPECT_TRUE(result); - - // Test disconnect is fired on tab close. - result = false; - ui_test_utils::ExecuteJavaScriptAndExtractBool( - host->render_view_host(), L"", L"testDisconnectOnClose()", &result); - EXPECT_TRUE(result); -} - // TODO(mpcomplete): reenable after figuring it out. #if 0 // Tests the process of updating an extension to one that requires higher @@ -670,7 +569,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DisableEnable) { ExtensionProcessManager* manager = browser()->profile()->GetExtensionProcessManager(); - // Load an extension, expect the toolstrip to be available. + // Load an extension, expect the background page to be available. ASSERT_FALSE(service->HasInstalledExtensions()); ASSERT_TRUE(LoadExtension( test_data_dir_.AppendASCII("good").AppendASCII("Extensions") @@ -678,21 +577,21 @@ IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, DisableEnable) { .AppendASCII("1.0"))); EXPECT_EQ(1u, service->extensions()->size()); EXPECT_EQ(0u, service->disabled_extensions()->size()); - EXPECT_TRUE(FindHostWithPath(manager, "/toolstrip.html", 1)); + EXPECT_TRUE(FindHostWithPath(manager, "/background.html", 1)); ASSERT_TRUE(service->HasInstalledExtensions()); - // After disabling, the toolstrip should go away. + // After disabling, the background page should go away. service->DisableExtension("bjafgdebaacbbbecmhlhpofkepfkgcpa"); EXPECT_EQ(0u, service->extensions()->size()); EXPECT_EQ(1u, service->disabled_extensions()->size()); - EXPECT_FALSE(FindHostWithPath(manager, "/toolstrip.html", 0)); + EXPECT_FALSE(FindHostWithPath(manager, "/background.html", 0)); ASSERT_TRUE(service->HasInstalledExtensions()); // And bring it back. service->EnableExtension("bjafgdebaacbbbecmhlhpofkepfkgcpa"); EXPECT_EQ(1u, service->extensions()->size()); EXPECT_EQ(0u, service->disabled_extensions()->size()); - EXPECT_TRUE(FindHostWithPath(manager, "/toolstrip.html", 1)); + EXPECT_TRUE(FindHostWithPath(manager, "/background.html", 1)); ASSERT_TRUE(service->HasInstalledExtensions()); } diff --git a/chrome/browser/extensions/extensions_service_unittest.cc b/chrome/browser/extensions/extensions_service_unittest.cc index 95748b7..c68f808 100644 --- a/chrome/browser/extensions/extensions_service_unittest.cc +++ b/chrome/browser/extensions/extensions_service_unittest.cc @@ -627,7 +627,7 @@ TEST_F(ExtensionsServiceTest, LoadAllExtensionsFromDirectorySuccess) { EXPECT_EQ(std::string(good2), loaded_[2]->id()); EXPECT_EQ(std::string("My extension 3"), loaded_[2]->name()); EXPECT_EQ(std::string(""), loaded_[2]->description()); - EXPECT_EQ(1u, loaded_[2]->content_scripts().size()); + EXPECT_EQ(0u, loaded_[2]->content_scripts().size()); EXPECT_EQ(Extension::INTERNAL, loaded_[2]->location()); }; 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 2f737f70..b876f7e 100644 --- a/chrome/test/data/extensions/api_test/messaging/connect/page.js +++ b/chrome/test/data/extensions/api_test/messaging/connect/page.js @@ -1,9 +1,46 @@ -// For test onRequest. -chrome.extension.sendRequest({step: 1}, function(response) { - if (response.nextStep) - chrome.extension.sendRequest({step: 2}); +// For complex connect tests. +chrome.extension.onConnect.addListener(function(port) { + console.log('connected'); + port.onMessage.addListener(function(msg) { + console.log('got ' + msg); + if (msg.testPostMessage) { + port.postMessage({success: true}); + } else if (msg.testPostMessageFromTab) { + testPostMessageFromTab(port); + } else if (msg.testSendRequestFromTab) { + testSendRequestFromTab(); + } else if (msg.testDisconnect) { + port.disconnect(); + } else if (msg.testDisconnectOnClose) { + window.location = "about:blank"; + } else if (msg.testPortName) { + port.postMessage({portName:port.name}); + } + }); }); +// Tests that postMessage to the extension and its response works. +function testPostMessageFromTab(origPort) { + var portName = "peter"; + var port = chrome.extension.connect({name: portName}); + port.postMessage({testPostMessageFromTab: true}); + port.onMessage.addListener(function(msg) { + origPort.postMessage({success: (msg.success && (msg.portName == portName))}); + console.log('testPostMessageFromTab sent ' + msg.success); + port.disconnect(); + }); +} + +// For test onRequest. +function testSendRequestFromTab() { + chrome.extension.sendRequest({step: 1}, function(response) { + if (response.nextStep) { + console.log('testSendRequestFromTab sent'); + chrome.extension.sendRequest({step: 2}); + } + }); +} + // 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 b108a8d..3e24916 100644 --- a/chrome/test/data/extensions/api_test/messaging/connect/test.html +++ b/chrome/test/data/extensions/api_test/messaging/connect/test.html @@ -1,36 +1,115 @@ <script> +// Keep track of the tab that we're running tests in, for simplicity. +var testTab = null; + chrome.test.runTests([ + function setupTestTab() { + chrome.test.log("Creating tab..."); + chrome.tabs.create({ + url: "http://localhost:1337/files/extensions/test_file.html" + }, function(tab) { + chrome.tabs.onUpdated.addListener(function listener(tabid, info) { + if (tab.id == tabid && info.status == 'complete') { + chrome.test.log("Created tab: " + tab.url); + chrome.tabs.onUpdated.removeListener(listener); + testTab = tab; + chrome.test.succeed(); + } + }); + }); + }, + + // Tests that postMessage to the tab and its response works. + function postMessage() { + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testPostMessage: true}); + port.onMessage.addListener(function(msg) { + port.disconnect(); + chrome.test.succeed(); + }); + }, + + // Tests that port name is sent & received correctly. + function portName() { + var portName = "lemonjello"; + var port = chrome.tabs.connect(testTab.id, {name: portName}); + port.postMessage({testPortName: true}); + port.onMessage.addListener(function(msg) { + chrome.test.assertEq(msg.portName, portName); + port.disconnect(); + chrome.test.succeed(); + }); + }, + + // Tests that postMessage from the tab and its response works. + function postMessageFromTab() { + chrome.extension.onConnect.addListener(function(port) { + port.onMessage.addListener(function(msg) { + chrome.test.assertTrue(msg.testPostMessageFromTab); + port.postMessage({success: true, portName: port.name}); + chrome.test.log("postMessageFromTab: got message from tab"); + }); + }); + + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testPostMessageFromTab: true}); + chrome.test.log("postMessageFromTab: sent first message to tab"); + port.onMessage.addListener(function(msg) { + port.disconnect(); + chrome.test.succeed(); + }); + }, + // Tests receiving a request from a content script and responding. - function onRequest() { + function sendRequestFromTab() { chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { chrome.test.assertTrue("url" in sender.tab, "no tab available."); chrome.test.assertEq(sender.id, location.host); if (request.step == 1) { // Step 1: Page should send another request for step 2. + chrome.test.log("sendRequestFromTab: got step 1"); sendResponse({nextStep: true}); } else { // Step 2. chrome.test.assertEq(request.step, 2); - sendResponse({}); + sendResponse(); chrome.test.succeed(); } - }); + }); + + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testSendRequestFromTab: true}); + port.disconnect(); + chrome.test.log("sendRequestFromTab: sent first message to tab"); }, + // Tests sending a request to a tab and receiving a response. function sendRequest() { - chrome.tabs.getSelected(null, function(tab) { - chrome.test.log("Selected tab: " + tab.url); - chrome.tabs.sendRequest(tab.id, {step2: 1}, function(response) { - chrome.test.assertTrue(response.success); - chrome.test.succeed(); - }); + chrome.tabs.sendRequest(testTab.id, {step2: 1}, function(response) { + chrome.test.assertTrue(response.success); + chrome.test.succeed(); }); - } + }, + + // Tests that we get the disconnect event when the tab disconnect. + function disconnect() { + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testDisconnect: true}); + port.onDisconnect.addListener(function() { + chrome.test.succeed(); + }); + }, + + // Tests that we get the disconnect event when the tab context closes. + function disconnectOnClose() { + var port = chrome.tabs.connect(testTab.id); + port.postMessage({testDisconnectOnClose: true}); + port.onDisconnect.addListener(function() { + chrome.test.succeed(); + testTab = null; // the tab is about:blank now. + }); + }, ]); -chrome.test.log("Creating tab..."); -chrome.tabs.create({ - url: "http://localhost:1337/files/extensions/test_file.html" -}); </script> diff --git a/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/background.html b/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/background.html new file mode 100644 index 0000000..4f8dda3 --- /dev/null +++ b/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/background.html @@ -0,0 +1,9 @@ +<script> +chrome.extension.onConnectExternal.addListener(function(port) { + port.onMessage.addListener(function(msg) { + if (msg.testConnectExternal) { + port.postMessage({success: true, senderId: port.sender.id}); + } + }); +}); +</script> diff --git a/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/manifest.json b/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/manifest.json index d87557c..37b7cd5 100644 --- a/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/manifest.json +++ b/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/manifest.json @@ -2,12 +2,6 @@ "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRS2GUBOUAO5VZ2CMRId/eRR8/e9V42nUvY5XG+0sZ+JDHEjIQdq8qQy7HqdqEpCXKPMSPuMiC2t2HE9/hpL89SblNn3mwYPtSJGQdZvAzuv6SB0oA6jZ66V7+h/k0noGD3Tcu+Ko/vfkt5wCx2uHVK29k5JR/vGr0klaoVezGlwIDAQAB", "version": "1.0", "name": "My extension 3", - "toolstrips": ["toolstrip.html"], - "permissions": ["tabs"], - "content_scripts": [ - { - "matches": ["http://localhost/*"], - "js": ["page.js"] - } - ] + "background_page": "background.html", + "permissions": ["tabs"] } diff --git a/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/toolstrip.html b/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/toolstrip.html deleted file mode 100644 index fb4c2eb..0000000 --- a/chrome/test/data/extensions/good/Extensions/bjafgdebaacbbbecmhlhpofkepfkgcpa/1.0/toolstrip.html +++ /dev/null @@ -1,87 +0,0 @@ -<script> -window.onload = function() { - chrome.extension.onConnect.addListener(function(port) { - console.log('onConnect'); - port.onMessage.addListener(function(msg) { - console.log('got message'); - if (msg.testPostMessageFromTab) { - port.postMessage({success: true, portName: port.name}); - console.log('sent success'); - } - // Ignore other messages since they are from us. - }); - }); - chrome.extension.onConnectExternal.addListener(function(port) { - port.onMessage.addListener(function(msg) { - if (msg.testConnectExternal) { - port.postMessage({success: true, senderId: port.sender.id}); - } - }); - }); -}; - -// Tests that postMessage to the tab and its response works. -function testPostMessage() { - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id); - console.log('connect to ' + tab.id); - port.postMessage({testPostMessage: true}); - port.onMessage.addListener(function(msg) { - window.domAutomationController.send(msg.success); - port.disconnect(); - }); - }); -} - -// Tests that port name is sent & received correctly. -function testPortName() { - chrome.tabs.getSelected(null, function(tab) { - var portName = "lemonjello"; - var port = chrome.tabs.connect(tab.id, {name: portName}); - console.log('naming port ' + portName); - port.postMessage({testPortName: true}); - port.onMessage.addListener(function(msg) { - console.log('got name ' + msg.portName); - window.domAutomationController.send(msg.portName == portName); - port.disconnect(); - }); - }); -} - -// Tests that postMessage from the tab and its response works. -function testPostMessageFromTab() { - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id); - console.log('connect to ' + tab.id); - port.postMessage({testPostMessageFromTab: true}); - port.onMessage.addListener(function(msg) { - window.domAutomationController.send(msg.success); - port.disconnect(); - }); - }); -} - -// Tests that we get the disconnect event when the tab disconnect. -function testDisconnect() { - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id); - console.log('connect to ' + tab.id); - port.postMessage({testDisconnect: true}); - port.onDisconnect.addListener(function() { - window.domAutomationController.send(true); - }); - }); -} - -// Tests that we get the disconnect event when the tab context closes. -function testDisconnectOnClose() { - chrome.tabs.getSelected(null, function(tab) { - var port = chrome.tabs.connect(tab.id); - console.log('connect to ' + tab.id); - port.postMessage({testDisconnectOnClose: true}); - port.onDisconnect.addListener(function() { - window.domAutomationController.send(true); - }); - }); -} -</script> diff --git a/chrome/test/data/extensions/good/Preferences b/chrome/test/data/extensions/good/Preferences index 42a2713..7aad8c2 100644 --- a/chrome/test/data/extensions/good/Preferences +++ b/chrome/test/data/extensions/good/Preferences @@ -46,14 +46,8 @@ "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRS2GUBOUAO5VZ2CMRId/eRR8/e9V42nUvY5XG+0sZ+JDHEjIQdq8qQy7HqdqEpCXKPMSPuMiC2t2HE9/hpL89SblNn3mwYPtSJGQdZvAzuv6SB0oA6jZ66V7+h/k0noGD3Tcu+Ko/vfkt5wCx2uHVK29k5JR/vGr0klaoVezGlwIDAQAB", "version": "1.0", "name": "My extension 3", - "toolstrips": ["toolstrip.html"], - "permissions": ["tabs"], - "content_scripts": [ - { - "matches": ["file://*"], - "js": ["page.js"] - } - ] + "background_page": "background.html", + "permissions": ["tabs"] } }, "hpiknbiabeeppbpihjehijgoemciehgk": { diff --git a/chrome/test/data/extensions/ui/create_extension_detail_value_expected_output/good-extension3.json b/chrome/test/data/extensions/ui/create_extension_detail_value_expected_output/good-extension3.json index bbf69cd..564053a 100644 --- a/chrome/test/data/extensions/ui/create_extension_detail_value_expected_output/good-extension3.json +++ b/chrome/test/data/extensions/ui/create_extension_detail_value_expected_output/good-extension3.json @@ -7,11 +7,6 @@ "permissions": [], "allow_reload": false, "order": 2, - "content_scripts": [ - { - "matches": ["http://localhost/*"], - "js": ["page.js"] - } - ], + "content_scripts": [], "views": [] } |