Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/CronetTestBase.java

Issue 1503943003: [Cronet] Unit test refactoring and fixes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.test.AndroidTestCase; 7 import android.test.AndroidTestCase;
8 8
9 import org.chromium.base.PathUtils; 9 import org.chromium.base.PathUtils;
10 10
11 import java.lang.annotation.ElementType; 11 import java.lang.annotation.ElementType;
12 import java.lang.annotation.Retention; 12 import java.lang.annotation.Retention;
13 import java.lang.annotation.RetentionPolicy; 13 import java.lang.annotation.RetentionPolicy;
14 import java.lang.annotation.Target; 14 import java.lang.annotation.Target;
15 import java.lang.reflect.Method; 15 import java.lang.reflect.Method;
16 import java.net.URL; 16 import java.net.URL;
17 17
18 /** 18 /**
19 * Base test class for all CronetTest based tests. 19 * Base test class for all CronetTest based tests.
20 */ 20 */
21 public class CronetTestBase extends AndroidTestCase { 21 public class CronetTestBase extends AndroidTestCase {
22 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; 22 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test";
23 23
24 private CronetTestFramework mCronetTestFramework; 24 /**
25 * Package name that contains Cronet URLConnection tests.
26 */
27 private static final String URLCONNECTION_PACKAGE = "org.chromium.net.urlcon nection";
28
29 /**
30 * Indicates that the test should run with CronetHttpURLConnection. If the f lag is
31 * <code>true</code>, the next call to any <code>startCronetTestFramework... ()</code> method
32 * will replace the global URLStreamHandlerFactory factory with the Cronet o ne.
33 */
34 private boolean mTestingCronetHttpURLConnection = false;
35
36 /**
37 * <code>URL.setURLStreamHandlerFactory()</code> method can only be called o nce. This flag
xunjieli 2015/12/07 23:09:10 nit: maybe use {@code true} ? That's how it was us
kapishnikov 2015/12/08 01:09:37 Will fix it.
38 * indicates that the method has already been called and should not be calle d again. The flag
39 * is needed for tests that test CronetHttpURLConnection and start the Crone t Test Framework
40 * multiple times.
41 */
42 private boolean mURLStreamHandlerFactoryReplaced = false;
25 43
26 @Override 44 @Override
27 protected void setUp() throws Exception { 45 protected void setUp() throws Exception {
28 super.setUp(); 46 super.setUp();
29 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, g etContext()); 47 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, g etContext());
48 CronetTestFramework.prepareTestStorage(getContext());
49
50 // If the test method has the OnlyRunCronetHttpURLConnection annotation then
51 // set mTestingCronetHttpURLConnection to true, so the next startCronetT estFramework...()
52 // method will replace URLStreamHandlerFactory.
53 if (getClass().getPackage().getName().equals(URLCONNECTION_PACKAGE)) {
54 Method method = getClass().getMethod(getName(), (Class[]) null);
55 if (method.isAnnotationPresent(OnlyRunCronetHttpURLConnection.class) ) {
56 mTestingCronetHttpURLConnection = true;
57 }
58 }
30 } 59 }
31 60
32 /** 61 /**
33 * Starts the CronetTest framework. 62 * Starts the CronetTest framework.
34 */ 63 */
35 protected CronetTestFramework startCronetTestFramework() { 64 protected CronetTestFramework startCronetTestFramework() {
36 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, null) ; 65 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, null) ;
37 } 66 }
38 67
39 /** 68 /**
40 * Starts the CronetTest framework and loads the given URL. The URL can be 69 * Starts the CronetTest framework and loads the given URL. The URL can be
41 * null. 70 * null.
42 */ 71 */
43 protected CronetTestFramework startCronetTestFrameworkWithUrl(String url) { 72 protected CronetTestFramework startCronetTestFrameworkWithUrl(String url) {
44 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(url, null); 73 return startCronetTestFrameworkWithUrlAndCronetEngineBuilder(url, null);
45 } 74 }
46 75
47 /** 76 /**
48 * Starts the CronetTest framework using the provided CronetEngine.Builder 77 * Starts the CronetTest framework using the provided CronetEngine.Builder
49 * and loads the given URL. The URL can be null. 78 * and loads the given URL. The URL can be null.
50 */ 79 */
51 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCronetEngine Builder( 80 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCronetEngine Builder(
52 String url, CronetEngine.Builder builder) { 81 String url, CronetEngine.Builder builder) {
53 mCronetTestFramework = new CronetTestFramework(url, null, getContext(), builder); 82 CronetTestFramework framework = new CronetTestFramework(url, null, getCo ntext(), builder);
54 return mCronetTestFramework; 83 switchToCronetHttpURLConnectionTestingIfNeeded(framework);
84 return framework;
55 } 85 }
56 86
57 /** 87 /**
58 * Starts the CronetTest framework appending the provided command line 88 * Starts the CronetTest framework appending the provided command line
59 * arguments and loads the given URL. The URL can be null. 89 * arguments and loads the given URL. The URL can be null.
60 */ 90 */
61 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCommandLineA rgs( 91 protected CronetTestFramework startCronetTestFrameworkWithUrlAndCommandLineA rgs(
62 String url, String[] commandLineArgs) { 92 String url, String[] commandLineArgs) {
63 mCronetTestFramework = new CronetTestFramework(url, commandLineArgs, get Context(), null); 93 CronetTestFramework framework =
64 return mCronetTestFramework; 94 new CronetTestFramework(url, commandLineArgs, getContext(), null );
95 switchToCronetHttpURLConnectionTestingIfNeeded(framework);
96 return framework;
65 } 97 }
66 98
67 // Helper method to tell the framework to skip library init during construct ion. 99 // Helper method to tell the framework to skip library init during construct ion.
68 protected CronetTestFramework startCronetTestFrameworkAndSkipLibraryInit() { 100 protected CronetTestFramework startCronetTestFrameworkAndSkipLibraryInit() {
69 String[] commandLineArgs = { 101 String[] commandLineArgs = {
70 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.NONE}; 102 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.NONE};
71 mCronetTestFramework = 103 return startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLi neArgs);
72 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL ineArgs);
73 return mCronetTestFramework;
74 } 104 }
75 105
76 /** 106 /**
77 * Starts the CronetTest framework for the legacy API. 107 * Starts the CronetTest framework for the legacy API.
78 * @param url if non-null, a request will be made with that url. 108 * @param url if non-null, a request will be made with that url.
79 */ 109 */
80 protected CronetTestFramework startCronetTestFrameworkForLegacyApi(String ur l) { 110 protected CronetTestFramework startCronetTestFrameworkForLegacyApi(String ur l) {
81 String[] commandLineArgs = { 111 String[] commandLineArgs = {
82 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY}; 112 CronetTestFramework.LIBRARY_INIT_KEY, CronetTestFramework.Librar yInitType.LEGACY};
83 mCronetTestFramework = 113 return startCronetTestFrameworkWithUrlAndCommandLineArgs(url, commandLin eArgs);
84 startCronetTestFrameworkWithUrlAndCommandLineArgs(url, commandLi neArgs);
85 return mCronetTestFramework;
86 } 114 }
87 115
88 @Override 116 @Override
89 protected void runTest() throws Throwable { 117 protected void runTest() throws Throwable {
90 if (!getClass().getPackage().getName().equals( 118 if (!getClass().getPackage().getName().equals(URLCONNECTION_PACKAGE)) {
91 "org.chromium.net.urlconnection")) {
92 super.runTest(); 119 super.runTest();
93 return; 120 return;
94 } 121 }
95 try { 122 try {
96 Method method = getClass().getMethod(getName(), (Class[]) null); 123 Method method = getClass().getMethod(getName(), (Class[]) null);
97 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { 124 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) {
98 // Run with the default HttpURLConnection implementation first. 125 // Run with the default HttpURLConnection implementation first.
99 super.runTest(); 126 super.runTest();
100 // Use Cronet's implementation, and run the same test. 127 // Use Cronet's implementation, and run the same test.
101 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory); 128 this.tearDown();
102 super.runTest(); 129 mTestingCronetHttpURLConnection = true;
103 } else if (method.isAnnotationPresent( 130 this.setUp();
104 OnlyRunCronetHttpURLConnection.class)) {
105 // Run only with Cronet's implementation.
106 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory);
107 super.runTest(); 131 super.runTest();
108 } else { 132 } else {
xunjieli 2015/12/07 23:09:10 I'd prefer to keep CronetHttpURLConnection logic a
kapishnikov 2015/12/08 01:09:37 The problem with keeping all logic inside runTest(
xunjieli 2015/12/08 13:40:25 I don't really like this. The @CompareDefaultWithC
109 // For all other tests. 133 // For all other tests.
110 super.runTest(); 134 super.runTest();
111 } 135 }
112 } catch (Throwable e) { 136 } catch (Throwable e) {
113 throw new Throwable("CronetTestBase#runTest failed.", e); 137 throw new Throwable("CronetTestBase#runTest failed.", e);
114 } 138 }
115 } 139 }
116 140
141 /**
142 * Registers test host resolver for testing with the new API.
143 */
144 protected void registerHostResolver(CronetTestFramework framework) {
145 registerHostResolver(framework, false);
146 }
147
148 /**
149 * Registers test host resolver.
150 *
151 * @param isLegacyAPI true if the test should use the legacy API.
152 */
153 protected void registerHostResolver(CronetTestFramework framework, boolean i sLegacyAPI) {
154 long urlRequestContextAdapter;
155 if (isLegacyAPI) {
156 urlRequestContextAdapter = ((ChromiumUrlRequestFactory) framework.mR equestFactory)
157 .getRequestContext()
158 .getUrlRequestContextAdapter();
159 } else {
160 urlRequestContextAdapter = ((CronetUrlRequestContext) framework.mCro netEngine)
161 .getUrlRequestContextAdapter();
162 }
163 NativeTestServer.registerHostResolverProc(urlRequestContextAdapter, isLe gacyAPI);
164 }
165
166 /**
167 * Replaces global URLStreamHandlerFactory with the Cronet one if it is requ ired by the
168 * running test.
169 *
170 * @param framework the Cronet Test Framework to use for StreamHandlerFactor y retreival.
171 */
172 private void switchToCronetHttpURLConnectionTestingIfNeeded(CronetTestFramew ork framework) {
173 if (mTestingCronetHttpURLConnection && !mURLStreamHandlerFactoryReplaced ) {
174 URL.setURLStreamHandlerFactory(framework.mStreamHandlerFactory);
175 mURLStreamHandlerFactoryReplaced = true;
176 }
177 }
178
117 @Target(ElementType.METHOD) 179 @Target(ElementType.METHOD)
118 @Retention(RetentionPolicy.RUNTIME) 180 @Retention(RetentionPolicy.RUNTIME)
119 public @interface CompareDefaultWithCronet { 181 public @interface CompareDefaultWithCronet {
120 } 182 }
121 183
122 @Target(ElementType.METHOD) 184 @Target(ElementType.METHOD)
123 @Retention(RetentionPolicy.RUNTIME) 185 @Retention(RetentionPolicy.RUNTIME)
124 public @interface OnlyRunCronetHttpURLConnection { 186 public @interface OnlyRunCronetHttpURLConnection {
125 } 187 }
126 188
127 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698