Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / test / javatests / src / org / chromium / cronet_test_apk / CronetTestBase.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_test_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 java.util.concurrent.atomic.AtomicBoolean;
16
17 /**
18  * Base test class for all CronetTest based tests.
19  */
20 public class CronetTestBase extends
21         ActivityInstrumentationTestCase2<CronetTestActivity> {
22     /**
23      * The maximum time the waitForActiveShellToBeDoneLoading method will wait.
24      */
25     private static final long
26             WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000);
27
28     protected static final long
29             WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15);
30
31     public CronetTestBase() {
32         super(CronetTestActivity.class);
33     }
34
35     /**
36      * Starts the CronetTest activity.
37      */
38     protected CronetTestActivity launchCronetTestApp() {
39         return launchCronetTestAppWithUrlAndCommandLineArgs(null, null);
40     }
41
42     /**
43      * Starts the CronetTest activity and loads the given URL. The URL can be
44      * null.
45      */
46     protected CronetTestActivity launchCronetTestAppWithUrl(String url) {
47         return launchCronetTestAppWithUrlAndCommandLineArgs(url, null);
48     }
49
50     /**
51      * Starts the CronetTest activity appending the provided command line
52      * arguments and loads the given URL. The URL can be null.
53      */
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);
59         if (url != null)
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,
66                     commandLineArgs);
67         }
68         setActivityIntent(intent);
69         return getActivity();
70     }
71
72     /**
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.
78      *
79      * @return Whether or not the Shell was actually finished loading.
80      * @throws InterruptedException
81      */
82     protected boolean waitForActiveShellToBeDoneLoading()
83             throws InterruptedException {
84         final CronetTestActivity activity = getActivity();
85
86         // Wait for the Content Shell to be initialized.
87         return CriteriaHelper.pollForCriteria(new Criteria() {
88                 @Override
89             public boolean isSatisfied() {
90                 try {
91                     final AtomicBoolean isLoaded = new AtomicBoolean(false);
92                     runTestOnUiThread(new Runnable() {
93                             @Override
94                         public void run() {
95                             if (activity != null) {
96                                 // There are two cases here that need to be
97                                 // accounted for.
98                                 // The first is that we've just created a Shell
99                                 // and it isn't
100                                 // loading because it has no URL set yet. The
101                                 // second is that
102                                 // we've set a URL and it actually is loading.
103                                 isLoaded.set(!activity.isLoading() && !TextUtils
104                                         .isEmpty(activity.getUrl()));
105                             } else {
106                                 isLoaded.set(false);
107                             }
108                         }
109                     });
110
111                     return isLoaded.get();
112                 } catch (Throwable e) {
113                     return false;
114                 }
115             }
116         }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
117                 CriteriaHelper.DEFAULT_POLLING_INTERVAL);
118     }
119 }