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

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: Fix FindBugs errors 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 assertTrue(false);
Lambros 2016/06/17 23:00:10 fail() instead of assertTrue(false) ?
Hzj_jie 2016/06/19 23:41:39 Done.
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 assertTrue(false);
Lambros 2016/06/17 23:00:10 fail() ?
Hzj_jie 2016/06/19 23:41:39 Done.
111 }
112 assertTrue(mText.equals(other.mText));
Lambros 2016/06/17 23:00:10 assertEquals() ?
Hzj_jie 2016/06/19 23:41:40 Done.
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 comparasion of
121 * {@link TouchEventData#getTouchPointId()} when comparing.
122 */
123 public static final int INVALID_ID;
Lambros 2016/06/17 23:00:10 I'm not sure why you would need this? Why would yo
Hzj_jie 2016/06/19 23:41:39 In current TouchInputStrategyTest, there is no com
124 /**
Lambros 2016/06/17 23:00:10 Blank lines above comments.
Hzj_jie 2016/06/19 23:41:39 Done.
125 * A random number to represent an invalid position for {@link #assertCo ntentMatch}.
126 * Consumers can use this number to ignore certain float fields in an {@ link TouchEventData}
127 * when comparing.
128 */
129 public static final float INVALID_POSITION;
130 /**
131 * A number to represent an invalid angle in radians for {@link #assertC ontentMatch}.
132 * Consumers can use this number to ignore the comparasion of
133 * {@link TouchEventData#getTouchPointAngle} when comparing.
134 */
135 public static final float INVALID_RADIANS;
136 private final TouchEventData.EventType mEventType;
137 private final TouchEventData[] mData;
138
139 static {
140 INVALID_ID = -883;
Lambros 2016/06/17 23:00:10 Do we want a static block for this? I think we usu
Hzj_jie 2016/06/19 23:41:40 Done.
141 INVALID_POSITION = -763.273f;
142 INVALID_RADIANS = 3 * (float) Math.PI;
143 }
144
145 public TouchEvent(TouchEventData.EventType eventType,
146 TouchEventData[] data) {
147 mEventType = eventType;
148 mData = (data == null ? null : Arrays.copyOf(data, data.length));
149 }
150
151 private static boolean idMatch(int left, int right) {
152 return left == right
153 || left == INVALID_ID
Lambros 2016/06/17 23:00:10 Wrong indentation. git cl format
Hzj_jie 2016/06/19 23:41:40 Done.
154 || right == INVALID_ID;
155 }
156
157 private static boolean positionMatch(float left, float right) {
158 return left == right
159 || left == INVALID_POSITION
160 || right == INVALID_POSITION;
161 }
162
163 private static boolean degreeMatch(float left, float right) {
164 // 3PI = 540
Lambros 2016/06/17 23:00:10 Can you clarify this comment? Or just remove it?
Hzj_jie 2016/06/19 23:41:39 Done.
165 return left == right
Lambros 2016/06/17 23:00:10 This exact compare of floats will probably trigger
Hzj_jie 2016/06/19 23:41:39 Indeed the floats should be exact equals, there sh
166 || left > 360
Lambros 2016/06/17 23:00:10 This is very weird. Firstly: Why not define INVALI
Hzj_jie 2016/06/19 23:41:39 Good point.
167 || right > 360;
168 }
169
170 private static void assertContentMatch(TouchEventData left, TouchEventDa ta right) {
171 assertTrue(idMatch(left.getTouchPointId(), right.getTouchPointId())) ;
172 assertTrue(positionMatch(left.getTouchPointX(), right.getTouchPointX ()));
173 assertTrue(positionMatch(left.getTouchPointY(), right.getTouchPointY ()));
174 assertTrue(positionMatch(left.getTouchPointRadiusX(), right.getTouch PointRadiusX()));
175 assertTrue(positionMatch(left.getTouchPointRadiusY(), right.getTouch PointRadiusY()));
176 assertTrue(degreeMatch(left.getTouchPointAngle(), right.getTouchPoin tAngle()));
177 assertTrue(positionMatch(left.getTouchPointPressure(), right.getTouc hPointPressure()));
178 }
179
180 private static void assertDataEquals(TouchEventData left, TouchEventData right) {
181 if (left == null && right == null) {
182 return;
183 }
184 if (left == null || right == null) {
185 assertTrue(false);
186 }
187 assertContentMatch(left, right);
188 }
189
190 private int dataSize() {
191 return mData == null ? 0 : mData.length;
192 }
193
194 @Override
195 protected void assertContentMatch(TouchEvent other) {
196 assertEquals(mEventType, other.mEventType);
197 if (dataSize() != 0 && other.dataSize() != 0) {
Lambros 2016/06/17 23:00:10 This is incorrect: && should probably be || If exa
Hzj_jie 2016/06/19 23:41:39 I may explain more about this in the comments. Thi
198 assertEquals(dataSize(), other.dataSize());
199 for (int i = 0; i < dataSize(); i++) {
200 assertDataEquals(mData[i], other.mData[i]);
201 }
202 }
203 }
204
205 }
206
207 private final LinkedList<MouseEvent> mMouseEvents;
208 private final LinkedList<WheelEvent> mWheelEvents;
209 private final LinkedList<KeyEvent> mKeyEvents;
210 private final LinkedList<TextEvent> mTextEvents;
211 private final LinkedList<TouchEvent> mTouchEvents;
212
213 public MockInputInjector() {
214 mMouseEvents = new LinkedList<>();
215 mWheelEvents = new LinkedList<>();
216 mKeyEvents = new LinkedList<>();
217 mTextEvents = new LinkedList<>();
218 mTouchEvents = new LinkedList<>();
219 }
220
221 /**
222 * Compares the first |right|.length events with the ones in |left|, asserts they are equal, and
223 * consumes these events from both |left| and |right|.
224 */
225 private static <E extends Event<E>> void assertContains(LinkedList<E> left,
226 LinkedList<E> right) {
227 if (left == null && right == null) {
228 return;
229 }
230 if (left == null || right == null) {
231 assertTrue(false);
232 return;
233 }
234 assertTrue(left.size() >= right.size());
235 while (right.size() > 0) {
236 E leftEvent = left.removeFirst();
237 E rightEvent = right.removeFirst();
238 if (leftEvent == null) {
239 assertNull(rightEvent);
240 } else {
241 leftEvent.assertEventEquals(rightEvent);
242 }
243 }
244 }
245
246 public void clear() {
247 mMouseEvents.clear();
248 mWheelEvents.clear();
249 mKeyEvents.clear();
250 mTextEvents.clear();
251 mTouchEvents.clear();
252 }
253
254 /**
255 * Compares the first |other|.length events with {@link #mMouseEvents} in th is instance,
256 * asserts they are equal, and consumes these events in both this instance a nd |other|.
257 */
258 public void assertContainsMouseEvents(LinkedList<MouseEvent> other) {
Lambros 2016/06/17 23:00:10 Does this need to consume |other|? Does this need
Hzj_jie 2016/06/19 23:41:40 Done.
259 assertContains(mMouseEvents, other);
260 }
261
262 /**
263 * Compares the first |other|.length events with {@link #mTouchEvents} in th is instance,
264 * asserts they are equal, and consumes these events in both this instance a nd |other|.
265 */
266 public void assertContainsTouchEvents(LinkedList<TouchEvent> other) {
267 assertContains(mTouchEvents, other);
268 }
269
270 /** Asserts current instance is emtpy. */
Lambros 2016/06/17 23:00:10 empty
Hzj_jie 2016/06/19 23:41:39 Done.
271 public void assertEmpty() {
272 assertTrue(mMouseEvents.isEmpty());
273 assertTrue(mWheelEvents.isEmpty());
274 assertTrue(mKeyEvents.isEmpty());
275 assertTrue(mTextEvents.isEmpty());
276 assertTrue(mTouchEvents.isEmpty());
277 }
278
279 /**
280 * Checks whether the first two events in {@link #mTouchEvents} are represen ting a down and
281 * an up action at specified position |x|, |y|, and consumes these events.
282 */
283 public void assertTapInjected(float x, float y) {
284 assertTouchEventInjected(TouchEventData.EventType.TOUCH_EVENT_START, x, y);
285 assertTouchEventInjected(TouchEventData.EventType.TOUCH_EVENT_END, x, y) ;
286 }
287
288 /**
289 * Checks whether the first event in {@link #mTouchEvents} is representing a n event with type
290 * |eventType| and at specified position |x|, |y|, and consumes this event.
291 */
292 public void assertTouchEventInjected(TouchEventData.EventType eventType, flo at x, float y) {
293 assertContainsTouchEvents(new LinkedList<TouchEvent>(Arrays.asList(new T ouchEvent[] {
294 (new TouchEventBuilder())
295 .withEventType(eventType)
296 .withX(x)
297 .withY(y)
298 .append()
299 .build(),
300 })));
301 }
302
303 /**
304 * Checks whether the first event in {@link #mTouchEvents} is representing a n event with type
305 * |eventType|, and consumes this event.
306 */
307 public void assertTouchEventInjected(TouchEventData.EventType eventType) {
308 assertContainsTouchEvents(new LinkedList<TouchEvent>(Arrays.asList(new T ouchEvent[] {
309 (new TouchEventBuilder()).withEventType(eventType).build(),
310 })));
311 }
312
313 /**
314 * Checks whether the first two events in {@link #mMouseEvents} are represen ting a down and an
315 * up action with |button| at specified position |x|, |y|, and consumes thes e events.
316 */
317 public void assertClickInjected(int button, int x, int y) {
318 assertContainsMouseEvents(new LinkedList<MouseEvent>(Arrays.asList(new M ouseEvent[] {
319 new MouseEvent(x, y, button, true),
320 new MouseEvent(x, y, button, false),
321 })));
322 }
323
324 public void assertTouchMoveEventInjected(PointF[] initPositions,
325 int stepX,
326 int stepY,
327 int moveCount) {
328 LinkedList<TouchEvent> events = new LinkedList<>();
329 assertTrue(initPositions != null && initPositions.length > 0);
330 for (int i = 0; i < initPositions.length; i++) {
331 assertNotNull(initPositions[i]);
332 events.add((new TouchEventBuilder())
Lambros 2016/06/17 23:00:10 I don't think you need parentheses around new Touc
Hzj_jie 2016/06/19 23:41:39 Done.
333 .withEventType(TouchEventData.EventType.TOUCH_EVENT_START)
334 .withX(initPositions[i].x)
335 .withY(initPositions[i].y)
336 .append()
337 .build());
338 }
339 // These tests send a single event for each finger that is moved. E.G. If we are injecting
Lambros 2016/06/17 23:00:10 Blank line.
Hzj_jie 2016/06/19 23:41:39 Done.
340 // a pan event with two fingers, then every two TouchEvents represents o ne 'frame' of the
341 // motion sequence. Here we determine which iteration this event repres ents so we can use
342 // that to accurately compare the locations with the expected values.
343 for (int i = 0; i < moveCount; i++) {
344 for (int j = 0; j < initPositions.length; j++) {
345 TouchEventBuilder builder = new TouchEventBuilder();
346 builder.withEventType(TouchEventData.EventType.TOUCH_EVENT_MOVE) ;
347 for (int k = 0; k < initPositions.length; k++) {
348 // We inject one finger at a time which means that finger wi ll have the correct
349 // value but other fingers may still be stepSize behind it.
350 int currentStep = i - 1;
351 if (j >= k) {
352 currentStep++;
353 }
354 if (currentStep < 0) {
355 currentStep = 0;
356 }
357 builder.withX(initPositions[k].x + currentStep * stepX)
358 .withY(initPositions[k].y + currentStep * stepY)
359 .append();
360 }
361 events.add(builder.build());
362 }
363 }
364
365 assertContainsTouchEvents(events);
366 }
367
368 /**
369 * Checks whether the first two events in {@link #mMouseEvents} are represen ting a down and an
370 * up action with right button at specified position |x|, |y|, and consumes these events.
371 */
372 public void assertRightClickInjected(int x, int y) {
373 assertClickInjected(TouchInputHandler.BUTTON_RIGHT, x, y);
374 }
375
376 // ---------------- Implementations of InputInjector ----------------------
377 @Override
378 public void sendMouseEvent(int x, int y, int whichButton, boolean buttonDown ) {
379 mMouseEvents.add(new MouseEvent(x, y, whichButton, buttonDown));
380 }
381
382 @Override
383 public void sendMouseWheelEvent(int deltaX, int deltaY) {
384 mWheelEvents.add(new WheelEvent(deltaX, deltaY));
385 }
386
387 @Override
388 public boolean sendKeyEvent(int scanCode, int keyCode, boolean keyDown) {
389 mKeyEvents.add(new KeyEvent(scanCode, keyCode, keyDown));
390 // Note: This implementation is not consistent with jni.Client, which ma y return false when
Lambros 2016/06/17 23:00:10 Blank line.
Hzj_jie 2016/06/19 23:41:39 Done.
391 // scanCode and keyCode cannot be mapped to a usb key code.
392 return true;
393 }
394
395 @Override
396 public void sendTextEvent(String text) {
397 mTextEvents.add(new TextEvent(text));
398 }
399
400 @Override
401 public void sendTouchEvent(TouchEventData.EventType eventType, TouchEventDat a[] data) {
402 mTouchEvents.add(new TouchEvent(eventType, data));
403 }
404 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698