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; |
pauljensen
2015/12/29 16:35:53
I'm a little uncertain of the lifetimes of these c
Charles
2016/01/05 21:53:56
Done.
| |
101 if (!getClass().getPackage().getName().equals( | 109 String packageName = getClass().getPackage().getName(); |
102 "org.chromium.net.urlconnection")) { | 110 if (packageName.equals("org.chromium.net.urlconnection")) { |
111 try { | |
112 Method method = getClass().getMethod(getName(), (Class[]) null); | |
113 if (method.isAnnotationPresent(CompareDefaultWithCronet.class)) { | |
114 // Run with the default HttpURLConnection implementation fir st. | |
115 mTestingSystemHttpURLConnection = true; | |
116 super.runTest(); | |
117 // Use Cronet's implementation, and run the same test. | |
118 mTestingSystemHttpURLConnection = false; | |
119 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | |
120 super.runTest(); | |
121 } else if (method.isAnnotationPresent(OnlyRunCronetHttpURLConnec tion.class)) { | |
122 // Run only with Cronet's implementation. | |
123 URL.setURLStreamHandlerFactory(mCronetTestFramework.mStreamH andlerFactory); | |
124 super.runTest(); | |
125 } else { | |
126 // For all other tests. | |
127 super.runTest(); | |
128 } | |
129 } catch (Throwable e) { | |
130 throw new Throwable("CronetTestBase#runTest failed.", e); | |
131 } | |
132 } else if (packageName.equals("org.chromium.net")) { | |
133 try { | |
134 Method method = getClass().getMethod(getName(), (Class[]) null); | |
135 super.runTest(); | |
136 if (!method.isAnnotationPresent(OnlyRunNativeCronet.class)) { | |
137 if (mCronetTestFramework != null) { | |
138 mCronetTestFramework.mCronetEngine = | |
139 new JavaCronetEngine(UserAgent.from(getContext() )); | |
140 } | |
141 mTestingJavaImpl = true; | |
142 super.runTest(); | |
143 } | |
144 } catch (Throwable e) { | |
145 throw new Throwable("CronetTestBase#runTest failed.", e); | |
146 } | |
147 } else { | |
103 super.runTest(); | 148 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 } | 149 } |
128 } | 150 } |
129 | 151 |
130 /** | 152 /** |
131 * Registers test host resolver for testing with the new API. | 153 * Registers test host resolver for testing with the new API. |
132 */ | 154 */ |
133 protected void registerHostResolver(CronetTestFramework framework) { | 155 protected void registerHostResolver(CronetTestFramework framework) { |
134 registerHostResolver(framework, false); | 156 registerHostResolver(framework, false); |
135 } | 157 } |
136 | 158 |
(...skipping 18 matching lines...) Expand all Loading... | |
155 @Target(ElementType.METHOD) | 177 @Target(ElementType.METHOD) |
156 @Retention(RetentionPolicy.RUNTIME) | 178 @Retention(RetentionPolicy.RUNTIME) |
157 public @interface CompareDefaultWithCronet { | 179 public @interface CompareDefaultWithCronet { |
158 } | 180 } |
159 | 181 |
160 @Target(ElementType.METHOD) | 182 @Target(ElementType.METHOD) |
161 @Retention(RetentionPolicy.RUNTIME) | 183 @Retention(RetentionPolicy.RUNTIME) |
162 public @interface OnlyRunCronetHttpURLConnection { | 184 public @interface OnlyRunCronetHttpURLConnection { |
163 } | 185 } |
164 | 186 |
187 @Target(ElementType.METHOD) | |
188 @Retention(RetentionPolicy.RUNTIME) | |
189 public @interface OnlyRunNativeCronet {} | |
165 } | 190 } |
OLD | NEW |