[email protected] | 35abc33 | 2012-02-24 23:48:38 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 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 "base/command_line.h" |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 6 | #include "base/memory/scoped_vector.h" |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 7 | #include "base/stringprintf.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 12 | #include "chrome/common/chrome_switches.h" |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 13 | #include "chrome/common/extensions/extension.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 14 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 16 | #include "net/base/mock_host_resolver.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 17 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 18 | using content::OpenURLParams; |
| 19 | using content::Referrer; |
| 20 | |
[email protected] | e78eb29 | 2010-12-22 08:35:39 | [diff] [blame] | 21 | // Disabled, http://crbug.com/64899. |
| 22 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) { |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 23 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 24 | switches::kEnableExperimentalExtensionApis); |
| 25 | |
| 26 | ResultCatcher catcher; |
| 27 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 28 | .AppendASCII("window_open").AppendASCII("spanning"))); |
| 29 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 30 | } |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 31 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 32 | void WaitForTabsAndPopups(Browser* browser, |
| 33 | int num_tabs, |
| 34 | int num_popups, |
| 35 | int num_panels) { |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 36 | SCOPED_TRACE( |
| 37 | StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d", |
| 38 | num_tabs, num_popups, num_panels)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 39 | // We start with one tab and one browser already open. |
| 40 | ++num_tabs; |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 41 | size_t num_browsers = static_cast<size_t>(num_popups + num_panels) + 1; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 42 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 43 | const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(15); |
| 44 | base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime; |
| 45 | while (base::TimeTicks::Now() < end_time) { |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 46 | if (BrowserList::GetBrowserCount(browser->profile()) == num_browsers && |
| 47 | browser->tab_count() == num_tabs) |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 48 | break; |
| 49 | |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 50 | ui_test_utils::RunAllPendingInMessageLoop(); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | EXPECT_EQ(num_browsers, BrowserList::GetBrowserCount(browser->profile())); |
| 54 | EXPECT_EQ(num_tabs, browser->tab_count()); |
| 55 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 56 | int num_popups_seen = 0; |
| 57 | int num_panels_seen = 0; |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 58 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 59 | iter != BrowserList::end(); ++iter) { |
| 60 | if (*iter == browser) |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 61 | continue; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 62 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 63 | // Check for TYPE_POPUP or TYPE_PANEL. |
| 64 | EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel()); |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 65 | if ((*iter)->is_type_popup()) |
| 66 | ++num_popups_seen; |
| 67 | else |
| 68 | ++num_panels_seen; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 69 | } |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 70 | EXPECT_EQ(num_popups, num_popups_seen); |
| 71 | EXPECT_EQ(num_panels, num_panels_seen); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 74 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) { |
| 75 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 76 | ASSERT_TRUE(StartTestServer()); |
| 77 | ASSERT_TRUE(LoadExtension( |
| 78 | test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app"))); |
| 79 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 80 | WaitForTabsAndPopups(browser(), 0, 2, 0); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 81 | |
| 82 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 83 | iter != BrowserList::end(); ++iter) { |
| 84 | if (*iter == browser()) |
| 85 | ASSERT_FALSE((*iter)->is_app()); |
| 86 | else |
| 87 | ASSERT_TRUE((*iter)->is_app()); |
| 88 | } |
| 89 | } |
| 90 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 91 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) { |
| 92 | ASSERT_TRUE(StartTestServer()); |
| 93 | ASSERT_TRUE(LoadExtension( |
| 94 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup"))); |
| 95 | |
| 96 | const int num_tabs = 1; |
| 97 | const int num_popups = 0; |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 98 | WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) { |
| 102 | ASSERT_TRUE(StartTestServer()); |
| 103 | ASSERT_TRUE(LoadExtension( |
| 104 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large"))); |
| 105 | |
| 106 | #if defined(OS_CHROMEOS) |
| 107 | // On ChromeOS this should open a new tab. |
| 108 | const int num_tabs = 1; |
| 109 | const int num_popups = 0; |
| 110 | #else |
| 111 | // On other systems this should open a new popup window. |
| 112 | const int num_tabs = 0; |
| 113 | const int num_popups = 1; |
| 114 | #endif |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 115 | WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) { |
| 119 | ASSERT_TRUE(StartTestServer()); |
| 120 | ASSERT_TRUE(LoadExtension( |
| 121 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small"))); |
| 122 | |
| 123 | // On ChromeOS this should open a new panel (acts like a new popup window). |
| 124 | // On other systems this should open a new popup window. |
| 125 | const int num_tabs = 0; |
| 126 | const int num_popups = 1; |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 127 | WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 130 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) { |
| 131 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 132 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 133 | |
| 134 | ASSERT_TRUE(LoadExtension( |
| 135 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 136 | .AppendASCII("extension"))); |
| 137 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 138 | WaitForTabsAndPopups(browser(), 5, 3, 0); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) { |
| 142 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 143 | ASSERT_TRUE(test_server()->Start()); |
| 144 | |
| 145 | ASSERT_TRUE(LoadExtension( |
| 146 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 147 | .AppendASCII("hosted_app"))); |
| 148 | |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 149 | // The app being tested owns the domain a.com . The test URLs we navigate |
| 150 | // to below must be within that domain, so that they fall within the app's |
| 151 | // web extent. |
| 152 | GURL::Replacements replace_host; |
| 153 | std::string a_dot_com = "a.com"; |
| 154 | replace_host.SetHostStr(a_dot_com); |
| 155 | |
| 156 | const std::string popup_app_contents_path( |
| 157 | "files/extensions/api_test/window_open/popup_blocking/hosted_app/"); |
| 158 | |
| 159 | GURL open_tab = |
| 160 | test_server()->GetURL(popup_app_contents_path + "open_tab.html") |
| 161 | .ReplaceComponents(replace_host); |
| 162 | GURL open_popup = |
| 163 | test_server()->GetURL(popup_app_contents_path + "open_popup.html") |
| 164 | .ReplaceComponents(replace_host); |
| 165 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 166 | browser()->OpenURL(OpenURLParams( |
| 167 | open_tab, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, |
| 168 | false)); |
| 169 | browser()->OpenURL(OpenURLParams( |
| 170 | open_popup, Referrer(), NEW_FOREGROUND_TAB, |
| 171 | content::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 172 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 173 | WaitForTabsAndPopups(browser(), 3, 1, 0); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 174 | } |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 175 | |
[email protected] | e7f9056 | 2011-05-23 21:38:24 | [diff] [blame] | 176 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) { |
| 177 | ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_; |
| 178 | } |
| 179 | |
[email protected] | d5c1406 | 2011-10-27 00:02:13 | [diff] [blame] | 180 | class WindowOpenPanelDisabledTest : public ExtensionApiTest { |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 181 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 182 | ExtensionApiTest::SetUpCommandLine(command_line); |
[email protected] | 7e7a2809 | 2011-12-09 22:24:55 | [diff] [blame] | 183 | // TODO(jennb): Re-enable when panels are enabled by default. |
| 184 | // command_line->AppendSwitch(switches::kDisablePanels); |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
[email protected] | 7e7a2809 | 2011-12-09 22:24:55 | [diff] [blame] | 188 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest, |
| 189 | DISABLED_WindowOpenPanelNotEnabled) { |
[email protected] | d5c1406 | 2011-10-27 00:02:13 | [diff] [blame] | 190 | ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_; |
| 191 | } |
| 192 | |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 193 | class WindowOpenPanelTest : public ExtensionApiTest { |
| 194 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 195 | ExtensionApiTest::SetUpCommandLine(command_line); |
| 196 | command_line->AppendSwitch(switches::kEnablePanels); |
| 197 | } |
| 198 | }; |
| 199 | |
| 200 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenPanel) { |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 201 | ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_; |
| 202 | } |
[email protected] | 54edcea | 2011-07-27 01:56:38 | [diff] [blame] | 203 | |
[email protected] | 4ff65f1 | 2011-10-06 00:35:54 | [diff] [blame] | 204 | #if defined(OS_MACOSX) || defined(OS_WIN) |
| 205 | // Focus test fails if there is no window manager on Linux. |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 206 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenFocus) { |
[email protected] | 4ff65f1 | 2011-10-06 00:35:54 | [diff] [blame] | 207 | ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_; |
| 208 | } |
| 209 | #endif |
| 210 | |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 211 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 212 | CloseNonExtensionPanelsOnUninstall) { |
| 213 | ASSERT_TRUE(StartTestServer()); |
| 214 | |
| 215 | // Setup listeners to wait on strings we expect the extension pages to send. |
| 216 | std::vector<std::string> test_strings; |
| 217 | test_strings.push_back("content_tab"); |
| 218 | test_strings.push_back("content_panel"); |
| 219 | test_strings.push_back("content_popup"); |
| 220 | |
| 221 | ScopedVector<ExtensionTestMessageListener> listeners; |
| 222 | for (size_t i = 0; i < test_strings.size(); ++i) { |
| 223 | listeners.push_back( |
| 224 | new ExtensionTestMessageListener(test_strings[i], false)); |
| 225 | } |
| 226 | |
| 227 | const Extension* extension = LoadExtension( |
| 228 | test_data_dir_.AppendASCII("window_open").AppendASCII( |
| 229 | "close_panels_on_uninstall")); |
| 230 | ASSERT_TRUE(extension); |
| 231 | |
| 232 | // Two tabs. One in extension domain and one in non-extension domain. |
| 233 | // Two popups - one in extension domain and one in non-extension domain. |
| 234 | // Two panels - one in extension domain and one in non-extension domain. |
| 235 | WaitForTabsAndPopups(browser(), 2, 2, 2); |
| 236 | |
| 237 | // Wait on test messages to make sure the pages loaded. |
| 238 | for (size_t i = 0; i < listeners.size(); ++i) |
| 239 | ASSERT_TRUE(listeners[i]->WaitUntilSatisfied()); |
| 240 | |
| 241 | UninstallExtension(extension->id()); |
| 242 | |
| 243 | // Wait for one tab and one popup in non-extension domain to stay open. |
| 244 | // Expect everything else, including panels, to close. |
| 245 | WaitForTabsAndPopups(browser(), 1, 1, 0); |
| 246 | } |
| 247 | |
[email protected] | 24c6bcfc | 2012-02-29 21:06:36 | [diff] [blame^] | 248 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) { |
[email protected] | 54edcea | 2011-07-27 01:56:38 | [diff] [blame] | 249 | ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_; |
| 250 | } |