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/root_window_event_filter.h" | |
6 | |
7 #include "ash/wm/activation_controller.h" | |
8 #include "ui/aura/client/activation_delegate.h" | |
9 #include "ui/aura/cursor.h" | |
10 #include "ui/aura/event.h" | |
11 #include "ui/aura/root_window.h" | |
12 #include "ui/aura/test/aura_test_base.h" | |
13 #include "ui/aura/test/event_generator.h" | |
14 #include "ui/aura/test/test_windows.h" | |
15 #include "ui/aura/test/test_event_filter.h" | |
16 #include "ui/aura/test/test_window_delegate.h" | |
17 #include "ui/aura_shell/shell_window_ids.h" | |
18 #include "ui/aura_shell/test/test_activation_delegate.h" | |
19 #include "ui/aura_shell/window_util.h" | |
20 #include "ui/base/hit_test.h" | |
21 #include "ui/gfx/screen.h" | |
22 | |
23 namespace aura_shell { | |
24 namespace test { | |
25 | |
26 class RootWindowEventFilterTest : public aura::test::AuraTestBase { | |
27 public: | |
28 RootWindowEventFilterTest() { | |
29 aura::RootWindow::GetInstance()->SetEventFilter( | |
30 new internal::RootWindowEventFilter); | |
31 | |
32 aura::RootWindow::GetInstance()->set_id( | |
33 internal::kShellWindowId_DefaultContainer); | |
34 activation_controller_.reset(new internal::ActivationController); | |
35 activation_controller_->set_default_container_for_test( | |
36 aura::RootWindow::GetInstance()); | |
37 } | |
38 virtual ~RootWindowEventFilterTest() { | |
39 aura::RootWindow::GetInstance()->SetEventFilter(NULL); | |
40 } | |
41 | |
42 private: | |
43 scoped_ptr<internal::ActivationController> activation_controller_; | |
44 | |
45 DISALLOW_COPY_AND_ASSIGN(RootWindowEventFilterTest); | |
46 }; | |
47 | |
48 class HitTestWindowDelegate : public aura::test::TestWindowDelegate { | |
49 public: | |
50 HitTestWindowDelegate() | |
51 : hittest_code_(HTNOWHERE) { | |
52 } | |
53 virtual ~HitTestWindowDelegate() {} | |
54 void set_hittest_code(int hittest_code) { hittest_code_ = hittest_code; } | |
55 | |
56 private: | |
57 // Overridden from TestWindowDelegate: | |
58 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
59 return hittest_code_; | |
60 } | |
61 | |
62 int hittest_code_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(HitTestWindowDelegate); | |
65 }; | |
66 | |
67 TEST_F(RootWindowEventFilterTest, Focus) { | |
68 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
69 root_window->SetBounds(gfx::Rect(0, 0, 510, 510)); | |
70 | |
71 // Supplied ids are negative so as not to collide with shell ids. | |
72 // TODO(beng): maybe introduce a MAKE_SHELL_ID() macro that generates a safe | |
73 // id beyond shell id max? | |
74 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindow( | |
75 SK_ColorWHITE, -1, gfx::Rect(10, 10, 500, 500), NULL)); | |
76 scoped_ptr<aura::Window> w11(aura::test::CreateTestWindow( | |
77 SK_ColorGREEN, -11, gfx::Rect(5, 5, 100, 100), w1.get())); | |
78 scoped_ptr<aura::Window> w111(aura::test::CreateTestWindow( | |
79 SK_ColorCYAN, -111, gfx::Rect(5, 5, 75, 75), w11.get())); | |
80 scoped_ptr<aura::Window> w1111(aura::test::CreateTestWindow( | |
81 SK_ColorRED, -1111, gfx::Rect(5, 5, 50, 50), w111.get())); | |
82 scoped_ptr<aura::Window> w12(aura::test::CreateTestWindow( | |
83 SK_ColorMAGENTA, -12, gfx::Rect(10, 420, 25, 25), w1.get())); | |
84 aura::test::ColorTestWindowDelegate* w121delegate = | |
85 new aura::test::ColorTestWindowDelegate(SK_ColorYELLOW); | |
86 scoped_ptr<aura::Window> w121(aura::test::CreateTestWindowWithDelegate( | |
87 w121delegate, -121, gfx::Rect(5, 5, 5, 5), w12.get())); | |
88 aura::test::ColorTestWindowDelegate* w122delegate = | |
89 new aura::test::ColorTestWindowDelegate(SK_ColorRED); | |
90 scoped_ptr<aura::Window> w122(aura::test::CreateTestWindowWithDelegate( | |
91 w122delegate, -122, gfx::Rect(10, 5, 5, 5), w12.get())); | |
92 scoped_ptr<aura::Window> w13(aura::test::CreateTestWindow( | |
93 SK_ColorGRAY, -13, gfx::Rect(5, 470, 50, 50), w1.get())); | |
94 | |
95 // Click on a sub-window (w121) to focus it. | |
96 gfx::Point click_point = w121->bounds().CenterPoint(); | |
97 aura::Window::ConvertPointToWindow(w121->parent(), root_window, &click_point); | |
98 aura::MouseEvent mouse(ui::ET_MOUSE_PRESSED, | |
99 click_point, | |
100 ui::EF_LEFT_MOUSE_BUTTON); | |
101 root_window->DispatchMouseEvent(&mouse); | |
102 aura::internal::FocusManager* focus_manager = w121->GetFocusManager(); | |
103 EXPECT_EQ(w121.get(), focus_manager->GetFocusedWindow()); | |
104 | |
105 // The key press should be sent to the focused sub-window. | |
106 aura::KeyEvent keyev(ui::ET_KEY_PRESSED, ui::VKEY_E, 0); | |
107 root_window->DispatchKeyEvent(&keyev); | |
108 EXPECT_EQ(ui::VKEY_E, w121delegate->last_key_code()); | |
109 | |
110 // Touch on a sub-window (w122) to focus it. | |
111 click_point = w122->bounds().CenterPoint(); | |
112 aura::Window::ConvertPointToWindow(w122->parent(), root_window, &click_point); | |
113 aura::TouchEvent touchev(ui::ET_TOUCH_PRESSED, click_point, 0); | |
114 root_window->DispatchTouchEvent(&touchev); | |
115 focus_manager = w122->GetFocusManager(); | |
116 EXPECT_EQ(w122.get(), focus_manager->GetFocusedWindow()); | |
117 | |
118 // The key press should be sent to the focused sub-window. | |
119 root_window->DispatchKeyEvent(&keyev); | |
120 EXPECT_EQ(ui::VKEY_E, w122delegate->last_key_code()); | |
121 | |
122 // Removing the focused window from parent should reset the focused window. | |
123 w12->RemoveChild(w122.get()); | |
124 EXPECT_EQ(NULL, w122->GetFocusManager()); | |
125 EXPECT_EQ(NULL, w12->GetFocusManager()->GetFocusedWindow()); | |
126 EXPECT_FALSE(root_window->DispatchKeyEvent(&keyev)); | |
127 } | |
128 | |
129 // Various assertion testing for activating windows. | |
130 TEST_F(RootWindowEventFilterTest, ActivateOnMouse) { | |
131 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
132 | |
133 TestActivationDelegate d1; | |
134 aura::test::TestWindowDelegate wd; | |
135 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
136 &wd, 1, gfx::Rect(10, 10, 50, 50), NULL)); | |
137 d1.SetWindow(w1.get()); | |
138 TestActivationDelegate d2; | |
139 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
140 &wd, 2, gfx::Rect(70, 70, 50, 50), NULL)); | |
141 d2.SetWindow(w2.get()); | |
142 | |
143 aura::internal::FocusManager* focus_manager = w1->GetFocusManager(); | |
144 | |
145 d1.Clear(); | |
146 d2.Clear(); | |
147 | |
148 // Activate window1. | |
149 aura_shell::ActivateWindow(w1.get()); | |
150 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
151 EXPECT_EQ(w1.get(), focus_manager->GetFocusedWindow()); | |
152 EXPECT_EQ(1, d1.activated_count()); | |
153 EXPECT_EQ(0, d1.lost_active_count()); | |
154 d1.Clear(); | |
155 | |
156 { | |
157 // Click on window2. | |
158 gfx::Point press_point = w2->bounds().CenterPoint(); | |
159 aura::Window::ConvertPointToWindow(w2->parent(), root_window, &press_point); | |
160 aura::test::EventGenerator generator(press_point); | |
161 generator.ClickLeftButton(); | |
162 | |
163 // Window2 should have become active. | |
164 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
165 EXPECT_EQ(w2.get(), focus_manager->GetFocusedWindow()); | |
166 EXPECT_EQ(0, d1.activated_count()); | |
167 EXPECT_EQ(1, d1.lost_active_count()); | |
168 EXPECT_EQ(1, d2.activated_count()); | |
169 EXPECT_EQ(0, d2.lost_active_count()); | |
170 d1.Clear(); | |
171 d2.Clear(); | |
172 } | |
173 | |
174 { | |
175 // Click back on window1, but set it up so w1 doesn't activate on click. | |
176 gfx::Point press_point = w1->bounds().CenterPoint(); | |
177 aura::Window::ConvertPointToWindow(w1->parent(), root_window, &press_point); | |
178 aura::test::EventGenerator generator(press_point); | |
179 d1.set_activate(false); | |
180 generator.ClickLeftButton(); | |
181 | |
182 // Window2 should still be active and focused. | |
183 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
184 EXPECT_EQ(w2.get(), focus_manager->GetFocusedWindow()); | |
185 EXPECT_EQ(0, d1.activated_count()); | |
186 EXPECT_EQ(0, d1.lost_active_count()); | |
187 EXPECT_EQ(0, d2.activated_count()); | |
188 EXPECT_EQ(0, d2.lost_active_count()); | |
189 d1.Clear(); | |
190 d2.Clear(); | |
191 } | |
192 | |
193 // Destroy window2, this should make window1 active. | |
194 d1.set_activate(true); | |
195 w2.reset(); | |
196 EXPECT_EQ(0, d2.activated_count()); | |
197 EXPECT_EQ(0, d2.lost_active_count()); | |
198 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
199 EXPECT_EQ(w1.get(), focus_manager->GetFocusedWindow()); | |
200 EXPECT_EQ(1, d1.activated_count()); | |
201 EXPECT_EQ(0, d1.lost_active_count()); | |
202 } | |
203 | |
204 // Essentially the same as ActivateOnMouse, but for touch events. | |
205 TEST_F(RootWindowEventFilterTest, ActivateOnTouch) { | |
206 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
207 | |
208 TestActivationDelegate d1; | |
209 aura::test::TestWindowDelegate wd; | |
210 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindowWithDelegate( | |
211 &wd, -1, gfx::Rect(10, 10, 50, 50), NULL)); | |
212 d1.SetWindow(w1.get()); | |
213 TestActivationDelegate d2; | |
214 scoped_ptr<aura::Window> w2(aura::test::CreateTestWindowWithDelegate( | |
215 &wd, -2, gfx::Rect(70, 70, 50, 50), NULL)); | |
216 d2.SetWindow(w2.get()); | |
217 | |
218 aura::internal::FocusManager* focus_manager = w1->GetFocusManager(); | |
219 | |
220 d1.Clear(); | |
221 d2.Clear(); | |
222 | |
223 // Activate window1. | |
224 aura_shell::ActivateWindow(w1.get()); | |
225 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
226 EXPECT_EQ(w1.get(), focus_manager->GetFocusedWindow()); | |
227 EXPECT_EQ(1, d1.activated_count()); | |
228 EXPECT_EQ(0, d1.lost_active_count()); | |
229 d1.Clear(); | |
230 | |
231 // Touch window2. | |
232 gfx::Point press_point = w2->bounds().CenterPoint(); | |
233 aura::Window::ConvertPointToWindow(w2->parent(), root_window, &press_point); | |
234 aura::TouchEvent touchev1(ui::ET_TOUCH_PRESSED, press_point, 0); | |
235 root_window->DispatchTouchEvent(&touchev1); | |
236 | |
237 // Window2 should have become active. | |
238 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
239 EXPECT_EQ(w2.get(), focus_manager->GetFocusedWindow()); | |
240 EXPECT_EQ(0, d1.activated_count()); | |
241 EXPECT_EQ(1, d1.lost_active_count()); | |
242 EXPECT_EQ(1, d2.activated_count()); | |
243 EXPECT_EQ(0, d2.lost_active_count()); | |
244 d1.Clear(); | |
245 d2.Clear(); | |
246 | |
247 // Touch window1, but set it up so w1 doesn't activate on touch. | |
248 press_point = w1->bounds().CenterPoint(); | |
249 aura::Window::ConvertPointToWindow(w1->parent(), root_window, &press_point); | |
250 d1.set_activate(false); | |
251 aura::TouchEvent touchev2(ui::ET_TOUCH_PRESSED, press_point, 0); | |
252 root_window->DispatchTouchEvent(&touchev2); | |
253 | |
254 // Window2 should still be active and focused. | |
255 EXPECT_TRUE(IsActiveWindow(w2.get())); | |
256 EXPECT_EQ(w2.get(), focus_manager->GetFocusedWindow()); | |
257 EXPECT_EQ(0, d1.activated_count()); | |
258 EXPECT_EQ(0, d1.lost_active_count()); | |
259 EXPECT_EQ(0, d2.activated_count()); | |
260 EXPECT_EQ(0, d2.lost_active_count()); | |
261 d1.Clear(); | |
262 d2.Clear(); | |
263 | |
264 // Destroy window2, this should make window1 active. | |
265 d1.set_activate(true); | |
266 w2.reset(); | |
267 EXPECT_EQ(0, d2.activated_count()); | |
268 EXPECT_EQ(0, d2.lost_active_count()); | |
269 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
270 EXPECT_EQ(w1.get(), focus_manager->GetFocusedWindow()); | |
271 EXPECT_EQ(1, d1.activated_count()); | |
272 EXPECT_EQ(0, d1.lost_active_count()); | |
273 } | |
274 | |
275 TEST_F(RootWindowEventFilterTest, MouseEventCursors) { | |
276 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
277 | |
278 // Create a window. | |
279 const int kWindowLeft = 123; | |
280 const int kWindowTop = 45; | |
281 HitTestWindowDelegate window_delegate; | |
282 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate( | |
283 &window_delegate, | |
284 -1, | |
285 gfx::Rect(kWindowLeft, kWindowTop, 640, 480), | |
286 NULL)); | |
287 | |
288 // Create two mouse movement events we can switch between. | |
289 gfx::Point point1(kWindowLeft, kWindowTop); | |
290 aura::Window::ConvertPointToWindow(window->parent(), root_window, &point1); | |
291 aura::MouseEvent move1(ui::ET_MOUSE_MOVED, point1, 0x0); | |
292 | |
293 gfx::Point point2(kWindowLeft + 1, kWindowTop + 1); | |
294 aura::Window::ConvertPointToWindow(window->parent(), root_window, &point2); | |
295 aura::MouseEvent move2(ui::ET_MOUSE_MOVED, point2, 0x0); | |
296 | |
297 // Cursor starts as null. | |
298 EXPECT_EQ(aura::kCursorNull, root_window->last_cursor()); | |
299 | |
300 // Resize edges and corners show proper cursors. | |
301 window_delegate.set_hittest_code(HTBOTTOM); | |
302 root_window->DispatchMouseEvent(&move1); | |
303 EXPECT_EQ(aura::kCursorSouthResize, root_window->last_cursor()); | |
304 | |
305 window_delegate.set_hittest_code(HTBOTTOMLEFT); | |
306 root_window->DispatchMouseEvent(&move2); | |
307 EXPECT_EQ(aura::kCursorSouthWestResize, root_window->last_cursor()); | |
308 | |
309 window_delegate.set_hittest_code(HTBOTTOMRIGHT); | |
310 root_window->DispatchMouseEvent(&move1); | |
311 EXPECT_EQ(aura::kCursorSouthEastResize, root_window->last_cursor()); | |
312 | |
313 window_delegate.set_hittest_code(HTLEFT); | |
314 root_window->DispatchMouseEvent(&move2); | |
315 EXPECT_EQ(aura::kCursorWestResize, root_window->last_cursor()); | |
316 | |
317 window_delegate.set_hittest_code(HTRIGHT); | |
318 root_window->DispatchMouseEvent(&move1); | |
319 EXPECT_EQ(aura::kCursorEastResize, root_window->last_cursor()); | |
320 | |
321 window_delegate.set_hittest_code(HTTOP); | |
322 root_window->DispatchMouseEvent(&move2); | |
323 EXPECT_EQ(aura::kCursorNorthResize, root_window->last_cursor()); | |
324 | |
325 window_delegate.set_hittest_code(HTTOPLEFT); | |
326 root_window->DispatchMouseEvent(&move1); | |
327 EXPECT_EQ(aura::kCursorNorthWestResize, root_window->last_cursor()); | |
328 | |
329 window_delegate.set_hittest_code(HTTOPRIGHT); | |
330 root_window->DispatchMouseEvent(&move2); | |
331 EXPECT_EQ(aura::kCursorNorthEastResize, root_window->last_cursor()); | |
332 | |
333 // Client area uses null cursor. | |
334 window_delegate.set_hittest_code(HTCLIENT); | |
335 root_window->DispatchMouseEvent(&move1); | |
336 EXPECT_EQ(aura::kCursorNull, root_window->last_cursor()); | |
337 } | |
338 | |
339 TEST_F(RootWindowEventFilterTest, TransformActivate) { | |
340 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
341 gfx::Size size = root_window->GetHostSize(); | |
342 EXPECT_EQ(gfx::Rect(size), | |
343 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point())); | |
344 | |
345 // Rotate it clock-wise 90 degrees. | |
346 ui::Transform transform; | |
347 transform.SetRotate(90.0f); | |
348 transform.ConcatTranslate(size.width(), 0); | |
349 root_window->SetTransform(transform); | |
350 | |
351 TestActivationDelegate d1; | |
352 aura::test::TestWindowDelegate wd; | |
353 scoped_ptr<aura::Window> w1( | |
354 CreateTestWindowWithDelegate(&wd, 1, gfx::Rect(0, 10, 50, 50), NULL)); | |
355 d1.SetWindow(w1.get()); | |
356 w1->Show(); | |
357 | |
358 gfx::Point miss_point(5, 5); | |
359 transform.TransformPoint(miss_point); | |
360 aura::MouseEvent mouseev1(ui::ET_MOUSE_PRESSED, | |
361 miss_point, | |
362 ui::EF_LEFT_MOUSE_BUTTON); | |
363 root_window->DispatchMouseEvent(&mouseev1); | |
364 EXPECT_FALSE(w1->GetFocusManager()->GetFocusedWindow()); | |
365 aura::MouseEvent mouseup(ui::ET_MOUSE_RELEASED, | |
366 miss_point, | |
367 ui::EF_LEFT_MOUSE_BUTTON); | |
368 root_window->DispatchMouseEvent(&mouseup); | |
369 | |
370 gfx::Point hit_point(5, 15); | |
371 transform.TransformPoint(hit_point); | |
372 aura::MouseEvent mouseev2(ui::ET_MOUSE_PRESSED, | |
373 hit_point, | |
374 ui::EF_LEFT_MOUSE_BUTTON); | |
375 root_window->DispatchMouseEvent(&mouseev2); | |
376 EXPECT_TRUE(IsActiveWindow(w1.get())); | |
377 EXPECT_EQ(w1.get(), w1->GetFocusManager()->GetFocusedWindow()); | |
378 } | |
379 | |
380 TEST_F(RootWindowEventFilterTest, AdditionalFilters) { | |
381 aura::RootWindow* root_window = aura::RootWindow::GetInstance(); | |
382 | |
383 // Creates a window and make it active | |
384 scoped_ptr<aura::Window> w1(aura::test::CreateTestWindow( | |
385 SK_ColorWHITE, -1, gfx::Rect(0, 0, 100, 100), NULL)); | |
386 aura_shell::ActivateWindow(w1.get()); | |
387 | |
388 // Creates two addition filters | |
389 scoped_ptr<aura::test::TestEventFilter> f1( | |
390 new aura::test::TestEventFilter(NULL)); | |
391 scoped_ptr<aura::test::TestEventFilter> f2( | |
392 new aura::test::TestEventFilter(NULL)); | |
393 | |
394 // Adds them to root window event filter. | |
395 internal::RootWindowEventFilter* root_window_filter = | |
396 static_cast<internal::RootWindowEventFilter*>( | |
397 root_window->event_filter()); | |
398 root_window_filter->AddFilter(f1.get()); | |
399 root_window_filter->AddFilter(f2.get()); | |
400 | |
401 // Dispatches mouse and keyboard events. | |
402 aura::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, 0); | |
403 root_window->DispatchKeyEvent(&key_event); | |
404 aura::MouseEvent mouse_pressed(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), 0x0); | |
405 root_window->DispatchMouseEvent(&mouse_pressed); | |
406 | |
407 // Both filters should get the events. | |
408 EXPECT_EQ(1, f1->key_event_count()); | |
409 EXPECT_EQ(1, f1->mouse_event_count()); | |
410 EXPECT_EQ(1, f2->key_event_count()); | |
411 EXPECT_EQ(1, f2->mouse_event_count()); | |
412 | |
413 f1->ResetCounts(); | |
414 f2->ResetCounts(); | |
415 | |
416 // Makes f1 consume events. | |
417 f1->set_consumes_key_events(true); | |
418 f1->set_consumes_mouse_events(true); | |
419 | |
420 // Dispatches events. | |
421 root_window->DispatchKeyEvent(&key_event); | |
422 aura::MouseEvent mouse_released(ui::ET_MOUSE_RELEASED, gfx::Point(0, 0), 0x0); | |
423 root_window->DispatchMouseEvent(&mouse_released); | |
424 | |
425 // f1 should still get the events but f2 no longer gets them. | |
426 EXPECT_EQ(1, f1->key_event_count()); | |
427 EXPECT_EQ(1, f1->mouse_event_count()); | |
428 EXPECT_EQ(0, f2->key_event_count()); | |
429 EXPECT_EQ(0, f2->mouse_event_count()); | |
430 | |
431 f1->ResetCounts(); | |
432 f2->ResetCounts(); | |
433 | |
434 // Remove f1 from additonal filters list. | |
435 root_window_filter->RemoveFilter(f1.get()); | |
436 | |
437 // Dispatches events. | |
438 root_window->DispatchKeyEvent(&key_event); | |
439 root_window->DispatchMouseEvent(&mouse_pressed); | |
440 | |
441 // f1 should get no events since it's out and f2 should get them. | |
442 EXPECT_EQ(0, f1->key_event_count()); | |
443 EXPECT_EQ(0, f1->mouse_event_count()); | |
444 EXPECT_EQ(1, f2->key_event_count()); | |
445 EXPECT_EQ(1, f2->mouse_event_count()); | |
446 | |
447 root_window_filter->RemoveFilter(f2.get()); | |
448 } | |
449 | |
450 } // namespace test | |
451 } // namespace aura_shell | |
OLD | NEW |