Update To 11.40.268.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         // Make sure the activity was created as expected.
70         assertNotNull(getActivity());
71         try {
72             waitForActiveShellToBeDoneLoading();
73         } catch (Throwable e) {
74             fail("Test activity has failed to load.");
75         }
76         return getActivity();
77     }
78
79     /**
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.
85      *
86      * @return Whether or not the Shell was actually finished loading.
87      * @throws InterruptedException
88      */
89     protected boolean waitForActiveShellToBeDoneLoading()
90             throws InterruptedException {
91         final CronetTestActivity activity = getActivity();
92
93         // Wait for the Content Shell to be initialized.
94         return CriteriaHelper.pollForCriteria(new Criteria() {
95                 @Override
96             public boolean isSatisfied() {
97                 try {
98                     final AtomicBoolean isLoaded = new AtomicBoolean(false);
99                     runTestOnUiThread(new Runnable() {
100                             @Override
101                         public void run() {
102                             if (activity != null) {
103                                 // There are two cases here that need to be
104                                 // accounted for.
105                                 // The first is that we've just created a Shell
106                                 // and it isn't
107                                 // loading because it has no URL set yet. The
108                                 // second is that
109                                 // we've set a URL and it actually is loading.
110                                 isLoaded.set(!activity.isLoading() && !TextUtils
111                                         .isEmpty(activity.getUrl()));
112                             } else {
113                                 isLoaded.set(false);
114                             }
115                         }
116                     });
117
118                     return isLoaded.get();
119                 } catch (Throwable e) {
120                     return false;
121                 }
122             }
123         }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT,
124                 CriteriaHelper.DEFAULT_POLLING_INTERVAL);
125     }
126 }