Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Side by Side Diff: remoting/android/javatests/src/org/chromium/chromoting/MockInputStub.java

Issue 2066683003: [Chromoting] Add InputInjector and InputInjectorWrapper for easy unittesting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resolve review comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 package org.chromium.chromoting;
6
7 import android.graphics.PointF;
8
9 import junit.framework.Assert;
10
11 import org.chromium.base.annotations.SuppressFBWarnings;
12 import org.chromium.chromoting.jni.TouchEventData;
13
14 import java.util.Arrays;
15 import java.util.LinkedList;
16
17 /**
18 * A mock implementation of {@link InputInjector} for testing purpose.
19 */
20 public final class MockInputStub extends Assert implements InputStub {
21 /** The base class to store an event, which represents an user activity. */
22 public abstract static class Event<T> {
23 public void assertEventEquals(T other) {
24 if (other == this) {
25 return;
26 }
27 if (other == null) {
28 fail();
29 }
30
31 assertContentMatch(other);
32 }
33
34 protected abstract void assertContentMatch(T other);
35 }
36
37 /** A mouse event. */
38 public static final class MouseEvent extends Event<MouseEvent> {
39 private final int mX;
40 private final int mY;
41 private final int mButton;
42 private final boolean mDown;
43
44 public MouseEvent(int x, int y, int button, boolean down) {
45 mX = x;
46 mY = y;
47 mButton = button;
48 mDown = down;
49 }
50
51 @Override
52 protected void assertContentMatch(MouseEvent other) {
53 assertEquals(mX, other.mX);
54 assertEquals(mY, other.mY);
55 assertEquals(mButton, other.mButton);
56 assertEquals(mDown, other.mDown);
57 }
58 }
59
60 /** A mouse wheel event. */
61 public static final class WheelEvent extends Event<WheelEvent> {
62 private final int mDeltaX;
63 private final int mDeltaY;
64
65 public WheelEvent(int deltaX, int deltaY) {
66 mDeltaX = deltaX;
67 mDeltaY = deltaY;
68 }
69
70 @Override
71 protected void assertContentMatch(WheelEvent other) {
72 assertEquals(mDeltaX, other.mDeltaX);
73 assertEquals(mDeltaY, other.mDeltaY);
74 }
75 }
76
77 /** A keyboard event. */
78 public static final class KeyEvent extends Event<KeyEvent> {
79 private final int mScanCode;
80 private final int mKeyCode;
81 private final boolean mKeyDown;
82
83 public KeyEvent(int scanCode, int keyCode, boolean keyDown) {
84 mScanCode = scanCode;
85 mKeyCode = keyCode;
86 mKeyDown = keyDown;
87 }
88
89 @Override
90 protected void assertContentMatch(KeyEvent other) {
91 assertEquals(mScanCode, other.mScanCode);
92 assertEquals(mKeyCode, other.mKeyCode);
93 assertEquals(mKeyDown, other.mKeyDown);
94 }
95 }
96
97 /** A text event. */
98 public static final class TextEvent extends Event<TextEvent> {
99 public final String mText;
100
101 public TextEvent(String text) {
102 mText = text;
103 }
104
105 @Override
106 protected void assertContentMatch(TextEvent other) {
107 if (mText == null && other.mText == null) {
108 return;
109 }
110 if (mText == null || other.mText == null) {
111 fail();
112 }
113 assertEquals(mText, other.mText);
114 }
115 }
116
117 /** A touch event. */
118 public static final class TouchEvent extends Event<TouchEvent> {
119 /**
120 * A random number to represent an invalid touch id for {@link assertCon tentMatch}.
121 * Consumers can use this number to ignore the comparison of
122 * {@link TouchEventData#getTouchPointId()} when comparing.
123 */
124 public static final int INVALID_ID = -883;
125
126 /**
127 * A random number to represent an invalid position for {@link #assertCo ntentMatch}.
128 * Consumers can use this number to ignore certain float fields in an {@ link TouchEventData}
129 * when comparing.
130 */
131 public static final float INVALID_POSITION = -763.273f;
132
133 /**
134 * A number to represent an invalid angle in radians for {@link #assertC ontentMatch}.
135 * Consumers can use this number to ignore the comparison of
136 * {@link TouchEventData#getTouchPointAngle} when comparing.
137 */
138 public static final float INVALID_RADIANS = 6.289f * (float) Math.PI;
139
140 // TouchEventData stores degrees instead of radians
141 private static final float INVALID_DEGREES = (float) Math.toDegrees(INVA LID_RADIANS);
142 private final TouchEventData.EventType mEventType;
143 private final TouchEventData[] mData;
144
145 public TouchEvent(TouchEventData.EventType eventType, TouchEventData[] d ata) {
146 mEventType = eventType;
147 mData = (data == null ? null : Arrays.copyOf(data, data.length));
148 }
149
150 private static boolean idMatch(int left, int right) {
151 return left == right || left == INVALID_ID || right == INVALID_ID;
152 }
153
154 private static boolean positionMatch(float left, float right) {
155 return left == right || left == INVALID_POSITION || right == INVALID _POSITION;
156 }
157
158 private static boolean degreeMatch(float left, float right) {
159 return left == right || left == INVALID_DEGREES || right == INVALID_ DEGREES;
160 }
161
162 private static void assertContentMatch(TouchEventData left, TouchEventDa ta right) {
163 assertTrue(idMatch(left.getTouchPointId(), right.getTouchPointId())) ;
164 assertTrue(positionMatch(left.getTouchPointX(), right.getTouchPointX ()));
165 assertTrue(positionMatch(left.getTouchPointY(), right.getTouchPointY ()));
166 assertTrue(positionMatch(left.getTouchPointRadiusX(), right.getTouch PointRadiusX()));
167 assertTrue(positionMatch(left.getTouchPointRadiusY(), right.getTouch PointRadiusY()));
168 assertTrue(degreeMatch(left.getTouchPointAngle(), right.getTouchPoin tAngle()));
169 assertTrue(positionMatch(left.getTouchPointPressure(), right.getTouc hPointPressure()));
170 }
171
172 private static void assertDataEquals(TouchEventData left, TouchEventData right) {
173 if (left == null && right == null) {
174 return;
175 }
176 if (left == null || right == null) {
177 fail();
178 }
179 assertContentMatch(left, right);
180 }
181
182 private int dataSize() {
183 return mData == null ? 0 : mData.length;
184 }
185
186 @Override
187 protected void assertContentMatch(TouchEvent other) {
188 assertEquals(mEventType, other.mEventType);
189 // An event with an empty mData array will match an event with a non -empty mData array.
190 // This is useful for tests which expect a TouchEvent with a certain EventType, but
191 // don't care about the content of mData.
192 if (dataSize() != 0 && other.dataSize() != 0) {
193 assertEquals(dataSize(), other.dataSize());
194 for (int i = 0; i < dataSize(); i++) {
195 assertDataEquals(mData[i], other.mData[i]);
196 }
197 }
198 }
199 }
200
201 private final LinkedList<MouseEvent> mMouseEvents;
202 private final LinkedList<WheelEvent> mWheelEvents;
203 private final LinkedList<KeyEvent> mKeyEvents;
204 private final LinkedList<TextEvent> mTextEvents;
205 private final LinkedList<TouchEvent> mTouchEvents;
206
207 public MockInputStub() {
208 mMouseEvents = new LinkedList<>();
209 mWheelEvents = new LinkedList<>();
210 mKeyEvents = new LinkedList<>();
211 mTextEvents = new LinkedList<>();
212 mTouchEvents = new LinkedList<>();
213 }
214
215 /**
216 * Compares the first |right|.length events with the ones in |left|, asserts they are equal, and
217 * consumes these events from |left|.
218 */
219 private static <E extends Event<E>> void assertContains(
220 LinkedList<E> left, E[] right) {
221 if (left == null && right == null) {
222 return;
223 }
224 if (left == null || right == null) {
225 fail();
226 }
227 assertTrue("left.size() " + left.size() + " != right.length " + right.le ngth,
228 left.size() >= right.length);
229 for (int i = 0; i < right.length; i++) {
230 E leftEvent = left.removeFirst();
231 if (leftEvent == null) {
232 assertNull(right[i]);
233 } else {
234 leftEvent.assertEventEquals(right[i]);
235 }
236 }
237 }
238
239 public void clear() {
240 mMouseEvents.clear();
241 mWheelEvents.clear();
242 mKeyEvents.clear();
243 mTextEvents.clear();
244 mTouchEvents.clear();
245 }
246
247 /**
248 * Compares the first |other|.length events with {@link #mMouseEvents} in th is instance,
249 * asserts they are equal, and consumes these events in this instance.
250 */
251 public void assertContainsMouseEvents(MouseEvent[] other) {
252 assertContains(mMouseEvents, other);
253 }
254
255 /**
256 * Compares the first |other|.length events with {@link #mTouchEvents} in th is instance,
257 * asserts they are equal, and consumes these events in this instance.
258 */
259 public void assertContainsTouchEvents(TouchEvent[] other) {
260 assertContains(mTouchEvents, other);
261 }
262
263 /** Asserts current instance is empty. */
264 public void assertEmpty() {
265 assertTrue(mMouseEvents.isEmpty());
266 assertTrue(mWheelEvents.isEmpty());
267 assertTrue(mKeyEvents.isEmpty());
268 assertTrue(mTextEvents.isEmpty());
269 assertTrue(mTouchEvents.isEmpty());
270 }
271
272 /**
273 * Checks whether the first two events in {@link #mTouchEvents} are represen ting a down and
274 * an up action at specified position |x|, |y|, and consumes these events.
275 */
276 public void assertTapInjected(float x, float y) {
277 assertTouchEventInjected(TouchEventData.EventType.TOUCH_EVENT_START, x, y);
278 assertTouchEventInjected(TouchEventData.EventType.TOUCH_EVENT_END, x, y) ;
279 }
280
281 /**
282 * Checks whether the first event in {@link #mTouchEvents} is representing a n event with type
283 * |eventType| and at specified position |x|, |y|, and consumes this event.
284 */
285 public void assertTouchEventInjected(TouchEventData.EventType eventType, flo at x, float y) {
286 assertContainsTouchEvents(new TouchEvent[] {
287 new TouchEventBuilder()
288 .withEventType(eventType)
289 .withX(x)
290 .withY(y)
291 .append()
292 .build(),
293 });
294 }
295
296 /**
297 * Checks whether the first event in {@link #mTouchEvents} is representing a n event with type
298 * |eventType|, and consumes this event.
299 */
300 public void assertTouchEventInjected(TouchEventData.EventType eventType) {
301 assertContainsTouchEvents(new TouchEvent[] {
302 new TouchEventBuilder().withEventType(eventType).build(),
303 });
304 }
305
306 /**
307 * Checks whether the first two events in {@link #mMouseEvents} are represen ting a down and an
308 * up action with |button| at specified position |x|, |y|, and consumes thes e events.
309 */
310 public void assertClickInjected(int button, int x, int y) {
311 assertContainsMouseEvents(new MouseEvent[] {
312 new MouseEvent(x, y, button, true), new MouseEvent(x, y, button, false),
313 });
314 }
315
316 public void assertTouchMoveEventInjected(
317 PointF[] initPositions, int stepX, int stepY, int moveCount) {
318 LinkedList<TouchEvent> events = new LinkedList<>();
319 assertTrue(initPositions != null && initPositions.length > 0);
320 for (int i = 0; i < initPositions.length; i++) {
321 assertNotNull(initPositions[i]);
322 events.add(new TouchEventBuilder()
323 .withEventType(TouchEventData.EventType.TOUCH_EVE NT_START)
324 .withX(initPositions[i].x)
325 .withY(initPositions[i].y)
326 .append()
327 .build());
328 }
329
330 // These tests send a single event for each finger that is moved. E.g. if we are injecting
331 // a pan event with two fingers, then every two TouchEvents represents o ne 'frame' of the
332 // motion sequence. Here we determine which iteration this event repres ents so we can use
333 // that to accurately compare the locations with the expected values.
334 for (int i = 0; i < moveCount; i++) {
335 for (int j = 0; j < initPositions.length; j++) {
336 TouchEventBuilder builder = new TouchEventBuilder();
337 builder.withEventType(TouchEventData.EventType.TOUCH_EVENT_MOVE) ;
338 for (int k = 0; k < initPositions.length; k++) {
339 // We inject one finger at a time which means that finger wi ll have the correct
340 // value but other fingers may still be stepSize behind it.
341 int currentStep = i - 1;
342 if (j >= k) {
343 currentStep++;
344 }
345 if (currentStep < 0) {
346 currentStep = 0;
347 }
348 builder.withX(initPositions[k].x + currentStep * stepX)
349 .withY(initPositions[k].y + currentStep * stepY)
350 .append();
351 }
352 events.add(builder.build());
353 }
354 }
355
356 assertContainsTouchEvents(events.toArray(new TouchEvent[0]));
357 }
358
359 /**
360 * Checks whether the first two events in {@link #mMouseEvents} are represen ting a down and an
361 * up action with right button at specified position |x|, |y|, and consumes these events.
362 */
363 public void assertRightClickInjected(int x, int y) {
364 assertClickInjected(BUTTON_RIGHT, x, y);
365 }
366
367 // ---------------- Implementations of InputInjector ----------------------
368 @Override
369 public void sendMouseEvent(int x, int y, int whichButton, boolean buttonDown ) {
370 mMouseEvents.add(new MouseEvent(x, y, whichButton, buttonDown));
371 }
372
373 @Override
374 public void sendMouseWheelEvent(int deltaX, int deltaY) {
375 mWheelEvents.add(new WheelEvent(deltaX, deltaY));
376 }
377
378 @Override
379 public boolean sendKeyEvent(int scanCode, int keyCode, boolean keyDown) {
380 mKeyEvents.add(new KeyEvent(scanCode, keyCode, keyDown));
381
382 // Note: This implementation is not consistent with jni.Client, which ma y return false when
383 // scanCode and keyCode cannot be mapped to a usb key code.
384 return true;
385 }
386
387 @Override
388 public void sendTextEvent(String text) {
389 mTextEvents.add(new TextEvent(text));
390 }
391
392 @SuppressFBWarnings("FE_FLOATING_POINT_EQUALITY")
393 @Override
394 public void sendTouchEvent(TouchEventData.EventType eventType, TouchEventDat a[] data) {
395 assertNotNull(data);
396 assertTrue(data.length != 0);
397 for (int i = 0; i < data.length; i++) {
398 assertTrue(data[i].getTouchPointId() != TouchEvent.INVALID_ID);
399 assertTrue(data[i].getTouchPointX() != TouchEvent.INVALID_POSITION);
400 assertTrue(data[i].getTouchPointY() != TouchEvent.INVALID_POSITION);
401 assertTrue(data[i].getTouchPointRadiusX() != TouchEvent.INVALID_POSI TION);
402 assertTrue(data[i].getTouchPointRadiusY() != TouchEvent.INVALID_POSI TION);
403 assertTrue(data[i].getTouchPointAngle() != TouchEvent.INVALID_DEGREE S);
404 assertTrue(data[i].getTouchPointPressure() != TouchEvent.INVALID_POS ITION);
405 }
406 mTouchEvents.add(new TouchEvent(eventType, data));
407 }
408 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698