1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.cronet_sample_apk;
7 import android.content.ComponentName;
8 import android.content.Intent;
9 import android.net.Uri;
10 import android.test.ActivityInstrumentationTestCase2;
11 import android.text.TextUtils;
13 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
15 import org.chromium.base.test.util.UrlUtils;
17 import java.util.concurrent.atomic.AtomicBoolean;
20 * Base test class for all CronetSample based tests.
22 public class CronetSampleTestBase extends
23 ActivityInstrumentationTestCase2<CronetSampleActivity> {
26 * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
28 private static final long
29 WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
31 protected static final long
32 WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
34 public CronetSampleTestBase() {
35 super(CronetSampleActivity.class);
39 * Starts the CronetSample activity and loads the given URL. The URL can be
40 * null, in which case will default to
41 * CronetSampleActivity.DEFAULT_SHELL_URL.
43 protected CronetSampleActivity launchCronetSampleWithUrl(String url) {
44 return launchCronetSampleWithUrlAndCommandLineArgs(url, null);
48 * Starts the CronetSample activity appending the provided command line
49 * arguments and loads the given URL. The URL can be null, in which case
50 * will default to CronetSampleActivity.DEFAULT_SHELL_URL.
52 protected CronetSampleActivity launchCronetSampleWithUrlAndCommandLineArgs(
53 String url, String[] commandLineArgs) {
54 Intent intent = new Intent(Intent.ACTION_MAIN);
55 intent.addCategory(Intent.CATEGORY_LAUNCHER);
56 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
58 intent.setData(Uri.parse(url));
59 intent.setComponent(new ComponentName(
60 getInstrumentation().getTargetContext(),
61 CronetSampleActivity.class));
62 if (commandLineArgs != null) {
63 intent.putExtra(CronetSampleActivity.COMMAND_LINE_ARGS_KEY,
66 setActivityIntent(intent);
70 // TODO(cjhopman): These functions are inconsistent with
71 // launchCronetSample***. Should be startCronetSample*** and should use the
72 // url exactly without the getTestFileUrl call. Possibly these two ways of
73 // starting the activity (launch* and start*) should be merged into one.
75 * Starts the content shell activity with the provided test url. The url is
76 * synchronously loaded.
78 * @param url Test url to load.
80 protected void startActivityWithTestUrl(String url) throws Throwable {
81 launchCronetSampleWithUrl(UrlUtils.getTestFileUrl(url));
82 assertNotNull(getActivity());
83 assertTrue(waitForActiveShellToBeDoneLoading());
84 assertEquals(UrlUtils.getTestFileUrl(url), getActivity().getUrl());
88 * Starts the content shell activity with the provided test url and optional
89 * command line arguments to append. The url is synchronously loaded.
91 * @param url Test url to load.
92 * @param commandLineArgs Optional command line args to append when
93 * launching the activity.
95 protected void startActivityWithTestUrlAndCommandLineArgs(String url,
96 String[] commandLineArgs) throws Throwable {
97 launchCronetSampleWithUrlAndCommandLineArgs(
98 UrlUtils.getTestFileUrl(url), commandLineArgs);
99 assertNotNull(getActivity());
100 assertTrue(waitForActiveShellToBeDoneLoading());
104 * Waits for the Active shell to finish loading. This times out after
105 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be
106 * used for long loading pages. Instead it should be used more for test
107 * initialization. The proper way to wait is to use a
108 * TestCallbackHelperContainer after the initial load is completed.
110 * @return Whether or not the Shell was actually finished loading.
111 * @throws InterruptedException
113 protected boolean waitForActiveShellToBeDoneLoading()
114 throws InterruptedException {
115 final CronetSampleActivity activity = getActivity();
117 // Wait for the Content Shell to be initialized.
118 return CriteriaHelper.pollForCriteria(new Criteria() {
120 public boolean isSatisfied() {
122 final AtomicBoolean isLoaded = new AtomicBoolean(false);
123 runTestOnUiThread(new Runnable() {
126 if (activity != null) {
127 // There are two cases here that need to be
129 // The first is that we've just created a Shell
131 // loading because it has no URL set yet. The
133 // we've set a URL and it actually is loading.
134 isLoaded.set(!activity.isLoading() && !TextUtils
135 .isEmpty(activity.getUrl()));
142 return isLoaded.get();
143 } catch (Throwable e) {
147 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
148 CriteriaHelper.DEFAULT_POLLING_INTERVAL);