Commit ad25c603 authored by mkosiba's avatar mkosiba Committed by Commit bot

[android_webview] Use a MotionEvent to trigger ShouldInterceptRequest tests.

This updates the test case to use a synthesized single tap gesture
to follow the link instead of JavaScript.

BUG=408426
TEST=AndroidWebViewTest

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

Cr-Commit-Position: refs/heads/master@{#293926}
parent 51b765b0
...@@ -4,12 +4,16 @@ ...@@ -4,12 +4,16 @@
package org.chromium.android_webview.test; package org.chromium.android_webview.test;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.util.Pair; import android.util.Pair;
import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwContentsClient.ShouldInterceptRequestParams; import org.chromium.android_webview.AwContentsClient.ShouldInterceptRequestParams;
import org.chromium.android_webview.AwWebResourceResponse; import org.chromium.android_webview.AwWebResourceResponse;
import org.chromium.android_webview.test.util.AwTestTouchUtils;
import org.chromium.android_webview.test.util.CommonResources; import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.android_webview.test.util.JSUtils; import org.chromium.android_webview.test.util.JSUtils;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
...@@ -236,9 +240,25 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase { ...@@ -236,9 +240,25 @@ public class AwContentsClientShouldInterceptRequestTest extends AwTestBase {
assertEquals(false, assertEquals(false,
mShouldInterceptRequestHelper.getParamsForUrl(pageWithLinkUrl).hasUserGesture); mShouldInterceptRequestHelper.getParamsForUrl(pageWithLinkUrl).hasUserGesture);
// TODO(mkosiba): Remove this once we have a real API to wait for the page to load and
// display.
// http://crbug.com/364612
//
// The code here is waiting for the "link" (which is a full-screen blue div) to appear on
// screen.
pollOnUiThread(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
Bitmap bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-(float)mTestContainerView.getWidth() / 2,
-(float)mTestContainerView.getHeight() / 2);
mAwContents.onDraw(canvas);
return bitmap.getPixel(0, 0) == Color.BLUE;
}
});
callCount = mShouldInterceptRequestHelper.getCallCount(); callCount = mShouldInterceptRequestHelper.getCallCount();
JSUtils.clickOnLinkUsingJs(this, mAwContents, AwTestTouchUtils.simulateTouchCenterOfView(mTestContainerView);
mContentsClient.getOnEvaluateJavaScriptResultHelper(), "link");
mShouldInterceptRequestHelper.waitForCallback(callCount); mShouldInterceptRequestHelper.waitForCallback(callCount);
assertEquals(true, assertEquals(true,
mShouldInterceptRequestHelper.getParamsForUrl(aboutPageUrl).hasUserGesture); mShouldInterceptRequestHelper.getParamsForUrl(aboutPageUrl).hasUserGesture);
......
...@@ -6,14 +6,13 @@ package org.chromium.android_webview.test; ...@@ -6,14 +6,13 @@ package org.chromium.android_webview.test;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.SystemClock;
import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.SmallTest; import android.test.suitebuilder.annotation.SmallTest;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MotionEvent;
import android.webkit.WebView.HitTestResult; import android.webkit.WebView.HitTestResult;
import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.test.util.AwTestTouchUtils;
import org.chromium.android_webview.test.util.CommonResources; import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
...@@ -62,23 +61,6 @@ public class WebKitHitTestTest extends AwTestBase { ...@@ -62,23 +61,6 @@ public class WebKitHitTestTest extends AwTestBase {
href + "\" " + "onclick=\"return false;\">" + anchorText + "</a>"); href + "\" " + "onclick=\"return false;\">" + anchorText + "</a>");
} }
private void simulateTouchCenterOfWebViewOnUiThread() throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
long eventTime = SystemClock.uptimeMillis();
float x = (float) (mTestView.getRight() - mTestView.getLeft()) / 2;
float y = (float) (mTestView.getBottom() - mTestView.getTop()) / 2;
mAwContents.onTouchEvent(MotionEvent.obtain(
eventTime, eventTime, MotionEvent.ACTION_DOWN,
x, y, 0));
mAwContents.onTouchEvent(MotionEvent.obtain(
eventTime, eventTime, MotionEvent.ACTION_UP,
x, y, 0));
}
});
}
private void simulateTabDownUpOnUiThread() throws Throwable { private void simulateTabDownUpOnUiThread() throws Throwable {
runTestOnUiThread(new Runnable() { runTestOnUiThread(new Runnable() {
@Override @Override
...@@ -95,7 +77,7 @@ public class WebKitHitTestTest extends AwTestBase { ...@@ -95,7 +77,7 @@ public class WebKitHitTestTest extends AwTestBase {
// Send a touch click event if byTouch is true. Otherwise, send a TAB // Send a touch click event if byTouch is true. Otherwise, send a TAB
// key event to change the focused element of the page. // key event to change the focused element of the page.
if (byTouch) { if (byTouch) {
simulateTouchCenterOfWebViewOnUiThread(); AwTestTouchUtils.simulateTouchCenterOfView(mTestView);
} else { } else {
simulateTabDownUpOnUiThread(); simulateTabDownUpOnUiThread();
} }
...@@ -334,7 +316,7 @@ public class WebKitHitTestTest extends AwTestBase { ...@@ -334,7 +316,7 @@ public class WebKitHitTestTest extends AwTestBase {
String page = CommonResources.makeHtmlPageFrom("", String page = CommonResources.makeHtmlPageFrom("",
"<img class=\"full_view\" src=\"" + relImageSrc + "\">"); "<img class=\"full_view\" src=\"" + relImageSrc + "\">");
setServerResponseAndLoad(page); setServerResponseAndLoad(page);
simulateTouchCenterOfWebViewOnUiThread(); AwTestTouchUtils.simulateTouchCenterOfView(mTestView);
pollForHitTestDataOnUiThread(HitTestResult.IMAGE_TYPE, fullImageSrc); pollForHitTestDataOnUiThread(HitTestResult.IMAGE_TYPE, fullImageSrc);
pollForHrefAndImageSrcOnUiThread(null, null, fullImageSrc); pollForHrefAndImageSrcOnUiThread(null, null, fullImageSrc);
} }
...@@ -403,7 +385,7 @@ public class WebKitHitTestTest extends AwTestBase { ...@@ -403,7 +385,7 @@ public class WebKitHitTestTest extends AwTestBase {
return mAwContents.getContentViewCore().getTitle().equals(title); return mAwContents.getContentViewCore().getTitle().equals(title);
} }
}); });
simulateTouchCenterOfWebViewOnUiThread(); AwTestTouchUtils.simulateTouchCenterOfView(mTestView);
pollForHitTestDataOnUiThread(HitTestResult.UNKNOWN_TYPE, null); pollForHitTestDataOnUiThread(HitTestResult.UNKNOWN_TYPE, null);
} }
...@@ -434,7 +416,7 @@ public class WebKitHitTestTest extends AwTestBase { ...@@ -434,7 +416,7 @@ public class WebKitHitTestTest extends AwTestBase {
// Touch image. Now the focus based hit test path will try to null out // Touch image. Now the focus based hit test path will try to null out
// the results and the touch based path will update with the result of // the results and the touch based path will update with the result of
// the image. // the image.
simulateTouchCenterOfWebViewOnUiThread(); AwTestTouchUtils.simulateTouchCenterOfView(mTestView);
// Make sure the result of image sticks. // Make sure the result of image sticks.
for (int i = 0; i < 2; ++i) { for (int i = 0; i < 2; ++i) {
......
...@@ -76,5 +76,29 @@ public class AwTestTouchUtils { ...@@ -76,5 +76,29 @@ public class AwTestTouchUtils {
} }
}); });
} }
/**
* Performs a single touch on the center of the supplied view.
* This is safe to call from the instrumentation thread and will invoke the touch
* asynchronously.
*
* @param view The view the coordinates are relative to.
*/
public static void simulateTouchCenterOfView(final View view) throws Throwable {
view.post(new Runnable() {
@Override
public void run() {
long eventTime = SystemClock.uptimeMillis();
float x = (float) (view.getRight() - view.getLeft()) / 2;
float y = (float) (view.getBottom() - view.getTop()) / 2;
view.onTouchEvent(MotionEvent.obtain(
eventTime, eventTime, MotionEvent.ACTION_DOWN,
x, y, 0));
view.onTouchEvent(MotionEvent.obtain(
eventTime, eventTime, MotionEvent.ACTION_UP,
x, y, 0));
}
});
}
} }
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