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/plugin/mac_key_event_processor.h"
6 #include "remoting/proto/event.pb.h"
7 #include "remoting/protocol/protocol_mock_objects.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using ::testing::InSequence
;
12 using remoting::protocol::InputStub
;
13 using remoting::protocol::KeyEvent
;
14 using remoting::protocol::MockInputStub
;
15 using remoting::protocol::MouseEvent
;
21 const unsigned int kUsbLeftShift
= 0x0700e1;
22 const unsigned int kUsbLeftOption
= 0x0700e2;
23 const unsigned int kUsbLeftCmd
= 0x0700e3;
24 const unsigned int kUsbRightCmd
= 0x0700e7;
26 MATCHER_P2(EqualsUsbEvent
, usb_keycode
, pressed
, "") {
27 return arg
.usb_keycode() == static_cast<uint32
>(usb_keycode
) &&
28 arg
.pressed() == pressed
;
31 KeyEvent
MakeKeyEvent(int keycode
, bool pressed
) {
33 event
.set_usb_keycode(keycode
);
34 event
.set_pressed(pressed
);
40 // Test without pressing command key.
41 TEST(MacKeyEventProcessorTest
, NoInjection
) {
43 MacKeyEventProcessor
processor(&stub
);
48 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
49 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
53 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
54 processor
.InjectKeyEvent(MakeKeyEvent('C', false));
57 // Test pressing command key and other normal keys.
58 TEST(MacKeyEventProcessorTest
, CmdKey
) {
60 MacKeyEventProcessor
processor(&stub
);
66 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, true)));
67 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
68 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
69 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, false)));
72 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, true)));
73 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
74 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
75 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, false)));
77 // More than one keys after CMD.
78 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, true)));
79 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
80 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('V', true)));
81 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
82 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('V', false)));
83 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, false)));
87 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, true));
88 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
89 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, false));
92 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, true));
93 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
94 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, false));
96 // More than one keys after CMD.
97 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, true));
98 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
99 processor
.InjectKeyEvent(MakeKeyEvent('V', true));
100 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, false));
103 // Test pressing command and special keys.
104 TEST(MacKeyEventProcessorTest
, SpecialKeys
) {
106 MacKeyEventProcessor
processor(&stub
);
112 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, true)));
113 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftShift
, true)));
114 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, false)));
115 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftShift
, false)));
118 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, true)));
119 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftOption
, true)));
120 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, false)));
121 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftOption
, false)));
125 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, true));
126 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftShift
, true));
127 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, false));
128 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftShift
, false));
131 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, true));
132 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftOption
, true));
133 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, false));
134 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftOption
, false));
137 // Test pressing multiple command keys.
138 TEST(MacKeyEventProcessorTest
, MultipleCmdKeys
) {
140 MacKeyEventProcessor
processor(&stub
);
145 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, true)));
146 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
147 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, true)));
148 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
149 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd
, false)));
152 // Test multiple CMD keys at the same time.
153 // L CMD Down, C Down, R CMD Down, L CMD Up.
154 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, true));
155 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
156 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, true));
157 processor
.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd
, false));
160 // Test press C key before command key.
161 TEST(MacKeyEventProcessorTest
, BeforeCmdKey
) {
163 MacKeyEventProcessor
processor(&stub
);
168 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', true)));
169 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, true)));
170 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
171 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd
, false)));
172 EXPECT_CALL(stub
, InjectKeyEvent(EqualsUsbEvent('C', false)));
175 // Press C before command key.
176 processor
.InjectKeyEvent(MakeKeyEvent('C', true));
177 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, true));
178 processor
.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd
, false));
179 processor
.InjectKeyEvent(MakeKeyEvent('C', false));
182 } // namespace remoting