[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" |
| 6 | #include "chrome/browser/extensions/extension_apitest.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 7 | #include "chrome/browser/ui/browser.h" |
| 8 | #include "chrome/browser/ui/browser_list.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 9 | #include "chrome/common/chrome_switches.h" |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 10 | #include "chrome/test/ui_test_utils.h" |
| 11 | #include "net/base/mock_host_resolver.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 12 | |
[email protected] | e78eb29 | 2010-12-22 08:35:39 | [diff] [blame] | 13 | // Disabled, http://crbug.com/64899. |
| 14 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) { |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 15 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 16 | switches::kEnableExperimentalExtensionApis); |
| 17 | |
| 18 | ResultCatcher catcher; |
| 19 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 20 | .AppendASCII("window_open").AppendASCII("spanning"))); |
| 21 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 22 | } |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 23 | |
| 24 | void WaitForTabsAndPopups(Browser* browser, int num_tabs, int num_popups) { |
| 25 | // We start with one tab and one browser already open. |
| 26 | ++num_tabs; |
| 27 | size_t num_browsers = static_cast<size_t>(num_popups) + 1; |
| 28 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame^] | 29 | const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(15); |
| 30 | base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime; |
| 31 | while (base::TimeTicks::Now() < end_time) { |
| 32 | if (BrowserList::GetBrowserCount(browser->profile()) >= num_browsers && |
| 33 | browser->tab_count() >= num_tabs) |
| 34 | break; |
| 35 | |
| 36 | MessageLoopForUI::current()->RunAllPending(); |
| 37 | } |
| 38 | |
| 39 | EXPECT_EQ(num_browsers, BrowserList::GetBrowserCount(browser->profile())); |
| 40 | EXPECT_EQ(num_tabs, browser->tab_count()); |
| 41 | |
| 42 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 43 | iter != BrowserList::end(); ++iter) { |
| 44 | if (*iter == browser) |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 45 | continue; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 46 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame^] | 47 | // Check for TYPE_POPUP or TYPE_PANEL. |
| 48 | EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 52 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) { |
| 53 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 54 | ASSERT_TRUE(StartTestServer()); |
| 55 | ASSERT_TRUE(LoadExtension( |
| 56 | test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app"))); |
| 57 | |
| 58 | WaitForTabsAndPopups(browser(), 0, 2); |
| 59 | |
| 60 | for (BrowserList::const_iterator iter = BrowserList::begin(); |
| 61 | iter != BrowserList::end(); ++iter) { |
| 62 | if (*iter == browser()) |
| 63 | ASSERT_FALSE((*iter)->is_app()); |
| 64 | else |
| 65 | ASSERT_TRUE((*iter)->is_app()); |
| 66 | } |
| 67 | } |
| 68 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame^] | 69 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) { |
| 70 | ASSERT_TRUE(StartTestServer()); |
| 71 | ASSERT_TRUE(LoadExtension( |
| 72 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup"))); |
| 73 | |
| 74 | const int num_tabs = 1; |
| 75 | const int num_popups = 0; |
| 76 | WaitForTabsAndPopups(browser(), num_tabs, num_popups); |
| 77 | } |
| 78 | |
| 79 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) { |
| 80 | ASSERT_TRUE(StartTestServer()); |
| 81 | ASSERT_TRUE(LoadExtension( |
| 82 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large"))); |
| 83 | |
| 84 | #if defined(OS_CHROMEOS) |
| 85 | // On ChromeOS this should open a new tab. |
| 86 | const int num_tabs = 1; |
| 87 | const int num_popups = 0; |
| 88 | #else |
| 89 | // On other systems this should open a new popup window. |
| 90 | const int num_tabs = 0; |
| 91 | const int num_popups = 1; |
| 92 | #endif |
| 93 | WaitForTabsAndPopups(browser(), num_tabs, num_popups); |
| 94 | } |
| 95 | |
| 96 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) { |
| 97 | ASSERT_TRUE(StartTestServer()); |
| 98 | ASSERT_TRUE(LoadExtension( |
| 99 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small"))); |
| 100 | |
| 101 | // On ChromeOS this should open a new panel (acts like a new popup window). |
| 102 | // On other systems this should open a new popup window. |
| 103 | const int num_tabs = 0; |
| 104 | const int num_popups = 1; |
| 105 | WaitForTabsAndPopups(browser(), num_tabs, num_popups); |
| 106 | } |
| 107 | |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 108 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) { |
| 109 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 110 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 111 | |
| 112 | ASSERT_TRUE(LoadExtension( |
| 113 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 114 | .AppendASCII("extension"))); |
| 115 | |
| 116 | WaitForTabsAndPopups(browser(), 5, 3); |
| 117 | } |
| 118 | |
| 119 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) { |
| 120 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 121 | ASSERT_TRUE(test_server()->Start()); |
| 122 | |
| 123 | ASSERT_TRUE(LoadExtension( |
| 124 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 125 | .AppendASCII("hosted_app"))); |
| 126 | |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 127 | // The app being tested owns the domain a.com . The test URLs we navigate |
| 128 | // to below must be within that domain, so that they fall within the app's |
| 129 | // web extent. |
| 130 | GURL::Replacements replace_host; |
| 131 | std::string a_dot_com = "a.com"; |
| 132 | replace_host.SetHostStr(a_dot_com); |
| 133 | |
| 134 | const std::string popup_app_contents_path( |
| 135 | "files/extensions/api_test/window_open/popup_blocking/hosted_app/"); |
| 136 | |
| 137 | GURL open_tab = |
| 138 | test_server()->GetURL(popup_app_contents_path + "open_tab.html") |
| 139 | .ReplaceComponents(replace_host); |
| 140 | GURL open_popup = |
| 141 | test_server()->GetURL(popup_app_contents_path + "open_popup.html") |
| 142 | .ReplaceComponents(replace_host); |
| 143 | |
| 144 | browser()->OpenURL(open_tab, GURL(), NEW_FOREGROUND_TAB, |
| 145 | PageTransition::TYPED); |
| 146 | browser()->OpenURL(open_popup, GURL(), NEW_FOREGROUND_TAB, |
| 147 | PageTransition::TYPED); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 148 | |
| 149 | WaitForTabsAndPopups(browser(), 3, 1); |
| 150 | } |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 151 | |
| 152 | #if defined(OS_MACOSX) || defined(OS_WIN) |
| 153 | // Focus test fails if there is no window manager on Linux. |
| 154 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenFocus) { |
| 155 | ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_; |
| 156 | } |
| 157 | #endif |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 158 | |
[email protected] | e7f9056 | 2011-05-23 21:38:24 | [diff] [blame] | 159 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) { |
| 160 | ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_; |
| 161 | } |
| 162 | |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 163 | class WindowOpenPanelTest : public ExtensionApiTest { |
| 164 | virtual void SetUpCommandLine(CommandLine* command_line) { |
| 165 | ExtensionApiTest::SetUpCommandLine(command_line); |
| 166 | command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis); |
[email protected] | b35b26b3 | 2011-05-05 20:35:14 | [diff] [blame] | 167 | command_line->AppendSwitch(switches::kEnablePanels); |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 168 | } |
| 169 | }; |
| 170 | |
[email protected] | 4dd4750 | 2011-06-06 20:41:04 | [diff] [blame] | 171 | #if !defined(TOOLKIT_VIEWS) |
| 172 | #define MAYBE_WindowOpenPanel WindowOpenPanel |
| 173 | #else |
| 174 | #define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel |
| 175 | #endif |
| 176 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) { |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 177 | ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_; |
| 178 | } |