OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "ui/views/corewm/shadow_controller.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "ui/aura/client/activation_client.h" | |
12 #include "ui/aura/client/window_tree_client.h" | |
13 #include "ui/aura/test/aura_test_base.h" | |
14 #include "ui/aura/window.h" | |
15 #include "ui/aura/window_event_dispatcher.h" | |
16 #include "ui/compositor/layer.h" | |
17 #include "ui/views/corewm/shadow.h" | |
18 #include "ui/views/corewm/shadow_types.h" | |
19 #include "ui/views/corewm/window_util.h" | |
20 #include "ui/views/corewm/wm_state.h" | |
21 | |
22 namespace views { | |
23 namespace corewm { | |
24 | |
25 class ShadowControllerTest : public aura::test::AuraTestBase { | |
26 public: | |
27 ShadowControllerTest() {} | |
28 virtual ~ShadowControllerTest() {} | |
29 | |
30 virtual void SetUp() OVERRIDE { | |
31 wm_state_.reset(new views::corewm::WMState); | |
32 AuraTestBase::SetUp(); | |
33 aura::client::ActivationClient* activation_client = | |
34 aura::client::GetActivationClient(root_window()); | |
35 shadow_controller_.reset(new ShadowController(activation_client)); | |
36 } | |
37 virtual void TearDown() OVERRIDE { | |
38 shadow_controller_.reset(); | |
39 AuraTestBase::TearDown(); | |
40 wm_state_.reset(); | |
41 } | |
42 | |
43 protected: | |
44 ShadowController* shadow_controller() { return shadow_controller_.get(); } | |
45 | |
46 void ActivateWindow(aura::Window* window) { | |
47 DCHECK(window); | |
48 DCHECK(window->GetRootWindow()); | |
49 aura::client::GetActivationClient(window->GetRootWindow())->ActivateWindow( | |
50 window); | |
51 } | |
52 | |
53 private: | |
54 scoped_ptr<ShadowController> shadow_controller_; | |
55 scoped_ptr<views::corewm::WMState> wm_state_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(ShadowControllerTest); | |
58 }; | |
59 | |
60 // Tests that various methods in Window update the Shadow object as expected. | |
61 TEST_F(ShadowControllerTest, Shadow) { | |
62 scoped_ptr<aura::Window> window(new aura::Window(NULL)); | |
63 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
64 window->Init(aura::WINDOW_LAYER_TEXTURED); | |
65 ParentWindow(window.get()); | |
66 | |
67 // We should create the shadow before the window is visible (the shadow's | |
68 // layer won't get drawn yet since it's a child of the window's layer). | |
69 ShadowController::TestApi api(shadow_controller()); | |
70 const Shadow* shadow = api.GetShadowForWindow(window.get()); | |
71 ASSERT_TRUE(shadow != NULL); | |
72 EXPECT_TRUE(shadow->layer()->visible()); | |
73 | |
74 // The shadow should remain visible after window visibility changes. | |
75 window->Show(); | |
76 EXPECT_TRUE(shadow->layer()->visible()); | |
77 window->Hide(); | |
78 EXPECT_TRUE(shadow->layer()->visible()); | |
79 | |
80 // If the shadow is disabled, it should be hidden. | |
81 SetShadowType(window.get(), SHADOW_TYPE_NONE); | |
82 window->Show(); | |
83 EXPECT_FALSE(shadow->layer()->visible()); | |
84 SetShadowType(window.get(), SHADOW_TYPE_RECTANGULAR); | |
85 EXPECT_TRUE(shadow->layer()->visible()); | |
86 | |
87 // The shadow's layer should be a child of the window's layer. | |
88 EXPECT_EQ(window->layer(), shadow->layer()->parent()); | |
89 | |
90 window->parent()->RemoveChild(window.get()); | |
91 aura::Window* window_ptr = window.get(); | |
92 window.reset(); | |
93 EXPECT_TRUE(api.GetShadowForWindow(window_ptr) == NULL); | |
94 } | |
95 | |
96 // Tests that the window's shadow's bounds are updated correctly. | |
97 TEST_F(ShadowControllerTest, ShadowBounds) { | |
98 scoped_ptr<aura::Window> window(new aura::Window(NULL)); | |
99 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
100 window->Init(aura::WINDOW_LAYER_TEXTURED); | |
101 ParentWindow(window.get()); | |
102 window->Show(); | |
103 | |
104 const gfx::Rect kOldBounds(20, 30, 400, 300); | |
105 window->SetBounds(kOldBounds); | |
106 | |
107 // When the shadow is first created, it should use the window's size (but | |
108 // remain at the origin, since it's a child of the window's layer). | |
109 SetShadowType(window.get(), SHADOW_TYPE_RECTANGULAR); | |
110 ShadowController::TestApi api(shadow_controller()); | |
111 const Shadow* shadow = api.GetShadowForWindow(window.get()); | |
112 ASSERT_TRUE(shadow != NULL); | |
113 EXPECT_EQ(gfx::Rect(kOldBounds.size()).ToString(), | |
114 shadow->content_bounds().ToString()); | |
115 | |
116 // When we change the window's bounds, the shadow's should be updated too. | |
117 gfx::Rect kNewBounds(50, 60, 500, 400); | |
118 window->SetBounds(kNewBounds); | |
119 EXPECT_EQ(gfx::Rect(kNewBounds.size()).ToString(), | |
120 shadow->content_bounds().ToString()); | |
121 } | |
122 | |
123 // Tests that activating a window changes the shadow style. | |
124 TEST_F(ShadowControllerTest, ShadowStyle) { | |
125 ShadowController::TestApi api(shadow_controller()); | |
126 | |
127 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); | |
128 window1->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
129 window1->Init(aura::WINDOW_LAYER_TEXTURED); | |
130 ParentWindow(window1.get()); | |
131 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); | |
132 window1->Show(); | |
133 ActivateWindow(window1.get()); | |
134 | |
135 // window1 is active, so style should have active appearance. | |
136 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); | |
137 ASSERT_TRUE(shadow1 != NULL); | |
138 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | |
139 | |
140 // Create another window and activate it. | |
141 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); | |
142 window2->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
143 window2->Init(aura::WINDOW_LAYER_TEXTURED); | |
144 ParentWindow(window2.get()); | |
145 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); | |
146 window2->Show(); | |
147 ActivateWindow(window2.get()); | |
148 | |
149 // window1 is now inactive, so shadow should go inactive. | |
150 Shadow* shadow2 = api.GetShadowForWindow(window2.get()); | |
151 ASSERT_TRUE(shadow2 != NULL); | |
152 EXPECT_EQ(Shadow::STYLE_INACTIVE, shadow1->style()); | |
153 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow2->style()); | |
154 } | |
155 | |
156 // Tests that we use smaller shadows for tooltips and menus. | |
157 TEST_F(ShadowControllerTest, SmallShadowsForTooltipsAndMenus) { | |
158 ShadowController::TestApi api(shadow_controller()); | |
159 | |
160 scoped_ptr<aura::Window> tooltip_window(new aura::Window(NULL)); | |
161 tooltip_window->SetType(ui::wm::WINDOW_TYPE_TOOLTIP); | |
162 tooltip_window->Init(aura::WINDOW_LAYER_TEXTURED); | |
163 ParentWindow(tooltip_window.get()); | |
164 tooltip_window->SetBounds(gfx::Rect(10, 20, 300, 400)); | |
165 tooltip_window->Show(); | |
166 | |
167 Shadow* tooltip_shadow = api.GetShadowForWindow(tooltip_window.get()); | |
168 ASSERT_TRUE(tooltip_shadow != NULL); | |
169 EXPECT_EQ(Shadow::STYLE_SMALL, tooltip_shadow->style()); | |
170 | |
171 scoped_ptr<aura::Window> menu_window(new aura::Window(NULL)); | |
172 menu_window->SetType(ui::wm::WINDOW_TYPE_MENU); | |
173 menu_window->Init(aura::WINDOW_LAYER_TEXTURED); | |
174 ParentWindow(menu_window.get()); | |
175 menu_window->SetBounds(gfx::Rect(10, 20, 300, 400)); | |
176 menu_window->Show(); | |
177 | |
178 Shadow* menu_shadow = api.GetShadowForWindow(tooltip_window.get()); | |
179 ASSERT_TRUE(menu_shadow != NULL); | |
180 EXPECT_EQ(Shadow::STYLE_SMALL, menu_shadow->style()); | |
181 } | |
182 | |
183 // http://crbug.com/120210 - transient parents of certain types of transients | |
184 // should not lose their shadow when they lose activation to the transient. | |
185 TEST_F(ShadowControllerTest, TransientParentKeepsActiveShadow) { | |
186 ShadowController::TestApi api(shadow_controller()); | |
187 | |
188 scoped_ptr<aura::Window> window1(new aura::Window(NULL)); | |
189 window1->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
190 window1->Init(aura::WINDOW_LAYER_TEXTURED); | |
191 ParentWindow(window1.get()); | |
192 window1->SetBounds(gfx::Rect(10, 20, 300, 400)); | |
193 window1->Show(); | |
194 ActivateWindow(window1.get()); | |
195 | |
196 // window1 is active, so style should have active appearance. | |
197 Shadow* shadow1 = api.GetShadowForWindow(window1.get()); | |
198 ASSERT_TRUE(shadow1 != NULL); | |
199 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | |
200 | |
201 // Create a window that is transient to window1, and that has the 'hide on | |
202 // deactivate' property set. Upon activation, window1 should still have an | |
203 // active shadow. | |
204 scoped_ptr<aura::Window> window2(new aura::Window(NULL)); | |
205 window2->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
206 window2->Init(aura::WINDOW_LAYER_TEXTURED); | |
207 ParentWindow(window2.get()); | |
208 window2->SetBounds(gfx::Rect(11, 21, 301, 401)); | |
209 AddTransientChild(window1.get(), window2.get()); | |
210 aura::client::SetHideOnDeactivate(window2.get(), true); | |
211 window2->Show(); | |
212 ActivateWindow(window2.get()); | |
213 | |
214 // window1 is now inactive, but its shadow should still appear active. | |
215 EXPECT_EQ(Shadow::STYLE_ACTIVE, shadow1->style()); | |
216 } | |
217 | |
218 } // namespace corewm | |
219 } // namespace views | |
OLD | NEW |