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_test_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 java.util.concurrent.atomic.AtomicBoolean;
18 * Base test class for all CronetTest based tests.
20 public class CronetTestBase extends
21 ActivityInstrumentationTestCase2<CronetTestActivity> {
23 * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
25 private static final long
26 WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
28 protected static final long
29 WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
31 public CronetTestBase() {
32 super(CronetTestActivity.class);
36 * Starts the CronetTest activity.
38 protected CronetTestActivity launchCronetTestApp() {
39 return launchCronetTestAppWithUrlAndCommandLineArgs(null, null);
43 * Starts the CronetTest activity and loads the given URL. The URL can be
46 protected CronetTestActivity launchCronetTestAppWithUrl(String url) {
47 return launchCronetTestAppWithUrlAndCommandLineArgs(url, null);
51 * Starts the CronetTest activity appending the provided command line
52 * arguments and loads the given URL. The URL can be null.
54 protected CronetTestActivity launchCronetTestAppWithUrlAndCommandLineArgs(
55 String url, String[] commandLineArgs) {
56 Intent intent = new Intent(Intent.ACTION_MAIN);
57 intent.addCategory(Intent.CATEGORY_LAUNCHER);
58 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60 intent.setData(Uri.parse(url));
61 intent.setComponent(new ComponentName(
62 getInstrumentation().getTargetContext(),
63 CronetTestActivity.class));
64 if (commandLineArgs != null) {
65 intent.putExtra(CronetTestActivity.COMMAND_LINE_ARGS_KEY,
68 setActivityIntent(intent);
73 * Waits for the Active shell to finish loading. This times out after
74 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be
75 * used for long loading pages. Instead it should be used more for test
76 * initialization. The proper way to wait is to use a
77 * TestCallbackHelperContainer after the initial load is completed.
79 * @return Whether or not the Shell was actually finished loading.
80 * @throws InterruptedException
82 protected boolean waitForActiveShellToBeDoneLoading()
83 throws InterruptedException {
84 final CronetTestActivity activity = getActivity();
86 // Wait for the Content Shell to be initialized.
87 return CriteriaHelper.pollForCriteria(new Criteria() {
89 public boolean isSatisfied() {
91 final AtomicBoolean isLoaded = new AtomicBoolean(false);
92 runTestOnUiThread(new Runnable() {
95 if (activity != null) {
96 // There are two cases here that need to be
98 // The first is that we've just created a Shell
100 // loading because it has no URL set yet. The
102 // we've set a URL and it actually is loading.
103 isLoaded.set(!activity.isLoading() && !TextUtils
104 .isEmpty(activity.getUrl()));
111 return isLoaded.get();
112 } catch (Throwable e) {
116 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
117 CriteriaHelper.DEFAULT_POLLING_INTERVAL);