OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/aura_shell/activation_controller.h" | |
6 | |
7 #include "ui/aura/focus_manager.h" | |
8 #include "ui/aura/root_window.h" | |
9 #include "ui/aura/test/event_generator.h" | |
10 #include "ui/aura/test/test_windows.h" | |
11 #include "ui/aura/test/test_window_delegate.h" | |
12 #include "ui/aura_shell/test/aura_shell_test_base.h" | |
13 #include "ui/aura_shell/test/test_activation_delegate.h" | |
14 #include "ui/aura_shell/window_util.h" | |
15 | |
16 #if defined(OS_WIN) | |
17 // Windows headers define macros for these function names which screw with us. | |
18 #if defined(CreateWindow) | |
19 #undef CreateWindow | |
20 #endif | |
21 #endif | |
22 | |
23 namespace aura_shell { | |
24 namespace test { | |
25 | |
26 typedef test::AuraShellTestBase ActivationControllerTest; | |
27 | |
28 // Utilities for a set of tests that test | |
29 // ActivationController::GetTopmostWindowToActivate(). | |
30 class GetTopmostWindowToActivateTest : public ActivationControllerTest { | |
31 public: | |
32 GetTopmostWindowToActivateTest() : ad_1_(false), ad_3_(false) {} | |
33 virtual ~GetTopmostWindowToActivateTest() {} | |
34 | |
35 // Overridden from ActivationControllerTest: | |
36 virtual void SetUp() OVERRIDE { | |
37 ActivationControllerTest::SetUp(); | |
38 CreateWindows(); | |
39 } | |
40 virtual void TearDown() OVERRIDE { | |
41 DestroyWindows(); | |
42 ActivationControllerTest::TearDown(); | |
43 } | |
44 | |
45 protected: | |
46 aura::Window* w1() { return w1_.get(); } | |
47 aura::Window* w2() { return w2_.get(); } | |
48 aura::Window* w3() { return w3_.get(); } | |
49 aura::Window* w4() { return w4_.get(); } | |
50 | |
51 void DestroyWindow2() { | |
52 w2_.reset(); | |
53 } | |
54 | |
55 private: | |
56 void CreateWindows() { | |
57 // Create four windows, the first and third are not activatable, the second | |
58 // and fourth are. | |
59 w1_.reset(CreateWindow(1, &ad_1_)); | |
60 w2_.reset(CreateWindow(2, &ad_2_)); | |
61 w3_.reset(CreateWindow(3, &ad_3_)); | |
62 w4_.reset(CreateWindow(4, &ad_4_)); | |
63 } | |
64 | |
65 aura::Window* CreateWindow(int id, TestActivationDelegate* delegate) { | |
66 aura::Window* window = aura::test::CreateTestWindowWithDelegate( | |
67 &delegate_, id, gfx::Rect(), NULL); | |
68 delegate->SetWindow(window); | |
69 return window; | |
70 } | |
71 | |
72 void DestroyWindows() { | |
73 w1_.reset(); | |
74 w2_.reset(); | |
75 w3_.reset(); | |
76 w4_.reset(); | |
77 } | |
78 | |
79 aura::test::TestWindowDelegate delegate_; | |
80 TestActivationDelegate ad_1_; | |
81 TestActivationDelegate ad_2_; | |
82 TestActivationDelegate ad_3_; | |
83 TestActivationDelegate ad_4_; | |
84 scoped_ptr<aura::Window> w1_; // Non-activatable. | |
85 scoped_ptr<aura::Window> w2_; // Activatable. | |
86 scoped_ptr<aura::Window> w3_; // Non-activatable. | |
87 scoped_ptr<aura::Window> w4_; // Activatable. | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(GetTopmostWindowToActivateTest); | |
90 }; | |
91 | |
92 // Hiding the active window should activate the next valid activatable window. | |
93 TEST_F(GetTopmostWindowToActivateTest, HideActivatesNext) { | |
94 ActivateWindow(w2()); | |
95 EXPECT_TRUE(IsActiveWindow(w2())); | |
96 | |
97 w2()->Hide(); | |
98 EXPECT_TRUE(IsActiveWindow(w4())); | |
99 } | |
100 | |
101 // Destroying the active window should activate the next valid activatable | |
102 // window. | |
103 TEST_F(GetTopmostWindowToActivateTest, DestroyActivatesNext) { | |
104 ActivateWindow(w2()); | |
105 EXPECT_TRUE(IsActiveWindow(w2())); | |
106 | |
107 DestroyWindow2(); | |
108 EXPECT_EQ(NULL, w2()); | |
109 EXPECT_TRUE(IsActiveWindow(w4())); | |
110 } | |
111 | |
112 // Deactivating the active window should activate the next valid activatable | |
113 // window. | |
114 TEST_F(GetTopmostWindowToActivateTest, DeactivateActivatesNext) { | |
115 ActivateWindow(w2()); | |
116 EXPECT_TRUE(IsActiveWindow(w2())); | |
117 | |
118 DeactivateWindow(w2()); | |
119 EXPECT_TRUE(IsActiveWindow(w4())); | |
120 } | |
121 | |
122 // Test if the clicking on a menu picks the transient parent as activatable | |
123 // window. | |
124 TEST_F(ActivationControllerTest, ClickOnMenu) { | |
125 aura::test::TestWindowDelegate wd; | |
126 TestActivationDelegate ad1; | |
127 TestActivationDelegate ad2(false); | |
128 | |
129 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
130 &wd, 1, gfx::Rect(100, 100), NULL)); | |
131 ad1.SetWindow(w1.get()); | |
132 EXPECT_TRUE(IsActiveWindow(NULL)); | |
133 | |
134 // Clicking on an activatable window activtes the window. | |
135 aura::test::EventGenerator generator(w1.get()); | |
136 generator.ClickLeftButton(); | |
137 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
138 | |
139 // Creates a menu that covers the transient parent. | |
140 scoped_ptr<aura::Window> menu(aura::test::CreateTestWindowWithDelegateAndType( | |
141 &wd, aura::client::WINDOW_TYPE_MENU, 2, gfx::Rect(100, 100), NULL)); | |
142 ad2.SetWindow(menu.get()); | |
143 w1->AddTransientChild(menu.get()); | |
144 | |
145 // Clicking on a menu whose transient parent is active window shouldn't | |
146 // change the active window. | |
147 generator.ClickLeftButton(); | |
148 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
149 } | |
150 | |
151 // Various assertions for activating/deactivating. | |
152 TEST_F(ActivationControllerTest, Deactivate) { | |
153 aura::test::TestWindowDelegate d1; | |
154 aura::test::TestWindowDelegate d2; | |
155 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
156 &d1, 1, gfx::Rect(), NULL)); | |
157 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
158 &d2, 2, gfx::Rect(), NULL)); | |
159 aura::Window* parent = w1->parent(); | |
160 parent->Show(); | |
161 ASSERT_TRUE(parent); | |
162 ASSERT_EQ(2u, parent->children().size()); | |
163 // Activate w2 and make sure it's active and frontmost. | |
164 ActivateWindow(w2.get()); | |
165 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
166 EXPECT_FALSE(IsActiveWindow(w1.get())); | |
167 EXPECT_EQ(w2.get(), parent->children()[1]); | |
168 | |
169 // Activate w1 and make sure it's active and frontmost. | |
170 ActivateWindow(w1.get()); | |
171 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
172 EXPECT_FALSE(IsActiveWindow(w2.get())); | |
173 EXPECT_EQ(w1.get(), parent->children()[1]); | |
174 | |
175 // Deactivate w1 and make sure w2 becomes active and frontmost. | |
176 DeactivateWindow(w1.get()); | |
177 EXPECT_FALSE(IsActiveWindow(w1.get())); | |
178 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
179 EXPECT_EQ(w2.get(), parent->children()[1]); | |
180 } | |
181 | |
182 // Verifies that when WindowDelegate::OnLostActive is invoked the window is not | |
183 // active. | |
184 TEST_F(ActivationControllerTest, NotActiveInLostActive) { | |
185 TestActivationDelegate ad1; | |
186 aura::test::TestWindowDelegate wd; | |
187 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
188 &wd, 1, gfx::Rect(10, 10, 50, 50), NULL)); | |
189 ad1.SetWindow(w1.get()); | |
190 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
191 NULL, 1, gfx::Rect(10, 10, 50, 50), NULL)); | |
192 | |
193 // Activate w1. | |
194 ActivateWindow(w1.get()); | |
195 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
196 | |
197 // Should not have gotten a OnLostActive yet. | |
198 EXPECT_EQ(0, ad1.lost_active_count()); | |
199 | |
200 // ActivateWindow(NULL) should not change the active window. | |
201 ActivateWindow(NULL); | |
202 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
203 | |
204 // Now activate another window. | |
205 ActivateWindow(w2.get()); | |
206 | |
207 // Should have gotten OnLostActive and w1 should not have been active at that | |
208 // time. | |
209 EXPECT_EQ(1, ad1.lost_active_count()); | |
210 EXPECT_FALSE(ad1.window_was_active()); | |
211 } | |
212 | |
213 // Verifies that focusing another window or its children causes it to become | |
214 // active. | |
215 TEST_F(ActivationControllerTest, FocusTriggersActivation) { | |
216 aura::test::TestWindowDelegate wd; | |
217 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
218 &wd, -1, gfx::Rect(50, 50), NULL)); | |
219 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
220 &wd, -2, gfx::Rect(50, 50), NULL)); | |
221 scoped_ptr<aura::Window> w21(aura::test::CreateTestWindowWithDelegate( | |
222 &wd, -21, gfx::Rect(50, 50), w2.get())); | |
223 | |
224 ActivateWindow(w1.get()); | |
225 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
226 EXPECT_TRUE(w1->HasFocus()); | |
227 | |
228 w2->Focus(); | |
229 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
230 EXPECT_TRUE(w2->HasFocus()); | |
231 | |
232 ActivateWindow(w1.get()); | |
233 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
234 EXPECT_TRUE(w1->HasFocus()); | |
235 | |
236 w21->Focus(); | |
237 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
238 EXPECT_TRUE(w21->HasFocus()); | |
239 } | |
240 | |
241 // Verifies that we prevent all attempts to focus a child of a non-activatable | |
242 // window from claiming focus to that window. | |
243 TEST_F(ActivationControllerTest, PreventFocusToNonActivatableWindow) { | |
244 aura::test::TestWindowDelegate wd; | |
245 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
246 &wd, -1, gfx::Rect(50, 50), NULL)); | |
247 // The RootWindow itself is a non-activatable parent. | |
248 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
249 &wd, -2, gfx::Rect(50, 50), aura::RootWindow::GetInstance())); | |
250 scoped_ptr<aura::Window> w21(aura::test::CreateTestWindowWithDelegate( | |
251 &wd, -21, gfx::Rect(50, 50), w2.get())); | |
252 | |
253 ActivateWindow(w1.get()); | |
254 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
255 EXPECT_TRUE(w1->HasFocus()); | |
256 | |
257 // Try activating |w2|. It's not a child of an activatable container, so it | |
258 // should neither be activated nor get focus. | |
259 ActivateWindow(w2.get()); | |
260 EXPECT_FALSE(IsActiveWindow(w2.get())); | |
261 EXPECT_FALSE(w2->HasFocus()); | |
262 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
263 EXPECT_TRUE(w1->HasFocus()); | |
264 | |
265 // Try focusing |w2|. Same rules apply. | |
266 w2->Focus(); | |
267 EXPECT_FALSE(IsActiveWindow(w2.get())); | |
268 EXPECT_FALSE(w2->HasFocus()); | |
269 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
270 EXPECT_TRUE(w1->HasFocus()); | |
271 | |
272 // Try focusing |w21|. Same rules apply. | |
273 EXPECT_FALSE(IsActiveWindow(w2.get())); | |
274 EXPECT_FALSE(w21->HasFocus()); | |
275 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
276 EXPECT_TRUE(w1->HasFocus()); | |
277 } | |
278 | |
279 } // namespace test | |
280 } // namespace aura_shell | |
OLD | NEW |