blob: 92e08b33f928dade60d05480af122c1d917ddc04 [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"
6#include "chrome/browser/extensions/extension_apitest.h"
[email protected]2ad4a902010-11-17 06:05:137#include "chrome/browser/ui/browser.h"
8#include "chrome/browser/ui/browser_list.h"
[email protected]32b7c492010-09-01 04:15:009#include "chrome/common/chrome_switches.h"
[email protected]4026ce1e2010-09-14 19:35:0410#include "chrome/test/ui_test_utils.h"
11#include "net/base/mock_host_resolver.h"
[email protected]32b7c492010-09-01 04:15:0012
[email protected]e78eb292010-12-22 08:35:3913// Disabled, http://crbug.com/64899.
14IN_PROC_BROWSER_TEST_F(ExtensionApiTest, DISABLED_WindowOpen) {
[email protected]32b7c492010-09-01 04:15:0015 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]4026ce1e2010-09-14 19:35:0423
24void 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]0a5bdc6522011-06-08 19:04:4929 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]4026ce1e2010-09-14 19:35:0445 continue;
[email protected]4026ce1e2010-09-14 19:35:0446
[email protected]0a5bdc6522011-06-08 19:04:4947 // Check for TYPE_POPUP or TYPE_PANEL.
48 EXPECT_TRUE((*iter)->is_type_popup() || (*iter)->is_type_panel());
[email protected]4026ce1e2010-09-14 19:35:0449 }
50}
51
[email protected]f112b0f2011-05-26 01:53:5252IN_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]0a5bdc6522011-06-08 19:04:4969IN_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
79IN_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
96IN_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]4026ce1e2010-09-14 19:35:04108IN_PROC_BROWSER_TEST_F(ExtensionApiTest, PopupBlockingExtension) {
109 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]e18a19b2010-10-19 20:32:30110 ASSERT_TRUE(StartTestServer());
[email protected]4026ce1e2010-09-14 19:35:04111
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
119IN_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]fe3048872010-10-18 14:58:59127 // 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]4026ce1e2010-09-14 19:35:04148
149 WaitForTabsAndPopups(browser(), 3, 1);
150}
[email protected]d4db6c702011-03-28 21:49:14151
152#if defined(OS_MACOSX) || defined(OS_WIN)
153// Focus test fails if there is no window manager on Linux.
154IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowOpenFocus) {
155 ASSERT_TRUE(RunExtensionTest("window_open/focus")) << message_;
156}
157#endif
[email protected]08aa0c32011-04-11 22:18:37158
[email protected]e7f90562011-05-23 21:38:24159IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowArgumentsOverflow) {
160 ASSERT_TRUE(RunExtensionTest("window_open/argument_overflow")) << message_;
161}
162
[email protected]08aa0c32011-04-11 22:18:37163class WindowOpenPanelTest : public ExtensionApiTest {
164 virtual void SetUpCommandLine(CommandLine* command_line) {
165 ExtensionApiTest::SetUpCommandLine(command_line);
166 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
[email protected]b35b26b32011-05-05 20:35:14167 command_line->AppendSwitch(switches::kEnablePanels);
[email protected]08aa0c32011-04-11 22:18:37168 }
169};
170
[email protected]4dd47502011-06-06 20:41:04171#if !defined(TOOLKIT_VIEWS)
172#define MAYBE_WindowOpenPanel WindowOpenPanel
173#else
174#define MAYBE_WindowOpenPanel DISABLED_WindowOpenPanel
175#endif
176IN_PROC_BROWSER_TEST_F(WindowOpenPanelTest, MAYBE_WindowOpenPanel) {
[email protected]08aa0c32011-04-11 22:18:37177 ASSERT_TRUE(RunExtensionTest("window_open/panel")) << message_;
178}