Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / sample / javatests / src / org / chromium / cronet_sample_apk / CronetSampleTestBase.java
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
5 package org.chromium.cronet_sample_apk;
6
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;
12
13 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
14
15 import org.chromium.base.test.util.UrlUtils;
16
17 import java.util.concurrent.atomic.AtomicBoolean;
18
19 /**
20  * Base test class for all CronetSample based tests.
21  */
22 public class CronetSampleTestBase extends
23         ActivityInstrumentationTestCase2<CronetSampleActivity> {
24
25     /**
26      * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
27      */
28     private static final long
29             WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
30
31     protected static final long
32             WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
33
34     public CronetSampleTestBase() {
35         super(CronetSampleActivity.class);
36     }
37
38     /**
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.
42      */
43     protected CronetSampleActivity launchCronetSampleWithUrl(String url) {
44         return launchCronetSampleWithUrlAndCommandLineArgs(url, null);
45     }
46
47     /**
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.
51      */
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);
57         if (url != null)
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,
64                     commandLineArgs);
65         }
66         setActivityIntent(intent);
67         return getActivity();
68     }
69
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.
74     /**
75      * Starts the content shell activity with the provided test url. The url is
76      * synchronously loaded.
77      *
78      * @param url Test url to load.
79      */
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());
85     }
86
87     /**
88      * Starts the content shell activity with the provided test url and optional
89      * command line arguments to append. The url is synchronously loaded.
90      *
91      * @param url Test url to load.
92      * @param commandLineArgs Optional command line args to append when
93      *            launching the activity.
94      */
95     protected void startActivityWithTestUrlAndCommandLineArgs(String url,
96             String[] commandLineArgs) throws Throwable {
97         launchCronetSampleWithUrlAndCommandLineArgs(
98                 UrlUtils.getTestFileUrl(url), commandLineArgs);
99         assertNotNull(getActivity());
100         assertTrue(waitForActiveShellToBeDoneLoading());
101     }
102
103     /**
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.
109      *
110      * @return Whether or not the Shell was actually finished loading.
111      * @throws InterruptedException
112      */
113     protected boolean waitForActiveShellToBeDoneLoading()
114             throws InterruptedException {
115         final CronetSampleActivity activity = getActivity();
116
117         // Wait for the Content Shell to be initialized.
118         return CriteriaHelper.pollForCriteria(new Criteria() {
119                 @Override
120             public boolean isSatisfied() {
121                 try {
122                     final AtomicBoolean isLoaded = new AtomicBoolean(false);
123                     runTestOnUiThread(new Runnable() {
124                             @Override
125                         public void run() {
126                             if (activity != null) {
127                                 // There are two cases here that need to be
128                                 // accounted for.
129                                 // The first is that we've just created a Shell
130                                 // and it isn't
131                                 // loading because it has no URL set yet. The
132                                 // second is that
133                                 // we've set a URL and it actually is loading.
134                                 isLoaded.set(!activity.isLoading() && !TextUtils
135                                         .isEmpty(activity.getUrl()));
136                             } else {
137                                 isLoaded.set(false);
138                             }
139                         }
140                     });
141
142                     return isLoaded.get();
143                 } catch (Throwable e) {
144                     return false;
145                 }
146             }
147         }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
148                 CriteriaHelper.DEFAULT_POLLING_INTERVAL);
149     }
150 }