Commit d717dd02 authored by jdduke's avatar jdduke Committed by Commit bot

[Android] Implement scrollTo in terms of scrollBy

Currently, ContentViewCore.scrollBy injects a GestureScrollUpdate event.
However, without a preceding GestureScrollBegin and trailing
GestureScrollEnd, this can lead to inconsistencies in the input pipeline
state. Fix scrollBy to send the proper gesture event sequence,
also changing scrollTo to be implemented in terms of scrollBy.

BUG=454355

Review URL: https://codereview.chromium.org/1131723006

Cr-Commit-Position: refs/heads/master@{#330475}
parent a085d53d
...@@ -1796,34 +1796,29 @@ public class ContentViewCore implements ...@@ -1796,34 +1796,29 @@ public class ContentViewCore implements
* are overridden, so that View's mScrollX and mScrollY will be unchanged at * are overridden, so that View's mScrollX and mScrollY will be unchanged at
* (0, 0). This is critical for drawing ContentView correctly. * (0, 0). This is critical for drawing ContentView correctly.
*/ */
public void scrollBy(int xPix, int yPix) { public void scrollBy(float dxPix, float dyPix) {
if (mNativeContentViewCore != 0) { if (mNativeContentViewCore == 0) return;
nativeScrollBy(mNativeContentViewCore, if (dxPix == 0 && dyPix == 0) return;
SystemClock.uptimeMillis(), 0, 0, xPix, yPix); long time = SystemClock.uptimeMillis();
} // It's a very real (and valid) possibility that a fling may still
// be active when programatically scrolling. Cancelling the fling in
// such cases ensures a consistent gesture event stream.
if (mPotentiallyActiveFlingCount > 0) nativeFlingCancel(mNativeContentViewCore, time);
nativeScrollBegin(mNativeContentViewCore, time, 0, 0, -dxPix, -dyPix);
nativeScrollBy(mNativeContentViewCore, time, 0, 0, dxPix, dyPix);
nativeScrollEnd(mNativeContentViewCore, time);
} }
/** /**
* @see View#scrollTo(int, int) * @see View#scrollTo(int, int)
*/ */
public void scrollTo(int xPix, int yPix) { public void scrollTo(float xPix, float yPix) {
if (mNativeContentViewCore == 0) return; if (mNativeContentViewCore == 0) return;
final float xCurrentPix = mRenderCoordinates.getScrollXPix(); final float xCurrentPix = mRenderCoordinates.getScrollXPix();
final float yCurrentPix = mRenderCoordinates.getScrollYPix(); final float yCurrentPix = mRenderCoordinates.getScrollYPix();
final float dxPix = xPix - xCurrentPix; final float dxPix = xPix - xCurrentPix;
final float dyPix = yPix - yCurrentPix; final float dyPix = yPix - yCurrentPix;
if (dxPix != 0 || dyPix != 0) { scrollBy(dxPix, dyPix);
long time = SystemClock.uptimeMillis();
// It's a very real (and valid) possibility that a fling may still
// be active when programatically scrolling. Cancelling the fling in
// such cases ensures a consistent gesture event stream.
if (mPotentiallyActiveFlingCount > 0) nativeFlingCancel(mNativeContentViewCore, time);
nativeScrollBegin(mNativeContentViewCore, time,
xCurrentPix, yCurrentPix, -dxPix, -dyPix);
nativeScrollBy(mNativeContentViewCore,
time, xCurrentPix, yCurrentPix, dxPix, dyPix);
nativeScrollEnd(mNativeContentViewCore, time);
}
} }
// NOTE: this can go away once ContentView.getScrollX() reports correct values. // NOTE: this can go away once ContentView.getScrollX() reports correct values.
......
...@@ -136,6 +136,15 @@ public class ContentViewScrollingTest extends ContentShellTestBase { ...@@ -136,6 +136,15 @@ public class ContentViewScrollingTest extends ContentShellTestBase {
}); });
} }
private void scrollBy(final int dx, final int dy) throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
getContentViewCore().getContainerView().scrollBy(dx, dy);
}
});
}
@Override @Override
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
...@@ -181,7 +190,7 @@ public class ContentViewScrollingTest extends ContentShellTestBase { ...@@ -181,7 +190,7 @@ public class ContentViewScrollingTest extends ContentShellTestBase {
@SmallTest @SmallTest
@RerunWithUpdatedContainerView @RerunWithUpdatedContainerView
@Feature({"Main"}) @Feature({"Main"})
public void testScroll() throws Throwable { public void testScrollTo() throws Throwable {
// Vertical scroll to lower-left. // Vertical scroll to lower-left.
scrollTo(0, 2500); scrollTo(0, 2500);
assertWaitForScroll(true, false); assertWaitForScroll(true, false);
...@@ -203,6 +212,38 @@ public class ContentViewScrollingTest extends ContentShellTestBase { ...@@ -203,6 +212,38 @@ public class ContentViewScrollingTest extends ContentShellTestBase {
assertWaitForScroll(false, false); assertWaitForScroll(false, false);
} }
@SmallTest
@RerunWithUpdatedContainerView
@Feature({"Main"})
public void testScrollBy() throws Throwable {
scrollTo(0, 0);
assertWaitForScroll(true, true);
// No scroll
scrollBy(0, 0);
assertWaitForScroll(true, true);
// Vertical scroll to lower-left.
scrollBy(0, 2500);
assertWaitForScroll(true, false);
// Horizontal scroll to lower-right.
scrollBy(2500, 0);
assertWaitForScroll(false, false);
// Vertical scroll to upper-right.
scrollBy(0, -2500);
assertWaitForScroll(false, true);
// Horizontal scroll to top-left.
scrollBy(-2500, 0);
assertWaitForScroll(true, true);
// Diagonal scroll to bottom-right.
scrollBy(2500, 2500);
assertWaitForScroll(false, false);
}
/** /**
* To ensure the device properly responds to bounds-exceeding scrolls, e.g., overscroll * To ensure the device properly responds to bounds-exceeding scrolls, e.g., overscroll
* effects are properly initialized. * effects are properly initialized.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment