blob: 98e9461dc92e999c063da235ec1e585ea70dd500 [file] [log] [blame]
[email protected]d4db6c702011-03-28 21:49:141// Copyright (c) 2011 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]e78eb292010-12-22 08:35:3918// Disabled, http://crbug.com/64899.
19IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
[email protected]32b7c492010-09-01 04:15:0020 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]4026ce1e2010-09-14 19:35:0428
[email protected]f9e82d92011-10-29 00:50:4529void WaitForTabsAndPopups(Browser* browser,
30 int num_tabs,
31 int num_popups,
32 int num_panels) {
[email protected]58b349b2011-11-23 02:07:1533 SCOPED_TRACE(
34 StringPrintf("WaitForTabsAndPopups tabs:%d, popups:%d, panels:%d",
35 num_tabs, num_popups, num_panels));
[email protected]4026ce1e2010-09-14 19:35:0436 // We start with one tab and one browser already open.
37 ++num_tabs;
[email protected]f9e82d92011-10-29 00:50:4538 size_t num_browsers = static_cast<size_t>(num_popups + num_panels) + 1;
[email protected]4026ce1e2010-09-14 19:35:0439
[email protected]0a5bdc6522011-06-08 19:04:4940 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]f9e82d92011-10-29 00:50:4543 if (BrowserList::GetBrowserCount(browser->profile()) == num_browsers &&
44 browser->tab_count() == num_tabs)
[email protected]0a5bdc6522011-06-08 19:04:4945 break;
46
[email protected]58b349b2011-11-23 02:07:1547 ui_test_utils::RunAllPendingInMessageLoop();
[email protected]0a5bdc6522011-06-08 19:04:4948 }
49
50 EXPECT_EQ(num_browsers, BrowserList::GetBrowserCount(browser->profile()));
51 EXPECT_EQ(num_tabs, browser->tab_count());
52
[email protected]f9e82d92011-10-29 00:50:4553 int num_popups_seen = 0;
54 int num_panels_seen = 0;
[email protected]0a5bdc6522011-06-08 19:04:4955 for (BrowserList::const_iterator iter = BrowserList::begin();
56 iter != BrowserList::end(); ++iter) {
57 if (*iter == browser)
[email protected]4026ce1e2010-09-14 19:35:0458 continue;
[email protected]4026ce1e2010-09-14 19:35:0459
[email protected]0a5bdc6522011-06-08 19:04:4960 // Check for TYPE_POPUP or TYPE_PANEL.
61 EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel());
[email protected]f9e82d92011-10-29 00:50:4562 if ((*iter)->is_type_popup())
63 ++num_popups_seen;
64 else
65 ++num_panels_seen;
[email protected]4026ce1e2010-09-14 19:35:0466 }
[email protected]f9e82d92011-10-29 00:50:4567 EXPECT_EQ(num_popups, num_popups_seen);
68 EXPECT_EQ(num_panels, num_panels_seen);
[email protected]4026ce1e2010-09-14 19:35:0469}
70
[email protected]f112b0f2011-05-26 01:53:5271IN_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]f9e82d92011-10-29 00:50:4577 WaitForTabsAndPopups(browser(), 0, 2, 0);
[email protected]f112b0f2011-05-26 01:53:5278
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]0a5bdc6522011-06-08 19:04:4988IN_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]f9e82d92011-10-29 00:50:4595 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:4996}
97
98IN_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]f9e82d92011-10-29 00:50:45112 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49113}
114
115IN_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]f9e82d92011-10-29 00:50:45124 WaitForTabsAndPopups(browser(), num_tabs, num_popups, 0);
[email protected]0a5bdc6522011-06-08 19:04:49125}
126
[email protected]4026ce1e2010-09-14 19:35:04127IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
128 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:30129 ASSERT_TRUE(StartTestServer());
[email protected]4026ce1e2010-09-14 19:35:04130
131 ASSERT_TRUE(LoadExtension(
132 test_data_dir_.AppendASCII("window_open").AppendASCII("popup_blocking")
133 .AppendASCII("extension")));
134
[email protected]f9e82d92011-10-29 00:50:45135 WaitForTabsAndPopups(browser(), 5, 3, 0);
[email protected]4026ce1e2010-09-14 19:35:04136}
137
138IN_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]fe3048872010-10-18 14:58:59146 // 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]2905f742011-10-13 03:51:58164 content::PAGE_TRANSITION_TYPED);
[email protected]fe3048872010-10-18 14:58:59165 browser()->OpenURL(open_popup, GURL(), NEW_FOREGROUND_TAB,
[email protected]2905f742011-10-13 03:51:58166 content::PAGE_TRANSITION_TYPED);
[email protected]4026ce1e2010-09-14 19:35:04167
[email protected]f9e82d92011-10-29 00:50:45168 WaitForTabsAndPopups(browser(), 3, 1, 0);
[email protected]4026ce1e2010-09-14 19:35:04169}
[email protected]d4db6c702011-03-28 21:49:14170
[email protected]e7f90562011-05-23 21:38:24171IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
172 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
173}
174
[email protected]d5c14062011-10-27 00:02:13175class WindowOpenPanelDisabledTest : public ExtensionApiTest {
[email protected]08aa0c32011-04-11 22:18:37176 virtual void SetUpCommandLine(CommandLine* command_line) {
177 ExtensionApiTest::SetUpCommandLine(command_line);
[email protected]d5c14062011-10-27 00:02:13178 command_line->AppendSwitch(switches::kDisablePanels);
[email protected]08aa0c32011-04-11 22:18:37179 }
180};
181
[email protected]d5c14062011-10-27 00:02:13182IN_PROC_BROWSER_TEST_F(WindowOpenPanelDisabledTest, WindowOpenPanelNotEnabled) {
183 ASSERT_TRUE(RunExtensionTest("window_open/panel_not_enabled")) << message_;
184}
185
186IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37187 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
188}
[email protected]54edcea2011-07-27 01:56:38189
[email protected]4ff65f12011-10-06 00:35:54190#if defined(OS_MACOSX) || defined(OS_WIN)
191// Focus test fails if there is no window manager on Linux.
[email protected]d5c14062011-10-27 00:02:13192IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenFocus) {
[email protected]4ff65f12011-10-06 00:35:54193 ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_;
194}
195#endif
196
[email protected]f9e82d92011-10-29 00:50:45197IN_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]54edcea2011-07-27 01:56:38234IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpener) {
235 ASSERT_TRUE(RunExtensionTest("window_open/opener")) << message_;
236}