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.test.suitebuilder.annotation.SmallTest;
12 import android.text.TextUtils;
14 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
16 import org.chromium.base.test.util.Feature;
18 import java.util.concurrent.atomic.AtomicBoolean;
21 * Base test class for all CronetSample based tests.
23 public class CronetSampleTest extends
24 ActivityInstrumentationTestCase2<CronetSampleActivity> {
27 * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
29 private static final long
30 WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
32 protected static final long
33 WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
35 // URL used for base tests.
36 private static final String URL = "http://127.0.0.1:8000";
38 public CronetSampleTest() {
39 super(CronetSampleActivity.class);
44 public void testLoadUrl() throws Exception {
45 CronetSampleActivity activity = launchCronetSampleWithUrl(URL);
47 // Make sure the activity was created as expected.
48 assertNotNull(activity);
49 // Make sure that the URL is set as expected.
50 assertEquals(URL, activity.getUrl());
51 assertEquals(200, activity.getHttpStatusCode());
55 * Starts the CronetSample activity and loads the given URL. The URL can be
56 * null, in which case will default to
57 * CronetSampleActivity.DEFAULT_SHELL_URL.
59 protected CronetSampleActivity launchCronetSampleWithUrl(String url) {
60 return launchCronetSampleWithUrlAndCommandLineArgs(url, null);
64 * Starts the CronetSample activity appending the provided command line
65 * arguments and loads the given URL. The URL can be null, in which case
66 * will default to CronetSampleActivity.DEFAULT_SHELL_URL.
68 protected CronetSampleActivity launchCronetSampleWithUrlAndCommandLineArgs(
69 String url, String[] commandLineArgs) {
70 Intent intent = new Intent(Intent.ACTION_MAIN);
71 intent.addCategory(Intent.CATEGORY_LAUNCHER);
72 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74 intent.setData(Uri.parse(url));
75 intent.setComponent(new ComponentName(
76 getInstrumentation().getTargetContext(),
77 CronetSampleActivity.class));
78 if (commandLineArgs != null) {
79 intent.putExtra(CronetSampleActivity.COMMAND_LINE_ARGS_KEY,
82 setActivityIntent(intent);
87 * Waits for the Active shell to finish loading. This times out after
88 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds and it shouldn't be
89 * used for long loading pages. Instead it should be used more for test
90 * initialization. The proper way to wait is to use a
91 * TestCallbackHelperContainer after the initial load is completed.
93 * @return Whether or not the Shell was actually finished loading.
94 * @throws InterruptedException
96 protected boolean waitForActiveShellToBeDoneLoading()
97 throws InterruptedException {
98 final CronetSampleActivity activity = getActivity();
100 // Wait for the Content Shell to be initialized.
101 return CriteriaHelper.pollForCriteria(new Criteria() {
103 public boolean isSatisfied() {
105 final AtomicBoolean isLoaded = new AtomicBoolean(false);
106 runTestOnUiThread(new Runnable() {
109 if (activity != null) {
110 // There are two cases here that need to be
112 // The first is that we've just created a Shell
114 // loading because it has no URL set yet. The
116 // we've set a URL and it actually is loading.
117 isLoaded.set(!activity.isLoading() && !TextUtils
118 .isEmpty(activity.getUrl()));
125 return isLoaded.get();
126 } catch (Throwable e) {
130 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
131 CriteriaHelper.DEFAULT_POLLING_INTERVAL);