| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/input_event_tracker.h" | 5 #include "remoting/protocol/input_event_tracker.h" |
| 6 | 6 |
| 7 #include "remoting/proto/event.pb.h" | 7 #include "remoting/proto/event.pb.h" |
| 8 #include "remoting/protocol/protocol_mock_objects.h" | 8 #include "remoting/protocol/protocol_mock_objects.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 using ::testing::_; | 12 using ::testing::_; |
| 13 using ::testing::ExpectationSet; | 13 using ::testing::ExpectationSet; |
| 14 using ::testing::InSequence; | 14 using ::testing::InSequence; |
| 15 | 15 |
| 16 namespace remoting { | 16 namespace remoting { |
| 17 namespace protocol { | 17 namespace protocol { |
| 18 | 18 |
| 19 static const MouseEvent::MouseButton BUTTON_LEFT = MouseEvent::BUTTON_LEFT; | 19 static const MouseEvent::MouseButton BUTTON_LEFT = MouseEvent::BUTTON_LEFT; |
| 20 static const MouseEvent::MouseButton BUTTON_RIGHT = MouseEvent::BUTTON_RIGHT; | 20 static const MouseEvent::MouseButton BUTTON_RIGHT = MouseEvent::BUTTON_RIGHT; |
| 21 | 21 |
| 22 MATCHER_P2(EqualsVkeyEvent, keycode, pressed, "") { | |
| 23 return arg.keycode() == keycode && arg.pressed() == pressed; | |
| 24 } | |
| 25 | |
| 26 MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") { | 22 MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") { |
| 27 return arg.usb_keycode() == static_cast<uint32>(usb_keycode) && | 23 return arg.usb_keycode() == static_cast<uint32>(usb_keycode) && |
| 28 arg.pressed() == pressed; | 24 arg.pressed() == pressed; |
| 29 } | 25 } |
| 30 | 26 |
| 31 MATCHER_P4(EqualsMouseEvent, x, y, button, down, "") { | 27 MATCHER_P4(EqualsMouseEvent, x, y, button, down, "") { |
| 32 return arg.x() == x && arg.y() == y && arg.button() == button && | 28 return arg.x() == x && arg.y() == y && arg.button() == button && |
| 33 arg.button_down() == down; | 29 arg.button_down() == down; |
| 34 } | 30 } |
| 35 | 31 |
| 36 static KeyEvent NewVkeyEvent(int keycode, bool pressed) { | |
| 37 KeyEvent event; | |
| 38 event.set_keycode(keycode); | |
| 39 event.set_pressed(pressed); | |
| 40 return event; | |
| 41 } | |
| 42 | |
| 43 static void PressAndReleaseVkey(InputStub* input_stub, int keycode) { | |
| 44 input_stub->InjectKeyEvent(NewVkeyEvent(keycode, true)); | |
| 45 input_stub->InjectKeyEvent(NewVkeyEvent(keycode, false)); | |
| 46 } | |
| 47 | |
| 48 static KeyEvent NewUsbEvent(uint32 usb_keycode, bool pressed) { | 32 static KeyEvent NewUsbEvent(uint32 usb_keycode, bool pressed) { |
| 49 KeyEvent event; | 33 KeyEvent event; |
| 50 event.set_usb_keycode(usb_keycode); | 34 event.set_usb_keycode(usb_keycode); |
| 51 event.set_pressed(pressed); | 35 event.set_pressed(pressed); |
| 52 return event; | 36 return event; |
| 53 } | 37 } |
| 54 | 38 |
| 55 static void PressAndReleaseUsb(InputStub* input_stub, | 39 static void PressAndReleaseUsb(InputStub* input_stub, |
| 56 uint32 usb_keycode) { | 40 uint32 usb_keycode) { |
| 57 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, true)); | 41 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, true)); |
| 58 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, false)); | 42 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, false)); |
| 59 } | 43 } |
| 60 | 44 |
| 61 static KeyEvent NewVkeyUsbEvent(int keycode, int usb_keycode, | |
| 62 bool pressed) { | |
| 63 KeyEvent event; | |
| 64 event.set_keycode(keycode); | |
| 65 event.set_usb_keycode(usb_keycode); | |
| 66 event.set_pressed(pressed); | |
| 67 return event; | |
| 68 } | |
| 69 | |
| 70 static MouseEvent NewMouseEvent(int x, int y, | 45 static MouseEvent NewMouseEvent(int x, int y, |
| 71 MouseEvent::MouseButton button, bool down) { | 46 MouseEvent::MouseButton button, bool down) { |
| 72 MouseEvent event; | 47 MouseEvent event; |
| 73 event.set_x(x); | 48 event.set_x(x); |
| 74 event.set_y(y); | 49 event.set_y(y); |
| 75 event.set_button(button); | 50 event.set_button(button); |
| 76 event.set_button_down(down); | 51 event.set_button_down(down); |
| 77 return event; | 52 return event; |
| 78 } | 53 } |
| 79 | 54 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 input_tracker.InjectMouseEvent(NewMouseEvent(1, 1, BUTTON_LEFT, false)); | 118 input_tracker.InjectMouseEvent(NewMouseEvent(1, 1, BUTTON_LEFT, false)); |
| 144 | 119 |
| 145 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); | 120 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); |
| 146 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); | 121 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); |
| 147 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); | 122 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); |
| 148 EXPECT_EQ(1, input_tracker.PressedKeyCount()); | 123 EXPECT_EQ(1, input_tracker.PressedKeyCount()); |
| 149 | 124 |
| 150 input_tracker.ReleaseAll(); | 125 input_tracker.ReleaseAll(); |
| 151 } | 126 } |
| 152 | 127 |
| 153 // Verify that we track both VK- and USB-based key events correctly. | 128 // Verify that we track both USB-based key events correctly. |
| 154 TEST(InputEventTrackerTest, TrackVkeyAndUsb) { | 129 TEST(InputEventTrackerTest, TrackUsbKeyEvents) { |
| 155 MockInputStub mock_stub; | 130 MockInputStub mock_stub; |
| 156 InputEventTracker input_tracker(&mock_stub); | 131 InputEventTracker input_tracker(&mock_stub); |
| 157 ExpectationSet injects; | 132 ExpectationSet injects; |
| 158 | 133 |
| 159 { | 134 { |
| 160 InSequence s; | 135 InSequence s; |
| 161 | 136 |
| 162 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); | 137 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); |
| 163 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(1, true))); | |
| 164 injects += EXPECT_CALL(mock_stub, | |
| 165 InjectKeyEvent(EqualsVkeyEvent(1, false))); | |
| 166 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, true))); | |
| 167 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, true))); | 138 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, true))); |
| 168 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, true))); | 139 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, true))); |
| 169 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true))); | 140 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true))); |
| 170 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true))); | 141 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true))); |
| 171 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); | 142 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); |
| 172 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); | 143 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); |
| 173 } | 144 } |
| 174 | 145 |
| 175 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) | 146 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) |
| 176 .After(injects); | 147 .After(injects); |
| 177 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, false))) | |
| 178 .After(injects); | |
| 179 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, false))) | 148 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, false))) |
| 180 .After(injects); | 149 .After(injects); |
| 181 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, false))) | 150 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, false))) |
| 182 .After(injects); | 151 .After(injects); |
| 183 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, false))) | 152 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, false))) |
| 184 .After(injects); | 153 .After(injects); |
| 185 | 154 |
| 186 input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); | 155 input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); |
| 187 PressAndReleaseVkey(&input_tracker, 1); | 156 input_tracker.InjectKeyEvent(NewUsbEvent(6, true)); |
| 188 input_tracker.InjectKeyEvent(NewVkeyEvent(4, true)); | 157 input_tracker.InjectKeyEvent(NewUsbEvent(7, true)); |
| 189 input_tracker.InjectKeyEvent(NewVkeyUsbEvent(5, 6, true)); | 158 input_tracker.InjectKeyEvent(NewUsbEvent(5, true)); |
| 190 input_tracker.InjectKeyEvent(NewVkeyUsbEvent(5, 7, true)); | 159 input_tracker.InjectKeyEvent(NewUsbEvent(5, true)); |
| 191 input_tracker.InjectKeyEvent(NewVkeyUsbEvent(6, 5, true)); | |
| 192 input_tracker.InjectKeyEvent(NewVkeyUsbEvent(7, 5, true)); | |
| 193 PressAndReleaseUsb(&input_tracker, 2); | 160 PressAndReleaseUsb(&input_tracker, 2); |
| 194 | 161 |
| 195 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); | 162 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); |
| 196 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); | 163 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); |
| 197 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); | 164 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); |
| 198 EXPECT_FALSE(input_tracker.IsKeyPressed(4)); // 4 was a VKEY. | |
| 199 EXPECT_TRUE(input_tracker.IsKeyPressed(5)); | 165 EXPECT_TRUE(input_tracker.IsKeyPressed(5)); |
| 200 EXPECT_TRUE(input_tracker.IsKeyPressed(6)); | 166 EXPECT_TRUE(input_tracker.IsKeyPressed(6)); |
| 201 EXPECT_TRUE(input_tracker.IsKeyPressed(7)); | 167 EXPECT_TRUE(input_tracker.IsKeyPressed(7)); |
| 202 EXPECT_EQ(5, input_tracker.PressedKeyCount()); | 168 EXPECT_EQ(4, input_tracker.PressedKeyCount()); |
| 203 | 169 |
| 204 input_tracker.ReleaseAll(); | 170 input_tracker.ReleaseAll(); |
| 205 } | 171 } |
| 206 | 172 |
| 207 // Verify that invalid events get passed through but not tracked. | 173 // Verify that invalid events get passed through but not tracked. |
| 208 TEST(InputEventTrackerTest, InvalidEventsNotTracked) { | 174 TEST(InputEventTrackerTest, InvalidEventsNotTracked) { |
| 209 MockInputStub mock_stub; | 175 MockInputStub mock_stub; |
| 210 InputEventTracker input_tracker(&mock_stub); | 176 InputEventTracker input_tracker(&mock_stub); |
| 211 ExpectationSet injects; | 177 ExpectationSet injects; |
| 212 | 178 |
| 213 { | 179 { |
| 214 InSequence s; | 180 InSequence s; |
| 215 | 181 |
| 216 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); | 182 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); |
| 217 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); | 183 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); |
| 218 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); | 184 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); |
| 219 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(3); | 185 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(2); |
| 220 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, true))); | |
| 221 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); | 186 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); |
| 222 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); | 187 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); |
| 223 } | 188 } |
| 224 | 189 |
| 225 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) | 190 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) |
| 226 .After(injects); | 191 .After(injects); |
| 227 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, false))) | |
| 228 .After(injects); | |
| 229 | 192 |
| 230 input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); | 193 input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); |
| 231 PressAndReleaseUsb(&input_tracker, 1); | 194 PressAndReleaseUsb(&input_tracker, 1); |
| 232 | 195 |
| 233 KeyEvent invalid_event1; | 196 KeyEvent invalid_event1; |
| 234 invalid_event1.set_pressed(true); | 197 invalid_event1.set_pressed(true); |
| 235 input_tracker.InjectKeyEvent(invalid_event1); | 198 input_tracker.InjectKeyEvent(invalid_event1); |
| 236 | 199 |
| 237 KeyEvent invalid_event2; | 200 KeyEvent invalid_event2; |
| 238 invalid_event2.set_keycode(5); | 201 invalid_event2.set_usb_keycode(6); |
| 239 input_tracker.InjectKeyEvent(invalid_event2); | 202 input_tracker.InjectKeyEvent(invalid_event2); |
| 240 | 203 |
| 241 KeyEvent invalid_event3; | |
| 242 invalid_event3.set_usb_keycode(6); | |
| 243 input_tracker.InjectKeyEvent(invalid_event3); | |
| 244 | |
| 245 input_tracker.InjectKeyEvent(NewVkeyEvent(4, true)); | |
| 246 PressAndReleaseUsb(&input_tracker, 2); | 204 PressAndReleaseUsb(&input_tracker, 2); |
| 247 | 205 |
| 248 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); | 206 EXPECT_FALSE(input_tracker.IsKeyPressed(1)); |
| 249 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); | 207 EXPECT_FALSE(input_tracker.IsKeyPressed(2)); |
| 250 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); | 208 EXPECT_TRUE(input_tracker.IsKeyPressed(3)); |
| 251 EXPECT_FALSE(input_tracker.IsKeyPressed(4)); // Injected as VKEY. | 209 EXPECT_EQ(1, input_tracker.PressedKeyCount()); |
| 252 EXPECT_EQ(2, input_tracker.PressedKeyCount()); | |
| 253 | 210 |
| 254 input_tracker.ReleaseAll(); | 211 input_tracker.ReleaseAll(); |
| 255 } | 212 } |
| 256 | 213 |
| 257 } // namespace protocol | 214 } // namespace protocol |
| 258 } // namespace remoting | 215 } // namespace remoting |
| OLD | NEW |