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
|
// 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 "remoting/client/plugin/mac_key_event_processor.h"
#include "remoting/proto/event.pb.h"
#include "remoting/protocol/protocol_mock_objects.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::InSequence;
using remoting::protocol::InputStub;
using remoting::protocol::KeyEvent;
using remoting::protocol::MockInputStub;
using remoting::protocol::MouseEvent;
namespace remoting {
namespace {
const unsigned int kUsbLeftShift = 0x0700e1;
const unsigned int kUsbLeftOption = 0x0700e2;
const unsigned int kUsbLeftCmd = 0x0700e3;
const unsigned int kUsbRightCmd = 0x0700e7;
MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") {
return arg.usb_keycode() == static_cast<uint32>(usb_keycode) &&
arg.pressed() == pressed;
}
KeyEvent MakeKeyEvent(int keycode, bool pressed) {
KeyEvent event;
event.set_usb_keycode(keycode);
event.set_pressed(pressed);
return event;
}
} // namespace
// Test without pressing command key.
TEST(MacKeyEventProcessorTest, NoInjection) {
MockInputStub stub;
MacKeyEventProcessor processor(&stub);
{
InSequence s;
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
}
// C Down and C Up.
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent('C', false));
}
// Test pressing command key and other normal keys.
TEST(MacKeyEventProcessorTest, CmdKey) {
MockInputStub stub;
MacKeyEventProcessor processor(&stub);
{
InSequence s;
// Left command key.
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, false)));
// Right command key.
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, false)));
// More than one keys after CMD.
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('V', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('V', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, false)));
}
// Left command key.
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, true));
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, false));
// Right command key.
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, true));
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, false));
// More than one keys after CMD.
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, true));
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent('V', true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, false));
}
// Test pressing command and special keys.
TEST(MacKeyEventProcessorTest, SpecialKeys) {
MockInputStub stub;
MacKeyEventProcessor processor(&stub);
{
InSequence s;
// Command + Shift.
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftShift, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftShift, false)));
// Command + Option.
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftOption, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftOption, false)));
}
// Command + Shift.
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftShift, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, false));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftShift, false));
// Command + Option.
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftOption, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, false));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftOption, false));
}
// Test pressing multiple command keys.
TEST(MacKeyEventProcessorTest, MultipleCmdKeys) {
MockInputStub stub;
MacKeyEventProcessor processor(&stub);
{
InSequence s;
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbLeftCmd, false)));
}
// Test multiple CMD keys at the same time.
// L CMD Down, C Down, R CMD Down, L CMD Up.
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, true));
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbLeftCmd, false));
}
// Test press C key before command key.
TEST(MacKeyEventProcessorTest, BeforeCmdKey) {
MockInputStub stub;
MacKeyEventProcessor processor(&stub);
{
InSequence s;
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, true)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent(kUsbRightCmd, false)));
EXPECT_CALL(stub, InjectKeyEvent(EqualsUsbEvent('C', false)));
}
// Press C before command key.
processor.InjectKeyEvent(MakeKeyEvent('C', true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, true));
processor.InjectKeyEvent(MakeKeyEvent(kUsbRightCmd, false));
processor.InjectKeyEvent(MakeKeyEvent('C', false));
}
} // namespace remoting
|