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
ash / events / select_to_speak_event_handler_unittest.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/events/select_to_speak_event_handler.h"
#include <memory>
#include <set>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/events/test_event_capturer.h"
#include "ash/public/cpp/select_to_speak_event_handler_delegate.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_helper.h"
#include "ash/test/ash_test_views_delegate.h"
#include "base/memory/raw_ptr.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/window.h"
#include "ui/display/manager/display_manager.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/events_test_utils.h"
#include "ui/events/types/event_type.h"
namespace ash {
namespace {
class TestDelegate : public SelectToSpeakEventHandlerDelegate {
public:
TestDelegate() = default;
TestDelegate(const TestDelegate&) = delete;
TestDelegate& operator=(const TestDelegate&) = delete;
virtual ~TestDelegate() = default;
bool CapturedMouseEvent(ui::EventType event_type) {
return base::Contains(mouse_events_captured_, event_type);
}
void Reset() {
mouse_events_captured_.clear();
last_mouse_location_.SetPoint(0, 0);
last_mouse_root_location_.SetPoint(0, 0);
}
gfx::Point last_mouse_event_location() { return last_mouse_location_; }
gfx::Point last_mouse_event_root_location() {
return last_mouse_root_location_;
}
private:
// SelectToSpeakEventHandlerDelegate:
void DispatchMouseEvent(const ui::MouseEvent& event) override {
mouse_events_captured_.insert(event.type());
last_mouse_location_ = event.location();
last_mouse_root_location_ = event.root_location();
}
void DispatchKeysCurrentlyDown(
const std::set<ui::KeyboardCode>& pressed_keys) override {
// Unused for now.
}
gfx::Point last_mouse_location_;
gfx::Point last_mouse_root_location_;
std::set<ui::EventType> mouse_events_captured_;
};
class SelectToSpeakEventHandlerTest : public AshTestBase {
public:
SelectToSpeakEventHandlerTest() = default;
SelectToSpeakEventHandlerTest(const SelectToSpeakEventHandlerTest&) = delete;
SelectToSpeakEventHandlerTest& operator=(
const SelectToSpeakEventHandlerTest&) = delete;
~SelectToSpeakEventHandlerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
// This test triggers a resize of WindowTreeHost, which will end up
// throttling events. set_throttle_input_on_resize_for_testing() disables
// this.
aura::Env::GetInstance()->set_throttle_input_on_resize_for_testing(false);
// Make sure the display is initialized so we don't fail the test due to any
// input events caused from creating the display.
Shell::Get()->display_manager()->UpdateDisplays();
base::RunLoop().RunUntilIdle();
delegate_ = std::make_unique<TestDelegate>();
generator_ = AshTestBase::GetEventGenerator();
GetContext()->AddPreTargetHandler(&event_capturer_);
controller_ = Shell::Get()->accessibility_controller();
controller_->select_to_speak().SetEnabled(true);
controller_->SetSelectToSpeakEventHandlerDelegate(delegate_.get());
}
void TearDown() override {
GetContext()->RemovePreTargetHandler(&event_capturer_);
generator_ = nullptr;
controller_ = nullptr;
AshTestBase::TearDown();
}
protected:
raw_ptr<ui::test::EventGenerator> generator_ = nullptr;
TestEventCapturer event_capturer_;
raw_ptr<AccessibilityController> controller_ = nullptr;
std::unique_ptr<TestDelegate> delegate_;
};
TEST_F(SelectToSpeakEventHandlerTest, PressAndReleaseSearchNotHandled) {
// If the user presses and releases the Search key, with no mouse
// presses, the key events won't be handled by the SelectToSpeakEventHandler
// and the normal behavior will occur.
EXPECT_FALSE(event_capturer_.LastKeyEvent());
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
}
// Note: when running these tests locally on desktop Linux, you may need
// to use xvfb-run, otherwise simulating the Search key press may not work.
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusClick) {
// If the user holds the Search key and then clicks the mouse button,
// the mouse events and the key release event get handled by the
// SelectToSpeakEventHandler, and mouse events are forwarded to the
// extension.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
gfx::Point click_location = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location);
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
EXPECT_EQ(click_location, delegate_->last_mouse_event_location());
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
EXPECT_EQ(click_location, delegate_->last_mouse_event_location());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusDrag) {
// Mouse move events should also be captured.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
gfx::Point click_location = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location);
generator_->PressLeftButton();
EXPECT_EQ(click_location, delegate_->last_mouse_event_location());
// Drags are not blocked.
gfx::Point drag_location = gfx::Point(120, 32);
generator_->DragMouseTo(drag_location);
EXPECT_EQ(drag_location, delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseLeftButton();
EXPECT_EQ(drag_location, delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusMove) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
gfx::Point initial_mouse_location = gfx::Point(100, 12);
generator_->set_current_screen_location(initial_mouse_location);
// Hovers are not passed through.
gfx::Point move_location = gfx::Point(120, 32);
generator_->MoveMouseTo(move_location);
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusDragOnLargeDisplay) {
// This display has twice the number of pixels per DIP. This means that
// each event coming in in px needs to be divided by two to be converted
// to DIPs.
UpdateDisplay("800x600*2");
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
gfx::Point click_location_px = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location_px);
generator_->PressLeftButton();
EXPECT_EQ(gfx::Point(click_location_px.x() / 2, click_location_px.y() / 2),
delegate_->last_mouse_event_location());
gfx::Point drag_location_px = gfx::Point(120, 32);
generator_->DragMouseTo(drag_location_px);
EXPECT_EQ(gfx::Point(drag_location_px.x() / 2, drag_location_px.y() / 2),
delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseLeftButton();
EXPECT_EQ(gfx::Point(drag_location_px.x() / 2, drag_location_px.y() / 2),
delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
}
TEST_F(SelectToSpeakEventHandlerTest, RepeatSearchKey) {
// Holding the Search key may generate key repeat events. Make sure it's
// still treated as if the search key is down.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->set_current_screen_location(gfx::Point(100, 12));
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, TapSearchKey) {
// Tapping the search key should not steal future events.
event_capturer_.ClearEvents();
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent()->handled());
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent()->handled());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusClickTwice) {
// Same as SearchPlusClick, above, but test that the user can keep
// holding down Search and click again.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
generator_->set_current_screen_location(gfx::Point(100, 12));
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
delegate_->Reset();
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusKeyIgnoresClicks) {
// If the user presses the Search key and then some other key
// besides 's', we should assume the user does not want select-to-speak,
// and click events should be ignored.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
generator_->PressKey(ui::VKEY_I, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
generator_->set_current_screen_location(gfx::Point(100, 12));
generator_->PressLeftButton();
ASSERT_TRUE(event_capturer_.LastMouseEvent());
EXPECT_FALSE(event_capturer_.LastMouseEvent()->handled());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->ReleaseLeftButton();
ASSERT_TRUE(event_capturer_.LastMouseEvent());
EXPECT_FALSE(event_capturer_.LastMouseEvent()->handled());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_I, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusSIsCaptured) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
// Press and release S, key presses should be captured.
event_capturer_.ClearEvents();
generator_->PressKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
generator_->ReleaseKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
// Press and release again while still holding down search.
// The events should continue to be captured.
generator_->PressKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
generator_->ReleaseKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
// S alone is not captured.
generator_->PressKey(ui::VKEY_S, ui::EF_NONE);
ASSERT_TRUE(controller_->GetSelectToSpeakEventHandlerForTesting()
->IsKeyDownForTesting(ui::VKEY_S));
ASSERT_TRUE(event_capturer_.LastKeyEvent());
ASSERT_FALSE(event_capturer_.LastKeyEvent()->handled());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusSIgnoresMouse) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
// Press S
event_capturer_.ClearEvents();
generator_->PressKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
// Mouse events are passed through like normal.
generator_->PressLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
generator_->ReleaseKey(ui::VKEY_S, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
ASSERT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, SearchPlusMouseIgnoresS) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
// Press the mouse.
event_capturer_.ClearEvents();
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
// S key events are passed through like normal.
generator_->PressKey(ui::VKEY_S, ui::EF_NONE);
ASSERT_TRUE(controller_->GetSelectToSpeakEventHandlerForTesting()
->IsKeyDownForTesting(ui::VKEY_S));
EXPECT_TRUE(event_capturer_.LastKeyEvent());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_S, ui::EF_NONE);
ASSERT_FALSE(controller_->GetSelectToSpeakEventHandlerForTesting()
->IsKeyDownForTesting(ui::VKEY_S));
EXPECT_TRUE(event_capturer_.LastKeyEvent());
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, DoesntStartSelectionModeIfNotInactive) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
// This shouldn't cause any changes since the state is not inactive.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
// Mouse event still captured.
gfx::Point click_location = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location);
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
// This shouldn't cause any changes since the state is not inactive.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
generator_->ReleaseLeftButton();
// Releasing the search key is still captured per the end of the search+click
// mode.
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest,
CancelSearchKeyUpAfterEarlyInactiveStateChange) {
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
gfx::Point click_location = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location);
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
// Set the state to inactive.
// This is realistic because Select-to-Speak will set the state to inactive
// after the hittest / search for the focused node callbacks, which may occur
// before the user actually releases the search key.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateInactive);
// The search key release should still be captured.
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_FALSE(event_capturer_.LastKeyEvent());
}
TEST_F(SelectToSpeakEventHandlerTest, PassesCtrlKey) {
generator_->PressKey(ui::VKEY_CONTROL, /*flags=*/0);
ASSERT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_CONTROL, /*flags=*/0);
EXPECT_TRUE(event_capturer_.LastKeyEvent());
EXPECT_FALSE(event_capturer_.LastKeyEvent()->handled());
}
TEST_F(SelectToSpeakEventHandlerTest, SelectionRequestedWorksWithMouse) {
gfx::Point click_location = gfx::Point(100, 12);
generator_->set_current_screen_location(click_location);
// Mouse events are let through normally before entering selecting state.
// Another mouse event is let through normally.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateInactive);
generator_->PressLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
// Start selection mode.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
generator_->PressLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
event_capturer_.ClearEvents();
gfx::Point drag_location = gfx::Point(120, 32);
generator_->DragMouseTo(drag_location);
EXPECT_EQ(drag_location, delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
// Mouse up is the last event captured in the sequence
generator_->ReleaseLeftButton();
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
// Another mouse event is let through normally.
generator_->PressLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
}
TEST_F(SelectToSpeakEventHandlerTest, SelectionRequestedWorksWithTouch) {
gfx::Point touch_location = gfx::Point(100, 12);
generator_->set_current_screen_location(touch_location);
// Mouse events are let through normally before entering selecting state.
// Another mouse event is let through normally.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateInactive);
generator_->PressTouch();
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
generator_->ReleaseTouch();
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
// Start selection mode.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
generator_->PressTouch();
EXPECT_FALSE(event_capturer_.LastTouchEvent());
// Touch events are converted to mouse events for the extension.
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
event_capturer_.ClearEvents();
gfx::Point drag_location = gfx::Point(120, 32);
generator_->MoveTouch(drag_location);
EXPECT_EQ(drag_location, delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
// Touch up is the last event captured in the sequence
generator_->ReleaseTouch();
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
event_capturer_.ClearEvents();
// Another touch event is let through normally.
generator_->PressTouch();
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
}
TEST_F(SelectToSpeakEventHandlerTest, SelectionRequestedIgnoresOtherInput) {
// Start selection mode.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
// Search key events are not impacted.
generator_->PressKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_TRUE(event_capturer_.LastKeyEvent());
event_capturer_.ClearEvents();
generator_->ReleaseKey(ui::VKEY_LWIN, ui::EF_COMMAND_DOWN);
EXPECT_TRUE(event_capturer_.LastKeyEvent());
event_capturer_.ClearEvents();
// Start a touch selection, it should get captured and forwarded.
generator_->PressTouch();
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
event_capturer_.ClearEvents();
// Mouse event happening during the touch selection are not impacted;
// we are locked into a touch selection mode.
generator_->PressLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
generator_->ReleaseLeftButton();
EXPECT_TRUE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
// Complete the touch selection.
generator_->ReleaseTouch();
EXPECT_FALSE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
}
TEST_F(SelectToSpeakEventHandlerTest, SelectionRequestedPreventsHovers) {
// Start selection mode.
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
// Set the mouse.
gfx::Point initial_mouse_location = gfx::Point(100, 12);
generator_->set_current_screen_location(initial_mouse_location);
// Hovers are not passed through.
gfx::Point move_location = gfx::Point(120, 32);
generator_->MoveMouseTo(move_location);
EXPECT_FALSE(event_capturer_.LastMouseEvent());
event_capturer_.ClearEvents();
}
TEST_F(SelectToSpeakEventHandlerTest, TrackingTouchIgnoresOtherTouchPointers) {
gfx::Point touch_location = gfx::Point(100, 12);
gfx::Point drag_location = gfx::Point(120, 32);
generator_->set_current_screen_location(touch_location);
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
// The first touch event is captured and sent to the extension.
generator_->PressTouchId(1);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
event_capturer_.ClearEvents();
delegate_->Reset();
// A second touch event up and down is canceled but not sent to the extension.
generator_->PressTouchId(2);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->MoveTouchId(drag_location, 2);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
generator_->ReleaseTouchId(2);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMouseReleased));
// A pointer type event will not be sent either, as we are tracking touch,
// even if the ID is the same.
generator_->EnterPenPointerMode();
generator_->PressTouchId(1);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
EXPECT_FALSE(delegate_->CapturedMouseEvent(ui::EventType::kMousePressed));
generator_->ExitPenPointerMode();
// The first pointer is still tracked.
generator_->MoveTouchId(drag_location, 1);
EXPECT_EQ(drag_location, delegate_->last_mouse_event_location());
EXPECT_TRUE(delegate_->CapturedMouseEvent(ui::EventType::kMouseDragged));
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
// Touch up is the last event captured in the sequence
generator_->ReleaseTouchId(1);
EXPECT_FALSE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
// Another touch event is let through normally.
generator_->PressTouchId(3);
EXPECT_TRUE(event_capturer_.LastTouchEvent());
event_capturer_.ClearEvents();
}
TEST_F(SelectToSpeakEventHandlerTest, TouchFirstOfMultipleDisplays) {
UpdateDisplay("1+0-800x700,801+1-800x700");
// On the first display.
gfx::Point touch_location(200, 200);
generator_->set_current_screen_location(touch_location);
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
generator_->PressTouch();
EXPECT_EQ(touch_location, delegate_->last_mouse_event_root_location());
}
TEST_F(SelectToSpeakEventHandlerTest, TouchSecondOfMultipleDisplays) {
UpdateDisplay("1+0-800x700,801+1-800x700");
// On the second display.
gfx::Point touch_location(1000, 200);
generator_->set_current_screen_location(touch_location);
controller_->SetSelectToSpeakState(
SelectToSpeakState::kSelectToSpeakStateSelecting);
generator_->PressTouch();
EXPECT_EQ(touch_location, delegate_->last_mouse_event_root_location());
}
} // namespace
} // namespace ash