OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/browser_action_test_util.h" |
| 6 #include "chrome/browser/extensions/extension_action.h" |
| 7 #include "chrome/browser/extensions/extension_action_manager.h" |
| 8 #include "chrome/browser/extensions/extension_apitest.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 13 #include "chrome/browser/ui/browser_finder.h" |
| 14 #include "chrome/browser/ui/browser_list.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/common/extensions/permissions/permissions_data.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 |
| 21 namespace extensions { |
| 22 namespace { |
| 23 |
| 24 // chrome.browserAction API tests that interact with the UI in such a way that |
| 25 // they cannot be run concurrently (i.e. openPopup API tests that require the |
| 26 // window be focused/active). |
| 27 class BrowserActionInteractiveTest : public ExtensionApiTest { |
| 28 public: |
| 29 BrowserActionInteractiveTest() {} |
| 30 virtual ~BrowserActionInteractiveTest() {} |
| 31 |
| 32 protected: |
| 33 // Function to control whether to run popup tests for the current platform. |
| 34 // These tests require RunExtensionSubtest to work as expected and the browser |
| 35 // window to able to be made active automatically. Returns false for platforms |
| 36 // where these conditions are not met. |
| 37 bool ShouldRunPopupTest() { |
| 38 // TODO(justinlin): http://crbug.com/177163 |
| 39 #if defined(OS_WIN) && !defined(NDEBUG) |
| 40 return false; |
| 41 #elif defined(OS_MACOSX) |
| 42 // TODO(justinlin): Browser window do not become active on Mac even when |
| 43 // Activate() is called on them. Enable when/if it's possible to fix. |
| 44 return false; |
| 45 #else |
| 46 return true; |
| 47 #endif |
| 48 } |
| 49 }; |
| 50 |
| 51 // Tests opening a popup using the chrome.browserAction.openPopup API. This test |
| 52 // opens a popup in the starting window, closes the popup, creates a new window |
| 53 // and opens a popup in the new window. Both popups should succeed in opening. |
| 54 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopup) { |
| 55 if (!ShouldRunPopupTest()) |
| 56 return; |
| 57 |
| 58 BrowserActionTestUtil browserActionBar = BrowserActionTestUtil(browser()); |
| 59 // Setup extension message listener to wait for javascript to finish running. |
| 60 ExtensionTestMessageListener listener("ready", true); |
| 61 { |
| 62 // Setup the notification observer to wait for the popup to finish loading. |
| 63 content::WindowedNotificationObserver frame_observer( |
| 64 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 65 content::NotificationService::AllSources()); |
| 66 // Show first popup in first window and expect it to have loaded. |
| 67 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup", |
| 68 "open_popup_succeeds.html")) << message_; |
| 69 frame_observer.Wait(); |
| 70 EXPECT_TRUE(browserActionBar.HasPopup()); |
| 71 browserActionBar.HidePopup(); |
| 72 } |
| 73 |
| 74 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 75 Browser* new_browser = NULL; |
| 76 { |
| 77 content::WindowedNotificationObserver frame_observer( |
| 78 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 79 content::NotificationService::AllSources()); |
| 80 // Open a new window. |
| 81 new_browser = chrome::FindBrowserWithWebContents( |
| 82 browser()->OpenURL(content::OpenURLParams( |
| 83 GURL("about:"), content::Referrer(), NEW_WINDOW, |
| 84 content::PAGE_TRANSITION_TYPED, false))); |
| 85 #if defined(OS_WIN) |
| 86 // Hide all the buttons to test that it opens even when browser action is |
| 87 // in the overflow bucket. |
| 88 // TODO(justinlin): Implement for other platforms. |
| 89 browserActionBar.SetIconVisibilityCount(0); |
| 90 #endif |
| 91 frame_observer.Wait(); |
| 92 } |
| 93 |
| 94 ResultCatcher catcher; |
| 95 { |
| 96 content::WindowedNotificationObserver frame_observer( |
| 97 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 98 content::NotificationService::AllSources()); |
| 99 // Show second popup in new window. |
| 100 listener.Reply(""); |
| 101 frame_observer.Wait(); |
| 102 EXPECT_TRUE(BrowserActionTestUtil(new_browser).HasPopup()); |
| 103 } |
| 104 ASSERT_TRUE(catcher.GetNextResult()) << message_; |
| 105 } |
| 106 |
| 107 // Tests opening a popup in an incognito window. |
| 108 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, TestOpenPopupIncognito) { |
| 109 if (!ShouldRunPopupTest()) |
| 110 return; |
| 111 |
| 112 content::WindowedNotificationObserver frame_observer( |
| 113 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 114 content::NotificationService::AllSources()); |
| 115 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup", |
| 116 "open_popup_succeeds.html", |
| 117 kFlagEnableIncognito | kFlagUseIncognito)) |
| 118 << message_; |
| 119 frame_observer.Wait(); |
| 120 // Non-Aura Linux uses a singleton for the popup, so it looks like all windows |
| 121 // have popups if there is any popup open. |
| 122 #if !(defined(OS_LINUX) && !defined(USE_AURA)) |
| 123 // Starting window does not have a popup. |
| 124 EXPECT_FALSE(BrowserActionTestUtil(browser()).HasPopup()); |
| 125 #endif |
| 126 // Incognito window should have a popup. |
| 127 EXPECT_TRUE(BrowserActionTestUtil(BrowserList::GetInstance( |
| 128 chrome::GetActiveDesktop())->GetLastActive()).HasPopup()); |
| 129 } |
| 130 |
| 131 // Tests if there is already a popup open (by a user click or otherwise), that |
| 132 // the openPopup API does not override it. |
| 133 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, |
| 134 TestOpenPopupDoesNotCloseOtherPopups) { |
| 135 if (!ShouldRunPopupTest()) |
| 136 return; |
| 137 |
| 138 // Load a first extension that can open a popup. |
| 139 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII( |
| 140 "browser_action/popup"))); |
| 141 const Extension* extension = GetSingleLoadedExtension(); |
| 142 ASSERT_TRUE(extension) << message_; |
| 143 |
| 144 ExtensionTestMessageListener listener("ready", true); |
| 145 // Load the test extension which will do nothing except notifyPass() to |
| 146 // return control here. |
| 147 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup", |
| 148 "open_popup_fails.html")) << message_; |
| 149 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 150 |
| 151 content::WindowedNotificationObserver frame_observer( |
| 152 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 153 content::NotificationService::AllSources()); |
| 154 // Open popup in the first extension. |
| 155 BrowserActionTestUtil(browser()).Press(0); |
| 156 frame_observer.Wait(); |
| 157 EXPECT_TRUE(BrowserActionTestUtil(browser()).HasPopup()); |
| 158 |
| 159 ResultCatcher catcher; |
| 160 // Return control to javascript to validate that opening a popup fails now. |
| 161 listener.Reply(""); |
| 162 ASSERT_TRUE(catcher.GetNextResult()) << message_; |
| 163 } |
| 164 |
| 165 // Test that openPopup does not grant tab permissions like for browser action |
| 166 // clicks if the activeTab permission is set. |
| 167 IN_PROC_BROWSER_TEST_F(BrowserActionInteractiveTest, |
| 168 TestOpenPopupDoesNotGrantTabPermissions) { |
| 169 if (!ShouldRunPopupTest()) |
| 170 return; |
| 171 |
| 172 content::WindowedNotificationObserver frame_observer( |
| 173 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
| 174 content::NotificationService::AllSources()); |
| 175 ASSERT_TRUE(RunExtensionSubtest("browser_action/open_popup", |
| 176 "open_popup_succeeds.html")) << message_; |
| 177 frame_observer.Wait(); |
| 178 |
| 179 ExtensionService* service = extensions::ExtensionSystem::Get( |
| 180 browser()->profile())->extension_service(); |
| 181 ASSERT_FALSE(PermissionsData::HasAPIPermissionForTab( |
| 182 service->GetExtensionById(last_loaded_extension_id_, false), |
| 183 SessionID::IdForTab(browser()->tab_strip_model()->GetActiveWebContents()), |
| 184 APIPermission::kTab)); |
| 185 } |
| 186 |
| 187 } // namespace |
| 188 } // namespace extensions |
OLD | NEW |