Chromium Code Reviews| 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.ContextUtils; | 9 import org.chromium.base.ContextUtils; |
| 10 import org.chromium.base.PathUtils; | 10 import org.chromium.base.PathUtils; |
| 11 import org.chromium.net.impl.CronetEngineBase; | 11 import org.chromium.net.impl.CronetEngineBase; |
| 12 import org.chromium.net.impl.JavaCronetEngine; | 12 import org.chromium.net.impl.JavaCronetEngine; |
| 13 import org.chromium.net.impl.JavaCronetProvider; | |
| 13 import org.chromium.net.impl.UserAgent; | 14 import org.chromium.net.impl.UserAgent; |
| 14 | 15 |
| 15 import java.lang.annotation.ElementType; | 16 import java.lang.annotation.ElementType; |
| 16 import java.lang.annotation.Retention; | 17 import java.lang.annotation.Retention; |
| 17 import java.lang.annotation.RetentionPolicy; | 18 import java.lang.annotation.RetentionPolicy; |
| 18 import java.lang.annotation.Target; | 19 import java.lang.annotation.Target; |
| 19 import java.lang.reflect.AnnotatedElement; | 20 import java.lang.reflect.AnnotatedElement; |
| 20 import java.net.URL; | 21 import java.net.URL; |
| 22 import java.util.List; | |
| 21 | 23 |
| 22 /** | 24 /** |
| 23 * Base test class for all CronetTest based tests. | 25 * Base test class for all CronetTest based tests. |
| 24 */ | 26 */ |
| 25 public class CronetTestBase extends AndroidTestCase { | 27 public class CronetTestBase extends AndroidTestCase { |
| 26 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; | 28 private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "cronet_test"; |
| 27 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; | 29 private static final String LOOPBACK_ADDRESS = "127.0.0.1"; |
| 28 | 30 |
| 29 private CronetTestFramework mCronetTestFramework; | 31 private CronetTestFramework mCronetTestFramework; |
| 30 // {@code true} when test is being run against system HttpURLConnection impl ementation. | 32 // {@code true} when test is being run against system HttpURLConnection impl ementation. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 } | 127 } |
| 126 } catch (Throwable e) { | 128 } catch (Throwable e) { |
| 127 throw new Throwable("CronetTestBase#runTest failed.", e); | 129 throw new Throwable("CronetTestBase#runTest failed.", e); |
| 128 } | 130 } |
| 129 } else if (packageName.equals("org.chromium.net")) { | 131 } else if (packageName.equals("org.chromium.net")) { |
| 130 try { | 132 try { |
| 131 AnnotatedElement method = getClass().getMethod(getName(), (Class []) null); | 133 AnnotatedElement method = getClass().getMethod(getName(), (Class []) null); |
| 132 super.runTest(); | 134 super.runTest(); |
| 133 if (!method.isAnnotationPresent(OnlyRunNativeCronet.class)) { | 135 if (!method.isAnnotationPresent(OnlyRunNativeCronet.class)) { |
| 134 if (mCronetTestFramework != null) { | 136 if (mCronetTestFramework != null) { |
| 135 ExperimentalCronetEngine.Builder builder = | 137 ExperimentalCronetEngine.Builder builder = createJavaEng ineBuilder(); |
| 136 new ExperimentalCronetEngine.Builder(getContext( )); | |
| 137 builder.setUserAgent(UserAgent.from(getContext())); | 138 builder.setUserAgent(UserAgent.from(getContext())); |
| 138 builder.enableLegacyMode(true); | |
| 139 mCronetTestFramework.mCronetEngine = (CronetEngineBase) builder.build(); | 139 mCronetTestFramework.mCronetEngine = (CronetEngineBase) builder.build(); |
| 140 // Make sure that the instantiated engine is JavaCronetE ngine. | 140 // Make sure that the instantiated engine is JavaCronetE ngine. |
| 141 assert mCronetTestFramework.mCronetEngine.getClass() | 141 assert mCronetTestFramework.mCronetEngine.getClass() |
| 142 == JavaCronetEngine.class; | 142 == JavaCronetEngine.class; |
| 143 } | 143 } |
| 144 mTestingJavaImpl = true; | 144 mTestingJavaImpl = true; |
| 145 super.runTest(); | 145 super.runTest(); |
| 146 } | 146 } |
| 147 } catch (Throwable e) { | 147 } catch (Throwable e) { |
| 148 throw new Throwable("CronetTestBase#runTest failed.", e); | 148 throw new Throwable("CronetTestBase#runTest failed.", e); |
| 149 } | 149 } |
| 150 } else { | 150 } else { |
| 151 super.runTest(); | 151 super.runTest(); |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 /** | |
| 156 * Creates and returns {@link ExperimentalCronetEngine.Builder} that creates | |
| 157 * Java (platform) based {@link CronetEngine.Builder}. | |
| 158 * | |
| 159 * @return the {@code CronetEngine.Builder} that builds Java-based {@code Cr onet engine}. | |
| 160 */ | |
| 161 ExperimentalCronetEngine.Builder createJavaEngineBuilder() { | |
| 162 CronetProvider provider = findJavaCronetProvider(); | |
| 163 return (ExperimentalCronetEngine.Builder) provider.createBuilder(); | |
|
pauljensen
2017/01/18 17:03:05
why not just make this "return new JavaCronetProvi
kapishnikov
2017/01/19 01:25:25
Some tests rely on features that are available in
pauljensen
2017/01/20 17:13:34
How about:
(ExperimentalCronetEngine.Builder) new
kapishnikov
2017/01/20 21:48:38
That is a great suggestion. I tried to avoid refer
| |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Finds {@link CronetProvider} that can be used to create Java-based {@link CronetEngine}. | |
| 168 * | |
| 169 * @return the found provider. | |
| 170 */ | |
| 171 private CronetProvider findJavaCronetProvider() { | |
| 172 List<CronetProvider> availableProviders = | |
| 173 CronetProviders.instance().getAvailableProviders(getContext()); | |
| 174 for (CronetProvider provider : availableProviders) { | |
| 175 if (provider.getName().equals(JavaCronetProvider.PROVIDER_NAME)) { | |
| 176 return provider; | |
| 177 } | |
| 178 } | |
| 179 throw new AssertionError("Unable to find Java Provider"); | |
| 180 } | |
| 181 | |
| 155 public void assertResponseEquals(UrlResponseInfo expected, UrlResponseInfo a ctual) { | 182 public void assertResponseEquals(UrlResponseInfo expected, UrlResponseInfo a ctual) { |
| 156 assertEquals(expected.getAllHeaders(), actual.getAllHeaders()); | 183 assertEquals(expected.getAllHeaders(), actual.getAllHeaders()); |
| 157 assertEquals(expected.getAllHeadersAsList(), actual.getAllHeadersAsList( )); | 184 assertEquals(expected.getAllHeadersAsList(), actual.getAllHeadersAsList( )); |
| 158 assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode()); | 185 assertEquals(expected.getHttpStatusCode(), actual.getHttpStatusCode()); |
| 159 assertEquals(expected.getHttpStatusText(), actual.getHttpStatusText()); | 186 assertEquals(expected.getHttpStatusText(), actual.getHttpStatusText()); |
| 160 assertEquals(expected.getUrlChain(), actual.getUrlChain()); | 187 assertEquals(expected.getUrlChain(), actual.getUrlChain()); |
| 161 assertEquals(expected.getUrl(), actual.getUrl()); | 188 assertEquals(expected.getUrl(), actual.getUrl()); |
| 162 // Transferred bytes and proxy server are not supported in pure java | 189 // Transferred bytes and proxy server are not supported in pure java |
| 163 if (!(mCronetTestFramework.mCronetEngine instanceof JavaCronetEngine)) { | 190 if (!(mCronetTestFramework.mCronetEngine instanceof JavaCronetEngine)) { |
| 164 assertEquals(expected.getReceivedByteCount(), actual.getReceivedByte Count()); | 191 assertEquals(expected.getReceivedByteCount(), actual.getReceivedByte Count()); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 183 | 210 |
| 184 @Target(ElementType.METHOD) | 211 @Target(ElementType.METHOD) |
| 185 @Retention(RetentionPolicy.RUNTIME) | 212 @Retention(RetentionPolicy.RUNTIME) |
| 186 public @interface OnlyRunCronetHttpURLConnection { | 213 public @interface OnlyRunCronetHttpURLConnection { |
| 187 } | 214 } |
| 188 | 215 |
| 189 @Target(ElementType.METHOD) | 216 @Target(ElementType.METHOD) |
| 190 @Retention(RetentionPolicy.RUNTIME) | 217 @Retention(RetentionPolicy.RUNTIME) |
| 191 public @interface OnlyRunNativeCronet {} | 218 public @interface OnlyRunNativeCronet {} |
| 192 } | 219 } |
| OLD | NEW |