| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "remoting/client/key_event_mapper.h" |
| 6 |
| 7 #include "remoting/proto/event.pb.h" |
| 8 #include "remoting/protocol/protocol_mock_objects.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 using ::testing::_; |
| 13 using ::testing::ExpectationSet; |
| 14 using ::testing::InSequence; |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 using protocol::InputStub; |
| 19 using protocol::KeyEvent; |
| 20 using protocol::MockInputStub; |
| 21 |
| 22 MATCHER_P2(EqualsVkeyEvent, keycode, pressed, "") { |
| 23 return arg.keycode() == keycode && arg.pressed() == pressed; |
| 24 } |
| 25 |
| 26 MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") { |
| 27 return arg.usb_keycode() == static_cast<uint32>(usb_keycode) && |
| 28 arg.pressed() == pressed; |
| 29 } |
| 30 |
| 31 static KeyEvent NewVkeyEvent(int keycode, bool pressed) { |
| 32 KeyEvent event; |
| 33 event.set_keycode(keycode); |
| 34 event.set_pressed(pressed); |
| 35 return event; |
| 36 } |
| 37 |
| 38 static void PressAndReleaseVkey(InputStub* input_stub, int keycode) { |
| 39 input_stub->InjectKeyEvent(NewVkeyEvent(keycode, true)); |
| 40 input_stub->InjectKeyEvent(NewVkeyEvent(keycode, false)); |
| 41 } |
| 42 |
| 43 static KeyEvent NewUsbEvent(uint32 usb_keycode, bool pressed) { |
| 44 KeyEvent event; |
| 45 event.set_usb_keycode(usb_keycode); |
| 46 event.set_pressed(pressed); |
| 47 return event; |
| 48 } |
| 49 |
| 50 static void PressAndReleaseUsb(InputStub* input_stub, |
| 51 uint32 usb_keycode) { |
| 52 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, true)); |
| 53 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, false)); |
| 54 } |
| 55 |
| 56 static void InjectTestSequence(InputStub* input_stub) { |
| 57 for (int i = 1; i <= 5; ++i) |
| 58 PressAndReleaseUsb(input_stub, i); |
| 59 } |
| 60 |
| 61 // Verify that keys are passed through the KeyEventMapper by default. |
| 62 TEST(KeyEventMapperTest, NoMappingOrTrapping) { |
| 63 MockInputStub mock_stub; |
| 64 KeyEventMapper event_mapper(&mock_stub); |
| 65 |
| 66 { |
| 67 InSequence s; |
| 68 |
| 69 for (int i = 1; i <= 5; ++i) { |
| 70 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(i, true))); |
| 71 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(i, false))); |
| 72 } |
| 73 |
| 74 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, true))); |
| 75 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, false))); |
| 76 } |
| 77 |
| 78 InjectTestSequence(&event_mapper); |
| 79 PressAndReleaseVkey(&event_mapper, 3); |
| 80 } |
| 81 |
| 82 // Verify that USB keys are remapped at most once, and VKEYs are not mapped. |
| 83 TEST(KeyEventMapperTest, RemapKeys) { |
| 84 MockInputStub mock_stub; |
| 85 KeyEventMapper event_mapper(&mock_stub); |
| 86 event_mapper.RemapKey(3, 4); |
| 87 event_mapper.RemapKey(4, 3); |
| 88 event_mapper.RemapKey(5, 3); |
| 89 |
| 90 { |
| 91 InSequence s; |
| 92 |
| 93 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); |
| 94 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); |
| 95 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); |
| 96 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); |
| 97 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(4, true))); |
| 98 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(4, false))); |
| 99 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); |
| 100 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))); |
| 101 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); |
| 102 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))); |
| 103 |
| 104 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, true))); |
| 105 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, false))); |
| 106 } |
| 107 |
| 108 InjectTestSequence(&event_mapper); |
| 109 PressAndReleaseVkey(&event_mapper, 3); |
| 110 } |
| 111 |
| 112 static void HandleTrappedKey(MockInputStub* stub, uint32 keycode, bool down) { |
| 113 stub->InjectKeyEvent(NewUsbEvent(keycode, down)); |
| 114 } |
| 115 |
| 116 // Verify that USB keys are trapped, not remapped, and VKEYs are not trapped. |
| 117 TEST(KeyEventMapperTest, TrapKeys) { |
| 118 MockInputStub mock_stub; |
| 119 MockInputStub trap_stub; |
| 120 KeyEventMapper event_mapper(&mock_stub); |
| 121 KeyEventMapper::KeyTrapCallback callback = |
| 122 base::Bind(&HandleTrappedKey, base::Unretained(&trap_stub)); |
| 123 event_mapper.SetTrapCallback(callback); |
| 124 event_mapper.TrapKey(4, true); |
| 125 event_mapper.TrapKey(5, true); |
| 126 event_mapper.RemapKey(3, 4); |
| 127 event_mapper.RemapKey(4, 3); |
| 128 event_mapper.RemapKey(5, 3); |
| 129 |
| 130 { |
| 131 InSequence s; |
| 132 |
| 133 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); |
| 134 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); |
| 135 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); |
| 136 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); |
| 137 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(4, true))); |
| 138 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(4, false))); |
| 139 |
| 140 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsUsbEvent(4, true))); |
| 141 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsUsbEvent(4, false))); |
| 142 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsUsbEvent(5, true))); |
| 143 EXPECT_CALL(trap_stub, InjectKeyEvent(EqualsUsbEvent(5, false))); |
| 144 |
| 145 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, true))); |
| 146 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(3, false))); |
| 147 } |
| 148 |
| 149 InjectTestSequence(&event_mapper); |
| 150 PressAndReleaseVkey(&event_mapper, 3); |
| 151 } |
| 152 |
| 153 } // namespace remoting |
| OLD | NEW |