OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "chrome/browser/view_ids.h" | 8 #include "chrome/browser/view_ids.h" |
9 #include "chrome/common/chrome_constants.h" | 9 #include "chrome/common/chrome_constants.h" |
10 #include "chrome/common/l10n_util.h" | 10 #include "chrome/common/l10n_util.h" |
11 #include "chrome/test/automation/automation_constants.h" | 11 #include "chrome/test/automation/automation_constants.h" |
12 #include "chrome/test/automation/browser_proxy.h" | 12 #include "chrome/test/automation/browser_proxy.h" |
13 #include "chrome/test/automation/constrained_window_proxy.h" | 13 #include "chrome/test/automation/constrained_window_proxy.h" |
14 #include "chrome/test/automation/tab_proxy.h" | 14 #include "chrome/test/automation/tab_proxy.h" |
15 #include "chrome/test/automation/window_proxy.h" | 15 #include "chrome/test/automation/window_proxy.h" |
16 #include "chrome/test/ui/ui_test.h" | 16 #include "chrome/test/ui/ui_test.h" |
17 #include "chrome/views/event.h" | 17 #include "chrome/views/event.h" |
18 #include "net/base/net_util.h" | 18 #include "net/base/net_util.h" |
19 | 19 |
20 #include "generated_resources.h" | 20 #include "generated_resources.h" |
21 | 21 |
22 const int kRightCloseButtonOffset = 55; | 22 const int kRightCloseButtonOffset = 55; |
23 const int kBottomCloseButtonOffset = 20; | 23 const int kBottomCloseButtonOffset = 20; |
24 | 24 |
25 class InteractiveConstrainedWindowTest : public UITest { | 25 class InteractiveConstrainedWindowTest : public UITest { |
26 protected: | 26 protected: |
27 InteractiveConstrainedWindowTest() { | 27 InteractiveConstrainedWindowTest() { |
28 show_window_ = true; | 28 show_window_ = true; |
29 } | 29 } |
| 30 |
| 31 virtual void SetUp() { |
| 32 UITest::SetUp(); |
| 33 |
| 34 browser_.reset(automation()->GetBrowserWindow(0)); |
| 35 ASSERT_TRUE(browser_.get()); |
| 36 |
| 37 window_.reset(automation()->GetWindowForBrowser(browser_.get())); |
| 38 ASSERT_TRUE(window_.get()); |
| 39 |
| 40 tab_.reset(browser_->GetTab(0)); |
| 41 ASSERT_TRUE(tab_.get()); |
| 42 } |
| 43 |
| 44 void NavigateMainTabTo(const std::wstring& file_name) { |
| 45 std::wstring filename(test_data_directory_); |
| 46 file_util::AppendToPath(&filename, L"constrained_files"); |
| 47 file_util::AppendToPath(&filename, file_name); |
| 48 ASSERT_TRUE(tab_->NavigateToURL(net::FilePathToFileURL(filename))); |
| 49 } |
| 50 |
| 51 void SimulateClickInCenterOf(const scoped_ptr<WindowProxy>& window) { |
| 52 gfx::Rect tab_view_bounds; |
| 53 ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER, |
| 54 &tab_view_bounds, true)); |
| 55 |
| 56 // Simulate a click of the actual link to force user_gesture to be |
| 57 // true; if we don't, the resulting popup will be constrained, which |
| 58 // isn't what we want to test. |
| 59 POINT link_point(tab_view_bounds.CenterPoint().ToPOINT()); |
| 60 ASSERT_TRUE(window->SimulateOSClick(link_point, |
| 61 views::Event::EF_LEFT_BUTTON_DOWN)); |
| 62 } |
| 63 |
| 64 scoped_ptr<BrowserProxy> browser_; |
| 65 scoped_ptr<WindowProxy> window_; |
| 66 scoped_ptr<TabProxy> tab_; |
30 }; | 67 }; |
31 | 68 |
32 TEST_F(InteractiveConstrainedWindowTest, TestOpenAndResizeTo) { | 69 TEST_F(InteractiveConstrainedWindowTest, TestOpenAndResizeTo) { |
33 scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | 70 NavigateMainTabTo(L"constrained_window_onload_resizeto.html"); |
34 ASSERT_TRUE(browser.get()); | 71 SimulateClickInCenterOf(window_); |
35 | |
36 scoped_ptr<WindowProxy> window( | |
37 automation()->GetWindowForBrowser(browser.get())); | |
38 ASSERT_TRUE(window.get()); | |
39 | |
40 scoped_ptr<TabProxy> tab(browser->GetTab(0)); | |
41 ASSERT_TRUE(tab.get()); | |
42 | |
43 std::wstring filename(test_data_directory_); | |
44 file_util::AppendToPath(&filename, L"constrained_files"); | |
45 file_util::AppendToPath(&filename, | |
46 L"constrained_window_onload_resizeto.html"); | |
47 ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename))); | |
48 | |
49 gfx::Rect tab_view_bounds; | |
50 ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER, | |
51 &tab_view_bounds, true)); | |
52 | |
53 // Simulate a click of the actual link to force user_gesture to be | |
54 // true; if we don't, the resulting popup will be constrained, which | |
55 // isn't what we want to test. | |
56 POINT link_point(tab_view_bounds.CenterPoint().ToPOINT()); | |
57 ASSERT_TRUE(window->SimulateOSClick(link_point, | |
58 views::Event::EF_LEFT_BUTTON_DOWN)); | |
59 | 72 |
60 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000)); | 73 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000)); |
61 | 74 |
62 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); | 75 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); |
63 ASSERT_TRUE(popup_browser != NULL); | 76 ASSERT_TRUE(popup_browser != NULL); |
64 scoped_ptr<WindowProxy> popup_window( | 77 scoped_ptr<WindowProxy> popup_window( |
65 automation()->GetWindowForBrowser(popup_browser.get())); | 78 automation()->GetWindowForBrowser(popup_browser.get())); |
66 ASSERT_TRUE(popup_window != NULL); | 79 ASSERT_TRUE(popup_window != NULL); |
67 | 80 |
68 // Make sure we were created with the correct width and height. | 81 // Make sure we were created with the correct width and height. |
69 gfx::Rect rect; | 82 gfx::Rect rect; |
70 bool is_timeout = false; | 83 bool is_timeout = false; |
71 ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout( | 84 ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout( |
72 VIEW_ID_TAB_CONTAINER, &rect, false, 1000, &is_timeout)); | 85 VIEW_ID_TAB_CONTAINER, &rect, false, 1000, &is_timeout)); |
73 ASSERT_FALSE(is_timeout); | 86 ASSERT_FALSE(is_timeout); |
74 ASSERT_EQ(300, rect.width()); | 87 ASSERT_EQ(300, rect.width()); |
75 ASSERT_EQ(320, rect.height()); | 88 ASSERT_EQ(320, rect.height()); |
76 | 89 |
77 // Send a click to the popup window to test resizeTo. | 90 SimulateClickInCenterOf(popup_window); |
78 ASSERT_TRUE(popup_window->GetViewBounds(VIEW_ID_TAB_CONTAINER, | |
79 &tab_view_bounds, true)); | |
80 POINT popup_link_point(tab_view_bounds.CenterPoint().ToPOINT()); | |
81 ASSERT_TRUE(popup_window->SimulateOSClick( | |
82 popup_link_point, views::Event::EF_LEFT_BUTTON_DOWN)); | |
83 | 91 |
84 // No idea how to wait here other then sleeping. This timeout used to be | 92 // No idea how to wait here other then sleeping. This timeout used to be |
85 // lower, then we started hitting it before it was done. :( | 93 // lower, then we started hitting it before it was done. :( |
86 Sleep(5000); | 94 Sleep(5000); |
87 | 95 |
88 // The actual content will be LESS than (200, 200) because resizeTo | 96 // The actual content will be LESS than (200, 200) because resizeTo |
89 // deals with a window's outer{Width,Height} instead of its | 97 // deals with a window's outer{Width,Height} instead of its |
90 // inner{Width,Height}. | 98 // inner{Width,Height}. |
91 is_timeout = false; | 99 is_timeout = false; |
92 ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout( | 100 ASSERT_TRUE(popup_window->GetViewBoundsWithTimeout( |
(...skipping 20 matching lines...) Expand all Loading... |
113 number += title[offset]; | 121 number += title[offset]; |
114 offset++; | 122 offset++; |
115 } | 123 } |
116 | 124 |
117 return StringToInt(number, output); | 125 return StringToInt(number, output); |
118 } | 126 } |
119 | 127 |
120 // Tests that in the window.open() equivalent of a fork bomb, we stop building | 128 // Tests that in the window.open() equivalent of a fork bomb, we stop building |
121 // windows. | 129 // windows. |
122 TEST_F(InteractiveConstrainedWindowTest, DontSpawnEndlessPopups) { | 130 TEST_F(InteractiveConstrainedWindowTest, DontSpawnEndlessPopups) { |
123 scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | 131 NavigateMainTabTo(L"infinite_popups.html"); |
124 ASSERT_TRUE(browser.get()); | 132 SimulateClickInCenterOf(window_); |
125 | |
126 scoped_ptr<WindowProxy> window( | |
127 automation()->GetWindowForBrowser(browser.get())); | |
128 ASSERT_TRUE(window.get()); | |
129 | |
130 scoped_ptr<TabProxy> tab(browser->GetTab(0)); | |
131 ASSERT_TRUE(tab.get()); | |
132 | |
133 std::wstring filename(test_data_directory_); | |
134 file_util::AppendToPath(&filename, L"constrained_files"); | |
135 file_util::AppendToPath(&filename, | |
136 L"infinite_popups.html"); | |
137 ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename))); | |
138 | |
139 gfx::Rect tab_view_bounds; | |
140 ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER, | |
141 &tab_view_bounds, true)); | |
142 | |
143 // Simulate a click of the actual link to force user_gesture to be | |
144 // true; if we don't, the resulting popup will be constrained, which | |
145 // isn't what we want to test. | |
146 POINT link_point(tab_view_bounds.CenterPoint().ToPOINT()); | |
147 ASSERT_TRUE(window->SimulateOSClick(link_point, | |
148 views::Event::EF_LEFT_BUTTON_DOWN)); | |
149 | 133 |
150 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000)); | 134 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 1000)); |
151 | 135 |
152 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); | 136 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); |
153 ASSERT_TRUE(popup_browser.get()); | 137 ASSERT_TRUE(popup_browser.get()); |
154 scoped_ptr<TabProxy> popup_tab(popup_browser->GetTab(0)); | 138 scoped_ptr<TabProxy> popup_tab(popup_browser->GetTab(0)); |
155 ASSERT_TRUE(popup_tab.get()); | 139 ASSERT_TRUE(popup_tab.get()); |
156 | 140 |
157 int constrained_window_count = 0; | 141 int constrained_window_count = 0; |
158 ASSERT_TRUE(popup_tab->WaitForChildWindowCountToChange( | 142 ASSERT_TRUE(popup_tab->WaitForChildWindowCountToChange( |
(...skipping 28 matching lines...) Expand all Loading... |
187 } else { | 171 } else { |
188 times_slept = 0; | 172 times_slept = 0; |
189 } | 173 } |
190 | 174 |
191 EXPECT_GE(new_popup_window_count, popup_window_count); | 175 EXPECT_GE(new_popup_window_count, popup_window_count); |
192 EXPECT_LE(new_popup_window_count, kMaxPopupWindows); | 176 EXPECT_LE(new_popup_window_count, kMaxPopupWindows); |
193 popup_window_count = new_popup_window_count; | 177 popup_window_count = new_popup_window_count; |
194 } | 178 } |
195 } | 179 } |
196 | 180 |
197 // Make sure that we refuse to close windows when a constrained popup is display
ed. | 181 // Make sure that we refuse to close windows when a constrained popup is |
| 182 // displayed. |
198 TEST_F(InteractiveConstrainedWindowTest, WindowOpenWindowClosePopup) { | 183 TEST_F(InteractiveConstrainedWindowTest, WindowOpenWindowClosePopup) { |
199 scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0)); | 184 NavigateMainTabTo(L"openclose_main.html"); |
200 ASSERT_TRUE(browser.get()); | 185 SimulateClickInCenterOf(window_); |
201 | |
202 scoped_ptr<WindowProxy> window( | |
203 automation()->GetWindowForBrowser(browser.get())); | |
204 ASSERT_TRUE(window.get()); | |
205 | |
206 scoped_ptr<TabProxy> tab(browser->GetTab(0)); | |
207 ASSERT_TRUE(tab.get()); | |
208 | |
209 std::wstring filename(test_data_directory_); | |
210 file_util::AppendToPath(&filename, L"constrained_files"); | |
211 file_util::AppendToPath(&filename, L"openclose_main.html"); | |
212 ASSERT_TRUE(tab->NavigateToURL(net::FilePathToFileURL(filename))); | |
213 | |
214 gfx::Rect tab_view_bounds; | |
215 ASSERT_TRUE(window->GetViewBounds(VIEW_ID_TAB_CONTAINER, | |
216 &tab_view_bounds, true)); | |
217 | |
218 // Simulate a click of the actual link to force user_gesture to be | |
219 // true; if we don't, the resulting popup will be constrained, which | |
220 // isn't what we want to test. | |
221 POINT link_point(tab_view_bounds.CenterPoint().ToPOINT()); | |
222 ASSERT_TRUE(window->SimulateOSClick(link_point, | |
223 views::Event::EF_LEFT_BUTTON_DOWN)); | |
224 | 186 |
225 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 5000)); | 187 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 5000)); |
226 | 188 |
227 Sleep(1000); | 189 Sleep(1000); |
228 | 190 |
229 // Make sure we have a blocked popup notification | 191 // Make sure we have a blocked popup notification |
230 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); | 192 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); |
231 ASSERT_TRUE(popup_browser.get()); | 193 ASSERT_TRUE(popup_browser.get()); |
232 scoped_ptr<WindowProxy> popup_window( | 194 scoped_ptr<WindowProxy> popup_window( |
233 automation()->GetWindowForBrowser(popup_browser.get())); | 195 automation()->GetWindowForBrowser(popup_browser.get())); |
234 ASSERT_TRUE(popup_window.get()); | 196 ASSERT_TRUE(popup_window.get()); |
235 scoped_ptr<TabProxy> popup_tab(popup_browser->GetTab(0)); | 197 scoped_ptr<TabProxy> popup_tab(popup_browser->GetTab(0)); |
236 ASSERT_TRUE(popup_tab.get()); | 198 ASSERT_TRUE(popup_tab.get()); |
237 scoped_ptr<ConstrainedWindowProxy> popup_notification( | 199 scoped_ptr<ConstrainedWindowProxy> popup_notification( |
238 popup_tab->GetConstrainedWindow(0)); | 200 popup_tab->GetConstrainedWindow(0)); |
239 ASSERT_TRUE(popup_notification.get()); | 201 ASSERT_TRUE(popup_notification.get()); |
240 std::wstring title; | 202 std::wstring title; |
241 ASSERT_TRUE(popup_notification->GetTitle(&title)); | 203 ASSERT_TRUE(popup_notification->GetTitle(&title)); |
242 int count = 0; | 204 int count = 0; |
243 ASSERT_TRUE(ParseCountOutOfTitle(title, &count)); | 205 ASSERT_TRUE(ParseCountOutOfTitle(title, &count)); |
244 ASSERT_EQ(1, count); | 206 ASSERT_EQ(1, count); |
245 | 207 |
246 // Ensure we didn't close the first popup window. | 208 // Ensure we didn't close the first popup window. |
247 ASSERT_FALSE(automation()->WaitForWindowCountToBecome(1, 3000)); | 209 ASSERT_FALSE(automation()->WaitForWindowCountToBecome(1, 3000)); |
248 } | 210 } |
| 211 |
| 212 TEST_F(InteractiveConstrainedWindowTest, BlockAlertFromBlockedPopup) { |
| 213 NavigateMainTabTo(L"block_alert.html"); |
| 214 |
| 215 // Wait for there to be an app modal dialog (and fail if it's shown). |
| 216 ASSERT_FALSE(automation()->WaitForAppModalDialog(4000)); |
| 217 |
| 218 // Ensure one browser window. |
| 219 int browser_window_count; |
| 220 ASSERT_TRUE(automation()->GetBrowserWindowCount(&browser_window_count)); |
| 221 ASSERT_EQ(1, browser_window_count); |
| 222 |
| 223 // Ensure one blocked popup window: the popup didn't escape. |
| 224 scoped_ptr<ConstrainedWindowProxy> popup_notification( |
| 225 tab_->GetConstrainedWindow(0)); |
| 226 ASSERT_TRUE(popup_notification.get()); |
| 227 std::wstring title; |
| 228 ASSERT_TRUE(popup_notification->GetTitle(&title)); |
| 229 int popup_count = 0; |
| 230 ASSERT_TRUE(ParseCountOutOfTitle(title, &popup_count)); |
| 231 ASSERT_EQ(1, popup_count); |
| 232 } |
| 233 |
| 234 TEST_F(InteractiveConstrainedWindowTest, ShowAlertFromNormalPopup) { |
| 235 NavigateMainTabTo(L"show_alert.html"); |
| 236 SimulateClickInCenterOf(window_); |
| 237 |
| 238 ASSERT_TRUE(automation()->WaitForWindowCountToBecome(2, 5000)); |
| 239 |
| 240 scoped_ptr<BrowserProxy> popup_browser(automation()->GetBrowserWindow(1)); |
| 241 ASSERT_TRUE(popup_browser.get()); |
| 242 scoped_ptr<WindowProxy> popup_window( |
| 243 automation()->GetWindowForBrowser(popup_browser.get())); |
| 244 ASSERT_TRUE(popup_window.get()); |
| 245 scoped_ptr<TabProxy> popup_tab(popup_browser->GetTab(0)); |
| 246 ASSERT_TRUE(popup_tab.get()); |
| 247 |
| 248 SimulateClickInCenterOf(popup_window); |
| 249 |
| 250 // Wait for there to be an app modal dialog. |
| 251 ASSERT_TRUE(automation()->WaitForAppModalDialog(5000)); |
| 252 } |
OLD | NEW |