diff options
author | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-28 00:10:29 +0000 |
---|---|---|
committer | skerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-28 00:10:29 +0000 |
commit | e478d670823e9c72dc00bf39123c8b19606510de (patch) | |
tree | bd8a898e9139d0960142889714e35c719d90b200 /chrome/browser/extensions | |
parent | 3a3a617805faf1f681155fdfddd2de0b47a4244f (diff) | |
download | chromium_src-e478d670823e9c72dc00bf39123c8b19606510de.zip chromium_src-e478d670823e9c72dc00bf39123c8b19606510de.tar.gz chromium_src-e478d670823e9c72dc00bf39123c8b19606510de.tar.bz2 |
Allow extensions to add, remove, or change their page action popup.
A similar change will be made for browser action popups.
BUG=27526
TEST=Added unit tests. Manual testing on linux.
Review URL: http://codereview.chromium.org/545068
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37353 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
4 files changed, 122 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 6e719fc..c5248cf 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -113,6 +113,7 @@ void FactoryRegistry::ResetFunctions() { RegisterFunction<PageActionHideFunction>(); RegisterFunction<PageActionSetIconFunction>(); RegisterFunction<PageActionSetTitleFunction>(); + RegisterFunction<PageActionSetPopupFunction>(); // Browser Actions. RegisterFunction<BrowserActionSetIconFunction>(); diff --git a/chrome/browser/extensions/extension_page_actions_module.cc b/chrome/browser/extensions/extension_page_actions_module.cc index ace9317..1379b2f 100644 --- a/chrome/browser/extensions/extension_page_actions_module.cc +++ b/chrome/browser/extensions/extension_page_actions_module.cc @@ -192,6 +192,29 @@ bool PageActionSetTitleFunction::RunImpl() { return true; } +bool PageActionSetPopupFunction::RunImpl() { + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* args = args_as_dictionary(); + + int tab_id; + EXTENSION_FUNCTION_VALIDATE(args->GetInteger(L"tabId", &tab_id)); + if (!InitCommon(tab_id)) + return false; + + // TODO(skerner): Consider allowing null and undefined to mean the popup + // should be removed. + std::string popup_string; + EXTENSION_FUNCTION_VALIDATE(args->GetString(L"popup", &popup_string)); + + GURL popup_url; + if (!popup_string.empty()) + popup_url = GetExtension()->GetResourceURL(popup_string); + + page_action_->SetPopupUrl(tab_id, popup_url); + contents_->PageActionStateChanged(); + return true; +} + // Not currently exposed to extensions. To re-enable, add mapping in // extension_function_dispatcher. bool PageActionSetBadgeBackgroundColorFunction::RunImpl() { diff --git a/chrome/browser/extensions/extension_page_actions_module.h b/chrome/browser/extensions/extension_page_actions_module.h index 8c0e291..3be9ce1 100644 --- a/chrome/browser/extensions/extension_page_actions_module.h +++ b/chrome/browser/extensions/extension_page_actions_module.h @@ -10,6 +10,7 @@ class TabContents; class ExtensionAction; +// Base class for page action APIs. class PageActionFunction : public SyncExtensionFunction { protected: virtual ~PageActionFunction() {} @@ -22,54 +23,70 @@ class PageActionFunction : public SyncExtensionFunction { TabContents* contents_; }; +// Implement chrome.pageActions.enableForTab(). class EnablePageActionFunction : public PageActionFunction { ~EnablePageActionFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageActions.enableForTab") }; +// Implement chrome.pageActions.disableForTab(). class DisablePageActionFunction : public PageActionFunction { ~DisablePageActionFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageActions.disableForTab") }; +// Implement chrome.pageActions.show(). class PageActionShowFunction : public PageActionFunction { ~PageActionShowFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.show") }; +// Implement chrome.pageActions.hide(). class PageActionHideFunction : public PageActionFunction { ~PageActionHideFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.hide") }; +// Implement chrome.pageActions.setIcon(). class PageActionSetIconFunction : public PageActionFunction { ~PageActionSetIconFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.setIcon") }; +// Implement chrome.pageActions.setTitle(). class PageActionSetTitleFunction : public PageActionFunction { ~PageActionSetTitleFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.setTitle") }; +// Implement chrome.pageActions.setPopup(). +class PageActionSetPopupFunction : public PageActionFunction { + ~PageActionSetPopupFunction() {} + virtual bool RunImpl(); + DECLARE_EXTENSION_FUNCTION_NAME("pageAction.setPopup") +}; + +// Implement chrome.pageActions.setBadgeBackgroundColor(). class PageActionSetBadgeBackgroundColorFunction : public PageActionFunction { ~PageActionSetBadgeBackgroundColorFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.setBadgeBackgroundColor") }; +// Implement chrome.pageActions.setBadgeTextColor(). class PageActionSetBadgeTextColorFunction : public PageActionFunction { ~PageActionSetBadgeTextColorFunction() {} virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("pageAction.setBadgeTextColor") }; +// Implement chrome.pageActions.setBadgeText(). class PageActionSetBadgeTextFunction : public PageActionFunction { ~PageActionSetBadgeTextFunction() {} virtual bool RunImpl(); diff --git a/chrome/browser/extensions/page_action_apitest.cc b/chrome/browser/extensions/page_action_apitest.cc index b545558..b5ba0f3 100644 --- a/chrome/browser/extensions/page_action_apitest.cc +++ b/chrome/browser/extensions/page_action_apitest.cc @@ -18,6 +18,7 @@ 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); @@ -61,6 +62,86 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { EXPECT_FALSE(action->GetIcon(tab_id).isNull()); } +// Test that calling chrome.pageAction.setPopup() can enable a popup. +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); + + int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + + ExtensionAction* page_action = extension->page_action(); + ASSERT_TRUE(page_action) + << "Page action test extension should have a page action."; + + ASSERT_FALSE(page_action->HasPopup(tab_id)); + + // Simulate the page action being clicked. The resulting event should + // install a page action popup. + { + ResultCatcher catcher; + ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( + browser()->profile(), extension->id(), "action", tab_id, "", 1); + ASSERT_TRUE(catcher.GetNextResult()); + } + + ASSERT_TRUE(page_action->HasPopup(tab_id)) + << "Clicking on the page action should have caused a popup to be added."; + + ASSERT_STREQ("/a_popup.html", + page_action->GetPopupUrl(tab_id).path().c_str()); + + // Now change the popup from a_popup.html to a_second_popup.html . + // Load a page which removes the popup using chrome.pageAction.setPopup(). + { + ResultCatcher catcher; + ui_test_utils::NavigateToURL( + browser(), + GURL(extension->GetResourceURL("change_popup.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + } + + ASSERT_TRUE(page_action->HasPopup(tab_id)); + ASSERT_STREQ("/another_popup.html", + page_action->GetPopupUrl(tab_id).path().c_str()); +} + +// Test that calling chrome.pageAction.setPopup() can remove a popup. +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); + + int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); + + ExtensionAction* page_action = extension->page_action(); + ASSERT_TRUE(page_action) + << "Page action test extension should have a page action."; + + ASSERT_TRUE(page_action->HasPopup(tab_id)) + << "Expect a page action popup before the test removes it."; + + // Load a page which removes the popup using chrome.pageAction.setPopup(). + { + ResultCatcher catcher; + ui_test_utils::NavigateToURL( + browser(), + GURL(extension->GetResourceURL("remove_popup.html"))); + ASSERT_TRUE(catcher.GetNextResult()); + } + + ASSERT_FALSE(page_action->HasPopup(tab_id)) + << "Page action popup should have been removed."; +} + // Tests old-style pageActions API that is deprecated but we don't want to // break. IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { |