This source file includes following definitions.
- launchContentShellWithUrl
- launchContentShellWithUrlAndCommandLineArgs
- startActivityWithTestUrl
- startActivityWithTestUrlAndCommandLineArgs
- getContentView
- getContentViewCore
- waitForActiveShellToBeDoneLoading
- loadUrl
- handleBlockingCallbackAction
- assertWaitForPageScaleFactorMatch
package org.chromium.content_shell_apk;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
import android.text.TextUtils;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore;
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 org.chromium.content.browser.test.util.TestCallbackHelperContainer;
import org.chromium.content_shell.Shell;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class ContentShellTestBase extends ActivityInstrumentationTestCase2<ContentShellActivity> {
private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
protected static final long WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
public ContentShellTestBase() {
super(ContentShellActivity.class);
}
protected ContentShellActivity launchContentShellWithUrl(String url) {
return launchContentShellWithUrlAndCommandLineArgs(url, null);
}
protected ContentShellActivity launchContentShellWithUrlAndCommandLineArgs(String url,
String[] commandLineArgs) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (url != null) intent.setData(Uri.parse(url));
intent.setComponent(new ComponentName(getInstrumentation().getTargetContext(),
ContentShellActivity.class));
if (commandLineArgs != null) {
intent.putExtra(ContentShellActivity.COMMAND_LINE_ARGS_KEY, commandLineArgs);
}
setActivityIntent(intent);
return getActivity();
}
protected void startActivityWithTestUrl(String url) throws Throwable {
launchContentShellWithUrl(UrlUtils.getTestFileUrl(url));
assertNotNull(getActivity());
assertTrue(waitForActiveShellToBeDoneLoading());
assertEquals(UrlUtils.getTestFileUrl(url), getContentView().getUrl());
}
protected void startActivityWithTestUrlAndCommandLineArgs(
String url, String[] commandLineArgs) throws Throwable {
launchContentShellWithUrlAndCommandLineArgs(
UrlUtils.getTestFileUrl(url), commandLineArgs);
assertNotNull(getActivity());
assertTrue(waitForActiveShellToBeDoneLoading());
}
protected ContentView getContentView() {
return getActivity().getActiveShell().getContentView();
}
protected ContentViewCore getContentViewCore() {
return getContentView() == null ? null : getContentView().getContentViewCore();
}
protected boolean waitForActiveShellToBeDoneLoading() throws InterruptedException {
final ContentShellActivity activity = getActivity();
return CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
final AtomicBoolean isLoaded = new AtomicBoolean(false);
runTestOnUiThread(new Runnable() {
@Override
public void run() {
Shell shell = activity.getActiveShell();
if (shell != null) {
isLoaded.set(!shell.isLoading()
&& !TextUtils.isEmpty(shell.getContentView().getUrl()));
} else {
isLoaded.set(false);
}
}
});
return isLoaded.get();
} catch (Throwable e) {
return false;
}
}
}, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
}
protected void loadUrl(
final ContentView contentView, TestCallbackHelperContainer callbackHelperContainer,
final LoadUrlParams params) throws Throwable {
handleBlockingCallbackAction(
callbackHelperContainer.getOnPageFinishedHelper(),
new Runnable() {
@Override
public void run() {
contentView.loadUrl(params);
}
});
}
protected void handleBlockingCallbackAction(
CallbackHelper callbackHelper, Runnable action) throws Throwable {
int currentCallCount = callbackHelper.getCallCount();
runTestOnUiThread(action);
callbackHelper.waitForCallback(
currentCallCount, 1, WAIT_PAGE_LOADING_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
protected void assertWaitForPageScaleFactorMatch(final float expectedScale)
throws InterruptedException {
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return getContentViewCore().getScale() == expectedScale;
}
}));
}
}