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);
69 // Make sure the activity was created as expected.
70 assertNotNull(getActivity());
72 waitForActiveShellToBeDoneLoading();
73 } catch (Throwable e) {
74 fail("Test activity has failed to load.");
80 * Waits for the Active shell to finish loading. This times out after
81 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be
82 * used for long loading pages. Instead it should be used more for test
83 * initialization. The proper way to wait is to use a
84 * TestCallbackHelperContainer after the initial load is completed.
86 * @return Whether or not the Shell was actually finished loading.
87 * @throws InterruptedException
89 protected boolean waitForActiveShellToBeDoneLoading()
90 throws InterruptedException {
91 final CronetTestActivity activity = getActivity();
93 // Wait for the Content Shell to be initialized.
94 return CriteriaHelper.pollForCriteria(new Criteria() {
96 public boolean isSatisfied() {
98 final AtomicBoolean isLoaded = new AtomicBoolean(false);
99 runTestOnUiThread(new Runnable() {
102 if (activity != null) {
103 // There are two cases here that need to be
105 // The first is that we've just created a Shell
107 // loading because it has no URL set yet. The
109 // we've set a URL and it actually is loading.
110 isLoaded.set(!activity.isLoading() && !TextUtils
111 .isEmpty(activity.getUrl()));
118 return isLoaded.get();
119 } catch (Throwable e) {
123 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
124 CriteriaHelper.DEFAULT_POLLING_INTERVAL);