window_util_unittest.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
1// 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
7#include "ash/screen_util.h"
8#include "ash/test/ash_test_base.h"
9#include "ash/wm/window_state.h"
10#include "ui/aura/window.h"
11
12namespace ash {
13
14namespace {
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
24typedef test::AshTestBase WindowUtilTest;
25
26TEST_F(WindowUtilTest, CenterWindow) {
27  if (!SupportsMultipleDisplays())
28    return;
29
30  UpdateDisplay("500x400, 600x400");
31  scoped_ptr<aura::Window> window(
32      CreateTestWindowInShellWithBounds(gfx::Rect(12, 20, 100, 100)));
33
34  wm::WindowState* window_state = wm::GetWindowState(window.get());
35  EXPECT_FALSE(window_state->bounds_changed_by_user());
36
37  wm::CenterWindow(window.get());
38  // Centring window is considered as a user's action.
39  EXPECT_TRUE(window_state->bounds_changed_by_user());
40  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),
43                            ScreenUtil::GetSecondaryDisplay());
44  wm::CenterWindow(window.get());
45  EXPECT_EQ("250,126 100x100", window->bounds().ToString());
46  EXPECT_EQ("750,126 100x100", window->GetBoundsInScreen().ToString());
47}
48
49TEST_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
99}  // namespace ash
100