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.
4
5package org.chromium.cronet_sample_apk;
6
7import android.content.ComponentName;
8import android.content.Intent;
9import android.net.Uri;
10import android.test.ActivityInstrumentationTestCase2;
11import android.test.suitebuilder.annotation.SmallTest;
12import android.text.TextUtils;
13
14import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
15
16import org.chromium.base.test.util.Feature;
17
18import java.util.concurrent.atomic.AtomicBoolean;
19
20/**
21 * Base test class for all CronetSample based tests.
22 */
23public class CronetSampleTest extends
24        ActivityInstrumentationTestCase2<CronetSampleActivity> {
25
26    /**
27     * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
28     */
29    private static final long
30            WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
31
32    protected static final long
33            WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
34
35    // URL used for base tests.
36    private static final String URL = "http://127.0.0.1:8000";
37
38    public CronetSampleTest() {
39        super(CronetSampleActivity.class);
40    }
41
42    @SmallTest
43    @Feature({"Cronet"})
44    public void testLoadUrl() throws Exception {
45        CronetSampleActivity activity = launchCronetSampleWithUrl(URL);
46
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());
52    }
53
54    /**
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.
58     */
59    protected CronetSampleActivity launchCronetSampleWithUrl(String url) {
60        return launchCronetSampleWithUrlAndCommandLineArgs(url, null);
61    }
62
63    /**
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.
67     */
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);
73        if (url != null)
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,
80                    commandLineArgs);
81        }
82        setActivityIntent(intent);
83        return getActivity();
84    }
85
86    /**
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.
92     *
93     * @return Whether or not the Shell was actually finished loading.
94     * @throws InterruptedException
95     */
96    protected boolean waitForActiveShellToBeDoneLoading()
97            throws InterruptedException {
98        final CronetSampleActivity activity = getActivity();
99
100        // Wait for the Content Shell to be initialized.
101        return CriteriaHelper.pollForCriteria(new Criteria() {
102                @Override
103            public boolean isSatisfied() {
104                try {
105                    final AtomicBoolean isLoaded = new AtomicBoolean(false);
106                    runTestOnUiThread(new Runnable() {
107                            @Override
108                        public void run() {
109                            if (activity != null) {
110                                // There are two cases here that need to be
111                                // accounted for.
112                                // The first is that we've just created a Shell
113                                // and it isn't
114                                // loading because it has no URL set yet. The
115                                // second is that
116                                // we've set a URL and it actually is loading.
117                                isLoaded.set(!activity.isLoading() && !TextUtils
118                                        .isEmpty(activity.getUrl()));
119                            } else {
120                                isLoaded.set(false);
121                            }
122                        }
123                    });
124
125                    return isLoaded.get();
126                } catch (Throwable e) {
127                    return false;
128                }
129            }
130        }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
131                CriteriaHelper.DEFAULT_POLLING_INTERVAL);
132    }
133}
134