1 // Copyright 2014 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/wm/workspace/phantom_window_controller.h"
7 #include "ash/ash_switches.h"
9 #include "ash/test/ash_test_base.h"
10 #include "base/command_line.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/aura/window_observer.h"
14 #include "ui/views/widget/widget.h"
21 // Returns true if |window| is non-NULL and is visible.
22 bool IsVisible(aura::Window* window) {
23 return window && window->IsVisible();
26 // Observes |window|'s deletion.
27 class WindowDeletionObserver : public aura::WindowObserver {
29 WindowDeletionObserver(aura::Window* window) : window_(window) {
30 window_->AddObserver(this);
33 virtual ~WindowDeletionObserver() {
35 window_->RemoveObserver(this);
38 // Returns true if the window has not been deleted yet.
39 bool IsWindowAlive() {
43 // aura::WindowObserver:
44 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
45 window_->RemoveObserver(this);
50 aura::Window* window_;
52 DISALLOW_COPY_AND_ASSIGN(WindowDeletionObserver);
57 class PhantomWindowControllerTest : public ash::test::AshTestBase {
59 PhantomWindowControllerTest() {
61 virtual ~PhantomWindowControllerTest() {
64 // ash::test::AshTestBase:
65 virtual void SetUp() OVERRIDE {
66 ash::test::AshTestBase::SetUp();
68 window_ = CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 50, 60));
69 controller_.reset(new PhantomWindowController(window_));
72 void DeleteController() {
76 PhantomWindowController* controller() {
77 return controller_.get();
80 aura::Window* window() { return window_; }
82 aura::Window* phantom_window_in_target_root() {
83 return controller_->phantom_widget_in_target_root_ ?
84 controller_->phantom_widget_in_target_root_->GetNativeView() :
88 aura::Window* phantom_window_in_start_root() {
89 return controller_->phantom_widget_in_start_root_ ?
90 controller_->phantom_widget_in_start_root_->GetNativeView() :
95 aura::Window* window_;
96 scoped_ptr<PhantomWindowController> controller_;
98 DISALLOW_COPY_AND_ASSIGN(PhantomWindowControllerTest);
101 // Test that two phantom windows are used when animating to bounds at least
102 // partially in another display when using the old caption button style.
103 TEST_F(PhantomWindowControllerTest, OldCaptionButtonStyle) {
104 if (!SupportsMultipleDisplays())
107 CommandLine::ForCurrentProcess()->AppendSwitch(
108 switches::kAshDisableAlternateFrameCaptionButtonStyle);
109 ASSERT_FALSE(switches::UseAlternateFrameCaptionButtonStyle());
111 UpdateDisplay("500x400,500x400");
113 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
114 EXPECT_EQ(root_windows[0], window()->GetRootWindow());
116 // Phantom preview only in the left screen.
117 controller()->Show(gfx::Rect(100, 100, 50, 60));
118 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
119 EXPECT_FALSE(IsVisible(phantom_window_in_start_root()));
120 EXPECT_EQ(root_windows[0], phantom_window_in_target_root()->GetRootWindow());
122 // Move phantom preview into the right screen. Test that 2 windows got
124 controller()->Show(gfx::Rect(600, 100, 50, 60));
125 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
126 EXPECT_TRUE(IsVisible(phantom_window_in_start_root()));
127 EXPECT_EQ(root_windows[1], phantom_window_in_target_root()->GetRootWindow());
128 EXPECT_EQ(root_windows[0], phantom_window_in_start_root()->GetRootWindow());
130 // Move phantom preview only in the right screen. Start window should close.
131 controller()->Show(gfx::Rect(700, 100, 50, 60));
132 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
133 EXPECT_FALSE(IsVisible(phantom_window_in_start_root()));
134 EXPECT_EQ(root_windows[1], phantom_window_in_target_root()->GetRootWindow());
136 // Move phantom preview into the left screen. Start window should open.
137 controller()->Show(gfx::Rect(100, 100, 50, 60));
138 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
139 EXPECT_TRUE(IsVisible(phantom_window_in_start_root()));
140 EXPECT_EQ(root_windows[0], phantom_window_in_target_root()->GetRootWindow());
141 EXPECT_EQ(root_windows[1], phantom_window_in_start_root()->GetRootWindow());
143 // Move phantom preview while in the left screen. Start window should close.
144 controller()->Show(gfx::Rect(200, 100, 50, 60));
145 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
146 EXPECT_FALSE(IsVisible(phantom_window_in_start_root()));
147 EXPECT_EQ(root_windows[0], phantom_window_in_target_root()->GetRootWindow());
149 // Move phantom preview spanning both screens with most of the preview in the
150 // right screen. Two windows are created.
151 controller()->Show(gfx::Rect(495, 100, 50, 60));
152 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
153 EXPECT_TRUE(IsVisible(phantom_window_in_start_root()));
154 EXPECT_EQ(root_windows[1], phantom_window_in_target_root()->GetRootWindow());
155 EXPECT_EQ(root_windows[0], phantom_window_in_start_root()->GetRootWindow());
157 // Move phantom preview back into the left screen. Phantom windows should
159 controller()->Show(gfx::Rect(200, 100, 50, 60));
160 EXPECT_TRUE(IsVisible(phantom_window_in_target_root()));
161 EXPECT_TRUE(IsVisible(phantom_window_in_start_root()));
162 EXPECT_EQ(root_windows[0], phantom_window_in_target_root()->GetRootWindow());
163 EXPECT_EQ(root_windows[1], phantom_window_in_start_root()->GetRootWindow());
165 // Destroy phantom controller. Both windows should close.
166 WindowDeletionObserver target_deletion_observer(
167 phantom_window_in_target_root());
168 WindowDeletionObserver start_deletion_observer(
169 phantom_window_in_start_root());
171 EXPECT_FALSE(target_deletion_observer.IsWindowAlive());
172 EXPECT_FALSE(start_deletion_observer.IsWindowAlive());
175 } // namespace internal