Convert AddUserStory to AddStory for perf/
[chromium-blink-merge.git] / remoting / client / key_event_mapper_unittest.cc
blob22c42a6d3af5b6234f9eb4a96a512a394d8e17fb
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"
7 #include "remoting/proto/event.pb.h"
8 #include "remoting/protocol/protocol_mock_objects.h"
9 #include "remoting/protocol/test_event_matchers.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using ::testing::_;
14 using ::testing::ExpectationSet;
15 using ::testing::InSequence;
17 namespace remoting {
19 using protocol::InputStub;
20 using protocol::KeyEvent;
21 using protocol::MockInputStub;
22 using protocol::test::EqualsKeyEventWithCapsLock;
24 static KeyEvent NewUsbEvent(uint32 usb_keycode,
25 bool pressed,
26 uint32 lock_states) {
27 KeyEvent event;
28 event.set_usb_keycode(usb_keycode);
29 event.set_pressed(pressed);
30 event.set_lock_states(lock_states);
32 return event;
35 static void PressAndReleaseUsb(InputStub* input_stub, uint32 usb_keycode) {
36 input_stub->InjectKeyEvent(
37 NewUsbEvent(usb_keycode, true, KeyEvent::LOCK_STATES_CAPSLOCK));
38 input_stub->InjectKeyEvent(
39 NewUsbEvent(usb_keycode, false, KeyEvent::LOCK_STATES_CAPSLOCK));
42 static void InjectTestSequence(InputStub* input_stub) {
43 for (int i = 1; i <= 5; ++i)
44 PressAndReleaseUsb(input_stub, i);
47 // Verify that keys are passed through the KeyEventMapper by default.
48 TEST(KeyEventMapperTest, NoMappingOrTrapping) {
49 MockInputStub mock_stub;
50 KeyEventMapper event_mapper(&mock_stub);
53 InSequence s;
55 for (int i = 1; i <= 5; ++i) {
56 EXPECT_CALL(mock_stub,
57 InjectKeyEvent(EqualsKeyEventWithCapsLock(i, true)));
58 EXPECT_CALL(mock_stub,
59 InjectKeyEvent(EqualsKeyEventWithCapsLock(i, false)));
62 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
63 EXPECT_CALL(mock_stub,
64 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
67 InjectTestSequence(&event_mapper);
68 PressAndReleaseUsb(&event_mapper, 3);
71 // Verify that USB keys are remapped at most once.
72 TEST(KeyEventMapperTest, RemapKeys) {
73 MockInputStub mock_stub;
74 KeyEventMapper event_mapper(&mock_stub);
75 event_mapper.RemapKey(3, 4);
76 event_mapper.RemapKey(4, 3);
77 event_mapper.RemapKey(5, 3);
80 InSequence s;
82 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(1, true)));
83 EXPECT_CALL(mock_stub,
84 InjectKeyEvent(EqualsKeyEventWithCapsLock(1, false)));
85 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(2, true)));
86 EXPECT_CALL(mock_stub,
87 InjectKeyEvent(EqualsKeyEventWithCapsLock(2, false)));
88 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
89 EXPECT_CALL(mock_stub,
90 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
91 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
92 EXPECT_CALL(mock_stub,
93 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
94 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(3, true)));
95 EXPECT_CALL(mock_stub,
96 InjectKeyEvent(EqualsKeyEventWithCapsLock(3, false)));
99 InjectTestSequence(&event_mapper);
102 static void HandleTrappedKey(MockInputStub* stub, const KeyEvent& event) {
103 stub->InjectKeyEvent(event);
106 // Verify that trapped and mapped USB keys are trapped but not remapped.
107 TEST(KeyEventMapperTest, TrapKeys) {
108 MockInputStub mock_stub;
109 MockInputStub trap_stub;
110 KeyEventMapper event_mapper(&mock_stub);
111 KeyEventMapper::KeyTrapCallback callback =
112 base::Bind(&HandleTrappedKey, base::Unretained(&trap_stub));
113 event_mapper.SetTrapCallback(callback);
114 event_mapper.TrapKey(4, true);
115 event_mapper.TrapKey(5, true);
116 event_mapper.RemapKey(3, 4);
117 event_mapper.RemapKey(4, 3);
118 event_mapper.RemapKey(5, 3);
121 InSequence s;
123 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(1, true)));
124 EXPECT_CALL(mock_stub,
125 InjectKeyEvent(EqualsKeyEventWithCapsLock(1, false)));
126 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(2, true)));
127 EXPECT_CALL(mock_stub,
128 InjectKeyEvent(EqualsKeyEventWithCapsLock(2, false)));
129 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
130 EXPECT_CALL(mock_stub,
131 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
133 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(4, true)));
134 EXPECT_CALL(trap_stub,
135 InjectKeyEvent(EqualsKeyEventWithCapsLock(4, false)));
136 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsKeyEventWithCapsLock(5, true)));
137 EXPECT_CALL(trap_stub,
138 InjectKeyEvent(EqualsKeyEventWithCapsLock(5, false)));
141 InjectTestSequence(&event_mapper);
144 } // namespace remoting