This source file includes following definitions.
- setUp
- needsBrowserProcessStarted
- runTestOnUiThreadAndGetResult
- enableJavaScriptOnUiThread
- setNetworkAvailableOnUiThread
- loadUrlSync
- loadUrlSyncAndExpectError
- loadUrlAsync
- postUrlSync
- postUrlAsync
- run
- loadDataSync
- loadDataSyncWithCharset
- loadDataAsync
- loadDataWithBaseUrlSync
- loadDataWithBaseUrlAsync
- reloadSync
- createAwTestContainerView
- createAwSettings
- createTestDependencyFactory
- createAwTestContainerView
- createAwTestContainerView
- createDetachedAwTestContainerView
- createDetachedAwTestContainerView
- createAwTestContainerViewOnMainSync
- createAwTestContainerViewOnMainSync
- destroyAwContentsOnMainSync
- getTitleOnUiThread
- getContentSettingsOnUiThread
- getAwSettingsOnUiThread
- executeJavaScriptAndWaitForResult
- poll
- pollOnUiThread
- clearCacheOnUiThread
- getScaleOnUiThread
- getPixelScaleOnUiThread
- canZoomInOnUiThread
- canZoomOutOnUiThread
package org.chromium.android_webview.test;
import android.app.Instrumentation;
import android.content.Context;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.android_webview.AwBrowserContext;
import org.chromium.android_webview.AwBrowserProcess;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwContentsClient;
import org.chromium.android_webview.AwSettings;
import org.chromium.android_webview.test.util.JSUtils;
import org.chromium.base.test.util.InMemorySharedPreferences;
import org.chromium.content.browser.ContentSettings;
import org.chromium.content.browser.LoadUrlParams;
import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class AwTestBase
extends ActivityInstrumentationTestCase2<AwTestRunnerActivity> {
protected static final long WAIT_TIMEOUT_MS = scaleTimeout(15000);
protected static final int CHECK_INTERVAL = 100;
private static final String TAG = "AwTestBase";
public AwTestBase() {
super(AwTestRunnerActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
if (needsBrowserProcessStarted()) {
final Context context = getActivity();
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
AwBrowserProcess.start(context);
}
});
}
}
protected boolean needsBrowserProcessStarted() {
return true;
}
public <R> R runTestOnUiThreadAndGetResult(Callable<R> callable)
throws Exception {
FutureTask<R> task = new FutureTask<R>(callable);
getInstrumentation().waitForIdleSync();
getInstrumentation().runOnMainSync(task);
return task.get();
}
protected void enableJavaScriptOnUiThread(final AwContents awContents) {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.getSettings().setJavaScriptEnabled(true);
}
});
}
protected void setNetworkAvailableOnUiThread(final AwContents awContents,
final boolean networkUp) {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.setNetworkAvailable(networkUp);
}
});
}
protected void loadUrlSync(final AwContents awContents,
CallbackHelper onPageFinishedHelper,
final String url) throws Exception {
int currentCallCount = onPageFinishedHelper.getCallCount();
loadUrlAsync(awContents, url);
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void loadUrlSyncAndExpectError(final AwContents awContents,
CallbackHelper onPageFinishedHelper,
CallbackHelper onReceivedErrorHelper,
final String url) throws Exception {
int onErrorCallCount = onReceivedErrorHelper.getCallCount();
int onFinishedCallCount = onPageFinishedHelper.getCallCount();
loadUrlAsync(awContents, url);
onReceivedErrorHelper.waitForCallback(onErrorCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
onPageFinishedHelper.waitForCallback(onFinishedCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void loadUrlAsync(final AwContents awContents,
final String url) throws Exception {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.loadUrl(new LoadUrlParams(url));
}
});
}
protected void postUrlSync(final AwContents awContents,
CallbackHelper onPageFinishedHelper, final String url,
byte[] postData) throws Exception {
int currentCallCount = onPageFinishedHelper.getCallCount();
postUrlAsync(awContents, url, postData);
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void postUrlAsync(final AwContents awContents,
final String url, byte[] postData) throws Exception {
class PostUrl implements Runnable {
byte[] mPostData;
public PostUrl(byte[] postData) {
mPostData = postData;
}
@Override
public void run() {
awContents.loadUrl(LoadUrlParams.createLoadHttpPostParams(url,
mPostData));
}
}
getInstrumentation().runOnMainSync(new PostUrl(postData));
}
protected void loadDataSync(final AwContents awContents,
CallbackHelper onPageFinishedHelper,
final String data, final String mimeType,
final boolean isBase64Encoded) throws Exception {
int currentCallCount = onPageFinishedHelper.getCallCount();
loadDataAsync(awContents, data, mimeType, isBase64Encoded);
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void loadDataSyncWithCharset(final AwContents awContents,
CallbackHelper onPageFinishedHelper,
final String data, final String mimeType,
final boolean isBase64Encoded, final String charset)
throws Exception {
int currentCallCount = onPageFinishedHelper.getCallCount();
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.loadUrl(LoadUrlParams.createLoadDataParams(
data, mimeType, isBase64Encoded, charset));
}
});
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void loadDataAsync(final AwContents awContents, final String data,
final String mimeType, final boolean isBase64Encoded)
throws Exception {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.loadUrl(LoadUrlParams.createLoadDataParams(
data, mimeType, isBase64Encoded));
}
});
}
protected void loadDataWithBaseUrlSync(final AwContents awContents,
CallbackHelper onPageFinishedHelper, final String data, final String mimeType,
final boolean isBase64Encoded, final String baseUrl,
final String historyUrl) throws Throwable {
int currentCallCount = onPageFinishedHelper.getCallCount();
loadDataWithBaseUrlAsync(awContents, data, mimeType, isBase64Encoded, baseUrl, historyUrl);
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
protected void loadDataWithBaseUrlAsync(final AwContents awContents,
final String data, final String mimeType, final boolean isBase64Encoded,
final String baseUrl, final String historyUrl) throws Throwable {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
awContents.loadUrl(LoadUrlParams.createLoadDataParamsWithBaseUrl(
data, mimeType, isBase64Encoded, baseUrl, historyUrl));
}
});
}
protected void reloadSync(final AwContents awContents,
CallbackHelper onPageFinishedHelper) throws Exception {
int currentCallCount = onPageFinishedHelper.getCallCount();
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.getContentViewCore().reload(true);
}
});
onPageFinishedHelper.waitForCallback(currentCallCount, 1, WAIT_TIMEOUT_MS,
TimeUnit.MILLISECONDS);
}
public static class TestDependencyFactory extends AwContents.DependencyFactory {
public AwTestContainerView createAwTestContainerView(AwTestRunnerActivity activity) {
return new AwTestContainerView(activity);
}
public AwSettings createAwSettings(Context context, boolean supportsLegacyQuirks) {
return new AwSettings(context, false, supportsLegacyQuirks);
}
}
protected TestDependencyFactory createTestDependencyFactory() {
return new TestDependencyFactory();
}
protected AwTestContainerView createAwTestContainerView(
final AwContentsClient awContentsClient) {
return createAwTestContainerView(awContentsClient, false);
}
protected AwTestContainerView createAwTestContainerView(
final AwContentsClient awContentsClient, boolean supportsLegacyQuirks) {
AwTestContainerView testContainerView =
createDetachedAwTestContainerView(awContentsClient, supportsLegacyQuirks);
getActivity().addView(testContainerView);
testContainerView.requestFocus();
return testContainerView;
}
private AwBrowserContext mBrowserContext =
new AwBrowserContext(new InMemorySharedPreferences());
protected AwTestContainerView createDetachedAwTestContainerView(
final AwContentsClient awContentsClient) {
return createDetachedAwTestContainerView(awContentsClient, false);
}
protected AwTestContainerView createDetachedAwTestContainerView(
final AwContentsClient awContentsClient, boolean supportsLegacyQuirks) {
final TestDependencyFactory testDependencyFactory = createTestDependencyFactory();
final AwTestContainerView testContainerView =
testDependencyFactory.createAwTestContainerView(getActivity());
AwSettings awSettings = testDependencyFactory.createAwSettings(getActivity(),
supportsLegacyQuirks);
testContainerView.initialize(new AwContents(
mBrowserContext, testContainerView, testContainerView.getInternalAccessDelegate(),
awContentsClient, awSettings, testDependencyFactory));
return testContainerView;
}
protected AwTestContainerView createAwTestContainerViewOnMainSync(
final AwContentsClient client) throws Exception {
return createAwTestContainerViewOnMainSync(client, false);
}
protected AwTestContainerView createAwTestContainerViewOnMainSync(
final AwContentsClient client, final boolean supportsLegacyQuirks) throws Exception {
final AtomicReference<AwTestContainerView> testContainerView =
new AtomicReference<AwTestContainerView>();
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
testContainerView.set(createAwTestContainerView(client, supportsLegacyQuirks));
}
});
return testContainerView.get();
}
protected void destroyAwContentsOnMainSync(final AwContents awContents) {
if (awContents == null) return;
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.destroy();
}
});
}
protected String getTitleOnUiThread(final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<String>() {
@Override
public String call() throws Exception {
return awContents.getContentViewCore().getTitle();
}
});
}
protected ContentSettings getContentSettingsOnUiThread(
final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<ContentSettings>() {
@Override
public ContentSettings call() throws Exception {
return awContents.getContentViewCore().getContentSettings();
}
});
}
protected AwSettings getAwSettingsOnUiThread(
final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<AwSettings>() {
@Override
public AwSettings call() throws Exception {
return awContents.getSettings();
}
});
}
protected String executeJavaScriptAndWaitForResult(final AwContents awContents,
TestAwContentsClient viewClient, final String code) throws Exception {
return JSUtils.executeJavaScriptAndWaitForResult(this, awContents,
viewClient.getOnEvaluateJavaScriptResultHelper(),
code);
}
protected static void poll(final Callable<Boolean> callable) throws Exception {
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
return callable.call();
} catch (Throwable e) {
Log.e(TAG, "Exception while polling.", e);
return false;
}
}
}, WAIT_TIMEOUT_MS, CHECK_INTERVAL));
}
protected void pollOnUiThread(final Callable<Boolean> callable) throws Exception {
poll(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return runTestOnUiThreadAndGetResult(callable);
}
});
}
protected void clearCacheOnUiThread(
final AwContents awContents,
final boolean includeDiskFiles) throws Exception {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
awContents.clearCache(includeDiskFiles);
}
});
}
protected float getScaleOnUiThread(final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<Float>() {
@Override
public Float call() throws Exception {
return awContents.getPageScaleFactor();
}
});
}
protected float getPixelScaleOnUiThread(final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<Float>() {
@Override
public Float call() throws Exception {
return awContents.getScale();
}
});
}
protected boolean canZoomInOnUiThread(final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return awContents.canZoomIn();
}
});
}
protected boolean canZoomOutOnUiThread(final AwContents awContents) throws Exception {
return runTestOnUiThreadAndGetResult(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return awContents.canZoomOut();
}
});
}
}