blob: 2becb95b2b06212e1c2bf6f7f45cc04154ed295c [file] [log] [blame]
[email protected]35abc332012-02-24 23:48:381// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]32b7c492010-09-01 04:15:002// 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]f9e82d92011-10-29 00:50:456#include "base/memory/scoped_vector.h"
[email protected]58b349b2011-11-23 02:07:157#include "base/stringprintf.h"
[email protected]32b7c492010-09-01 04:15:008#include "chrome/browser/extensions/extension_apitest.h"
[email protected]f9e82d92011-10-29 00:50:459#include "chrome/browser/extensions/extension_test_message_listener.h"
[email protected]2ad4a902010-11-17 06:05:1310#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_list.h"
[email protected]32b7c492010-09-01 04:15:0012#include "chrome/common/chrome_switches.h"
[email protected]f9e82d92011-10-29 00:50:4513#include "chrome/common/extensions/extension.h"
[email protected]af44e7fb2011-07-29 18:32:3214#include "chrome/test/base/ui_test_utils.h"
[email protected]58b349b2011-11-23 02:07:1515#include "testing/gtest/include/gtest/gtest.h"
[email protected]4026ce1e2010-09-14 19:35:0416#include "net/base/mock_host_resolver.h"
[email protected]32b7c492010-09-01 04:15:0017
[email protected]e5d549d2011-12-28 01:29:2018using content::OpenURLParams;
19using content::Referrer;
20
[email protected]e78eb292010-12-22 08:35:3921// Disabled, http://crbug.com/64899.
22IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
[email protected]32b7c492010-09-01 04:15:0023 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]4026ce1e2010-09-14 19:35:0431
[email protected]f9e82d92011-10-29 00:50:4532void WaitForTabsAndPopups(Browser* browser,
33 int num_tabs,
34 int num_popups,
35 int num_panels) {
[email protected]58b349b2011-11-23 02:07:1536 SCOPED_TRACE(
37 StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
38 num_tabs, num_popups, num_panels));
[email protected]4026ce1e2010-09-14 19:35:0439 // We start with one tab and one browser already open.
40 ++num_tabs;
[email protected]f9e82d92011-10-29 00:50:4541 size_t num_browsers = static_cast<size_t>(num_popups + num_panels) + 1;
[email protected]4026ce1e2010-09-14 19:35:0442
[email protected]0a5bdc6522011-06-08 19:04:4943 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]f9e82d92011-10-29 00:50:4546 if (BrowserList::GetBrowserCount(browser->profile()) == num_browsers &&
47 browser->tab_count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4948 break;
49
[email protected]58b349b2011-11-23 02:07:1550 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4951 }
52
53 EXPECT_EQ(num_browsers, BrowserList::GetBrowserCount(browser->profile()));
54 EXPECT_EQ(num_tabs, browser->tab_count());
55
[email protected]f9e82d92011-10-29 00:50:4556 int num_popups_seen = 0;
57 int num_panels_seen = 0;
[email protected]0a5bdc6522011-06-08 19:04:4958 for (BrowserList::const_iterator iter = BrowserList::begin();
59 iter != BrowserList::end(); ++iter) {
60 if (*iter == browser)
[email protected]4026ce1e2010-09-14 19:35:0461 continue;
[email protected]4026ce1e2010-09-14 19:35:0462
[email protected]0a5bdc6522011-06-08 19:04:4963 // Check for TYPE_POPUP or TYPE_PANEL.
64 EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel());
[email protected]f9e82d92011-10-29 00:50:4565 if ((*iter)->is_type_popup())
66 ++num_popups_seen;
67 else
68 ++num_panels_seen;
[email protected]4026ce1e2010-09-14 19:35:0469 }
[email protected]f9e82d92011-10-29 00:50:4570 EXPECT_EQ(num_popups, num_popups_seen);
71 EXPECT_EQ(num_panels, num_panels_seen);
[email protected]4026ce1e2010-09-14 19:35:0472}
73
[email protected]f112b0f2011-05-26 01:53:5274IN_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]f9e82d92011-10-29 00:50:4580 WaitForTabsAndPopups(browser(), 0, 2, 0);
[email protected]f112b0f2011-05-26 01:53:5281
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]0a5bdc6522011-06-08 19:04:4991IN_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]f9e82d92011-10-29 00:50:4598 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:4999}
100
101IN_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]f9e82d92011-10-29 00:50:45115 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49116}
117
118IN_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]f9e82d92011-10-29 00:50:45127 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49128}
129
[email protected]4026ce1e2010-09-14 19:35:04130IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
131 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:30132 ASSERT_TRUE(StartTestServer());
[email protected]4026ce1e2010-09-14 19:35:04133
134 ASSERT_TRUE(LoadExtension(
135 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
136 .AppendASCII("extension")));
137
[email protected]f9e82d92011-10-29 00:50:45138 WaitForTabsAndPopups(browser(), 5, 3, 0);
[email protected]4026ce1e2010-09-14 19:35:04139}
140
141IN_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]fe3048872010-10-18 14:58:59149 // 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]e5d549d2011-12-28 01:29:20166 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]4026ce1e2010-09-14 19:35:04172
[email protected]f9e82d92011-10-29 00:50:45173 WaitForTabsAndPopups(browser(), 3, 1, 0);
[email protected]4026ce1e2010-09-14 19:35:04174}
[email protected]d4db6c702011-03-28 21:49:14175
[email protected]e7f90562011-05-23 21:38:24176IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
177 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
178}
179
[email protected]d5c14062011-10-27 00:02:13180class WindowOpenPanelDisabledTest : public ExtensionApiTest {
[email protected]08aa0c32011-04-11 22:18:37181 virtual void SetUpCommandLine(CommandLine* command_line) {
182 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]7e7a28092011-12-09 22:24:55183 // TODO(jennb): Re-enable when panels are enabled by default.
184 // command_line->AppendSwitch(switches::kDisablePanels);
[email protected]08aa0c32011-04-11 22:18:37185 }
186};
187
[email protected]7e7a28092011-12-09 22:24:55188IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest,
189 DISABLED_WindowOpenPanelNotEnabled) {
[email protected]d5c14062011-10-27 00:02:13190 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
191}
192
[email protected]af6b54512011-12-14 06:11:21193class WindowOpenPanelTest : public ExtensionApiTest {
194 virtual void SetUpCommandLine(CommandLine* command_line) {
195 ExtensionApiTest::SetUpCommandLine(command_line);
196 command_line->AppendSwitch(switches::kEnablePanels);
197 }
198};
199
200IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37201 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
202}
[email protected]54edcea2011-07-27 01:56:38203
[email protected]4ff65f12011-10-06 00:35:54204#if defined(OS_MACOSX) || defined(OS_WIN)
205// Focus test fails if there is no window manager on Linux.
[email protected]af6b54512011-12-14 06:11:21206IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, WindowOpenFocus) {
[email protected]4ff65f12011-10-06 00:35:54207 ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_;
208}
209#endif
210
[email protected]af6b54512011-12-14 06:11:21211IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest,
[email protected]f9e82d92011-10-29 00:50:45212 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]24c6bcfc2012-02-29 21:06:36248IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpener) {
[email protected]54edcea2011-07-27 01:56:38249 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
250}