diff options
author | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-03 20:58:50 +0000 |
---|---|---|
committer | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-03 20:58:50 +0000 |
commit | 85ae959285a1ed1d423abd3d01cac5a71cd7b51f (patch) | |
tree | 69a19062ec994dd0524e355f2221aa968b360bd7 /chrome/browser/extensions | |
parent | c775e00efade22d264a07888b5c70a443552fd99 (diff) | |
download | chromium_src-85ae959285a1ed1d423abd3d01cac5a71cd7b51f.zip chromium_src-85ae959285a1ed1d423abd3d01cac5a71cd7b51f.tar.gz chromium_src-85ae959285a1ed1d423abd3d01cac5a71cd7b51f.tar.bz2 |
Implement chrome.browserAction.setPopup().
BUG=27526
TEST=Unit tests BrowserActionApiTest.BrowserAction*Popup .
Review URL: http://codereview.chromium.org/552263
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38015 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
4 files changed, 114 insertions, 0 deletions
diff --git a/chrome/browser/extensions/browser_action_apitest.cc b/chrome/browser/extensions/browser_action_apitest.cc index 10f5b01..eafa655 100644 --- a/chrome/browser/extensions/browser_action_apitest.cc +++ b/chrome/browser/extensions/browser_action_apitest.cc @@ -172,3 +172,92 @@ IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionPopup) { EXPECT_EQ(maxSize, bounds.size()); EXPECT_TRUE(GetBrowserActionsBar().HidePopup()); } + +// Test that calling chrome.browserAction.setPopup() can enable and change +// a popup. +IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionAddPopup) { + ASSERT_TRUE(RunExtensionTest("browser_action/add_popup")) << message_; + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; + + int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + + ExtensionAction* browser_action = extension->browser_action(); + ASSERT_TRUE(browser_action) + << "Browser action test extension should have a browser action."; + + ASSERT_FALSE(browser_action->HasPopup(tab_id)); + ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId)); + + // Simulate a click on the browser action icon. The onClicked handler + // will add a popup. + { + ResultCatcher catcher; + GetBrowserActionsBar().Press(0); + ASSERT_TRUE(catcher.GetNextResult()); + } + + // The call to setPopup in background.html set a tab id, so the + // current tab's setting should have changed, but the default setting + // should not have changed. + ASSERT_TRUE(browser_action->HasPopup(tab_id)) + << "Clicking on the browser action should have caused a popup to " + << "be added."; + ASSERT_FALSE(browser_action->HasPopup(ExtensionAction::kDefaultTabId)) + << "Clicking on the browser action should not have set a default " + << "popup."; + + ASSERT_STREQ("/a_popup.html", + browser_action->GetPopupUrl(tab_id).path().c_str()); + + // Now change the popup from a_popup.html to another_popup.html by loading + // a page which removes the popup using chrome.browserAction.setPopup(). + { + ResultCatcher catcher; + ui_test_utils::NavigateToURL( + browser(), + GURL(extension->GetResourceURL("change_popup.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + } + + // The call to setPopup in change_popup.html did not use a tab id, + // so the default setting should have changed as well as the current tab. + ASSERT_TRUE(browser_action->HasPopup(tab_id)); + ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId)); + ASSERT_STREQ("/another_popup.html", + browser_action->GetPopupUrl(tab_id).path().c_str()); +} + +// Test that calling chrome.browserAction.setPopup() can remove a popup. +IN_PROC_BROWSER_TEST_F(BrowserActionApiTest, BrowserActionRemovePopup) { + // Load the extension, which has a browser action with a default popup. + ASSERT_TRUE(RunExtensionTest("browser_action/remove_popup")) << message_; + Extension* extension = GetSingleLoadedExtension(); + ASSERT_TRUE(extension) << message_; + + int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + + ExtensionAction* browser_action = extension->browser_action(); + ASSERT_TRUE(browser_action) + << "Browser action test extension should have a browser action."; + + ASSERT_TRUE(browser_action->HasPopup(tab_id)) + << "Expect a browser action popup before the test removes it."; + ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId)) + << "Expect a browser action popup is the default for all tabs."; + + // Load a page which removes the popup using chrome.browserAction.setPopup(). + { + ResultCatcher catcher; + ui_test_utils::NavigateToURL( + browser(), + GURL(extension->GetResourceURL("remove_popup.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + } + + ASSERT_FALSE(browser_action->HasPopup(tab_id)) + << "Browser action popup should have been removed."; + ASSERT_TRUE(browser_action->HasPopup(ExtensionAction::kDefaultTabId)) + << "Browser action popup default should not be changed by setting " + << "a specific tab id."; +} diff --git a/chrome/browser/extensions/extension_browser_actions_api.cc b/chrome/browser/extensions/extension_browser_actions_api.cc index 7306d98..c415d02 100644 --- a/chrome/browser/extensions/extension_browser_actions_api.cc +++ b/chrome/browser/extensions/extension_browser_actions_api.cc @@ -61,6 +61,18 @@ bool BrowserActionSetTitleFunction::RunBrowserAction() { return true; } +bool BrowserActionSetPopupFunction::RunBrowserAction() { + std::string popup_string; + EXTENSION_FUNCTION_VALIDATE(details_->GetString(L"popup", &popup_string)); + + GURL popup_url; + if (!popup_string.empty()) + popup_url = dispatcher()->GetExtension()->GetResourceURL(popup_string); + + browser_action_->SetPopupUrl(tab_id_, popup_url); + return true; +} + bool BrowserActionSetBadgeTextFunction::RunBrowserAction() { std::string badge_text; EXTENSION_FUNCTION_VALIDATE(details_->GetString(L"text", &badge_text)); diff --git a/chrome/browser/extensions/extension_browser_actions_api.h b/chrome/browser/extensions/extension_browser_actions_api.h index 524e3b0..742eecd 100644 --- a/chrome/browser/extensions/extension_browser_actions_api.h +++ b/chrome/browser/extensions/extension_browser_actions_api.h @@ -10,6 +10,7 @@ class ExtensionAction; +// Base class for chrome.browserAction.* APIs. class BrowserActionFunction : public SyncExtensionFunction { protected: BrowserActionFunction() : tab_id_(ExtensionAction::kDefaultTabId) {} @@ -29,24 +30,35 @@ class BrowserActionFunction : public SyncExtensionFunction { ExtensionAction* browser_action_; }; +// Implement chrome.browserAction.setIcon(). class BrowserActionSetIconFunction : public BrowserActionFunction { ~BrowserActionSetIconFunction() {} virtual bool RunBrowserAction(); DECLARE_EXTENSION_FUNCTION_NAME("browserAction.setIcon") }; +// Implement chrome.browserAction.setTitle(). class BrowserActionSetTitleFunction : public BrowserActionFunction { ~BrowserActionSetTitleFunction() {} virtual bool RunBrowserAction(); DECLARE_EXTENSION_FUNCTION_NAME("browserAction.setTitle") }; +// Implement chrome.browserActions.setPopup(). +class BrowserActionSetPopupFunction : public BrowserActionFunction { + ~BrowserActionSetPopupFunction() {} + virtual bool RunBrowserAction(); + DECLARE_EXTENSION_FUNCTION_NAME("browserAction.setPopup") +}; + +// Implement chrome.browserAction.setBadgeText(). class BrowserActionSetBadgeTextFunction : public BrowserActionFunction { ~BrowserActionSetBadgeTextFunction() {} virtual bool RunBrowserAction(); DECLARE_EXTENSION_FUNCTION_NAME("browserAction.setBadgeText") }; +// Implement chrome.browserAction.setBadgeBackgroundColor(). class BrowserActionSetBadgeBackgroundColorFunction : public BrowserActionFunction { ~BrowserActionSetBadgeBackgroundColorFunction() {} diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 7ba97ff..d39bd07 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -122,6 +122,7 @@ void FactoryRegistry::ResetFunctions() { RegisterFunction<BrowserActionSetTitleFunction>(); RegisterFunction<BrowserActionSetBadgeTextFunction>(); RegisterFunction<BrowserActionSetBadgeBackgroundColorFunction>(); + RegisterFunction<BrowserActionSetPopupFunction>(); // Bookmarks. RegisterFunction<GetBookmarksFunction>(); |