This source file includes following definitions.
- startChromeBrowserProcessSync
- launchChromeShellWithUrl
- launchChromeShellWithBlankPage
- waitForActiveShellToBeDoneLoading
- clearAppData
- loadUrlWithSanitization
- assertWaitForPageScaleFactorMatch
package org.chromium.chrome.shell;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
import android.text.TextUtils;
import android.util.Log;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.base.CommandLine;
import org.chromium.base.ThreadUtils;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.chrome.test.util.ApplicationData;
import org.chromium.content.browser.BrowserStartupController;
import org.chromium.content.browser.test.util.Criteria;
import org.chromium.content.browser.test.util.CriteriaHelper;
import java.util.concurrent.atomic.AtomicBoolean;
public class ChromeShellTestBase extends ActivityInstrumentationTestCase2<ChromeShellActivity> {
private static final long WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
private static final String TAG = "ChromeShellTestBase";
public ChromeShellTestBase() {
super(ChromeShellActivity.class);
}
protected static void startChromeBrowserProcessSync(final Context targetContext) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
CommandLine.initFromFile("/data/local/tmp/chrome-shell-command-line");
try {
BrowserStartupController.get(targetContext).startBrowserProcessesSync(
BrowserStartupController.MAX_RENDERERS_LIMIT);
} catch (ProcessInitException e) {
Log.e(TAG, "Unable to load native library.", e);
fail("Unable to load native library");
}
}
});
}
protected ChromeShellActivity launchChromeShellWithUrl(String url) {
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(),
ChromeShellActivity.class));
setActivityIntent(intent);
return getActivity();
}
protected ChromeShellActivity launchChromeShellWithBlankPage() {
return launchChromeShellWithUrl("about:blank");
}
protected boolean waitForActiveShellToBeDoneLoading() throws InterruptedException {
final ChromeShellActivity 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() {
ChromeShellTab tab = activity.getActiveTab();
if (tab != null) {
isLoaded.set(!tab.isLoading()
&& !TextUtils.isEmpty(tab.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 boolean clearAppData() throws InterruptedException {
return ApplicationData.clearAppData(getInstrumentation().getTargetContext());
}
public void loadUrlWithSanitization(final String url) throws InterruptedException {
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
getActivity().getActiveTab().loadUrlWithSanitization(url);
}
});
waitForActiveShellToBeDoneLoading();
}
protected void assertWaitForPageScaleFactorMatch(final float expectedScale)
throws InterruptedException {
assertTrue(CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
return getActivity().getActiveTab().getContentViewCore().getScale() ==
expectedScale;
}
}));
}
}