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