Commit 8703246c authored by hjd@google.com's avatar hjd@google.com

Adds unit tests for AwContentsClientCallbackHelper

Also filled in some JavaDocs to satisfy the linter.

BUG=355072
TEST=AndroidWebviewTest

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263041 0039d316-1c4b-4281-b951-d872f2087c98
parent c8a74766
...@@ -10,6 +10,8 @@ import android.os.Looper; ...@@ -10,6 +10,8 @@ import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
import com.google.common.annotations.VisibleForTesting;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/** /**
...@@ -18,7 +20,8 @@ import java.util.concurrent.Callable; ...@@ -18,7 +20,8 @@ import java.util.concurrent.Callable;
* Most callbacks do no go through here, but get forwarded to AwContentsClient directly. The * Most callbacks do no go through here, but get forwarded to AwContentsClient directly. The
* messages processed here may originate from the IO or UI thread. * messages processed here may originate from the IO or UI thread.
*/ */
class AwContentsClientCallbackHelper { @VisibleForTesting
public class AwContentsClientCallbackHelper {
// TODO(boliu): Consider removing DownloadInfo and LoginRequestInfo by using native // TODO(boliu): Consider removing DownloadInfo and LoginRequestInfo by using native
// MessageLoop to post directly to AwContents. // MessageLoop to post directly to AwContents.
......
...@@ -8,8 +8,8 @@ import android.test.suitebuilder.annotation.SmallTest; ...@@ -8,8 +8,8 @@ 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.test.TestAwContentsClient.OnReceivedLoginRequestHelper;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.net.test.util.TestWebServer; import org.chromium.net.test.util.TestWebServer;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -19,53 +19,6 @@ import java.util.List; ...@@ -19,53 +19,6 @@ import java.util.List;
* Tests for the AwContentsClient.onReceivedLoginRequest callback. * Tests for the AwContentsClient.onReceivedLoginRequest callback.
*/ */
public class AwContentsClientAutoLoginTest extends AwTestBase { public class AwContentsClientAutoLoginTest extends AwTestBase {
public static class OnReceivedLoginRequestHelper extends CallbackHelper {
String mRealm;
String mAccount;
String mArgs;
public String getRealm() {
assert getCallCount() > 0;
return mRealm;
}
public String getAccount() {
assert getCallCount() > 0;
return mAccount;
}
public String getArgs() {
assert getCallCount() > 0;
return mArgs;
}
public void notifyCalled(String realm, String account, String args) {
mRealm = realm;
mAccount = account;
mArgs = args;
notifyCalled();
}
}
private static class TestAwContentsClient
extends org.chromium.android_webview.test.TestAwContentsClient {
private OnReceivedLoginRequestHelper mOnReceivedLoginRequestHelper;
public TestAwContentsClient() {
mOnReceivedLoginRequestHelper = new OnReceivedLoginRequestHelper();
}
public OnReceivedLoginRequestHelper getOnReceivedLoginRequestHelper() {
return mOnReceivedLoginRequestHelper;
}
@Override
public void onReceivedLoginRequest(String realm, String account, String args) {
getOnReceivedLoginRequestHelper().notifyCalled(realm, account, args);
}
}
private TestAwContentsClient mContentsClient = new TestAwContentsClient(); private TestAwContentsClient mContentsClient = new TestAwContentsClient();
private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader, private void autoLoginTestHelper(final String testName, final String xAutoLoginHeader,
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.android_webview.test;
import android.graphics.Picture;
import android.os.Handler;
import android.os.Looper;
import android.test.suitebuilder.annotation.SmallTest;
import org.chromium.android_webview.AwContentsClientCallbackHelper;
import org.chromium.android_webview.test.TestAwContentsClient.OnDownloadStartHelper;
import org.chromium.android_webview.test.TestAwContentsClient.OnReceivedLoginRequestHelper;
import org.chromium.android_webview.test.TestAwContentsClient.PictureListenerHelper;
import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageStartedHelper;
import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnReceivedErrorHelper;
import java.util.concurrent.Callable;
/**
* Test suite for AwContentsClientCallbackHelper.
*/
public class AwContentsClientCallbackHelperTest extends AwTestBase {
/**
* Callback helper for OnLoadedResource.
*/
public static class OnLoadResourceHelper extends CallbackHelper {
private String mLastLoadedResource;
public String getLastLoadedResource() {
assert getCallCount() > 0;
return mLastLoadedResource;
}
public void notifyCalled(String url) {
mLastLoadedResource = url;
notifyCalled();
}
}
/**
* TestAwContentsClient extended with OnLoadResourceHelper.
*/
public static class TestAwContentsClient
extends org.chromium.android_webview.test.TestAwContentsClient {
private final OnLoadResourceHelper mOnLoadResourceHelper;
public TestAwContentsClient() {
super();
mOnLoadResourceHelper = new OnLoadResourceHelper();
}
public OnLoadResourceHelper getOnLoadResourceHelper() {
return mOnLoadResourceHelper;
}
@Override
public void onLoadResource(String url) {
mOnLoadResourceHelper.notifyCalled(url);
}
}
static final int PICTURE_TIMEOUT = 5000;
static final String TEST_URL = "www.example.com";
static final String REALM = "www.example.com";
static final String ACCOUNT = "account";
static final String ARGS = "args";
static final String USER_AGENT = "userAgent";
static final String CONTENT_DISPOSITION = "contentDisposition";
static final String MIME_TYPE = "mimeType";
static final int CONTENT_LENGTH = 42;
static final float NEW_SCALE = 1.0f;
static final float OLD_SCALE = 2.0f;
static final int ERROR_CODE = 2;
static final String ERROR_MESSAGE = "A horrible thing has occurred!";
private TestAwContentsClient mContentsClient;
private AwContentsClientCallbackHelper mClientHelper;
private Looper mLooper;
@Override
protected void setUp() throws Exception {
super.setUp();
mLooper = Looper.getMainLooper();
mContentsClient = new TestAwContentsClient();
mClientHelper = new AwContentsClientCallbackHelper(mLooper, mContentsClient);
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnLoadResource() throws Exception {
OnLoadResourceHelper loadResourceHelper = mContentsClient.getOnLoadResourceHelper();
int onLoadResourceCount = loadResourceHelper.getCallCount();
mClientHelper.postOnLoadResource(TEST_URL);
loadResourceHelper.waitForCallback(onLoadResourceCount);
assertEquals(TEST_URL, loadResourceHelper.getLastLoadedResource());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnPageStarted() throws Exception {
OnPageStartedHelper pageStartedHelper = mContentsClient.getOnPageStartedHelper();
int onPageStartedCount = pageStartedHelper.getCallCount();
mClientHelper.postOnPageStarted(TEST_URL);
pageStartedHelper.waitForCallback(onPageStartedCount);
assertEquals(TEST_URL, pageStartedHelper.getUrl());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnDownloadStart() throws Exception {
OnDownloadStartHelper downloadStartHelper = mContentsClient.getOnDownloadStartHelper();
int onDownloadStartCount = downloadStartHelper.getCallCount();
mClientHelper.postOnDownloadStart(TEST_URL, USER_AGENT, CONTENT_DISPOSITION,
MIME_TYPE, CONTENT_LENGTH);
downloadStartHelper.waitForCallback(onDownloadStartCount);
assertEquals(TEST_URL, downloadStartHelper.getUrl());
assertEquals(USER_AGENT, downloadStartHelper.getUserAgent());
assertEquals(CONTENT_DISPOSITION, downloadStartHelper.getContentDisposition());
assertEquals(MIME_TYPE, downloadStartHelper.getMimeType());
assertEquals(CONTENT_LENGTH, downloadStartHelper.getContentLength());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnNewPicture() throws Exception {
final PictureListenerHelper pictureListenerHelper =
mContentsClient.getPictureListenerHelper();
final Picture thePicture = new Picture();
final Callable<Picture> pictureProvider = new Callable<Picture>() {
@Override
public Picture call() {
return thePicture;
}
};
// AwContentsClientCallbackHelper rate limits photo callbacks so two posts in close
// succession should only result in one callback.
final int onNewPictureCount = pictureListenerHelper.getCallCount();
// To trip the rate limiting the second postNewPicture call needs to happen
// before mLooper processes the first. To do this we run both posts as a single block
// and we do it in the thread that is processes the callbacks (mLooper).
Handler mainHandler = new Handler(mLooper);
Runnable postPictures = new Runnable() {
@Override
public void run() {
mClientHelper.postOnNewPicture(pictureProvider);
mClientHelper.postOnNewPicture(pictureProvider);
}
};
mainHandler.post(postPictures);
// We want to check that one and only one callback is fired,
// First we wait for the first call back to complete, this ensures that both posts have
// finished.
pictureListenerHelper.waitForCallback(onNewPictureCount);
// Then we post a runnable on the callback handler thread. Since both posts have happened
// and the first callback has happened a second callback (if it exists) must be
// in the queue before this runnable.
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
}
});
// When that runnable has finished we assert that one and only on callback happened.
assertEquals(thePicture, pictureListenerHelper.getPicture());
assertEquals(onNewPictureCount + 1, pictureListenerHelper.getCallCount());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnReceivedLoginRequest() throws Exception {
OnReceivedLoginRequestHelper receivedLoginRequestHelper =
mContentsClient.getOnReceivedLoginRequestHelper();
int onReceivedLoginRequestCount = receivedLoginRequestHelper.getCallCount();
mClientHelper.postOnReceivedLoginRequest(REALM, ACCOUNT, ARGS);
receivedLoginRequestHelper.waitForCallback(onReceivedLoginRequestCount);
assertEquals(REALM, receivedLoginRequestHelper.getRealm());
assertEquals(ACCOUNT, receivedLoginRequestHelper.getAccount());
assertEquals(ARGS, receivedLoginRequestHelper.getArgs());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnReceivedError() throws Exception {
OnReceivedErrorHelper receivedErrorHelper =
mContentsClient.getOnReceivedErrorHelper();
int onReceivedErrorCount = receivedErrorHelper.getCallCount();
mClientHelper.postOnReceivedError(ERROR_CODE, ERROR_MESSAGE, TEST_URL);
receivedErrorHelper.waitForCallback(onReceivedErrorCount);
assertEquals(ERROR_CODE, receivedErrorHelper.getErrorCode());
assertEquals(ERROR_MESSAGE, receivedErrorHelper.getDescription());
assertEquals(TEST_URL, receivedErrorHelper.getFailingUrl());
}
@Feature({"AndroidWebView"})
@SmallTest
public void testOnScaleChangedScaled() throws Exception {
TestAwContentsClient.OnScaleChangedHelper scaleChangedHelper =
mContentsClient.getOnScaleChangedHelper();
int onScaleChangeCount = scaleChangedHelper.getCallCount();
mClientHelper.postOnScaleChangedScaled(OLD_SCALE, NEW_SCALE);
scaleChangedHelper.waitForCallback(onScaleChangeCount);
assertEquals(OLD_SCALE, scaleChangedHelper.getOldScale());
assertEquals(NEW_SCALE, scaleChangedHelper.getNewScale());
}
}
...@@ -16,6 +16,7 @@ import android.util.Pair; ...@@ -16,6 +16,7 @@ import android.util.Pair;
import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwSettings; import org.chromium.android_webview.AwSettings;
import org.chromium.android_webview.test.TestAwContentsClient.OnDownloadStartHelper;
import org.chromium.android_webview.test.util.CommonResources; import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.base.test.util.Feature; import org.chromium.base.test.util.Feature;
import org.chromium.content.browser.test.util.CallbackHelper; import org.chromium.content.browser.test.util.CallbackHelper;
...@@ -34,72 +35,6 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -34,72 +35,6 @@ import java.util.concurrent.atomic.AtomicInteger;
* AwContents tests. * AwContents tests.
*/ */
public class AwContentsTest extends AwTestBase { public class AwContentsTest extends AwTestBase {
private static class OnDownloadStartHelper extends CallbackHelper {
String mUrl;
String mUserAgent;
String mContentDisposition;
String mMimeType;
long mContentLength;
public String getUrl() {
assert getCallCount() > 0;
return mUrl;
}
public String getUserAgent() {
assert getCallCount() > 0;
return mUserAgent;
}
public String getContentDisposition() {
assert getCallCount() > 0;
return mContentDisposition;
}
public String getMimeType() {
assert getCallCount() > 0;
return mMimeType;
}
public long getContentLength() {
assert getCallCount() > 0;
return mContentLength;
}
public void notifyCalled(String url, String userAgent, String contentDisposition,
String mimeType, long contentLength) {
mUrl = url;
mUserAgent = userAgent;
mContentDisposition = contentDisposition;
mMimeType = mimeType;
mContentLength = contentLength;
notifyCalled();
}
}
private static class TestAwContentsClient
extends org.chromium.android_webview.test.TestAwContentsClient {
private OnDownloadStartHelper mOnDownloadStartHelper;
public TestAwContentsClient() {
mOnDownloadStartHelper = new OnDownloadStartHelper();
}
public OnDownloadStartHelper getOnDownloadStartHelper() {
return mOnDownloadStartHelper;
}
@Override
public void onDownloadStart(String url,
String userAgent,
String contentDisposition,
String mimeType,
long contentLength) {
getOnDownloadStartHelper().notifyCalled(url, userAgent, contentDisposition, mimeType,
contentLength);
}
}
private TestAwContentsClient mContentsClient = new TestAwContentsClient(); private TestAwContentsClient mContentsClient = new TestAwContentsClient();
......
...@@ -19,6 +19,8 @@ class TestAwContentsClient extends NullContentsClient { ...@@ -19,6 +19,8 @@ class TestAwContentsClient extends NullContentsClient {
private final OnPageStartedHelper mOnPageStartedHelper; private final OnPageStartedHelper mOnPageStartedHelper;
private final OnPageFinishedHelper mOnPageFinishedHelper; private final OnPageFinishedHelper mOnPageFinishedHelper;
private final OnReceivedErrorHelper mOnReceivedErrorHelper; private final OnReceivedErrorHelper mOnReceivedErrorHelper;
private final OnDownloadStartHelper mOnDownloadStartHelper;
private final OnReceivedLoginRequestHelper mOnReceivedLoginRequestHelper;
private final OnEvaluateJavaScriptResultHelper mOnEvaluateJavaScriptResultHelper; private final OnEvaluateJavaScriptResultHelper mOnEvaluateJavaScriptResultHelper;
private final AddMessageToConsoleHelper mAddMessageToConsoleHelper; private final AddMessageToConsoleHelper mAddMessageToConsoleHelper;
private final OnScaleChangedHelper mOnScaleChangedHelper; private final OnScaleChangedHelper mOnScaleChangedHelper;
...@@ -30,6 +32,8 @@ class TestAwContentsClient extends NullContentsClient { ...@@ -30,6 +32,8 @@ class TestAwContentsClient extends NullContentsClient {
mOnPageStartedHelper = new OnPageStartedHelper(); mOnPageStartedHelper = new OnPageStartedHelper();
mOnPageFinishedHelper = new OnPageFinishedHelper(); mOnPageFinishedHelper = new OnPageFinishedHelper();
mOnReceivedErrorHelper = new OnReceivedErrorHelper(); mOnReceivedErrorHelper = new OnReceivedErrorHelper();
mOnDownloadStartHelper = new OnDownloadStartHelper();
mOnReceivedLoginRequestHelper = new OnReceivedLoginRequestHelper();
mOnEvaluateJavaScriptResultHelper = new OnEvaluateJavaScriptResultHelper(); mOnEvaluateJavaScriptResultHelper = new OnEvaluateJavaScriptResultHelper();
mAddMessageToConsoleHelper = new AddMessageToConsoleHelper(); mAddMessageToConsoleHelper = new AddMessageToConsoleHelper();
mOnScaleChangedHelper = new OnScaleChangedHelper(); mOnScaleChangedHelper = new OnScaleChangedHelper();
...@@ -49,6 +53,14 @@ class TestAwContentsClient extends NullContentsClient { ...@@ -49,6 +53,14 @@ class TestAwContentsClient extends NullContentsClient {
return mOnReceivedErrorHelper; return mOnReceivedErrorHelper;
} }
public OnDownloadStartHelper getOnDownloadStartHelper() {
return mOnDownloadStartHelper;
}
public OnReceivedLoginRequestHelper getOnReceivedLoginRequestHelper() {
return mOnReceivedLoginRequestHelper;
}
public OnEvaluateJavaScriptResultHelper getOnEvaluateJavaScriptResultHelper() { public OnEvaluateJavaScriptResultHelper getOnEvaluateJavaScriptResultHelper() {
return mOnEvaluateJavaScriptResultHelper; return mOnEvaluateJavaScriptResultHelper;
} }
...@@ -69,6 +81,15 @@ class TestAwContentsClient extends NullContentsClient { ...@@ -69,6 +81,15 @@ class TestAwContentsClient extends NullContentsClient {
mCurrentScale = newScale; mCurrentScale = newScale;
super.notifyCalled(); super.notifyCalled();
} }
public float getOldScale() {
return mPreviousScale;
}
public float getNewScale() {
return mCurrentScale;
}
public float getLastScaleRatio() { public float getLastScaleRatio() {
assert getCallCount() > 0; assert getCallCount() > 0;
return mCurrentScale / mPreviousScale; return mCurrentScale / mPreviousScale;
...@@ -107,6 +128,98 @@ class TestAwContentsClient extends NullContentsClient { ...@@ -107,6 +128,98 @@ class TestAwContentsClient extends NullContentsClient {
mOnReceivedErrorHelper.notifyCalled(errorCode, description, failingUrl); mOnReceivedErrorHelper.notifyCalled(errorCode, description, failingUrl);
} }
/**
* CallbackHelper for OnDownloadStart.
*/
public static class OnDownloadStartHelper extends CallbackHelper {
private String mUrl;
private String mUserAgent;
private String mContentDisposition;
private String mMimeType;
long mContentLength;
public String getUrl() {
assert getCallCount() > 0;
return mUrl;
}
public String getUserAgent() {
assert getCallCount() > 0;
return mUserAgent;
}
public String getContentDisposition() {
assert getCallCount() > 0;
return mContentDisposition;
}
public String getMimeType() {
assert getCallCount() > 0;
return mMimeType;
}
public long getContentLength() {
assert getCallCount() > 0;
return mContentLength;
}
public void notifyCalled(String url, String userAgent, String contentDisposition,
String mimeType, long contentLength) {
mUrl = url;
mUserAgent = userAgent;
mContentDisposition = contentDisposition;
mMimeType = mimeType;
mContentLength = contentLength;
notifyCalled();
}
}
@Override
public void onDownloadStart(String url,
String userAgent,
String contentDisposition,
String mimeType,
long contentLength) {
getOnDownloadStartHelper().notifyCalled(url, userAgent, contentDisposition, mimeType,
contentLength);
}
/**
* CallbackHelper for OnReceivedLoginRequest.
*/
public static class OnReceivedLoginRequestHelper extends CallbackHelper {
private String mRealm;
private String mAccount;
private String mArgs;
public String getRealm() {
assert getCallCount() > 0;
return mRealm;
}
public String getAccount() {
assert getCallCount() > 0;
return mAccount;
}
public String getArgs() {
assert getCallCount() > 0;
return mArgs;
}
public void notifyCalled(String realm, String account, String args) {
mRealm = realm;
mAccount = account;
mArgs = args;
notifyCalled();
}
}
@Override
public void onReceivedLoginRequest(String realm, String account, String args) {
getOnReceivedLoginRequestHelper().notifyCalled(realm, account, args);
}
@Override @Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) { public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
mAddMessageToConsoleHelper.notifyCalled(consoleMessage.messageLevel().ordinal(), mAddMessageToConsoleHelper.notifyCalled(consoleMessage.messageLevel().ordinal(),
......
...@@ -30,6 +30,9 @@ public class TestCallbackHelperContainer { ...@@ -30,6 +30,9 @@ public class TestCallbackHelperContainer {
mTestWebContentsObserver = contentsObserver; mTestWebContentsObserver = contentsObserver;
} }
/**
* CallbackHelper for OnPageFinished.
*/
public static class OnPageFinishedHelper extends CallbackHelper { public static class OnPageFinishedHelper extends CallbackHelper {
private String mUrl; private String mUrl;
public void notifyCalled(String url) { public void notifyCalled(String url) {
...@@ -42,6 +45,9 @@ public class TestCallbackHelperContainer { ...@@ -42,6 +45,9 @@ public class TestCallbackHelperContainer {
} }
} }
/**
* CallbackHelper for OnPageStarted.
*/
public static class OnPageStartedHelper extends CallbackHelper { public static class OnPageStartedHelper extends CallbackHelper {
private String mUrl; private String mUrl;
public void notifyCalled(String url) { public void notifyCalled(String url) {
...@@ -54,6 +60,9 @@ public class TestCallbackHelperContainer { ...@@ -54,6 +60,9 @@ public class TestCallbackHelperContainer {
} }
} }
/**
* CallbackHelper for OnReceivedError.
*/
public static class OnReceivedErrorHelper extends CallbackHelper { public static class OnReceivedErrorHelper extends CallbackHelper {
private int mErrorCode; private int mErrorCode;
private String mDescription; private String mDescription;
...@@ -78,6 +87,11 @@ public class TestCallbackHelperContainer { ...@@ -78,6 +87,11 @@ public class TestCallbackHelperContainer {
} }
} }
/**
* CallbackHelper for OnEvaluateJavaScriptResult.
* This class wraps the evaluation of JavaScript code allowing test code to
* synchronously evaluate JavaScript and then test the result.
*/
public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper { public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper {
private String mJsonResult; private String mJsonResult;
...@@ -152,6 +166,9 @@ public class TestCallbackHelperContainer { ...@@ -152,6 +166,9 @@ public class TestCallbackHelperContainer {
} }
} }
/**
* CallbackHelper for OnStartContentIntent.
*/
public static class OnStartContentIntentHelper extends CallbackHelper { public static class OnStartContentIntentHelper extends CallbackHelper {
private String mIntentUrl; private String mIntentUrl;
public void notifyCalled(String intentUrl) { public void notifyCalled(String intentUrl) {
......
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