OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chromoting; | 5 package org.chromium.chromoting; |
6 | 6 |
7 import android.content.Context; | |
7 import android.os.SystemClock; | 8 import android.os.SystemClock; |
8 import android.test.InstrumentationTestCase; | 9 import android.test.InstrumentationTestCase; |
9 import android.test.suitebuilder.annotation.SmallTest; | 10 import android.test.suitebuilder.annotation.SmallTest; |
10 import android.view.InputDevice; | 11 import android.view.InputDevice; |
11 import android.view.MotionEvent; | 12 import android.view.MotionEvent; |
13 import android.view.ViewConfiguration; | |
12 | 14 |
13 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
14 | 16 |
15 /** Tests for {@link SwipePinchDetector}. */ | 17 /** Tests for {@link SwipePinchDetector}. */ |
16 public class SwipePinchDetectorTest extends InstrumentationTestCase { | 18 public class SwipePinchDetectorTest extends InstrumentationTestCase { |
17 private SwipePinchDetector mDetector; | 19 private SwipePinchDetector mDetector; |
20 private float mScaledTouchSlop; | |
Sergey Ulanov
2014/07/24 01:22:57
better to call it mTouchSlop. The fact that it's s
Lambros
2014/07/24 23:13:41
Done.
| |
18 private MotionEvent.PointerProperties[] mPointers; | 21 private MotionEvent.PointerProperties[] mPointers; |
19 | 22 |
23 // Stores the current finger positions, for convenience in writing tests. Th ese values are | |
24 // initialized during setUp(). | |
25 private float mX0; | |
Sergey Ulanov
2014/07/24 01:22:57
Can these be stored as MotionEvent.PointerCoords[]
Lambros
2014/07/24 23:13:41
Done.
| |
26 private float mY0; | |
27 private float mX1; | |
28 private float mY1; | |
29 | |
20 @Override | 30 @Override |
21 public void setUp() { | 31 public void setUp() { |
22 mDetector = new SwipePinchDetector(getInstrumentation().getTargetContext ()); | 32 Context context = getInstrumentation().getTargetContext(); |
33 mDetector = new SwipePinchDetector(context); | |
34 mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); | |
23 MotionEvent.PointerProperties pointer0 = new MotionEvent.PointerProperti es(); | 35 MotionEvent.PointerProperties pointer0 = new MotionEvent.PointerProperti es(); |
24 pointer0.id = 0; | 36 pointer0.id = 0; |
25 MotionEvent.PointerProperties pointer1 = new MotionEvent.PointerProperti es(); | 37 MotionEvent.PointerProperties pointer1 = new MotionEvent.PointerProperti es(); |
26 pointer1.id = 1; | 38 pointer1.id = 1; |
27 mPointers = new MotionEvent.PointerProperties[] {pointer0, pointer1}; | 39 mPointers = new MotionEvent.PointerProperties[] {pointer0, pointer1}; |
40 | |
41 // The starting points are arbitrary, but non-zero to ensure that the te sts detect relative, | |
42 // not absolute, motion. | |
43 mX0 = 100; | |
44 mY0 = 200; | |
45 mX1 = 300; | |
46 mY1 = 400; | |
28 } | 47 } |
29 | 48 |
30 /** Verify that a simple swipe gesture is recognized as a swipe. */ | 49 /** |
50 * Simulates a 2-finger event. The action parameter should be MotionEvent.AC TION_POINTER_DOWN, | |
51 * MotionEvent.ACTION_MOVE or MotionEvent.ACTION_POINTER_UP. | |
52 */ | |
53 private void injectEvent(int action) { | |
54 final long eventTime = SystemClock.uptimeMillis(); | |
55 MotionEvent.PointerCoords p0 = new MotionEvent.PointerCoords(); | |
56 MotionEvent.PointerCoords p1 = new MotionEvent.PointerCoords(); | |
57 MotionEvent.PointerCoords[] pointerCoords = {p0, p1}; | |
58 p0.x = mX0; | |
59 p0.y = mY0; | |
60 p1.x = mX1; | |
61 p1.y = mY1; | |
62 MotionEvent event = MotionEvent.obtain(eventTime, eventTime, action, 2, mPointers, | |
63 pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0); | |
64 mDetector.onTouchEvent(event); | |
65 } | |
66 | |
67 /** Verifies that a simple swipe gesture is recognized as a swipe. */ | |
31 @SmallTest | 68 @SmallTest |
32 @Feature({"Chromoting"}) | 69 @Feature({"Chromoting"}) |
33 public void testSwipeRecognition() throws Exception { | 70 public void testSwipeRecognition() throws Exception { |
34 final long eventTime = SystemClock.uptimeMillis(); | 71 injectEvent(MotionEvent.ACTION_POINTER_DOWN); |
35 MotionEvent.PointerCoords p0 = new MotionEvent.PointerCoords(); | |
36 MotionEvent.PointerCoords p1 = new MotionEvent.PointerCoords(); | |
37 p1.x = 50; | |
38 p1.y = 0; | |
39 MotionEvent.PointerCoords[] pointerCoords = {p0, p1}; | |
40 MotionEvent event = MotionEvent.obtain(eventTime, eventTime, | |
41 MotionEvent.ACTION_POINTER_DOWN, 2, mPointers, pointerCoords, 0, 0, 1, 1, 0, 0, | |
42 InputDevice.SOURCE_TOUCHSCREEN , 0); | |
43 mDetector.onTouchEvent(event); | |
44 assertFalse(mDetector.isSwiping()); | 72 assertFalse(mDetector.isSwiping()); |
45 assertFalse(mDetector.isPinching()); | 73 assertFalse(mDetector.isPinching()); |
46 | 74 |
47 // Any distance greater than the touch-slop threshold should work. | 75 // Any distance greater than the touch-slop threshold should work. |
48 p0.y += 100; | 76 mY0 += mScaledTouchSlop * 2; |
49 p1.y += 100; | 77 mY1 += mScaledTouchSlop * 2; |
50 | 78 injectEvent(MotionEvent.ACTION_MOVE); |
51 event = MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_MOVE , 2, mPointers, | |
52 pointerCoords, 0, 0, 1, 1, 0, 0, InputDevice.SOURCE_TOUCHSCREEN , 0); | |
53 mDetector.onTouchEvent(event); | |
54 assertTrue(mDetector.isSwiping()); | 79 assertTrue(mDetector.isSwiping()); |
55 assertFalse(mDetector.isPinching()); | 80 assertFalse(mDetector.isPinching()); |
56 } | 81 } |
82 | |
83 /** Verifies that a simple pinch gesture is recognized. */ | |
84 @SmallTest | |
85 @Feature({"Chromoting"}) | |
86 public void testPinchRecognition() throws Exception { | |
87 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
88 assertFalse(mDetector.isSwiping()); | |
89 assertFalse(mDetector.isPinching()); | |
90 | |
91 // Any distance greater than the touch-slop threshold should work. | |
92 mX0 -= mScaledTouchSlop * 2; | |
93 mX1 += mScaledTouchSlop * 2; | |
94 injectEvent(MotionEvent.ACTION_MOVE); | |
95 assertFalse(mDetector.isSwiping()); | |
96 assertTrue(mDetector.isPinching()); | |
97 } | |
98 | |
99 /** Verifies that motion less than touch-slop does not trigger anything. */ | |
100 @SmallTest | |
101 @Feature({"Chromoting"}) | |
102 public void testNoMotion() throws Exception { | |
103 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
104 mX0 += mScaledTouchSlop / 2; | |
105 mY0 += mScaledTouchSlop / 2; | |
106 mX1 -= mScaledTouchSlop / 2; | |
107 mY1 -= mScaledTouchSlop / 2; | |
108 injectEvent(MotionEvent.ACTION_MOVE); | |
109 assertFalse(mDetector.isSwiping()); | |
110 assertFalse(mDetector.isPinching()); | |
111 } | |
112 | |
113 /** Verifies that a pinch with one finger stationary is detected. */ | |
Sergey Ulanov
2014/07/24 01:22:57
Should we also test the case when there are multip
Lambros
2014/07/24 23:13:41
Added more gradual-movement tests.
| |
114 @SmallTest | |
115 @Feature({"Chromoting"}) | |
116 public void testOneFingerStationary() throws Exception { | |
117 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
118 | |
119 // The triggering threshold in this case (one finger stationary) is mSca ledTouchSlop * 2; | |
120 mX1 += mScaledTouchSlop * 3; | |
121 injectEvent(MotionEvent.ACTION_MOVE); | |
122 assertFalse(mDetector.isSwiping()); | |
123 assertTrue(mDetector.isPinching()); | |
124 | |
125 // Do the same test for the other finger. | |
126 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
127 mX0 += mScaledTouchSlop * 3; | |
128 injectEvent(MotionEvent.ACTION_MOVE); | |
129 assertFalse(mDetector.isSwiping()); | |
130 assertTrue(mDetector.isPinching()); | |
131 } | |
132 | |
133 /** Verifies that a swipe is recognized, even if the fingers move at differe nt rates. */ | |
134 @SmallTest | |
135 @Feature({"Chromoting"}) | |
136 public void testUnevenSwipe() throws Exception { | |
137 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
138 for (int i = 0; i < 50; i++) { | |
139 // The fingers need to move similarly enough so that one finger move s a distance of | |
140 // 2 * mScaledTouchSlop after the other finger moves a distance of m ScaledTouchSlop. | |
141 // Otherwise the gesture would be mis-detected as a one-finger-stati onary pinch. | |
142 mY0 += 2; | |
143 mY1 += 3; | |
144 injectEvent(MotionEvent.ACTION_MOVE); | |
145 } | |
146 | |
147 assertTrue(mDetector.isSwiping()); | |
148 assertFalse(mDetector.isPinching()); | |
149 } | |
150 | |
151 /** Verifies that the detector is reset when a gesture terminates or a new g esture begins. */ | |
152 @SmallTest | |
153 @Feature({"Chromoting"}) | |
154 public void testDetectorReset() throws Exception { | |
155 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
156 mX0 += mScaledTouchSlop * 3; | |
157 injectEvent(MotionEvent.ACTION_MOVE); | |
158 assertTrue(mDetector.isPinching()); | |
159 | |
160 // ACTION_POINTER_UP should terminate the gesture. | |
161 injectEvent(MotionEvent.ACTION_POINTER_UP); | |
162 assertFalse(mDetector.isPinching()); | |
163 | |
164 // Repeat the same test, but use ACTION_POINTER_DOWN to start a new gest ure, which should | |
165 // terminate the current one. | |
166 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
167 mX0 += mScaledTouchSlop * 3; | |
168 injectEvent(MotionEvent.ACTION_MOVE); | |
169 assertTrue(mDetector.isPinching()); | |
170 injectEvent(MotionEvent.ACTION_POINTER_DOWN); | |
171 assertFalse(mDetector.isPinching()); | |
172 } | |
57 } | 173 } |
OLD | NEW |