Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / phantom_window_controller_unittest.cc
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.
4
5 #include "ash/wm/workspace/phantom_window_controller.h"
6
7 #include "ash/ash_switches.h"
8 #include "ash/shell.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"
15
16 namespace ash {
17 namespace internal {
18
19 namespace {
20
21 // Returns true if |window| is non-NULL and is visible.
22 bool IsVisible(aura::Window* window) {
23   return window && window->IsVisible();
24 }
25
26 // Observes |window|'s deletion.
27 class WindowDeletionObserver : public aura::WindowObserver {
28  public:
29   WindowDeletionObserver(aura::Window* window) : window_(window) {
30     window_->AddObserver(this);
31   }
32
33   virtual ~WindowDeletionObserver() {
34     if (window_)
35       window_->RemoveObserver(this);
36   }
37
38   // Returns true if the window has not been deleted yet.
39   bool IsWindowAlive() {
40     return !!window_;
41   }
42
43   // aura::WindowObserver:
44   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
45     window_->RemoveObserver(this);
46     window_ = NULL;
47    }
48
49  private:
50   aura::Window* window_;
51
52   DISALLOW_COPY_AND_ASSIGN(WindowDeletionObserver);
53 };
54
55 }  // namespace
56
57 class PhantomWindowControllerTest : public ash::test::AshTestBase {
58  public:
59   PhantomWindowControllerTest() {
60   }
61   virtual ~PhantomWindowControllerTest() {
62   }
63
64   // ash::test::AshTestBase:
65   virtual void SetUp() OVERRIDE {
66     ash::test::AshTestBase::SetUp();
67
68     window_ = CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 50, 60));
69     controller_.reset(new PhantomWindowController(window_));
70   }
71
72   void DeleteController() {
73     controller_.reset();
74   }
75
76   PhantomWindowController* controller() {
77     return controller_.get();
78   }
79
80   aura::Window* window() { return window_; }
81
82   aura::Window* phantom_window_in_target_root() {
83     return controller_->phantom_widget_in_target_root_ ?
84         controller_->phantom_widget_in_target_root_->GetNativeView() :
85         NULL;
86   }
87
88   aura::Window* phantom_window_in_start_root() {
89     return controller_->phantom_widget_in_start_root_ ?
90         controller_->phantom_widget_in_start_root_->GetNativeView() :
91         NULL;
92   }
93
94  private:
95   aura::Window* window_;
96   scoped_ptr<PhantomWindowController> controller_;
97
98   DISALLOW_COPY_AND_ASSIGN(PhantomWindowControllerTest);
99 };
100
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())
105     return;
106
107   CommandLine::ForCurrentProcess()->AppendSwitch(
108       switches::kAshDisableAlternateFrameCaptionButtonStyle);
109   ASSERT_FALSE(switches::UseAlternateFrameCaptionButtonStyle());
110
111   UpdateDisplay("500x400,500x400");
112
113   aura::Window::Windows root_windows = Shell::GetAllRootWindows();
114   EXPECT_EQ(root_windows[0], window()->GetRootWindow());
115
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());
121
122   // Move phantom preview into the right screen. Test that 2 windows got
123   // created.
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());
129
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());
135
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());
142
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());
148
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());
156
157   // Move phantom preview back into the left screen. Phantom windows should
158   // swap.
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());
164
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());
170   DeleteController();
171   EXPECT_FALSE(target_deletion_observer.IsWindowAlive());
172   EXPECT_FALSE(start_deletion_observer.IsWindowAlive());
173 }
174
175 }  // namespace internal
176 }  // namespace ash