1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/window_sizer/window_sizer_common_unittest.h"
7 #include "ash/wm/window_resizer.h"
8 #include "base/compiler_specific.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/gfx/display.h"
14 #include "ui/gfx/screen.h"
17 #include "ui/aura/window.h"
22 class TestScreen : public gfx::Screen {
25 virtual ~TestScreen() {}
27 // Overridden from gfx::Screen:
28 virtual bool IsDIPEnabled() OVERRIDE {
33 virtual gfx::Point GetCursorScreenPoint() OVERRIDE {
38 virtual gfx::NativeWindow GetWindowUnderCursor() OVERRIDE {
43 virtual gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point)
49 virtual int GetNumDisplays() const OVERRIDE {
50 return displays_.size();
53 virtual std::vector<gfx::Display> GetAllDisplays() const OVERRIDE {
57 virtual gfx::Display GetDisplayNearestWindow(
58 gfx::NativeView view) const OVERRIDE {
60 return GetDisplayMatching(view->GetBoundsInScreen());
63 return gfx::Display();
67 virtual gfx::Display GetDisplayNearestPoint(
68 const gfx::Point& point) const OVERRIDE {
70 return gfx::Display();
73 virtual gfx::Display GetDisplayMatching(
74 const gfx::Rect& match_rect) const OVERRIDE {
76 size_t max_area_index = 0;
78 for (size_t i = 0; i < displays_.size(); ++i) {
79 gfx::Rect overlap = displays_[i].bounds();
80 overlap.Intersect(match_rect);
81 int area = overlap.width() * overlap.height();
82 if (area > max_area) {
87 return displays_[max_area_index];
90 virtual gfx::Display GetPrimaryDisplay() const OVERRIDE {
94 virtual void AddObserver(gfx::DisplayObserver* observer) OVERRIDE {
98 virtual void RemoveObserver(gfx::DisplayObserver* observer) OVERRIDE {
102 void AddDisplay(const gfx::Rect& bounds,
103 const gfx::Rect& work_area) {
104 gfx::Display display(displays_.size(), bounds);
105 display.set_work_area(work_area);
106 displays_.push_back(display);
110 std::vector<gfx::Display> displays_;
112 DISALLOW_COPY_AND_ASSIGN(TestScreen);
117 TestStateProvider::TestStateProvider():
118 has_persistent_data_(false),
119 persistent_show_state_(ui::SHOW_STATE_DEFAULT),
120 has_last_active_data_(false),
121 last_active_show_state_(ui::SHOW_STATE_DEFAULT) {
124 void TestStateProvider::SetPersistentState(const gfx::Rect& bounds,
125 const gfx::Rect& work_area,
126 ui::WindowShowState show_state,
127 bool has_persistent_data) {
128 persistent_bounds_ = bounds;
129 persistent_work_area_ = work_area;
130 persistent_show_state_ = show_state;
131 has_persistent_data_ = has_persistent_data;
134 void TestStateProvider::SetLastActiveState(const gfx::Rect& bounds,
135 ui::WindowShowState show_state,
136 bool has_last_active_data) {
137 last_active_bounds_ = bounds;
138 last_active_show_state_ = show_state;
139 has_last_active_data_ = has_last_active_data;
142 bool TestStateProvider::GetPersistentState(
144 gfx::Rect* saved_work_area,
145 ui::WindowShowState* show_state) const {
147 *bounds = persistent_bounds_;
148 *saved_work_area = persistent_work_area_;
149 if (*show_state == ui::SHOW_STATE_DEFAULT)
150 *show_state = persistent_show_state_;
151 return has_persistent_data_;
154 bool TestStateProvider::GetLastActiveWindowState(
156 ui::WindowShowState* show_state) const {
158 *bounds = last_active_bounds_;
159 if (*show_state == ui::SHOW_STATE_DEFAULT)
160 *show_state = last_active_show_state_;
161 return has_last_active_data_;
164 int kWindowTilePixels = WindowSizer::kWindowTilePixels;
166 // The window sizer commonly used test functions.
167 void GetWindowBoundsAndShowState(
168 const gfx::Rect& monitor1_bounds,
169 const gfx::Rect& monitor1_work_area,
170 const gfx::Rect& monitor2_bounds,
171 const gfx::Rect& bounds,
172 const gfx::Rect& work_area,
173 ui::WindowShowState show_state_persisted,
174 ui::WindowShowState show_state_last,
176 const Browser* browser,
177 const gfx::Rect& passed_in,
178 gfx::Rect* out_bounds,
179 ui::WindowShowState* out_show_state) {
180 DCHECK(out_show_state);
181 TestScreen test_screen;
182 test_screen.AddDisplay(monitor1_bounds, monitor1_work_area);
183 if (!monitor2_bounds.IsEmpty())
184 test_screen.AddDisplay(monitor2_bounds, monitor2_bounds);
185 TestStateProvider* sp = new TestStateProvider;
186 if (source == PERSISTED || source == BOTH)
187 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
188 if (source == LAST_ACTIVE || source == BOTH)
189 sp->SetLastActiveState(bounds, show_state_last, true);
191 WindowSizer sizer(sp, &test_screen, browser);
192 sizer.DetermineWindowBoundsAndShowState(passed_in,
197 void GetWindowBounds(const gfx::Rect& monitor1_bounds,
198 const gfx::Rect& monitor1_work_area,
199 const gfx::Rect& monitor2_bounds,
200 const gfx::Rect& bounds,
201 const gfx::Rect& work_area,
203 const Browser* browser,
204 const gfx::Rect& passed_in,
205 gfx::Rect* out_bounds) {
206 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
207 GetWindowBoundsAndShowState(
208 monitor1_bounds, monitor1_work_area, monitor2_bounds, bounds, work_area,
209 ui::SHOW_STATE_DEFAULT, ui::SHOW_STATE_DEFAULT, source, browser,
210 passed_in, out_bounds, &out_show_state);
213 ui::WindowShowState GetWindowShowState(
214 ui::WindowShowState show_state_persisted,
215 ui::WindowShowState show_state_last,
217 const Browser* browser,
218 const gfx::Rect& display_config) {
219 gfx::Rect bounds = display_config;
220 gfx::Rect work_area = display_config;
221 TestScreen test_screen;
222 test_screen.AddDisplay(display_config, display_config);
223 TestStateProvider* sp = new TestStateProvider;
224 if (source == PERSISTED || source == BOTH)
225 sp->SetPersistentState(bounds, work_area, show_state_persisted, true);
226 if (source == LAST_ACTIVE || source == BOTH)
227 sp->SetLastActiveState(bounds, show_state_last, true);
229 WindowSizer sizer(sp, &test_screen, browser);
231 ui::WindowShowState out_show_state = ui::SHOW_STATE_DEFAULT;
232 gfx::Rect out_bounds;
233 sizer.DetermineWindowBoundsAndShowState(
237 return out_show_state;
240 #if !defined(OS_MACOSX)
241 TEST(WindowSizerTestCommon, PersistedWindowOffscreenWithNonAggressiveRepositioning) {
242 { // off the left but the minimum visibility condition is barely satisfied
243 // without relocaiton.
244 gfx::Rect initial_bounds(-470, 50, 500, 400);
246 gfx::Rect window_bounds;
247 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
248 initial_bounds, gfx::Rect(), PERSISTED,
249 NULL, gfx::Rect(), &window_bounds);
250 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
253 { // off the left and the minimum visibility condition is satisfied by
255 gfx::Rect window_bounds;
256 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
257 gfx::Rect(-471, 50, 500, 400), gfx::Rect(), PERSISTED,
258 NULL, gfx::Rect(), &window_bounds);
259 EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 50, 500, 400).ToString(),
260 window_bounds.ToString());
264 gfx::Rect initial_bounds(50, -370, 500, 400);
266 gfx::Rect window_bounds;
267 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
268 gfx::Rect(50, -370, 500, 400), gfx::Rect(), PERSISTED,
269 NULL, gfx::Rect(), &window_bounds);
270 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
273 { // off the right but the minimum visibility condition is barely satisified
274 // without relocation.
275 gfx::Rect initial_bounds(994, 50, 500, 400);
277 gfx::Rect window_bounds;
278 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
279 initial_bounds, gfx::Rect(), PERSISTED,
280 NULL, gfx::Rect(), &window_bounds);
281 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
284 { // off the right and the minimum visibility condition is satisified by
286 gfx::Rect window_bounds;
287 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
288 gfx::Rect(995, 50, 500, 400), gfx::Rect(), PERSISTED,
289 NULL, gfx::Rect(), &window_bounds);
290 EXPECT_EQ(gfx::Rect(994 /* not 995 */, 50, 500, 400).ToString(),
291 window_bounds.ToString());
294 { // off the bottom but the minimum visibility condition is barely satisified
295 // without relocation.
296 gfx::Rect initial_bounds(50, 738, 500, 400);
298 gfx::Rect window_bounds;
299 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
300 initial_bounds, gfx::Rect(), PERSISTED,
301 NULL, gfx::Rect(), &window_bounds);
302 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
305 { // off the bottom and the minimum visibility condition is satisified by
307 gfx::Rect window_bounds;
308 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
309 gfx::Rect(50, 739, 500, 400), gfx::Rect(), PERSISTED,
310 NULL, gfx::Rect(), &window_bounds);
311 EXPECT_EQ(gfx::Rect(50, 738 /* not 739 */, 500, 400).ToString(),
312 window_bounds.ToString());
316 gfx::Rect window_bounds;
317 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
318 gfx::Rect(-471, -371, 500, 400), gfx::Rect(), PERSISTED,
319 NULL, gfx::Rect(), &window_bounds);
320 EXPECT_EQ(gfx::Rect(-470 /* not -471 */, 0, 500, 400).ToString(),
321 window_bounds.ToString());
324 { // off the topright and the minimum visibility condition is satisified by
326 gfx::Rect window_bounds;
327 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
328 gfx::Rect(995, -371, 500, 400), gfx::Rect(), PERSISTED,
329 NULL, gfx::Rect(), &window_bounds);
330 EXPECT_EQ(gfx::Rect(994 /* not 995 */, 0, 500, 400).ToString(),
331 window_bounds.ToString());
334 { // off the bottomleft and the minimum visibility condition is satisified by
336 gfx::Rect window_bounds;
337 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
338 gfx::Rect(-471, 739, 500, 400), gfx::Rect(), PERSISTED,
339 NULL, gfx::Rect(), &window_bounds);
340 EXPECT_EQ(gfx::Rect(-470 /* not -471 */,
344 window_bounds.ToString());
347 { // off the bottomright and the minimum visibility condition is satisified by
349 gfx::Rect window_bounds;
350 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
351 gfx::Rect(995, 739, 500, 400), gfx::Rect(), PERSISTED,
352 NULL, gfx::Rect(), &window_bounds);
353 EXPECT_EQ(gfx::Rect(994 /* not 995 */,
357 window_bounds.ToString());
360 { // entirely off left
361 gfx::Rect window_bounds;
362 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
363 gfx::Rect(-700, 50, 500, 400), gfx::Rect(), PERSISTED,
364 NULL, gfx::Rect(), &window_bounds);
365 EXPECT_EQ(gfx::Rect(-470 /* not -700 */, 50, 500, 400).ToString(),
366 window_bounds.ToString());
369 { // entirely off left (monitor was detached since last run)
370 gfx::Rect window_bounds;
371 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
372 gfx::Rect(-700, 50, 500, 400), left_s1024x768, PERSISTED,
373 NULL, gfx::Rect(), &window_bounds);
374 EXPECT_EQ("0,50 500x400", window_bounds.ToString());
377 { // entirely off top
378 gfx::Rect window_bounds;
379 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
380 gfx::Rect(50, -500, 500, 400), gfx::Rect(), PERSISTED,
381 NULL, gfx::Rect(), &window_bounds);
382 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
385 { // entirely off top (monitor was detached since last run)
386 gfx::Rect window_bounds;
387 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
388 gfx::Rect(50, -500, 500, 400), top_s1024x768,
389 PERSISTED, NULL, gfx::Rect(), &window_bounds);
390 EXPECT_EQ("50,0 500x400", window_bounds.ToString());
393 { // entirely off right
394 gfx::Rect window_bounds;
395 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
396 gfx::Rect(1200, 50, 500, 400), gfx::Rect(), PERSISTED,
397 NULL, gfx::Rect(), &window_bounds);
398 EXPECT_EQ(gfx::Rect(994 /* not 1200 */, 50, 500, 400).ToString(),
399 window_bounds.ToString());
402 { // entirely off right (monitor was detached since last run)
403 gfx::Rect window_bounds;
404 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
405 gfx::Rect(1200, 50, 500, 400), right_s1024x768,
406 PERSISTED, NULL, gfx::Rect(), &window_bounds);
407 EXPECT_EQ("524,50 500x400", window_bounds.ToString());
410 { // entirely off bottom
411 gfx::Rect window_bounds;
412 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
413 gfx::Rect(50, 800, 500, 400), gfx::Rect(), PERSISTED,
414 NULL, gfx::Rect(), &window_bounds);
415 EXPECT_EQ(gfx::Rect(50, 738 /* not 800 */, 500, 400).ToString(),
416 window_bounds.ToString());
419 { // entirely off bottom (monitor was detached since last run)
420 gfx::Rect window_bounds;
421 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
422 gfx::Rect(50, 800, 500, 400), bottom_s1024x768,
423 PERSISTED, NULL, gfx::Rect(), &window_bounds);
424 EXPECT_EQ("50,368 500x400", window_bounds.ToString());
428 // Test that the window is sized appropriately for the first run experience
429 // where the default window bounds calculation is invoked.
430 TEST(WindowSizerTestCommon, AdjustFitSize) {
431 { // Check that the window gets resized to the screen.
432 gfx::Rect window_bounds;
433 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
434 gfx::Rect(), DEFAULT, NULL,
435 gfx::Rect(-10, -10, 1024 + 20, 768 + 20), &window_bounds);
436 EXPECT_EQ("0,0 1024x768", window_bounds.ToString());
439 { // Check that a window which hangs out of the screen get moved back in.
440 gfx::Rect window_bounds;
441 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
442 gfx::Rect(), DEFAULT, NULL,
443 gfx::Rect(1020, 700, 100, 100), &window_bounds);
444 EXPECT_EQ("924,668 100x100", window_bounds.ToString());
448 #endif // defined(OS_MACOSX)