diff options
author | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-04 17:26:37 +0000 |
---|---|---|
committer | wez@chromium.org <wez@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-04 17:26:37 +0000 |
commit | fa8c7292d70c39c154a3957835d01190c82fdc7c (patch) | |
tree | 512d95d2ec9066af5c412a6fb01c30ee1602d871 /remoting/protocol/input_event_tracker_unittest.cc | |
parent | 360ba059f7200d138c19bcd6a0593b1e45d44d31 (diff) | |
download | chromium_src-fa8c7292d70c39c154a3957835d01190c82fdc7c.zip chromium_src-fa8c7292d70c39c154a3957835d01190c82fdc7c.tar.gz chromium_src-fa8c7292d70c39c154a3957835d01190c82fdc7c.tar.bz2 |
Fix InputEventTracker to track key events based on both VK- and USB-keycodes.
This CL also fixes InputEventTracker to cope with events with missing fields, switches the tracker unit-tests to concentrate on USB-based key events, and adds some tests of handling VK-based, VK&USB-based and invalid key events.
Previously the InputEventTracker required that all events have a VK-style keycode set, and used that to track the pressed keys. When switching to USB-style events, clients may not transmit a VK-style keycode, or may send a dummy value, which would result in events being incorrectly tracked.
BUG=121696
Review URL: https://chromiumcodereview.appspot.com/9963117
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130661 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting/protocol/input_event_tracker_unittest.cc')
-rw-r--r-- | remoting/protocol/input_event_tracker_unittest.cc | 196 |
1 files changed, 157 insertions, 39 deletions
diff --git a/remoting/protocol/input_event_tracker_unittest.cc b/remoting/protocol/input_event_tracker_unittest.cc index 8d40532..38e820e 100644 --- a/remoting/protocol/input_event_tracker_unittest.cc +++ b/remoting/protocol/input_event_tracker_unittest.cc @@ -5,6 +5,7 @@ #include "remoting/protocol/input_event_tracker.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" @@ -13,46 +14,67 @@ using ::testing::ExpectationSet; using ::testing::InSequence; namespace remoting { +namespace protocol { -static const protocol::MouseEvent::MouseButton BUTTON_LEFT = - protocol::MouseEvent::BUTTON_LEFT; -static const protocol::MouseEvent::MouseButton BUTTON_RIGHT = - protocol::MouseEvent::BUTTON_RIGHT; +static const MouseEvent::MouseButton BUTTON_LEFT = MouseEvent::BUTTON_LEFT; +static const MouseEvent::MouseButton BUTTON_RIGHT = MouseEvent::BUTTON_RIGHT; -MATCHER_P2(EqualsKeyEvent, keycode, pressed, "") { +MATCHER_P2(EqualsVkeyEvent, keycode, pressed, "") { return arg.keycode() == keycode && arg.pressed() == pressed; } +MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") { + return arg.usb_keycode() == static_cast<uint32>(usb_keycode) && + arg.pressed() == pressed; +} + +MATCHER_P3(EqualsVkeyUsbEvent, keycode, usb_keycode, pressed, "") { + return arg.keycode() == keycode && arg.pressed() == pressed && + arg.usb_keycode() == static_cast<uint32>(usb_keycode); +} + MATCHER_P4(EqualsMouseEvent, x, y, button, down, "") { return arg.x() == x && arg.y() == y && arg.button() == button && arg.button_down() == down; } -class MockInputStub : public protocol::InputStub { - public: - MockInputStub() {} +static KeyEvent NewVkeyEvent(int keycode, bool pressed) { + KeyEvent event; + event.set_keycode(keycode); + event.set_pressed(pressed); + return event; +} - MOCK_METHOD1(InjectKeyEvent, void(const protocol::KeyEvent&)); - MOCK_METHOD1(InjectMouseEvent, void(const protocol::MouseEvent&)); - private: - DISALLOW_COPY_AND_ASSIGN(MockInputStub); -}; +static void PressAndReleaseVkey(InputStub* input_stub, int keycode) { + input_stub->InjectKeyEvent(NewVkeyEvent(keycode, true)); + input_stub->InjectKeyEvent(NewVkeyEvent(keycode, false)); +} -static protocol::KeyEvent NewKeyEvent(int keycode, bool pressed) { - protocol::KeyEvent event; - event.set_keycode(keycode); +static KeyEvent NewUsbEvent(uint32 usb_keycode, bool pressed) { + KeyEvent event; + event.set_usb_keycode(usb_keycode); event.set_pressed(pressed); return event; } -static void PressAndReleaseKey(protocol::InputStub* input_stub, int keycode) { - input_stub->InjectKeyEvent(NewKeyEvent(keycode, true)); - input_stub->InjectKeyEvent(NewKeyEvent(keycode, false)); +static void PressAndReleaseUsb(InputStub* input_stub, + uint32 usb_keycode) { + input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, true)); + input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, false)); +} + +static KeyEvent NewVkeyUsbEvent(int keycode, int usb_keycode, + bool pressed) { + KeyEvent event; + event.set_keycode(keycode); + event.set_usb_keycode(usb_keycode); + event.set_pressed(pressed); + return event; } -static protocol::MouseEvent NewMouseEvent(int x, int y, - protocol::MouseEvent::MouseButton button, bool down) { - protocol::MouseEvent event; +static MouseEvent NewMouseEvent(int x, int y, + MouseEvent::MouseButton button, bool down) { + MouseEvent event; event.set_x(x); event.set_y(y); event.set_button(button); @@ -63,15 +85,15 @@ static protocol::MouseEvent NewMouseEvent(int x, int y, // Verify that keys that were pressed and released aren't re-released. TEST(InputEventTrackerTest, NothingToRelease) { MockInputStub mock_stub; - protocol::InputEventTracker input_tracker(&mock_stub); + InputEventTracker input_tracker(&mock_stub); { InSequence s; - EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(1, true))); - EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(1, false))); - EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(2, true))); - EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(2, false))); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); EXPECT_CALL(mock_stub, InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_LEFT, true))); @@ -79,8 +101,8 @@ TEST(InputEventTrackerTest, NothingToRelease) { InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_LEFT, false))); } - PressAndReleaseKey(&input_tracker, 1); - PressAndReleaseKey(&input_tracker, 2); + PressAndReleaseUsb(&input_tracker, 1); + PressAndReleaseUsb(&input_tracker, 2); input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, true)); input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, false)); @@ -91,17 +113,17 @@ TEST(InputEventTrackerTest, NothingToRelease) { // Verify that keys that were left pressed get released. TEST(InputEventTrackerTest, ReleaseAllKeys) { MockInputStub mock_stub; - protocol::InputEventTracker input_tracker(&mock_stub); + InputEventTracker input_tracker(&mock_stub); ExpectationSet injects; { InSequence s; - injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(3, true))); - injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(1, true))); - injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(1, false))); - injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(2, true))); - injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(2, false))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); injects += EXPECT_CALL(mock_stub, InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_RIGHT, true))); @@ -111,15 +133,15 @@ TEST(InputEventTrackerTest, ReleaseAllKeys) { InjectMouseEvent(EqualsMouseEvent(1, 1, BUTTON_LEFT, false))); } - EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsKeyEvent(3, false))) + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) .After(injects); EXPECT_CALL(mock_stub, InjectMouseEvent(EqualsMouseEvent(1, 1, BUTTON_RIGHT, false))) .After(injects); - input_tracker.InjectKeyEvent(NewKeyEvent(3, true)); - PressAndReleaseKey(&input_tracker, 1); - PressAndReleaseKey(&input_tracker, 2); + input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); + PressAndReleaseUsb(&input_tracker, 1); + PressAndReleaseUsb(&input_tracker, 2); input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_RIGHT, true)); input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, true)); @@ -128,4 +150,100 @@ TEST(InputEventTrackerTest, ReleaseAllKeys) { input_tracker.ReleaseAll(); } +// Verify that we track both VK- and USB-based key events correctly. +TEST(InputEventTrackerTest, TrackVkeyAndUsb) { + MockInputStub mock_stub; + InputEventTracker input_tracker(&mock_stub); + ExpectationSet injects; + + { + InSequence s; + + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(1, true))); + injects += EXPECT_CALL(mock_stub, + InjectKeyEvent(EqualsVkeyEvent(1, false))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, true))); + injects += EXPECT_CALL(mock_stub, + InjectKeyEvent(EqualsVkeyUsbEvent(5, 6, true))); + injects += EXPECT_CALL(mock_stub, + InjectKeyEvent(EqualsVkeyUsbEvent(5, 7, true))); + injects += EXPECT_CALL(mock_stub, + InjectKeyEvent(EqualsVkeyUsbEvent(6, 5, true))); + injects += EXPECT_CALL(mock_stub, + InjectKeyEvent(EqualsVkeyUsbEvent(7, 5, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); + } + + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyUsbEvent(5, 6, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyUsbEvent(5, 7, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyUsbEvent(6, 5, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyUsbEvent(7, 5, false))) + .After(injects); + + input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); + PressAndReleaseVkey(&input_tracker, 1); + input_tracker.InjectKeyEvent(NewVkeyEvent(4, true)); + input_tracker.InjectKeyEvent(NewVkeyUsbEvent(5, 6, true)); + input_tracker.InjectKeyEvent(NewVkeyUsbEvent(5, 7, true)); + input_tracker.InjectKeyEvent(NewVkeyUsbEvent(6, 5, true)); + input_tracker.InjectKeyEvent(NewVkeyUsbEvent(7, 5, true)); + PressAndReleaseUsb(&input_tracker, 2); + + input_tracker.ReleaseAll(); +} + +// Verify that invalid events get passed through but not tracked. +TEST(InputEventTrackerTest, InvalidEventsNotTracked) { + MockInputStub mock_stub; + InputEventTracker input_tracker(&mock_stub); + ExpectationSet injects; + + { + InSequence s; + + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(3); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true))); + injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false))); + } + + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false))) + .After(injects); + EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsVkeyEvent(4, false))) + .After(injects); + + input_tracker.InjectKeyEvent(NewUsbEvent(3, true)); + PressAndReleaseUsb(&input_tracker, 1); + + KeyEvent invalid_event1; + invalid_event1.set_pressed(true); + input_tracker.InjectKeyEvent(invalid_event1); + + KeyEvent invalid_event2; + invalid_event2.set_keycode(5); + input_tracker.InjectKeyEvent(invalid_event2); + + KeyEvent invalid_event3; + invalid_event3.set_usb_keycode(6); + input_tracker.InjectKeyEvent(invalid_event3); + + input_tracker.InjectKeyEvent(NewVkeyEvent(4, true)); + PressAndReleaseUsb(&input_tracker, 2); + + input_tracker.ReleaseAll(); +} + +} // namespace protocol } // namespace remoting |