forked from kiwibrowser/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_event_dispatcher_unittest.cc
3267 lines (2750 loc) · 123 KB
/
window_event_dispatcher_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/aura/window_event_dispatcher.h"
#include <stddef.h>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/test/histogram_tester.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "services/ui/public/interfaces/window_tree_constants.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/event_client.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/env.h"
#include "ui/aura/mus/window_tree_client.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/env_test_helper.h"
#include "ui/aura/test/test_cursor_client.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/aura/window_targeter.h"
#include "ui/aura/window_tracker.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/events/event_handler.h"
#include "ui/events/event_utils.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/test_event_handler.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/transform.h"
namespace aura {
namespace {
// A delegate that always returns a non-client component for hit tests.
class NonClientDelegate : public test::TestWindowDelegate {
public:
NonClientDelegate()
: non_client_count_(0),
mouse_event_count_(0),
mouse_event_flags_(0x0) {
}
~NonClientDelegate() override {}
int non_client_count() const { return non_client_count_; }
const gfx::Point& non_client_location() const { return non_client_location_; }
int mouse_event_count() const { return mouse_event_count_; }
const gfx::Point& mouse_event_location() const {
return mouse_event_location_;
}
int mouse_event_flags() const { return mouse_event_flags_; }
int GetNonClientComponent(const gfx::Point& location) const override {
NonClientDelegate* self = const_cast<NonClientDelegate*>(this);
self->non_client_count_++;
self->non_client_location_ = location;
return HTTOPLEFT;
}
void OnMouseEvent(ui::MouseEvent* event) override {
mouse_event_count_++;
mouse_event_location_ = event->location();
mouse_event_flags_ = event->flags();
event->SetHandled();
}
private:
int non_client_count_;
gfx::Point non_client_location_;
int mouse_event_count_;
gfx::Point mouse_event_location_;
int mouse_event_flags_;
DISALLOW_COPY_AND_ASSIGN(NonClientDelegate);
};
// A simple event handler that consumes key events.
class ConsumeKeyHandler : public ui::test::TestEventHandler {
public:
ConsumeKeyHandler() {}
~ConsumeKeyHandler() override {}
// Overridden from ui::EventHandler:
void OnKeyEvent(ui::KeyEvent* event) override {
ui::test::TestEventHandler::OnKeyEvent(event);
event->StopPropagation();
}
private:
DISALLOW_COPY_AND_ASSIGN(ConsumeKeyHandler);
};
bool IsFocusedWindow(aura::Window* window) {
return client::GetFocusClient(window)->GetFocusedWindow() == window;
}
} // namespace
using WindowEventDispatcherTest = test::AuraTestBaseWithType;
TEST_P(WindowEventDispatcherTest, OnHostMouseEvent) {
// Create two non-overlapping windows so we don't have to worry about which
// is on top.
std::unique_ptr<NonClientDelegate> delegate1(new NonClientDelegate());
std::unique_ptr<NonClientDelegate> delegate2(new NonClientDelegate());
const int kWindowWidth = 123;
const int kWindowHeight = 45;
gfx::Rect bounds1(100, 200, kWindowWidth, kWindowHeight);
gfx::Rect bounds2(300, 400, kWindowWidth, kWindowHeight);
std::unique_ptr<aura::Window> window1(CreateTestWindowWithDelegate(
delegate1.get(), -1234, bounds1, root_window()));
std::unique_ptr<aura::Window> window2(CreateTestWindowWithDelegate(
delegate2.get(), -5678, bounds2, root_window()));
// Send a mouse event to window1.
gfx::Point point(101, 201);
ui::MouseEvent event1(ui::ET_MOUSE_PRESSED, point, point,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
DispatchEventUsingWindowDispatcher(&event1);
// Event was tested for non-client area for the target window.
EXPECT_EQ(1, delegate1->non_client_count());
EXPECT_EQ(0, delegate2->non_client_count());
// The non-client component test was in local coordinates.
EXPECT_EQ(gfx::Point(1, 1), delegate1->non_client_location());
// Mouse event was received by target window.
EXPECT_EQ(1, delegate1->mouse_event_count());
EXPECT_EQ(0, delegate2->mouse_event_count());
// Event was in local coordinates.
EXPECT_EQ(gfx::Point(1, 1), delegate1->mouse_event_location());
// Non-client flag was set.
EXPECT_TRUE(delegate1->mouse_event_flags() & ui::EF_IS_NON_CLIENT);
}
TEST_P(WindowEventDispatcherTest, RepostEvent) {
// Test RepostEvent in RootWindow. It only works for Mouse Press and touch
// press.
EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
gfx::Point point(10, 10);
ui::MouseEvent event(ui::ET_MOUSE_PRESSED, point, point,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
host()->dispatcher()->RepostEvent(&event);
RunAllPendingInMessageLoop();
EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
ui::TouchEvent touch_pressed_event(
ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
host()->dispatcher()->RepostEvent(&touch_pressed_event);
RunAllPendingInMessageLoop();
EXPECT_TRUE(Env::GetInstance()->is_touch_down());
}
// Check that we correctly track whether any touch devices are down in response
// to touch press and release events with two WindowTreeHost.
TEST_P(WindowEventDispatcherTest, TouchDownState) {
std::unique_ptr<WindowTreeHost> second_host(
WindowTreeHost::Create(gfx::Rect(20, 30, 100, 50)));
second_host->InitHost();
second_host->window()->Show();
ui::TouchEvent touch_pressed_event1(
ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
ui::TouchEvent touch_pressed_event2(
ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 2));
ui::TouchEvent touch_released_event1(
ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
ui::TouchEvent touch_released_event2(
ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 2));
EXPECT_FALSE(Env::GetInstance()->is_touch_down());
host()->dispatcher()->OnEventFromSource(&touch_pressed_event1);
EXPECT_TRUE(Env::GetInstance()->is_touch_down());
second_host->dispatcher()->OnEventFromSource(&touch_pressed_event2);
EXPECT_TRUE(Env::GetInstance()->is_touch_down());
host()->dispatcher()->OnEventFromSource(&touch_released_event1);
EXPECT_TRUE(Env::GetInstance()->is_touch_down());
second_host->dispatcher()->OnEventFromSource(&touch_released_event2);
EXPECT_FALSE(Env::GetInstance()->is_touch_down());
}
// Check that we correctly track the state of the mouse buttons in response to
// button press and release events.
TEST_P(WindowEventDispatcherTest, MouseButtonState) {
EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
gfx::Point location;
std::unique_ptr<ui::MouseEvent> event;
// Press the left button.
event.reset(new ui::MouseEvent(
ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
DispatchEventUsingWindowDispatcher(event.get());
EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
// Additionally press the right.
event.reset(new ui::MouseEvent(
ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON,
ui::EF_RIGHT_MOUSE_BUTTON));
DispatchEventUsingWindowDispatcher(event.get());
EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
// Release the left button.
event.reset(new ui::MouseEvent(
ui::ET_MOUSE_RELEASED, location, location, ui::EventTimeForNow(),
ui::EF_RIGHT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON));
DispatchEventUsingWindowDispatcher(event.get());
EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
// Release the right button. We should ignore the Shift-is-down flag.
event.reset(new ui::MouseEvent(ui::ET_MOUSE_RELEASED, location, location,
ui::EventTimeForNow(), ui::EF_SHIFT_DOWN,
ui::EF_RIGHT_MOUSE_BUTTON));
DispatchEventUsingWindowDispatcher(event.get());
EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
// Press the middle button.
event.reset(new ui::MouseEvent(
ui::ET_MOUSE_PRESSED, location, location, ui::EventTimeForNow(),
ui::EF_MIDDLE_MOUSE_BUTTON, ui::EF_MIDDLE_MOUSE_BUTTON));
DispatchEventUsingWindowDispatcher(event.get());
EXPECT_TRUE(Env::GetInstance()->IsMouseButtonDown());
}
TEST_P(WindowEventDispatcherTest, TranslatedEvent) {
std::unique_ptr<Window> w1(test::CreateTestWindowWithDelegate(
NULL, 1, gfx::Rect(50, 50, 100, 100), root_window()));
gfx::Point origin(100, 100);
ui::MouseEvent root(ui::ET_MOUSE_PRESSED, origin, origin,
ui::EventTimeForNow(), 0, 0);
EXPECT_EQ("100,100", root.location().ToString());
EXPECT_EQ("100,100", root.root_location().ToString());
ui::MouseEvent translated_event(
root, static_cast<Window*>(root_window()), w1.get(),
ui::ET_MOUSE_ENTERED, root.flags());
EXPECT_EQ("50,50", translated_event.location().ToString());
EXPECT_EQ("100,100", translated_event.root_location().ToString());
}
namespace {
class TestEventClient : public client::EventClient {
public:
static const int kNonLockWindowId = 100;
static const int kLockWindowId = 200;
explicit TestEventClient(Window* root_window)
: root_window_(root_window),
lock_(false) {
client::SetEventClient(root_window_, this);
Window* lock_window =
test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
lock_window->set_id(kLockWindowId);
Window* non_lock_window =
test::CreateTestWindowWithBounds(root_window_->bounds(), root_window_);
non_lock_window->set_id(kNonLockWindowId);
}
~TestEventClient() override { client::SetEventClient(root_window_, NULL); }
// Starts/stops locking. Locking prevents windows other than those inside
// the lock container from receiving events, getting focus etc.
void Lock() {
lock_ = true;
}
void Unlock() {
lock_ = false;
}
Window* GetLockWindow() {
return const_cast<Window*>(
static_cast<const TestEventClient*>(this)->GetLockWindow());
}
const Window* GetLockWindow() const {
return root_window_->GetChildById(kLockWindowId);
}
Window* GetNonLockWindow() {
return root_window_->GetChildById(kNonLockWindowId);
}
private:
// Overridden from client::EventClient:
bool CanProcessEventsWithinSubtree(const Window* window) const override {
return lock_ ?
window->Contains(GetLockWindow()) || GetLockWindow()->Contains(window) :
true;
}
ui::EventTarget* GetToplevelEventTarget() override { return NULL; }
Window* root_window_;
bool lock_;
DISALLOW_COPY_AND_ASSIGN(TestEventClient);
};
} // namespace
TEST_P(WindowEventDispatcherTest, CanProcessEventsWithinSubtree) {
TestEventClient client(root_window());
test::TestWindowDelegate d;
ui::test::TestEventHandler nonlock_ef;
ui::test::TestEventHandler lock_ef;
client.GetNonLockWindow()->AddPreTargetHandler(&nonlock_ef);
client.GetLockWindow()->AddPreTargetHandler(&lock_ef);
Window* w1 = test::CreateTestWindowWithBounds(gfx::Rect(10, 10, 20, 20),
client.GetNonLockWindow());
w1->set_id(1);
Window* w2 = test::CreateTestWindowWithBounds(gfx::Rect(30, 30, 20, 20),
client.GetNonLockWindow());
w2->set_id(2);
std::unique_ptr<Window> w3(test::CreateTestWindowWithDelegate(
&d, 3, gfx::Rect(30, 30, 20, 20), client.GetLockWindow()));
w1->Focus();
EXPECT_TRUE(IsFocusedWindow(w1));
client.Lock();
// Since we're locked, the attempt to focus w2 will be ignored.
w2->Focus();
EXPECT_TRUE(IsFocusedWindow(w1));
EXPECT_FALSE(IsFocusedWindow(w2));
{
// Attempting to send a key event to w1 (not in the lock container) should
// cause focus to be reset.
ui::test::EventGenerator generator(root_window());
generator.PressKey(ui::VKEY_SPACE, 0);
EXPECT_EQ(NULL, client::GetFocusClient(w1)->GetFocusedWindow());
EXPECT_FALSE(IsFocusedWindow(w1));
}
{
// Events sent to a window not in the lock container will not be processed.
// i.e. never sent to the non-lock container's event filter.
ui::test::EventGenerator generator(root_window(), w1);
generator.ClickLeftButton();
EXPECT_EQ(0, nonlock_ef.num_mouse_events());
// Events sent to a window in the lock container will be processed.
ui::test::EventGenerator generator3(root_window(), w3.get());
generator3.PressLeftButton();
EXPECT_EQ(1, lock_ef.num_mouse_events());
}
// Prevent w3 from being deleted by the hierarchy since its delegate is owned
// by this scope.
w3->parent()->RemoveChild(w3.get());
}
TEST_P(WindowEventDispatcherTest, DontIgnoreUnknownKeys) {
ConsumeKeyHandler handler;
root_window()->AddPreTargetHandler(&handler);
ui::KeyEvent unknown_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN, ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&unknown_event);
EXPECT_TRUE(unknown_event.handled());
EXPECT_EQ(1, handler.num_key_events());
handler.Reset();
ui::KeyEvent known_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&known_event);
EXPECT_TRUE(known_event.handled());
EXPECT_EQ(1, handler.num_key_events());
handler.Reset();
ui::KeyEvent ime_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
ui::EF_IME_FABRICATED_KEY);
DispatchEventUsingWindowDispatcher(&ime_event);
EXPECT_TRUE(ime_event.handled());
EXPECT_EQ(1, handler.num_key_events());
handler.Reset();
ui::KeyEvent unknown_key_with_char_event(ui::ET_KEY_PRESSED, ui::VKEY_UNKNOWN,
ui::EF_NONE);
unknown_key_with_char_event.set_character(0x00e4 /* "ä" */);
DispatchEventUsingWindowDispatcher(&unknown_key_with_char_event);
EXPECT_TRUE(unknown_key_with_char_event.handled());
EXPECT_EQ(1, handler.num_key_events());
root_window()->RemovePreTargetHandler(&handler);
}
TEST_P(WindowEventDispatcherTest, NoDelegateWindowReceivesKeyEvents) {
std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), NULL));
w1->Show();
w1->Focus();
ui::test::TestEventHandler handler;
w1->AddPreTargetHandler(&handler);
ui::KeyEvent key_press(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE);
DispatchEventUsingWindowDispatcher(&key_press);
EXPECT_TRUE(key_press.handled());
EXPECT_EQ(1, handler.num_key_events());
w1->RemovePreTargetHandler(&handler);
}
// Tests that touch-events that are beyond the bounds of the root-window do get
// propagated to the event filters correctly with the root as the target.
TEST_P(WindowEventDispatcherTest, TouchEventsOutsideBounds) {
ui::test::TestEventHandler handler;
root_window()->AddPreTargetHandler(&handler);
gfx::Point position = root_window()->bounds().origin();
position.Offset(-10, -10);
ui::TouchEvent press(
ui::ET_TOUCH_PRESSED, position, ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
DispatchEventUsingWindowDispatcher(&press);
EXPECT_EQ(1, handler.num_touch_events());
position = root_window()->bounds().origin();
position.Offset(root_window()->bounds().width() + 10,
root_window()->bounds().height() + 10);
ui::TouchEvent release(
ui::ET_TOUCH_RELEASED, position, ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
DispatchEventUsingWindowDispatcher(&release);
EXPECT_EQ(2, handler.num_touch_events());
root_window()->RemovePreTargetHandler(&handler);
}
// Tests that scroll events are dispatched correctly.
TEST_P(WindowEventDispatcherTest, ScrollEventDispatch) {
base::TimeTicks now = ui::EventTimeForNow();
ui::test::TestEventHandler handler;
root_window()->AddPreTargetHandler(&handler);
test::TestWindowDelegate delegate;
std::unique_ptr<Window> w1(CreateNormalWindow(1, root_window(), &delegate));
w1->SetBounds(gfx::Rect(20, 20, 40, 40));
// A scroll event on the root-window itself is dispatched.
ui::ScrollEvent scroll1(ui::ET_SCROLL,
gfx::Point(10, 10),
now,
0,
0, -10,
0, -10,
2);
DispatchEventUsingWindowDispatcher(&scroll1);
EXPECT_EQ(1, handler.num_scroll_events());
// Scroll event on a window should be dispatched properly.
ui::ScrollEvent scroll2(ui::ET_SCROLL,
gfx::Point(25, 30),
now,
0,
-10, 0,
-10, 0,
2);
DispatchEventUsingWindowDispatcher(&scroll2);
EXPECT_EQ(2, handler.num_scroll_events());
root_window()->RemovePreTargetHandler(&handler);
}
namespace {
// ui::EventHandler that tracks the types of events it's seen.
class EventFilterRecorder : public ui::EventHandler {
public:
typedef std::vector<ui::EventType> Events;
typedef std::vector<gfx::Point> EventLocations;
typedef std::vector<int> EventFlags;
EventFilterRecorder()
: wait_until_event_(ui::ET_UNKNOWN),
last_touch_may_cause_scrolling_(false) {
}
const Events& events() const { return events_; }
const EventLocations& mouse_locations() const { return mouse_locations_; }
gfx::Point mouse_location(int i) const { return mouse_locations_[i]; }
const EventLocations& touch_locations() const { return touch_locations_; }
const EventLocations& gesture_locations() const { return gesture_locations_; }
const EventFlags& mouse_event_flags() const { return mouse_event_flags_; }
void WaitUntilReceivedEvent(ui::EventType type) {
wait_until_event_ = type;
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
}
Events GetAndResetEvents() {
Events events = events_;
Reset();
return events;
}
void Reset() {
events_.clear();
mouse_locations_.clear();
touch_locations_.clear();
gesture_locations_.clear();
mouse_event_flags_.clear();
last_touch_may_cause_scrolling_ = false;
}
// ui::EventHandler overrides:
void OnEvent(ui::Event* event) override {
ui::EventHandler::OnEvent(event);
events_.push_back(event->type());
if (wait_until_event_ == event->type() && run_loop_) {
run_loop_->Quit();
wait_until_event_ = ui::ET_UNKNOWN;
}
}
void OnMouseEvent(ui::MouseEvent* event) override {
mouse_locations_.push_back(event->location());
mouse_event_flags_.push_back(event->flags());
}
void OnTouchEvent(ui::TouchEvent* event) override {
touch_locations_.push_back(event->location());
last_touch_may_cause_scrolling_ = event->may_cause_scrolling();
}
void OnGestureEvent(ui::GestureEvent* event) override {
gesture_locations_.push_back(event->location());
}
bool HasReceivedEvent(ui::EventType type) {
return base::ContainsValue(events_, type);
}
bool LastTouchMayCauseScrolling() const {
return last_touch_may_cause_scrolling_;
}
private:
std::unique_ptr<base::RunLoop> run_loop_;
ui::EventType wait_until_event_;
Events events_;
EventLocations mouse_locations_;
EventLocations touch_locations_;
EventLocations gesture_locations_;
EventFlags mouse_event_flags_;
bool last_touch_may_cause_scrolling_;
DISALLOW_COPY_AND_ASSIGN(EventFilterRecorder);
};
// Converts an EventType to a string.
std::string EventTypeToString(ui::EventType type) {
switch (type) {
case ui::ET_TOUCH_RELEASED:
return "TOUCH_RELEASED";
case ui::ET_TOUCH_CANCELLED:
return "TOUCH_CANCELLED";
case ui::ET_TOUCH_PRESSED:
return "TOUCH_PRESSED";
case ui::ET_TOUCH_MOVED:
return "TOUCH_MOVED";
case ui::ET_MOUSE_PRESSED:
return "MOUSE_PRESSED";
case ui::ET_MOUSE_DRAGGED:
return "MOUSE_DRAGGED";
case ui::ET_MOUSE_RELEASED:
return "MOUSE_RELEASED";
case ui::ET_MOUSE_MOVED:
return "MOUSE_MOVED";
case ui::ET_MOUSE_ENTERED:
return "MOUSE_ENTERED";
case ui::ET_MOUSE_EXITED:
return "MOUSE_EXITED";
case ui::ET_GESTURE_SCROLL_BEGIN:
return "GESTURE_SCROLL_BEGIN";
case ui::ET_GESTURE_SCROLL_END:
return "GESTURE_SCROLL_END";
case ui::ET_GESTURE_SCROLL_UPDATE:
return "GESTURE_SCROLL_UPDATE";
case ui::ET_GESTURE_PINCH_BEGIN:
return "GESTURE_PINCH_BEGIN";
case ui::ET_GESTURE_PINCH_END:
return "GESTURE_PINCH_END";
case ui::ET_GESTURE_PINCH_UPDATE:
return "GESTURE_PINCH_UPDATE";
case ui::ET_GESTURE_TAP:
return "GESTURE_TAP";
case ui::ET_GESTURE_TAP_DOWN:
return "GESTURE_TAP_DOWN";
case ui::ET_GESTURE_TAP_CANCEL:
return "GESTURE_TAP_CANCEL";
case ui::ET_GESTURE_SHOW_PRESS:
return "GESTURE_SHOW_PRESS";
case ui::ET_GESTURE_BEGIN:
return "GESTURE_BEGIN";
case ui::ET_GESTURE_END:
return "GESTURE_END";
default:
// We should explicitly require each event type.
NOTREACHED() << "Received unexpected event: " << type;
break;
}
return "";
}
std::string EventTypesToString(const EventFilterRecorder::Events& events) {
std::string result;
for (size_t i = 0; i < events.size(); ++i) {
if (i != 0)
result += " ";
result += EventTypeToString(events[i]);
}
return result;
}
} // namespace
#if defined(OS_WIN) && defined(ARCH_CPU_X86)
#define MAYBE(x) DISABLED_##x
#else
#define MAYBE(x) x
#endif
// Verifies a repost mouse event targets the window with capture (if there is
// one).
// Flaky on 32-bit Windows bots. http://crbug.com/388290
TEST_P(WindowEventDispatcherTest, MAYBE(RepostTargetsCaptureWindow)) {
// Set capture on |window| generate a mouse event (that is reposted) and not
// over |window| and verify |window| gets it (|window| gets it because it has
// capture).
EXPECT_FALSE(Env::GetInstance()->IsMouseButtonDown());
EventFilterRecorder recorder;
std::unique_ptr<Window> window(CreateNormalWindow(1, root_window(), NULL));
window->SetBounds(gfx::Rect(20, 20, 40, 30));
window->AddPreTargetHandler(&recorder);
window->SetCapture();
const ui::MouseEvent press_event(
ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
host()->dispatcher()->RepostEvent(&press_event);
RunAllPendingInMessageLoop(); // Necessitated by RepostEvent().
// Mouse moves/enters may be generated. We only care about a pressed.
EXPECT_TRUE(EventTypesToString(recorder.events()).find("MOUSE_PRESSED") !=
std::string::npos) << EventTypesToString(recorder.events());
}
TEST_P(WindowEventDispatcherTest, MouseMovesHeld) {
EventFilterRecorder recorder;
root_window()->AddPreTargetHandler(&recorder);
test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(0, 0, 100, 100), root_window()));
ui::MouseEvent mouse_move_event(ui::ET_MOUSE_MOVED, gfx::Point(0, 0),
gfx::Point(0, 0), ui::EventTimeForNow(), 0,
0);
DispatchEventUsingWindowDispatcher(&mouse_move_event);
// Discard MOUSE_ENTER.
recorder.Reset();
host()->dispatcher()->HoldPointerMoves();
// Check that we don't immediately dispatch the MOUSE_DRAGGED event.
ui::MouseEvent mouse_dragged_event(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0),
gfx::Point(0, 0), ui::EventTimeForNow(), 0,
0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
EXPECT_TRUE(recorder.events().empty());
// Check that we do dispatch the held MOUSE_DRAGGED event before another type
// of event.
ui::MouseEvent mouse_pressed_event(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0),
gfx::Point(0, 0), ui::EventTimeForNow(), 0,
0);
DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
EventTypesToString(recorder.events()));
recorder.Reset();
// Check that we coalesce held MOUSE_DRAGGED events. Note that here (and
// elsewhere in this test) we re-define each event prior to dispatch so that
// it has the correct state (phase, handled, target, etc.).
mouse_dragged_event =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
ui::MouseEvent mouse_dragged_event2(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(),
0, 0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
EXPECT_TRUE(recorder.events().empty());
mouse_pressed_event =
ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
EventTypesToString(recorder.events()));
recorder.Reset();
// Check that on ReleasePointerMoves, held events are not dispatched
// immediately, but posted instead.
mouse_dragged_event =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
host()->dispatcher()->ReleasePointerMoves();
EXPECT_TRUE(recorder.events().empty());
RunAllPendingInMessageLoop();
EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
recorder.Reset();
// However if another message comes in before the dispatch of the posted
// event, check that the posted event is dispatched before this new event.
host()->dispatcher()->HoldPointerMoves();
mouse_dragged_event =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
host()->dispatcher()->ReleasePointerMoves();
mouse_pressed_event =
ui::MouseEvent(ui::ET_MOUSE_PRESSED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_pressed_event);
EXPECT_EQ("MOUSE_DRAGGED MOUSE_PRESSED",
EventTypesToString(recorder.events()));
recorder.Reset();
RunAllPendingInMessageLoop();
EXPECT_TRUE(recorder.events().empty());
// Check that if the other message is another MOUSE_DRAGGED, we still coalesce
// them.
host()->dispatcher()->HoldPointerMoves();
mouse_dragged_event =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
host()->dispatcher()->ReleasePointerMoves();
mouse_dragged_event2 =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
EXPECT_EQ("MOUSE_DRAGGED", EventTypesToString(recorder.events()));
recorder.Reset();
RunAllPendingInMessageLoop();
EXPECT_TRUE(recorder.events().empty());
// Check that synthetic mouse move event has a right location when issued
// while holding pointer moves.
mouse_dragged_event =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(0, 0), gfx::Point(0, 0),
ui::EventTimeForNow(), 0, 0);
mouse_dragged_event2 =
ui::MouseEvent(ui::ET_MOUSE_DRAGGED, gfx::Point(10, 10),
gfx::Point(10, 10), ui::EventTimeForNow(), 0, 0);
ui::MouseEvent mouse_dragged_event3(ui::ET_MOUSE_DRAGGED, gfx::Point(28, 28),
gfx::Point(28, 28), ui::EventTimeForNow(),
0, 0);
host()->dispatcher()->HoldPointerMoves();
DispatchEventUsingWindowDispatcher(&mouse_dragged_event);
DispatchEventUsingWindowDispatcher(&mouse_dragged_event2);
window->SetBounds(gfx::Rect(15, 15, 80, 80));
DispatchEventUsingWindowDispatcher(&mouse_dragged_event3);
RunAllPendingInMessageLoop();
EXPECT_TRUE(recorder.events().empty());
host()->dispatcher()->ReleasePointerMoves();
RunAllPendingInMessageLoop();
EXPECT_EQ("MOUSE_MOVED", EventTypesToString(recorder.events()));
EXPECT_EQ(gfx::Point(13, 13), recorder.mouse_location(0));
recorder.Reset();
root_window()->RemovePreTargetHandler(&recorder);
}
TEST_P(WindowEventDispatcherTest, TouchMovesHeld) {
EventFilterRecorder recorder;
root_window()->AddPreTargetHandler(&recorder);
test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
// Starting the touch and throwing out the first few events, since the system
// is going to generate synthetic mouse events that are not relevant to the
// test.
ui::TouchEvent touch_pressed_event(
ui::ET_TOUCH_PRESSED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
DispatchEventUsingWindowDispatcher(&touch_pressed_event);
recorder.WaitUntilReceivedEvent(ui::ET_GESTURE_SHOW_PRESS);
recorder.Reset();
host()->dispatcher()->HoldPointerMoves();
// Check that we don't immediately dispatch the TOUCH_MOVED event.
ui::TouchEvent touch_moved_event(
ui::ET_TOUCH_MOVED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
ui::TouchEvent touch_moved_event2(
ui::ET_TOUCH_MOVED, gfx::Point(11, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
ui::TouchEvent touch_moved_event3(
ui::ET_TOUCH_MOVED, gfx::Point(12, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
DispatchEventUsingWindowDispatcher(&touch_moved_event);
EXPECT_TRUE(recorder.events().empty());
// Check that on ReleasePointerMoves, held events are not dispatched
// immediately, but posted instead.
DispatchEventUsingWindowDispatcher(&touch_moved_event2);
host()->dispatcher()->ReleasePointerMoves();
EXPECT_TRUE(recorder.events().empty());
RunAllPendingInMessageLoop();
EXPECT_EQ("TOUCH_MOVED", EventTypesToString(recorder.events()));
recorder.Reset();
// If another touch event occurs then the held touch should be dispatched
// immediately before it.
ui::TouchEvent touch_released_event(
ui::ET_TOUCH_RELEASED, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
recorder.Reset();
host()->dispatcher()->HoldPointerMoves();
DispatchEventUsingWindowDispatcher(&touch_moved_event3);
DispatchEventUsingWindowDispatcher(&touch_released_event);
EXPECT_EQ("TOUCH_MOVED TOUCH_RELEASED GESTURE_TAP GESTURE_END",
EventTypesToString(recorder.events()));
recorder.Reset();
host()->dispatcher()->ReleasePointerMoves();
RunAllPendingInMessageLoop();
EXPECT_TRUE(recorder.events().empty());
root_window()->RemovePreTargetHandler(&recorder);
}
// Tests that mouse move event has a right location
// when there isn't the target window
TEST_P(WindowEventDispatcherTest, MouseEventWithoutTargetWindow) {
EventFilterRecorder recorder_first;
EventFilterRecorder recorder_second;
test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window_first(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(20, 10, 10, 20), root_window()));
window_first->Show();
window_first->AddPreTargetHandler(&recorder_first);
std::unique_ptr<aura::Window> window_second(CreateTestWindowWithDelegate(
&delegate, 2, gfx::Rect(20, 30, 10, 20), root_window()));
window_second->Show();
window_second->AddPreTargetHandler(&recorder_second);
const gfx::Point event_location(22, 33);
ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse);
EXPECT_TRUE(recorder_first.events().empty());
EXPECT_EQ("MOUSE_ENTERED MOUSE_MOVED",
EventTypesToString(recorder_second.events()));
ASSERT_EQ(2u, recorder_second.mouse_locations().size());
EXPECT_EQ(gfx::Point(2, 3).ToString(),
recorder_second.mouse_locations()[0].ToString());
}
// Tests that a mouse exit is dispatched to the last mouse location when
// the window is hiddden.
TEST_P(WindowEventDispatcherTest, DispatchMouseExitWhenHidingWindow) {
EventFilterRecorder recorder;
test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(10, 10, 50, 50), root_window()));
window->Show();
window->AddPreTargetHandler(&recorder);
// Dispatch a mouse move event into the window.
const gfx::Point event_location(22, 33);
ui::MouseEvent mouse(ui::ET_MOUSE_MOVED, event_location, event_location,
ui::EventTimeForNow(), 0, 0);
DispatchEventUsingWindowDispatcher(&mouse);
EXPECT_FALSE(recorder.events().empty());
recorder.Reset();
// Hide the window and verify a mouse exit event's location.
window->Hide();
EXPECT_FALSE(recorder.events().empty());
EXPECT_EQ("MOUSE_EXITED", EventTypesToString(recorder.events()));
ASSERT_EQ(1u, recorder.mouse_locations().size());
EXPECT_EQ(gfx::Point(12, 23).ToString(),
recorder.mouse_locations()[0].ToString());
}
// Verifies that a direct call to ProcessedTouchEvent() does not cause a crash.
TEST_P(WindowEventDispatcherTest, CallToProcessedTouchEvent) {
test::TestWindowDelegate delegate;
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
host()->dispatcher()->ProcessedTouchEvent(
0, window.get(), ui::ER_UNHANDLED,
false /* is_source_touch_event_set_non_blocking */);
}
// This event handler requests the dispatcher to start holding pointer-move
// events when it receives the first scroll-update gesture.
class HoldPointerOnScrollHandler : public ui::test::TestEventHandler {
public:
HoldPointerOnScrollHandler(WindowEventDispatcher* dispatcher,
EventFilterRecorder* filter)
: dispatcher_(dispatcher),
filter_(filter),
holding_moves_(false) {}
~HoldPointerOnScrollHandler() override {}
private:
// ui::test::TestEventHandler:
void OnGestureEvent(ui::GestureEvent* gesture) override {
if (!holding_moves_ && gesture->type() == ui::ET_GESTURE_SCROLL_UPDATE) {
holding_moves_ = true;
dispatcher_->HoldPointerMoves();
filter_->Reset();
} else if (gesture->type() == ui::ET_GESTURE_SCROLL_END) {
dispatcher_->ReleasePointerMoves();
holding_moves_ = false;
}
}
WindowEventDispatcher* dispatcher_;
EventFilterRecorder* filter_;
bool holding_moves_;
DISALLOW_COPY_AND_ASSIGN(HoldPointerOnScrollHandler);
};
// Tests that touch-move events don't contribute to an in-progress scroll
// gesture if touch-move events are being held by the dispatcher.
TEST_P(WindowEventDispatcherTest, TouchMovesHeldOnScroll) {
EventFilterRecorder recorder;
root_window()->AddPreTargetHandler(&recorder);
test::TestWindowDelegate delegate;
HoldPointerOnScrollHandler handler(host()->dispatcher(), &recorder);
std::unique_ptr<aura::Window> window(CreateTestWindowWithDelegate(
&delegate, 1, gfx::Rect(50, 50, 100, 100), root_window()));
window->AddPreTargetHandler(&handler);
ui::test::EventGenerator generator(root_window());
generator.GestureScrollSequence(
gfx::Point(60, 60), gfx::Point(10, 60),
base::TimeDelta::FromMilliseconds(100), 25);
// |handler| will have reset |filter| and started holding the touch-move