blob: 9c10cf1d0f272f45e0e2ca396fa665b62a7a051f [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// Copyright (c) 2013 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.
4
5#include "ash/wm/window_util.h"
6
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00007#include "ash/screen_util.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01008#include "ash/test/ash_test_base.h"
Ben Murdocheffb81e2014-03-31 11:51:25 +01009#include "ash/wm/window_state.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010010#include "ui/aura/window.h"
11
12namespace ash {
13
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000014namespace {
15
16std::string GetAdjustedBounds(const gfx::Rect& visible,
17 gfx::Rect to_be_adjusted) {
18 wm::AdjustBoundsToEnsureMinimumWindowVisibility(visible, &to_be_adjusted);
19 return to_be_adjusted.ToString();
20}
21
22}
23
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010024typedef test::AshTestBase WindowUtilTest;
25
26TEST_F(WindowUtilTest, CenterWindow) {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010027 if (!SupportsMultipleDisplays())
28 return;
29
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010030 UpdateDisplay("500x400, 600x400");
31 scoped_ptr<aura::Window> window(
32 CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 100, 100)));
Ben Murdocheffb81e2014-03-31 11:51:25 +010033
34 wm::WindowState* window_state = wm::GetWindowState(window.get());
35 EXPECT_FALSE(window_state->bounds_changed_by_user());
36
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010037 wm::CenterWindow(window.get());
Ben Murdocheffb81e2014-03-31 11:51:25 +010038 // Centring window is considered as a user's action.
39 EXPECT_TRUE(window_state->bounds_changed_by_user());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010040 EXPECT_EQ("200,126 100x100", window->bounds().ToString());
41 EXPECT_EQ("200,126 100x100", window->GetBoundsInScreen().ToString());
42 window->SetBoundsInScreen(gfx::Rect(600, 0, 100, 100),
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000043 ScreenUtil::GetSecondaryDisplay());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010044 wm::CenterWindow(window.get());
45 EXPECT_EQ("250,126 100x100", window->bounds().ToString());
46 EXPECT_EQ("750,126 100x100", window->GetBoundsInScreen().ToString());
47}
48
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000049TEST_F(WindowUtilTest, AdjustBoundsToEnsureMinimumVisibility) {
50 const gfx::Rect visible_bounds(0, 0, 100, 100);
51
52 EXPECT_EQ("0,0 90x90",
53 GetAdjustedBounds(visible_bounds, gfx::Rect(0, 0, 90, 90)));
54 EXPECT_EQ("0,0 100x100",
55 GetAdjustedBounds(visible_bounds, gfx::Rect(0, 0, 150, 150)));
56 EXPECT_EQ("-50,0 100x100",
57 GetAdjustedBounds(visible_bounds, gfx::Rect(-50, -50, 150, 150)));
58 EXPECT_EQ("-90,10 100x100",
59 GetAdjustedBounds(visible_bounds, gfx::Rect(-100, 10, 150, 150)));
60 EXPECT_EQ("90,90 100x100",
61 GetAdjustedBounds(visible_bounds, gfx::Rect(100, 100, 150, 150)));
62
63 const gfx::Rect visible_bounds_right(200, 50, 100, 100);
64
65 EXPECT_EQ(
66 "210,60 90x90",
67 GetAdjustedBounds(visible_bounds_right, gfx::Rect(210, 60, 90, 90)));
68 EXPECT_EQ(
69 "210,60 100x100",
70 GetAdjustedBounds(visible_bounds_right, gfx::Rect(210, 60, 150, 150)));
71 EXPECT_EQ(
72 "110,50 100x100",
73 GetAdjustedBounds(visible_bounds_right, gfx::Rect(0, 0, 150, 150)));
74 EXPECT_EQ(
75 "290,50 100x100",
76 GetAdjustedBounds(visible_bounds_right, gfx::Rect(300, 20, 150, 150)));
77 EXPECT_EQ(
78 "110,140 100x100",
79 GetAdjustedBounds(visible_bounds_right, gfx::Rect(-100, 150, 150, 150)));
80
81 const gfx::Rect visible_bounds_left(-200, -50, 100, 100);
82 EXPECT_EQ(
83 "-190,-40 90x90",
84 GetAdjustedBounds(visible_bounds_left, gfx::Rect(-190, -40, 90, 90)));
85 EXPECT_EQ(
86 "-190,-40 100x100",
87 GetAdjustedBounds(visible_bounds_left, gfx::Rect(-190, -40, 150, 150)));
88 EXPECT_EQ(
89 "-250,-40 100x100",
90 GetAdjustedBounds(visible_bounds_left, gfx::Rect(-250, -40, 150, 150)));
91 EXPECT_EQ(
92 "-290,-50 100x100",
93 GetAdjustedBounds(visible_bounds_left, gfx::Rect(-400, -60, 150, 150)));
94 EXPECT_EQ(
95 "-110,0 100x100",
96 GetAdjustedBounds(visible_bounds_left, gfx::Rect(0, 0, 150, 150)));
97}
98
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010099} // namespace ash