summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/launcher3/testcomponent/TouchEventGenerator.java
blob: 80d6341e70a254197998cfee78ca15995681b784 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.testcomponent;

import android.graphics.Point;
import android.util.Pair;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
import android.view.MotionEvent.PointerProperties;

import java.util.HashMap;
import java.util.Map;

/**
 * Utility class to generate MotionEvent event sequences for testing touch gesture detectors.
 */
public class TouchEventGenerator {

    /**
     * Amount of time between two generated events.
     */
    private static final long TIME_INCREMENT_MS = 20L;

    /**
     * Id of the fake device generating the events.
     */
    private static final int DEVICE_ID = 2104;

    /**
     * The fingers currently present on the emulated touch screen.
     */
    private Map<Integer, Point> mFingers;

    /**
     * Initial event time for the current sequence.
     */
    private long mInitialTime;

    /**
     * Time of the last generated event.
     */
    private long mLastEventTime;

    /**
     * Time of the next event.
     */
    private long mTime;

    /**
     * Receives the generated events.
     */
    public interface Listener {

        /**
         * Called when an event was generated.
         */
        void onTouchEvent(MotionEvent event);
    }
    private final Listener mListener;

    public TouchEventGenerator(Listener listener) {
        mListener = listener;
        mFingers = new HashMap<Integer, Point>();
    }

    /**
     * Adds a finger on the touchscreen.
     */
    public TouchEventGenerator put(int id, int x, int y, long ms) {
        checkFingerExistence(id, false);
        boolean isInitialDown = mFingers.isEmpty();
        mFingers.put(id, new Point(x, y));
        int action;
        if (isInitialDown) {
            action = MotionEvent.ACTION_DOWN;
        } else {
            action = MotionEvent.ACTION_POINTER_DOWN;
            // Set the id of the changed pointer.
            action |= id << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        }
        generateEvent(action, ms);
        return this;
    }

    /**
     * Adds a finger on the touchscreen after advancing default time interval.
     */
    public TouchEventGenerator put(int id, int x, int y) {
        return put(id, x, y, TIME_INCREMENT_MS);
    }

    /**
     * Adjusts the position of a finger for an upcoming move event.
     *
     * @see #move(long ms)
     */
    public TouchEventGenerator position(int id, int x, int y) {
        checkFingerExistence(id, true);
        mFingers.get(id).set(x, y);
        return this;
    }

    /**
     * Commits the finger position changes of {@link #position(int, int, int)} by generating a move
     * event.
     *
     * @see #position(int, int, int)
     */
    public TouchEventGenerator move(long ms) {
        generateEvent(MotionEvent.ACTION_MOVE, ms);
        return this;
    }

    /**
     * Commits the finger position changes of {@link #position(int, int, int)} by generating a move
     * event after advancing the default time interval.
     *
     * @see #position(int, int, int)
     */
    public TouchEventGenerator move() {
        return move(TIME_INCREMENT_MS);
    }

    /**
     * Moves a single finger on the touchscreen.
     */
    public TouchEventGenerator move(int id, int x, int y, long ms) {
        return position(id, x, y).move(ms);
    }

    /**
     * Moves a single finger on the touchscreen after advancing default time interval.
     */
    public TouchEventGenerator move(int id, int x, int y) {
        return move(id, x, y, TIME_INCREMENT_MS);
    }

    /**
     * Removes an existing finger from the touchscreen.
     */
    public TouchEventGenerator lift(int id, long ms) {
        checkFingerExistence(id, true);
        boolean isFinalUp = mFingers.size() == 1;
        int action;
        if (isFinalUp) {
            action = MotionEvent.ACTION_UP;
        } else {
            action = MotionEvent.ACTION_POINTER_UP;
            // Set the id of the changed pointer.
            action |= id << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        }
        generateEvent(action, ms);
        mFingers.remove(id);
        return this;
    }

    /**
     * Removes a finger from the touchscreen.
     */
    public TouchEventGenerator lift(int id, int x, int y, long ms) {
        checkFingerExistence(id, true);
        mFingers.get(id).set(x, y);
        return lift(id, ms);
    }

    /**
     * Removes an existing finger from the touchscreen after advancing default time interval.
     */
    public TouchEventGenerator lift(int id) {
        return lift(id, TIME_INCREMENT_MS);
    }

    /**
     * Cancels an ongoing sequence.
     */
    public TouchEventGenerator cancel(long ms) {
        generateEvent(MotionEvent.ACTION_CANCEL, ms);
        mFingers.clear();
        return this;
    }

    /**
     * Cancels an ongoing sequence.
     */
    public TouchEventGenerator cancel() {
        return cancel(TIME_INCREMENT_MS);
    }

    private void checkFingerExistence(int id, boolean shouldExist) {
        if (shouldExist != mFingers.containsKey(id)) {
            throw new IllegalArgumentException(
                    shouldExist ? "Finger does not exist" : "Finger already exists");
        }
    }

    private void generateEvent(int action, long ms) {
        mTime = mLastEventTime + ms;
        Pair<PointerProperties[], PointerCoords[]> state = getFingerState();
        MotionEvent event = MotionEvent.obtain(
                mInitialTime,
                mTime,
                action,
                state.first.length,
                state.first,
                state.second,
                0 /* metaState */,
                0 /* buttonState */,
                1.0f /* xPrecision */,
                1.0f /* yPrecision */,
                DEVICE_ID,
                0 /* edgeFlags */,
                InputDevice.SOURCE_TOUCHSCREEN,
                0 /* flags */);
        mListener.onTouchEvent(event);
        if (action == MotionEvent.ACTION_UP) {
            resetTime();
        }
        event.recycle();
        mLastEventTime = mTime;
    }

    /**
     * Returns the description of the fingers' state expected by MotionEvent.
     */
    private Pair<PointerProperties[], PointerCoords[]> getFingerState() {
        int nFingers = mFingers.size();
        PointerProperties[] properties = new PointerProperties[nFingers];
        PointerCoords[] coordinates = new PointerCoords[nFingers];

        int index = 0;
        for (Map.Entry<Integer, Point> entry : mFingers.entrySet()) {
            int id = entry.getKey();
            Point location = entry.getValue();

            PointerProperties property = new PointerProperties();
            property.id = id;
            property.toolType = MotionEvent.TOOL_TYPE_FINGER;
            properties[index] = property;

            PointerCoords coordinate = new PointerCoords();
            coordinate.x = location.x;
            coordinate.y = location.y;
            coordinate.pressure = 1.0f;
            coordinates[index] = coordinate;

            index++;
        }

        return new Pair<MotionEvent.PointerProperties[], MotionEvent.PointerCoords[]>(
                properties, coordinates);
    }

    /**
     * Resets the time references for a new sequence.
     */
    private void resetTime() {
        mInitialTime = 0L;
        mLastEventTime = -1L;
        mTime = 0L;
    }
}