OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.content.ComponentName; | 7 import android.test.AndroidTestCase; |
8 import android.content.Intent; | |
9 import android.net.Uri; | |
10 import android.test.ActivityInstrumentationTestCase2; | |
11 | |
12 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout; | |
13 | 8 |
14 import java.lang.annotation.ElementType; | 9 import java.lang.annotation.ElementType; |
15 import java.lang.annotation.Retention; | 10 import java.lang.annotation.Retention; |
16 import java.lang.annotation.RetentionPolicy; | 11 import java.lang.annotation.RetentionPolicy; |
17 import java.lang.annotation.Target; | 12 import java.lang.annotation.Target; |
18 import java.lang.reflect.Method; | 13 import java.lang.reflect.Method; |
19 import java.net.URL; | 14 import java.net.URL; |
20 import java.util.concurrent.atomic.AtomicBoolean; | |
21 | 15 |
22 /** | 16 /** |
23 * Base test class for all CronetTest based tests. | 17 * Base test class for all CronetTest based tests. |
24 */ | 18 */ |
25 public class CronetTestBase extends | 19 public class CronetTestBase extends AndroidTestCase { |
26 ActivityInstrumentationTestCase2<CronetTestActivity> { | 20 private CronetTestFramework mCronetTestFramework; |
27 /** | |
28 * The maximum time the waitForActiveShellToBeDoneLoading method will wait. | |
29 */ | |
30 private static final long | |
31 WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT = scaleTimeout(10000); | |
32 | |
33 protected static final long | |
34 WAIT_PAGE_LOADING_TIMEOUT_SECONDS = scaleTimeout(15); | |
35 | |
36 public CronetTestBase() { | |
37 super(CronetTestActivity.class); | |
38 } | |
39 | 21 |
40 /** | 22 /** |
41 * Starts the CronetTest activity. | 23 * Starts the CronetTest activity. |
xunjieli
2015/10/22 18:41:24
nit: outdated comment containing the word "activit
pauljensen
2015/10/23 11:35:50
Done.
| |
42 */ | 24 */ |
43 protected CronetTestActivity launchCronetTestApp() { | 25 protected CronetTestFramework startCronetTestFramework() { |
44 return launchCronetTestAppWithUrlAndCommandLineArgs(null, null); | 26 return startCronetTestFrameworkWithUrlAndCommandLineArgs(null, null); |
45 } | 27 } |
46 | 28 |
47 /** | 29 /** |
48 * Starts the CronetTest activity and loads the given URL. The URL can be | 30 * Starts the CronetTest activity and loads the given URL. The URL can be |
49 * null. | 31 * null. |
50 */ | 32 */ |
51 protected CronetTestActivity launchCronetTestAppWithUrl(String url) { | 33 protected CronetTestFramework startCronetTestFrameworkWithUrl(String url) { |
52 return launchCronetTestAppWithUrlAndCommandLineArgs(url, null); | 34 return startCronetTestFrameworkWithUrlAndCommandLineArgs(url, null); |
53 } | 35 } |
54 | 36 |
55 /** | 37 /** |
56 * Starts the CronetTest activity appending the provided command line | 38 * Starts the CronetTest activity appending the provided command line |
xunjieli
2015/10/22 18:41:24
nit: outdated comment containing the word "activit
pauljensen
2015/10/23 11:35:50
Done.
| |
57 * arguments and loads the given URL. The URL can be null. | 39 * arguments and loads the given URL. The URL can be null. |
58 */ | 40 */ |
59 protected CronetTestActivity launchCronetTestAppWithUrlAndCommandLineArgs( | 41 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCommandLineA rgs( |
60 String url, String[] commandLineArgs) { | 42 String url, String[] commandLineArgs) { |
61 Intent intent = new Intent(Intent.ACTION_MAIN); | 43 mCronetTestFramework = new CronetTestFramework(url, commandLineArgs, get Context()); |
62 intent.addCategory(Intent.CATEGORY_LAUNCHER); | 44 return mCronetTestFramework; |
63 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
64 if (url != null) intent.setData(Uri.parse(url)); | |
65 intent.setComponent(new ComponentName( | |
66 getInstrumentation().getTargetContext(), | |
67 CronetTestActivity.class)); | |
68 if (commandLineArgs != null) { | |
69 intent.putExtra(CronetTestActivity.COMMAND_LINE_ARGS_KEY, | |
70 commandLineArgs); | |
71 } | |
72 setActivityIntent(intent); | |
73 // Make sure the activity was created as expected. | |
74 assertNotNull(getActivity()); | |
75 try { | |
76 waitForActivityToBeDoneLoading(); | |
77 } catch (Throwable e) { | |
78 fail("Test activity has failed to load."); | |
79 } | |
80 return getActivity(); | |
81 } | 45 } |
82 | 46 |
83 // Helper method to tell the activity to skip factory init in onCreate(). | 47 // Helper method to tell the activity to skip factory init in onCreate(). |
xunjieli
2015/10/22 18:41:24
nit: outdated comment containing the word "activit
pauljensen
2015/10/23 11:35:50
Done.
| |
84 protected CronetTestActivity launchCronetTestAppAndSkipFactoryInit() { | 48 protected CronetTestFramework startCronetTestFrameworkAndSkipFactoryInit() { |
85 String[] commandLineArgs = { | 49 String[] commandLineArgs = { |
86 CronetTestActivity.LIBRARY_INIT_KEY, CronetTestActivity.LIBRARY_ INIT_SKIP}; | 50 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.LIBRAR Y_INIT_SKIP}; |
87 CronetTestActivity activity = | 51 mCronetTestFramework = |
88 launchCronetTestAppWithUrlAndCommandLineArgs(null, commandLineAr gs); | 52 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL ineArgs); |
89 return activity; | 53 return mCronetTestFramework; |
90 } | |
91 | |
92 /** | |
93 * Waits for the Activity to finish loading. This times out after | |
94 * WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT milliseconds. | |
95 * | |
96 * @return Whether or not the Shell was actually finished loading. | |
97 * @throws InterruptedException | |
98 */ | |
99 private boolean waitForActivityToBeDoneLoading() | |
100 throws InterruptedException { | |
101 final CronetTestActivity activity = getActivity(); | |
102 | |
103 // Wait for the Activity to load. | |
104 return CriteriaHelper.pollForCriteria(new Criteria() { | |
105 @Override | |
106 public boolean isSatisfied() { | |
107 try { | |
108 final AtomicBoolean isLoaded = new AtomicBoolean(false); | |
109 runTestOnUiThread(new Runnable() { | |
110 @Override | |
111 public void run() { | |
112 isLoaded.set(activity != null && !activity.isLoading ()); | |
113 } | |
114 }); | |
115 | |
116 return isLoaded.get(); | |
117 } catch (Throwable e) { | |
118 return false; | |
119 } | |
120 } | |
121 }, WAIT_FOR_ACTIVE_SHELL_LOADING_TIMEOUT, | |
122 CriteriaHelper.DEFAULT_POLLING_INTERVAL); | |
123 } | 54 } |
124 | 55 |
125 @Override | 56 @Override |
126 protected void runTest() throws Throwable { | 57 protected void runTest() throws Throwable { |
127 if (!getClass().getPackage().getName().equals( | 58 if (!getClass().getPackage().getName().equals( |
128 "org.chromium.net.urlconnection")) { | 59 "org.chromium.net.urlconnection")) { |
129 super.runTest(); | 60 super.runTest(); |
130 return; | 61 return; |
131 } | 62 } |
132 try { | 63 try { |
133 Method method = getClass().getMethod(getName(), (Class[]) null); | 64 Method method = getClass().getMethod(getName(), (Class[]) null); |
134 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { | 65 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { |
135 // Run with the default HttpURLConnection implementation first. | 66 // Run with the default HttpURLConnection implementation first. |
136 super.runTest(); | 67 super.runTest(); |
137 // Use Cronet's implementation, and run the same test. | 68 // Use Cronet's implementation, and run the same test. |
138 URL.setURLStreamHandlerFactory( | 69 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory); |
139 getActivity().mStreamHandlerFactory); | |
140 super.runTest(); | 70 super.runTest(); |
141 } else if (method.isAnnotationPresent( | 71 } else if (method.isAnnotationPresent( |
142 OnlyRunCronetHttpURLConnection.class)) { | 72 OnlyRunCronetHttpURLConnection.class)) { |
143 // Run only with Cronet's implementation. | 73 // Run only with Cronet's implementation. |
144 URL.setURLStreamHandlerFactory( | 74 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory); |
145 getActivity().mStreamHandlerFactory); | |
146 super.runTest(); | 75 super.runTest(); |
147 } else { | 76 } else { |
148 // For all other tests. | 77 // For all other tests. |
149 super.runTest(); | 78 super.runTest(); |
150 } | 79 } |
151 } catch (Throwable e) { | 80 } catch (Throwable e) { |
152 throw new Throwable("CronetTestBase#runTest failed.", e); | 81 throw new Throwable("CronetTestBase#runTest failed.", e); |
153 } | 82 } |
154 } | 83 } |
155 | 84 |
156 @Target(ElementType.METHOD) | 85 @Target(ElementType.METHOD) |
157 @Retention(RetentionPolicy.RUNTIME) | 86 @Retention(RetentionPolicy.RUNTIME) |
158 public @interface CompareDefaultWithCronet { | 87 public @interface CompareDefaultWithCronet { |
159 } | 88 } |
160 | 89 |
161 @Target(ElementType.METHOD) | 90 @Target(ElementType.METHOD) |
162 @Retention(RetentionPolicy.RUNTIME) | 91 @Retention(RetentionPolicy.RUNTIME) |
163 public @interface OnlyRunCronetHttpURLConnection { | 92 public @interface OnlyRunCronetHttpURLConnection { |
164 } | 93 } |
165 | 94 |
166 } | 95 } |
OLD | NEW |