1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "app/l10n_util.h"
#include "base/file_path.h"
#include "base/gfx/rect.h"
#include "base/keyboard_codes.h"
#include "chrome/browser/view_ids.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/test/automation/automation_constants.h"
#include "chrome/test/automation/browser_proxy.h"
#include "chrome/test/automation/tab_proxy.h"
#include "chrome/test/automation/window_proxy.h"
#include "chrome/test/ui/ui_test.h"
#include "grit/generated_resources.h"
#include "net/base/net_util.h"
#include "views/event.h"
class BlockedPopupContainerInteractiveTest : public UITest {
protected:
BlockedPopupContainerInteractiveTest() {
show_window_ = true;
}
virtual void SetUp() {
UITest::SetUp();
browser_ = automation()->GetBrowserWindow(0);
ASSERT_TRUE(browser_.get());
window_ = browser_->GetWindow();
ASSERT_TRUE(window_.get());
tab_ = browser_->GetTab(0);
ASSERT_TRUE(tab_.get());
}
void NavigateMainTabTo(const std::string& file_name) {
FilePath filename(test_data_directory_);
filename = filename.AppendASCII("constrained_files");
filename = filename.AppendASCII(file_name);
ASSERT_TRUE(tab_->NavigateToURL(net::FilePathToFileURL(filename)));
}
void SimulateClickInCenterOf(const scoped_refptr<WindowProxy>& window) {
gfx::Rect tab_view_bounds;
ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER,
&tab_view_bounds, true));
// Simulate a click of the actual link to force user_gesture to be
// true; if we don't, the resulting popup will be constrained, which
// isn't what we want to test.
ASSERT_TRUE(window->SimulateOSClick(tab_view_bounds.CenterPoint(),
views::Event::EF_LEFT_BUTTON_DOWN));
}
scoped_refptr<BrowserProxy> browser_;
scoped_refptr<WindowProxy> window_;
scoped_refptr<TabProxy> tab_;
};
TEST_F(BlockedPopupContainerInteractiveTest, TestOpenAndResizeTo) {
NavigateMainTabTo("constrained_window_onload_resizeto.html");
SimulateClickInCenterOf(window_);
ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000));
scoped_refptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1));
ASSERT_TRUE(popup_browser != NULL);
scoped_refptr<WindowProxy> popup_window(popup_browser->GetWindow());
ASSERT_TRUE(popup_window != NULL);
// Make sure we were created with the correct width and height.
gfx::Rect rect;
bool is_timeout = false;
ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout(
VIEW_ID_TAB_CONTAINER, &rect, false, 1000, &is_timeout));
ASSERT_FALSE(is_timeout);
EXPECT_EQ(300, rect.width());
EXPECT_EQ(320, rect.height());
#if defined(OS_LINUX)
// It seems we have to wait a little bit for the widgets to spin up before
// we can start clicking on them.
PlatformThread::Sleep(500);
#endif
SimulateClickInCenterOf(popup_window);
// No idea how to wait here other then sleeping. This timeout used to be
// lower, then we started hitting it before it was done. :(
PlatformThread::Sleep(5000);
// The actual content will be LESS than (200, 200) because resizeTo
// deals with a window's outer{Width,Height} instead of its
// inner{Width,Height}.
is_timeout = false;
ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout(
VIEW_ID_TAB_CONTAINER, &rect, false, 1000, &is_timeout));
ASSERT_FALSE(is_timeout);
EXPECT_LT(rect.height(), 200);
#if defined(OS_LINUX)
// On Linux we may run in an environment where there is no window frame. In
// this case our width might be exactly 200. The height will still be less
// because we have to show the location bar.
EXPECT_LE(rect.width(), 200);
#else
EXPECT_LT(rect.width(), 200);
#endif
}
// Helper function used to get the number of blocked popups out of the window
// title.
bool ParseCountOutOfTitle(const std::wstring& title, int* output) {
// Since we will be reading the number of popup windows open by grabbing the
// number out of the window title, and that format string is localized, we
// need to find out the offset into that string.
const wchar_t* placeholder = L"XXXX";
size_t offset =
l10n_util::GetStringF(IDS_POPUPS_BLOCKED_COUNT, placeholder).
find(placeholder);
std::wstring number;
while (offset < title.size() && iswdigit(title[offset])) {
number += title[offset];
offset++;
}
return StringToInt(WideToUTF16(number), output);
}
// Tests that in the window.open() equivalent of a fork bomb, we stop building
// windows.
TEST_F(BlockedPopupContainerInteractiveTest, DontSpawnEndlessPopups) {
NavigateMainTabTo("infinite_popups.html");
SimulateClickInCenterOf(window_);
ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000));
scoped_refptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1));
ASSERT_TRUE(popup_browser.get());
scoped_refptr<TabProxy> popup_tab(popup_browser->GetTab(0));
ASSERT_TRUE(popup_tab.get());
// And now we spin, waiting to make sure that we don't spawn popup
// windows endlessly. The current limit is 25, so allowing for possible race
// conditions and one off errors, don't break out until we go over 30 popup
// windows (in which case we are bork bork bork).
const int kMaxPopupWindows = 30;
int popup_window_count = 0;
int new_popup_window_count = 0;
int times_slept = 0;
bool continuing = true;
while (continuing && popup_window_count < kMaxPopupWindows) {
ASSERT_TRUE(popup_tab->GetBlockedPopupCount(&new_popup_window_count));
if (new_popup_window_count == popup_window_count) {
if (times_slept == 10) {
continuing = false;
} else {
// Nothing intereseting is going on wait it out.
PlatformThread::Sleep(automation::kSleepTime);
times_slept++;
}
} else {
times_slept = 0;
}
EXPECT_GE(new_popup_window_count, popup_window_count);
EXPECT_LE(new_popup_window_count, kMaxPopupWindows);
popup_window_count = new_popup_window_count;
}
}
// Make sure that we refuse to close windows when a constrained popup is
// displayed.
TEST_F(BlockedPopupContainerInteractiveTest, WindowOpenWindowClosePopup) {
NavigateMainTabTo("openclose_main.html");
SimulateClickInCenterOf(window_);
ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 5000));
// Make sure we have a blocked popup notification
scoped_refptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1));
ASSERT_TRUE(popup_browser.get());
scoped_refptr<WindowProxy> popup_window(popup_browser->GetWindow());
ASSERT_TRUE(popup_window.get());
scoped_refptr<TabProxy> popup_tab(popup_browser->GetTab(0));
ASSERT_TRUE(popup_tab.get());
ASSERT_TRUE(popup_tab->WaitForBlockedPopupCountToChangeTo(1, 1000));
// Ensure we didn't close the first popup window.
ASSERT_FALSE(automation()->WaitForWindowCountToBecome(1, 3000));
}
TEST_F(BlockedPopupContainerInteractiveTest, BlockAlertFromBlockedPopup) {
NavigateMainTabTo("block_alert.html");
// Wait for there to be an app modal dialog (and fail if it's shown).
ASSERT_FALSE(automation()->WaitForAppModalDialog(4000));
// Ensure one browser window.
int browser_window_count;
ASSERT_TRUE(automation()->GetBrowserWindowCount(&browser_window_count));
ASSERT_EQ(1, browser_window_count);
// Ensure one blocked popup window: the popup didn't escape.
int popup_count = 0;
ASSERT_TRUE(tab_->GetBlockedPopupCount(&popup_count));
ASSERT_EQ(1, popup_count);
}
TEST_F(BlockedPopupContainerInteractiveTest, ShowAlertFromNormalPopup) {
NavigateMainTabTo("show_alert.html");
SimulateClickInCenterOf(window_);
ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 5000));
scoped_refptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1));
ASSERT_TRUE(popup_browser.get());
scoped_refptr<WindowProxy> popup_window(popup_browser->GetWindow());
ASSERT_TRUE(popup_window.get());
scoped_refptr<TabProxy> popup_tab(popup_browser->GetTab(0));
ASSERT_TRUE(popup_tab.get());
#if defined(OS_LINUX)
// It seems we have to wait a little bit for the widgets to spin up before
// we can start clicking on them.
PlatformThread::Sleep(500);
#endif
SimulateClickInCenterOf(popup_window);
// Wait for there to be an app modal dialog.
ASSERT_TRUE(automation()->WaitForAppModalDialog(5000));
}
// Make sure that window focus works while creating a popup window so that we
// don't
TEST_F(BlockedPopupContainerInteractiveTest, DontBreakOnBlur) {
NavigateMainTabTo("window_blur_test.html");
SimulateClickInCenterOf(window_);
// Wait for the popup window to open.
ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000));
// We popup shouldn't be closed by the onblur handler.
ASSERT_FALSE(automation()->WaitForWindowCountToBecome(1, 1500));
}
#if !defined(OS_MACOSX) // see BrowserWindowCocoa::GetCommandId
// Tests that tab related keyboard accelerators are reserved by the app.
class BrowserInteractiveTest : public UITest {
};
TEST_F(BrowserInteractiveTest, ReserveKeyboardAccelerators) {
const std::string kBadPage =
"<html><script>"
"document.onkeydown = function() {"
" event.preventDefault();"
" return false;"
"}"
"</script></html>";
scoped_refptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
browser->AppendTab(GURL("data:text/html," + kBadPage));
int tab_count = 0;
ASSERT_TRUE(browser->GetTabCount(&tab_count));
ASSERT_EQ(tab_count, 2);
int active_tab = 0;
ASSERT_TRUE(browser->GetActiveTabIndex(&active_tab));
ASSERT_EQ(active_tab, 1);
scoped_refptr<WindowProxy> window(browser->GetWindow());
ASSERT_TRUE(window->SimulateOSKeyPress(
base::VKEY_TAB, views::Event::EF_CONTROL_DOWN));
ASSERT_TRUE(browser->WaitForTabToBecomeActive(0, action_max_timeout_ms()));
ASSERT_TRUE(browser->ActivateTab(1));
ASSERT_TRUE(window->SimulateOSKeyPress(
base::VKEY_W, views::Event::EF_CONTROL_DOWN));
ASSERT_TRUE(browser->WaitForTabCountToBecome(1, action_max_timeout_ms()));
}
#endif
|