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 "ash/scoped_target_root_window.h"
6 #include "ash/screen_util.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/test_shell_delegate.h"
10 #include "ash/wm/window_positioner.h"
11 #include "ash/wm/window_resizer.h"
12 #include "ash/wm/window_state.h"
13 #include "base/compiler_specific.h"
14 #include "chrome/browser/ui/ash/ash_util.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/window_sizer/window_sizer_common_unittest.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/test/render_view_test.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/test/test_windows.h"
24 #include "ui/aura/window_event_dispatcher.h"
25 #include "ui/gfx/screen.h"
26 #include "ui/wm/public/activation_client.h"
28 typedef ash::test::AshTestBase WindowSizerAshTest;
32 // A browser window proxy which is able to associate an aura native window with
34 class TestBrowserWindowAura : public TestBrowserWindow {
36 // |native_window| will still be owned by the caller after the constructor
38 explicit TestBrowserWindowAura(aura::Window* native_window)
39 : native_window_(native_window) {
41 ~TestBrowserWindowAura() override {}
43 // TestBrowserWindow overrides:
44 void Show() override {
45 native_window_->Show();
48 void Hide() override { native_window_->Hide(); }
49 void Activate() override {
50 aura::client::GetActivationClient(
51 native_window_->GetRootWindow())->ActivateWindow(native_window_.get());
53 gfx::NativeWindow GetNativeWindow() const override {
54 return native_window_.get();
56 gfx::Rect GetBounds() const override { return native_window_->bounds(); }
58 Browser* browser() { return browser_.get(); }
60 void CreateBrowser(const Browser::CreateParams& params) {
61 Browser::CreateParams create_params = params;
62 create_params.window = this;
63 browser_.reset(new Browser(create_params));
64 if (browser_->is_type_tabbed() || browser_->is_app()) {
65 ash::wm::GetWindowState(native_window_.get())->
66 set_window_position_managed(true);
71 scoped_ptr<Browser> browser_;
72 scoped_ptr<aura::Window> native_window_;
74 DISALLOW_COPY_AND_ASSIGN(TestBrowserWindowAura);
77 scoped_ptr<TestBrowserWindowAura> CreateTestBrowserWindow(
79 const gfx::Rect& bounds,
80 const Browser::CreateParams& params) {
81 if (!bounds.IsEmpty())
82 window->SetBounds(bounds);
83 scoped_ptr<TestBrowserWindowAura> browser_window(
84 new TestBrowserWindowAura(window));
85 browser_window->CreateBrowser(params);
86 return browser_window.Pass();
91 // On desktop linux aura, we currently don't use the ash frame, breaking some
92 // tests which expect ash sizes: http://crbug.com/303862
93 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
94 #define MAYBE_DefaultSizeCase DISABLED_DefaultSizeCase
96 #define MAYBE_DefaultSizeCase DefaultSizeCase
99 // Test that the window is sized appropriately for the first run experience
100 // where the default window bounds calculation is invoked.
101 TEST_F(WindowSizerAshTest, MAYBE_DefaultSizeCase) {
103 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kOpenAsh);
105 { // 4:3 monitor case, 1024x768, no taskbar
106 gfx::Rect window_bounds;
107 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), gfx::Rect(),
108 gfx::Rect(), DEFAULT, NULL, gfx::Rect(), &window_bounds);
109 EXPECT_EQ(gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
110 ash::WindowPositioner::kDesktopBorderSize,
111 1024 - ash::WindowPositioner::kDesktopBorderSize * 2,
112 768 - ash::WindowPositioner::kDesktopBorderSize),
116 { // 4:3 monitor case, 1024x768, taskbar on bottom
117 gfx::Rect window_bounds;
118 GetWindowBounds(p1024x768, taskbar_bottom_work_area, gfx::Rect(),
119 gfx::Rect(), gfx::Rect(), DEFAULT, NULL, gfx::Rect(),
121 EXPECT_EQ(gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
122 ash::WindowPositioner::kDesktopBorderSize,
123 1024 - ash::WindowPositioner::kDesktopBorderSize * 2,
124 taskbar_bottom_work_area.height() -
125 ash::WindowPositioner::kDesktopBorderSize),
129 { // 4:3 monitor case, 1024x768, taskbar on right
130 gfx::Rect window_bounds;
131 GetWindowBounds(p1024x768, taskbar_right_work_area, gfx::Rect(),
132 gfx::Rect(), gfx::Rect(), DEFAULT, NULL, gfx::Rect(),
134 EXPECT_EQ(gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
135 ash::WindowPositioner::kDesktopBorderSize,
136 taskbar_right_work_area.width() -
137 ash::WindowPositioner::kDesktopBorderSize * 2,
138 768 - ash::WindowPositioner::kDesktopBorderSize),
142 { // 4:3 monitor case, 1024x768, taskbar on left
143 gfx::Rect window_bounds;
144 GetWindowBounds(p1024x768, taskbar_left_work_area, gfx::Rect(),
145 gfx::Rect(), gfx::Rect(), DEFAULT, NULL, gfx::Rect(),
147 EXPECT_EQ(gfx::Rect(taskbar_left_work_area.x() +
148 ash::WindowPositioner::kDesktopBorderSize,
149 ash::WindowPositioner::kDesktopBorderSize,
150 taskbar_left_work_area.width() -
151 ash::WindowPositioner::kDesktopBorderSize * 2,
152 taskbar_left_work_area.height() -
153 ash::WindowPositioner::kDesktopBorderSize),
157 { // 4:3 monitor case, 1024x768, taskbar on top
158 gfx::Rect window_bounds;
159 GetWindowBounds(p1024x768, taskbar_top_work_area, gfx::Rect(),
160 gfx::Rect(), gfx::Rect(), DEFAULT, NULL, gfx::Rect(),
162 EXPECT_EQ(gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
163 taskbar_top_work_area.y() +
164 ash::WindowPositioner::kDesktopBorderSize,
165 1024 - ash::WindowPositioner::kDesktopBorderSize * 2,
166 taskbar_top_work_area.height() -
167 ash::WindowPositioner::kDesktopBorderSize),
171 { // 4:3 monitor case, 1280x1024
172 gfx::Rect window_bounds;
173 GetWindowBounds(p1280x1024, p1280x1024, gfx::Rect(), gfx::Rect(),
174 gfx::Rect(), DEFAULT, NULL, gfx::Rect(), &window_bounds);
175 EXPECT_EQ(gfx::Rect((1280 - ash::WindowPositioner::kMaximumWindowWidth) / 2,
176 ash::WindowPositioner::kDesktopBorderSize,
177 ash::WindowPositioner::kMaximumWindowWidth,
178 1024 - ash::WindowPositioner::kDesktopBorderSize),
182 { // 4:3 monitor case, 1600x1200
183 gfx::Rect window_bounds;
184 GetWindowBounds(p1600x1200, p1600x1200, gfx::Rect(), gfx::Rect(),
185 gfx::Rect(), DEFAULT, NULL, gfx::Rect(), &window_bounds);
186 EXPECT_EQ(gfx::Rect((1600 - ash::WindowPositioner::kMaximumWindowWidth) / 2,
187 ash::WindowPositioner::kDesktopBorderSize,
188 ash::WindowPositioner::kMaximumWindowWidth,
189 1200 - ash::WindowPositioner::kDesktopBorderSize),
193 { // 16:10 monitor case, 1680x1050
194 gfx::Rect window_bounds;
195 GetWindowBounds(p1680x1050, p1680x1050, gfx::Rect(), gfx::Rect(),
196 gfx::Rect(), DEFAULT, NULL, gfx::Rect(), &window_bounds);
197 EXPECT_EQ(gfx::Rect((1680 - ash::WindowPositioner::kMaximumWindowWidth) / 2,
198 ash::WindowPositioner::kDesktopBorderSize,
199 ash::WindowPositioner::kMaximumWindowWidth,
200 1050 - ash::WindowPositioner::kDesktopBorderSize),
204 { // 16:10 monitor case, 1920x1200
205 gfx::Rect window_bounds;
206 GetWindowBounds(p1920x1200, p1920x1200, gfx::Rect(), gfx::Rect(),
207 gfx::Rect(), DEFAULT, NULL, gfx::Rect(), &window_bounds);
208 EXPECT_EQ(gfx::Rect((1920 - ash::WindowPositioner::kMaximumWindowWidth) / 2,
209 ash::WindowPositioner::kDesktopBorderSize,
210 ash::WindowPositioner::kMaximumWindowWidth,
211 1200 - ash::WindowPositioner::kDesktopBorderSize),
216 // Test that the next opened window is positioned appropriately given the
217 // bounds of an existing window of the same type.
218 TEST_F(WindowSizerAshTest, LastWindowBoundsCase) {
219 { // normal, in the middle of the screen somewhere.
220 gfx::Rect window_bounds;
222 p1024x768, p1024x768, gfx::Rect(),
223 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
224 ash::WindowPositioner::kDesktopBorderSize, 500, 400),
225 gfx::Rect(), LAST_ACTIVE, NULL, gfx::Rect(),
228 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
229 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
230 500, 400).ToString(),
231 window_bounds.ToString());
235 gfx::Rect window_bounds;
237 p1024x768, taskbar_top_work_area, gfx::Rect(),
238 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
239 ash::WindowPositioner::kDesktopBorderSize, 500, 400),
240 gfx::Rect(), LAST_ACTIVE, NULL, gfx::Rect(),
243 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
244 std::max(kWindowTilePixels +
245 ash::WindowPositioner::kDesktopBorderSize,
246 34 /* toolbar height */),
247 500, 400).ToString(),
248 window_bounds.ToString());
251 { // Too small to satisify the minimum visibility condition.
252 gfx::Rect window_bounds;
254 p1024x768, p1024x768, gfx::Rect(),
255 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
256 ash::WindowPositioner::kDesktopBorderSize, 29, 29),
257 gfx::Rect(), LAST_ACTIVE, NULL, gfx::Rect(),
260 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
261 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
263 30 /* not 29 */).ToString(),
264 window_bounds.ToString());
269 gfx::Rect window_bounds;
271 p1024x768, p1024x768, gfx::Rect(),
272 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
273 ash::WindowPositioner::kDesktopBorderSize, 500, 400),
274 gfx::Rect(), LAST_ACTIVE, NULL, gfx::Rect(),
277 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
278 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
279 500, 400).ToString(),
280 window_bounds.ToString());
284 // Test that the window opened is sized appropriately given persisted sizes.
285 TEST_F(WindowSizerAshTest, PersistedBoundsCase) {
286 { // normal, in the middle of the screen somewhere.
287 gfx::Rect initial_bounds(
288 ash::WindowPositioner::kDesktopBorderSize,
289 ash::WindowPositioner::kDesktopBorderSize, 500, 400);
291 gfx::Rect window_bounds;
292 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), initial_bounds,
293 gfx::Rect(), PERSISTED, NULL, gfx::Rect(), &window_bounds);
294 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
298 gfx::Rect initial_bounds(0, 0, 1024, 768);
300 gfx::Rect window_bounds;
301 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(), initial_bounds,
302 gfx::Rect(), PERSISTED, NULL, gfx::Rect(), &window_bounds);
303 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
306 { // normal, on non-primary monitor in negative coords.
307 gfx::Rect initial_bounds(-600, 10, 500, 400);
309 gfx::Rect window_bounds;
310 GetWindowBounds(p1024x768, p1024x768, left_s1024x768,
311 initial_bounds, gfx::Rect(), PERSISTED, NULL, gfx::Rect(),
313 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
316 { // normal, on non-primary monitor in negative coords.
317 gfx::Rect initial_bounds(-1024, 0, 1024, 768);
319 gfx::Rect window_bounds;
320 GetWindowBounds(p1024x768, p1024x768, left_s1024x768,
321 initial_bounds, gfx::Rect(), PERSISTED, NULL, gfx::Rect(),
323 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
326 { // Non-primary monitor resoultion has changed, but the monitor still
327 // completely contains the window.
329 gfx::Rect initial_bounds(1074, 50, 600, 500);
331 gfx::Rect window_bounds;
332 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(1024, 0, 800, 600),
333 initial_bounds, right_s1024x768, PERSISTED, NULL,
334 gfx::Rect(), &window_bounds);
335 EXPECT_EQ(initial_bounds.ToString(), window_bounds.ToString());
338 { // Non-primary monitor resoultion has changed, and the window is partially
341 gfx::Rect initial_bounds(1274, 50, 600, 500);
343 gfx::Rect window_bounds;
344 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(1024, 0, 800, 600),
345 initial_bounds, right_s1024x768, PERSISTED,
346 NULL, gfx::Rect(), &window_bounds);
347 EXPECT_EQ("1224,50 600x500", window_bounds.ToString());
350 { // Non-primary monitor resoultion has changed, and the window is now too
351 // large for the monitor.
353 gfx::Rect initial_bounds(1274, 50, 900, 700);
355 gfx::Rect window_bounds;
356 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(1024, 0, 800, 600),
357 initial_bounds, right_s1024x768, PERSISTED,
358 NULL, gfx::Rect(), &window_bounds);
359 EXPECT_EQ("1024,0 800x600", window_bounds.ToString());
362 { // width and height too small
363 gfx::Rect window_bounds;
365 p1024x768, p1024x768, gfx::Rect(),
366 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
367 ash::WindowPositioner::kDesktopBorderSize, 29, 29),
368 gfx::Rect(), PERSISTED, NULL, gfx::Rect(), &window_bounds);
369 EXPECT_EQ(gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
370 ash::WindowPositioner::kDesktopBorderSize,
371 30 /* not 29 */, 30 /* not 29 */).ToString(),
372 window_bounds.ToString());
376 //////////////////////////////////////////////////////////////////////////////
377 // The following unittests have different results on Mac/non-Mac because we
378 // reposition windows aggressively on Mac. The *WithAggressiveReposition tests
379 // are run on Mac, and the *WithNonAggressiveRepositioning tests are run on
382 TEST_F(WindowSizerAshTest, LastWindowOffscreenWithNonAggressiveRepositioning) {
383 { // taskbar on left.
384 gfx::Rect window_bounds;
386 p1024x768, taskbar_left_work_area, gfx::Rect(),
387 gfx::Rect(ash::WindowPositioner::kDesktopBorderSize,
388 ash::WindowPositioner::kDesktopBorderSize, 500, 400),
389 gfx::Rect(), LAST_ACTIVE, NULL, gfx::Rect(),
392 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
393 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
394 500, 400).ToString(),
395 window_bounds.ToString());
398 { // offset would put the new window offscreen at the bottom but the minimum
399 // visibility condition is barely satisfied without relocation.
400 gfx::Rect window_bounds;
401 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
402 gfx::Rect(10, 728, 500, 400), gfx::Rect(), LAST_ACTIVE,
403 NULL, gfx::Rect(), &window_bounds);
404 EXPECT_EQ(gfx::Rect(10 + kWindowTilePixels, 738, 500, 400).ToString(),
405 window_bounds.ToString());
408 { // offset would put the new window offscreen at the bottom and the minimum
409 // visibility condition is satisified by relocation.
410 gfx::Rect window_bounds;
411 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
412 gfx::Rect(10, 729, 500, 400), gfx::Rect(), LAST_ACTIVE,
413 NULL, gfx::Rect(), &window_bounds);
414 EXPECT_EQ(gfx::Rect(10 + kWindowTilePixels,
418 window_bounds.ToString());
421 { // offset would put the new window offscreen at the right but the minimum
422 // visibility condition is barely satisfied without relocation.
423 gfx::Rect window_bounds;
424 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
425 gfx::Rect(984, 10, 500, 400), gfx::Rect(), LAST_ACTIVE,
426 NULL, gfx::Rect(), &window_bounds);
427 EXPECT_EQ(gfx::Rect(994, 10 + kWindowTilePixels, 500, 400).ToString(),
428 window_bounds.ToString());
431 { // offset would put the new window offscreen at the right and the minimum
432 // visibility condition is satisified by relocation.
433 gfx::Rect window_bounds;
434 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
435 gfx::Rect(985, 10, 500, 400), gfx::Rect(), LAST_ACTIVE,
436 NULL, gfx::Rect(), &window_bounds);
437 EXPECT_EQ(gfx::Rect(994 /* not 995 */,
438 10 + kWindowTilePixels,
441 window_bounds.ToString());
444 { // offset would put the new window offscreen at the bottom right and the
445 // minimum visibility condition is satisified by relocation.
446 gfx::Rect window_bounds;
447 GetWindowBounds(p1024x768, p1024x768, gfx::Rect(),
448 gfx::Rect(985, 729, 500, 400), gfx::Rect(), LAST_ACTIVE,
449 NULL, gfx::Rect(), &window_bounds);
450 EXPECT_EQ(gfx::Rect(994 /* not 995 */,
454 window_bounds.ToString());
458 // On desktop linux aura, we currently don't use the ash frame, breaking some
459 // tests which expect ash sizes: http://crbug.com/303862
460 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
461 #define MAYBE_PlaceNewWindows DISABLED_PlaceNewWindows
463 #define MAYBE_PlaceNewWindows PlaceNewWindows
466 // Test the placement of newly created windows.
467 TEST_F(WindowSizerAshTest, MAYBE_PlaceNewWindows) {
468 // Create a browser to pass into the GetWindowBounds function.
469 scoped_ptr<TestingProfile> profile(new TestingProfile());
470 // Creating a popup handler here to make sure it does not interfere with the
472 Browser::CreateParams native_params(profile.get(),
473 chrome::HOST_DESKTOP_TYPE_ASH);
474 scoped_ptr<Browser> browser(
475 chrome::CreateBrowserWithTestWindowForParams(&native_params));
477 // Creating a popup handler here to make sure it does not interfere with the
479 scoped_ptr<BrowserWindow> browser_window(CreateTestBrowserWindow(
480 CreateTestWindowInShellWithId(0),
481 gfx::Rect(16, 32, 640, 320),
482 Browser::CreateParams(profile.get(), chrome::HOST_DESKTOP_TYPE_ASH)));
484 // Creating a popup to make sure it does not interfere with the positioning.
485 scoped_ptr<TestBrowserWindowAura> browser_popup(CreateTestBrowserWindow(
486 CreateTestWindowInShellWithId(1),
487 gfx::Rect(16, 32, 128, 256),
488 Browser::CreateParams(Browser::TYPE_POPUP, profile.get(),
489 chrome::HOST_DESKTOP_TYPE_ASH)));
491 // Creating a panel to make sure it does not interfere with the positioning.
492 scoped_ptr<BrowserWindow> browser_panel(CreateTestBrowserWindow(
493 CreateTestWindowInShellWithId(2),
494 gfx::Rect(32, 48, 256, 512),
495 Browser::CreateParams(Browser::TYPE_POPUP, profile.get(),
496 chrome::HOST_DESKTOP_TYPE_ASH)));
497 browser_window->Show();
498 { // Make sure that popups do not get changed.
499 gfx::Rect window_bounds;
500 GetWindowBounds(p1600x1200, p1600x1200, gfx::Rect(),
501 gfx::Rect(50, 100, 300, 150), bottom_s1600x1200,
502 PERSISTED, browser_popup->browser(),
503 gfx::Rect(), &window_bounds);
504 EXPECT_EQ("50,100 300x150", window_bounds.ToString());
507 browser_window->Hide();
508 { // If a window is there but not shown the persisted default should be used.
509 gfx::Rect window_bounds;
510 GetWindowBounds(p1600x1200, p1600x1200, gfx::Rect(),
511 gfx::Rect(50, 100, 300, 150), bottom_s1600x1200,
512 PERSISTED, browser.get(), gfx::Rect(), &window_bounds);
513 EXPECT_EQ("50,100 300x150", window_bounds.ToString());
516 { // If a window is there but not shown the default should be returned.
517 gfx::Rect window_bounds;
518 GetWindowBounds(p1600x1200, p1600x1200, gfx::Rect(),
519 gfx::Rect(), bottom_s1600x1200,
520 DEFAULT, browser.get(), gfx::Rect(), &window_bounds);
521 // Note: We need to also take the defaults maximum width into account here
522 // since that might get used if the resolution is too big.
525 std::max(ash::WindowPositioner::kDesktopBorderSize,
526 (1600 - ash::WindowPositioner::kMaximumWindowWidth) / 2),
527 ash::WindowPositioner::kDesktopBorderSize,
528 std::min(ash::WindowPositioner::kMaximumWindowWidth,
529 1600 - 2 * ash::WindowPositioner::kDesktopBorderSize),
530 1200 - ash::WindowPositioner::kDesktopBorderSize).ToString(),
531 window_bounds.ToString());
535 // On desktop linux aura, we currently don't use the ash frame, breaking some
536 // tests which expect ash sizes: http://crbug.com/303862
537 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
538 #define MAYBE_PlaceNewBrowserWindowOnEmptyDesktop DISABLED_PlaceNewBrowserWindowOnEmptyDesktop
540 #define MAYBE_PlaceNewBrowserWindowOnEmptyDesktop PlaceNewBrowserWindowOnEmptyDesktop
543 // Test the placement of newly created windows on an empty desktop.
544 // This test supplements "PlaceNewWindows" by testing the creation of a newly
545 // created browser window on an empty desktop.
546 TEST_F(WindowSizerAshTest, MAYBE_PlaceNewBrowserWindowOnEmptyDesktop) {
547 // Create a browser to pass into the GetWindowBoundsAndShowState function.
548 scoped_ptr<TestingProfile> profile(new TestingProfile());
549 Browser::CreateParams native_params(profile.get(),
550 chrome::HOST_DESKTOP_TYPE_ASH);
551 scoped_ptr<Browser> browser(
552 chrome::CreateBrowserWithTestWindowForParams(&native_params));
554 // A common screen size for Chrome OS devices where this behavior is
556 const gfx::Rect p1366x768(0, 0, 1366, 768);
558 // If there is no previous state the window should get maximized if the
559 // screen is less than or equal to our limit (1366 pixels width).
560 gfx::Rect window_bounds;
561 ui::WindowShowState out_show_state1 = ui::SHOW_STATE_DEFAULT;
562 GetWindowBoundsAndShowState(
563 p1366x768, // The screen resolution.
564 p1366x768, // The monitor work area.
565 gfx::Rect(), // The second monitor.
566 gfx::Rect(), // The (persisted) bounds.
567 p1366x768, // The overall work area.
568 ui::SHOW_STATE_NORMAL, // The persisted show state.
569 ui::SHOW_STATE_DEFAULT, // The last show state.
570 DEFAULT, // No persisted values.
571 browser.get(), // Use this browser.
572 gfx::Rect(), // Don't request valid bounds.
575 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, out_show_state1);
577 // If there is a stored coordinate however, that should be taken instead.
578 ui::WindowShowState out_show_state2 = ui::SHOW_STATE_DEFAULT;
579 GetWindowBoundsAndShowState(
580 p1366x768, // The screen resolution.
581 p1366x768, // The monitor work area.
582 gfx::Rect(), // The second monitor.
583 gfx::Rect(50, 100, 300, 150), // The (persisted) bounds.
584 p1366x768, // The overall work area.
585 ui::SHOW_STATE_NORMAL, // The persisted show state.
586 ui::SHOW_STATE_DEFAULT, // The last show state.
587 PERSISTED, // Set the persisted values.
588 browser.get(), // Use this browser.
589 gfx::Rect(), // Don't request valid bounds.
592 EXPECT_EQ(ui::SHOW_STATE_NORMAL, out_show_state2);
593 EXPECT_EQ("50,100 300x150", window_bounds.ToString());
595 // A larger monitor should not trigger auto-maximize.
596 ui::WindowShowState out_show_state3 = ui::SHOW_STATE_DEFAULT;
597 GetWindowBoundsAndShowState(
598 p1600x1200, // The screen resolution.
599 p1600x1200, // The monitor work area.
600 gfx::Rect(), // The second monitor.
601 gfx::Rect(), // The (persisted) bounds.
602 p1600x1200, // The overall work area.
603 ui::SHOW_STATE_NORMAL, // The persisted show state.
604 ui::SHOW_STATE_DEFAULT, // The last show state.
605 DEFAULT, // No persisted values.
606 browser.get(), // Use this browser.
607 gfx::Rect(), // Don't request valid bounds.
611 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, out_show_state3);
613 EXPECT_EQ(ui::SHOW_STATE_DEFAULT, out_show_state3);
617 #if defined(OS_CHROMEOS)
618 #define MAYBE_PlaceNewWindowsOnMultipleDisplays PlaceNewWindowsOnMultipleDisplays
620 // No multiple displays on windows ash.
621 #define MAYBE_PlaceNewWindowsOnMultipleDisplays DISABLED_PlaceNewWindowsOnMultipleDisplays
624 // Test the placement of newly created windows on multiple dislays.
625 TEST_F(WindowSizerAshTest, MAYBE_PlaceNewWindowsOnMultipleDisplays) {
626 UpdateDisplay("1600x1200,1600x1200");
627 gfx::Rect primary_bounds = ash::Shell::GetInstance()->GetScreen()->
628 GetPrimaryDisplay().bounds();
629 gfx::Rect secondary_bounds = ash::ScreenUtil::GetSecondaryDisplay().bounds();
631 ash::Shell::GetInstance()->set_target_root_window(
632 ash::Shell::GetPrimaryRootWindow());
634 scoped_ptr<TestingProfile> profile(new TestingProfile());
636 // Create browser windows that are used as reference.
637 scoped_ptr<BrowserWindow> browser_window(CreateTestBrowserWindow(
638 CreateTestWindowInShellWithId(0),
639 gfx::Rect(10, 10, 200, 200),
640 Browser::CreateParams(profile.get(), chrome::HOST_DESKTOP_TYPE_ASH)));
641 browser_window->Show();
642 EXPECT_EQ(browser_window->GetNativeWindow()->GetRootWindow(),
643 ash::Shell::GetTargetRootWindow());
645 scoped_ptr<BrowserWindow> another_browser_window(CreateTestBrowserWindow(
646 CreateTestWindowInShellWithId(1),
647 gfx::Rect(400, 10, 300, 300),
648 Browser::CreateParams(profile.get(), chrome::HOST_DESKTOP_TYPE_ASH)));
649 another_browser_window->Show();
651 // Creating a new window to verify the new placement.
652 scoped_ptr<TestBrowserWindowAura> new_browser_window(CreateTestBrowserWindow(
653 CreateTestWindowInShellWithId(0),
655 Browser::CreateParams(profile.get(),
656 chrome::HOST_DESKTOP_TYPE_ASH)));
658 // Make sure the primary root is active.
659 ASSERT_EQ(ash::Shell::GetPrimaryRootWindow(),
660 ash::Shell::GetTargetRootWindow());
662 // First new window should be in the primary.
664 gfx::Rect window_bounds;
665 GetWindowBounds(p1600x1200, p1600x1200, secondary_bounds,
666 gfx::Rect(), secondary_bounds,
667 PERSISTED, new_browser_window->browser(),
668 gfx::Rect(), &window_bounds);
669 // TODO(oshima): Use exact bounds when the window_sizer_ash is
670 // moved to ash and changed to include the result from
671 // RearrangeVisibleWindowOnShow.
672 EXPECT_TRUE(primary_bounds.Contains(window_bounds));
675 // Move the window to the right side of the secondary display and create a new
676 // window. It should be opened then on the secondary display.
678 gfx::Display second_display = ash::Shell::GetScreen()->
679 GetDisplayNearestPoint(gfx::Point(1600 + 100,10));
680 browser_window->GetNativeWindow()->SetBoundsInScreen(
681 gfx::Rect(secondary_bounds.CenterPoint().x() - 100, 10, 200, 200),
683 browser_window->Activate();
684 EXPECT_NE(ash::Shell::GetPrimaryRootWindow(),
685 ash::Shell::GetTargetRootWindow());
686 gfx::Rect window_bounds;
687 GetWindowBounds(p1600x1200, p1600x1200, secondary_bounds,
688 gfx::Rect(), secondary_bounds,
689 PERSISTED, new_browser_window->browser(),
690 gfx::Rect(), &window_bounds);
691 // TODO(oshima): Use exact bounds when the window_sizer_ash is
692 // moved to ash and changed to include the result from
693 // RearrangeVisibleWindowOnShow.
694 EXPECT_TRUE(secondary_bounds.Contains(window_bounds));
697 // Activate another window in the primary display and create a new window.
698 // It should be created in the primary display.
700 another_browser_window->Activate();
701 EXPECT_EQ(ash::Shell::GetPrimaryRootWindow(),
702 ash::Shell::GetTargetRootWindow());
704 gfx::Rect window_bounds;
705 GetWindowBounds(p1600x1200, p1600x1200, secondary_bounds,
706 gfx::Rect(), secondary_bounds,
707 PERSISTED, new_browser_window->browser(),
708 gfx::Rect(), &window_bounds);
709 // TODO(oshima): Use exact bounds when the window_sizer_ash is
710 // moved to ash and changed to include the result from
711 // RearrangeVisibleWindowOnShow.
712 EXPECT_TRUE(primary_bounds.Contains(window_bounds));
716 // On desktop linux aura, we currently don't use the ash frame, breaking some
717 // tests which expect ash sizes: http://crbug.com/303862
718 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
719 #define MAYBE_TestShowState DISABLED_TestShowState
721 #define MAYBE_TestShowState TestShowState
724 // Test that the show state is properly returned for non default cases.
725 TEST_F(WindowSizerAshTest, MAYBE_TestShowState) {
726 scoped_ptr<TestingProfile> profile(new TestingProfile());
728 // Creating a browser & window to play with.
729 scoped_ptr<TestBrowserWindowAura> browser_window(CreateTestBrowserWindow(
730 CreateTestWindowInShellWithId(0),
731 gfx::Rect(16, 32, 640, 320),
732 Browser::CreateParams(Browser::TYPE_TABBED, profile.get(),
733 chrome::HOST_DESKTOP_TYPE_ASH)));
735 // Create also a popup browser since that behaves different.
736 scoped_ptr<TestBrowserWindowAura> browser_popup(CreateTestBrowserWindow(
737 CreateTestWindowInShellWithId(1),
738 gfx::Rect(16, 32, 640, 320),
739 Browser::CreateParams(Browser::TYPE_POPUP, profile.get(),
740 chrome::HOST_DESKTOP_TYPE_ASH)));
742 // Tabbed windows should retrieve the saved window state - since there is a
744 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
745 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
746 ui::SHOW_STATE_NORMAL,
748 browser_window->browser(),
751 // A window that is smaller than the whole work area is set to default state.
752 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
753 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
754 ui::SHOW_STATE_NORMAL,
756 browser_window->browser(),
759 // A window that is sized to occupy the whole work area is maximized.
760 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
761 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
762 ui::SHOW_STATE_NORMAL,
764 browser_window->browser(),
767 // Non tabbed windows should always follow the window saved visibility state.
768 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
769 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
770 ui::SHOW_STATE_NORMAL,
772 browser_popup->browser(),
775 // The non tabbed window will take the status of the last active of its kind.
776 EXPECT_EQ(ui::SHOW_STATE_NORMAL,
777 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
778 ui::SHOW_STATE_NORMAL,
780 browser_popup->browser(),
784 // Now create a top level window and check again for both. Only the tabbed
785 // window should follow the top level window's state.
786 // Creating a browser & window to play with.
787 scoped_ptr<TestBrowserWindowAura> browser_window2(CreateTestBrowserWindow(
788 CreateTestWindowInShellWithId(3),
789 gfx::Rect(16, 32, 640, 320),
790 Browser::CreateParams(Browser::TYPE_TABBED, profile.get(),
791 chrome::HOST_DESKTOP_TYPE_ASH)));
793 // A tabbed window should now take the top level window state.
794 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
795 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
796 ui::SHOW_STATE_DEFAULT,
798 browser_window->browser(),
801 // Non tabbed windows should always follow the window saved visibility state.
802 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
803 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
804 ui::SHOW_STATE_MINIMIZED,
806 browser_popup->browser(),
810 // In smaller screen resolutions we default to maximized if there is no other
812 int min_size = ash::WindowPositioner::GetForceMaximizedWidthLimit() / 2;
814 const gfx::Rect tiny_screen(0, 0, min_size, min_size);
815 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
816 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
817 ui::SHOW_STATE_DEFAULT,
819 browser_window->browser(),
822 browser_window->Hide();
823 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
824 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
825 ui::SHOW_STATE_DEFAULT,
827 browser_window2->browser(),
834 // Test that the default show state override behavior is properly handled.
835 TEST_F(WindowSizerAshTest, TestShowStateDefaults) {
836 // Creating a browser & window to play with.
837 scoped_ptr<TestingProfile> profile(new TestingProfile());
839 scoped_ptr<TestBrowserWindowAura> browser_window(CreateTestBrowserWindow(
840 CreateTestWindowInShellWithId(0),
841 gfx::Rect(16, 32, 640, 320),
842 Browser::CreateParams(Browser::TYPE_TABBED, profile.get(),
843 chrome::HOST_DESKTOP_TYPE_ASH)));
845 // Create also a popup browser since that behaves slightly different for
847 scoped_ptr<TestBrowserWindowAura> browser_popup(CreateTestBrowserWindow(
848 CreateTestWindowInShellWithId(1),
849 gfx::Rect(16, 32, 128, 256),
850 Browser::CreateParams(Browser::TYPE_POPUP, profile.get(),
851 chrome::HOST_DESKTOP_TYPE_ASH)));
853 // Check that a browser creation state always get used if not given as
854 // SHOW_STATE_DEFAULT. On Windows ASH it should be SHOW_STATE_MAXIMIZED.
855 ui::WindowShowState window_show_state =
856 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
857 ui::SHOW_STATE_MAXIMIZED,
859 browser_window->browser(),
863 EXPECT_EQ(window_show_state, ui::SHOW_STATE_MAXIMIZED);
865 EXPECT_EQ(window_show_state, ui::SHOW_STATE_DEFAULT);
868 browser_window->browser()->set_initial_show_state(ui::SHOW_STATE_MINIMIZED);
869 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
870 ui::SHOW_STATE_MAXIMIZED,
872 browser_window->browser(),
874 p1600x1200), ui::SHOW_STATE_MINIMIZED);
875 browser_window->browser()->set_initial_show_state(ui::SHOW_STATE_NORMAL);
876 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
877 ui::SHOW_STATE_MAXIMIZED,
879 browser_window->browser(),
881 p1600x1200), ui::SHOW_STATE_NORMAL);
882 browser_window->browser()->set_initial_show_state(ui::SHOW_STATE_MAXIMIZED);
883 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
884 ui::SHOW_STATE_NORMAL,
886 browser_window->browser(),
888 p1600x1200), ui::SHOW_STATE_MAXIMIZED);
890 // Check that setting the maximized command line option is forcing the
892 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kStartMaximized);
894 browser_window->browser()->set_initial_show_state(ui::SHOW_STATE_NORMAL);
895 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
896 ui::SHOW_STATE_NORMAL,
898 browser_window->browser(),
900 p1600x1200), ui::SHOW_STATE_MAXIMIZED);
902 // The popup should favor the initial show state over the command line.
903 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
904 ui::SHOW_STATE_NORMAL,
906 browser_popup->browser(),
908 p1600x1200), ui::SHOW_STATE_NORMAL);
911 TEST_F(WindowSizerAshTest, DefaultStateBecomesMaximized) {
912 // Create a browser to pass into the GetWindowBounds function.
913 scoped_ptr<TestingProfile> profile(new TestingProfile());
914 Browser::CreateParams native_params(profile.get(),
915 chrome::HOST_DESKTOP_TYPE_ASH);
916 scoped_ptr<Browser> browser(
917 chrome::CreateBrowserWithTestWindowForParams(&native_params));
919 gfx::Rect display_bounds = ash::Shell::GetInstance()->GetScreen()->
920 GetPrimaryDisplay().bounds();
921 gfx::Rect specified_bounds = display_bounds;
923 // Make a window bigger than the display work area.
924 specified_bounds.Inset(-20, -20);
925 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT;
927 WindowSizer::GetBrowserWindowBoundsAndShowState(
928 std::string(), specified_bounds, browser.get(), &bounds, &show_state);
929 // The window should start maximized with its restore bounds shrunken.
930 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, show_state);
931 EXPECT_NE(display_bounds.ToString(), bounds.ToString());
932 EXPECT_TRUE(display_bounds.Contains(bounds));
934 // Make a window smaller than the display work area.
935 specified_bounds.Inset(100, 100);
936 show_state = ui::SHOW_STATE_DEFAULT;
937 WindowSizer::GetBrowserWindowBoundsAndShowState(
938 std::string(), specified_bounds, browser.get(), &bounds, &show_state);
939 // The window should start in default state.
940 EXPECT_EQ(ui::SHOW_STATE_DEFAULT, show_state);
941 EXPECT_EQ(specified_bounds.ToString(), bounds.ToString());
944 // Test that the target root window is used as the destination of
945 // the non browser window. This differ from PersistedBoundsCase
946 // in that this uses real ash shell implementations + StateProvider
947 // TargetDisplayProvider, rather than mocks.
948 TEST_F(WindowSizerAshTest, DefaultBoundsInTargetDisplay) {
949 if (!SupportsMultipleDisplays() || !chrome::ShouldOpenAshOnStartup())
951 UpdateDisplay("500x500,600x600");
953 aura::Window* first_root =
954 ash::Shell::GetAllRootWindows()[0];
955 ash::ScopedTargetRootWindow tmp(first_root);
957 ui::WindowShowState show_state;
958 WindowSizer::GetBrowserWindowBoundsAndShowState(
964 EXPECT_TRUE(first_root->GetBoundsInScreen().Contains(bounds));
967 aura::Window* second_root =
968 ash::Shell::GetAllRootWindows()[1];
969 ash::ScopedTargetRootWindow tmp(second_root);
971 ui::WindowShowState show_state;
972 WindowSizer::GetBrowserWindowBoundsAndShowState(
978 EXPECT_TRUE(second_root->GetBoundsInScreen().Contains(bounds));
982 TEST_F(WindowSizerAshTest, TrustedPopupBehavior) {
983 scoped_ptr<TestingProfile> profile(new TestingProfile());
984 Browser::CreateParams trusted_popup_create_params(
985 Browser::TYPE_POPUP, profile.get(), chrome::HOST_DESKTOP_TYPE_ASH);
986 trusted_popup_create_params.trusted_source = true;
988 scoped_ptr<TestBrowserWindowAura> trusted_popup(CreateTestBrowserWindow(
989 CreateTestWindowInShellWithId(1),
990 gfx::Rect(16, 32, 640, 320),
991 trusted_popup_create_params));
992 // Trusted popup windows should follow the saved show state and ignore the
994 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
995 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
996 ui::SHOW_STATE_NORMAL,
998 trusted_popup->browser(),
1001 // A popup that is sized to occupy the whole work area has default state.
1002 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
1003 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
1004 ui::SHOW_STATE_NORMAL,
1006 trusted_popup->browser(),