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.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 private CronetTestFramework mCronetTestFramework; |
25 // {@code true} when test is being run against system HttpURLConnection impl ementation. | 25 // {@code true} when test is being run against system HttpURLConnection impl ementation. |
26 private boolean mTestingSystemHttpURLConnection; | 26 private boolean mTestingSystemHttpURLConnection; |
27 private boolean mTestingJavaImpl = false; | |
27 | 28 |
28 @Override | 29 @Override |
29 protected void setUp() throws Exception { | 30 protected void setUp() throws Exception { |
30 super.setUp(); | 31 super.setUp(); |
31 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, g etContext()); | 32 PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX, g etContext()); |
32 CronetTestFramework.prepareTestStorage(getContext()); | 33 CronetTestFramework.prepareTestStorage(getContext()); |
33 } | 34 } |
34 | 35 |
35 /** | 36 /** |
36 * Starts the CronetTest framework. | 37 * Starts the CronetTest framework. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 return mCronetTestFramework; | 89 return mCronetTestFramework; |
89 } | 90 } |
90 | 91 |
91 /** | 92 /** |
92 * Returns {@code true} when test is being run against system HttpURLConnect ion implementation. | 93 * Returns {@code true} when test is being run against system HttpURLConnect ion implementation. |
93 */ | 94 */ |
94 protected boolean testingSystemHttpURLConnection() { | 95 protected boolean testingSystemHttpURLConnection() { |
95 return mTestingSystemHttpURLConnection; | 96 return mTestingSystemHttpURLConnection; |
96 } | 97 } |
97 | 98 |
99 /** | |
100 * Returns {@code true} when test is being run against the java implementati on of CronetEngine. | |
101 */ | |
102 protected boolean testingJavaImpl() { | |
103 return mTestingJavaImpl; | |
104 } | |
105 | |
98 @Override | 106 @Override |
99 protected void runTest() throws Throwable { | 107 protected void runTest() throws Throwable { |
100 mTestingSystemHttpURLConnection = false; | 108 mTestingSystemHttpURLConnection = false; |
101 if (!getClass().getPackage().getName().equals( | 109 mTestingJavaImpl = false; |
102 "org.chromium.net.urlconnection")) { | 110 String packageName = getClass().getPackage().getName(); |
111 if (packageName.equals("org.chromium.net.urlconnection")) { | |
112 try { | |
113 Method method = getClass().getMethod(getName(), (Class[]) null); | |
114 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { | |
115 // Run with the default HttpURLConnection implementation fir st. | |
116 mTestingSystemHttpURLConnection = true; | |
117 super.runTest(); | |
118 // Use Cronet's implementation, and run the same test. | |
119 mTestingSystemHttpURLConnection = false; | |
120 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | |
121 super.runTest(); | |
122 } else if (method.isAnnotationPresent(OnlyRunCronetHttpURLConnec tion.class)) { | |
123 // Run only with Cronet's implementation. | |
124 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | |
125 super.runTest(); | |
126 } else { | |
127 // For all other tests. | |
128 super.runTest(); | |
129 } | |
130 } catch (Throwable e) { | |
131 throw new Throwable("CronetTestBase#runTest failed.", e); | |
132 } | |
133 } else if (packageName.equals("org.chromium.net")) { | |
134 try { | |
135 Method method = getClass().getMethod(getName(), (Class[]) null); | |
136 super.runTest(); | |
137 if (!method.isAnnotationPresent(OnlyRunNativeCronet.class)) { | |
138 if (mCronetTestFramework != null) { | |
139 mCronetTestFramework.mCronetEngine = | |
140 new JavaCronetEngine(UserAgent.from(getContext() )); | |
141 } | |
142 mTestingJavaImpl = true; | |
143 super.runTest(); | |
144 } | |
145 } catch (Throwable e) { | |
146 throw new Throwable("CronetTestBase#runTest failed.", e); | |
147 } | |
148 } else { | |
103 super.runTest(); | 149 super.runTest(); |
104 return; | |
105 } | |
106 try { | |
107 Method method = getClass().getMethod(getName(), (Class[]) null); | |
108 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { | |
109 // Run with the default HttpURLConnection implementation first. | |
110 mTestingSystemHttpURLConnection = true; | |
111 super.runTest(); | |
112 // Use Cronet's implementation, and run the same test. | |
113 mTestingSystemHttpURLConnection = false; | |
114 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory); | |
115 super.runTest(); | |
116 } else if (method.isAnnotationPresent( | |
117 OnlyRunCronetHttpURLConnection.class)) { | |
118 // Run only with Cronet's implementation. | |
119 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamHandl erFactory); | |
120 super.runTest(); | |
121 } else { | |
122 // For all other tests. | |
123 super.runTest(); | |
124 } | |
125 } catch (Throwable e) { | |
126 throw new Throwable("CronetTestBase#runTest failed.", e); | |
127 } | 150 } |
128 } | 151 } |
129 | 152 |
130 /** | 153 /** |
131 * Registers test host resolver for testing with the new API. | 154 * Registers test host resolver for testing with the new API. |
132 */ | 155 */ |
133 protected void registerHostResolver(CronetTestFramework framework) { | 156 protected void registerHostResolver(CronetTestFramework framework) { |
134 registerHostResolver(framework, false); | 157 registerHostResolver(framework, false); |
135 } | 158 } |
136 | 159 |
(...skipping 18 matching lines...) Expand all Loading... | |
155 @Target(ElementType.METHOD) | 178 @Target(ElementType.METHOD) |
156 @Retention(RetentionPolicy.RUNTIME) | 179 @Retention(RetentionPolicy.RUNTIME) |
157 public @interface CompareDefaultWithCronet { | 180 public @interface CompareDefaultWithCronet { |
158 } | 181 } |
159 | 182 |
160 @Target(ElementType.METHOD) | 183 @Target(ElementType.METHOD) |
161 @Retention(RetentionPolicy.RUNTIME) | 184 @Retention(RetentionPolicy.RUNTIME) |
162 public @interface OnlyRunCronetHttpURLConnection { | 185 public @interface OnlyRunCronetHttpURLConnection { |
163 } | 186 } |
164 | 187 |
188 @Target(ElementType.METHOD) | |
189 @Retention(RetentionPolicy.RUNTIME) | |
190 public @interface OnlyRunNativeCronet {} | |
pauljensen
2016/01/07 04:33:38
can you make these braces match those on lines 180
Charles
2016/01/07 16:10:02
Done.
| |
165 } | 191 } |
OLD | NEW |