This source file includes following definitions.
- launchCronetSampleWithUrl
- launchCronetSampleWithUrlAndCommandLineArgs
- startActivityWithTestUrl
- startActivityWithTestUrlAndCommandLineArgs
- waitForActiveShellToBeDoneLoading
package org.chromium.cronet_sample_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 java.util.concurrent.atomic.AtomicBoolean;
public class CronetSampleTestBase extends
ActivityInstrumentationTestCase2<CronetSampleActivity> {
private static final long
WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
protected static final long
WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
public CronetSampleTestBase() {
super(CronetSampleActivity.class);
}
protected CronetSampleActivity launchCronetSampleWithUrl(String url) {
return launchCronetSampleWithUrlAndCommandLineArgs(url, null);
}
protected CronetSampleActivity launchCronetSampleWithUrlAndCommandLineArgs(
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(),
CronetSampleActivity.class));
if (commandLineArgs != null) {
intent.putExtra(CronetSampleActivity.COMMAND_LINE_ARGS_KEY,
commandLineArgs);
}
setActivityIntent(intent);
return getActivity();
}
protected void startActivityWithTestUrl(String url) throws Throwable {
launchCronetSampleWithUrl(UrlUtils.getTestFileUrl(url));
assertNotNull(getActivity());
assertTrue(waitForActiveShellToBeDoneLoading());
assertEquals(UrlUtils.getTestFileUrl(url), getActivity().getUrl());
}
protected void startActivityWithTestUrlAndCommandLineArgs(String url,
String[] commandLineArgs) throws Throwable {
launchCronetSampleWithUrlAndCommandLineArgs(
UrlUtils.getTestFileUrl(url), commandLineArgs);
assertNotNull(getActivity());
assertTrue(waitForActiveShellToBeDoneLoading());
}
protected boolean waitForActiveShellToBeDoneLoading()
throws InterruptedException {
final CronetSampleActivity 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() {
if (activity != null) {
isLoaded.set(!activity.isLoading() && !TextUtils
.isEmpty(activity.getUrl()));
} else {
isLoaded.set(false);
}
}
});
return isLoaded.get();
} catch (Throwable e) {
return false;
}
}
}, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
CriteriaHelper.DEFAULT_POLLING_INTERVAL);
}
}