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