[Android] Fixed chrome://history's overlapped search and clear(x) icons on RTL
[chromium-blink-merge.git] / remoting / protocol / input_event_tracker_unittest.cc
blob6f44b9ec0699a894fc3b219fcb767261c3168da0
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/protocol/input_event_tracker.h"
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"
12 using ::testing::_;
13 using ::testing::ExpectationSet;
14 using ::testing::InSequence;
16 namespace remoting {
17 namespace protocol {
19 static const MouseEvent::MouseButton BUTTON_LEFT = MouseEvent::BUTTON_LEFT;
20 static const MouseEvent::MouseButton BUTTON_RIGHT = MouseEvent::BUTTON_RIGHT;
22 MATCHER_P2(EqualsUsbEvent, usb_keycode, pressed, "") {
23 return arg.usb_keycode() == static_cast<uint32>(usb_keycode) &&
24 arg.pressed() == pressed;
27 MATCHER_P4(EqualsMouseEvent, x, y, button, down, "") {
28 return arg.x() == x && arg.y() == y && arg.button() == button &&
29 arg.button_down() == down;
32 static KeyEvent NewUsbEvent(uint32 usb_keycode, bool pressed) {
33 KeyEvent event;
34 event.set_usb_keycode(usb_keycode);
35 event.set_pressed(pressed);
36 return event;
39 static void PressAndReleaseUsb(InputStub* input_stub,
40 uint32 usb_keycode) {
41 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, true));
42 input_stub->InjectKeyEvent(NewUsbEvent(usb_keycode, false));
45 static MouseEvent NewMouseEvent(int x, int y,
46 MouseEvent::MouseButton button, bool down) {
47 MouseEvent event;
48 event.set_x(x);
49 event.set_y(y);
50 event.set_button(button);
51 event.set_button_down(down);
52 return event;
55 // Verify that keys that were pressed and released aren't re-released.
56 TEST(InputEventTrackerTest, NothingToRelease) {
57 MockInputStub mock_stub;
58 InputEventTracker input_tracker(&mock_stub);
61 InSequence s;
63 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true)));
64 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false)));
65 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true)));
66 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false)));
68 EXPECT_CALL(mock_stub,
69 InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_LEFT, true)));
70 EXPECT_CALL(mock_stub,
71 InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_LEFT, false)));
74 PressAndReleaseUsb(&input_tracker, 1);
75 PressAndReleaseUsb(&input_tracker, 2);
77 input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, true));
78 input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, false));
80 input_tracker.ReleaseAll();
83 // Verify that keys that were left pressed get released.
84 TEST(InputEventTrackerTest, ReleaseAllKeys) {
85 MockInputStub mock_stub;
86 InputEventTracker input_tracker(&mock_stub);
87 ExpectationSet injects;
90 InSequence s;
92 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true)));
93 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true)));
94 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false)));
95 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true)));
96 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false)));
98 injects += EXPECT_CALL(mock_stub,
99 InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_RIGHT, true)));
100 injects += EXPECT_CALL(mock_stub,
101 InjectMouseEvent(EqualsMouseEvent(0, 0, BUTTON_LEFT, true)));
102 injects += EXPECT_CALL(mock_stub,
103 InjectMouseEvent(EqualsMouseEvent(1, 1, BUTTON_LEFT, false)));
106 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false)))
107 .After(injects);
108 EXPECT_CALL(mock_stub,
109 InjectMouseEvent(EqualsMouseEvent(1, 1, BUTTON_RIGHT, false)))
110 .After(injects);
112 input_tracker.InjectKeyEvent(NewUsbEvent(3, true));
113 PressAndReleaseUsb(&input_tracker, 1);
114 PressAndReleaseUsb(&input_tracker, 2);
116 input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_RIGHT, true));
117 input_tracker.InjectMouseEvent(NewMouseEvent(0, 0, BUTTON_LEFT, true));
118 input_tracker.InjectMouseEvent(NewMouseEvent(1, 1, BUTTON_LEFT, false));
120 EXPECT_FALSE(input_tracker.IsKeyPressed(1));
121 EXPECT_FALSE(input_tracker.IsKeyPressed(2));
122 EXPECT_TRUE(input_tracker.IsKeyPressed(3));
123 EXPECT_EQ(1, input_tracker.PressedKeyCount());
125 input_tracker.ReleaseAll();
128 // Verify that we track both USB-based key events correctly.
129 TEST(InputEventTrackerTest, TrackUsbKeyEvents) {
130 MockInputStub mock_stub;
131 InputEventTracker input_tracker(&mock_stub);
132 ExpectationSet injects;
135 InSequence s;
137 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true)));
138 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, true)));
139 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, true)));
140 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true)));
141 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, true)));
142 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true)));
143 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false)));
146 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false)))
147 .After(injects);
148 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(6, false)))
149 .After(injects);
150 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(7, false)))
151 .After(injects);
152 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(5, false)))
153 .After(injects);
155 input_tracker.InjectKeyEvent(NewUsbEvent(3, true));
156 input_tracker.InjectKeyEvent(NewUsbEvent(6, true));
157 input_tracker.InjectKeyEvent(NewUsbEvent(7, true));
158 input_tracker.InjectKeyEvent(NewUsbEvent(5, true));
159 input_tracker.InjectKeyEvent(NewUsbEvent(5, true));
160 PressAndReleaseUsb(&input_tracker, 2);
162 EXPECT_FALSE(input_tracker.IsKeyPressed(1));
163 EXPECT_FALSE(input_tracker.IsKeyPressed(2));
164 EXPECT_TRUE(input_tracker.IsKeyPressed(3));
165 EXPECT_TRUE(input_tracker.IsKeyPressed(5));
166 EXPECT_TRUE(input_tracker.IsKeyPressed(6));
167 EXPECT_TRUE(input_tracker.IsKeyPressed(7));
168 EXPECT_EQ(4, input_tracker.PressedKeyCount());
170 input_tracker.ReleaseAll();
173 // Verify that invalid events get passed through but not tracked.
174 TEST(InputEventTrackerTest, InvalidEventsNotTracked) {
175 MockInputStub mock_stub;
176 InputEventTracker input_tracker(&mock_stub);
177 ExpectationSet injects;
180 InSequence s;
182 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, true)));
183 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, true)));
184 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(1, false)));
185 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(_)).Times(2);
186 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, true)));
187 injects += EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(2, false)));
190 EXPECT_CALL(mock_stub, InjectKeyEvent(EqualsUsbEvent(3, false)))
191 .After(injects);
193 input_tracker.InjectKeyEvent(NewUsbEvent(3, true));
194 PressAndReleaseUsb(&input_tracker, 1);
196 KeyEvent invalid_event1;
197 invalid_event1.set_pressed(true);
198 input_tracker.InjectKeyEvent(invalid_event1);
200 KeyEvent invalid_event2;
201 invalid_event2.set_usb_keycode(6);
202 input_tracker.InjectKeyEvent(invalid_event2);
204 PressAndReleaseUsb(&input_tracker, 2);
206 EXPECT_FALSE(input_tracker.IsKeyPressed(1));
207 EXPECT_FALSE(input_tracker.IsKeyPressed(2));
208 EXPECT_TRUE(input_tracker.IsKeyPressed(3));
209 EXPECT_EQ(1, input_tracker.PressedKeyCount());
211 input_tracker.ReleaseAll();
214 } // namespace protocol
215 } // namespace remoting