1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.view.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.view.MotionEvent;
22 import android.view.MotionEvent.PointerCoords;
23 import android.view.MotionEvent.PointerProperties;
24 
25 public class MotionEventUtils {
26     private static final float DELTA = 0.01f;
27 
MotionEventUtils()28     private MotionEventUtils() {
29     }
30 
withCoords(float x, float y)31     public static PointerCoordsBuilder withCoords(float x, float y) {
32         final PointerCoordsBuilder builder = new PointerCoordsBuilder();
33         builder.x = x;
34         builder.y = y;
35         return builder;
36     }
37 
withProperties(int id, int toolType)38     public static PointerPropertiesBuilder withProperties(int id, int toolType) {
39         final PointerPropertiesBuilder builder = new PointerPropertiesBuilder();
40         builder.id = id;
41         builder.toolType = toolType;
42         return builder;
43     }
44 
45     public static class PointerPropertiesBuilder {
46         private int id;
47         private int toolType;
48 
build()49         public PointerProperties build() {
50             final PointerProperties pointerProperties =
51                     new PointerProperties();
52             pointerProperties.id = id;
53             pointerProperties.toolType = toolType;
54             return pointerProperties;
55         }
56 
verifyMatches(MotionEvent that, int pointerIndex)57         public void verifyMatches(MotionEvent that, int pointerIndex) {
58             assertEquals("Pointer ID should be the same",
59                     that.getPointerId(pointerIndex), this.id);
60             assertEquals("Tool type should be the same",
61                     that.getToolType(pointerIndex), this.toolType);
62         }
63 
verifyMatchesPointerProperties(PointerProperties that)64         public void verifyMatchesPointerProperties(PointerProperties that) {
65             assertEquals("Pointer ID should be the same", that.id, this.id);
66             assertEquals("Tool type should be the same", that.toolType, this.toolType);
67         }
68 
verifyMatchesPointerProperties(MotionEvent motionEvent, int pointerIndex)69         public void verifyMatchesPointerProperties(MotionEvent motionEvent, int pointerIndex) {
70             final PointerProperties that = new PointerProperties();
71             motionEvent.getPointerProperties(pointerIndex, that);
72 
73             verifyMatchesPointerProperties(that);
74         }
75     }
76 
77     public static class PointerCoordsBuilder {
78         private float x;
79         private float y;
80         private float pressure = 1.0f;
81         private float size = 1.0f;
82         private float touchMajor;
83         private float touchMinor;
84         private float toolMajor;
85         private float toolMinor;
86         private float orientation;
87         private float generic1;
88 
withPressure(float pressure)89         public PointerCoordsBuilder withPressure(float pressure) {
90             this.pressure = pressure;
91             return this;
92         }
93 
withSize(float size)94         public PointerCoordsBuilder withSize(float size) {
95             this.size = size;
96             return this;
97         }
98 
withTouch(float touchMajor, float touchMinor)99         public PointerCoordsBuilder withTouch(float touchMajor, float touchMinor) {
100             this.touchMajor = touchMajor;
101             this.touchMinor = touchMinor;
102             return this;
103         }
104 
withTool(float toolMajor, float toolMinor)105         public PointerCoordsBuilder withTool(float toolMajor, float toolMinor) {
106             this.toolMajor = toolMajor;
107             this.toolMinor = toolMinor;
108             return this;
109         }
110 
withOrientation(float orientation)111         public PointerCoordsBuilder withOrientation(float orientation) {
112             this.orientation = orientation;
113             return this;
114         }
115 
withGenericAxis1(float generic1)116         public PointerCoordsBuilder withGenericAxis1(float generic1) {
117             this.generic1 = generic1;
118             return this;
119         }
120 
build()121         public PointerCoords build() {
122             final PointerCoords pointerCoords = new PointerCoords();
123             pointerCoords.x = x;
124             pointerCoords.y = y;
125             pointerCoords.pressure = pressure;
126             pointerCoords.size = size;
127             pointerCoords.touchMajor = touchMajor;
128             pointerCoords.touchMinor = touchMinor;
129             pointerCoords.toolMajor = toolMajor;
130             pointerCoords.toolMinor = toolMinor;
131             pointerCoords.orientation = orientation;
132             pointerCoords.setAxisValue(MotionEvent.AXIS_GENERIC_1, generic1);
133             return pointerCoords;
134         }
135 
verifyMatches(MotionEvent that)136         public void verifyMatches(MotionEvent that) {
137             assertEquals("X coordinates should be the same", that.getX(), this.x, DELTA);
138             assertEquals("X coordinates should be the same",
139                     that.getAxisValue(MotionEvent.AXIS_X), this.x, DELTA);
140 
141             assertEquals("Y coordinates should be the same", that.getY(), this.y, DELTA);
142             assertEquals("Y coordinates should be the same",
143                     that.getAxisValue(MotionEvent.AXIS_Y), this.y, DELTA);
144 
145             assertEquals("Pressure should be the same", that.getPressure(), this.pressure, DELTA);
146             assertEquals("Pressure should be the same",
147                     that.getAxisValue(MotionEvent.AXIS_PRESSURE), this.pressure, DELTA);
148 
149             assertEquals("Size should be the same", that.getSize(), this.size, DELTA);
150             assertEquals("Size should be the same",
151                     that.getAxisValue(MotionEvent.AXIS_SIZE), this.size, DELTA);
152 
153             assertEquals("Touch major should be the same",
154                     that.getTouchMajor(), this.touchMajor,DELTA);
155             assertEquals("Touch major should be the same",
156                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR), this.touchMajor, DELTA);
157 
158             assertEquals("Touch minor should be the same",
159                     that.getTouchMinor(), this.touchMinor, DELTA);
160             assertEquals("Touch minor should be the same",
161                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR), this.touchMinor, DELTA);
162 
163             assertEquals("Tool major should be the same",
164                     that.getToolMajor(), this.toolMajor, DELTA);
165             assertEquals("Tool major should be the same",
166                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR), this.toolMajor, DELTA);
167 
168             assertEquals("Tool minor should be the same",
169                     that.getToolMinor(), this.toolMinor, DELTA);
170             assertEquals("Tool minor should be the same",
171                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR), this.toolMinor, DELTA);
172 
173             assertEquals("Orientation should be the same",
174                     that.getOrientation(), this.orientation, DELTA);
175             assertEquals("Orientation should be the same",
176                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION), this.orientation, DELTA);
177 
178             assertEquals("Generic axis 1 should be the same",
179                     that.getAxisValue(MotionEvent.AXIS_GENERIC_1), this.generic1, DELTA);
180         }
181 
verifyMatches(MotionEvent that, int pointerIndex)182         public void verifyMatches(MotionEvent that, int pointerIndex) {
183             assertEquals("X coordinates should be the same",
184                     that.getX(pointerIndex), this.x, DELTA);
185             assertEquals("X coordinates should be the same",
186                     that.getAxisValue(MotionEvent.AXIS_X, pointerIndex), this.x, DELTA);
187 
188             assertEquals("Y coordinates should be the same",
189                     that.getY(pointerIndex), this.y, DELTA);
190             assertEquals("Y coordinates should be the same",
191                     that.getAxisValue(MotionEvent.AXIS_Y, pointerIndex), this.y, DELTA);
192 
193             assertEquals("Pressure should be the same",
194                     that.getPressure(pointerIndex), this.pressure, DELTA);
195             assertEquals("Pressure should be the same",
196                     that.getAxisValue(MotionEvent.AXIS_PRESSURE, pointerIndex), this.pressure,
197                         DELTA);
198 
199             assertEquals("Size should be the same",
200                     that.getSize(pointerIndex), this.size, DELTA);
201             assertEquals("Size should be the same",
202                     that.getAxisValue(MotionEvent.AXIS_SIZE, pointerIndex), this.size, DELTA);
203 
204             assertEquals("Touch major should be the same",
205                     that.getTouchMajor(pointerIndex), this.touchMajor,DELTA);
206             assertEquals("Touch major should be the same",
207                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR, pointerIndex), this.touchMajor,
208                         DELTA);
209 
210             assertEquals("Touch minor should be the same",
211                     that.getTouchMinor(pointerIndex), this.touchMinor, DELTA);
212             assertEquals("Touch minor should be the same",
213                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR, pointerIndex), this.touchMinor,
214                         DELTA);
215 
216             assertEquals("Tool major should be the same",
217                     that.getToolMajor(pointerIndex), this.toolMajor, DELTA);
218             assertEquals("Tool major should be the same",
219                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR, pointerIndex), this.toolMajor,
220                         DELTA);
221 
222             assertEquals("Tool minor should be the same",
223                     that.getToolMinor(pointerIndex), this.toolMinor, DELTA);
224             assertEquals("Tool minor should be the same",
225                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR, pointerIndex), this.toolMinor,
226                         DELTA);
227 
228             assertEquals("Orientation should be the same",
229                     that.getOrientation(pointerIndex), this.orientation, DELTA);
230             assertEquals("Orientation should be the same",
231                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION, pointerIndex), this.orientation,
232                         DELTA);
233 
234             assertEquals("Generic axis 1 should be the same",
235                     that.getAxisValue(MotionEvent.AXIS_GENERIC_1, pointerIndex), this.generic1,
236                     DELTA);
237         }
238 
verifyMatchesHistorical(MotionEvent that, int position)239         public void verifyMatchesHistorical(MotionEvent that, int position) {
240             assertEquals("X coordinates should be the same",
241                     that.getHistoricalX(position), this.x, DELTA);
242             assertEquals("X coordinates should be the same",
243                     that.getHistoricalAxisValue(MotionEvent.AXIS_X, position), this.x, DELTA);
244 
245             assertEquals("Y coordinates should be the same",
246                     that.getHistoricalY(position), this.y, DELTA);
247             assertEquals("Y coordinates should be the same",
248                     that.getHistoricalAxisValue(MotionEvent.AXIS_Y, position), this.y, DELTA);
249 
250             assertEquals("Pressure should be the same",
251                     that.getHistoricalPressure(position), this.pressure, DELTA);
252             assertEquals("Pressure should be the same",
253                     that.getHistoricalAxisValue(MotionEvent.AXIS_PRESSURE, position), this.pressure,
254                     DELTA);
255 
256             assertEquals("Size should be the same",
257                     that.getHistoricalSize(position), this.size, DELTA);
258             assertEquals("Size should be the same",
259                     that.getHistoricalAxisValue(MotionEvent.AXIS_SIZE, position), this.size, DELTA);
260 
261             assertEquals("Touch major should be the same",
262                     that.getHistoricalTouchMajor(position), this.touchMajor,DELTA);
263             assertEquals("Touch major should be the same",
264                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MAJOR, position),
265                     this.touchMajor, DELTA);
266 
267             assertEquals("Touch minor should be the same",
268                     that.getHistoricalTouchMinor(position), this.touchMinor, DELTA);
269             assertEquals("Touch minor should be the same",
270                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MINOR, position),
271                     this.touchMinor, DELTA);
272 
273             assertEquals("Tool major should be the same",
274                     that.getHistoricalToolMajor(position), this.toolMajor, DELTA);
275             assertEquals("Tool major should be the same",
276                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MAJOR, position),
277                     this.toolMajor, DELTA);
278 
279             assertEquals("Tool minor should be the same",
280                     that.getHistoricalToolMinor(position), this.toolMinor, DELTA);
281             assertEquals("Tool minor should be the same",
282                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MINOR, position),
283                     this.toolMinor, DELTA);
284 
285             assertEquals("Orientation should be the same",
286                     that.getHistoricalOrientation(position), this.orientation, DELTA);
287             assertEquals("Orientation should be the same",
288                     that.getHistoricalAxisValue(MotionEvent.AXIS_ORIENTATION, position),
289                     this.orientation, DELTA);
290 
291             assertEquals("Generic axis 1 should be the same",
292                     that.getHistoricalAxisValue(MotionEvent.AXIS_GENERIC_1, position),
293                     this.generic1, DELTA);
294         }
295 
verifyMatchesHistorical(MotionEvent that, int pointerIndex, int position)296         public void verifyMatchesHistorical(MotionEvent that, int pointerIndex, int position) {
297             assertEquals("X coordinates should be the same",
298                     that.getHistoricalX(pointerIndex, position), this.x, DELTA);
299             assertEquals("X coordinates should be the same",
300                     that.getHistoricalAxisValue(MotionEvent.AXIS_X, pointerIndex, position),
301                     this.x, DELTA);
302 
303             assertEquals("Y coordinates should be the same",
304                     that.getHistoricalY(pointerIndex, position), this.y, DELTA);
305             assertEquals("Y coordinates should be the same",
306                     that.getHistoricalAxisValue(MotionEvent.AXIS_Y, pointerIndex, position),
307                     this.y, DELTA);
308 
309             assertEquals("Pressure should be the same",
310                     that.getHistoricalPressure(pointerIndex, position), this.pressure, DELTA);
311             assertEquals("Pressure should be the same",
312                     that.getHistoricalAxisValue(MotionEvent.AXIS_PRESSURE, pointerIndex, position),
313                     this.pressure, DELTA);
314 
315             assertEquals("Size should be the same",
316                     that.getHistoricalSize(pointerIndex, position), this.size, DELTA);
317             assertEquals("Size should be the same",
318                     that.getHistoricalAxisValue(MotionEvent.AXIS_SIZE, pointerIndex, position),
319                     this.size, DELTA);
320 
321             assertEquals("Touch major should be the same",
322                     that.getHistoricalTouchMajor(pointerIndex, position), this.touchMajor, DELTA);
323             assertEquals("Touch major should be the same",
324                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MAJOR,
325                             pointerIndex, position),
326                     this.touchMajor, DELTA);
327 
328             assertEquals("Touch minor should be the same",
329                     that.getHistoricalTouchMinor(pointerIndex, position), this.touchMinor, DELTA);
330             assertEquals("Touch minor should be the same",
331                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MINOR,
332                             pointerIndex, position),
333                     this.touchMinor, DELTA);
334 
335             assertEquals("Tool major should be the same",
336                     that.getHistoricalToolMajor(pointerIndex, position), this.toolMajor, DELTA);
337             assertEquals("Tool major should be the same",
338                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MAJOR,
339                             pointerIndex, position),
340                     this.toolMajor, DELTA);
341 
342             assertEquals("Tool minor should be the same",
343                     that.getHistoricalToolMinor(pointerIndex, position), this.toolMinor, DELTA);
344             assertEquals("Tool minor should be the same",
345                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MINOR,
346                             pointerIndex, position),
347                     this.toolMinor, DELTA);
348 
349             assertEquals("Orientation should be the same",
350                     that.getHistoricalOrientation(pointerIndex, position), this.orientation, DELTA);
351             assertEquals("Orientation should be the same",
352                     that.getHistoricalAxisValue(MotionEvent.AXIS_ORIENTATION,
353                             pointerIndex, position),
354                     this.orientation, DELTA);
355 
356             assertEquals("Generic axis 1 should be the same",
357                     that.getHistoricalAxisValue(MotionEvent.AXIS_GENERIC_1, pointerIndex, position),
358                     this.generic1, DELTA);
359         }
360 
verifyMatchesPointerCoords(PointerCoords that)361         public void verifyMatchesPointerCoords(PointerCoords that) {
362             assertEquals("X coordinates should be the same", that.x, this.x, DELTA);
363             assertEquals("X coordinates should be the same",
364                     that.getAxisValue(MotionEvent.AXIS_X), this.x, DELTA);
365 
366             assertEquals("Y coordinates should be the same", that.y, this.y, DELTA);
367             assertEquals("Y coordinates should be the same",
368                     that.getAxisValue(MotionEvent.AXIS_Y), this.y, DELTA);
369 
370             assertEquals("Pressure should be the same", that.pressure, this.pressure, DELTA);
371             assertEquals("Pressure should be the same",
372                     that.getAxisValue(MotionEvent.AXIS_PRESSURE), this.pressure, DELTA);
373 
374             assertEquals("Size should be the same", that.size, this.size, DELTA);
375             assertEquals("Size should be the same",
376                     that.getAxisValue(MotionEvent.AXIS_SIZE), this.size, DELTA);
377 
378             assertEquals("Touch major should be the same", that.touchMajor, this.touchMajor, DELTA);
379             assertEquals("Touch major should be the same",
380                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR), this.touchMajor, DELTA);
381 
382             assertEquals("Touch minor should be the same", that.touchMinor, this.touchMinor, DELTA);
383             assertEquals("Touch minor should be the same",
384                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR), this.touchMinor, DELTA);
385 
386             assertEquals("Tool major should be the same", that.toolMajor, this.toolMajor, DELTA);
387             assertEquals("Tool major should be the same",
388                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR), this.toolMajor, DELTA);
389 
390             assertEquals("Tool minor should be the same", that.toolMinor, this.toolMinor, DELTA);
391             assertEquals("Tool minor should be the same",
392                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR), this.toolMinor, DELTA);
393 
394             assertEquals("Orientation should be the same",
395                     that.orientation, this.orientation, DELTA);
396             assertEquals("Orientation should be the same",
397                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION), this.orientation, DELTA);
398 
399             assertEquals("Generic axis 1 should be the same",
400                     that.getAxisValue(MotionEvent.AXIS_GENERIC_1), this.generic1, DELTA);
401         }
402 
verifyMatchesPointerCoords(MotionEvent motionEvent, int pointerIndex)403         public void verifyMatchesPointerCoords(MotionEvent motionEvent, int pointerIndex) {
404             final PointerCoords that = new PointerCoords();
405             motionEvent.getPointerCoords(pointerIndex, that);
406 
407             verifyMatchesPointerCoords(that);
408         }
409 
verifyMatchesHistoricalPointerCoords(MotionEvent motionEvent, int pointerIndex, int pos)410         public void verifyMatchesHistoricalPointerCoords(MotionEvent motionEvent, int pointerIndex,
411                 int pos) {
412             final PointerCoords that = new PointerCoords();
413             motionEvent.getHistoricalPointerCoords(pointerIndex, pos, that);
414 
415             verifyMatchesPointerCoords(that);
416         }
417     }
418 }
419