forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerator_controller_unittest.cc
1371 lines (1179 loc) · 53.9 KB
/
accelerator_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 (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 "ash/accelerators/accelerator_controller.h"
#include "ash/accelerators/accelerator_table.h"
#include "ash/accessibility_delegate.h"
#include "ash/accessibility_types.h"
#include "ash/ash_switches.h"
#include "ash/ime_control_delegate.h"
#include "ash/public/cpp/config.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/shell_port.h"
#include "ash/system/brightness_control_delegate.h"
#include "ash/system/keyboard_brightness_control_delegate.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/lock_state_controller_test_api.h"
#include "ash/test/test_screenshot_delegate.h"
#include "ash/test/test_session_state_animator.h"
#include "ash/wm/lock_state_controller.h"
#include "ash/wm/panels/panel_layout_manager.h"
#include "ash/wm/window_positioning_utils.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_state_aura.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "ash/wm_window.h"
#include "base/command_line.h"
#include "base/test/user_action_tester.h"
#include "services/ui/public/interfaces/window_manager_constants.mojom.h"
#include "ui/app_list/presenter/app_list.h"
#include "ui/app_list/presenter/test/test_app_list_presenter.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/base/ime/chromeos/fake_ime_keyboard.h"
#include "ui/base/ime/chromeos/ime_keyboard.h"
#include "ui/base/ime/chromeos/input_method_manager.h"
#include "ui/base/ime/chromeos/mock_input_method_manager.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/events/event_sink.h"
#include "ui/events/test/event_generator.h"
#include "ui/message_center/message_center.h"
#include "ui/views/widget/widget.h"
#if defined(USE_X11)
#include <X11/Xlib.h>
#include "ui/events/test/events_test_utils_x11.h"
#endif
namespace ash {
namespace {
class TestTarget : public ui::AcceleratorTarget {
public:
TestTarget() : accelerator_pressed_count_(0), accelerator_repeat_count_(0) {}
~TestTarget() override {}
int accelerator_pressed_count() const { return accelerator_pressed_count_; }
int accelerator_repeat_count() const { return accelerator_repeat_count_; }
void reset() {
accelerator_pressed_count_ = 0;
accelerator_repeat_count_ = 0;
}
// Overridden from ui::AcceleratorTarget:
bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
bool CanHandleAccelerators() const override;
private:
int accelerator_pressed_count_;
int accelerator_repeat_count_;
DISALLOW_COPY_AND_ASSIGN(TestTarget);
};
ui::Accelerator CreateReleaseAccelerator(ui::KeyboardCode key_code,
int modifiers) {
ui::Accelerator accelerator(key_code, modifiers);
accelerator.set_key_state(ui::Accelerator::KeyState::RELEASED);
return accelerator;
}
class DummyBrightnessControlDelegate : public BrightnessControlDelegate {
public:
DummyBrightnessControlDelegate()
: handle_brightness_down_count_(0), handle_brightness_up_count_(0) {}
~DummyBrightnessControlDelegate() override {}
void HandleBrightnessDown(const ui::Accelerator& accelerator) override {
++handle_brightness_down_count_;
last_accelerator_ = accelerator;
}
void HandleBrightnessUp(const ui::Accelerator& accelerator) override {
++handle_brightness_up_count_;
last_accelerator_ = accelerator;
}
void SetBrightnessPercent(double percent, bool gradual) override {}
void GetBrightnessPercent(
const base::Callback<void(double)>& callback) override {
callback.Run(100.0);
}
int handle_brightness_down_count() const {
return handle_brightness_down_count_;
}
int handle_brightness_up_count() const { return handle_brightness_up_count_; }
const ui::Accelerator& last_accelerator() const { return last_accelerator_; }
private:
int handle_brightness_down_count_;
int handle_brightness_up_count_;
ui::Accelerator last_accelerator_;
DISALLOW_COPY_AND_ASSIGN(DummyBrightnessControlDelegate);
};
class DummyImeControlDelegate : public ImeControlDelegate {
public:
DummyImeControlDelegate()
: handle_next_ime_count_(0),
handle_previous_ime_count_(0),
handle_switch_ime_count_(0) {}
~DummyImeControlDelegate() override {}
bool CanCycleIme() override { return true; }
void HandleNextIme() override { ++handle_next_ime_count_; }
void HandlePreviousIme() override { ++handle_previous_ime_count_; }
bool CanSwitchIme(const ui::Accelerator& accelerator) override {
return true;
}
void HandleSwitchIme(const ui::Accelerator& accelerator) override {
++handle_switch_ime_count_;
}
int handle_next_ime_count() const { return handle_next_ime_count_; }
int handle_previous_ime_count() const { return handle_previous_ime_count_; }
int handle_switch_ime_count() const { return handle_switch_ime_count_; }
private:
int handle_next_ime_count_;
int handle_previous_ime_count_;
int handle_switch_ime_count_;
DISALLOW_COPY_AND_ASSIGN(DummyImeControlDelegate);
};
class DummyKeyboardBrightnessControlDelegate
: public KeyboardBrightnessControlDelegate {
public:
DummyKeyboardBrightnessControlDelegate()
: handle_keyboard_brightness_down_count_(0),
handle_keyboard_brightness_up_count_(0) {}
~DummyKeyboardBrightnessControlDelegate() override {}
void HandleKeyboardBrightnessDown(
const ui::Accelerator& accelerator) override {
++handle_keyboard_brightness_down_count_;
last_accelerator_ = accelerator;
}
void HandleKeyboardBrightnessUp(const ui::Accelerator& accelerator) override {
++handle_keyboard_brightness_up_count_;
last_accelerator_ = accelerator;
}
int handle_keyboard_brightness_down_count() const {
return handle_keyboard_brightness_down_count_;
}
int handle_keyboard_brightness_up_count() const {
return handle_keyboard_brightness_up_count_;
}
const ui::Accelerator& last_accelerator() const { return last_accelerator_; }
private:
int handle_keyboard_brightness_down_count_;
int handle_keyboard_brightness_up_count_;
ui::Accelerator last_accelerator_;
DISALLOW_COPY_AND_ASSIGN(DummyKeyboardBrightnessControlDelegate);
};
bool TestTarget::AcceleratorPressed(const ui::Accelerator& accelerator) {
if (accelerator.IsRepeat())
++accelerator_repeat_count_;
else
++accelerator_pressed_count_;
return true;
}
bool TestTarget::CanHandleAccelerators() const {
return true;
}
} // namespace
class AcceleratorControllerTest : public test::AshTestBase {
public:
AcceleratorControllerTest() {}
~AcceleratorControllerTest() override {}
protected:
static AcceleratorController* GetController();
static bool ProcessInController(const ui::Accelerator& accelerator) {
if (accelerator.key_state() == ui::Accelerator::KeyState::RELEASED) {
// If the |accelerator| should trigger on release, then we store the
// pressed version of it first in history then the released one to
// simulate what happens in reality.
ui::Accelerator pressed_accelerator = accelerator;
pressed_accelerator.set_key_state(ui::Accelerator::KeyState::PRESSED);
GetController()->accelerator_history()->StoreCurrentAccelerator(
pressed_accelerator);
}
GetController()->accelerator_history()->StoreCurrentAccelerator(
accelerator);
return GetController()->Process(accelerator);
}
static const ui::Accelerator& GetPreviousAccelerator() {
return GetController()->accelerator_history()->previous_accelerator();
}
static const ui::Accelerator& GetCurrentAccelerator() {
return GetController()->accelerator_history()->current_accelerator();
}
// Several functions to access ExitWarningHandler (as friend).
static void StubForTest(ExitWarningHandler* ewh) {
ewh->stub_timer_for_test_ = true;
}
static void Reset(ExitWarningHandler* ewh) {
ewh->state_ = ExitWarningHandler::IDLE;
}
static void SimulateTimerExpired(ExitWarningHandler* ewh) {
ewh->TimerAction();
}
static bool is_ui_shown(ExitWarningHandler* ewh) { return !!ewh->widget_; }
static bool is_idle(ExitWarningHandler* ewh) {
return ewh->state_ == ExitWarningHandler::IDLE;
}
static bool is_exiting(ExitWarningHandler* ewh) {
return ewh->state_ == ExitWarningHandler::EXITING;
}
void SetBrightnessControlDelegate(
std::unique_ptr<BrightnessControlDelegate> delegate) {
Shell::Get()->brightness_control_delegate_ = std::move(delegate);
}
void SetKeyboardBrightnessControlDelegate(
std::unique_ptr<KeyboardBrightnessControlDelegate> delegate) {
Shell::Get()->keyboard_brightness_control_delegate_ = std::move(delegate);
}
private:
DISALLOW_COPY_AND_ASSIGN(AcceleratorControllerTest);
};
AcceleratorController* AcceleratorControllerTest::GetController() {
return Shell::Get()->accelerator_controller();
}
// Double press of exit shortcut => exiting
TEST_F(AcceleratorControllerTest, ExitWarningHandlerTestDoublePress) {
ui::Accelerator press(ui::VKEY_Q, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
ui::Accelerator release(press);
release.set_key_state(ui::Accelerator::KeyState::RELEASED);
ExitWarningHandler* ewh = GetController()->GetExitWarningHandlerForTest();
ASSERT_TRUE(ewh);
StubForTest(ewh);
EXPECT_TRUE(is_idle(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
EXPECT_TRUE(ProcessInController(press));
EXPECT_FALSE(ProcessInController(release));
EXPECT_FALSE(is_idle(ewh));
EXPECT_TRUE(is_ui_shown(ewh));
EXPECT_TRUE(ProcessInController(press)); // second press before timer.
EXPECT_FALSE(ProcessInController(release));
SimulateTimerExpired(ewh);
EXPECT_TRUE(is_exiting(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
Reset(ewh);
}
// Single press of exit shortcut before timer => idle
TEST_F(AcceleratorControllerTest, ExitWarningHandlerTestSinglePress) {
ui::Accelerator press(ui::VKEY_Q, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
ui::Accelerator release(press);
release.set_key_state(ui::Accelerator::KeyState::RELEASED);
ExitWarningHandler* ewh = GetController()->GetExitWarningHandlerForTest();
ASSERT_TRUE(ewh);
StubForTest(ewh);
EXPECT_TRUE(is_idle(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
EXPECT_TRUE(ProcessInController(press));
EXPECT_FALSE(ProcessInController(release));
EXPECT_FALSE(is_idle(ewh));
EXPECT_TRUE(is_ui_shown(ewh));
SimulateTimerExpired(ewh);
EXPECT_TRUE(is_idle(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
Reset(ewh);
}
// Shutdown ash with exit warning bubble open should not crash.
TEST_F(AcceleratorControllerTest, LingeringExitWarningBubble) {
ExitWarningHandler* ewh = GetController()->GetExitWarningHandlerForTest();
ASSERT_TRUE(ewh);
StubForTest(ewh);
// Trigger once to show the bubble.
ewh->HandleAccelerator();
EXPECT_FALSE(is_idle(ewh));
EXPECT_TRUE(is_ui_shown(ewh));
// Exit ash and there should be no crash
}
TEST_F(AcceleratorControllerTest, Register) {
TestTarget target;
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
const ui::Accelerator accelerator_b(ui::VKEY_B, ui::EF_NONE);
const ui::Accelerator accelerator_c(ui::VKEY_C, ui::EF_NONE);
const ui::Accelerator accelerator_d(ui::VKEY_D, ui::EF_NONE);
GetController()->Register(
{accelerator_a, accelerator_b, accelerator_c, accelerator_d}, &target);
// The registered accelerators are processed.
EXPECT_TRUE(ProcessInController(accelerator_a));
EXPECT_TRUE(ProcessInController(accelerator_b));
EXPECT_TRUE(ProcessInController(accelerator_c));
EXPECT_TRUE(ProcessInController(accelerator_d));
EXPECT_EQ(4, target.accelerator_pressed_count());
}
TEST_F(AcceleratorControllerTest, RegisterMultipleTarget) {
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
TestTarget target1;
GetController()->Register({accelerator_a}, &target1);
TestTarget target2;
GetController()->Register({accelerator_a}, &target2);
// If multiple targets are registered with the same accelerator, the target
// registered later processes the accelerator.
EXPECT_TRUE(ProcessInController(accelerator_a));
EXPECT_EQ(0, target1.accelerator_pressed_count());
EXPECT_EQ(1, target2.accelerator_pressed_count());
}
TEST_F(AcceleratorControllerTest, Unregister) {
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
const ui::Accelerator accelerator_b(ui::VKEY_B, ui::EF_NONE);
TestTarget target;
GetController()->Register({accelerator_a, accelerator_b}, &target);
// Unregistering a different accelerator does not affect the other
// accelerator.
GetController()->Unregister(accelerator_b, &target);
EXPECT_TRUE(ProcessInController(accelerator_a));
EXPECT_EQ(1, target.accelerator_pressed_count());
// The unregistered accelerator is no longer processed.
target.reset();
GetController()->Unregister(accelerator_a, &target);
EXPECT_FALSE(ProcessInController(accelerator_a));
EXPECT_EQ(0, target.accelerator_pressed_count());
}
TEST_F(AcceleratorControllerTest, UnregisterAll) {
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
const ui::Accelerator accelerator_b(ui::VKEY_B, ui::EF_NONE);
TestTarget target1;
GetController()->Register({accelerator_a, accelerator_b}, &target1);
const ui::Accelerator accelerator_c(ui::VKEY_C, ui::EF_NONE);
TestTarget target2;
GetController()->Register({accelerator_c}, &target2);
GetController()->UnregisterAll(&target1);
// All the accelerators registered for |target1| are no longer processed.
EXPECT_FALSE(ProcessInController(accelerator_a));
EXPECT_FALSE(ProcessInController(accelerator_b));
EXPECT_EQ(0, target1.accelerator_pressed_count());
// UnregisterAll with a different target does not affect the other target.
EXPECT_TRUE(ProcessInController(accelerator_c));
EXPECT_EQ(1, target2.accelerator_pressed_count());
}
TEST_F(AcceleratorControllerTest, Process) {
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
TestTarget target1;
GetController()->Register({accelerator_a}, &target1);
// The registered accelerator is processed.
EXPECT_TRUE(ProcessInController(accelerator_a));
EXPECT_EQ(1, target1.accelerator_pressed_count());
// The non-registered accelerator is not processed.
const ui::Accelerator accelerator_b(ui::VKEY_B, ui::EF_NONE);
EXPECT_FALSE(ProcessInController(accelerator_b));
}
TEST_F(AcceleratorControllerTest, IsRegistered) {
const ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
const ui::Accelerator accelerator_shift_a(ui::VKEY_A, ui::EF_SHIFT_DOWN);
TestTarget target;
GetController()->Register({accelerator_a}, &target);
EXPECT_TRUE(GetController()->IsRegistered(accelerator_a));
EXPECT_FALSE(GetController()->IsRegistered(accelerator_shift_a));
GetController()->UnregisterAll(&target);
EXPECT_FALSE(GetController()->IsRegistered(accelerator_a));
}
TEST_F(AcceleratorControllerTest, WindowSnap) {
std::unique_ptr<aura::Window> window(
CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
wm::WindowState* window_state = wm::GetWindowState(window.get());
window_state->Activate();
{
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_LEFT);
gfx::Rect expected_bounds = wm::GetDefaultLeftSnappedWindowBoundsInParent(
WmWindow::Get(window.get()));
EXPECT_EQ(expected_bounds.ToString(), window->bounds().ToString());
}
{
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_RIGHT);
gfx::Rect expected_bounds = wm::GetDefaultRightSnappedWindowBoundsInParent(
WmWindow::Get(window.get()));
EXPECT_EQ(expected_bounds.ToString(), window->bounds().ToString());
}
{
gfx::Rect normal_bounds = window_state->GetRestoreBoundsInParent();
GetController()->PerformActionIfEnabled(TOGGLE_MAXIMIZED);
EXPECT_TRUE(window_state->IsMaximized());
EXPECT_NE(normal_bounds.ToString(), window->bounds().ToString());
GetController()->PerformActionIfEnabled(TOGGLE_MAXIMIZED);
EXPECT_FALSE(window_state->IsMaximized());
// Window gets restored to its restore bounds since side-maximized state
// is treated as a "maximized" state.
EXPECT_EQ(normal_bounds.ToString(), window->bounds().ToString());
GetController()->PerformActionIfEnabled(TOGGLE_MAXIMIZED);
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_LEFT);
EXPECT_FALSE(window_state->IsMaximized());
GetController()->PerformActionIfEnabled(TOGGLE_MAXIMIZED);
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_RIGHT);
EXPECT_FALSE(window_state->IsMaximized());
GetController()->PerformActionIfEnabled(TOGGLE_MAXIMIZED);
EXPECT_TRUE(window_state->IsMaximized());
GetController()->PerformActionIfEnabled(WINDOW_MINIMIZE);
EXPECT_FALSE(window_state->IsMaximized());
EXPECT_TRUE(window_state->IsMinimized());
window_state->Restore();
window_state->Activate();
}
{
GetController()->PerformActionIfEnabled(WINDOW_MINIMIZE);
EXPECT_TRUE(window_state->IsMinimized());
}
}
// Tests that window snapping works.
TEST_F(AcceleratorControllerTest, TestRepeatedSnap) {
std::unique_ptr<aura::Window> window(
CreateTestWindowInShellWithBounds(gfx::Rect(5, 5, 20, 20)));
wm::WindowState* window_state = wm::GetWindowState(window.get());
window_state->Activate();
// Snap right.
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_RIGHT);
gfx::Rect normal_bounds = window_state->GetRestoreBoundsInParent();
gfx::Rect expected_bounds = wm::GetDefaultRightSnappedWindowBoundsInParent(
WmWindow::Get(window.get()));
EXPECT_EQ(expected_bounds.ToString(), window->bounds().ToString());
EXPECT_TRUE(window_state->IsSnapped());
// Snap right again ->> becomes normal.
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_RIGHT);
EXPECT_TRUE(window_state->IsNormalStateType());
EXPECT_EQ(normal_bounds.ToString(), window->bounds().ToString());
// Snap right.
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_RIGHT);
EXPECT_TRUE(window_state->IsSnapped());
// Snap left.
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_LEFT);
EXPECT_TRUE(window_state->IsSnapped());
expected_bounds = wm::GetDefaultLeftSnappedWindowBoundsInParent(
WmWindow::Get(window.get()));
EXPECT_EQ(expected_bounds.ToString(), window->bounds().ToString());
// Snap left again ->> becomes normal.
GetController()->PerformActionIfEnabled(WINDOW_CYCLE_SNAP_LEFT);
EXPECT_TRUE(window_state->IsNormalStateType());
EXPECT_EQ(normal_bounds.ToString(), window->bounds().ToString());
}
TEST_F(AcceleratorControllerTest, RotateScreen) {
// TODO: needs GetDisplayInfo http://crbug.com/622480.
if (Shell::GetAshConfig() == Config::MASH)
return;
display::Display display = display::Screen::GetScreen()->GetPrimaryDisplay();
display::Display::Rotation initial_rotation =
GetActiveDisplayRotation(display.id());
ui::test::EventGenerator& generator = GetEventGenerator();
generator.PressKey(ui::VKEY_BROWSER_REFRESH,
ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN);
generator.ReleaseKey(ui::VKEY_BROWSER_REFRESH,
ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN);
display::Display::Rotation new_rotation =
GetActiveDisplayRotation(display.id());
// |new_rotation| is determined by the AcceleratorControllerDelegate.
EXPECT_NE(initial_rotation, new_rotation);
}
TEST_F(AcceleratorControllerTest, AutoRepeat) {
ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_CONTROL_DOWN);
TestTarget target_a;
GetController()->Register({accelerator_a}, &target_a);
ui::Accelerator accelerator_b(ui::VKEY_B, ui::EF_CONTROL_DOWN);
TestTarget target_b;
GetController()->Register({accelerator_b}, &target_b);
ui::test::EventGenerator& generator = GetEventGenerator();
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
generator.ReleaseKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
EXPECT_EQ(1, target_a.accelerator_pressed_count());
EXPECT_EQ(0, target_a.accelerator_repeat_count());
// Long press should generate one
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_IS_REPEAT);
EXPECT_EQ(2, target_a.accelerator_pressed_count());
EXPECT_EQ(1, target_a.accelerator_repeat_count());
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_IS_REPEAT);
EXPECT_EQ(2, target_a.accelerator_pressed_count());
EXPECT_EQ(2, target_a.accelerator_repeat_count());
generator.ReleaseKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
EXPECT_EQ(2, target_a.accelerator_pressed_count());
EXPECT_EQ(2, target_a.accelerator_repeat_count());
// Long press was intercepted by another key press.
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_IS_REPEAT);
generator.PressKey(ui::VKEY_B, ui::EF_CONTROL_DOWN);
generator.ReleaseKey(ui::VKEY_B, ui::EF_CONTROL_DOWN);
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
generator.PressKey(ui::VKEY_A, ui::EF_CONTROL_DOWN | ui::EF_IS_REPEAT);
generator.ReleaseKey(ui::VKEY_A, ui::EF_CONTROL_DOWN);
EXPECT_EQ(1, target_b.accelerator_pressed_count());
EXPECT_EQ(0, target_b.accelerator_repeat_count());
EXPECT_EQ(4, target_a.accelerator_pressed_count());
EXPECT_EQ(4, target_a.accelerator_repeat_count());
}
TEST_F(AcceleratorControllerTest, Previous) {
ui::test::EventGenerator& generator = GetEventGenerator();
generator.PressKey(ui::VKEY_VOLUME_MUTE, ui::EF_NONE);
generator.ReleaseKey(ui::VKEY_VOLUME_MUTE, ui::EF_NONE);
EXPECT_EQ(ui::VKEY_VOLUME_MUTE, GetPreviousAccelerator().key_code());
EXPECT_EQ(ui::EF_NONE, GetPreviousAccelerator().modifiers());
generator.PressKey(ui::VKEY_TAB, ui::EF_CONTROL_DOWN);
generator.ReleaseKey(ui::VKEY_TAB, ui::EF_CONTROL_DOWN);
EXPECT_EQ(ui::VKEY_TAB, GetPreviousAccelerator().key_code());
EXPECT_EQ(ui::EF_CONTROL_DOWN, GetPreviousAccelerator().modifiers());
}
TEST_F(AcceleratorControllerTest, DontRepeatToggleFullscreen) {
const AcceleratorData accelerators[] = {
{true, ui::VKEY_J, ui::EF_ALT_DOWN, TOGGLE_FULLSCREEN},
{true, ui::VKEY_K, ui::EF_ALT_DOWN, TOGGLE_FULLSCREEN},
};
GetController()->RegisterAccelerators(accelerators, arraysize(accelerators));
views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
params.context = CurrentContext();
params.bounds = gfx::Rect(5, 5, 20, 20);
views::Widget* widget = new views::Widget;
widget->Init(params);
widget->Show();
widget->Activate();
widget->GetNativeView()->SetProperty(aura::client::kResizeBehaviorKey,
ui::mojom::kResizeBehaviorCanMaximize);
ui::test::EventGenerator& generator = GetEventGenerator();
wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeView());
// Toggling not suppressed.
generator.PressKey(ui::VKEY_J, ui::EF_ALT_DOWN);
EXPECT_TRUE(window_state->IsFullscreen());
// The same accelerator - toggling suppressed.
generator.PressKey(ui::VKEY_J, ui::EF_ALT_DOWN | ui::EF_IS_REPEAT);
EXPECT_TRUE(window_state->IsFullscreen());
// Different accelerator.
generator.PressKey(ui::VKEY_K, ui::EF_ALT_DOWN);
EXPECT_FALSE(window_state->IsFullscreen());
}
// TODO(oshima): Fix this test to use EventGenerator.
#if defined(USE_X11)
TEST_F(AcceleratorControllerTest, ProcessOnce) {
// The IME event filter interferes with the basic key event propagation we
// attempt to do here, so we disable it.
DisableIME();
ui::Accelerator accelerator_a(ui::VKEY_A, ui::EF_NONE);
TestTarget target;
GetController()->Register({accelerator_a}, &target);
// The accelerator is processed only once.
ui::EventSink* sink = Shell::GetPrimaryRootWindow()->GetHost()->event_sink();
ui::ScopedXI2Event key_event;
key_event.InitKeyEvent(ui::ET_KEY_PRESSED, ui::VKEY_A, 0);
ui::KeyEvent key_event1(key_event);
ui::EventDispatchDetails details = sink->OnEventFromSource(&key_event1);
EXPECT_TRUE(key_event1.handled() || details.dispatcher_destroyed);
ui::KeyEvent key_event2('A', ui::VKEY_A, ui::EF_NONE);
details = sink->OnEventFromSource(&key_event2);
EXPECT_FALSE(key_event2.handled() || details.dispatcher_destroyed);
key_event.InitKeyEvent(ui::ET_KEY_RELEASED, ui::VKEY_A, 0);
ui::KeyEvent key_event3(key_event);
details = sink->OnEventFromSource(&key_event3);
EXPECT_FALSE(key_event3.handled() || details.dispatcher_destroyed);
EXPECT_EQ(1, target.accelerator_pressed_count());
}
#endif
TEST_F(AcceleratorControllerTest, GlobalAccelerators) {
// TODO: TestScreenshotDelegate is null in mash http://crbug.com/632111.
if (Shell::GetAshConfig() == Config::MASH)
return;
// CycleBackward
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_TAB, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN)));
// CycleForward
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_TAB, ui::EF_ALT_DOWN)));
// CycleLinear
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_NONE)));
// The "Take Screenshot", "Take Partial Screenshot", volume, brightness, and
// keyboard brightness accelerators are only defined on ChromeOS.
{
test::TestScreenshotDelegate* delegate = GetScreenshotDelegate();
delegate->set_can_take_screenshot(false);
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_CONTROL_DOWN)));
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_PRINT, ui::EF_NONE)));
EXPECT_TRUE(ProcessInController(ui::Accelerator(
ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN)));
delegate->set_can_take_screenshot(true);
EXPECT_EQ(0, delegate->handle_take_screenshot_count());
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_CONTROL_DOWN)));
EXPECT_EQ(1, delegate->handle_take_screenshot_count());
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_PRINT, ui::EF_NONE)));
EXPECT_EQ(2, delegate->handle_take_screenshot_count());
EXPECT_TRUE(ProcessInController(ui::Accelerator(
ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN)));
EXPECT_EQ(2, delegate->handle_take_screenshot_count());
}
const ui::Accelerator volume_mute(ui::VKEY_VOLUME_MUTE, ui::EF_NONE);
const ui::Accelerator volume_down(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
const ui::Accelerator volume_up(ui::VKEY_VOLUME_UP, ui::EF_NONE);
{
base::UserActionTester user_action_tester;
ui::AcceleratorHistory* history = GetController()->accelerator_history();
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
EXPECT_TRUE(ProcessInController(volume_mute));
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeMute_F8"));
EXPECT_EQ(volume_mute, history->current_accelerator());
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
EXPECT_TRUE(ProcessInController(volume_down));
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeDown_F9"));
EXPECT_EQ(volume_down, history->current_accelerator());
EXPECT_EQ(0, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
EXPECT_TRUE(ProcessInController(volume_up));
EXPECT_EQ(volume_up, history->current_accelerator());
EXPECT_EQ(1, user_action_tester.GetActionCount("Accel_VolumeUp_F10"));
}
// Brightness
// ui::VKEY_BRIGHTNESS_DOWN/UP are not defined on Windows.
const ui::Accelerator brightness_down(ui::VKEY_BRIGHTNESS_DOWN, ui::EF_NONE);
const ui::Accelerator brightness_up(ui::VKEY_BRIGHTNESS_UP, ui::EF_NONE);
{
DummyBrightnessControlDelegate* delegate =
new DummyBrightnessControlDelegate;
SetBrightnessControlDelegate(
std::unique_ptr<BrightnessControlDelegate>(delegate));
EXPECT_EQ(0, delegate->handle_brightness_down_count());
EXPECT_TRUE(ProcessInController(brightness_down));
EXPECT_EQ(1, delegate->handle_brightness_down_count());
EXPECT_EQ(brightness_down, delegate->last_accelerator());
EXPECT_EQ(0, delegate->handle_brightness_up_count());
EXPECT_TRUE(ProcessInController(brightness_up));
EXPECT_EQ(1, delegate->handle_brightness_up_count());
EXPECT_EQ(brightness_up, delegate->last_accelerator());
}
// Keyboard brightness
const ui::Accelerator alt_brightness_down(ui::VKEY_BRIGHTNESS_DOWN,
ui::EF_ALT_DOWN);
const ui::Accelerator alt_brightness_up(ui::VKEY_BRIGHTNESS_UP,
ui::EF_ALT_DOWN);
{
EXPECT_TRUE(ProcessInController(alt_brightness_down));
EXPECT_TRUE(ProcessInController(alt_brightness_up));
DummyKeyboardBrightnessControlDelegate* delegate =
new DummyKeyboardBrightnessControlDelegate;
SetKeyboardBrightnessControlDelegate(
std::unique_ptr<KeyboardBrightnessControlDelegate>(delegate));
EXPECT_EQ(0, delegate->handle_keyboard_brightness_down_count());
EXPECT_TRUE(ProcessInController(alt_brightness_down));
EXPECT_EQ(1, delegate->handle_keyboard_brightness_down_count());
EXPECT_EQ(alt_brightness_down, delegate->last_accelerator());
EXPECT_EQ(0, delegate->handle_keyboard_brightness_up_count());
EXPECT_TRUE(ProcessInController(alt_brightness_up));
EXPECT_EQ(1, delegate->handle_keyboard_brightness_up_count());
EXPECT_EQ(alt_brightness_up, delegate->last_accelerator());
}
// Exit
ExitWarningHandler* ewh = GetController()->GetExitWarningHandlerForTest();
ASSERT_TRUE(ewh);
StubForTest(ewh);
EXPECT_TRUE(is_idle(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_Q, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN)));
EXPECT_FALSE(is_idle(ewh));
EXPECT_TRUE(is_ui_shown(ewh));
SimulateTimerExpired(ewh);
EXPECT_TRUE(is_idle(ewh));
EXPECT_FALSE(is_ui_shown(ewh));
Reset(ewh);
// New tab
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_T, ui::EF_CONTROL_DOWN)));
// New incognito window
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_N, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN)));
// New window
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_N, ui::EF_CONTROL_DOWN)));
// Restore tab
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_T, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN)));
// Show task manager
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_ESCAPE, ui::EF_SHIFT_DOWN)));
// Open file manager
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_M, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN)));
// Lock screen
// NOTE: Accelerators that do not work on the lock screen need to be
// tested before the sequence below is invoked because it causes a side
// effect of locking the screen.
EXPECT_TRUE(
ProcessInController(ui::Accelerator(ui::VKEY_L, ui::EF_COMMAND_DOWN)));
}
TEST_F(AcceleratorControllerTest, GlobalAcceleratorsToggleAppList) {
app_list::test::TestAppListPresenter test_app_list_presenter;
Shell::Get()->app_list()->SetAppListPresenter(
test_app_list_presenter.CreateInterfacePtrAndBind());
AccessibilityDelegate* delegate = Shell::Get()->accessibility_delegate();
// The press event should not toggle the AppList, the release should instead.
EXPECT_FALSE(
ProcessInController(ui::Accelerator(ui::VKEY_LWIN, ui::EF_NONE)));
RunAllPendingInMessageLoop();
EXPECT_EQ(ui::VKEY_LWIN, GetCurrentAccelerator().key_code());
EXPECT_EQ(0u, test_app_list_presenter.toggle_count());
EXPECT_TRUE(ProcessInController(
CreateReleaseAccelerator(ui::VKEY_LWIN, ui::EF_NONE)));
RunAllPendingInMessageLoop();
EXPECT_EQ(1u, test_app_list_presenter.toggle_count());
EXPECT_EQ(ui::VKEY_LWIN, GetPreviousAccelerator().key_code());
// When spoken feedback is on, the AppList should not toggle.
delegate->ToggleSpokenFeedback(A11Y_NOTIFICATION_NONE);
EXPECT_FALSE(
ProcessInController(ui::Accelerator(ui::VKEY_LWIN, ui::EF_NONE)));
EXPECT_FALSE(ProcessInController(
CreateReleaseAccelerator(ui::VKEY_LWIN, ui::EF_NONE)));
delegate->ToggleSpokenFeedback(A11Y_NOTIFICATION_NONE);
RunAllPendingInMessageLoop();
EXPECT_EQ(1u, test_app_list_presenter.toggle_count());
// Turning off spoken feedback should allow the AppList to toggle again.
EXPECT_FALSE(
ProcessInController(ui::Accelerator(ui::VKEY_LWIN, ui::EF_NONE)));
EXPECT_TRUE(ProcessInController(
CreateReleaseAccelerator(ui::VKEY_LWIN, ui::EF_NONE)));
RunAllPendingInMessageLoop();
EXPECT_EQ(2u, test_app_list_presenter.toggle_count());
// The press of VKEY_BROWSER_SEARCH should toggle the AppList
EXPECT_TRUE(ProcessInController(
ui::Accelerator(ui::VKEY_BROWSER_SEARCH, ui::EF_NONE)));
RunAllPendingInMessageLoop();
EXPECT_EQ(3u, test_app_list_presenter.toggle_count());
EXPECT_FALSE(ProcessInController(
CreateReleaseAccelerator(ui::VKEY_BROWSER_SEARCH, ui::EF_NONE)));
RunAllPendingInMessageLoop();
EXPECT_EQ(3u, test_app_list_presenter.toggle_count());
}
TEST_F(AcceleratorControllerTest, ImeGlobalAccelerators) {
// Test IME shortcuts.
ui::Accelerator control_space_down(ui::VKEY_SPACE, ui::EF_CONTROL_DOWN);
ui::Accelerator control_space_up(ui::VKEY_SPACE, ui::EF_CONTROL_DOWN);
control_space_up.set_key_state(ui::Accelerator::KeyState::RELEASED);
const ui::Accelerator convert(ui::VKEY_CONVERT, ui::EF_NONE);
const ui::Accelerator non_convert(ui::VKEY_NONCONVERT, ui::EF_NONE);
const ui::Accelerator wide_half_1(ui::VKEY_DBE_SBCSCHAR, ui::EF_NONE);
const ui::Accelerator wide_half_2(ui::VKEY_DBE_DBCSCHAR, ui::EF_NONE);
const ui::Accelerator hangul(ui::VKEY_HANGUL, ui::EF_NONE);
EXPECT_FALSE(ProcessInController(control_space_down));
EXPECT_FALSE(ProcessInController(control_space_up));
EXPECT_FALSE(ProcessInController(convert));
EXPECT_FALSE(ProcessInController(non_convert));
EXPECT_FALSE(ProcessInController(wide_half_1));
EXPECT_FALSE(ProcessInController(wide_half_2));
EXPECT_FALSE(ProcessInController(hangul));
DummyImeControlDelegate* delegate = new DummyImeControlDelegate;
GetController()->SetImeControlDelegate(
std::unique_ptr<ImeControlDelegate>(delegate));
EXPECT_EQ(0, delegate->handle_previous_ime_count());
EXPECT_TRUE(ProcessInController(control_space_down));
EXPECT_EQ(1, delegate->handle_previous_ime_count());
EXPECT_TRUE(ProcessInController(control_space_up));
EXPECT_EQ(1, delegate->handle_previous_ime_count());
EXPECT_EQ(0, delegate->handle_switch_ime_count());
EXPECT_TRUE(ProcessInController(convert));
EXPECT_EQ(1, delegate->handle_switch_ime_count());
EXPECT_TRUE(ProcessInController(non_convert));
EXPECT_EQ(2, delegate->handle_switch_ime_count());
EXPECT_TRUE(ProcessInController(wide_half_1));
EXPECT_EQ(3, delegate->handle_switch_ime_count());
EXPECT_TRUE(ProcessInController(wide_half_2));
EXPECT_EQ(4, delegate->handle_switch_ime_count());
EXPECT_TRUE(ProcessInController(hangul));
EXPECT_EQ(5, delegate->handle_switch_ime_count());
}
// TODO(nona|mazda): Remove this when crbug.com/139556 in a better way.
TEST_F(AcceleratorControllerTest, ImeGlobalAcceleratorsWorkaround139556) {
// The workaround for crbug.com/139556 depends on the fact that we don't
// use Shift+Alt+Enter/Space with ET_KEY_PRESSED as an accelerator. Test it.
const ui::Accelerator shift_alt_return_press(
ui::VKEY_RETURN, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN);
EXPECT_FALSE(ProcessInController(shift_alt_return_press));
const ui::Accelerator shift_alt_space_press(
ui::VKEY_SPACE, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN);
EXPECT_FALSE(ProcessInController(shift_alt_space_press));
}
TEST_F(AcceleratorControllerTest, PreferredReservedAccelerators) {
// Power key is reserved on chromeos.
EXPECT_TRUE(GetController()->IsReserved(
ui::Accelerator(ui::VKEY_POWER, ui::EF_NONE)));
EXPECT_FALSE(GetController()->IsPreferred(
ui::Accelerator(ui::VKEY_POWER, ui::EF_NONE)));
// ALT+Tab are not reserved but preferred.
EXPECT_FALSE(GetController()->IsReserved(
ui::Accelerator(ui::VKEY_TAB, ui::EF_ALT_DOWN)));
EXPECT_FALSE(GetController()->IsReserved(
ui::Accelerator(ui::VKEY_TAB, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN)));
EXPECT_TRUE(GetController()->IsPreferred(
ui::Accelerator(ui::VKEY_TAB, ui::EF_ALT_DOWN)));
EXPECT_TRUE(GetController()->IsPreferred(
ui::Accelerator(ui::VKEY_TAB, ui::EF_SHIFT_DOWN | ui::EF_ALT_DOWN)));
// Others are not reserved nor preferred
EXPECT_FALSE(GetController()->IsReserved(
ui::Accelerator(ui::VKEY_PRINT, ui::EF_NONE)));
EXPECT_FALSE(GetController()->IsPreferred(
ui::Accelerator(ui::VKEY_PRINT, ui::EF_NONE)));
EXPECT_FALSE(
GetController()->IsReserved(ui::Accelerator(ui::VKEY_TAB, ui::EF_NONE)));
EXPECT_FALSE(
GetController()->IsPreferred(ui::Accelerator(ui::VKEY_TAB, ui::EF_NONE)));
EXPECT_FALSE(
GetController()->IsReserved(ui::Accelerator(ui::VKEY_A, ui::EF_NONE)));
EXPECT_FALSE(
GetController()->IsPreferred(ui::Accelerator(ui::VKEY_A, ui::EF_NONE)));
}
namespace {
class TestInputMethodManager
: public chromeos::input_method::MockInputMethodManager {
public:
TestInputMethodManager() = default;
~TestInputMethodManager() override = default;
// MockInputMethodManager:
chromeos::input_method::ImeKeyboard* GetImeKeyboard() override {
return &keyboard_;
}
private:
chromeos::input_method::FakeImeKeyboard keyboard_;
DISALLOW_COPY_AND_ASSIGN(TestInputMethodManager);
};
class ToggleCapsLockTest : public AcceleratorControllerTest {
public:
ToggleCapsLockTest() = default;
~ToggleCapsLockTest() override = default;
void SetUp() override {
AcceleratorControllerTest::SetUp();
chromeos::input_method::InputMethodManager::Initialize(
new TestInputMethodManager);
}
void TearDown() override {
chromeos::input_method::InputMethodManager::Shutdown();
AcceleratorControllerTest::TearDown();
}
private:
DISALLOW_COPY_AND_ASSIGN(ToggleCapsLockTest);
};
// Tests the five combinations of the TOGGLE_CAPS_LOCK accelerator.
TEST_F(ToggleCapsLockTest, ToggleCapsLockAccelerators) {
chromeos::input_method::InputMethodManager* input_method_manager =
chromeos::input_method::InputMethodManager::Get();
ASSERT_TRUE(input_method_manager);
EXPECT_FALSE(input_method_manager->GetImeKeyboard()->CapsLockIsEnabled());
// 1. Press Alt, Press Search, Release Search, Release Alt.
// Note when you press Alt then press search, the key_code at this point is
// VKEY_LWIN (for search) and Alt is the modifier.
const ui::Accelerator press_alt_then_search(ui::VKEY_LWIN, ui::EF_ALT_DOWN);
EXPECT_FALSE(ProcessInController(press_alt_then_search));
// When you release Search before Alt, the key_code is still VKEY_LWIN and
// Alt is still the modifier.
const ui::Accelerator release_search_before_alt(
CreateReleaseAccelerator(ui::VKEY_LWIN, ui::EF_ALT_DOWN));
EXPECT_TRUE(ProcessInController(release_search_before_alt));
EXPECT_TRUE(input_method_manager->GetImeKeyboard()->CapsLockIsEnabled());
input_method_manager->GetImeKeyboard()->SetCapsLockEnabled(false);
// 2. Press Search, Press Alt, Release Search, Release Alt.
const ui::Accelerator press_search_then_alt(ui::VKEY_MENU,
ui::EF_COMMAND_DOWN);
EXPECT_FALSE(ProcessInController(press_search_then_alt));
EXPECT_TRUE(ProcessInController(release_search_before_alt));
EXPECT_TRUE(input_method_manager->GetImeKeyboard()->CapsLockIsEnabled());
input_method_manager->GetImeKeyboard()->SetCapsLockEnabled(false);