OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
6 | 6 |
7 #include "components/mus/public/cpp/event_matcher.h" | 7 #include "components/mus/public/cpp/event_matcher.h" |
8 #include "components/mus/ws/event_dispatcher_delegate.h" | 8 #include "components/mus/ws/event_dispatcher_delegate.h" |
9 #include "components/mus/ws/server_window.h" | 9 #include "components/mus/ws/server_window.h" |
| 10 #include "components/mus/ws/server_window_surface_manager_test_api.h" |
10 #include "components/mus/ws/test_server_window_delegate.h" | 11 #include "components/mus/ws/test_server_window_delegate.h" |
11 #include "mojo/converters/input_events/input_events_type_converters.h" | 12 #include "mojo/converters/input_events/input_events_type_converters.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
14 | 15 |
15 namespace mus { | 16 namespace mus { |
16 namespace ws { | 17 namespace ws { |
17 namespace { | 18 namespace { |
18 | 19 |
19 class TestEventDispatcherDelegate : public EventDispatcherDelegate { | 20 class TestEventDispatcherDelegate : public EventDispatcherDelegate { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 128 |
128 TEST(EventDispatcherTest, OnEvent) { | 129 TEST(EventDispatcherTest, OnEvent) { |
129 TestServerWindowDelegate window_delegate; | 130 TestServerWindowDelegate window_delegate; |
130 ServerWindow root(&window_delegate, WindowId(1, 2)); | 131 ServerWindow root(&window_delegate, WindowId(1, 2)); |
131 window_delegate.set_root_window(&root); | 132 window_delegate.set_root_window(&root); |
132 root.SetVisible(true); | 133 root.SetVisible(true); |
133 | 134 |
134 ServerWindow child(&window_delegate, WindowId(1, 3)); | 135 ServerWindow child(&window_delegate, WindowId(1, 3)); |
135 root.Add(&child); | 136 root.Add(&child); |
136 child.SetVisible(true); | 137 child.SetVisible(true); |
| 138 EnableHitTest(&child); |
137 | 139 |
138 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 140 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
139 child.SetBounds(gfx::Rect(10, 10, 20, 20)); | 141 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
140 | 142 |
141 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 143 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
142 EventDispatcher dispatcher(&event_dispatcher_delegate); | 144 EventDispatcher dispatcher(&event_dispatcher_delegate); |
143 dispatcher.set_root(&root); | 145 dispatcher.set_root(&root); |
144 | 146 |
145 // Send event that is over child. | 147 // Send event that is over child. |
146 const ui::MouseEvent ui_event( | 148 const ui::MouseEvent ui_event( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 ServerWindow root(&window_delegate, WindowId(1, 2)); | 205 ServerWindow root(&window_delegate, WindowId(1, 2)); |
204 window_delegate.set_root_window(&root); | 206 window_delegate.set_root_window(&root); |
205 root.SetVisible(true); | 207 root.SetVisible(true); |
206 | 208 |
207 ServerWindow child(&window_delegate, WindowId(1, 3)); | 209 ServerWindow child(&window_delegate, WindowId(1, 3)); |
208 root.Add(&child); | 210 root.Add(&child); |
209 child.SetVisible(true); | 211 child.SetVisible(true); |
210 | 212 |
211 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 213 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
212 child.SetBounds(gfx::Rect(10, 10, 20, 20)); | 214 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
| 215 EnableHitTest(&child); |
213 | 216 |
214 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 217 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
215 EventDispatcher dispatcher(&event_dispatcher_delegate); | 218 EventDispatcher dispatcher(&event_dispatcher_delegate); |
216 dispatcher.set_root(&root); | 219 dispatcher.set_root(&root); |
217 | 220 |
218 MouseEventTest tests[] = { | 221 MouseEventTest tests[] = { |
219 // Send a mouse down event over child. | 222 // Send a mouse down event over child. |
220 {ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(20, 25), | 223 {ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(20, 25), |
221 gfx::Point(20, 25), base::TimeDelta(), | 224 gfx::Point(20, 25), base::TimeDelta(), |
222 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON), | 225 ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON), |
(...skipping 22 matching lines...) Expand all Loading... |
245 | 248 |
246 TEST(EventDispatcherTest, CaptureMultipleMouseButtons) { | 249 TEST(EventDispatcherTest, CaptureMultipleMouseButtons) { |
247 TestServerWindowDelegate window_delegate; | 250 TestServerWindowDelegate window_delegate; |
248 ServerWindow root(&window_delegate, WindowId(1, 2)); | 251 ServerWindow root(&window_delegate, WindowId(1, 2)); |
249 window_delegate.set_root_window(&root); | 252 window_delegate.set_root_window(&root); |
250 root.SetVisible(true); | 253 root.SetVisible(true); |
251 | 254 |
252 ServerWindow child(&window_delegate, WindowId(1, 3)); | 255 ServerWindow child(&window_delegate, WindowId(1, 3)); |
253 root.Add(&child); | 256 root.Add(&child); |
254 child.SetVisible(true); | 257 child.SetVisible(true); |
| 258 EnableHitTest(&child); |
255 | 259 |
256 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 260 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
257 child.SetBounds(gfx::Rect(10, 10, 20, 20)); | 261 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
258 | 262 |
259 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 263 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
260 EventDispatcher dispatcher(&event_dispatcher_delegate); | 264 EventDispatcher dispatcher(&event_dispatcher_delegate); |
261 dispatcher.set_root(&root); | 265 dispatcher.set_root(&root); |
262 | 266 |
263 MouseEventTest tests[] = { | 267 MouseEventTest tests[] = { |
264 // Send a mouse down event over child with a left mouse button | 268 // Send a mouse down event over child with a left mouse button |
(...skipping 27 matching lines...) Expand all Loading... |
292 | 296 |
293 TEST(EventDispatcherTest, ClientAreaGoesToOwner) { | 297 TEST(EventDispatcherTest, ClientAreaGoesToOwner) { |
294 TestServerWindowDelegate window_delegate; | 298 TestServerWindowDelegate window_delegate; |
295 ServerWindow root(&window_delegate, WindowId(1, 2)); | 299 ServerWindow root(&window_delegate, WindowId(1, 2)); |
296 window_delegate.set_root_window(&root); | 300 window_delegate.set_root_window(&root); |
297 root.SetVisible(true); | 301 root.SetVisible(true); |
298 | 302 |
299 ServerWindow child(&window_delegate, WindowId(1, 3)); | 303 ServerWindow child(&window_delegate, WindowId(1, 3)); |
300 root.Add(&child); | 304 root.Add(&child); |
301 child.SetVisible(true); | 305 child.SetVisible(true); |
| 306 EnableHitTest(&child); |
302 | 307 |
303 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 308 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
304 child.SetBounds(gfx::Rect(10, 10, 20, 20)); | 309 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
305 | 310 |
306 child.SetClientArea(gfx::Insets(5, 5, 5, 5)); | 311 child.SetClientArea(gfx::Insets(5, 5, 5, 5)); |
307 | 312 |
308 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 313 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
309 EventDispatcher dispatcher(&event_dispatcher_delegate); | 314 EventDispatcher dispatcher(&event_dispatcher_delegate); |
310 dispatcher.set_root(&root); | 315 dispatcher.set_root(&root); |
311 | 316 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 359 |
355 TEST(EventDispatcherTest, DontFocusOnSecondDown) { | 360 TEST(EventDispatcherTest, DontFocusOnSecondDown) { |
356 TestServerWindowDelegate window_delegate; | 361 TestServerWindowDelegate window_delegate; |
357 ServerWindow root(&window_delegate, WindowId(1, 2)); | 362 ServerWindow root(&window_delegate, WindowId(1, 2)); |
358 window_delegate.set_root_window(&root); | 363 window_delegate.set_root_window(&root); |
359 root.SetVisible(true); | 364 root.SetVisible(true); |
360 | 365 |
361 ServerWindow child1(&window_delegate, WindowId(1, 3)); | 366 ServerWindow child1(&window_delegate, WindowId(1, 3)); |
362 root.Add(&child1); | 367 root.Add(&child1); |
363 child1.SetVisible(true); | 368 child1.SetVisible(true); |
| 369 EnableHitTest(&child1); |
364 | 370 |
365 ServerWindow child2(&window_delegate, WindowId(1, 4)); | 371 ServerWindow child2(&window_delegate, WindowId(1, 4)); |
366 root.Add(&child2); | 372 root.Add(&child2); |
367 child2.SetVisible(true); | 373 child2.SetVisible(true); |
| 374 EnableHitTest(&child2); |
368 | 375 |
369 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 376 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
370 child1.SetBounds(gfx::Rect(10, 10, 20, 20)); | 377 child1.SetBounds(gfx::Rect(10, 10, 20, 20)); |
371 child2.SetBounds(gfx::Rect(50, 51, 11, 12)); | 378 child2.SetBounds(gfx::Rect(50, 51, 11, 12)); |
372 | 379 |
373 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 380 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
374 EventDispatcher dispatcher(&event_dispatcher_delegate); | 381 EventDispatcher dispatcher(&event_dispatcher_delegate); |
375 dispatcher.set_root(&root); | 382 dispatcher.set_root(&root); |
376 | 383 |
377 // Press on child1. First press event should change focus. | 384 // Press on child1. First press event should change focus. |
(...skipping 17 matching lines...) Expand all Loading... |
395 | 402 |
396 TEST(EventDispatcherTest, TwoPointersActive) { | 403 TEST(EventDispatcherTest, TwoPointersActive) { |
397 TestServerWindowDelegate window_delegate; | 404 TestServerWindowDelegate window_delegate; |
398 ServerWindow root(&window_delegate, WindowId(1, 2)); | 405 ServerWindow root(&window_delegate, WindowId(1, 2)); |
399 window_delegate.set_root_window(&root); | 406 window_delegate.set_root_window(&root); |
400 root.SetVisible(true); | 407 root.SetVisible(true); |
401 | 408 |
402 ServerWindow child1(&window_delegate, WindowId(1, 3)); | 409 ServerWindow child1(&window_delegate, WindowId(1, 3)); |
403 root.Add(&child1); | 410 root.Add(&child1); |
404 child1.SetVisible(true); | 411 child1.SetVisible(true); |
| 412 EnableHitTest(&child1); |
405 | 413 |
406 ServerWindow child2(&window_delegate, WindowId(1, 4)); | 414 ServerWindow child2(&window_delegate, WindowId(1, 4)); |
407 root.Add(&child2); | 415 root.Add(&child2); |
408 child2.SetVisible(true); | 416 child2.SetVisible(true); |
| 417 EnableHitTest(&child2); |
409 | 418 |
410 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 419 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
411 child1.SetBounds(gfx::Rect(10, 10, 20, 20)); | 420 child1.SetBounds(gfx::Rect(10, 10, 20, 20)); |
412 child2.SetBounds(gfx::Rect(50, 51, 11, 12)); | 421 child2.SetBounds(gfx::Rect(50, 51, 11, 12)); |
413 | 422 |
414 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 423 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
415 EventDispatcher dispatcher(&event_dispatcher_delegate); | 424 EventDispatcher dispatcher(&event_dispatcher_delegate); |
416 dispatcher.set_root(&root); | 425 dispatcher.set_root(&root); |
417 | 426 |
418 // Press on child1. | 427 // Press on child1. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 TEST(EventDispatcherTest, DestroyWindowWhileGettingEvents) { | 473 TEST(EventDispatcherTest, DestroyWindowWhileGettingEvents) { |
465 TestServerWindowDelegate window_delegate; | 474 TestServerWindowDelegate window_delegate; |
466 ServerWindow root(&window_delegate, WindowId(1, 2)); | 475 ServerWindow root(&window_delegate, WindowId(1, 2)); |
467 window_delegate.set_root_window(&root); | 476 window_delegate.set_root_window(&root); |
468 root.SetVisible(true); | 477 root.SetVisible(true); |
469 | 478 |
470 scoped_ptr<ServerWindow> child( | 479 scoped_ptr<ServerWindow> child( |
471 new ServerWindow(&window_delegate, WindowId(1, 3))); | 480 new ServerWindow(&window_delegate, WindowId(1, 3))); |
472 root.Add(child.get()); | 481 root.Add(child.get()); |
473 child->SetVisible(true); | 482 child->SetVisible(true); |
| 483 EnableHitTest(child.get()); |
474 | 484 |
475 root.SetBounds(gfx::Rect(0, 0, 100, 100)); | 485 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
476 child->SetBounds(gfx::Rect(10, 10, 20, 20)); | 486 child->SetBounds(gfx::Rect(10, 10, 20, 20)); |
477 | 487 |
478 TestEventDispatcherDelegate event_dispatcher_delegate(&root); | 488 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
479 EventDispatcher dispatcher(&event_dispatcher_delegate); | 489 EventDispatcher dispatcher(&event_dispatcher_delegate); |
480 dispatcher.set_root(&root); | 490 dispatcher.set_root(&root); |
481 | 491 |
482 // Press on child. | 492 // Press on child. |
483 const ui::TouchEvent touch_event1(ui::ET_TOUCH_PRESSED, gfx::Point(12, 13), 1, | 493 const ui::TouchEvent touch_event1(ui::ET_TOUCH_PRESSED, gfx::Point(12, 13), 1, |
484 base::TimeDelta()); | 494 base::TimeDelta()); |
485 dispatcher.OnEvent( | 495 dispatcher.OnEvent( |
486 mojom::Event::From(static_cast<const ui::Event&>(touch_event1))); | 496 mojom::Event::From(static_cast<const ui::Event&>(touch_event1))); |
487 EXPECT_EQ(child.get(), event_dispatcher_delegate.GetAndClearLastTarget()); | 497 EXPECT_EQ(child.get(), event_dispatcher_delegate.GetAndClearLastTarget()); |
488 | 498 |
489 event_dispatcher_delegate.GetAndClearLastDispatchedEvent(); | 499 event_dispatcher_delegate.GetAndClearLastDispatchedEvent(); |
490 | 500 |
491 // Delete child, and continue the drag. Event should not be dispatched. | 501 // Delete child, and continue the drag. Event should not be dispatched. |
492 child.reset(); | 502 child.reset(); |
493 | 503 |
494 const ui::TouchEvent drag_event1(ui::ET_TOUCH_MOVED, gfx::Point(53, 54), 1, | 504 const ui::TouchEvent drag_event1(ui::ET_TOUCH_MOVED, gfx::Point(53, 54), 1, |
495 base::TimeDelta()); | 505 base::TimeDelta()); |
496 dispatcher.OnEvent( | 506 dispatcher.OnEvent( |
497 mojom::Event::From(static_cast<const ui::Event&>(drag_event1))); | 507 mojom::Event::From(static_cast<const ui::Event&>(drag_event1))); |
498 EXPECT_EQ(nullptr, event_dispatcher_delegate.GetAndClearLastTarget()); | 508 EXPECT_EQ(nullptr, event_dispatcher_delegate.GetAndClearLastTarget()); |
499 EXPECT_EQ(nullptr, | 509 EXPECT_EQ(nullptr, |
500 event_dispatcher_delegate.GetAndClearLastDispatchedEvent().get()); | 510 event_dispatcher_delegate.GetAndClearLastDispatchedEvent().get()); |
501 } | 511 } |
502 | 512 |
| 513 TEST(EventDispatcherTest, MouseInExtendedHitTestRegion) { |
| 514 TestServerWindowDelegate window_delegate; |
| 515 ServerWindow root(&window_delegate, WindowId(1, 2)); |
| 516 window_delegate.set_root_window(&root); |
| 517 root.SetVisible(true); |
| 518 |
| 519 ServerWindow child(&window_delegate, WindowId(1, 3)); |
| 520 root.Add(&child); |
| 521 child.SetVisible(true); |
| 522 EnableHitTest(&child); |
| 523 |
| 524 root.SetBounds(gfx::Rect(0, 0, 100, 100)); |
| 525 child.SetBounds(gfx::Rect(10, 10, 20, 20)); |
| 526 |
| 527 TestEventDispatcherDelegate event_dispatcher_delegate(&root); |
| 528 EventDispatcher dispatcher(&event_dispatcher_delegate); |
| 529 dispatcher.set_root(&root); |
| 530 |
| 531 // Send event that is not over child. |
| 532 const ui::MouseEvent ui_event( |
| 533 ui::ET_MOUSE_PRESSED, gfx::Point(8, 9), gfx::Point(8, 9), |
| 534 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 535 dispatcher.OnEvent( |
| 536 mojom::Event::From(static_cast<const ui::Event&>(ui_event))); |
| 537 ASSERT_EQ(&root, event_dispatcher_delegate.last_target()); |
| 538 event_dispatcher_delegate.GetAndClearLastDispatchedEvent(); |
| 539 |
| 540 // Release the mouse. |
| 541 const ui::MouseEvent release_event( |
| 542 ui::ET_MOUSE_RELEASED, gfx::Point(8, 9), gfx::Point(8, 9), |
| 543 base::TimeDelta(), ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON); |
| 544 dispatcher.OnEvent( |
| 545 mojom::Event::From(static_cast<const ui::Event&>(release_event))); |
| 546 |
| 547 // The event should not have been dispatched to the delegate. |
| 548 ASSERT_EQ(&root, event_dispatcher_delegate.last_target()); |
| 549 EXPECT_TRUE(event_dispatcher_delegate.GetAndClearLastDispatchedEvent()); |
| 550 EXPECT_FALSE(event_dispatcher_delegate.GetAndClearLastInNonclientArea()); |
| 551 |
| 552 // Change the extended hit test region and send event in extended hit test |
| 553 // region. |
| 554 child.set_extended_hit_test_region(gfx::Insets(5, 5, 5, 5)); |
| 555 dispatcher.OnEvent( |
| 556 mojom::Event::From(static_cast<const ui::Event&>(ui_event))); |
| 557 ASSERT_EQ(&child, event_dispatcher_delegate.last_target()); |
| 558 EXPECT_TRUE(event_dispatcher_delegate.GetAndClearLastInNonclientArea()); |
| 559 mojom::EventPtr dispatched_event_mojo = |
| 560 event_dispatcher_delegate.GetAndClearLastDispatchedEvent(); |
| 561 ASSERT_TRUE(dispatched_event_mojo.get()); |
| 562 scoped_ptr<ui::Event> dispatched_event( |
| 563 dispatched_event_mojo.To<scoped_ptr<ui::Event>>()); |
| 564 ASSERT_TRUE(dispatched_event.get()); |
| 565 ASSERT_TRUE(dispatched_event->IsMouseEvent()); |
| 566 ui::MouseEvent* dispatched_mouse_event = |
| 567 static_cast<ui::MouseEvent*>(dispatched_event.get()); |
| 568 EXPECT_EQ(gfx::Point(-2, -1), dispatched_mouse_event->location()); |
| 569 } |
| 570 |
503 } // namespace ws | 571 } // namespace ws |
504 } // namespace mus | 572 } // namespace mus |
OLD | NEW |