1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/accelerators/accelerator_manager.h"
7 #include "base/compiler_specific.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/events/event_constants.h"
10 #include "ui/events/keycodes/keyboard_codes.h"
17 class TestTarget
: public AcceleratorTarget
{
19 TestTarget() : accelerator_pressed_count_(0) {}
20 ~TestTarget() override
{}
22 int accelerator_pressed_count() const {
23 return accelerator_pressed_count_
;
26 void set_accelerator_pressed_count(int accelerator_pressed_count
) {
27 accelerator_pressed_count_
= accelerator_pressed_count
;
30 // Overridden from AcceleratorTarget:
31 bool AcceleratorPressed(const Accelerator
& accelerator
) override
;
32 bool CanHandleAccelerators() const override
;
35 int accelerator_pressed_count_
;
37 DISALLOW_COPY_AND_ASSIGN(TestTarget
);
40 bool TestTarget::AcceleratorPressed(const Accelerator
& accelerator
) {
41 ++accelerator_pressed_count_
;
45 bool TestTarget::CanHandleAccelerators() const {
49 Accelerator
GetAccelerator(KeyboardCode code
, int mask
) {
50 return Accelerator(code
, mask
);
55 class AcceleratorManagerTest
: public testing::Test
{
57 AcceleratorManagerTest() {}
58 ~AcceleratorManagerTest() override
{}
60 AcceleratorManager manager_
;
63 TEST_F(AcceleratorManagerTest
, Register
) {
64 const Accelerator
accelerator_a(VKEY_A
, EF_NONE
);
66 manager_
.Register(accelerator_a
, AcceleratorManager::kNormalPriority
,
69 // The registered accelerator is processed.
70 EXPECT_TRUE(manager_
.Process(accelerator_a
));
71 EXPECT_EQ(1, target
.accelerator_pressed_count());
74 TEST_F(AcceleratorManagerTest
, RegisterMultipleTarget
) {
75 const Accelerator
accelerator_a(VKEY_A
, EF_NONE
);
77 manager_
.Register(accelerator_a
, AcceleratorManager::kNormalPriority
,
80 manager_
.Register(accelerator_a
, AcceleratorManager::kNormalPriority
,
83 // If multiple targets are registered with the same accelerator, the target
84 // registered later processes the accelerator.
85 EXPECT_TRUE(manager_
.Process(accelerator_a
));
86 EXPECT_EQ(0, target1
.accelerator_pressed_count());
87 EXPECT_EQ(1, target2
.accelerator_pressed_count());
90 TEST_F(AcceleratorManagerTest
, Unregister
) {
91 const Accelerator
accelerator_a(VKEY_A
, EF_NONE
);
93 manager_
.Register(accelerator_a
, AcceleratorManager::kNormalPriority
,
95 const Accelerator
accelerator_b(VKEY_B
, EF_NONE
);
96 manager_
.Register(accelerator_b
, AcceleratorManager::kNormalPriority
,
99 // Unregistering a different accelerator does not affect the other
101 manager_
.Unregister(accelerator_b
, &target
);
102 EXPECT_TRUE(manager_
.Process(accelerator_a
));
103 EXPECT_EQ(1, target
.accelerator_pressed_count());
105 // The unregistered accelerator is no longer processed.
106 target
.set_accelerator_pressed_count(0);
107 manager_
.Unregister(accelerator_a
, &target
);
108 EXPECT_FALSE(manager_
.Process(accelerator_a
));
109 EXPECT_EQ(0, target
.accelerator_pressed_count());
112 TEST_F(AcceleratorManagerTest
, UnregisterAll
) {
113 const Accelerator
accelerator_a(VKEY_A
, EF_NONE
);
115 manager_
.Register(accelerator_a
, AcceleratorManager::kNormalPriority
,
117 const Accelerator
accelerator_b(VKEY_B
, EF_NONE
);
118 manager_
.Register(accelerator_b
, AcceleratorManager::kNormalPriority
,
120 const Accelerator
accelerator_c(VKEY_C
, EF_NONE
);
122 manager_
.Register(accelerator_c
, AcceleratorManager::kNormalPriority
,
124 manager_
.UnregisterAll(&target1
);
126 // All the accelerators registered for |target1| are no longer processed.
127 EXPECT_FALSE(manager_
.Process(accelerator_a
));
128 EXPECT_FALSE(manager_
.Process(accelerator_b
));
129 EXPECT_EQ(0, target1
.accelerator_pressed_count());
131 // UnregisterAll with a different target does not affect the other target.
132 EXPECT_TRUE(manager_
.Process(accelerator_c
));
133 EXPECT_EQ(1, target2
.accelerator_pressed_count());
136 TEST_F(AcceleratorManagerTest
, Process
) {
139 // Test all 2*2*2 cases (shift/control/alt = on/off).
140 for (int mask
= 0; mask
< 2 * 2 * 2; ++mask
) {
141 Accelerator
accelerator(GetAccelerator(VKEY_A
, mask
));
142 const base::string16 text
= accelerator
.GetShortcutText();
143 manager_
.Register(accelerator
, AcceleratorManager::kNormalPriority
,
146 // The registered accelerator is processed.
147 const int last_count
= target
.accelerator_pressed_count();
148 EXPECT_TRUE(manager_
.Process(accelerator
)) << text
;
149 EXPECT_EQ(last_count
+ 1, target
.accelerator_pressed_count()) << text
;
151 // The non-registered accelerators are not processed.
152 accelerator
.set_type(ET_UNKNOWN
);
153 EXPECT_FALSE(manager_
.Process(accelerator
)) << text
; // different type
154 accelerator
.set_type(ET_TRANSLATED_KEY_PRESS
);
155 EXPECT_FALSE(manager_
.Process(accelerator
)) << text
; // different type
156 accelerator
.set_type(ET_KEY_RELEASED
);
157 EXPECT_FALSE(manager_
.Process(accelerator
)) << text
; // different type
158 accelerator
.set_type(ET_TRANSLATED_KEY_RELEASE
);
159 EXPECT_FALSE(manager_
.Process(accelerator
)) << text
; // different type
161 EXPECT_FALSE(manager_
.Process(GetAccelerator(VKEY_UNKNOWN
, mask
)))
162 << text
; // different vkey
163 EXPECT_FALSE(manager_
.Process(GetAccelerator(VKEY_B
, mask
)))
164 << text
; // different vkey
165 EXPECT_FALSE(manager_
.Process(GetAccelerator(VKEY_SHIFT
, mask
)))
166 << text
; // different vkey
168 for (int test_mask
= 0; test_mask
< 2 * 2 * 2; ++test_mask
) {
169 if (test_mask
== mask
)
171 const Accelerator
test_accelerator(GetAccelerator(VKEY_A
, test_mask
));
172 const base::string16 test_text
= test_accelerator
.GetShortcutText();
173 EXPECT_FALSE(manager_
.Process(test_accelerator
))
174 << text
<< ", " << test_text
; // different modifiers
177 EXPECT_EQ(last_count
+ 1, target
.accelerator_pressed_count()) << text
;
178 manager_
.UnregisterAll(&target
);