Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / ash / wm / window_selector_unittest.cc
blob9ac15d688cbebd966111fe584bb563e9177fc6c5
1 // Copyright 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.
5 #include "ash/root_window_controller.h"
6 #include "ash/screen_ash.h"
7 #include "ash/shell.h"
8 #include "ash/test/ash_test_base.h"
9 #include "ash/test/shell_test_api.h"
10 #include "ash/wm/mru_window_tracker.h"
11 #include "ash/wm/window_selector_controller.h"
12 #include "ash/wm/window_util.h"
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/run_loop.h"
17 #include "ui/aura/client/aura_constants.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/test/event_generator.h"
20 #include "ui/aura/test/test_window_delegate.h"
21 #include "ui/aura/test/test_windows.h"
22 #include "ui/aura/window.h"
23 #include "ui/compositor/layer_animator.h"
24 #include "ui/gfx/rect_conversions.h"
25 #include "ui/gfx/transform.h"
27 namespace ash {
28 namespace internal {
30 namespace {
32 class LayerAnimationObserver : public ui::LayerAnimationObserver {
33 public:
34 LayerAnimationObserver(ui::Layer* layer)
35 : layer_(layer), animating_(false), message_loop_running_(false) {
36 layer_->GetAnimator()->AddObserver(this);
39 virtual ~LayerAnimationObserver() {
40 layer_->GetAnimator()->RemoveObserver(this);
43 virtual void OnLayerAnimationEnded(
44 ui::LayerAnimationSequence* sequence) OVERRIDE {
45 AnimationDone();
48 virtual void OnLayerAnimationScheduled(
49 ui::LayerAnimationSequence* sequence) OVERRIDE {
50 animating_ = true;
53 virtual void OnLayerAnimationAborted(
54 ui::LayerAnimationSequence* sequence) OVERRIDE {
55 AnimationDone();
58 void WaitUntilDone() {
59 while (animating_) {
60 message_loop_running_ = true;
61 base::MessageLoop::current()->Run();
62 message_loop_running_ = false;
66 private:
67 void AnimationDone() {
68 animating_ = false;
69 if (message_loop_running_)
70 base::MessageLoop::current()->Quit();
73 ui::Layer* layer_;
74 bool animating_;
75 bool message_loop_running_;
77 DISALLOW_COPY_AND_ASSIGN(LayerAnimationObserver);
80 } // namespace
82 class WindowSelectorTest : public test::AshTestBase {
83 public:
84 WindowSelectorTest() {}
85 virtual ~WindowSelectorTest() {}
87 aura::Window* CreateWindow(const gfx::Rect& bounds) {
88 return CreateTestWindowInShellWithDelegate(&wd, -1, bounds);
91 bool WindowsOverlapping(aura::Window* window1, aura::Window* window2) {
92 gfx::RectF window1_bounds = GetTransformedTargetBounds(window1);
93 gfx::RectF window2_bounds = GetTransformedTargetBounds(window2);
94 return window1_bounds.Intersects(window2_bounds);
97 void ToggleOverview() {
98 std::vector<aura::Window*> windows = ash::Shell::GetInstance()->
99 mru_window_tracker()->BuildMruWindowList();
100 ScopedVector<LayerAnimationObserver> animations;
101 for (size_t i = 0; i < windows.size(); ++i) {
102 animations.push_back(new LayerAnimationObserver(windows[i]->layer()));
104 ash::Shell::GetInstance()->window_selector_controller()->ToggleOverview();
105 for (size_t i = 0; i < animations.size(); ++i) {
106 animations[i]->WaitUntilDone();
110 void Cycle(WindowSelector::Direction direction) {
111 if (!IsSelecting()) {
112 std::vector<aura::Window*> windows = ash::Shell::GetInstance()->
113 mru_window_tracker()->BuildMruWindowList();
114 ScopedVector<LayerAnimationObserver> animations;
115 for (size_t i = 0; i < windows.size(); ++i)
116 animations.push_back(new LayerAnimationObserver(windows[i]->layer()));
117 ash::Shell::GetInstance()->window_selector_controller()->
118 HandleCycleWindow(direction);
119 for (size_t i = 0; i < animations.size(); ++i)
120 animations[i]->WaitUntilDone();
121 } else {
122 ash::Shell::GetInstance()->window_selector_controller()->
123 HandleCycleWindow(direction);
127 void StopCycling() {
128 ash::Shell::GetInstance()->window_selector_controller()->AltKeyReleased();
131 gfx::RectF GetTransformedBounds(aura::Window* window) {
132 gfx::RectF bounds(window->layer()->bounds());
133 window->layer()->transform().TransformRect(&bounds);
134 return bounds;
137 gfx::RectF GetTransformedTargetBounds(aura::Window* window) {
138 gfx::RectF bounds(window->layer()->GetTargetBounds());
139 window->layer()->GetTargetTransform().TransformRect(&bounds);
140 return bounds;
143 void ClickWindow(aura::Window* window) {
144 aura::test::EventGenerator event_generator(window->GetRootWindow(), window);
145 gfx::RectF target = GetTransformedBounds(window);
146 event_generator.ClickLeftButton();
149 bool IsSelecting() {
150 return ash::Shell::GetInstance()->window_selector_controller()->
151 IsSelecting();
154 private:
155 aura::test::TestWindowDelegate wd;
157 DISALLOW_COPY_AND_ASSIGN(WindowSelectorTest);
160 // Tests entering overview mode with two windows and selecting one.
161 TEST_F(WindowSelectorTest, Basic) {
162 gfx::Rect bounds(0, 0, 400, 400);
163 scoped_ptr<aura::Window> window1(CreateWindow(bounds));
164 scoped_ptr<aura::Window> window2(CreateWindow(bounds));
165 EXPECT_TRUE(WindowsOverlapping(window1.get(), window2.get()));
166 wm::ActivateWindow(window2.get());
167 EXPECT_FALSE(wm::IsActiveWindow(window1.get()));
168 EXPECT_TRUE(wm::IsActiveWindow(window2.get()));
170 // In overview mode the windows should no longer overlap.
171 ToggleOverview();
172 EXPECT_FALSE(WindowsOverlapping(window1.get(), window2.get()));
174 // Clicking window 1 should activate it.
175 ClickWindow(window1.get());
176 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
177 EXPECT_FALSE(wm::IsActiveWindow(window2.get()));
180 // Tests entering overview mode with three windows and cycling through them.
181 TEST_F(WindowSelectorTest, BasicCycle) {
182 gfx::Rect bounds(0, 0, 400, 400);
183 scoped_ptr<aura::Window> window1(CreateWindow(bounds));
184 scoped_ptr<aura::Window> window2(CreateWindow(bounds));
185 scoped_ptr<aura::Window> window3(CreateWindow(bounds));
186 wm::ActivateWindow(window3.get());
187 wm::ActivateWindow(window2.get());
188 wm::ActivateWindow(window1.get());
189 EXPECT_TRUE(wm::IsActiveWindow(window1.get()));
190 EXPECT_FALSE(wm::IsActiveWindow(window2.get()));
191 EXPECT_FALSE(wm::IsActiveWindow(window3.get()));
193 Cycle(WindowSelector::FORWARD);
194 EXPECT_TRUE(IsSelecting());
195 Cycle(WindowSelector::FORWARD);
196 StopCycling();
197 EXPECT_FALSE(IsSelecting());
198 EXPECT_FALSE(wm::IsActiveWindow(window1.get()));
199 EXPECT_FALSE(wm::IsActiveWindow(window2.get()));
200 EXPECT_TRUE(wm::IsActiveWindow(window3.get()));
203 // Tests that overview mode is exited if the last remaining window is destroyed.
204 TEST_F(WindowSelectorTest, LastWindowDestroyed) {
205 gfx::Rect bounds(0, 0, 400, 400);
206 scoped_ptr<aura::Window> window1(CreateWindow(bounds));
207 scoped_ptr<aura::Window> window2(CreateWindow(bounds));
208 ToggleOverview();
210 window1.reset();
211 window2.reset();
212 EXPECT_FALSE(IsSelecting());
215 // Tests that windows remain on the display they are currently on in overview
216 // mode.
217 TEST_F(WindowSelectorTest, MultipleDisplays) {
218 if (!SupportsMultipleDisplays())
219 return;
221 UpdateDisplay("400x400,400x400");
222 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
224 scoped_ptr<aura::Window> window1(CreateWindow(gfx::Rect(0, 0, 100, 100)));
225 scoped_ptr<aura::Window> window2(CreateWindow(gfx::Rect(0, 0, 100, 100)));
226 scoped_ptr<aura::Window> window3(CreateWindow(gfx::Rect(450, 0, 100, 100)));
227 scoped_ptr<aura::Window> window4(CreateWindow(gfx::Rect(450, 0, 100, 100)));
228 EXPECT_EQ(root_windows[0], window1->GetRootWindow());
229 EXPECT_EQ(root_windows[0], window2->GetRootWindow());
230 EXPECT_EQ(root_windows[1], window3->GetRootWindow());
231 EXPECT_EQ(root_windows[1], window4->GetRootWindow());
233 // In overview mode, each window remains in the same root window.
234 ToggleOverview();
235 EXPECT_EQ(root_windows[0], window1->GetRootWindow());
236 EXPECT_EQ(root_windows[0], window2->GetRootWindow());
237 EXPECT_EQ(root_windows[1], window3->GetRootWindow());
238 EXPECT_EQ(root_windows[1], window4->GetRootWindow());
239 root_windows[0]->bounds().Contains(
240 ToEnclosingRect(GetTransformedBounds(window1.get())));
241 root_windows[0]->bounds().Contains(
242 ToEnclosingRect(GetTransformedBounds(window2.get())));
243 root_windows[1]->bounds().Contains(
244 ToEnclosingRect(GetTransformedBounds(window3.get())));
245 root_windows[1]->bounds().Contains(
246 ToEnclosingRect(GetTransformedBounds(window4.get())));
249 } // namespace internal
250 } // namespace ash