Home | History | Annotate | Download | only in cts
      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 
     28     private MotionEventUtils() {
     29     }
     30 
     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 
     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 
     49         public PointerProperties build() {
     50             final PointerProperties pointerProperties =
     51                     new PointerProperties();
     52             pointerProperties.id = id;
     53             pointerProperties.toolType = toolType;
     54             return pointerProperties;
     55         }
     56 
     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 
     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 
     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 
     88         public PointerCoordsBuilder withPressure(float pressure) {
     89             this.pressure = pressure;
     90             return this;
     91         }
     92 
     93         public PointerCoordsBuilder withSize(float size) {
     94             this.size = size;
     95             return this;
     96         }
     97 
     98         public PointerCoordsBuilder withTouch(float touchMajor, float touchMinor) {
     99             this.touchMajor = touchMajor;
    100             this.touchMinor = touchMinor;
    101             return this;
    102         }
    103 
    104         public PointerCoordsBuilder withTool(float toolMajor, float toolMinor) {
    105             this.toolMajor = toolMajor;
    106             this.toolMinor = toolMinor;
    107             return this;
    108         }
    109 
    110         public PointerCoordsBuilder withOrientation(float orientation) {
    111             this.orientation = orientation;
    112             return this;
    113         }
    114 
    115         public PointerCoords build() {
    116             final PointerCoords pointerCoords = new PointerCoords();
    117             pointerCoords.x = x;
    118             pointerCoords.y = y;
    119             pointerCoords.pressure = pressure;
    120             pointerCoords.size = size;
    121             pointerCoords.touchMajor = touchMajor;
    122             pointerCoords.touchMinor = touchMinor;
    123             pointerCoords.toolMajor = toolMajor;
    124             pointerCoords.toolMinor = toolMinor;
    125             pointerCoords.orientation = orientation;
    126             return pointerCoords;
    127         }
    128 
    129         public void verifyMatches(MotionEvent that) {
    130             assertEquals("X coordinates should be the same", that.getX(), this.x, DELTA);
    131             assertEquals("X coordinates should be the same",
    132                     that.getAxisValue(MotionEvent.AXIS_X), this.x, DELTA);
    133 
    134             assertEquals("Y coordinates should be the same", that.getY(), this.y, DELTA);
    135             assertEquals("Y coordinates should be the same",
    136                     that.getAxisValue(MotionEvent.AXIS_Y), this.y, DELTA);
    137 
    138             assertEquals("Pressure should be the same", that.getPressure(), this.pressure, DELTA);
    139             assertEquals("Pressure should be the same",
    140                     that.getAxisValue(MotionEvent.AXIS_PRESSURE), this.pressure, DELTA);
    141 
    142             assertEquals("Size should be the same", that.getSize(), this.size, DELTA);
    143             assertEquals("Size should be the same",
    144                     that.getAxisValue(MotionEvent.AXIS_SIZE), this.size, DELTA);
    145 
    146             assertEquals("Touch major should be the same",
    147                     that.getTouchMajor(), this.touchMajor,DELTA);
    148             assertEquals("Touch major should be the same",
    149                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR), this.touchMajor, DELTA);
    150 
    151             assertEquals("Touch minor should be the same",
    152                     that.getTouchMinor(), this.touchMinor, DELTA);
    153             assertEquals("Touch minor should be the same",
    154                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR), this.touchMinor, DELTA);
    155 
    156             assertEquals("Tool major should be the same",
    157                     that.getToolMajor(), this.toolMajor, DELTA);
    158             assertEquals("Tool major should be the same",
    159                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR), this.toolMajor, DELTA);
    160 
    161             assertEquals("Tool minor should be the same",
    162                     that.getToolMinor(), this.toolMinor, DELTA);
    163             assertEquals("Tool minor should be the same",
    164                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR), this.toolMinor, DELTA);
    165 
    166             assertEquals("Orientation should be the same",
    167                     that.getOrientation(), this.orientation, DELTA);
    168             assertEquals("Orientation should be the same",
    169                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION), this.orientation, DELTA);
    170         }
    171 
    172         public void verifyMatches(MotionEvent that, int pointerIndex) {
    173             assertEquals("X coordinates should be the same",
    174                     that.getX(pointerIndex), this.x, DELTA);
    175             assertEquals("X coordinates should be the same",
    176                     that.getAxisValue(MotionEvent.AXIS_X, pointerIndex), this.x, DELTA);
    177 
    178             assertEquals("Y coordinates should be the same",
    179                     that.getY(pointerIndex), this.y, DELTA);
    180             assertEquals("Y coordinates should be the same",
    181                     that.getAxisValue(MotionEvent.AXIS_Y, pointerIndex), this.y, DELTA);
    182 
    183             assertEquals("Pressure should be the same",
    184                     that.getPressure(pointerIndex), this.pressure, DELTA);
    185             assertEquals("Pressure should be the same",
    186                     that.getAxisValue(MotionEvent.AXIS_PRESSURE, pointerIndex), this.pressure,
    187                         DELTA);
    188 
    189             assertEquals("Size should be the same",
    190                     that.getSize(pointerIndex), this.size, DELTA);
    191             assertEquals("Size should be the same",
    192                     that.getAxisValue(MotionEvent.AXIS_SIZE, pointerIndex), this.size, DELTA);
    193 
    194             assertEquals("Touch major should be the same",
    195                     that.getTouchMajor(pointerIndex), this.touchMajor,DELTA);
    196             assertEquals("Touch major should be the same",
    197                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR, pointerIndex), this.touchMajor,
    198                         DELTA);
    199 
    200             assertEquals("Touch minor should be the same",
    201                     that.getTouchMinor(pointerIndex), this.touchMinor, DELTA);
    202             assertEquals("Touch minor should be the same",
    203                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR, pointerIndex), this.touchMinor,
    204                         DELTA);
    205 
    206             assertEquals("Tool major should be the same",
    207                     that.getToolMajor(pointerIndex), this.toolMajor, DELTA);
    208             assertEquals("Tool major should be the same",
    209                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR, pointerIndex), this.toolMajor,
    210                         DELTA);
    211 
    212             assertEquals("Tool minor should be the same",
    213                     that.getToolMinor(pointerIndex), this.toolMinor, DELTA);
    214             assertEquals("Tool minor should be the same",
    215                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR, pointerIndex), this.toolMinor,
    216                         DELTA);
    217 
    218             assertEquals("Orientation should be the same",
    219                     that.getOrientation(pointerIndex), this.orientation, DELTA);
    220             assertEquals("Orientation should be the same",
    221                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION, pointerIndex), this.orientation,
    222                         DELTA);
    223         }
    224 
    225         public void verifyMatchesHistorical(MotionEvent that, int position) {
    226             assertEquals("X coordinates should be the same",
    227                     that.getHistoricalX(position), this.x, DELTA);
    228             assertEquals("X coordinates should be the same",
    229                     that.getHistoricalAxisValue(MotionEvent.AXIS_X, position), this.x, DELTA);
    230 
    231             assertEquals("Y coordinates should be the same",
    232                     that.getHistoricalY(position), this.y, DELTA);
    233             assertEquals("Y coordinates should be the same",
    234                     that.getHistoricalAxisValue(MotionEvent.AXIS_Y, position), this.y, DELTA);
    235 
    236             assertEquals("Pressure should be the same",
    237                     that.getHistoricalPressure(position), this.pressure, DELTA);
    238             assertEquals("Pressure should be the same",
    239                     that.getHistoricalAxisValue(MotionEvent.AXIS_PRESSURE, position), this.pressure,
    240                     DELTA);
    241 
    242             assertEquals("Size should be the same",
    243                     that.getHistoricalSize(position), this.size, DELTA);
    244             assertEquals("Size should be the same",
    245                     that.getHistoricalAxisValue(MotionEvent.AXIS_SIZE, position), this.size, DELTA);
    246 
    247             assertEquals("Touch major should be the same",
    248                     that.getHistoricalTouchMajor(position), this.touchMajor,DELTA);
    249             assertEquals("Touch major should be the same",
    250                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MAJOR, position),
    251                     this.touchMajor, DELTA);
    252 
    253             assertEquals("Touch minor should be the same",
    254                     that.getHistoricalTouchMinor(position), this.touchMinor, DELTA);
    255             assertEquals("Touch minor should be the same",
    256                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MINOR, position),
    257                     this.touchMinor, DELTA);
    258 
    259             assertEquals("Tool major should be the same",
    260                     that.getHistoricalToolMajor(position), this.toolMajor, DELTA);
    261             assertEquals("Tool major should be the same",
    262                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MAJOR, position),
    263                     this.toolMajor, DELTA);
    264 
    265             assertEquals("Tool minor should be the same",
    266                     that.getHistoricalToolMinor(position), this.toolMinor, DELTA);
    267             assertEquals("Tool minor should be the same",
    268                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MINOR, position),
    269                     this.toolMinor, DELTA);
    270 
    271             assertEquals("Orientation should be the same",
    272                     that.getHistoricalOrientation(position), this.orientation, DELTA);
    273             assertEquals("Orientation should be the same",
    274                     that.getHistoricalAxisValue(MotionEvent.AXIS_ORIENTATION, position),
    275                     this.orientation, DELTA);
    276         }
    277 
    278         public void verifyMatchesHistorical(MotionEvent that, int pointerIndex, int position) {
    279             assertEquals("X coordinates should be the same",
    280                     that.getHistoricalX(pointerIndex, position), this.x, DELTA);
    281             assertEquals("X coordinates should be the same",
    282                     that.getHistoricalAxisValue(MotionEvent.AXIS_X, pointerIndex, position),
    283                     this.x, DELTA);
    284 
    285             assertEquals("Y coordinates should be the same",
    286                     that.getHistoricalY(pointerIndex, position), this.y, DELTA);
    287             assertEquals("Y coordinates should be the same",
    288                     that.getHistoricalAxisValue(MotionEvent.AXIS_Y, pointerIndex, position),
    289                     this.y, DELTA);
    290 
    291             assertEquals("Pressure should be the same",
    292                     that.getHistoricalPressure(pointerIndex, position), this.pressure, DELTA);
    293             assertEquals("Pressure should be the same",
    294                     that.getHistoricalAxisValue(MotionEvent.AXIS_PRESSURE, pointerIndex, position),
    295                     this.pressure, DELTA);
    296 
    297             assertEquals("Size should be the same",
    298                     that.getHistoricalSize(pointerIndex, position), this.size, DELTA);
    299             assertEquals("Size should be the same",
    300                     that.getHistoricalAxisValue(MotionEvent.AXIS_SIZE, pointerIndex, position),
    301                     this.size, DELTA);
    302 
    303             assertEquals("Touch major should be the same",
    304                     that.getHistoricalTouchMajor(pointerIndex, position), this.touchMajor, DELTA);
    305             assertEquals("Touch major should be the same",
    306                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MAJOR,
    307                             pointerIndex, position),
    308                     this.touchMajor, DELTA);
    309 
    310             assertEquals("Touch minor should be the same",
    311                     that.getHistoricalTouchMinor(pointerIndex, position), this.touchMinor, DELTA);
    312             assertEquals("Touch minor should be the same",
    313                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOUCH_MINOR,
    314                             pointerIndex, position),
    315                     this.touchMinor, DELTA);
    316 
    317             assertEquals("Tool major should be the same",
    318                     that.getHistoricalToolMajor(pointerIndex, position), this.toolMajor, DELTA);
    319             assertEquals("Tool major should be the same",
    320                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MAJOR,
    321                             pointerIndex, position),
    322                     this.toolMajor, DELTA);
    323 
    324             assertEquals("Tool minor should be the same",
    325                     that.getHistoricalToolMinor(pointerIndex, position), this.toolMinor, DELTA);
    326             assertEquals("Tool minor should be the same",
    327                     that.getHistoricalAxisValue(MotionEvent.AXIS_TOOL_MINOR,
    328                             pointerIndex, position),
    329                     this.toolMinor, DELTA);
    330 
    331             assertEquals("Orientation should be the same",
    332                     that.getHistoricalOrientation(pointerIndex, position), this.orientation, DELTA);
    333             assertEquals("Orientation should be the same",
    334                     that.getHistoricalAxisValue(MotionEvent.AXIS_ORIENTATION,
    335                             pointerIndex, position),
    336                     this.orientation, DELTA);
    337         }
    338 
    339         public void verifyMatchesPointerCoords(PointerCoords that) {
    340             assertEquals("X coordinates should be the same", that.x, this.x, DELTA);
    341             assertEquals("X coordinates should be the same",
    342                     that.getAxisValue(MotionEvent.AXIS_X), this.x, DELTA);
    343 
    344             assertEquals("Y coordinates should be the same", that.y, this.y, DELTA);
    345             assertEquals("Y coordinates should be the same",
    346                     that.getAxisValue(MotionEvent.AXIS_Y), this.y, DELTA);
    347 
    348             assertEquals("Pressure should be the same", that.pressure, this.pressure, DELTA);
    349             assertEquals("Pressure should be the same",
    350                     that.getAxisValue(MotionEvent.AXIS_PRESSURE), this.pressure, DELTA);
    351 
    352             assertEquals("Size should be the same", that.size, this.size, DELTA);
    353             assertEquals("Size should be the same",
    354                     that.getAxisValue(MotionEvent.AXIS_SIZE), this.size, DELTA);
    355 
    356             assertEquals("Touch major should be the same", that.touchMajor, this.touchMajor, DELTA);
    357             assertEquals("Touch major should be the same",
    358                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MAJOR), this.touchMajor, DELTA);
    359 
    360             assertEquals("Touch minor should be the same", that.touchMinor, this.touchMinor, DELTA);
    361             assertEquals("Touch minor should be the same",
    362                     that.getAxisValue(MotionEvent.AXIS_TOUCH_MINOR), this.touchMinor, DELTA);
    363 
    364             assertEquals("Tool major should be the same", that.toolMajor, this.toolMajor, DELTA);
    365             assertEquals("Tool major should be the same",
    366                     that.getAxisValue(MotionEvent.AXIS_TOOL_MAJOR), this.toolMajor, DELTA);
    367 
    368             assertEquals("Tool minor should be the same", that.toolMinor, this.toolMinor, DELTA);
    369             assertEquals("Tool minor should be the same",
    370                     that.getAxisValue(MotionEvent.AXIS_TOOL_MINOR), this.toolMinor, DELTA);
    371 
    372             assertEquals("Orientation should be the same",
    373                     that.orientation, this.orientation, DELTA);
    374             assertEquals("Orientation should be the same",
    375                     that.getAxisValue(MotionEvent.AXIS_ORIENTATION), this.orientation, DELTA);
    376         }
    377 
    378         public void verifyMatchesPointerCoords(MotionEvent motionEvent, int pointerIndex) {
    379             final PointerCoords that = new PointerCoords();
    380             motionEvent.getPointerCoords(pointerIndex, that);
    381 
    382             verifyMatchesPointerCoords(that);
    383         }
    384 
    385         public void verifyMatchesHistoricalPointerCoords(MotionEvent motionEvent, int pointerIndex,
    386                 int pos) {
    387             final PointerCoords that = new PointerCoords();
    388             motionEvent.getHistoricalPointerCoords(pointerIndex, pos, that);
    389 
    390             verifyMatchesPointerCoords(that);
    391         }
    392     }
    393 }
    394