Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / window_sizer / window_sizer_ash_unittest.cc
blobbcfdd4fd0db929d3f96e1165b6141a43fb8c77a0
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"
7 #include "ash/shell.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;
30 namespace {
32 // A browser window proxy which is able to associate an aura native window with
33 // it.
34 class TestBrowserWindowAura : public TestBrowserWindow {
35 public:
36 // |native_window| will still be owned by the caller after the constructor
37 // was called.
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();
46 Activate();
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);
70 private:
71 scoped_ptr<Browser> browser_;
72 scoped_ptr<aura::Window> native_window_;
74 DISALLOW_COPY_AND_ASSIGN(TestBrowserWindowAura);
77 scoped_ptr<TestBrowserWindowAura> CreateTestBrowserWindow(
78 aura::Window* window,
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();
89 } // namespace
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
95 #else
96 #define MAYBE_DefaultSizeCase DefaultSizeCase
97 #endif
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) {
102 #if defined(OS_WIN)
103 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kOpenAsh);
104 #endif
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),
113 window_bounds);
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(),
120 &window_bounds);
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),
126 window_bounds);
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(),
133 &window_bounds);
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),
139 window_bounds);
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(),
146 &window_bounds);
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),
154 window_bounds);
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(),
161 &window_bounds);
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),
168 window_bounds);
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),
179 window_bounds);
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),
190 window_bounds);
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),
201 window_bounds);
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),
212 window_bounds);
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;
221 GetWindowBounds(
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(),
226 &window_bounds);
227 EXPECT_EQ(
228 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
229 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
230 500, 400).ToString(),
231 window_bounds.ToString());
234 { // taskbar on top.
235 gfx::Rect window_bounds;
236 GetWindowBounds(
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(),
241 &window_bounds);
242 EXPECT_EQ(
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;
253 GetWindowBounds(
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(),
258 &window_bounds);
259 EXPECT_EQ(
260 gfx::Rect(kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
261 kWindowTilePixels + ash::WindowPositioner::kDesktopBorderSize,
262 30 /* not 29 */,
263 30 /* not 29 */).ToString(),
264 window_bounds.ToString());
268 { // Normal.
269 gfx::Rect window_bounds;
270 GetWindowBounds(
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(),
275 &window_bounds);
276 EXPECT_EQ(
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());
297 { // Normal.
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(),
312 &window_bounds);
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(),
322 &window_bounds);
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
339 // off-screen.
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;
364 GetWindowBounds(
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
380 // other platforms.
382 TEST_F(WindowSizerAshTest, LastWindowOffscreenWithNonAggressiveRepositioning) {
383 { // taskbar on left.
384 gfx::Rect window_bounds;
385 GetWindowBounds(
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(),
390 &window_bounds);
391 EXPECT_EQ(
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,
415 738 /* not 739 */,
416 500,
417 400).ToString(),
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,
439 500,
440 400).ToString(),
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 */,
451 738 /* not 739 */,
452 500,
453 400).ToString(),
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
462 #else
463 #define MAYBE_PlaceNewWindows PlaceNewWindows
464 #endif
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
471 // existing windows.
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
478 // existing windows.
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.
523 EXPECT_EQ(
524 gfx::Rect(
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
539 #else
540 #define MAYBE_PlaceNewBrowserWindowOnEmptyDesktop PlaceNewBrowserWindowOnEmptyDesktop
541 #endif
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
555 // desirable.
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.
573 &window_bounds,
574 &out_show_state1);
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.
590 &window_bounds,
591 &out_show_state2);
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.
608 &window_bounds,
609 &out_show_state3);
610 #if defined(OS_WIN)
611 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, out_show_state3);
612 #else
613 EXPECT_EQ(ui::SHOW_STATE_DEFAULT, out_show_state3);
614 #endif
617 #if defined(OS_CHROMEOS)
618 #define MAYBE_PlaceNewWindowsOnMultipleDisplays PlaceNewWindowsOnMultipleDisplays
619 #else
620 // No multiple displays on windows ash.
621 #define MAYBE_PlaceNewWindowsOnMultipleDisplays DISABLED_PlaceNewWindowsOnMultipleDisplays
622 #endif
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),
654 gfx::Rect(),
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),
682 second_display);
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
720 #else
721 #define MAYBE_TestShowState TestShowState
722 #endif
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
743 // top window.
744 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
745 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
746 ui::SHOW_STATE_NORMAL,
747 BOTH,
748 browser_window->browser(),
749 p1600x1200,
750 p1600x1200));
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,
755 BOTH,
756 browser_window->browser(),
757 p1280x1024,
758 p1600x1200));
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,
763 BOTH,
764 browser_window->browser(),
765 p1600x1200,
766 p1600x1200));
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,
771 BOTH,
772 browser_popup->browser(),
773 p1600x1200,
774 p1600x1200));
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,
779 BOTH,
780 browser_popup->browser(),
781 p1600x1200,
782 p1600x1200));
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,
797 BOTH,
798 browser_window->browser(),
799 p1600x1200,
800 p1600x1200));
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,
805 BOTH,
806 browser_popup->browser(),
807 p1600x1200,
808 p1600x1200));
810 // In smaller screen resolutions we default to maximized if there is no other
811 // window visible.
812 int min_size = ash::WindowPositioner::GetForceMaximizedWidthLimit() / 2;
813 if (min_size > 0) {
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,
818 BOTH,
819 browser_window->browser(),
820 tiny_screen,
821 tiny_screen));
822 browser_window->Hide();
823 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED,
824 GetWindowShowState(ui::SHOW_STATE_MAXIMIZED,
825 ui::SHOW_STATE_DEFAULT,
826 BOTH,
827 browser_window2->browser(),
828 tiny_screen,
829 tiny_screen));
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
846 // defaults.
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,
858 DEFAULT,
859 browser_window->browser(),
860 p1600x1200,
861 p1600x1200);
862 #if defined(OS_WIN)
863 EXPECT_EQ(window_show_state, ui::SHOW_STATE_MAXIMIZED);
864 #else
865 EXPECT_EQ(window_show_state, ui::SHOW_STATE_DEFAULT);
866 #endif
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,
871 BOTH,
872 browser_window->browser(),
873 p1600x1200,
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,
878 BOTH,
879 browser_window->browser(),
880 p1600x1200,
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,
885 BOTH,
886 browser_window->browser(),
887 p1600x1200,
888 p1600x1200), ui::SHOW_STATE_MAXIMIZED);
890 // Check that setting the maximized command line option is forcing the
891 // maximized state.
892 base::CommandLine::ForCurrentProcess()->AppendSwitch(
893 switches::kStartMaximized);
895 browser_window->browser()->set_initial_show_state(ui::SHOW_STATE_NORMAL);
896 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
897 ui::SHOW_STATE_NORMAL,
898 BOTH,
899 browser_window->browser(),
900 p1600x1200,
901 p1600x1200), ui::SHOW_STATE_MAXIMIZED);
903 // The popup should favor the initial show state over the command line.
904 EXPECT_EQ(GetWindowShowState(ui::SHOW_STATE_NORMAL,
905 ui::SHOW_STATE_NORMAL,
906 BOTH,
907 browser_popup->browser(),
908 p1600x1200,
909 p1600x1200), ui::SHOW_STATE_NORMAL);
912 TEST_F(WindowSizerAshTest, DefaultStateBecomesMaximized) {
913 // Create a browser to pass into the GetWindowBounds function.
914 scoped_ptr<TestingProfile> profile(new TestingProfile());
915 Browser::CreateParams native_params(profile.get(),
916 chrome::HOST_DESKTOP_TYPE_ASH);
917 scoped_ptr<Browser> browser(
918 chrome::CreateBrowserWithTestWindowForParams(&native_params));
920 gfx::Rect display_bounds = ash::Shell::GetInstance()->GetScreen()->
921 GetPrimaryDisplay().bounds();
922 gfx::Rect specified_bounds = display_bounds;
924 // Make a window bigger than the display work area.
925 specified_bounds.Inset(-20, -20);
926 ui::WindowShowState show_state = ui::SHOW_STATE_DEFAULT;
927 gfx::Rect bounds;
928 WindowSizer::GetBrowserWindowBoundsAndShowState(
929 std::string(), specified_bounds, browser.get(), &bounds, &show_state);
930 // The window should start maximized with its restore bounds shrunken.
931 EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, show_state);
932 EXPECT_NE(display_bounds.ToString(), bounds.ToString());
933 EXPECT_TRUE(display_bounds.Contains(bounds));
935 // Make a window smaller than the display work area.
936 specified_bounds.Inset(100, 100);
937 show_state = ui::SHOW_STATE_DEFAULT;
938 WindowSizer::GetBrowserWindowBoundsAndShowState(
939 std::string(), specified_bounds, browser.get(), &bounds, &show_state);
940 // The window should start in default state.
941 EXPECT_EQ(ui::SHOW_STATE_DEFAULT, show_state);
942 EXPECT_EQ(specified_bounds.ToString(), bounds.ToString());
945 // Test that the target root window is used as the destination of
946 // the non browser window. This differ from PersistedBoundsCase
947 // in that this uses real ash shell implementations + StateProvider
948 // TargetDisplayProvider, rather than mocks.
949 TEST_F(WindowSizerAshTest, DefaultBoundsInTargetDisplay) {
950 if (!SupportsMultipleDisplays() || !chrome::ShouldOpenAshOnStartup())
951 return;
952 UpdateDisplay("500x500,600x600");
954 aura::Window* first_root =
955 ash::Shell::GetAllRootWindows()[0];
956 ash::ScopedTargetRootWindow tmp(first_root);
957 gfx::Rect bounds;
958 ui::WindowShowState show_state;
959 WindowSizer::GetBrowserWindowBoundsAndShowState(
960 std::string(),
961 gfx::Rect(),
962 NULL,
963 &bounds,
964 &show_state);
965 EXPECT_TRUE(first_root->GetBoundsInScreen().Contains(bounds));
968 aura::Window* second_root =
969 ash::Shell::GetAllRootWindows()[1];
970 ash::ScopedTargetRootWindow tmp(second_root);
971 gfx::Rect bounds;
972 ui::WindowShowState show_state;
973 WindowSizer::GetBrowserWindowBoundsAndShowState(
974 std::string(),
975 gfx::Rect(),
976 NULL,
977 &bounds,
978 &show_state);
979 EXPECT_TRUE(second_root->GetBoundsInScreen().Contains(bounds));
983 TEST_F(WindowSizerAshTest, TrustedPopupBehavior) {
984 scoped_ptr<TestingProfile> profile(new TestingProfile());
985 Browser::CreateParams trusted_popup_create_params(
986 Browser::TYPE_POPUP, profile.get(), chrome::HOST_DESKTOP_TYPE_ASH);
987 trusted_popup_create_params.trusted_source = true;
989 scoped_ptr<TestBrowserWindowAura> trusted_popup(CreateTestBrowserWindow(
990 CreateTestWindowInShellWithId(1),
991 gfx::Rect(16, 32, 640, 320),
992 trusted_popup_create_params));
993 // Trusted popup windows should follow the saved show state and ignore the
994 // last show state.
995 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
996 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
997 ui::SHOW_STATE_NORMAL,
998 BOTH,
999 trusted_popup->browser(),
1000 p1280x1024,
1001 p1600x1200));
1002 // A popup that is sized to occupy the whole work area has default state.
1003 EXPECT_EQ(ui::SHOW_STATE_DEFAULT,
1004 GetWindowShowState(ui::SHOW_STATE_DEFAULT,
1005 ui::SHOW_STATE_NORMAL,
1006 BOTH,
1007 trusted_popup->browser(),
1008 p1600x1200,
1009 p1600x1200));