From 5f9c249d80cd424784f5c34542c82e5ef4188b1e Mon Sep 17 00:00:00 2001 From: "skerner@chromium.org" Date: Tue, 2 Feb 2010 21:37:45 +0000 Subject: Move repetive testing code into a method of the base class used for testing extension actions. BUG=None TEST=Run unit tests that were altered. Review URL: http://codereview.chromium.org/558046 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37884 0039d316-1c4b-4281-b951-d872f2087c98 --- .../browser/extensions/browser_action_apitest.cc | 12 ++++--- chrome/browser/extensions/extension_apitest.cc | 18 ++++++++++ chrome/browser/extensions/extension_apitest.h | 18 +++++++--- chrome/browser/extensions/page_action_apitest.cc | 39 ++++++---------------- 4 files changed, 50 insertions(+), 37 deletions(-) (limited to 'chrome/browser/extensions') diff --git a/chrome/browser/extensions/browser_action_apitest.cc b/chrome/browser/extensions/browser_action_apitest.cc index f71a043..10f5b01 100644 --- a/chrome/browser/extensions/browser_action_apitest.cc +++ b/chrome/browser/extensions/browser_action_apitest.cc @@ -46,14 +46,14 @@ class BrowserActionApiTest : public ExtensionApiTest { IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) { StartHTTPServer(); ASSERT_TRUE(RunExtensionTest("browser_action/basics")) << message_; + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // Test that there is a browser action in the toolbar. ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); // Tell the extension to update the browser action state. ResultCatcher catcher; - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - Extension* extension = service->extensions()->at(0); ui_test_utils::NavigateToURL(browser(), GURL(extension->GetResourceURL("update.html"))); ASSERT_TRUE(catcher.GetNextResult()); @@ -86,6 +86,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, Basic) { IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { ASSERT_TRUE(RunExtensionTest("browser_action/no_icon")) << message_; + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // Test that there is a browser action in the toolbar and that it has no icon. ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); @@ -93,8 +95,6 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { // Tell the extension to update the icon using setIcon({imageData:...}). ResultCatcher catcher; - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - Extension* extension = service->extensions()->at(0); ui_test_utils::NavigateToURL(browser(), GURL(extension->GetResourceURL("update.html"))); ASSERT_TRUE(catcher.GetNextResult()); @@ -116,6 +116,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, DynamicBrowserAction) { IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TabSpecificBrowserActionState) { ASSERT_TRUE(RunExtensionTest("browser_action/tab_specific_state")) << message_; + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // Test that there is a browser action in the toolbar and that it has an icon. ASSERT_EQ(1, GetBrowserActionsBar().NumberOfBrowserActions()); @@ -143,6 +145,8 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, TabSpecificBrowserActionState) { IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionPopup) { ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII( "browser_action/popup"))); + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // The extension's popup's size grows by |growFactor| each click. const int growFactor = 500; diff --git a/chrome/browser/extensions/extension_apitest.cc b/chrome/browser/extensions/extension_apitest.cc index 65f1058..498e437 100644 --- a/chrome/browser/extensions/extension_apitest.cc +++ b/chrome/browser/extensions/extension_apitest.cc @@ -5,6 +5,7 @@ #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/browser.h" +#include "chrome/browser/extensions/extensions_service.h" #include "chrome/common/notification_registrar.h" #include "chrome/test/ui_test_utils.h" @@ -81,6 +82,23 @@ bool ExtensionApiTest::RunExtensionTest(const char* extension_name) { } } +// Test that exactly one extension loaded. +Extension* ExtensionApiTest::GetSingleLoadedExtension() { + ExtensionsService* service = browser()->profile()->GetExtensionsService(); + if (service->extensions()->size() != 1u) { + message_ = StringPrintf( + "Expected only one extension to be present. Found %u.", + static_cast(service->extensions()->size())); + return NULL; + } + Extension* extension = service->extensions()->at(0); + if (!extension) { + message_ = "extension pointer is NULL."; + return NULL; + } + return extension; +} + void ExtensionApiTest::SetUpCommandLine(CommandLine* command_line) { ExtensionBrowserTest::SetUpCommandLine(command_line); test_data_dir_ = test_data_dir_.AppendASCII("api_test"); diff --git a/chrome/browser/extensions/extension_apitest.h b/chrome/browser/extensions/extension_apitest.h index 01ada82..bd05fe5 100644 --- a/chrome/browser/extensions/extension_apitest.h +++ b/chrome/browser/extensions/extension_apitest.h @@ -8,6 +8,8 @@ #include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/common/notification_service.h" +class Extension; + // The general flow of these API tests should work like this: // (1) Setup initial browser state (e.g. create some bookmarks for the // bookmark test) @@ -19,10 +21,11 @@ class ExtensionApiTest : public ExtensionBrowserTest { protected: - // Helper class that observes tests failing or passing. Observation starts when - // the class is constructed. Get the next result by calling GetNextResult() and - // message() if GetNextResult() return false. If there are no results, this - // method will pump the UI message loop until one is received. + // Helper class that observes tests failing or passing. Observation starts + // when the class is constructed. Get the next result by calling + // GetNextResult() and message() if GetNextResult() return false. If there + // are no results, this method will pump the UI message loop until one is + // received. class ResultCatcher : public NotificationObserver { public: ResultCatcher(); @@ -34,7 +37,8 @@ class ExtensionApiTest : public ExtensionBrowserTest { const std::string& message() { return message_; } private: - virtual void Observe(NotificationType type, const NotificationSource& source, + virtual void Observe(NotificationType type, + const NotificationSource& source, const NotificationDetails& details); NotificationRegistrar registrar_; @@ -51,6 +55,10 @@ class ExtensionApiTest : public ExtensionBrowserTest { // |extension_name| is a directory in "test/data/extensions/api_test". bool RunExtensionTest(const char* extension_name); + // Test that exactly one extension loaded. If so, return a pointer to + // the extension. If not, return NULL and set message_. + Extension* GetSingleLoadedExtension(); + // All extensions tested by ExtensionApiTest are in the "api_test" dir. virtual void SetUpCommandLine(CommandLine* command_line); diff --git a/chrome/browser/extensions/page_action_apitest.cc b/chrome/browser/extensions/page_action_apitest.cc index b5ba0f3..e26e4ab 100644 --- a/chrome/browser/extensions/page_action_apitest.cc +++ b/chrome/browser/extensions/page_action_apitest.cc @@ -7,23 +7,18 @@ #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_browser_event_router.h" #include "chrome/browser/extensions/extension_tabs_module.h" -#include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/profile.h" #include "chrome/browser/location_bar.h" #include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_action.h" #include "chrome/test/ui_test_utils.h" IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { StartHTTPServer(); ASSERT_TRUE(RunExtensionTest("page_action/basics")) << message_; - - // TODO(skerner): Move the next four lines into a helper method. - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - ASSERT_EQ(1u, service->extensions()->size()); - Extension* extension = service->extensions()->at(0); - ASSERT_TRUE(extension); - + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; { // Tell the extension to update the page action state. ResultCatcher catcher; @@ -66,11 +61,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionAddPopup) { // Load the extension, which has no default popup. ASSERT_TRUE(RunExtensionTest("page_action/add_popup")) << message_; - - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - ASSERT_EQ(1u, service->extensions()->size()); - Extension* extension = service->extensions()->at(0); - ASSERT_TRUE(extension); + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); @@ -114,11 +106,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionAddPopup) { IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionRemovePopup) { // Load the extension, which has a page action with a default popup. ASSERT_TRUE(RunExtensionTest("page_action/remove_popup")) << message_; - - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - ASSERT_EQ(1u, service->extensions()->size()); - Extension* extension = service->extensions()->at(0); - ASSERT_TRUE(extension); + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); @@ -146,11 +135,8 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionRemovePopup) { // break. IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { ASSERT_TRUE(RunExtensionTest("page_action/old_api")) << message_; - - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - ASSERT_EQ(1u, service->extensions()->size()); - Extension* extension = service->extensions()->at(0); - ASSERT_TRUE(extension); + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // Have the extension enable the page action. { @@ -230,11 +216,8 @@ IN_PROC_BROWSER_TEST_F(PageActionPopupTest, MAYBE_Show) { NotificationService::AllSources()); ASSERT_TRUE(RunExtensionTest("page_action/popup")) << message_; - - ExtensionsService* service = browser()->profile()->GetExtensionsService(); - ASSERT_EQ(1u, service->extensions()->size()); - Extension* extension = service->extensions()->at(0); - ASSERT_TRUE(extension); + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; // Wait for The page action to actually become visible. if (!last_visibility_) -- cgit v1.1