OLD | NEW |
(Empty) | |
| 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/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/window_cycle_controller.h" |
| 11 #include "ash/wm/window_util.h" |
| 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 15 #include "base/run_loop.h" |
| 16 #include "ui/aura/client/aura_constants.h" |
| 17 #include "ui/aura/root_window.h" |
| 18 #include "ui/aura/test/event_generator.h" |
| 19 #include "ui/aura/test/test_window_delegate.h" |
| 20 #include "ui/aura/test/test_windows.h" |
| 21 #include "ui/aura/window.h" |
| 22 #include "ui/compositor/layer_animator.h" |
| 23 #include "ui/gfx/rect_conversions.h" |
| 24 #include "ui/gfx/transform.h" |
| 25 |
| 26 namespace ash { |
| 27 namespace internal { |
| 28 |
| 29 namespace { |
| 30 |
| 31 class LayerAnimationObserver : public ui::LayerAnimationObserver { |
| 32 public: |
| 33 LayerAnimationObserver(ui::Layer* layer) |
| 34 : layer_(layer), animating_(false), message_loop_running_(false) { |
| 35 layer_->GetAnimator()->AddObserver(this); |
| 36 } |
| 37 |
| 38 virtual ~LayerAnimationObserver() { |
| 39 layer_->GetAnimator()->RemoveObserver(this); |
| 40 } |
| 41 |
| 42 virtual void OnLayerAnimationEnded( |
| 43 ui::LayerAnimationSequence* sequence) OVERRIDE { |
| 44 AnimationDone(); |
| 45 } |
| 46 |
| 47 virtual void OnLayerAnimationScheduled( |
| 48 ui::LayerAnimationSequence* sequence) OVERRIDE { |
| 49 animating_ = true; |
| 50 } |
| 51 |
| 52 virtual void OnLayerAnimationAborted( |
| 53 ui::LayerAnimationSequence* sequence) OVERRIDE { |
| 54 AnimationDone(); |
| 55 } |
| 56 |
| 57 void WaitUntilDone() { |
| 58 while (animating_) { |
| 59 message_loop_running_ = true; |
| 60 base::MessageLoop::current()->Run(); |
| 61 message_loop_running_ = false; |
| 62 } |
| 63 } |
| 64 |
| 65 private: |
| 66 void AnimationDone() { |
| 67 animating_ = false; |
| 68 if (message_loop_running_) |
| 69 base::MessageLoop::current()->Quit(); |
| 70 } |
| 71 |
| 72 ui::Layer* layer_; |
| 73 bool animating_; |
| 74 bool message_loop_running_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(LayerAnimationObserver); |
| 77 }; |
| 78 |
| 79 } // namespace |
| 80 |
| 81 class WindowSelectorTest : public test::AshTestBase { |
| 82 public: |
| 83 WindowSelectorTest() {} |
| 84 virtual ~WindowSelectorTest() {} |
| 85 |
| 86 aura::Window* CreateWindow(const gfx::Rect& bounds) { |
| 87 return CreateTestWindowInShellWithDelegate(&wd, -1, bounds); |
| 88 } |
| 89 |
| 90 bool WindowsOverlapping(aura::Window* window1, aura::Window* window2) { |
| 91 gfx::RectF window1_bounds = GetTransformedTargetBounds(window1); |
| 92 gfx::RectF window2_bounds = GetTransformedTargetBounds(window2); |
| 93 return window1_bounds.Intersects(window2_bounds); |
| 94 } |
| 95 |
| 96 void ToggleOverview() { |
| 97 std::vector<aura::Window*> windows = |
| 98 WindowCycleController::BuildWindowList(NULL, false); |
| 99 ScopedVector<LayerAnimationObserver> animations; |
| 100 for (size_t i = 0; i < windows.size(); ++i) { |
| 101 animations.push_back(new LayerAnimationObserver(windows[i]->layer())); |
| 102 } |
| 103 ash::Shell::GetInstance()->window_cycle_controller()->ToggleOverview(); |
| 104 for (size_t i = 0; i < animations.size(); ++i) { |
| 105 animations[i]->WaitUntilDone(); |
| 106 } |
| 107 } |
| 108 |
| 109 gfx::RectF GetTransformedBounds(aura::Window* window) { |
| 110 gfx::RectF bounds(window->layer()->bounds()); |
| 111 window->layer()->transform().TransformRect(&bounds); |
| 112 return bounds; |
| 113 } |
| 114 |
| 115 gfx::RectF GetTransformedTargetBounds(aura::Window* window) { |
| 116 gfx::RectF bounds(window->layer()->GetTargetBounds()); |
| 117 window->layer()->GetTargetTransform().TransformRect(&bounds); |
| 118 return bounds; |
| 119 } |
| 120 |
| 121 void ClickWindow(aura::Window* window) { |
| 122 aura::test::EventGenerator event_generator(window->GetRootWindow(), window); |
| 123 gfx::RectF target = GetTransformedBounds(window); |
| 124 event_generator.ClickLeftButton(); |
| 125 } |
| 126 |
| 127 private: |
| 128 aura::test::TestWindowDelegate wd; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(WindowSelectorTest); |
| 131 }; |
| 132 |
| 133 // Tests entering overview mode with two windows and selecting one. |
| 134 TEST_F(WindowSelectorTest, Basic) { |
| 135 gfx::Rect bounds(0, 0, 400, 400); |
| 136 scoped_ptr<aura::Window> window1(CreateWindow(bounds)); |
| 137 scoped_ptr<aura::Window> window2(CreateWindow(bounds)); |
| 138 EXPECT_TRUE(WindowsOverlapping(window1.get(), window2.get())); |
| 139 wm::ActivateWindow(window2.get()); |
| 140 EXPECT_FALSE(wm::IsActiveWindow(window1.get())); |
| 141 EXPECT_TRUE(wm::IsActiveWindow(window2.get())); |
| 142 |
| 143 // In overview mode the windows should no longer overlap. |
| 144 ToggleOverview(); |
| 145 EXPECT_FALSE(WindowsOverlapping(window1.get(), window2.get())); |
| 146 |
| 147 // Clicking window 1 should activate it. |
| 148 ClickWindow(window1.get()); |
| 149 EXPECT_TRUE(wm::IsActiveWindow(window1.get())); |
| 150 EXPECT_FALSE(wm::IsActiveWindow(window2.get())); |
| 151 } |
| 152 |
| 153 // Tests that windows remain on the display they are currently on in overview |
| 154 // mode. |
| 155 TEST_F(WindowSelectorTest, MultipleDisplays) { |
| 156 if (!SupportsMultipleDisplays()) |
| 157 return; |
| 158 |
| 159 UpdateDisplay("400x400,400x400"); |
| 160 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| 161 |
| 162 scoped_ptr<aura::Window> window1(CreateWindow(gfx::Rect(0, 0, 100, 100))); |
| 163 scoped_ptr<aura::Window> window2(CreateWindow(gfx::Rect(0, 0, 100, 100))); |
| 164 scoped_ptr<aura::Window> window3(CreateWindow(gfx::Rect(450, 0, 100, 100))); |
| 165 scoped_ptr<aura::Window> window4(CreateWindow(gfx::Rect(450, 0, 100, 100))); |
| 166 EXPECT_EQ(root_windows[0], window1->GetRootWindow()); |
| 167 EXPECT_EQ(root_windows[0], window2->GetRootWindow()); |
| 168 EXPECT_EQ(root_windows[1], window3->GetRootWindow()); |
| 169 EXPECT_EQ(root_windows[1], window4->GetRootWindow()); |
| 170 |
| 171 // In overview mode, each window remains in the same root window. |
| 172 ToggleOverview(); |
| 173 EXPECT_EQ(root_windows[0], window1->GetRootWindow()); |
| 174 EXPECT_EQ(root_windows[0], window2->GetRootWindow()); |
| 175 EXPECT_EQ(root_windows[1], window3->GetRootWindow()); |
| 176 EXPECT_EQ(root_windows[1], window4->GetRootWindow()); |
| 177 root_windows[0]->bounds().Contains( |
| 178 ToEnclosingRect(GetTransformedBounds(window1.get()))); |
| 179 root_windows[0]->bounds().Contains( |
| 180 ToEnclosingRect(GetTransformedBounds(window2.get()))); |
| 181 root_windows[1]->bounds().Contains( |
| 182 ToEnclosingRect(GetTransformedBounds(window3.get()))); |
| 183 root_windows[1]->bounds().Contains( |
| 184 ToEnclosingRect(GetTransformedBounds(window4.get()))); |
| 185 } |
| 186 |
| 187 } // namespace internal |
| 188 } // namespace ash |
OLD | NEW |