1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/extension_apitest.h" 6 #include "chrome/browser/extensions/extension_browser_event_router.h" 7 #include "chrome/browser/extensions/extension_tabs_module.h" 8 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/ui/browser.h" 11 #include "chrome/browser/ui/browser_window.h" 12 #include "chrome/browser/ui/omnibox/location_bar.h" 13 #include "chrome/common/extensions/extension_action.h" 14 #include "chrome/common/extensions/extension.h" 15 #include "chrome/test/ui_test_utils.h" 16 #include "content/browser/tab_contents/tab_contents.h" 17 18 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageAction) { 19 ASSERT_TRUE(test_server()->Start()); 20 ASSERT_TRUE(RunExtensionTest("page_action/basics")) << message_; 21 const Extension* extension = GetSingleLoadedExtension(); 22 ASSERT_TRUE(extension) << message_; 23 { 24 // Tell the extension to update the page action state. 25 ResultCatcher catcher; 26 ui_test_utils::NavigateToURL(browser(), 27 GURL(extension->GetResourceURL("update.html"))); 28 ASSERT_TRUE(catcher.GetNextResult()); 29 } 30 31 // Test that we received the changes. 32 int tab_id = 33 browser()->GetSelectedTabContents()->controller().session_id().id(); 34 ExtensionAction* action = extension->page_action(); 35 ASSERT_TRUE(action); 36 EXPECT_EQ("Modified", action->GetTitle(tab_id)); 37 38 { 39 // Simulate the page action being clicked. 40 ResultCatcher catcher; 41 int tab_id = 42 ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); 43 ExtensionService* service = browser()->profile()->GetExtensionService(); 44 service->browser_event_router()->PageActionExecuted( 45 browser()->profile(), extension->id(), "", tab_id, "", 0); 46 EXPECT_TRUE(catcher.GetNextResult()); 47 } 48 49 { 50 // Tell the extension to update the page action state again. 51 ResultCatcher catcher; 52 ui_test_utils::NavigateToURL(browser(), 53 GURL(extension->GetResourceURL("update2.html"))); 54 ASSERT_TRUE(catcher.GetNextResult()); 55 } 56 57 // Test that we received the changes. 58 tab_id = browser()->GetSelectedTabContents()->controller().session_id().id(); 59 EXPECT_FALSE(action->GetIcon(tab_id).isNull()); 60 } 61 62 // Test that calling chrome.pageAction.setPopup() can enable a popup. 63 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionAddPopup) { 64 // Load the extension, which has no default popup. 65 ASSERT_TRUE(RunExtensionTest("page_action/add_popup")) << message_; 66 const Extension* extension = GetSingleLoadedExtension(); 67 ASSERT_TRUE(extension) << message_; 68 69 int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); 70 71 ExtensionAction* page_action = extension->page_action(); 72 ASSERT_TRUE(page_action) 73 << "Page action test extension should have a page action."; 74 75 ASSERT_FALSE(page_action->HasPopup(tab_id)); 76 77 // Simulate the page action being clicked. The resulting event should 78 // install a page action popup. 79 { 80 ResultCatcher catcher; 81 ExtensionService* service = browser()->profile()->GetExtensionService(); 82 service->browser_event_router()->PageActionExecuted( 83 browser()->profile(), extension->id(), "action", tab_id, "", 1); 84 ASSERT_TRUE(catcher.GetNextResult()); 85 } 86 87 ASSERT_TRUE(page_action->HasPopup(tab_id)) 88 << "Clicking on the page action should have caused a popup to be added."; 89 90 ASSERT_STREQ("/a_popup.html", 91 page_action->GetPopupUrl(tab_id).path().c_str()); 92 93 // Now change the popup from a_popup.html to a_second_popup.html . 94 // Load a page which removes the popup using chrome.pageAction.setPopup(). 95 { 96 ResultCatcher catcher; 97 ui_test_utils::NavigateToURL( 98 browser(), 99 GURL(extension->GetResourceURL("change_popup.html"))); 100 ASSERT_TRUE(catcher.GetNextResult()); 101 } 102 103 ASSERT_TRUE(page_action->HasPopup(tab_id)); 104 ASSERT_STREQ("/another_popup.html", 105 page_action->GetPopupUrl(tab_id).path().c_str()); 106 } 107 108 // Test that calling chrome.pageAction.setPopup() can remove a popup. 109 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PageActionRemovePopup) { 110 // Load the extension, which has a page action with a default popup. 111 ASSERT_TRUE(RunExtensionTest("page_action/remove_popup")) << message_; 112 const Extension* extension = GetSingleLoadedExtension(); 113 ASSERT_TRUE(extension) << message_; 114 115 int tab_id = ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); 116 117 ExtensionAction* page_action = extension->page_action(); 118 ASSERT_TRUE(page_action) 119 << "Page action test extension should have a page action."; 120 121 ASSERT_TRUE(page_action->HasPopup(tab_id)) 122 << "Expect a page action popup before the test removes it."; 123 124 // Load a page which removes the popup using chrome.pageAction.setPopup(). 125 { 126 ResultCatcher catcher; 127 ui_test_utils::NavigateToURL( 128 browser(), 129 GURL(extension->GetResourceURL("remove_popup.html"))); 130 ASSERT_TRUE(catcher.GetNextResult()); 131 } 132 133 ASSERT_FALSE(page_action->HasPopup(tab_id)) 134 << "Page action popup should have been removed."; 135 } 136 137 // Tests old-style pageActions API that is deprecated but we don't want to 138 // break. 139 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, OldPageActions) { 140 ASSERT_TRUE(RunExtensionTest("page_action/old_api")) << message_; 141 const Extension* extension = GetSingleLoadedExtension(); 142 ASSERT_TRUE(extension) << message_; 143 144 // Have the extension enable the page action. 145 { 146 ResultCatcher catcher; 147 ui_test_utils::NavigateToURL(browser(), 148 GURL(extension->GetResourceURL("page.html"))); 149 ASSERT_TRUE(catcher.GetNextResult()); 150 } 151 152 // Simulate the page action being clicked. 153 { 154 ResultCatcher catcher; 155 int tab_id = 156 ExtensionTabUtil::GetTabId(browser()->GetSelectedTabContents()); 157 ExtensionService* service = browser()->profile()->GetExtensionService(); 158 service->browser_event_router()->PageActionExecuted( 159 browser()->profile(), extension->id(), "action", tab_id, "", 1); 160 EXPECT_TRUE(catcher.GetNextResult()); 161 } 162 } 163 164 // Tests popups in page actions. 165 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ShowPageActionPopup) { 166 ASSERT_TRUE(RunExtensionTest("page_action/popup")) << message_; 167 const Extension* extension = GetSingleLoadedExtension(); 168 ASSERT_TRUE(extension) << message_; 169 170 ASSERT_TRUE(WaitForPageActionVisibilityChangeTo(1)); 171 172 { 173 ResultCatcher catcher; 174 LocationBarTesting* location_bar = 175 browser()->window()->GetLocationBar()->GetLocationBarForTesting(); 176 location_bar->TestPageActionPressed(0); 177 ASSERT_TRUE(catcher.GetNextResult()); 178 } 179 } 180 181 // Test http://crbug.com/57333: that two page action extensions using the same 182 // icon for the page action icon and the extension icon do not crash. 183 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TestCrash57333) { 184 // Load extension A. 185 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("page_action") 186 .AppendASCII("crash_57333") 187 .AppendASCII("Extension1"))); 188 // Load extension B. 189 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("page_action") 190 .AppendASCII("crash_57333") 191 .AppendASCII("Extension2"))); 192 } 193