[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] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_host.h" |
| 10 | #include "chrome/browser/extensions/extension_process_manager.h" |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_system.h" |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_test_message_listener.h" |
[email protected] | 2ad4a90 | 2010-11-17 06:05:13 | [diff] [blame] | 13 | #include "chrome/browser/ui/browser.h" |
[email protected] | d874814 | 2012-05-16 21:13:43 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser_finder.h" |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_iterator.h" |
[email protected] | 52877dbc6 | 2012-06-29 22:22:03 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_tabstrip.h" |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 17 | #include "chrome/browser/ui/panels/panel_manager.h" |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 18 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 19 | #include "chrome/common/chrome_switches.h" |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 20 | #include "chrome/common/extensions/extension.h" |
[email protected] | af44e7fb | 2011-07-29 18:32:32 | [diff] [blame] | 21 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 22 | #include "content/public/browser/render_process_host.h" |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 23 | #include "content/public/browser/web_contents.h" |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 24 | #include "content/public/common/result_codes.h" |
[email protected] | 7d478cb | 2012-07-24 17:19:42 | [diff] [blame] | 25 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 26 | #include "net/base/mock_host_resolver.h" |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 27 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 28 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 29 | #if defined(USE_ASH) |
| 30 | #include "chrome/browser/extensions/shell_window_registry.h" |
| 31 | #endif |
| 32 | |
| 33 | #if defined(USE_ASH) && !defined(OS_WIN) |
| 34 | // TODO(stevenjb): Figure out the correct behavior for Ash + Win |
| 35 | #define USE_ASH_PANELS |
| 36 | #endif |
| 37 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 38 | using content::OpenURLParams; |
| 39 | using content::Referrer; |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 40 | using content::WebContents; |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 41 | |
[email protected] | e78eb29 | 2010-12-22 08:35:39 | [diff] [blame] | 42 | // Disabled, http://crbug.com/64899. |
| 43 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) { |
[email protected] | 32b7c49 | 2010-09-01 04:15:00 | [diff] [blame] | 44 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 45 | switches::kEnableExperimentalExtensionApis); |
| 46 | |
| 47 | ResultCatcher catcher; |
| 48 | ASSERT_TRUE(LoadExtensionIncognito(test_data_dir_ |
| 49 | .AppendASCII("window_open").AppendASCII("spanning"))); |
| 50 | EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 51 | } |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 52 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 53 | int GetPanelCount(Browser* browser) { |
| 54 | #if defined(USE_ASH_PANELS) |
| 55 | return static_cast<int>(extensions::ShellWindowRegistry::Get( |
| 56 | browser->profile())->shell_windows().size()); |
| 57 | #else |
| 58 | return PanelManager::GetInstance()->num_panels(); |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | bool WaitForTabsAndPopups(Browser* browser, |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 63 | int num_tabs, |
| 64 | int num_popups, |
| 65 | int num_panels) { |
[email protected] | 58b349b | 2011-11-23 02:07:15 | [diff] [blame] | 66 | SCOPED_TRACE( |
| 67 | StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d", |
| 68 | num_tabs, num_popups, num_panels)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 69 | // We start with one tab and one browser already open. |
| 70 | ++num_tabs; |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 71 | size_t num_browsers = static_cast<size_t>(num_popups) + 1; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 72 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 73 | const base::TimeDelta kWaitTime = base::TimeDelta::FromSeconds(10); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 74 | base::TimeTicks end_time = base::TimeTicks::Now() + kWaitTime; |
| 75 | while (base::TimeTicks::Now() < end_time) { |
[email protected] | 52ff7d93 | 2012-10-28 19:46:46 | [diff] [blame] | 76 | if (chrome::GetBrowserCount(browser->profile()) == num_browsers && |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 77 | browser->tab_strip_model()->count() == num_tabs && |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 78 | GetPanelCount(browser) == num_panels) |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 79 | break; |
| 80 | |
[email protected] | b8deecd | 2012-07-30 21:09:44 | [diff] [blame] | 81 | content::RunAllPendingInMessageLoop(); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 52ff7d93 | 2012-10-28 19:46:46 | [diff] [blame] | 84 | EXPECT_EQ(num_browsers, chrome::GetBrowserCount(browser->profile())); |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 85 | EXPECT_EQ(num_tabs, browser->tab_strip_model()->count()); |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 86 | EXPECT_EQ(num_panels, GetPanelCount(browser)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 87 | |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 88 | int num_popups_seen = 0; |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 89 | for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) { |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 90 | if (*iter == browser) |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 91 | continue; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 92 | |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 93 | // Check for TYPE_POPUP. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 94 | #if defined(USE_ASH_PANELS) |
| 95 | // On Ash, panel windows may open as popup windows. |
[email protected] | 99f4fdb9 | 2012-08-30 23:13:51 | [diff] [blame] | 96 | EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel()); |
| 97 | #else |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 98 | EXPECT_TRUE((*iter)->is_type_popup()); |
[email protected] | 99f4fdb9 | 2012-08-30 23:13:51 | [diff] [blame] | 99 | #endif |
[email protected] | f6ffa7d | 2012-08-17 17:27:08 | [diff] [blame] | 100 | ++num_popups_seen; |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 101 | } |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 102 | EXPECT_EQ(num_popups, num_popups_seen); |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 103 | |
| 104 | return ((num_browsers == chrome::GetBrowserCount(browser->profile())) && |
| 105 | (num_tabs == browser->tab_strip_model()->count()) && |
| 106 | (num_panels == GetPanelCount(browser)) && |
| 107 | (num_popups == num_popups_seen)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 110 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, BrowserIsApp) { |
| 111 | host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 112 | ASSERT_TRUE(StartTestServer()); |
| 113 | ASSERT_TRUE(LoadExtension( |
| 114 | test_data_dir_.AppendASCII("window_open").AppendASCII("browser_is_app"))); |
| 115 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 116 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 0, 2, 0)); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 117 | |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 118 | for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) { |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 119 | if (*iter == browser()) |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 120 | ASSERT_FALSE(iter->is_app()); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 121 | else |
[email protected] | bc44f414 | 2013-02-12 06:15:56 | [diff] [blame] | 122 | ASSERT_TRUE(iter->is_app()); |
[email protected] | f112b0f | 2011-05-26 01:53:52 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 126 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupDefault) { |
| 127 | ASSERT_TRUE(StartTestServer()); |
| 128 | ASSERT_TRUE(LoadExtension( |
| 129 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup"))); |
| 130 | |
| 131 | const int num_tabs = 1; |
| 132 | const int num_popups = 0; |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 133 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupLarge) { |
| 137 | ASSERT_TRUE(StartTestServer()); |
| 138 | ASSERT_TRUE(LoadExtension( |
| 139 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_large"))); |
| 140 | |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 141 | // On other systems this should open a new popup window. |
| 142 | const int num_tabs = 0; |
| 143 | const int num_popups = 1; |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 144 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPopupSmall) { |
| 148 | ASSERT_TRUE(StartTestServer()); |
| 149 | ASSERT_TRUE(LoadExtension( |
| 150 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_small"))); |
| 151 | |
| 152 | // On ChromeOS this should open a new panel (acts like a new popup window). |
| 153 | // On other systems this should open a new popup window. |
| 154 | const int num_tabs = 0; |
| 155 | const int num_popups = 1; |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 156 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0)); |
[email protected] | 0a5bdc652 | 2011-06-08 19:04:49 | [diff] [blame] | 157 | } |
| 158 | |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 159 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) { |
| 160 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | e18a19b | 2010-10-19 20:32:30 | [diff] [blame] | 161 | ASSERT_TRUE(StartTestServer()); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 162 | |
| 163 | ASSERT_TRUE(LoadExtension( |
| 164 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 165 | .AppendASCII("extension"))); |
| 166 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 167 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 5, 3, 0)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingHostedApp) { |
| 171 | host_resolver()->AddRule("*", "127.0.0.1"); |
| 172 | ASSERT_TRUE(test_server()->Start()); |
| 173 | |
| 174 | ASSERT_TRUE(LoadExtension( |
| 175 | test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking") |
| 176 | .AppendASCII("hosted_app"))); |
| 177 | |
[email protected] | fe304887 | 2010-10-18 14:58:59 | [diff] [blame] | 178 | // The app being tested owns the domain a.com . The test URLs we navigate |
| 179 | // to below must be within that domain, so that they fall within the app's |
| 180 | // web extent. |
| 181 | GURL::Replacements replace_host; |
| 182 | std::string a_dot_com = "a.com"; |
| 183 | replace_host.SetHostStr(a_dot_com); |
| 184 | |
| 185 | const std::string popup_app_contents_path( |
| 186 | "files/extensions/api_test/window_open/popup_blocking/hosted_app/"); |
| 187 | |
| 188 | GURL open_tab = |
| 189 | test_server()->GetURL(popup_app_contents_path + "open_tab.html") |
| 190 | .ReplaceComponents(replace_host); |
| 191 | GURL open_popup = |
| 192 | test_server()->GetURL(popup_app_contents_path + "open_popup.html") |
| 193 | .ReplaceComponents(replace_host); |
| 194 | |
[email protected] | e5d549d | 2011-12-28 01:29:20 | [diff] [blame] | 195 | browser()->OpenURL(OpenURLParams( |
| 196 | open_tab, Referrer(), NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, |
| 197 | false)); |
| 198 | browser()->OpenURL(OpenURLParams( |
| 199 | open_popup, Referrer(), NEW_FOREGROUND_TAB, |
| 200 | content::PAGE_TRANSITION_TYPED, false)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 201 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 202 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 3, 1, 0)); |
[email protected] | 4026ce1e | 2010-09-14 19:35:04 | [diff] [blame] | 203 | } |
[email protected] | d4db6c70 | 2011-03-28 21:49:14 | [diff] [blame] | 204 | |
[email protected] | e7f9056 | 2011-05-23 21:38:24 | [diff] [blame] | 205 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) { |
| 206 | ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_; |
| 207 | } |
| 208 | |
[email protected] | d5c1406 | 2011-10-27 00:02:13 | [diff] [blame] | 209 | class WindowOpenPanelDisabledTest : public ExtensionApiTest { |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 210 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 211 | ExtensionApiTest::SetUpCommandLine(command_line); |
[email protected] | 7e7a2809 | 2011-12-09 22:24:55 | [diff] [blame] | 212 | // TODO(jennb): Re-enable when panels are enabled by default. |
| 213 | // command_line->AppendSwitch(switches::kDisablePanels); |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 214 | } |
| 215 | }; |
| 216 | |
[email protected] | 7e7a2809 | 2011-12-09 22:24:55 | [diff] [blame] | 217 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest, |
| 218 | DISABLED_WindowOpenPanelNotEnabled) { |
[email protected] | d5c1406 | 2011-10-27 00:02:13 | [diff] [blame] | 219 | ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_; |
| 220 | } |
| 221 | |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 222 | class WindowOpenPanelTest : public ExtensionApiTest { |
[email protected] | 49aeab6 | 2013-02-07 02:53:11 | [diff] [blame] | 223 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 224 | ExtensionApiTest::SetUpCommandLine(command_line); |
| 225 | command_line->AppendSwitch(switches::kEnablePanels); |
| 226 | } |
| 227 | }; |
| 228 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 229 | #if defined(USE_ASH_PANELS) |
[email protected] | 87d722a | 2012-07-12 15:38:33 | [diff] [blame] | 230 | // On Ash, this currently fails because we're currently opening new panel |
[email protected] | d101b0c | 2012-03-16 00:30:57 | [diff] [blame] | 231 | // windows as popup windows instead. |
[email protected] | e00ccc9 | 2012-11-01 17:32:30 | [diff] [blame] | 232 | #define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel |
[email protected] | d101b0c | 2012-03-16 00:30:57 | [diff] [blame] | 233 | #else |
| 234 | #define MAYBE_WindowOpenPanel WindowOpenPanel |
| 235 | #endif |
| 236 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) { |
[email protected] | 08aa0c3 | 2011-04-11 22:18:37 | [diff] [blame] | 237 | ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_; |
| 238 | } |
[email protected] | 54edcea | 2011-07-27 01:56:38 | [diff] [blame] | 239 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 240 | #if defined(USE_ASH_PANELS) |
[email protected] | ca29ed1 | 2012-09-06 09:10:50 | [diff] [blame] | 241 | // On Ash, this currently fails because we're currently opening new panel |
| 242 | // windows as popup windows instead. |
[email protected] | e00ccc9 | 2012-11-01 17:32:30 | [diff] [blame] | 243 | #define MAYBE_WindowOpenPanelDetached DISABLED_WindowOpenPanelDetached |
[email protected] | ca29ed1 | 2012-09-06 09:10:50 | [diff] [blame] | 244 | #else |
| 245 | #define MAYBE_WindowOpenPanelDetached WindowOpenPanelDetached |
| 246 | #endif |
| 247 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanelDetached) { |
[email protected] | ca29ed1 | 2012-09-06 09:10:50 | [diff] [blame] | 248 | ASSERT_TRUE(RunExtensionTest("window_open/panel_detached")) << message_; |
| 249 | } |
| 250 | |
[email protected] | af6b5451 | 2011-12-14 06:11:21 | [diff] [blame] | 251 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, |
[email protected] | f9e82d9 | 2011-10-29 00:50:45 | [diff] [blame] | 252 | CloseNonExtensionPanelsOnUninstall) { |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 253 | #if defined(USE_ASH_PANELS) |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 254 | // On Ash, new panel windows open as popup windows instead. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 255 | int num_popups, num_panels; |
| 256 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels)) { |
| 257 | num_popups = 2; |
| 258 | num_panels = 2; |
| 259 | } else { |
| 260 | num_popups = 4; |
| 261 | num_panels = 0; |
| 262 | } |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 263 | #else |
| 264 | int num_popups = 2; |
| 265 | int num_panels = 2; |
| 266 | #endif |
| 267 | ASSERT_TRUE(StartTestServer()); |
| 268 | |
| 269 | // Setup listeners to wait on strings we expect the extension pages to send. |
| 270 | std::vector<std::string> test_strings; |
| 271 | test_strings.push_back("content_tab"); |
| 272 | if (num_panels) |
| 273 | test_strings.push_back("content_panel"); |
| 274 | test_strings.push_back("content_popup"); |
| 275 | |
| 276 | ScopedVector<ExtensionTestMessageListener> listeners; |
| 277 | for (size_t i = 0; i < test_strings.size(); ++i) { |
| 278 | listeners.push_back( |
| 279 | new ExtensionTestMessageListener(test_strings[i], false)); |
| 280 | } |
| 281 | |
| 282 | const extensions::Extension* extension = LoadExtension( |
| 283 | test_data_dir_.AppendASCII("window_open").AppendASCII( |
| 284 | "close_panels_on_uninstall")); |
| 285 | ASSERT_TRUE(extension); |
| 286 | |
| 287 | // Two tabs. One in extension domain and one in non-extension domain. |
| 288 | // Two popups - one in extension domain and one in non-extension domain. |
| 289 | // Two panels - one in extension domain and one in non-extension domain. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 290 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels)); |
[email protected] | 4cdd857 | 2013-02-15 00:33:25 | [diff] [blame] | 291 | |
| 292 | // Wait on test messages to make sure the pages loaded. |
| 293 | for (size_t i = 0; i < listeners.size(); ++i) |
| 294 | ASSERT_TRUE(listeners[i]->WaitUntilSatisfied()); |
| 295 | |
| 296 | UninstallExtension(extension->id()); |
| 297 | |
| 298 | // Wait for the tabs and popups in non-extension domain to stay open. |
| 299 | // Expect everything else, including panels, to close. |
[email protected] | 4cdd857 | 2013-02-15 00:33:25 | [diff] [blame] | 300 | num_popups -= 1; |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 301 | #if defined(USE_ASH_PANELS) |
| 302 | if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnablePanels)) { |
| 303 | // On Ash, new panel windows open as popup windows instead, so there are 2 |
| 304 | // extension domain popups that will close (instead of 1 popup on non-Ash). |
| 305 | num_popups -= 1; |
| 306 | } |
[email protected] | 4cdd857 | 2013-02-15 00:33:25 | [diff] [blame] | 307 | #endif |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 308 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, num_popups, 0)); |
[email protected] | 4cdd857 | 2013-02-15 00:33:25 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | #if defined(OS_CHROMEOS) |
| 312 | // TODO(derat): See if there's some way to get this to work on Chrome OS. It |
| 313 | // crashes there, apparently because we automatically reload crashed pages: |
| 314 | // http:/crbug.com/161073 |
| 315 | #define MAYBE_ClosePanelsOnExtensionCrash DISABLED_ClosePanelsOnExtensionCrash |
| 316 | #else |
| 317 | #define MAYBE_ClosePanelsOnExtensionCrash ClosePanelsOnExtensionCrash |
| 318 | #endif |
| 319 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_ClosePanelsOnExtensionCrash) { |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 320 | #if defined(USE_ASH_PANELS) |
[email protected] | 4cdd857 | 2013-02-15 00:33:25 | [diff] [blame] | 321 | // On Ash, new panel windows open as popup windows instead. |
| 322 | int num_popups = 4; |
| 323 | int num_panels = 0; |
| 324 | #else |
| 325 | int num_popups = 2; |
| 326 | int num_panels = 2; |
| 327 | #endif |
| 328 | ASSERT_TRUE(StartTestServer()); |
| 329 | |
| 330 | // Setup listeners to wait on strings we expect the extension pages to send. |
| 331 | std::vector<std::string> test_strings; |
| 332 | test_strings.push_back("content_tab"); |
| 333 | if (num_panels) |
| 334 | test_strings.push_back("content_panel"); |
| 335 | test_strings.push_back("content_popup"); |
| 336 | |
| 337 | ScopedVector<ExtensionTestMessageListener> listeners; |
| 338 | for (size_t i = 0; i < test_strings.size(); ++i) { |
| 339 | listeners.push_back( |
| 340 | new ExtensionTestMessageListener(test_strings[i], false)); |
| 341 | } |
| 342 | |
| 343 | const extensions::Extension* extension = LoadExtension( |
| 344 | test_data_dir_.AppendASCII("window_open").AppendASCII( |
| 345 | "close_panels_on_uninstall")); |
| 346 | ASSERT_TRUE(extension); |
| 347 | |
| 348 | // Two tabs. One in extension domain and one in non-extension domain. |
| 349 | // Two popups - one in extension domain and one in non-extension domain. |
| 350 | // Two panels - one in extension domain and one in non-extension domain. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 351 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, num_panels)); |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 352 | |
| 353 | // Wait on test messages to make sure the pages loaded. |
| 354 | for (size_t i = 0; i < listeners.size(); ++i) |
| 355 | ASSERT_TRUE(listeners[i]->WaitUntilSatisfied()); |
| 356 | |
| 357 | // Crash the extension. |
| 358 | extensions::ExtensionHost* extension_host = |
[email protected] | be93bba0 | 2012-10-24 16:44:03 | [diff] [blame] | 359 | extensions::ExtensionSystem::Get(browser()->profile())-> |
| 360 | process_manager()->GetBackgroundHostForExtension(extension->id()); |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 361 | ASSERT_TRUE(extension_host); |
| 362 | base::KillProcess(extension_host->render_process_host()->GetHandle(), |
| 363 | content::RESULT_CODE_KILLED, false); |
| 364 | WaitForExtensionCrash(extension->id()); |
| 365 | |
| 366 | // Only expect panels to close. The rest stay open to show a sad-tab. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 367 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 2, num_popups, 0)); |
[email protected] | 1dd66b7 | 2012-09-19 20:41:48 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 370 | #if defined(USE_ASH_PANELS) |
| 371 | // This test is not applicable on Ash. The modified window.open behavior only |
| 372 | // applies to non-Ash panel windows. |
[email protected] | 99f4fdb9 | 2012-08-30 23:13:51 | [diff] [blame] | 373 | #define MAYBE_WindowOpenFromPanel DISABLED_WindowOpenFromPanel |
| 374 | #else |
| 375 | #define MAYBE_WindowOpenFromPanel WindowOpenFromPanel |
| 376 | #endif |
| 377 | IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenFromPanel) { |
[email protected] | 6798c33 | 2012-08-29 02:16:51 | [diff] [blame] | 378 | ASSERT_TRUE(StartTestServer()); |
| 379 | |
[email protected] | 99f4fdb9 | 2012-08-30 23:13:51 | [diff] [blame] | 380 | // Load the extension that will open a panel which then calls window.open. |
[email protected] | 6798c33 | 2012-08-29 02:16:51 | [diff] [blame] | 381 | ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("window_open"). |
| 382 | AppendASCII("panel_window_open"))); |
[email protected] | 99f4fdb9 | 2012-08-30 23:13:51 | [diff] [blame] | 383 | |
| 384 | // Expect one panel (opened by extension) and one tab (from the panel calling |
| 385 | // window.open). Panels modify the WindowOpenDisposition in window.open |
| 386 | // to always open in a tab. |
[email protected] | f1c102b | 2013-02-15 07:44:12 | [diff] [blame^] | 387 | EXPECT_TRUE(WaitForTabsAndPopups(browser(), 1, 0, 1)); |
[email protected] | 6798c33 | 2012-08-29 02:16:51 | [diff] [blame] | 388 | } |
| 389 | |
[email protected] | 24c6bcfc | 2012-02-29 21:06:36 | [diff] [blame] | 390 | IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) { |
[email protected] | 54edcea | 2011-07-27 01:56:38 | [diff] [blame] | 391 | ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_; |
| 392 | } |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 393 | |
| 394 | // Tests that an extension page can call window.open to an extension URL and |
| 395 | // the new window has extension privileges. |
| 396 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenExtension) { |
| 397 | ASSERT_TRUE(LoadExtension( |
| 398 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); |
| 399 | |
| 400 | GURL start_url(std::string("chrome-extension://") + |
| 401 | last_loaded_extension_id_ + "/test.html"); |
| 402 | ui_test_utils::NavigateToURL(browser(), start_url); |
[email protected] | f3e73f6 | 2012-10-20 05:35:16 | [diff] [blame] | 403 | WebContents* newtab = NULL; |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 404 | ASSERT_NO_FATAL_FAILURE( |
| 405 | OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(), |
| 406 | start_url.Resolve("newtab.html"), true, &newtab)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 407 | |
| 408 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 409 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", |
| 410 | &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 411 | EXPECT_TRUE(result); |
| 412 | } |
| 413 | |
| 414 | // Tests that if an extension page calls window.open to an invalid extension |
| 415 | // URL, the browser doesn't crash. |
| 416 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenInvalidExtension) { |
| 417 | ASSERT_TRUE(LoadExtension( |
| 418 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); |
| 419 | |
| 420 | GURL start_url(std::string("chrome-extension://") + |
| 421 | last_loaded_extension_id_ + "/test.html"); |
| 422 | ui_test_utils::NavigateToURL(browser(), start_url); |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 423 | ASSERT_NO_FATAL_FAILURE( |
| 424 | OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 425 | GURL("chrome-extension://thisissurelynotavalidextensionid/newtab.html"), |
| 426 | false, NULL)); |
| 427 | |
| 428 | // If we got to this point, we didn't crash, so we're good. |
| 429 | } |
| 430 | |
| 431 | // Tests that calling window.open from the newtab page to an extension URL |
| 432 | // gives the new window extension privileges - even though the opening page |
| 433 | // does not have extension privileges, we break the script connection, so |
| 434 | // there is no privilege leak. |
| 435 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, WindowOpenNoPrivileges) { |
| 436 | ASSERT_TRUE(LoadExtension( |
| 437 | test_data_dir_.AppendASCII("uitest").AppendASCII("window_open"))); |
| 438 | |
| 439 | ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); |
[email protected] | f3e73f6 | 2012-10-20 05:35:16 | [diff] [blame] | 440 | WebContents* newtab = NULL; |
[email protected] | e044887 | 2013-01-11 19:35:02 | [diff] [blame] | 441 | ASSERT_NO_FATAL_FAILURE( |
| 442 | OpenWindow(browser()->tab_strip_model()->GetActiveWebContents(), |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 443 | GURL(std::string("chrome-extension://") + last_loaded_extension_id_ + |
| 444 | "/newtab.html"), false, &newtab)); |
| 445 | |
| 446 | // Extension API should succeed. |
| 447 | bool result = false; |
[email protected] | b6987e0 | 2013-01-04 18:30:43 | [diff] [blame] | 448 | ASSERT_TRUE(content::ExecuteScriptAndExtractBool(newtab, "testExtensionApi()", |
| 449 | &result)); |
[email protected] | fad7367 | 2012-06-15 23:26:06 | [diff] [blame] | 450 | EXPECT_TRUE(result); |
| 451 | } |