This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
ScrollManagerTest.java
205 lines (173 loc) · 7.97 KB
/
ScrollManagerTest.java
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
/*
* 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 androidx.wear.widget;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import android.os.SystemClock;
import android.view.MotionEvent;
import androidx.test.filters.LargeTest;
import androidx.test.filters.MediumTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import androidx.wear.widget.util.WakeLockRule;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@MediumTest
@RunWith(AndroidJUnit4.class)
public class ScrollManagerTest {
private static final int TEST_WIDTH = 400;
private static final int TEST_HEIGHT = 400;
private static final int STEP_COUNT = 300;
private static final int EXPECTED_SCROLLS_FOR_STRAIGHT_GESTURE = 36;
private static final int EXPECTED_SCROLLS_FOR_CIRCULAR_GESTURE = 199;
@Rule
public final WakeLockRule wakeLock = new WakeLockRule();
@Rule
public final ActivityTestRule<WearableRecyclerViewTestActivity> mActivityRule =
new ActivityTestRule<>(WearableRecyclerViewTestActivity.class, true, true);
@Mock
WearableRecyclerView mMockWearableRecyclerView;
ScrollManager mScrollManagerUnderTest;
@Before
public void setUp() throws Throwable {
MockitoAnnotations.initMocks(this);
mScrollManagerUnderTest = new ScrollManager();
mScrollManagerUnderTest.setRecyclerView(mMockWearableRecyclerView, TEST_WIDTH, TEST_HEIGHT);
}
@Test
public void testStraightUpScrollingGestureLeft() throws Throwable {
// Pretend to scroll in a straight line from center left to upper left
scroll(mScrollManagerUnderTest, 30, 30, 200, 150);
// The scroll manager should require the recycler view to scroll up and only up
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_STRAIGHT_GESTURE))
.scrollBy(0, 1);
}
@LargeTest
@Test
public void testStraightDownScrollingGestureLeft() throws Throwable {
// Pretend to scroll in a straight line upper left to center left
scroll(mScrollManagerUnderTest, 30, 30, 150, 200);
// The scroll manager should require the recycler view to scroll down and only down
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_STRAIGHT_GESTURE))
.scrollBy(0, -1);
}
@Test
public void testStraightUpScrollingGestureRight() throws Throwable {
// Pretend to scroll in a straight line from center right to upper right
scroll(mScrollManagerUnderTest, 370, 370, 200, 150);
// The scroll manager should require the recycler view to scroll down and only down
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_STRAIGHT_GESTURE))
.scrollBy(0, -1);
}
@Test
public void testStraightDownScrollingGestureRight() throws Throwable {
// Pretend to scroll in a straight line upper right to center right
scroll(mScrollManagerUnderTest, 370, 370, 150, 200);
// The scroll manager should require the recycler view to scroll up and only up
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_STRAIGHT_GESTURE))
.scrollBy(0, 1);
}
@Test
public void testCircularScrollingGestureLeft() throws Throwable {
// Pretend to scroll in an arch from center left to center right
scrollOnArch(mScrollManagerUnderTest, 30, 200, 180.0f);
// The scroll manager should never reverse the scroll direction and scroll up
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_CIRCULAR_GESTURE))
.scrollBy(0, 1);
}
@Test
public void testCircularScrollingGestureRight() throws Throwable {
// Pretend to scroll in an arch from center left to center right
scrollOnArch(mScrollManagerUnderTest, 370, 200, -180.0f);
// The scroll manager should never reverse the scroll direction and scroll down.
verify(mMockWearableRecyclerView, times(EXPECTED_SCROLLS_FOR_CIRCULAR_GESTURE))
.scrollBy(0, -1);
}
private static void scroll(ScrollManager scrollManager, float fromX, float toX, float fromY,
float toY) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float y = fromY;
float x = fromX;
float yStep = (toY - fromY) / STEP_COUNT;
float xStep = (toX - fromX) / STEP_COUNT;
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
scrollManager.onTouchEvent(event);
for (int i = 0; i < STEP_COUNT; ++i) {
y += yStep;
x += xStep;
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
scrollManager.onTouchEvent(event);
}
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
scrollManager.onTouchEvent(event);
}
private static void scrollOnArch(ScrollManager scrollManager, float fromX, float fromY,
float deltaAngle) {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
float stepAngle = deltaAngle / STEP_COUNT;
double relativeX = fromX - (TEST_WIDTH / 2);
double relativeY = fromY - (TEST_HEIGHT / 2);
float radius = (float) Math.sqrt(relativeX * relativeX + relativeY * relativeY);
float angle = getAngle(fromX, fromY, TEST_WIDTH, TEST_HEIGHT);
float y = fromY;
float x = fromX;
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
scrollManager.onTouchEvent(event);
for (int i = 0; i < STEP_COUNT; ++i) {
angle += stepAngle;
x = getX(angle, radius, TEST_WIDTH);
y = getY(angle, radius, TEST_HEIGHT);
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
scrollManager.onTouchEvent(event);
}
eventTime = SystemClock.uptimeMillis();
event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
scrollManager.onTouchEvent(event);
}
private static float getX(double angle, double radius, double viewWidth) {
double radianAngle = Math.toRadians(angle - 90);
double relativeX = cos(radianAngle) * radius;
return (float) (relativeX + (viewWidth / 2));
}
private static float getY(double angle, double radius, double viewHeight) {
double radianAngle = Math.toRadians(angle - 90);
double relativeY = sin(radianAngle) * radius;
return (float) (relativeY + (viewHeight / 2));
}
private static float getAngle(double x, double y, double viewWidth, double viewHeight) {
double relativeX = x - (viewWidth / 2);
double relativeY = y - (viewHeight / 2);
double rowAngle = Math.atan2(relativeX, relativeY);
double angle = -Math.toDegrees(rowAngle) - 180;
if (angle < 0) {
angle += 360;
}
return (float) angle;
}
}