1 /*
2  * Copyright 2023 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.widget;
18 
19 import static android.view.InputDevice.SOURCE_CLASS_POINTER;
20 import static android.view.InputDevice.SOURCE_ROTARY_ENCODER;
21 import static android.view.MotionEvent.ACTION_SCROLL;
22 import static android.view.MotionEvent.AXIS_HSCROLL;
23 import static android.view.MotionEvent.AXIS_SCROLL;
24 import static android.view.MotionEvent.AXIS_VSCROLL;
25 
26 import android.view.MotionEvent;
27 
28 /** Test utilities for {@link MotionEvent}s. */
29 public class MotionEventUtils {
30 
31     /** Creates a test {@link MotionEvent} from a {@link SOURCE_ROTARY_ENCODER}. */
createRotaryEvent(float scroll)32     public static MotionEvent createRotaryEvent(float scroll) {
33         MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
34         coords.setAxisValue(AXIS_SCROLL, scroll);
35 
36         return createGenericMotionEvent(SOURCE_ROTARY_ENCODER, ACTION_SCROLL, coords);
37     }
38 
39     /** Creates a test {@link MotionEvent} from a {@link SOURCE_CLASS_POINTER}. */
createGenericPointerEvent(float hScroll, float vScroll)40     public static MotionEvent createGenericPointerEvent(float hScroll, float vScroll) {
41         MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
42         coords.setAxisValue(AXIS_HSCROLL, hScroll);
43         coords.setAxisValue(AXIS_VSCROLL, vScroll);
44 
45         return createGenericMotionEvent(SOURCE_CLASS_POINTER, ACTION_SCROLL, coords);
46     }
47 
createGenericMotionEvent( int source, int action, MotionEvent.PointerCoords coords)48     private static MotionEvent createGenericMotionEvent(
49             int source, int action, MotionEvent.PointerCoords coords) {
50         MotionEvent.PointerProperties props = new MotionEvent.PointerProperties();
51         props.id = 0;
52 
53         return MotionEvent.obtain(
54                 /* downTime= */ 0,
55                 /* eventTime= */ 100,
56                 action,
57                 /* pointerCount= */ 1,
58                 new MotionEvent.PointerProperties[] {props},
59                 new MotionEvent.PointerCoords[] {coords},
60                 /* metaState= */ 0,
61                 /* buttonState= */ 0,
62                 /* xPrecision= */ 0,
63                 /* yPrecision= */ 0,
64                 /* deviceId= */ 1,
65                 /* edgeFlags= */ 0,
66                 source,
67                 /* flags= */ 0);
68     }
69 }
70