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 "remoting/client/key_event_mapper.h"
8 #include "remoting/proto/event.pb.h"
9 #include "remoting/protocol/protocol_mock_objects.h"
10 #include "remoting/protocol/test_event_matchers.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
15 using ::testing::ExpectationSet
;
16 using ::testing::InSequence
;
20 using protocol::InputStub
;
21 using protocol::KeyEvent
;
22 using protocol::MockInputStub
;
23 using protocol::test::EqualsKeyEventWithCapsLock
;
25 static KeyEvent
NewUsbEvent(uint32 usb_keycode
,
29 event
.set_usb_keycode(usb_keycode
);
30 event
.set_pressed(pressed
);
31 event
.set_lock_states(lock_states
);
36 static void PressAndReleaseUsb(InputStub
* input_stub
, uint32 usb_keycode
) {
37 input_stub
->InjectKeyEvent(
38 NewUsbEvent(usb_keycode
, true, KeyEvent::LOCK_STATES_CAPSLOCK
));
39 input_stub
->InjectKeyEvent(
40 NewUsbEvent(usb_keycode
, false, KeyEvent::LOCK_STATES_CAPSLOCK
));
43 static void InjectTestSequence(InputStub
* input_stub
) {
44 for (int i
= 1; i
<= 5; ++i
)
45 PressAndReleaseUsb(input_stub
, i
);
48 // Verify that keys are passed through the KeyEventMapper by default.
49 TEST(KeyEventMapperTest
, NoMappingOrTrapping
) {
50 MockInputStub mock_stub
;
51 KeyEventMapper
event_mapper(&mock_stub
);
56 for (int i
= 1; i
<= 5; ++i
) {
57 EXPECT_CALL(mock_stub
,
58 InjectKeyEvent(EqualsKeyEventWithCapsLock(i
, true)));
59 EXPECT_CALL(mock_stub
,
60 InjectKeyEvent(EqualsKeyEventWithCapsLock(i
, false)));
63 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
64 EXPECT_CALL(mock_stub
,
65 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
68 InjectTestSequence(&event_mapper
);
69 PressAndReleaseUsb(&event_mapper
, 3);
72 // Verify that USB keys are remapped at most once.
73 TEST(KeyEventMapperTest
, RemapKeys
) {
74 MockInputStub mock_stub
;
75 KeyEventMapper
event_mapper(&mock_stub
);
76 event_mapper
.RemapKey(3, 4);
77 event_mapper
.RemapKey(4, 3);
78 event_mapper
.RemapKey(5, 3);
83 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(1, true)));
84 EXPECT_CALL(mock_stub
,
85 InjectKeyEvent(EqualsKeyEventWithCapsLock(1, false)));
86 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(2, true)));
87 EXPECT_CALL(mock_stub
,
88 InjectKeyEvent(EqualsKeyEventWithCapsLock(2, false)));
89 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
90 EXPECT_CALL(mock_stub
,
91 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
92 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
93 EXPECT_CALL(mock_stub
,
94 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
95 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
96 EXPECT_CALL(mock_stub
,
97 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
100 InjectTestSequence(&event_mapper
);
103 static void HandleTrappedKey(MockInputStub
* stub
, const KeyEvent
& event
) {
104 stub
->InjectKeyEvent(event
);
107 // Verify that trapped and mapped USB keys are trapped but not remapped.
108 TEST(KeyEventMapperTest
, TrapKeys
) {
109 MockInputStub mock_stub
;
110 MockInputStub trap_stub
;
111 KeyEventMapper
event_mapper(&mock_stub
);
112 KeyEventMapper::KeyTrapCallback callback
=
113 base::Bind(&HandleTrappedKey
, base::Unretained(&trap_stub
));
114 event_mapper
.SetTrapCallback(callback
);
115 event_mapper
.TrapKey(4, true);
116 event_mapper
.TrapKey(5, true);
117 event_mapper
.RemapKey(3, 4);
118 event_mapper
.RemapKey(4, 3);
119 event_mapper
.RemapKey(5, 3);
124 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(1, true)));
125 EXPECT_CALL(mock_stub
,
126 InjectKeyEvent(EqualsKeyEventWithCapsLock(1, false)));
127 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(2, true)));
128 EXPECT_CALL(mock_stub
,
129 InjectKeyEvent(EqualsKeyEventWithCapsLock(2, false)));
130 EXPECT_CALL(mock_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
131 EXPECT_CALL(mock_stub
,
132 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
134 EXPECT_CALL(trap_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
135 EXPECT_CALL(trap_stub
,
136 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
137 EXPECT_CALL(trap_stub
, InjectKeyEvent(EqualsKeyEventWithCapsLock(5, true)));
138 EXPECT_CALL(trap_stub
,
139 InjectKeyEvent(EqualsKeyEventWithCapsLock(5, false)));
142 InjectTestSequence(&event_mapper
);
145 } // namespace remoting