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

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

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

Powered by Google App Engine
This is Rietveld 408576698