| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browser.h" | 5 #include "chrome/browser/browser.h" |
| 6 #include "chrome/browser/browser_window.h" | 6 #include "chrome/browser/browser_window.h" |
| 7 #include "chrome/browser/extensions/extension_apitest.h" | 7 #include "chrome/browser/extensions/extension_apitest.h" |
| 8 #include "chrome/browser/extensions/extension_browser_event_router.h" | 8 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 9 #include "chrome/browser/extensions/extension_tabs_module.h" | 9 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 10 #include "chrome/browser/extensions/extensions_service.h" | 10 #include "chrome/browser/extensions/extensions_service.h" |
| 11 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents.h" | 12 #include "chrome/browser/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/extensions/extension_action.h" |
| 14 #include "chrome/test/ui_test_utils.h" |
| 15 |
| 16 #if defined(OS_WIN) |
| 13 #include "chrome/browser/views/browser_actions_container.h" | 17 #include "chrome/browser/views/browser_actions_container.h" |
| 14 #include "chrome/browser/views/extensions/extension_popup.h" | 18 #include "chrome/browser/views/extensions/extension_popup.h" |
| 15 #include "chrome/browser/views/toolbar_view.h" | 19 #include "chrome/browser/views/toolbar_view.h" |
| 16 #include "chrome/common/extensions/extension_action.h" | 20 #elif defined(OS_LINUX) |
| 17 #include "chrome/test/ui_test_utils.h" | 21 #include "chrome/browser/gtk/view_id_util.h" |
| 22 #endif |
| 18 | 23 |
| 19 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserAction) { | 24 class BrowserActionTest : public ExtensionApiTest { |
| 25 public: |
| 26 BrowserActionTest() { } |
| 27 virtual ~BrowserActionTest() { } |
| 28 |
| 29 int NumberOfBrowserActions() { |
| 30 int rv = -1; |
| 31 |
| 32 #if defined(OS_WIN) |
| 33 BrowserActionsContainer* browser_actions = |
| 34 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> |
| 35 browser_actions(); |
| 36 if (browser_actions) |
| 37 rv = browser_actions->num_browser_actions(); |
| 38 #elif defined(OS_LINUX) |
| 39 GtkWidget* toolbar = ViewIDUtil::GetWidget( |
| 40 GTK_WIDGET(browser()->window()->GetNativeHandle()), |
| 41 VIEW_ID_BROWSER_ACTION_TOOLBAR); |
| 42 |
| 43 if (toolbar) { |
| 44 GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar)); |
| 45 rv = g_list_length(children); |
| 46 g_list_free(children); |
| 47 } |
| 48 #endif |
| 49 |
| 50 EXPECT_NE(-1, rv); |
| 51 return rv; |
| 52 } |
| 53 |
| 54 bool IsIconNull(int index) { |
| 55 #if defined(OS_WIN) |
| 56 BrowserActionsContainer* browser_actions = |
| 57 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> |
| 58 browser_actions(); |
| 59 // We can't ASSERT_TRUE in non-void functions. |
| 60 if (browser_actions) { |
| 61 return browser_actions->GetBrowserActionViewAt(index)->button()->icon(). |
| 62 empty(); |
| 63 } else { |
| 64 EXPECT_TRUE(false); |
| 65 } |
| 66 #elif defined(OS_LINUX) |
| 67 GtkWidget* button = GetButton(index); |
| 68 if (button) |
| 69 return gtk_button_get_image(GTK_BUTTON(button)) == NULL; |
| 70 else |
| 71 EXPECT_TRUE(false); |
| 72 #endif |
| 73 |
| 74 return false; |
| 75 } |
| 76 |
| 77 void ExecuteBrowserAction(int index) { |
| 78 #if defined(OS_WIN) |
| 79 BrowserActionsContainer* browser_actions = |
| 80 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> |
| 81 browser_actions(); |
| 82 ASSERT_TRUE(browser_actions); |
| 83 browser_actions->TestExecuteBrowserAction(index); |
| 84 #elif defined(OS_LINUX) |
| 85 GtkWidget* button = GetButton(index); |
| 86 ASSERT_TRUE(button); |
| 87 gtk_button_clicked(GTK_BUTTON(button)); |
| 88 #endif |
| 89 } |
| 90 |
| 91 std::string GetTooltip(int index) { |
| 92 #if defined(OS_WIN) |
| 93 BrowserActionsContainer* browser_actions = |
| 94 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> |
| 95 browser_actions(); |
| 96 if (browser_actions) { |
| 97 std::wstring text; |
| 98 EXPECT_TRUE(browser_actions->GetBrowserActionViewAt(0)->button()-> |
| 99 GetTooltipText(0, 0, &text)); |
| 100 return WideToUTF8(text); |
| 101 } |
| 102 #elif defined(OS_LINUX) |
| 103 GtkWidget* button = GetButton(index); |
| 104 if (button) { |
| 105 gchar* text = gtk_widget_get_tooltip_text(button); |
| 106 std::string rv = std::string(text); |
| 107 g_free(text); |
| 108 return rv; |
| 109 } |
| 110 #endif |
| 111 EXPECT_TRUE(false); |
| 112 return std::string(); |
| 113 } |
| 114 |
| 115 private: |
| 116 #if defined(OS_LINUX) |
| 117 GtkWidget* GetButton(int index) { |
| 118 GtkWidget* rv = NULL; |
| 119 GtkWidget* toolbar = ViewIDUtil::GetWidget( |
| 120 GTK_WIDGET(browser()->window()->GetNativeHandle()), |
| 121 VIEW_ID_BROWSER_ACTION_TOOLBAR); |
| 122 |
| 123 if (toolbar) { |
| 124 GList* children = gtk_container_get_children(GTK_CONTAINER(toolbar)); |
| 125 rv = static_cast<GtkWidget*>(g_list_nth(children, index)->data); |
| 126 g_list_free(children); |
| 127 } |
| 128 |
| 129 return rv; |
| 130 } |
| 131 #endif |
| 132 }; |
| 133 |
| 134 IN_PROC_BROWSER_TEST_F(BrowserActionTest, Basic) { |
| 20 StartHTTPServer(); | 135 StartHTTPServer(); |
| 21 ASSERT_TRUE(RunExtensionTest("browser_action")) << message_; | 136 ASSERT_TRUE(RunExtensionTest("browser_action")) << message_; |
| 22 | 137 |
| 23 // Test that there is a browser action in the toolbar. | 138 // Test that there is a browser action in the toolbar. |
| 24 BrowserActionsContainer* browser_actions = | 139 ASSERT_EQ(1, NumberOfBrowserActions()); |
| 25 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> | |
| 26 browser_actions(); | |
| 27 ASSERT_EQ(1, browser_actions->num_browser_actions()); | |
| 28 | 140 |
| 29 // Tell the extension to update the browser action state. | 141 // Tell the extension to update the browser action state. |
| 30 ResultCatcher catcher; | 142 ResultCatcher catcher; |
| 31 ExtensionsService* service = browser()->profile()->GetExtensionsService(); | 143 ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 32 Extension* extension = service->extensions()->at(0); | 144 Extension* extension = service->extensions()->at(0); |
| 33 ui_test_utils::NavigateToURL(browser(), | 145 ui_test_utils::NavigateToURL(browser(), |
| 34 GURL(extension->GetResourceURL("update.html"))); | 146 GURL(extension->GetResourceURL("update.html"))); |
| 35 ASSERT_TRUE(catcher.GetNextResult()); | 147 ASSERT_TRUE(catcher.GetNextResult()); |
| 36 | 148 |
| 37 // Test that we received the changes. | 149 // Test that we received the changes. |
| 38 ExtensionAction* action = extension->browser_action(); | 150 ExtensionAction* action = extension->browser_action(); |
| 39 ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId)); | 151 ASSERT_EQ("Modified", action->GetTitle(ExtensionAction::kDefaultTabId)); |
| 40 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId)); | 152 ASSERT_EQ("badge", action->GetBadgeText(ExtensionAction::kDefaultTabId)); |
| 41 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255), | 153 ASSERT_EQ(SkColorSetARGB(255, 255, 255, 255), |
| 42 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId)); | 154 action->GetBadgeBackgroundColor(ExtensionAction::kDefaultTabId)); |
| 43 | 155 |
| 44 // Simulate the browser action being clicked. | 156 // Simulate the browser action being clicked. |
| 45 ui_test_utils::NavigateToURL(browser(), | 157 ui_test_utils::NavigateToURL(browser(), |
| 46 GURL("http://localhost:1337/files/extensions/test_file.txt")); | 158 GURL("http://localhost:1337/files/extensions/test_file.txt")); |
| 47 | 159 |
| 48 int window_id = ExtensionTabUtil::GetWindowId(browser()); | |
| 49 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( | 160 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( |
| 50 browser()->profile(), action->extension_id(), browser()); | 161 browser()->profile(), action->extension_id(), browser()); |
| 51 | 162 |
| 52 // Verify the command worked. | 163 // Verify the command worked. |
| 53 TabContents* tab = browser()->GetSelectedTabContents(); | 164 TabContents* tab = browser()->GetSelectedTabContents(); |
| 54 bool result = false; | 165 bool result = false; |
| 55 ui_test_utils::ExecuteJavaScriptAndExtractBool( | 166 ui_test_utils::ExecuteJavaScriptAndExtractBool( |
| 56 tab->render_view_host(), L"", | 167 tab->render_view_host(), L"", |
| 57 L"setInterval(function(){" | 168 L"setInterval(function(){" |
| 58 L" if(document.body.bgColor == 'red'){" | 169 L" if(document.body.bgColor == 'red'){" |
| 59 L" window.domAutomationController.send(true)}}, 100)", | 170 L" window.domAutomationController.send(true)}}, 100)", |
| 60 &result); | 171 &result); |
| 61 ASSERT_TRUE(result); | 172 ASSERT_TRUE(result); |
| 62 } | 173 } |
| 63 | 174 |
| 64 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DynamicBrowserAction) { | 175 IN_PROC_BROWSER_TEST_F(BrowserActionTest, DynamicBrowserAction) { |
| 65 ASSERT_TRUE(RunExtensionTest("browser_action_no_icon")) << message_; | 176 ASSERT_TRUE(RunExtensionTest("browser_action_no_icon")) << message_; |
| 66 | 177 |
| 67 // Test that there is a browser action in the toolbar and that it has no icon. | 178 // Test that there is a browser action in the toolbar and that it has no icon. |
| 68 BrowserActionsContainer* browser_actions = | 179 ASSERT_EQ(1, NumberOfBrowserActions()); |
| 69 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> | 180 EXPECT_TRUE(IsIconNull(0)); |
| 70 browser_actions(); | |
| 71 ASSERT_EQ(1, browser_actions->num_browser_actions()); | |
| 72 ASSERT_TRUE(browser_actions->GetBrowserActionViewAt(0)->button()->icon() | |
| 73 .empty()); | |
| 74 | 181 |
| 75 // Tell the extension to update the icon using setIcon({imageData:...}). | 182 // Tell the extension to update the icon using setIcon({imageData:...}). |
| 76 ResultCatcher catcher; | 183 ResultCatcher catcher; |
| 77 ExtensionsService* service = browser()->profile()->GetExtensionsService(); | 184 ExtensionsService* service = browser()->profile()->GetExtensionsService(); |
| 78 Extension* extension = service->extensions()->at(0); | 185 Extension* extension = service->extensions()->at(0); |
| 79 ui_test_utils::NavigateToURL(browser(), | 186 ui_test_utils::NavigateToURL(browser(), |
| 80 GURL(extension->GetResourceURL("update.html"))); | 187 GURL(extension->GetResourceURL("update.html"))); |
| 81 ASSERT_TRUE(catcher.GetNextResult()); | 188 ASSERT_TRUE(catcher.GetNextResult()); |
| 82 | 189 |
| 83 // Test that we received the changes. | 190 // Test that we received the changes. |
| 84 ASSERT_FALSE(browser_actions->GetBrowserActionViewAt(0)->button()->icon() | 191 EXPECT_FALSE(IsIconNull(0)); |
| 85 .empty()); | |
| 86 | 192 |
| 87 // Tell the extension to update using setIcon({path:...}); | 193 // Tell the extension to update using setIcon({path:...}); |
| 88 ui_test_utils::NavigateToURL(browser(), | 194 ui_test_utils::NavigateToURL(browser(), |
| 89 GURL(extension->GetResourceURL("update2.html"))); | 195 GURL(extension->GetResourceURL("update2.html"))); |
| 90 ASSERT_TRUE(catcher.GetNextResult()); | 196 ASSERT_TRUE(catcher.GetNextResult()); |
| 91 | 197 |
| 92 // Test that we received the changes. | 198 // Test that we received the changes. |
| 93 ASSERT_FALSE(browser_actions->GetBrowserActionViewAt(0)->button()->icon() | 199 EXPECT_FALSE(IsIconNull(0)); |
| 94 .empty()); | |
| 95 | 200 |
| 96 // TODO(aa): Would be nice here to actually compare that the pixels change. | 201 // TODO(aa): Would be nice here to actually compare that the pixels change. |
| 97 } | 202 } |
| 98 | 203 |
| 99 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabSpecificBrowserActionState) { | 204 IN_PROC_BROWSER_TEST_F(BrowserActionTest, TabSpecificBrowserActionState) { |
| 100 ASSERT_TRUE(RunExtensionTest("browser_action_tab_specific_state")) | 205 ASSERT_TRUE(RunExtensionTest("browser_action_tab_specific_state")) << |
| 101 << message_; | 206 message_; |
| 102 | 207 |
| 103 // Test that there is a browser action in the toolbar and that it has no icon. | 208 // Test that there is a browser action in the toolbar and that it has an icon. |
| 104 BrowserActionsContainer* browser_actions = | 209 ASSERT_EQ(1, NumberOfBrowserActions()); |
| 105 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> | 210 EXPECT_FALSE(IsIconNull(0)); |
| 106 browser_actions(); | |
| 107 ASSERT_EQ(1, browser_actions->num_browser_actions()); | |
| 108 ASSERT_FALSE(browser_actions->GetBrowserActionViewAt(0)->button()->icon() | |
| 109 .empty()); | |
| 110 | 211 |
| 111 // Execute the action, its title should change | 212 // Execute the action, its title should change. |
| 112 std::wstring text; | |
| 113 ResultCatcher catcher; | 213 ResultCatcher catcher; |
| 114 browser_actions->TestExecuteBrowserAction(0); | 214 ExecuteBrowserAction(0); |
| 115 ASSERT_TRUE(catcher.GetNextResult()); | 215 ASSERT_TRUE(catcher.GetNextResult()); |
| 116 ASSERT_TRUE( | 216 EXPECT_EQ("Showing icon 2", GetTooltip(0)); |
| 117 browser_actions->GetBrowserActionViewAt(0)->button()->GetTooltipText( | |
| 118 0, 0, &text)); | |
| 119 ASSERT_EQ(L"Showing icon 2", text); | |
| 120 | 217 |
| 121 // open a new tab, the title should go back | 218 // Open a new tab, the title should go back. |
| 122 browser()->NewTab(); | 219 browser()->NewTab(); |
| 123 ASSERT_TRUE( | 220 EXPECT_EQ("hi!", GetTooltip(0)); |
| 124 browser_actions->GetBrowserActionViewAt(0)->button()->GetTooltipText( | |
| 125 0, 0, &text)); | |
| 126 ASSERT_EQ(L"hi!", text); | |
| 127 | 221 |
| 128 // go back to first tab, changed title should reappear | 222 // Go back to first tab, changed title should reappear. |
| 129 browser()->SelectTabContentsAt(0, true); | 223 browser()->SelectTabContentsAt(0, true); |
| 130 ASSERT_TRUE( | 224 EXPECT_EQ("Showing icon 2", GetTooltip(0)); |
| 131 browser_actions->GetBrowserActionViewAt(0)->button()->GetTooltipText( | |
| 132 0, 0, &text)); | |
| 133 ASSERT_EQ(L"Showing icon 2", text); | |
| 134 | 225 |
| 135 // reload that tab, default title should come back | 226 // Reload that tab, default title should come back. |
| 136 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | 227 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
| 137 ASSERT_TRUE( | 228 EXPECT_EQ("hi!", GetTooltip(0)); |
| 138 browser_actions->GetBrowserActionViewAt(0)->button()->GetTooltipText( | |
| 139 0, 0, &text)); | |
| 140 ASSERT_EQ(L"hi!", text); | |
| 141 } | 229 } |
| 142 | 230 |
| 143 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserActionPopup) { | 231 // TODO(estade): port to Linux. |
| 232 #if defined(OS_WIN) |
| 233 IN_PROC_BROWSER_TEST_F(BrowserActionTest, BrowserActionPopup) { |
| 144 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("popup"))); | 234 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("popup"))); |
| 145 | 235 |
| 146 ResultCatcher catcher; | 236 ResultCatcher catcher; |
| 147 BrowserActionsContainer* browser_actions = | |
| 148 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> | |
| 149 browser_actions(); | |
| 150 | 237 |
| 151 // This value is in api_test/popup/popup.html. | 238 // This value is in api_test/popup/popup.html. |
| 152 const int growFactor = 500; | 239 const int growFactor = 500; |
| 153 ASSERT_GT(ExtensionPopup::kMinHeight + growFactor * 2, | 240 ASSERT_GT(ExtensionPopup::kMinHeight + growFactor * 2, |
| 154 ExtensionPopup::kMaxHeight); | 241 ExtensionPopup::kMaxHeight); |
| 155 ASSERT_GT(ExtensionPopup::kMinWidth + growFactor * 2, | 242 ASSERT_GT(ExtensionPopup::kMinWidth + growFactor * 2, |
| 156 ExtensionPopup::kMaxWidth); | 243 ExtensionPopup::kMaxWidth); |
| 157 | 244 |
| 158 // Our initial expected size. | 245 // Our initial expected size. |
| 159 int width = ExtensionPopup::kMinWidth; | 246 int width = ExtensionPopup::kMinWidth; |
| 160 int height = ExtensionPopup::kMinHeight; | 247 int height = ExtensionPopup::kMinHeight; |
| 161 | 248 |
| 249 BrowserActionsContainer* browser_actions = |
| 250 browser()->window()->GetBrowserWindowTesting()->GetToolbarView()-> |
| 251 browser_actions(); |
| 252 ASSERT_TRUE(browser_actions); |
| 162 // Simulate a click on the browser action and verify the size of the resulting | 253 // Simulate a click on the browser action and verify the size of the resulting |
| 163 // popup. The first one tries to be 0x0, so it should be the min values. | 254 // popup. The first one tries to be 0x0, so it should be the min values. |
| 164 browser_actions->TestExecuteBrowserAction(0); | 255 ExecuteBrowserAction(0); |
| 165 EXPECT_TRUE(browser_actions->TestGetPopup() != NULL); | 256 EXPECT_TRUE(browser_actions->TestGetPopup() != NULL); |
| 166 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 257 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 167 gfx::Rect bounds = browser_actions->TestGetPopup()->view()->bounds(); | 258 gfx::Rect bounds = browser_actions->TestGetPopup()->view()->bounds(); |
| 168 EXPECT_EQ(width, bounds.width()); | 259 EXPECT_EQ(width, bounds.width()); |
| 169 EXPECT_EQ(height, bounds.height()); | 260 EXPECT_EQ(height, bounds.height()); |
| 170 browser_actions->HidePopup(); | 261 browser_actions->HidePopup(); |
| 171 EXPECT_TRUE(browser_actions->TestGetPopup() == NULL); | 262 EXPECT_TRUE(browser_actions->TestGetPopup() == NULL); |
| 172 | 263 |
| 173 // Do it again, and verify the new bigger size (the popup grows each time it's | 264 // Do it again, and verify the new bigger size (the popup grows each time it's |
| 174 // opened). | 265 // opened). |
| (...skipping 13 matching lines...) Expand all Loading... |
| 188 height = ExtensionPopup::kMaxHeight; | 279 height = ExtensionPopup::kMaxHeight; |
| 189 browser_actions->TestExecuteBrowserAction(0); | 280 browser_actions->TestExecuteBrowserAction(0); |
| 190 EXPECT_TRUE(browser_actions->TestGetPopup() != NULL); | 281 EXPECT_TRUE(browser_actions->TestGetPopup() != NULL); |
| 191 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 282 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 192 bounds = browser_actions->TestGetPopup()->view()->bounds(); | 283 bounds = browser_actions->TestGetPopup()->view()->bounds(); |
| 193 EXPECT_EQ(width, bounds.width()); | 284 EXPECT_EQ(width, bounds.width()); |
| 194 EXPECT_EQ(height, bounds.height()); | 285 EXPECT_EQ(height, bounds.height()); |
| 195 browser_actions->HidePopup(); | 286 browser_actions->HidePopup(); |
| 196 EXPECT_TRUE(browser_actions->TestGetPopup() == NULL); | 287 EXPECT_TRUE(browser_actions->TestGetPopup() == NULL); |
| 197 } | 288 } |
| 289 #endif |
| OLD | NEW |