forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch_exploration_controller_unittest.cc
2088 lines (1782 loc) · 82.8 KB
/
touch_exploration_controller_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 2014 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 "ash/accessibility/touch_exploration_controller.h"
#include <math.h>
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_cursor_client.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/gestures/gesture_provider_aura.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/events_test_utils.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gl/gl_implementation.h"
#include "ui/gl/test/gl_surface_test_support.h"
using EventList = std::vector<std::unique_ptr<ui::Event>>;
namespace ash {
namespace {
// Records all mouse, touch, gesture, and key events.
class EventCapturer : public ui::EventHandler {
public:
EventCapturer() {}
~EventCapturer() override {}
void Reset() { events_.clear(); }
void OnEvent(ui::Event* event) override {
if (event->IsMouseEvent() || event->IsTouchEvent() ||
event->IsGestureEvent() || event->IsKeyEvent()) {
events_.push_back(ui::Event::Clone(*event));
} else {
return;
}
// Stop event propagation so we don't click on random stuff that
// might break test assumptions.
event->StopPropagation();
// If there is a possibility that we're in an infinite loop, we should
// exit early with a sensible error rather than letting the test time out.
ASSERT_LT(events_.size(), 100u);
}
const EventList& captured_events() const { return events_; }
private:
EventList events_;
DISALLOW_COPY_AND_ASSIGN(EventCapturer);
};
int Factorial(int n) {
if (n <= 0)
return 0;
if (n == 1)
return 1;
return n * Factorial(n - 1);
}
class MockTouchExplorationControllerDelegate
: public TouchExplorationControllerDelegate {
public:
void SetOutputLevel(int volume) override {
volume_changes_.push_back(volume);
}
void SilenceSpokenFeedback() override {}
void PlayVolumeAdjustEarcon() override { ++num_times_adjust_sound_played_; }
void PlayPassthroughEarcon() override { ++num_times_passthrough_played_; }
void PlayExitScreenEarcon() override { ++num_times_exit_screen_played_; }
void PlayEnterScreenEarcon() override { ++num_times_enter_screen_played_; }
void PlayTouchTypeEarcon() override { ++num_times_touch_type_sound_played_; }
void HandleAccessibilityGesture(ax::mojom::Gesture gesture) override {
last_gesture_ = gesture;
}
const std::vector<float> VolumeChanges() const { return volume_changes_; }
size_t NumAdjustSounds() const { return num_times_adjust_sound_played_; }
size_t NumPassthroughSounds() const { return num_times_passthrough_played_; }
size_t NumExitScreenSounds() const { return num_times_exit_screen_played_; }
size_t NumEnterScreenSounds() const { return num_times_enter_screen_played_; }
size_t NumTouchTypeSounds() const {
return num_times_touch_type_sound_played_;
}
ax::mojom::Gesture GetLastGesture() const { return last_gesture_; }
void ResetLastGesture() { last_gesture_ = ax::mojom::Gesture::kNone; }
void ResetCountersToZero() {
num_times_adjust_sound_played_ = 0;
num_times_passthrough_played_ = 0;
num_times_exit_screen_played_ = 0;
num_times_enter_screen_played_ = 0;
num_times_touch_type_sound_played_ = 0;
}
private:
std::vector<float> volume_changes_;
size_t num_times_adjust_sound_played_ = 0;
size_t num_times_passthrough_played_ = 0;
size_t num_times_exit_screen_played_ = 0;
size_t num_times_enter_screen_played_ = 0;
size_t num_times_touch_type_sound_played_ = 0;
ax::mojom::Gesture last_gesture_ = ax::mojom::Gesture::kNone;
};
} // namespace
class TouchExplorationControllerTestApi {
public:
TouchExplorationControllerTestApi(
TouchExplorationController* touch_exploration_controller) {
touch_exploration_controller_.reset(touch_exploration_controller);
}
void CallTapTimerNowForTesting() {
DCHECK(touch_exploration_controller_->tap_timer_.IsRunning());
touch_exploration_controller_->tap_timer_.Stop();
touch_exploration_controller_->OnTapTimerFired();
}
void CallTapTimerNowIfRunningForTesting() {
if (touch_exploration_controller_->tap_timer_.IsRunning()) {
touch_exploration_controller_->tap_timer_.Stop();
touch_exploration_controller_->OnTapTimerFired();
}
}
bool IsInNoFingersDownStateForTesting() const {
return touch_exploration_controller_->state_ ==
touch_exploration_controller_->NO_FINGERS_DOWN;
}
bool IsInGestureInProgressStateForTesting() const {
return touch_exploration_controller_->state_ ==
touch_exploration_controller_->GESTURE_IN_PROGRESS;
}
bool IsInSlideGestureStateForTesting() const {
return touch_exploration_controller_->state_ ==
touch_exploration_controller_->SLIDE_GESTURE;
}
bool IsInTwoFingerTapStateForTesting() const {
return touch_exploration_controller_->state_ ==
touch_exploration_controller_->TWO_FINGER_TAP;
}
gfx::Rect BoundsOfRootWindowInDIPForTesting() const {
return touch_exploration_controller_->root_window_->GetBoundsInScreen();
}
// VLOGs should be suppressed in tests that generate a lot of logs,
// for example permutations of nine touch events.
void SuppressVLOGsForTesting(bool suppress) {
touch_exploration_controller_->VLOG_on_ = !suppress;
}
float GetMaxDistanceFromEdge() const {
return touch_exploration_controller_->kMaxDistanceFromEdge;
}
float GetSlopDistanceFromEdge() const {
return touch_exploration_controller_->kSlopDistanceFromEdge;
}
void SetTouchAccessibilityAnchorPoint(const gfx::Point& location) {
touch_exploration_controller_->SetTouchAccessibilityAnchorPoint(location);
}
void SetExcludeBounds(const gfx::Rect& bounds) {
touch_exploration_controller_->SetExcludeBounds(bounds);
}
void SetLiftActivationBounds(const gfx::Rect& bounds) {
touch_exploration_controller_->SetLiftActivationBounds(bounds);
}
private:
std::unique_ptr<TouchExplorationController> touch_exploration_controller_;
DISALLOW_COPY_AND_ASSIGN(TouchExplorationControllerTestApi);
};
class TouchExplorationTest : public aura::test::AuraTestBase {
public:
TouchExplorationTest() {}
~TouchExplorationTest() override {}
void SetUp() override {
if (gl::GetGLImplementation() == gl::kGLImplementationNone)
gl::GLSurfaceTestSupport::InitializeOneOff();
aura::test::AuraTestBase::SetUp();
cursor_client_.reset(new aura::test::TestCursorClient(root_window()));
root_window()->AddPreTargetHandler(&event_capturer_);
generator_.reset(new ui::test::EventGenerator(root_window()));
// Tests fail if time is ever 0.
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(10));
// ui takes ownership of the tick clock.
ui::SetEventTickClockForTesting(&simulated_clock_);
cursor_client()->ShowCursor();
cursor_client()->DisableMouseEvents();
}
void TearDown() override {
ui::SetEventTickClockForTesting(nullptr);
root_window()->RemovePreTargetHandler(&event_capturer_);
SwitchTouchExplorationMode(false);
cursor_client_.reset();
aura::test::AuraTestBase::TearDown();
}
protected:
aura::client::CursorClient* cursor_client() { return cursor_client_.get(); }
const EventList& GetCapturedEvents() {
return event_capturer_.captured_events();
}
std::vector<ui::LocatedEvent*> GetCapturedLocatedEvents() {
const EventList& all_events = GetCapturedEvents();
std::vector<ui::LocatedEvent*> located_events;
for (size_t i = 0; i < all_events.size(); ++i) {
if (all_events[i]->IsMouseEvent() || all_events[i]->IsTouchEvent() ||
all_events[i]->IsGestureEvent()) {
located_events.push_back(
static_cast<ui::LocatedEvent*>(all_events[i].get()));
}
}
return located_events;
}
std::vector<ui::Event*> GetCapturedEventsOfType(int type) {
const EventList& all_events = GetCapturedEvents();
std::vector<ui::Event*> events;
for (size_t i = 0; i < all_events.size(); ++i) {
if (type == all_events[i]->type())
events.push_back(all_events[i].get());
}
return events;
}
std::vector<ui::LocatedEvent*> GetCapturedLocatedEventsOfType(int type) {
std::vector<ui::LocatedEvent*> located_events = GetCapturedLocatedEvents();
std::vector<ui::LocatedEvent*> events;
for (size_t i = 0; i < located_events.size(); ++i) {
if (type == located_events[i]->type())
events.push_back(located_events[i]);
}
return events;
}
void ClearCapturedEvents() { event_capturer_.Reset(); }
void AdvanceSimulatedTimePastTapDelay() {
simulated_clock_.Advance(gesture_detector_config_.double_tap_timeout);
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
touch_exploration_controller_->CallTapTimerNowForTesting();
}
void AdvanceSimulatedTimePastPotentialTapDelay() {
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(1000));
touch_exploration_controller_->CallTapTimerNowIfRunningForTesting();
}
void SuppressVLOGs(bool suppress) {
touch_exploration_controller_->SuppressVLOGsForTesting(suppress);
}
void SwitchTouchExplorationMode(bool on) {
if (!on && touch_exploration_controller_.get()) {
touch_exploration_controller_.reset();
} else if (on && !touch_exploration_controller_.get()) {
touch_exploration_controller_.reset(new TouchExplorationControllerTestApi(
new TouchExplorationController(root_window(), &delegate_, nullptr)));
cursor_client()->ShowCursor();
cursor_client()->DisableMouseEvents();
}
}
void EnterTouchExplorationModeAtLocation(gfx::Point tap_location) {
ui::TouchEvent touch_press(
ui::ET_TOUCH_PRESSED, tap_location, Now(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 0));
generator_->Dispatch(&touch_press);
AdvanceSimulatedTimePastTapDelay();
EXPECT_TRUE(IsInTouchToMouseMode());
}
bool IsInTouchToMouseMode() {
aura::client::CursorClient* cursor_client =
aura::client::GetCursorClient(root_window());
return cursor_client && cursor_client->IsMouseEventsEnabled() &&
!cursor_client->IsCursorVisible();
}
bool IsInNoFingersDownState() {
return touch_exploration_controller_->IsInNoFingersDownStateForTesting();
}
bool IsInGestureInProgressState() {
return touch_exploration_controller_
->IsInGestureInProgressStateForTesting();
}
bool IsInSlideGestureState() {
return touch_exploration_controller_->IsInSlideGestureStateForTesting();
}
bool IsInTwoFingerTapState() {
return touch_exploration_controller_->IsInTwoFingerTapStateForTesting();
}
gfx::Rect BoundsOfRootWindowInDIP() {
return touch_exploration_controller_->BoundsOfRootWindowInDIPForTesting();
}
float GetMaxDistanceFromEdge() const {
return touch_exploration_controller_->GetMaxDistanceFromEdge();
}
float GetSlopDistanceFromEdge() const {
return touch_exploration_controller_->GetSlopDistanceFromEdge();
}
base::TimeTicks Now() { return ui::EventTimeForNow(); }
void SetTouchAccessibilityAnchorPoint(const gfx::Point& location) {
touch_exploration_controller_->SetTouchAccessibilityAnchorPoint(location);
}
void SetExcludeBounds(const gfx::Rect& bounds) {
touch_exploration_controller_->SetExcludeBounds(bounds);
}
void SetLiftActivationBounds(const gfx::Rect& bounds) {
touch_exploration_controller_->SetLiftActivationBounds(bounds);
}
std::unique_ptr<ui::test::EventGenerator> generator_;
ui::GestureDetector::Config gesture_detector_config_;
base::SimpleTestTickClock simulated_clock_;
MockTouchExplorationControllerDelegate delegate_;
private:
EventCapturer event_capturer_;
std::unique_ptr<TouchExplorationControllerTestApi>
touch_exploration_controller_;
std::unique_ptr<aura::test::TestCursorClient> cursor_client_;
DISALLOW_COPY_AND_ASSIGN(TouchExplorationTest);
};
// Executes a number of assertions to confirm that |e1| and |e2| are touch
// events and are equal to each other.
void ConfirmEventsAreTouchAndEqual(ui::Event* e1, ui::Event* e2) {
ASSERT_TRUE(e1->IsTouchEvent());
ASSERT_TRUE(e2->IsTouchEvent());
ui::TouchEvent* touch_event1 = e1->AsTouchEvent();
ui::TouchEvent* touch_event2 = e2->AsTouchEvent();
EXPECT_EQ(touch_event1->type(), touch_event2->type());
EXPECT_EQ(touch_event1->location(), touch_event2->location());
EXPECT_EQ(touch_event1->pointer_details().id,
touch_event2->pointer_details().id);
EXPECT_EQ(touch_event1->flags(), touch_event2->flags());
EXPECT_EQ(touch_event1->time_stamp(), touch_event2->time_stamp());
}
// Executes a number of assertions to confirm that |e1| and |e2| are mouse
// events and are equal to each other.
void ConfirmEventsAreMouseAndEqual(ui::Event* e1, ui::Event* e2) {
ASSERT_TRUE(e1->IsMouseEvent());
ASSERT_TRUE(e2->IsMouseEvent());
ui::MouseEvent* mouse_event1 = e1->AsMouseEvent();
ui::MouseEvent* mouse_event2 = e2->AsMouseEvent();
EXPECT_EQ(mouse_event1->type(), mouse_event2->type());
EXPECT_EQ(mouse_event1->location(), mouse_event2->location());
EXPECT_EQ(mouse_event1->root_location(), mouse_event2->root_location());
EXPECT_EQ(mouse_event1->flags(), mouse_event2->flags());
}
// Executes a number of assertions to confirm that |e1| and |e2| are key events
// and are equal to each other.
void ConfirmEventsAreKeyAndEqual(ui::Event* e1, ui::Event* e2) {
ASSERT_TRUE(e1->IsKeyEvent());
ASSERT_TRUE(e2->IsKeyEvent());
ui::KeyEvent* key_event1 = e1->AsKeyEvent();
ui::KeyEvent* key_event2 = e2->AsKeyEvent();
EXPECT_EQ(key_event1->type(), key_event2->type());
EXPECT_EQ(key_event1->key_code(), key_event2->key_code());
EXPECT_EQ(key_event1->code(), key_event2->code());
EXPECT_EQ(key_event1->flags(), key_event2->flags());
}
#define CONFIRM_EVENTS_ARE_TOUCH_AND_EQUAL(e1, e2) \
ASSERT_NO_FATAL_FAILURE(ConfirmEventsAreTouchAndEqual(e1, e2))
#define CONFIRM_EVENTS_ARE_MOUSE_AND_EQUAL(e1, e2) \
ASSERT_NO_FATAL_FAILURE(ConfirmEventsAreMouseAndEqual(e1, e2))
#define CONFIRM_EVENTS_ARE_KEY_AND_EQUAL(e1, e2) \
ASSERT_NO_FATAL_FAILURE(ConfirmEventsAreKeyAndEqual(e1, e2))
// TODO(mfomitchev): Need to investigate why we don't get mouse enter/exit
// events when running these tests as part of ui_base_unittests. We do get them
// when the tests are run as part of ash unit tests.
TEST_F(TouchExplorationTest, EntersTouchToMouseModeAfterPressAndDelay) {
SwitchTouchExplorationMode(true);
EXPECT_FALSE(IsInTouchToMouseMode());
generator_->PressTouch();
AdvanceSimulatedTimePastTapDelay();
EXPECT_TRUE(IsInTouchToMouseMode());
}
TEST_F(TouchExplorationTest, EntersTouchToMouseModeAfterMoveOutsideSlop) {
int slop = gesture_detector_config_.touch_slop;
int half_slop = slop / 2;
SwitchTouchExplorationMode(true);
EXPECT_FALSE(IsInTouchToMouseMode());
generator_->set_current_screen_location(gfx::Point(11, 12));
generator_->PressTouch();
generator_->MoveTouch(gfx::Point(11 + half_slop, 12));
EXPECT_FALSE(IsInTouchToMouseMode());
generator_->MoveTouch(gfx::Point(11, 12 + half_slop));
EXPECT_FALSE(IsInTouchToMouseMode());
AdvanceSimulatedTimePastTapDelay();
generator_->MoveTouch(gfx::Point(11 + slop + 1, 12));
EXPECT_TRUE(IsInTouchToMouseMode());
}
TEST_F(TouchExplorationTest, OneFingerTap) {
SwitchTouchExplorationMode(true);
gfx::Point location(11, 12);
generator_->set_current_screen_location(location);
generator_->PressTouch();
generator_->ReleaseTouch();
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_TRUE(IsInNoFingersDownState());
}
TEST_F(TouchExplorationTest, ActualMouseMovesUnaffected) {
SwitchTouchExplorationMode(true);
gfx::Point location_start(11, 12);
gfx::Point location_end(13, 14);
generator_->set_current_screen_location(location_start);
generator_->PressTouch();
AdvanceSimulatedTimePastTapDelay();
generator_->MoveTouch(location_end);
gfx::Point location_real_mouse_move(15, 16);
ui::MouseEvent mouse_move(ui::ET_MOUSE_MOVED, location_real_mouse_move,
location_real_mouse_move, ui::EventTimeForNow(), 0,
0);
generator_->Dispatch(&mouse_move);
generator_->ReleaseTouch();
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(4U, events.size());
EXPECT_EQ(location_start, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(location_end, events[1]->location());
EXPECT_TRUE(events[1]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
// The real mouse move goes through.
EXPECT_EQ(location_real_mouse_move, events[2]->location());
CONFIRM_EVENTS_ARE_MOUSE_AND_EQUAL(events[2], &mouse_move);
EXPECT_FALSE(events[2]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_FALSE(events[2]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
// The touch release gets written as a mouse move.
EXPECT_EQ(location_end, events[3]->location());
EXPECT_TRUE(events[3]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[3]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_TRUE(IsInNoFingersDownState());
}
// Turn the touch exploration mode on in the middle of the touch gesture.
// Confirm that events from the finger which was touching when the mode was
// turned on don't get rewritten.
TEST_F(TouchExplorationTest, TurnOnMidTouch) {
SwitchTouchExplorationMode(false);
generator_->PressTouchId(1);
EXPECT_TRUE(cursor_client()->IsCursorVisible());
ClearCapturedEvents();
// Enable touch exploration mode while the first finger is touching the
// screen. Ensure that subsequent events from that first finger are not
// affected by the touch exploration mode, while the touch events from another
// finger get rewritten.
SwitchTouchExplorationMode(true);
ui::TouchEvent touch_move(
ui::ET_TOUCH_MOVED, gfx::Point(11, 12), Now(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
generator_->Dispatch(&touch_move);
EXPECT_TRUE(cursor_client()->IsCursorVisible());
EXPECT_FALSE(cursor_client()->IsMouseEventsEnabled());
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(1u, captured_events.size());
CONFIRM_EVENTS_ARE_TOUCH_AND_EQUAL(captured_events[0], &touch_move);
ClearCapturedEvents();
// The press from the second finger should get rewritten.
generator_->PressTouchId(2);
AdvanceSimulatedTimePastTapDelay();
EXPECT_TRUE(IsInTouchToMouseMode());
captured_events = GetCapturedLocatedEvents();
std::vector<ui::LocatedEvent*>::const_iterator it;
for (it = captured_events.begin(); it != captured_events.end(); ++it) {
if ((*it)->type() == ui::ET_MOUSE_MOVED) {
EXPECT_TRUE((*it)->flags() & ui::EF_TOUCH_ACCESSIBILITY);
break;
}
}
EXPECT_NE(captured_events.end(), it);
ClearCapturedEvents();
// The release of the first finger shouldn't be affected.
ui::TouchEvent touch_release(
ui::ET_TOUCH_RELEASED, gfx::Point(11, 12), Now(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
generator_->Dispatch(&touch_release);
captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(1u, captured_events.size());
CONFIRM_EVENTS_ARE_TOUCH_AND_EQUAL(captured_events[0], &touch_release);
ClearCapturedEvents();
// The move and release from the second finger should get rewritten.
generator_->MoveTouchId(gfx::Point(13, 14), 2);
generator_->ReleaseTouchId(2);
AdvanceSimulatedTimePastTapDelay();
captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(2u, captured_events.size());
EXPECT_EQ(ui::ET_MOUSE_MOVED, captured_events[0]->type());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(ui::ET_MOUSE_MOVED, captured_events[1]->type());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_TRUE(IsInNoFingersDownState());
}
// If an event is received after the double-tap timeout has elapsed, but
// before the timer has fired, a mouse move should still be generated.
TEST_F(TouchExplorationTest, TimerFiresLateDuringTouchExploration) {
SwitchTouchExplorationMode(true);
// Make sure the touch is not in a corner of the screen.
generator_->MoveTouch(gfx::Point(100, 200));
// Send a press, then add another finger after the double-tap timeout.
generator_->PressTouchId(1);
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(1000));
generator_->PressTouchId(2);
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
generator_->ReleaseTouchId(2);
generator_->ReleaseTouchId(1);
AdvanceSimulatedTimePastTapDelay();
EXPECT_TRUE(IsInNoFingersDownState());
}
// If a new tap is received after the double-tap timeout has elapsed from
// a previous tap, but before the timer has fired, a mouse move should
// still be generated from the old tap.
TEST_F(TouchExplorationTest, TimerFiresLateAfterTap) {
SwitchTouchExplorationMode(true);
// Send a tap at location1.
gfx::Point location0(11, 12);
generator_->set_current_screen_location(location0);
generator_->PressTouch();
generator_->ReleaseTouch();
// Send a tap at location2, after the double-tap timeout, but before the
// timer fires.
gfx::Point location1(33, 34);
generator_->set_current_screen_location(location1);
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(301));
generator_->PressTouch();
generator_->ReleaseTouch();
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(2U, events.size());
EXPECT_EQ(location0, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(location1, events[1]->location());
EXPECT_TRUE(events[1]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_TRUE(IsInNoFingersDownState());
}
// Double-tapping should send a touch press and release through to the location
// of the last successful touch exploration.
TEST_F(TouchExplorationTest, DoubleTap) {
SwitchTouchExplorationMode(true);
// Tap at one location, and get a mouse move event.
gfx::Point tap_location(51, 52);
generator_->set_current_screen_location(tap_location);
generator_->PressTouchId(1);
generator_->ReleaseTouchId(1);
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(tap_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// Now double-tap at a different location. This should result in
// a single touch press and release at the location of the tap,
// not at the location of the double-tap.
gfx::Point double_tap_location(33, 34);
generator_->set_current_screen_location(double_tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
generator_->PressTouch();
generator_->ReleaseTouch();
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(2U, captured_events.size());
EXPECT_EQ(ui::ET_TOUCH_PRESSED, captured_events[0]->type());
EXPECT_EQ(tap_location, captured_events[0]->location());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(ui::ET_TOUCH_RELEASED, captured_events[1]->type());
EXPECT_EQ(tap_location, captured_events[1]->location());
EXPECT_TRUE(captured_events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_TRUE(IsInNoFingersDownState());
}
// The press of the second tap in a double-tap must come within the double-tap
// timeout, but the release of the second tap can come later.
TEST_F(TouchExplorationTest, DoubleTapTiming) {
SwitchTouchExplorationMode(true);
// Tap at one location, and get a mouse move event.
gfx::Point tap_location(51, 52);
generator_->set_current_screen_location(tap_location);
generator_->PressTouchId(1);
generator_->ReleaseTouchId(1);
AdvanceSimulatedTimePastTapDelay();
SetTouchAccessibilityAnchorPoint(tap_location);
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(tap_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// The press of the second tap happens in time, but the release does not.
gfx::Point double_tap_location(33, 34);
generator_->set_current_screen_location(double_tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
simulated_clock_.Advance(gesture_detector_config_.double_tap_timeout -
base::TimeDelta::FromMilliseconds(25));
generator_->PressTouch();
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(50));
generator_->ReleaseTouch();
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(0U, captured_events.size());
EXPECT_EQ(ax::mojom::Gesture::kClick, delegate_.GetLastGesture());
}
// If an explicit anchor point is set during touch exploration, double-tapping
// should send a 'click' gesture rather than a simulated touch press and
// release.
TEST_F(TouchExplorationTest, DoubleTapWithExplicitAnchorPoint) {
SwitchTouchExplorationMode(true);
// Tap at one location, and get a mouse move event.
gfx::Point tap_location(51, 52);
generator_->set_current_screen_location(tap_location);
generator_->PressTouchId(1);
generator_->ReleaseTouchId(1);
AdvanceSimulatedTimePastTapDelay();
SetTouchAccessibilityAnchorPoint(tap_location);
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(tap_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// Now double-tap at a different location. This should result in
// a click gesture.
gfx::Point double_tap_location(33, 34);
generator_->set_current_screen_location(double_tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
generator_->PressTouch();
generator_->ReleaseTouch();
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(0U, captured_events.size());
EXPECT_TRUE(IsInNoFingersDownState());
EXPECT_EQ(ax::mojom::Gesture::kClick, delegate_.GetLastGesture());
}
// Double-tapping where the user holds their finger down for the second time
// for a longer press should send a touch press and passthrough all further
// events from that finger. Other finger presses should be ignored.
TEST_F(TouchExplorationTest, DoubleTapPassthrough) {
SwitchTouchExplorationMode(true);
// Tap at one location, and get a mouse move event.
gfx::Point tap_location(11, 12);
generator_->set_current_screen_location(tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(tap_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// Now double-tap and hold at a different location.
// This should result in a single touch press at the location of the tap,
// not at the location of the double-tap.
gfx::Point first_tap_location(13, 14);
generator_->set_current_screen_location(first_tap_location);
generator_->PressTouchId(1);
generator_->ReleaseTouchId(1);
gfx::Point second_tap_location(15, 16);
generator_->set_current_screen_location(second_tap_location);
generator_->PressTouchId(1);
// Advance to the finger passing through.
AdvanceSimulatedTimePastTapDelay();
gfx::Vector2d passthrough_offset = second_tap_location - tap_location;
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(1U, captured_events.size());
EXPECT_EQ(ui::ET_TOUCH_PRESSED, captured_events[0]->type());
EXPECT_EQ(second_tap_location - passthrough_offset,
captured_events[0]->location());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// All events for the first finger should pass through now, displaced
// relative to the last touch exploration location.
gfx::Point first_move_location(17, 18);
generator_->MoveTouchId(first_move_location, 1);
gfx::Point second_move_location(12, 13);
generator_->MoveTouchId(second_move_location, 1);
captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(2U, captured_events.size());
EXPECT_EQ(ui::ET_TOUCH_MOVED, captured_events[0]->type());
EXPECT_EQ(first_move_location - passthrough_offset,
captured_events[0]->location());
EXPECT_EQ(ui::ET_TOUCH_MOVED, captured_events[1]->type());
EXPECT_EQ(second_move_location - passthrough_offset,
captured_events[1]->location());
EXPECT_TRUE(captured_events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// Events for other fingers should do nothing.
generator_->PressTouchId(2);
generator_->PressTouchId(3);
generator_->MoveTouchId(gfx::Point(34, 36), 2);
generator_->ReleaseTouchId(2);
captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(0U, captured_events.size());
// Even with finger 3 still down, events for the first finger should still
// pass through.
gfx::Point third_move_location(14, 15);
generator_->MoveTouchId(third_move_location, 1);
captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(1U, captured_events.size());
EXPECT_EQ(ui::ET_TOUCH_MOVED, captured_events[0]->type());
EXPECT_EQ(third_move_location - passthrough_offset,
captured_events[0]->location());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
// No fingers down state is only reached when every finger is lifted.
generator_->ReleaseTouchId(1);
EXPECT_FALSE(IsInNoFingersDownState());
generator_->ReleaseTouchId(3);
EXPECT_TRUE(IsInNoFingersDownState());
}
// Double-tapping, going into passthrough, and holding for the longpress
// time should send a touch press and released (right click)
// to the location of the last successful touch exploration.
TEST_F(TouchExplorationTest, DoubleTapLongPress) {
SwitchTouchExplorationMode(true);
// Tap at one location, and get a mouse move event.
gfx::Point tap_location(11, 12);
generator_->set_current_screen_location(tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
AdvanceSimulatedTimePastTapDelay();
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(tap_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
// Now double-tap and hold at a different location.
// This should result in a single touch long press and release
// at the location of the tap, not at the location of the double-tap.
// There should be a time delay between the touch press and release.
gfx::Point first_tap_location(33, 34);
generator_->set_current_screen_location(first_tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
gfx::Point second_tap_location(23, 24);
generator_->set_current_screen_location(second_tap_location);
generator_->PressTouch();
// Advance to the finger passing through, and then to the longpress timeout.
AdvanceSimulatedTimePastTapDelay();
simulated_clock_.Advance(gesture_detector_config_.longpress_timeout);
generator_->ReleaseTouch();
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(2U, captured_events.size());
EXPECT_EQ(ui::ET_TOUCH_PRESSED, captured_events[0]->type());
EXPECT_EQ(tap_location, captured_events[0]->location());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
base::TimeTicks pressed_time = captured_events[0]->time_stamp();
EXPECT_EQ(ui::ET_TOUCH_RELEASED, captured_events[1]->type());
EXPECT_EQ(tap_location, captured_events[1]->location());
EXPECT_TRUE(captured_events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
base::TimeTicks released_time = captured_events[1]->time_stamp();
EXPECT_EQ(released_time - pressed_time,
gesture_detector_config_.longpress_timeout);
}
// Single-tapping should send a touch press and release through to the location
// of the last successful touch exploration if the grace period has not
// elapsed.
TEST_F(TouchExplorationTest, SingleTap) {
SwitchTouchExplorationMode(true);
// Tap once to simulate a mouse moved event.
gfx::Point initial_location(11, 12);
generator_->set_current_screen_location(initial_location);
generator_->PressTouch();
AdvanceSimulatedTimePastTapDelay();
ClearCapturedEvents();
// Move to another location for single tap
gfx::Point tap_location(22, 23);
generator_->MoveTouch(tap_location);
generator_->ReleaseTouch();
// Allow time to pass within the grace period of releasing before
// tapping again.
gfx::Point final_location(33, 34);
generator_->set_current_screen_location(final_location);
simulated_clock_.Advance(base::TimeDelta::FromMilliseconds(250));
generator_->PressTouch();
generator_->ReleaseTouch();
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(4U, captured_events.size());
EXPECT_EQ(ui::ET_MOUSE_MOVED, captured_events[0]->type());
EXPECT_TRUE(captured_events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(ui::ET_MOUSE_MOVED, captured_events[1]->type());
EXPECT_TRUE(captured_events[1]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(ui::ET_TOUCH_PRESSED, captured_events[2]->type());
EXPECT_EQ(tap_location, captured_events[2]->location());
EXPECT_TRUE(captured_events[2]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
EXPECT_EQ(ui::ET_TOUCH_RELEASED, captured_events[3]->type());
EXPECT_EQ(tap_location, captured_events[3]->location());
EXPECT_TRUE(captured_events[3]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
}
// Double-tapping without coming from touch exploration (no previous touch
// exploration event) should not generate any events.
TEST_F(TouchExplorationTest, DoubleTapNoTouchExplore) {
SwitchTouchExplorationMode(true);
// Double-tap without any previous touch.
// Touch exploration mode has not been entered, so there is no previous
// touch exploration event. The double-tap should be discarded, and no events
// should be generated at all.
gfx::Point double_tap_location(33, 34);
generator_->set_current_screen_location(double_tap_location);
generator_->PressTouch();
generator_->ReleaseTouch();
generator_->PressTouch();
// Since the state stays in single_tap_released, we need to make sure the
// tap timer doesn't fire and set the state to no fingers down (since there
// is still a finger down).
AdvanceSimulatedTimePastPotentialTapDelay();
EXPECT_FALSE(IsInNoFingersDownState());
generator_->ReleaseTouch();
EXPECT_TRUE(IsInNoFingersDownState());
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();
ASSERT_EQ(0U, captured_events.size());
}
// Tapping and releasing with a second finger when in touch exploration mode
// should send a touch press and released to the location of the last
// successful touch exploration and return to touch explore.
TEST_F(TouchExplorationTest, SplitTap) {
SwitchTouchExplorationMode(true);
gfx::Point initial_touch_location(11, 12);
gfx::Point second_touch_location(33, 34);
// Tap and hold at one location, and get a mouse move event in touch explore.
EnterTouchExplorationModeAtLocation(initial_touch_location);
std::vector<ui::LocatedEvent*> events =
GetCapturedLocatedEventsOfType(ui::ET_MOUSE_MOVED);
ASSERT_EQ(1U, events.size());
EXPECT_EQ(initial_touch_location, events[0]->location());
EXPECT_TRUE(events[0]->flags() & ui::EF_IS_SYNTHESIZED);
EXPECT_TRUE(events[0]->flags() & ui::EF_TOUCH_ACCESSIBILITY);
ClearCapturedEvents();
EXPECT_TRUE(IsInTouchToMouseMode());
// Now tap and release at a different location. This should result in a
// single touch and release at the location of the first (held) tap,
// not at the location of the second tap and release.
// After the release, there is still a finger in touch explore mode.
ui::TouchEvent split_tap_press(
ui::ET_TOUCH_PRESSED, second_touch_location, Now(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
generator_->Dispatch(&split_tap_press);
// To simulate the behavior of the real device, we manually disable
// mouse events. To not rely on manually setting the state, this is also
// tested in touch_exploration_controller_browsertest.
cursor_client()->DisableMouseEvents();
EXPECT_FALSE(cursor_client()->IsMouseEventsEnabled());
EXPECT_FALSE(cursor_client()->IsCursorVisible());
EXPECT_FALSE(IsInGestureInProgressState());
ui::TouchEvent split_tap_release(
ui::ET_TOUCH_RELEASED, second_touch_location, Now(),
ui::PointerDetails(ui::EventPointerType::POINTER_TYPE_TOUCH, 1));
generator_->Dispatch(&split_tap_release);
// Releasing the second finger should re-enable mouse events putting us
// back into the touch exploration mode.
EXPECT_TRUE(IsInTouchToMouseMode());
EXPECT_FALSE(IsInNoFingersDownState());
std::vector<ui::LocatedEvent*> captured_events = GetCapturedLocatedEvents();