diff options
author | pshenoy@chromium.org <pshenoy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 17:08:53 +0000 |
---|---|---|
committer | pshenoy@chromium.org <pshenoy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 17:08:53 +0000 |
commit | 1e6e8954ae27faedff3115ae9775fe28a61ebae2 (patch) | |
tree | 5dd52d24bc07cd548e3eb7e7a260f3d6401b16ec | |
parent | 323dbf7094e7a782e416eaaeb2b62c7f62ddf9fc (diff) | |
download | chromium_src-1e6e8954ae27faedff3115ae9775fe28a61ebae2.zip chromium_src-1e6e8954ae27faedff3115ae9775fe28a61ebae2.tar.gz chromium_src-1e6e8954ae27faedff3115ae9775fe28a61ebae2.tar.bz2 |
Rewriting pyauto extensions tests as browser tests. Below are the pyauto tests being converted.
testSetExtensionStates
testTriggerBrowserAction
testTriggerPageAction
testAdblockExtensionCrash
BUG=None
Review URL: https://chromiumcodereview.appspot.com/12500014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191512 0039d316-1c4b-4281-b951-d872f2087c98
9 files changed, 251 insertions, 122 deletions
diff --git a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc index f607d4e..0a5497f 100644 --- a/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc +++ b/chrome/browser/extensions/api/extension_action/browser_action_apitest.cc @@ -658,5 +658,47 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Getters) { ASSERT_TRUE(catcher.GetNextResult()); } +// Verify triggering browser action. +IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TestTriggerBrowserAction) { + ASSERT_TRUE(test_server()->Start()); + + ASSERT_TRUE(RunExtensionTest("trigger_actions/browser_action")) << message_; + const Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; + + // Test that there is a browser action in the toolbar. + ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); + + ui_test_utils::NavigateToURL( + browser(), + test_server()->GetURL("files/simple.html")); + + ExtensionAction* browser_action = GetBrowserAction(*extension); + EXPECT_TRUE(browser_action != NULL); + + // Simulate a click on the browser action icon. + { + ResultCatcher catcher; + GetBrowserActionsBar().Press(0); + EXPECT_TRUE(catcher.GetNextResult()); + } + + WebContents* tab = + browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_TRUE(tab != NULL); + + // Verify that the browser action turned the background color red. + const std::string script = + "window.domAutomationController.send(document.body.style." + "backgroundColor);"; + std::string result; + const std::string frame_xpath = ""; + EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString(tab, + frame_xpath, + script, + &result)); + EXPECT_EQ(result, "red"); +} + } // namespace } // namespace extensions diff --git a/chrome/browser/extensions/api/extension_action/page_action_apitest.cc b/chrome/browser/extensions/api/extension_action/page_action_apitest.cc index 42ae657..232280a 100644 --- a/chrome/browser/extensions/api/extension_action/page_action_apitest.cc +++ b/chrome/browser/extensions/api/extension_action/page_action_apitest.cc @@ -13,12 +13,16 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/sessions/session_tab_helper.h" #include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_window.h" #include "chrome/browser/ui/omnibox/location_bar.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" #include "chrome/common/extensions/extension.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/web_contents.h" +#include "content/public/test/browser_test_utils.h" + +using content::WebContents; namespace extensions { namespace { @@ -231,5 +235,51 @@ IN_PROC_BROWSER_TEST_F(PageActionApiTest, Getters) { ASSERT_TRUE(catcher.GetNextResult()); } +// Verify triggering page action. +IN_PROC_BROWSER_TEST_F(PageActionApiTest, TestTriggerPageAction) { + ASSERT_TRUE(test_server()->Start()); + + ASSERT_TRUE(RunExtensionTest("trigger_actions/page_action")) << message_; + const Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; + + // Page action icon is displayed when a tab is created. + ui_test_utils::NavigateToURL(browser(), + test_server()->GetURL("files/simple.html")); + chrome::NewTab(browser()); + browser()->tab_strip_model()->ActivateTabAt(0, true); + + ExtensionAction* page_action = GetPageAction(*extension); + ASSERT_TRUE(page_action); + + { + // Simulate the page action being clicked. + ResultCatcher catcher; + int tab_id = ExtensionTabUtil::GetTabId( + browser()->tab_strip_model()->GetActiveWebContents()); + ExtensionService* service = extensions::ExtensionSystem::Get( + browser()->profile())->extension_service(); + service->browser_event_router()->PageActionExecuted( + browser()->profile(), *page_action, tab_id, "", 0); + EXPECT_TRUE(catcher.GetNextResult()); + } + + WebContents* tab = + browser()->tab_strip_model()->GetActiveWebContents(); + EXPECT_TRUE(tab != NULL); + + // Verify that the browser action turned the background color red. + const std::string script = + "window.domAutomationController.send(document.body.style." + "backgroundColor);"; + std::string result; + const std::string frame_xpath = ""; + EXPECT_TRUE(content::ExecuteScriptInFrameAndExtractString(tab, + frame_xpath, + script, + &result)); + EXPECT_EQ(result, "red"); +} + } // namespace } // namespace extensions diff --git a/chrome/browser/extensions/extension_functional_browsertest.cc b/chrome/browser/extensions/extension_functional_browsertest.cc new file mode 100644 index 0000000..a1c5c91 --- /dev/null +++ b/chrome/browser/extensions/extension_functional_browsertest.cc @@ -0,0 +1,95 @@ +// Copyright (c) 2012 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. + +#include "chrome/browser/extensions/crx_installer.h" +#include "chrome/browser/extensions/extension_browsertest.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser_commands.h" +#include "chrome/test/base/ui_test_utils.h" + +namespace extensions { + +class ExtensionFunctionalTest : public ExtensionBrowserTest { +public: + void InstallExtensionSilently(ExtensionService* service, + const char* filename) { + service->set_show_extensions_prompts(false); + size_t num_before = service->extensions()->size(); + + base::FilePath path = test_data_dir_.AppendASCII(filename); + + content::WindowedNotificationObserver extension_loaded_observer( + chrome::NOTIFICATION_EXTENSION_LOADED, + content::NotificationService::AllSources()); + + scoped_refptr<extensions::CrxInstaller> installer( + extensions::CrxInstaller::Create(service, NULL)); + installer->set_is_gallery_install(false); + installer->set_allow_silent_install(true); + installer->set_install_source(Manifest::INTERNAL); + installer->set_off_store_install_allow_reason( + extensions::CrxInstaller::OffStoreInstallAllowedInTest); + + content::NotificationRegistrar registrar; + registrar.Add(this, chrome::NOTIFICATION_CRX_INSTALLER_DONE, + content::Source<extensions::CrxInstaller>(installer.get())); + + installer->InstallCrx(path); + content::RunMessageLoop(); + + size_t num_after = service->extensions()->size(); + EXPECT_EQ(num_before + 1, num_after); + + extension_loaded_observer.Wait(); + const Extension* extension = service->GetExtensionById( + last_loaded_extension_id_, false); + EXPECT_TRUE(extension != NULL); + } +}; + +// Failing on mac_rel trybots with timeout error. Disabling for now. +#if defined(OS_MACOSX) +#define TestAdblockExtensionCrash DISABLED_TestAdblockExtensionCrash +#endif +IN_PROC_BROWSER_TEST_F(ExtensionFunctionalTest, TestAdblockExtensionCrash) { + ExtensionService* service = profile()->GetExtensionService(); + InstallExtensionSilently(service, "adblock.crx"); + + // Restart the browser. + chrome::Exit(); + chrome::NewWindow(browser()); + + // Verify that the extension is enabled and allowed in incognito + // is disabled. + EXPECT_TRUE(service->IsExtensionEnabled(last_loaded_extension_id_)); + EXPECT_FALSE(service->IsIncognitoEnabled(last_loaded_extension_id_)); +} + +IN_PROC_BROWSER_TEST_F(ExtensionFunctionalTest, TestSetExtensionsState) { + ExtensionService* service = profile()->GetExtensionService(); + InstallExtensionSilently(service, "google_talk.crx"); + + // Disable the extension and verify. + service->SetIsIncognitoEnabled(last_loaded_extension_id_, false); + service->DisableExtension(last_loaded_extension_id_, + Extension::DISABLE_USER_ACTION); + EXPECT_FALSE(service->IsExtensionEnabled(last_loaded_extension_id_)); + + // Enable the extension and verify. + service->SetIsIncognitoEnabled(last_loaded_extension_id_, false); + service->EnableExtension(last_loaded_extension_id_); + EXPECT_TRUE(service->IsExtensionEnabled(last_loaded_extension_id_)); + + // Allow extension in incognito mode and verify. + service->EnableExtension(last_loaded_extension_id_); + service->SetIsIncognitoEnabled(last_loaded_extension_id_, true); + EXPECT_TRUE(service->IsIncognitoEnabled(last_loaded_extension_id_)); + + // Disallow extension in incognito mode and verify. + service->EnableExtension(last_loaded_extension_id_); + service->SetIsIncognitoEnabled(last_loaded_extension_id_, false); + EXPECT_FALSE(service->IsIncognitoEnabled(last_loaded_extension_id_)); +} +} // namespace extensions diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index e734f20..97fcdca 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1286,6 +1286,7 @@ 'browser/extensions/extension_disabled_ui_browsertest.cc', 'browser/extensions/extension_dom_clipboard_apitest.cc', 'browser/extensions/extension_fileapi_apitest.cc', + 'browser/extensions/extension_functional_browsertest.cc', 'browser/extensions/extension_function_test_utils.cc', 'browser/extensions/extension_function_test_utils.h', 'browser/extensions/extension_geolocation_apitest.cc', diff --git a/chrome/test/data/extensions/api_test/trigger_actions/browser_action/background.js b/chrome/test/data/extensions/api_test/trigger_actions/browser_action/background.js new file mode 100644 index 0000000..1b1ec9f --- /dev/null +++ b/chrome/test/data/extensions/api_test/trigger_actions/browser_action/background.js @@ -0,0 +1,12 @@ +// Copyright (c) 2011 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. + +// Turn the background red when the user clicks on the browser action. +chrome.browserAction.onClicked.addListener(function(tab) { + chrome.tabs.executeScript( + null, {code: "document.body.style.backgroundColor='red'"}); + chrome.test.notifyPass(); +}); + +chrome.test.notifyPass();
\ No newline at end of file diff --git a/chrome/test/data/extensions/api_test/trigger_actions/browser_action/manifest.json b/chrome/test/data/extensions/api_test/trigger_actions/browser_action/manifest.json new file mode 100644 index 0000000..e4850aa --- /dev/null +++ b/chrome/test/data/extensions/api_test/trigger_actions/browser_action/manifest.json @@ -0,0 +1,15 @@ +{ + "name": "A browser action that makes the active tab's background red", + "version": "1.0", + "manifest_version": 2, + "background": { + "scripts": ["background.js"] + }, + "permissions": [ + "tabs", "http://*/*" + ], + "browser_action": { + "name": "Make this page red", + "default_icon": "icon.png" + } +} diff --git a/chrome/test/data/extensions/api_test/trigger_actions/page_action/background.js b/chrome/test/data/extensions/api_test/trigger_actions/page_action/background.js new file mode 100644 index 0000000..78daa3b --- /dev/null +++ b/chrome/test/data/extensions/api_test/trigger_actions/page_action/background.js @@ -0,0 +1,21 @@ +// Copyright (c) 2011 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. + +// Show the page action icon for all tabs. +chrome.tabs.onCreated.addListener(function(tab) { + chrome.tabs.getAllInWindow(null, function(tabs) { + for (var i = 0, t; t = tabs[i]; i++) { + chrome.pageAction.show(t.id); + } + }); +}); + +// Turn the background red when the user clicks on the page action. +chrome.pageAction.onClicked.addListener(function(tab) { + chrome.tabs.executeScript( + null, {code: "document.body.style.backgroundColor='red'"}); + chrome.test.notifyPass(); +}); + +chrome.test.notifyPass(); diff --git a/chrome/test/data/extensions/api_test/trigger_actions/page_action/manifest.json b/chrome/test/data/extensions/api_test/trigger_actions/page_action/manifest.json new file mode 100644 index 0000000..e08166e --- /dev/null +++ b/chrome/test/data/extensions/api_test/trigger_actions/page_action/manifest.json @@ -0,0 +1,15 @@ +{ + "name": "A page action that makes the active tab's background red", + "version": "1.0", + "manifest_version": 2, + "background": { + "scripts": ["background.js"] + }, + "permissions": [ + "tabs", "http://*/*" + ], + "page_action": { + "name": "Make this page red", + "default_icon": "icon.png" + } +} diff --git a/chrome/test/functional/extensions.py b/chrome/test/functional/extensions.py index a850e70..62f206f 100755 --- a/chrome/test/functional/extensions.py +++ b/chrome/test/functional/extensions.py @@ -262,128 +262,6 @@ class ExtensionsTest(pyauto.PyUITest): 'tabs' in permissions_api, msg='Unexpected API permissions information.') - def testSetExtensionStates(self): - """Test setting different extension states.""" - ext_id = self._InstallExtensionCheckDefaults('google_talk.crx') - - # Disable the extension and verify. - self.SetExtensionStateById(ext_id, enable=False, allow_in_incognito=False) - extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) - self.assertFalse(extension['is_enabled']) - - # Enable extension and verify. - self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=False) - extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) - self.assertTrue(extension['is_enabled']) - - # Allow extension in incognito mode and verify. - self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=True) - extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) - self.assertTrue(extension['allowed_in_incognito']) - - # Disallow extension in incognito mode and verify. - self.SetExtensionStateById(ext_id, enable=True, allow_in_incognito=False) - extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) - self.assertFalse(extension['allowed_in_incognito']) - - def testTriggerBrowserAction(self): - """Test triggering browser action.""" - dir_path = os.path.abspath( - os.path.join(self.DataDir(), 'extensions', 'trigger_actions', - 'browser_action')) - ext_id = self.InstallExtension(dir_path) - - self.NavigateToURL(self.GetFileURLForDataPath('simple.html')) - - self.TriggerBrowserActionById(ext_id) - - # Verify that the browser action turned the background red. - self.assertTrue(self.WaitUntil( - lambda: self.GetDOMValue('document.body.style.backgroundColor'), - expect_retval='red'), - msg='Browser action was not triggered.') - - def testTriggerBrowserActionWithPopup(self): - """Test triggering browser action that shows a popup.""" - # Fails on Vista Chromium bot only. crbug.com/106620 - if (self.IsWinVista() and - self.GetBrowserInfo()['properties']['branding'] == 'Chromium'): - return - dir_path = os.path.abspath( - os.path.join(self.DataDir(), 'extensions', 'trigger_actions', - 'browser_action_popup')) - ext_id = self.InstallExtension(dir_path) - - self.TriggerBrowserActionById(ext_id) - - # Verify that the extension popup is displayed. - popup = self.WaitUntilExtensionViewLoaded( - view_type='EXTENSION_POPUP') - self.assertTrue(popup, - msg='Browser action failed to display the popup (views=%s).' % - self.GetBrowserInfo()['extension_views']) - - def testTriggerPageAction(self): - """Test triggering page action.""" - dir_path = os.path.abspath( - os.path.join(self.DataDir(), 'extensions', 'trigger_actions', - 'page_action')) - ext_id = self.InstallExtension(dir_path) - - # Page action icon is displayed when a tab is created. - self.NavigateToURL(self.GetFileURLForDataPath('simple.html')) - self.AppendTab(pyauto.GURL('chrome://newtab')) - self.ActivateTab(0) - self.assertTrue(self.WaitUntil( - lambda: ext_id in - self.GetBrowserInfo()['windows'][0]['visible_page_actions']), - msg='Page action icon is not visible.') - - self.TriggerPageActionById(ext_id) - - # Verify that page action turned the background red. - self.assertTrue(self.WaitUntil( - lambda: self.GetDOMValue('document.body.style.backgroundColor'), - expect_retval='red'), - msg='Page action was not triggered.') - - def testTriggerPageActionWithPopup(self): - """Test triggering page action that shows a popup.""" - # Fails on Vista Chromium bot only. crbug.com/106620 - if (self.IsWinVista() and - self.GetBrowserInfo()['properties']['branding'] == 'Chromium'): - return - dir_path = os.path.abspath( - os.path.join(self.DataDir(), 'extensions', 'trigger_actions', - 'page_action_popup')) - ext_id = self.InstallExtension(dir_path) - - # Page action icon is displayed when a tab is created. - self.AppendTab(pyauto.GURL('chrome://newtab')) - self.ActivateTab(0) - self.assertTrue(self.WaitUntil( - lambda: ext_id in - self.GetBrowserInfo()['windows'][0]['visible_page_actions']), - msg='Page action icon is not visible.') - - self.TriggerPageActionById(ext_id) - - # Verify that the extension popup is displayed. - popup = self.WaitUntilExtensionViewLoaded( - view_type='EXTENSION_POPUP') - self.assertTrue(popup, - msg='Page action failed to display the popup (views=%s).' % - self.GetBrowserInfo()['extension_views']) - - def testAdblockExtensionCrash(self): - """Test AdBlock extension does not cause a browser crash.""" - ext_id = self._InstallExtensionCheckDefaults('adblock.crx') - - self.RestartBrowser(clear_profile=False) - extension = self._GetExtensionInfoById(self.GetExtensionsInfo(), ext_id) - self.assertTrue(extension['is_enabled']) - self.assertFalse(extension['allowed_in_incognito']) - def testDisableEnableExtension(self): """Tests that an extension can be disabled and enabled with the UI.""" ext_id = self._InstallExtensionCheckDefaults('good.crx') |