OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.content.res.Configuration; | 7 import android.content.res.Configuration; |
8 import android.os.SystemClock; | 8 import android.os.SystemClock; |
9 import android.support.test.filters.SmallTest; | 9 import android.support.test.filters.SmallTest; |
10 import android.view.InputDevice; | 10 import android.view.InputDevice; |
11 import android.view.KeyEvent; | 11 import android.view.KeyEvent; |
12 import android.view.MotionEvent; | 12 import android.view.MotionEvent; |
13 | 13 |
14 import org.chromium.base.test.util.Feature; | 14 import org.chromium.base.test.util.Feature; |
15 import org.chromium.base.test.util.RetryOnFailure; | 15 import org.chromium.base.test.util.RetryOnFailure; |
16 import org.chromium.base.test.util.UrlUtils; | 16 import org.chromium.base.test.util.UrlUtils; |
17 import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate; | 17 import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate; |
18 import org.chromium.content.browser.test.util.Criteria; | 18 import org.chromium.content.browser.test.util.Criteria; |
19 import org.chromium.content.browser.test.util.CriteriaHelper; | 19 import org.chromium.content.browser.test.util.CriteriaHelper; |
20 import org.chromium.content_shell_apk.ContentShellTestBase; | 20 import org.chromium.content_shell_apk.ContentShellTestBase; |
| 21 import org.chromium.content_shell_apk.ContentShellActivityTestRule.RerunWithUpda
tedContainerView; |
| 22 import org.junit.Rule; |
| 23 import org.junit.Test; |
| 24 import org.chromium.base.test.BaseJUnit4ClassRunner; |
| 25 import org.junit.runner.RunWith; |
| 26 import android.support.test.InstrumentationRegistry; |
| 27 import org.junit.Assert; |
| 28 import org.junit.Before; |
| 29 import org.chromium.content_shell_apk.ContentShellActivityTestRule; |
21 | 30 |
22 /** | 31 /** |
23 * Tests that we can scroll and fling a ContentView running inside ContentShell. | 32 * Tests that we can scroll and fling a ContentView running inside ContentShell. |
24 */ | 33 */ |
25 public class ContentViewScrollingTest extends ContentShellTestBase { | 34 @RunWith(BaseJUnit4ClassRunner.class) |
| 35 public class ContentViewScrollingTest { |
| 36 |
| 37 @Rule |
| 38 public ContentShellActivityTestRule mActivityTestRule = new ContentShellActi
vityTestRule(); |
26 | 39 |
27 private static final String LARGE_PAGE = UrlUtils.encodeHtmlDataUri("<html><
head>" | 40 private static final String LARGE_PAGE = UrlUtils.encodeHtmlDataUri("<html><
head>" |
28 + "<meta name=\"viewport\" content=\"width=device-width, " | 41 + "<meta name=\"viewport\" content=\"width=device-width, " |
29 + "initial-scale=2.0, maximum-scale=2.0\" />" | 42 + "initial-scale=2.0, maximum-scale=2.0\" />" |
30 + "<style>body { width: 5000px; height: 5000px; }</style></head>" | 43 + "<style>body { width: 5000px; height: 5000px; }</style></head>" |
31 + "<body>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b
ody>" | 44 + "<body>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</b
ody>" |
32 + "</html>"); | 45 + "</html>"); |
33 | 46 |
34 /** | 47 /** |
35 * InternalAccessDelegate to ensure AccessibilityEvent notifications (Eg:TYP
E_VIEW_SCROLLED) | 48 * InternalAccessDelegate to ensure AccessibilityEvent notifications (Eg:TYP
E_VIEW_SCROLLED) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 101 |
89 private void assertWaitForScroll(final boolean hugLeft, final boolean hugTop
) { | 102 private void assertWaitForScroll(final boolean hugLeft, final boolean hugTop
) { |
90 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 103 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
91 @Override | 104 @Override |
92 public boolean isSatisfied() { | 105 public boolean isSatisfied() { |
93 // Scrolling and flinging don't result in exact coordinates. | 106 // Scrolling and flinging don't result in exact coordinates. |
94 final int minThreshold = 5; | 107 final int minThreshold = 5; |
95 final int maxThreshold = 100; | 108 final int maxThreshold = 100; |
96 | 109 |
97 boolean xCorrect = hugLeft | 110 boolean xCorrect = hugLeft |
98 ? getContentViewCore().getNativeScrollXForTest() < minTh
reshold | 111 ? mActivityTestRule.getContentViewCore().getNativeScroll
XForTest() < minThreshold |
99 : getContentViewCore().getNativeScrollXForTest() > maxTh
reshold; | 112 : mActivityTestRule.getContentViewCore().getNativeScroll
XForTest() > maxThreshold; |
100 boolean yCorrect = hugTop | 113 boolean yCorrect = hugTop |
101 ? getContentViewCore().getNativeScrollYForTest() < minTh
reshold | 114 ? mActivityTestRule.getContentViewCore().getNativeScroll
YForTest() < minThreshold |
102 : getContentViewCore().getNativeScrollYForTest() > maxTh
reshold; | 115 : mActivityTestRule.getContentViewCore().getNativeScroll
YForTest() > maxThreshold; |
103 return xCorrect && yCorrect; | 116 return xCorrect && yCorrect; |
104 } | 117 } |
105 }); | 118 }); |
106 } | 119 } |
107 | 120 |
108 private void fling(final int vx, final int vy) throws Throwable { | 121 private void fling(final int vx, final int vy) throws Throwable { |
109 runTestOnUiThread(new Runnable() { | 122 mActivityTestRule.runOnUiThread(new Runnable() { |
110 @Override | 123 @Override |
111 public void run() { | 124 public void run() { |
112 getContentViewCore().flingViewport(SystemClock.uptimeMillis(), v
x, vy); | 125 mActivityTestRule.getContentViewCore().flingViewport(SystemClock
.uptimeMillis(), vx, vy); |
113 } | 126 } |
114 }); | 127 }); |
115 } | 128 } |
116 | 129 |
117 private void scrollTo(final int x, final int y) throws Throwable { | 130 private void scrollTo(final int x, final int y) throws Throwable { |
118 runTestOnUiThread(new Runnable() { | 131 mActivityTestRule.runOnUiThread(new Runnable() { |
119 @Override | 132 @Override |
120 public void run() { | 133 public void run() { |
121 getContentViewCore().getContainerView().scrollTo(x, y); | 134 mActivityTestRule.getContentViewCore().getContainerView().scroll
To(x, y); |
122 } | 135 } |
123 }); | 136 }); |
124 } | 137 } |
125 | 138 |
126 private void scrollBy(final int dx, final int dy) throws Throwable { | 139 private void scrollBy(final int dx, final int dy) throws Throwable { |
127 runTestOnUiThread(new Runnable() { | 140 mActivityTestRule.runOnUiThread(new Runnable() { |
128 @Override | 141 @Override |
129 public void run() { | 142 public void run() { |
130 getContentViewCore().getContainerView().scrollBy(dx, dy); | 143 mActivityTestRule.getContentViewCore().getContainerView().scroll
By(dx, dy); |
131 } | 144 } |
132 }); | 145 }); |
133 } | 146 } |
134 | 147 |
135 private void scrollWithJoystick(final float deltaAxisX, final float deltaAxi
sY) | 148 private void scrollWithJoystick(final float deltaAxisX, final float deltaAxi
sY) |
136 throws Throwable { | 149 throws Throwable { |
137 runTestOnUiThread(new Runnable() { | 150 mActivityTestRule.runOnUiThread(new Runnable() { |
138 @Override | 151 @Override |
139 public void run() { | 152 public void run() { |
140 // Synthesize joystick motion event and send to ContentViewCore. | 153 // Synthesize joystick motion event and send to ContentViewCore. |
141 MotionEvent leftJoystickMotionEvent = | 154 MotionEvent leftJoystickMotionEvent = |
142 MotionEvent.obtain(0, SystemClock.uptimeMillis(), Motion
Event.ACTION_MOVE, | 155 MotionEvent.obtain(0, SystemClock.uptimeMillis(), Motion
Event.ACTION_MOVE, |
143 deltaAxisX, deltaAxisY, 0); | 156 deltaAxisX, deltaAxisY, 0); |
144 leftJoystickMotionEvent.setSource( | 157 leftJoystickMotionEvent.setSource( |
145 leftJoystickMotionEvent.getSource() | InputDevice.SOURCE
_CLASS_JOYSTICK); | 158 leftJoystickMotionEvent.getSource() | InputDevice.SOURCE
_CLASS_JOYSTICK); |
146 getContentViewCore().getContainerView().onGenericMotionEvent( | 159 mActivityTestRule.getContentViewCore().getContainerView().onGene
ricMotionEvent( |
147 leftJoystickMotionEvent); | 160 leftJoystickMotionEvent); |
148 } | 161 } |
149 }); | 162 }); |
150 } | 163 } |
151 | 164 |
152 @Override | 165 @Before |
153 protected void setUp() throws Exception { | 166 |
154 super.setUp(); | 167 public void setUp() throws Exception { |
155 | 168 |
156 launchContentShellWithUrl(LARGE_PAGE); | 169 mActivityTestRule.launchContentShellWithUrl(LARGE_PAGE); |
157 waitForActiveShellToBeDoneLoading(); | 170 mActivityTestRule.waitForActiveShellToBeDoneLoading(); |
158 | 171 |
159 assertEquals(0, getContentViewCore().getNativeScrollXForTest()); | 172 Assert.assertEquals(0, mActivityTestRule.getContentViewCore().getNativeS
crollXForTest()); |
160 assertEquals(0, getContentViewCore().getNativeScrollYForTest()); | 173 Assert.assertEquals(0, mActivityTestRule.getContentViewCore().getNativeS
crollYForTest()); |
161 } | 174 } |
162 | 175 |
| 176 @Test |
163 @SmallTest | 177 @SmallTest |
164 @Feature({"Main"}) | 178 @Feature({"Main"}) |
165 @RetryOnFailure | 179 @RetryOnFailure |
166 public void testFling() throws Throwable { | 180 public void testFling() throws Throwable { |
167 // Scaling the initial velocity by the device scale factor ensures that | 181 // Scaling the initial velocity by the device scale factor ensures that |
168 // it's of sufficient magnitude for all displays densities. | 182 // it's of sufficient magnitude for all displays densities. |
169 float deviceScaleFactor = | 183 float deviceScaleFactor = |
170 getInstrumentation().getTargetContext().getResources().getDispla
yMetrics().density; | 184 InstrumentationRegistry.getInstrumentation().getTargetContext().
getResources().getDisplayMetrics().density; |
171 int velocity = (int) (1000 * deviceScaleFactor); | 185 int velocity = (int) (1000 * deviceScaleFactor); |
172 | 186 |
173 // Vertical fling to lower-left. | 187 // Vertical fling to lower-left. |
174 fling(0, -velocity); | 188 fling(0, -velocity); |
175 assertWaitForScroll(true, false); | 189 assertWaitForScroll(true, false); |
176 | 190 |
177 // Horizontal fling to lower-right. | 191 // Horizontal fling to lower-right. |
178 fling(-velocity, 0); | 192 fling(-velocity, 0); |
179 assertWaitForScroll(false, false); | 193 assertWaitForScroll(false, false); |
180 | 194 |
181 // Vertical fling to upper-right. | 195 // Vertical fling to upper-right. |
182 fling(0, velocity); | 196 fling(0, velocity); |
183 assertWaitForScroll(false, true); | 197 assertWaitForScroll(false, true); |
184 | 198 |
185 // Horizontal fling to top-left. | 199 // Horizontal fling to top-left. |
186 fling(velocity, 0); | 200 fling(velocity, 0); |
187 assertWaitForScroll(true, true); | 201 assertWaitForScroll(true, true); |
188 | 202 |
189 // Diagonal fling to bottom-right. | 203 // Diagonal fling to bottom-right. |
190 fling(-velocity, -velocity); | 204 fling(-velocity, -velocity); |
191 assertWaitForScroll(false, false); | 205 assertWaitForScroll(false, false); |
192 } | 206 } |
193 | 207 |
| 208 @Test |
194 @SmallTest | 209 @SmallTest |
195 @RerunWithUpdatedContainerView | 210 @RerunWithUpdatedContainerView |
196 @Feature({"Main"}) | 211 @Feature({"Main"}) |
197 @RetryOnFailure | 212 @RetryOnFailure |
198 public void testScrollTo() throws Throwable { | 213 public void testScrollTo() throws Throwable { |
199 // Vertical scroll to lower-left. | 214 // Vertical scroll to lower-left. |
200 scrollTo(0, 2500); | 215 scrollTo(0, 2500); |
201 assertWaitForScroll(true, false); | 216 assertWaitForScroll(true, false); |
202 | 217 |
203 // Horizontal scroll to lower-right. | 218 // Horizontal scroll to lower-right. |
204 scrollTo(2500, 2500); | 219 scrollTo(2500, 2500); |
205 assertWaitForScroll(false, false); | 220 assertWaitForScroll(false, false); |
206 | 221 |
207 // Vertical scroll to upper-right. | 222 // Vertical scroll to upper-right. |
208 scrollTo(2500, 0); | 223 scrollTo(2500, 0); |
209 assertWaitForScroll(false, true); | 224 assertWaitForScroll(false, true); |
210 | 225 |
211 // Horizontal scroll to top-left. | 226 // Horizontal scroll to top-left. |
212 scrollTo(0, 0); | 227 scrollTo(0, 0); |
213 assertWaitForScroll(true, true); | 228 assertWaitForScroll(true, true); |
214 | 229 |
215 // Diagonal scroll to bottom-right. | 230 // Diagonal scroll to bottom-right. |
216 scrollTo(2500, 2500); | 231 scrollTo(2500, 2500); |
217 assertWaitForScroll(false, false); | 232 assertWaitForScroll(false, false); |
218 } | 233 } |
219 | 234 |
| 235 @Test |
220 @SmallTest | 236 @SmallTest |
221 @RerunWithUpdatedContainerView | 237 @RerunWithUpdatedContainerView |
222 @Feature({"Main"}) | 238 @Feature({"Main"}) |
223 @RetryOnFailure | 239 @RetryOnFailure |
224 public void testScrollBy() throws Throwable { | 240 public void testScrollBy() throws Throwable { |
225 scrollTo(0, 0); | 241 scrollTo(0, 0); |
226 assertWaitForScroll(true, true); | 242 assertWaitForScroll(true, true); |
227 | 243 |
228 // No scroll | 244 // No scroll |
229 scrollBy(0, 0); | 245 scrollBy(0, 0); |
(...skipping 13 matching lines...) Expand all Loading... |
243 | 259 |
244 // Horizontal scroll to top-left. | 260 // Horizontal scroll to top-left. |
245 scrollBy(-2500, 0); | 261 scrollBy(-2500, 0); |
246 assertWaitForScroll(true, true); | 262 assertWaitForScroll(true, true); |
247 | 263 |
248 // Diagonal scroll to bottom-right. | 264 // Diagonal scroll to bottom-right. |
249 scrollBy(2500, 2500); | 265 scrollBy(2500, 2500); |
250 assertWaitForScroll(false, false); | 266 assertWaitForScroll(false, false); |
251 } | 267 } |
252 | 268 |
| 269 @Test |
253 @SmallTest | 270 @SmallTest |
254 @Feature({"Main"}) | 271 @Feature({"Main"}) |
255 public void testJoystickScroll() throws Throwable { | 272 public void testJoystickScroll() throws Throwable { |
256 scrollTo(0, 0); | 273 scrollTo(0, 0); |
257 assertWaitForScroll(true, true); | 274 assertWaitForScroll(true, true); |
258 | 275 |
259 // Scroll with X axis in deadzone and the Y axis active. | 276 // Scroll with X axis in deadzone and the Y axis active. |
260 // Only the Y axis should have an effect, arriving at lower-left. | 277 // Only the Y axis should have an effect, arriving at lower-left. |
261 scrollWithJoystick(0.1f, 1f); | 278 scrollWithJoystick(0.1f, 1f); |
262 assertWaitForScroll(true, false); | 279 assertWaitForScroll(true, false); |
(...skipping 12 matching lines...) Expand all Loading... |
275 | 292 |
276 // Diagonal scroll to bottom-right. | 293 // Diagonal scroll to bottom-right. |
277 scrollWithJoystick(1f, 1f); | 294 scrollWithJoystick(1f, 1f); |
278 assertWaitForScroll(false, false); | 295 assertWaitForScroll(false, false); |
279 } | 296 } |
280 | 297 |
281 /** | 298 /** |
282 * To ensure the device properly responds to bounds-exceeding scrolls, e.g.,
overscroll | 299 * To ensure the device properly responds to bounds-exceeding scrolls, e.g.,
overscroll |
283 * effects are properly initialized. | 300 * effects are properly initialized. |
284 */ | 301 */ |
| 302 @Test |
285 @SmallTest | 303 @SmallTest |
286 @RerunWithUpdatedContainerView | 304 @RerunWithUpdatedContainerView |
287 @Feature({"Main"}) | 305 @Feature({"Main"}) |
288 @RetryOnFailure | 306 @RetryOnFailure |
289 public void testOverScroll() throws Throwable { | 307 public void testOverScroll() throws Throwable { |
290 // Overscroll lower-left. | 308 // Overscroll lower-left. |
291 scrollTo(-10000, 10000); | 309 scrollTo(-10000, 10000); |
292 assertWaitForScroll(true, false); | 310 assertWaitForScroll(true, false); |
293 | 311 |
294 // Overscroll lower-right. | 312 // Overscroll lower-right. |
(...skipping 10 matching lines...) Expand all Loading... |
305 | 323 |
306 // Diagonal overscroll lower-right. | 324 // Diagonal overscroll lower-right. |
307 scrollTo(10000, 10000); | 325 scrollTo(10000, 10000); |
308 assertWaitForScroll(false, false); | 326 assertWaitForScroll(false, false); |
309 } | 327 } |
310 | 328 |
311 /** | 329 /** |
312 * To ensure the AccessibilityEvent notifications (Eg:TYPE_VIEW_SCROLLED) ar
e being sent | 330 * To ensure the AccessibilityEvent notifications (Eg:TYPE_VIEW_SCROLLED) ar
e being sent |
313 * properly on scrolling a page. | 331 * properly on scrolling a page. |
314 */ | 332 */ |
| 333 @Test |
315 @SmallTest | 334 @SmallTest |
316 @RerunWithUpdatedContainerView | 335 @RerunWithUpdatedContainerView |
317 @Feature({"Main"}) | 336 @Feature({"Main"}) |
318 @RetryOnFailure | 337 @RetryOnFailure |
319 public void testOnScrollChanged() throws Throwable { | 338 public void testOnScrollChanged() throws Throwable { |
320 final int scrollToX = getContentViewCore().getNativeScrollXForTest() + 2
500; | 339 final int scrollToX = mActivityTestRule.getContentViewCore().getNativeSc
rollXForTest() + 2500; |
321 final int scrollToY = getContentViewCore().getNativeScrollYForTest() + 2
500; | 340 final int scrollToY = mActivityTestRule.getContentViewCore().getNativeSc
rollYForTest() + 2500; |
322 final TestInternalAccessDelegate containerViewInternals = new TestIntern
alAccessDelegate(); | 341 final TestInternalAccessDelegate containerViewInternals = new TestIntern
alAccessDelegate(); |
323 runTestOnUiThread(new Runnable() { | 342 mActivityTestRule.runOnUiThread(new Runnable() { |
324 @Override | 343 @Override |
325 public void run() { | 344 public void run() { |
326 getContentViewCore().setContainerViewInternals(containerViewInte
rnals); | 345 mActivityTestRule.getContentViewCore().setContainerViewInternals
(containerViewInternals); |
327 } | 346 } |
328 }); | 347 }); |
329 scrollTo(scrollToX, scrollToY); | 348 scrollTo(scrollToX, scrollToY); |
330 assertWaitForScroll(false, false); | 349 assertWaitForScroll(false, false); |
331 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 350 CriteriaHelper.pollInstrumentationThread(new Criteria() { |
332 @Override | 351 @Override |
333 public boolean isSatisfied() { | 352 public boolean isSatisfied() { |
334 return containerViewInternals.isScrollChanged(); | 353 return containerViewInternals.isScrollChanged(); |
335 } | 354 } |
336 }); | 355 }); |
337 } | 356 } |
338 } | 357 } |
OLD | NEW |