| 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.Context; | 7 import android.content.Context; |
| 8 import android.content.ContextWrapper; | 8 import android.content.ContextWrapper; |
| 9 import android.os.ConditionVariable; | 9 import android.os.ConditionVariable; |
| 10 import android.os.Handler; | 10 import android.os.Handler; |
| 11 import android.os.Looper; | 11 import android.os.Looper; |
| 12 import android.os.StrictMode; | 12 import android.os.StrictMode; |
| 13 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
| 14 | 14 |
| 15 import org.json.JSONObject; | 15 import org.json.JSONObject; |
| 16 | 16 |
| 17 import static org.chromium.net.CronetEngine.Builder.HTTP_CACHE_IN_MEMORY; |
| 18 |
| 17 import org.chromium.base.FileUtils; | 19 import org.chromium.base.FileUtils; |
| 18 import org.chromium.base.PathUtils; | 20 import org.chromium.base.PathUtils; |
| 19 import org.chromium.base.annotations.JNINamespace; | 21 import org.chromium.base.annotations.JNINamespace; |
| 20 import org.chromium.base.test.util.Feature; | 22 import org.chromium.base.test.util.Feature; |
| 21 import org.chromium.net.MetricsTestUtil.TestExecutor; | 23 import org.chromium.net.MetricsTestUtil.TestExecutor; |
| 22 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 24 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
| 25 import org.chromium.net.impl.CronetEngineBase; |
| 26 import org.chromium.net.impl.CronetEngineBuilderImpl; |
| 23 import org.chromium.net.impl.CronetLibraryLoader; | 27 import org.chromium.net.impl.CronetLibraryLoader; |
| 24 import org.chromium.net.impl.CronetUrlRequestContext; | 28 import org.chromium.net.impl.CronetUrlRequestContext; |
| 25 import org.chromium.net.test.EmbeddedTestServer; | 29 import org.chromium.net.test.EmbeddedTestServer; |
| 26 | 30 |
| 27 import java.io.BufferedReader; | 31 import java.io.BufferedReader; |
| 28 import java.io.File; | 32 import java.io.File; |
| 29 import java.io.FileReader; | 33 import java.io.FileReader; |
| 30 import java.net.URL; | 34 import java.net.URL; |
| 31 import java.util.Arrays; | 35 import java.util.Arrays; |
| 32 import java.util.concurrent.Executor; | 36 import java.util.concurrent.Executor; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 mUrl = url; | 87 mUrl = url; |
| 84 mRunBlocker = runBlocker; | 88 mRunBlocker = runBlocker; |
| 85 } | 89 } |
| 86 | 90 |
| 87 @Override | 91 @Override |
| 88 public void run() { | 92 public void run() { |
| 89 mRunBlocker.block(); | 93 mRunBlocker.block(); |
| 90 CronetEngine cronetEngine = mTestFramework.initCronetEngine(); | 94 CronetEngine cronetEngine = mTestFramework.initCronetEngine(); |
| 91 mCallback = new TestUrlRequestCallback(); | 95 mCallback = new TestUrlRequestCallback(); |
| 92 UrlRequest.Builder urlRequestBuilder = | 96 UrlRequest.Builder urlRequestBuilder = |
| 93 new UrlRequest.Builder(mUrl, mCallback, mCallback.getExecuto
r(), cronetEngine); | 97 cronetEngine.newUrlRequestBuilder(mUrl, mCallback, mCallback
.getExecutor()); |
| 94 urlRequestBuilder.build().start(); | 98 urlRequestBuilder.build().start(); |
| 95 mCallback.blockForDone(); | 99 mCallback.blockForDone(); |
| 96 } | 100 } |
| 97 } | 101 } |
| 98 | 102 |
| 99 /** | 103 /** |
| 100 * Callback that shutdowns the request context when request has succeeded | 104 * Callback that shutdowns the request context when request has succeeded |
| 101 * or failed. | 105 * or failed. |
| 102 */ | 106 */ |
| 103 static class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { | 107 static class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 return mNetworkQualityThread; | 153 return mNetworkQualityThread; |
| 150 } | 154 } |
| 151 } | 155 } |
| 152 | 156 |
| 153 @SmallTest | 157 @SmallTest |
| 154 @Feature({"Cronet"}) | 158 @Feature({"Cronet"}) |
| 155 @SuppressWarnings("deprecation") | 159 @SuppressWarnings("deprecation") |
| 156 public void testConfigUserAgent() throws Exception { | 160 public void testConfigUserAgent() throws Exception { |
| 157 String userAgentName = "User-Agent"; | 161 String userAgentName = "User-Agent"; |
| 158 String userAgentValue = "User-Agent-Value"; | 162 String userAgentValue = "User-Agent-Value"; |
| 159 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC
ontext()); | 163 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 164 new ExperimentalCronetEngine.Builder(getContext()); |
| 160 if (testingJavaImpl()) { | 165 if (testingJavaImpl()) { |
| 161 cronetEngineBuilder.enableLegacyMode(true); | 166 cronetEngineBuilder.enableLegacyMode(true); |
| 162 } | 167 } |
| 163 cronetEngineBuilder.setUserAgent(userAgentValue); | 168 cronetEngineBuilder.setUserAgent(userAgentValue); |
| 164 cronetEngineBuilder.setLibraryName("cronet_tests"); | 169 CronetTestUtil.setLibraryName(cronetEngineBuilder, "cronet_tests"); |
| 165 final CronetTestFramework testFramework = | 170 final CronetTestFramework testFramework = |
| 166 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(mUrl, cron
etEngineBuilder); | 171 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(mUrl, cron
etEngineBuilder); |
| 167 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re
turns false if it's | 172 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re
turns false if it's |
| 168 // already running | 173 // already running |
| 169 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 174 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 170 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 175 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 171 UrlRequest.Builder urlRequestBuilder = | 176 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 172 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge
ntName), callback, | 177 NativeTestServer.getEchoHeaderURL(userAgentName), callback, call
back.getExecutor()); |
| 173 callback.getExecutor(), testFramework.mCronetEngine); | |
| 174 urlRequestBuilder.build().start(); | 178 urlRequestBuilder.build().start(); |
| 175 callback.blockForDone(); | 179 callback.blockForDone(); |
| 176 assertEquals(userAgentValue, callback.mResponseAsString); | 180 assertEquals(userAgentValue, callback.mResponseAsString); |
| 177 } | 181 } |
| 178 | 182 |
| 179 @SmallTest | 183 @SmallTest |
| 180 @Feature({"Cronet"}) | 184 @Feature({"Cronet"}) |
| 181 @OnlyRunNativeCronet | 185 @OnlyRunNativeCronet |
| 182 public void testDataReductionProxyEnabled() throws Exception { | 186 public void testDataReductionProxyEnabled() throws Exception { |
| 183 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); | 187 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 184 | 188 |
| 185 // Ensure native code is loaded before trying to start test server. | 189 // Ensure native code is loaded before trying to start test server. |
| 186 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu
ild().shutdown(); | 190 ExperimentalCronetEngine.Builder engineBuilder = |
| 191 new ExperimentalCronetEngine.Builder(getContext()); |
| 192 CronetTestUtil.setLibraryName(engineBuilder, "cronet_tests"); |
| 193 engineBuilder.build().shutdown(); |
| 187 | 194 |
| 188 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 195 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 189 if (!NativeTestServer.isDataReductionProxySupported()) { | 196 if (!NativeTestServer.isDataReductionProxySupported()) { |
| 190 return; | 197 return; |
| 191 } | 198 } |
| 192 String serverHostPort = NativeTestServer.getHostPort(); | 199 String serverHostPort = NativeTestServer.getHostPort(); |
| 193 | 200 |
| 194 // Enable the Data Reduction Proxy and configure it to use the test | 201 // Enable the Data Reduction Proxy and configure it to use the test |
| 195 // server as its primary proxy, and to check successfully that this | 202 // server as its primary proxy, and to check successfully that this |
| 196 // proxy is OK to use. | 203 // proxy is OK to use. |
| 197 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC
ontext()); | 204 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 205 new ExperimentalCronetEngine.Builder(getContext()); |
| 198 cronetEngineBuilder.enableDataReductionProxy("test-key"); | 206 cronetEngineBuilder.enableDataReductionProxy("test-key"); |
| 199 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused
.net:9999", | 207 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused
.net:9999", |
| 200 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); | 208 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); |
| 201 cronetEngineBuilder.setLibraryName("cronet_tests"); | 209 CronetTestUtil.setLibraryName(cronetEngineBuilder, "cronet_tests"); |
| 202 testFramework.mCronetEngine = cronetEngineBuilder.build(); | 210 testFramework.mCronetEngine = (CronetEngineBase) cronetEngineBuilder.bui
ld(); |
| 203 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 211 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 204 | 212 |
| 205 // Construct and start a request that can only be returned by the test | 213 // Construct and start a request that can only be returned by the test |
| 206 // server. This request will fail if the configuration logic for the | 214 // server. This request will fail if the configuration logic for the |
| 207 // Data Reduction Proxy is not used. | 215 // Data Reduction Proxy is not used. |
| 208 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 216 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 209 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
callback, | 217 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
callback, |
| 210 callback.getExecutor(), testFramework.mCronetEngine); | 218 callback.getExecutor()); |
| 211 urlRequestBuilder.build().start(); | 219 urlRequestBuilder.build().start(); |
| 212 callback.blockForDone(); | 220 callback.blockForDone(); |
| 213 | 221 |
| 214 // Verify that the request is successful and that the Data Reduction | 222 // Verify that the request is successful and that the Data Reduction |
| 215 // Proxy logic configured to use the test server as its proxy. | 223 // Proxy logic configured to use the test server as its proxy. |
| 216 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 224 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 217 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); | 225 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); |
| 218 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess.
txt", | 226 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess.
txt", |
| 219 callback.mResponseInfo.getUrl()); | 227 callback.mResponseInfo.getUrl()); |
| 220 } | 228 } |
| 221 | 229 |
| 222 @SmallTest | 230 @SmallTest |
| 223 @Feature({"Cronet"}) | 231 @Feature({"Cronet"}) |
| 224 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except
ion { | 232 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except
ion { |
| 225 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get
Context()); | 233 ExperimentalCronetEngine.Builder mCronetEngineBuilder = |
| 234 new ExperimentalCronetEngine.Builder(getContext()); |
| 226 final CronetTestFramework testFramework = | 235 final CronetTestFramework testFramework = |
| 227 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | 236 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); |
| 228 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); | 237 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); |
| 229 TestNetworkQualityRttListener rttListener = | 238 TestNetworkQualityRttListener rttListener = |
| 230 new TestNetworkQualityRttListener(networkQualityExecutor); | 239 new TestNetworkQualityRttListener(networkQualityExecutor); |
| 231 TestNetworkQualityThroughputListener throughputListener = | 240 TestNetworkQualityThroughputListener throughputListener = |
| 232 new TestNetworkQualityThroughputListener(networkQualityExecutor,
null); | 241 new TestNetworkQualityThroughputListener(networkQualityExecutor,
null); |
| 233 try { | 242 try { |
| 234 testFramework.mCronetEngine.addRttListener(rttListener); | 243 testFramework.mCronetEngine.addRttListener(rttListener); |
| 235 fail("Should throw an exception."); | 244 fail("Should throw an exception."); |
| 236 } catch (IllegalStateException e) { | 245 } catch (IllegalStateException e) { |
| 237 } | 246 } |
| 238 try { | 247 try { |
| 239 testFramework.mCronetEngine.addThroughputListener(throughputListener
); | 248 testFramework.mCronetEngine.addThroughputListener(throughputListener
); |
| 240 fail("Should throw an exception."); | 249 fail("Should throw an exception."); |
| 241 } catch (IllegalStateException e) { | 250 } catch (IllegalStateException e) { |
| 242 } | 251 } |
| 243 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 252 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 244 UrlRequest.Builder builder = new UrlRequest.Builder( | 253 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( |
| 245 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 254 mUrl, callback, callback.getExecutor()); |
| 246 UrlRequest urlRequest = builder.build(); | 255 UrlRequest urlRequest = builder.build(); |
| 247 | 256 |
| 248 urlRequest.start(); | 257 urlRequest.start(); |
| 249 callback.blockForDone(); | 258 callback.blockForDone(); |
| 250 assertEquals(0, rttListener.rttObservationCount()); | 259 assertEquals(0, rttListener.rttObservationCount()); |
| 251 assertEquals(0, throughputListener.throughputObservationCount()); | 260 assertEquals(0, throughputListener.throughputObservationCount()); |
| 252 testFramework.mCronetEngine.shutdown(); | 261 testFramework.mCronetEngine.shutdown(); |
| 253 } | 262 } |
| 254 | 263 |
| 255 @SmallTest | 264 @SmallTest |
| 256 @Feature({"Cronet"}) | 265 @Feature({"Cronet"}) |
| 257 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E
xception { | 266 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E
xception { |
| 258 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get
Context()); | 267 ExperimentalCronetEngine.Builder mCronetEngineBuilder = |
| 268 new ExperimentalCronetEngine.Builder(getContext()); |
| 259 TestExecutor networkQualityExecutor = new TestExecutor(); | 269 TestExecutor networkQualityExecutor = new TestExecutor(); |
| 260 TestNetworkQualityRttListener rttListener = | 270 TestNetworkQualityRttListener rttListener = |
| 261 new TestNetworkQualityRttListener(networkQualityExecutor); | 271 new TestNetworkQualityRttListener(networkQualityExecutor); |
| 262 mCronetEngineBuilder.enableNetworkQualityEstimator(true); | 272 mCronetEngineBuilder.enableNetworkQualityEstimator(true); |
| 263 final CronetTestFramework testFramework = | 273 final CronetTestFramework testFramework = |
| 264 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | 274 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); |
| 265 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true); | 275 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true); |
| 266 | 276 |
| 267 testFramework.mCronetEngine.addRttListener(rttListener); | 277 testFramework.mCronetEngine.addRttListener(rttListener); |
| 268 testFramework.mCronetEngine.removeRttListener(rttListener); | 278 testFramework.mCronetEngine.removeRttListener(rttListener); |
| 269 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 279 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 270 UrlRequest.Builder builder = new UrlRequest.Builder( | 280 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( |
| 271 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 281 mUrl, callback, callback.getExecutor()); |
| 272 UrlRequest urlRequest = builder.build(); | 282 UrlRequest urlRequest = builder.build(); |
| 273 urlRequest.start(); | 283 urlRequest.start(); |
| 274 callback.blockForDone(); | 284 callback.blockForDone(); |
| 275 networkQualityExecutor.runAllTasks(); | 285 networkQualityExecutor.runAllTasks(); |
| 276 assertEquals(0, rttListener.rttObservationCount()); | 286 assertEquals(0, rttListener.rttObservationCount()); |
| 277 testFramework.mCronetEngine.shutdown(); | 287 testFramework.mCronetEngine.shutdown(); |
| 278 } | 288 } |
| 279 | 289 |
| 280 @SmallTest | 290 @SmallTest |
| 281 @Feature({"Cronet"}) | 291 @Feature({"Cronet"}) |
| 282 public void testRealTimeNetworkQualityObservationsQuicDisabled() throws Exce
ption { | 292 public void testRealTimeNetworkQualityObservationsQuicDisabled() throws Exce
ption { |
| 283 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get
Context()); | 293 ExperimentalCronetEngine.Builder mCronetEngineBuilder = |
| 294 new ExperimentalCronetEngine.Builder(getContext()); |
| 284 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0; | 295 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0; |
| 285 | |
| 286 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu
torThreadFactory()); | 296 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu
torThreadFactory()); |
| 287 ConditionVariable waitForThroughput = new ConditionVariable(); | 297 ConditionVariable waitForThroughput = new ConditionVariable(); |
| 288 TestNetworkQualityRttListener rttListener = | 298 TestNetworkQualityRttListener rttListener = |
| 289 new TestNetworkQualityRttListener(listenersExecutor); | 299 new TestNetworkQualityRttListener(listenersExecutor); |
| 290 TestNetworkQualityThroughputListener throughputListener = | 300 TestNetworkQualityThroughputListener throughputListener = |
| 291 new TestNetworkQualityThroughputListener(listenersExecutor, wait
ForThroughput); | 301 new TestNetworkQualityThroughputListener(listenersExecutor, wait
ForThroughput); |
| 292 mCronetEngineBuilder.enableHttp2(true).enableQuic(false).enableNetworkQu
alityEstimator( | 302 mCronetEngineBuilder.enableNetworkQualityEstimator(true).enableHttp2(tru
e).enableQuic( |
| 293 true); | 303 false); |
| 294 final CronetTestFramework testFramework = | 304 final CronetTestFramework testFramework = |
| 295 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | 305 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); |
| 296 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true); | 306 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true); |
| 297 | 307 |
| 298 testFramework.mCronetEngine.addRttListener(rttListener); | 308 testFramework.mCronetEngine.addRttListener(rttListener); |
| 299 testFramework.mCronetEngine.addThroughputListener(throughputListener); | 309 testFramework.mCronetEngine.addThroughputListener(throughputListener); |
| 300 | 310 |
| 301 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 311 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 302 UrlRequest.Builder builder = new UrlRequest.Builder( | 312 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( |
| 303 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 313 mUrl, callback, callback.getExecutor()); |
| 304 UrlRequest urlRequest = builder.build(); | 314 UrlRequest urlRequest = builder.build(); |
| 305 urlRequest.start(); | 315 urlRequest.start(); |
| 306 callback.blockForDone(); | 316 callback.blockForDone(); |
| 307 | 317 |
| 308 // Throughput observation is posted to the network quality estimator on
the network thread | 318 // Throughput observation is posted to the network quality estimator on
the network thread |
| 309 // after the UrlRequest is completed. The observations are then eventual
ly posted to | 319 // after the UrlRequest is completed. The observations are then eventual
ly posted to |
| 310 // throughput listeners on the executor provided to network quality. | 320 // throughput listeners on the executor provided to network quality. |
| 311 waitForThroughput.block(); | 321 waitForThroughput.block(); |
| 312 assertTrue(throughputListener.throughputObservationCount() > 0); | 322 assertTrue(throughputListener.throughputObservationCount() > 0); |
| 313 | 323 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 @Feature({"Cronet"}) | 366 @Feature({"Cronet"}) |
| 357 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/
/crbug.com/637972 | 367 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/
/crbug.com/637972 |
| 358 @OnlyRunNativeCronet | 368 @OnlyRunNativeCronet |
| 359 public void testShutdown() throws Exception { | 369 public void testShutdown() throws Exception { |
| 360 final CronetTestFramework testFramework = startCronetTestFramework(); | 370 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 361 ShutdownTestUrlRequestCallback callback = | 371 ShutdownTestUrlRequestCallback callback = |
| 362 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine); | 372 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine); |
| 363 // Block callback when response starts to verify that shutdown fails | 373 // Block callback when response starts to verify that shutdown fails |
| 364 // if there are active requests. | 374 // if there are active requests. |
| 365 callback.setAutoAdvance(false); | 375 callback.setAutoAdvance(false); |
| 366 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 376 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 367 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 377 mUrl, callback, callback.getExecutor()); |
| 368 UrlRequest urlRequest = urlRequestBuilder.build(); | 378 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 369 urlRequest.start(); | 379 urlRequest.start(); |
| 370 try { | 380 try { |
| 371 testFramework.mCronetEngine.shutdown(); | 381 testFramework.mCronetEngine.shutdown(); |
| 372 fail("Should throw an exception"); | 382 fail("Should throw an exception"); |
| 373 } catch (Exception e) { | 383 } catch (Exception e) { |
| 374 assertEquals("Cannot shutdown with active requests.", | 384 assertEquals("Cannot shutdown with active requests.", |
| 375 e.getMessage()); | 385 e.getMessage()); |
| 376 } | 386 } |
| 377 | 387 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 } | 502 } |
| 493 | 503 |
| 494 @SmallTest | 504 @SmallTest |
| 495 @Feature({"Cronet"}) | 505 @Feature({"Cronet"}) |
| 496 // TODO: Remove the annotation after fixing http://crbug.com/637972 | 506 // TODO: Remove the annotation after fixing http://crbug.com/637972 |
| 497 @OnlyRunNativeCronet | 507 @OnlyRunNativeCronet |
| 498 public void testShutdownAfterError() throws Exception { | 508 public void testShutdownAfterError() throws Exception { |
| 499 final CronetTestFramework testFramework = startCronetTestFramework(); | 509 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 500 ShutdownTestUrlRequestCallback callback = | 510 ShutdownTestUrlRequestCallback callback = |
| 501 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine); | 511 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine); |
| 502 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE
T_TEST_FAILED_URL, | 512 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 503 callback, callback.getExecutor(), testFramework.mCronetEngine); | 513 MOCK_CRONET_TEST_FAILED_URL, callback, callback.getExecutor()); |
| 504 urlRequestBuilder.build().start(); | 514 urlRequestBuilder.build().start(); |
| 505 callback.blockForDone(); | 515 callback.blockForDone(); |
| 506 assertTrue(callback.mOnErrorCalled); | 516 assertTrue(callback.mOnErrorCalled); |
| 507 callback.blockForCallbackToComplete(); | 517 callback.blockForCallbackToComplete(); |
| 508 callback.shutdownExecutor(); | 518 callback.shutdownExecutor(); |
| 509 } | 519 } |
| 510 | 520 |
| 511 @SmallTest | 521 @SmallTest |
| 512 @Feature({"Cronet"}) | 522 @Feature({"Cronet"}) |
| 513 public void testShutdownAfterCancel() throws Exception { | 523 public void testShutdownAfterCancel() throws Exception { |
| 514 final CronetTestFramework testFramework = startCronetTestFramework(); | 524 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 515 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 525 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 516 // Block callback when response starts to verify that shutdown fails | 526 // Block callback when response starts to verify that shutdown fails |
| 517 // if there are active requests. | 527 // if there are active requests. |
| 518 callback.setAutoAdvance(false); | 528 callback.setAutoAdvance(false); |
| 519 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 529 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 520 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 530 mUrl, callback, callback.getExecutor()); |
| 521 UrlRequest urlRequest = urlRequestBuilder.build(); | 531 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 522 urlRequest.start(); | 532 urlRequest.start(); |
| 523 try { | 533 try { |
| 524 testFramework.mCronetEngine.shutdown(); | 534 testFramework.mCronetEngine.shutdown(); |
| 525 fail("Should throw an exception"); | 535 fail("Should throw an exception"); |
| 526 } catch (Exception e) { | 536 } catch (Exception e) { |
| 527 assertEquals("Cannot shutdown with active requests.", | 537 assertEquals("Cannot shutdown with active requests.", |
| 528 e.getMessage()); | 538 e.getMessage()); |
| 529 } | 539 } |
| 530 callback.waitForNextStep(); | 540 callback.waitForNextStep(); |
| 531 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep); | 541 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep); |
| 532 urlRequest.cancel(); | 542 urlRequest.cancel(); |
| 533 testFramework.mCronetEngine.shutdown(); | 543 testFramework.mCronetEngine.shutdown(); |
| 534 } | 544 } |
| 535 | 545 |
| 536 @SmallTest | 546 @SmallTest |
| 537 @Feature({"Cronet"}) | 547 @Feature({"Cronet"}) |
| 538 @OnlyRunNativeCronet // No netlogs for pure java impl | 548 @OnlyRunNativeCronet // No netlogs for pure java impl |
| 539 public void testNetLog() throws Exception { | 549 public void testNetLog() throws Exception { |
| 540 Context context = getContext(); | 550 Context context = getContext(); |
| 541 File directory = new File(PathUtils.getDataDirectory()); | 551 File directory = new File(PathUtils.getDataDirectory()); |
| 542 File file = File.createTempFile("cronet", "json", directory); | 552 File file = File.createTempFile("cronet", "json", directory); |
| 543 CronetEngine cronetEngine = new CronetUrlRequestContext( | 553 CronetEngine cronetEngine = new CronetUrlRequestContext( |
| 544 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 554 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 545 // Start NetLog immediately after the request context is created to make | 555 // Start NetLog immediately after the request context is created to make |
| 546 // sure that the call won't crash the app even when the native request | 556 // sure that the call won't crash the app even when the native request |
| 547 // context is not fully initialized. See crbug.com/470196. | 557 // context is not fully initialized. See crbug.com/470196. |
| 548 cronetEngine.startNetLogToFile(file.getPath(), false); | 558 cronetEngine.startNetLogToFile(file.getPath(), false); |
| 549 | 559 |
| 550 // Start a request. | 560 // Start a request. |
| 551 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 561 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 552 UrlRequest.Builder urlRequestBuilder = | 562 UrlRequest.Builder urlRequestBuilder = |
| 553 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 563 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 554 urlRequestBuilder.build().start(); | 564 urlRequestBuilder.build().start(); |
| 555 callback.blockForDone(); | 565 callback.blockForDone(); |
| 556 cronetEngine.stopNetLog(); | 566 cronetEngine.stopNetLog(); |
| 557 assertTrue(file.exists()); | 567 assertTrue(file.exists()); |
| 558 assertTrue(file.length() != 0); | 568 assertTrue(file.length() != 0); |
| 559 assertFalse(hasBytesInNetLog(file)); | 569 assertFalse(hasBytesInNetLog(file)); |
| 560 assertTrue(file.delete()); | 570 assertTrue(file.delete()); |
| 561 assertTrue(!file.exists()); | 571 assertTrue(!file.exists()); |
| 562 } | 572 } |
| 563 | 573 |
| 564 @SmallTest | 574 @SmallTest |
| 565 @Feature({"Cronet"}) | 575 @Feature({"Cronet"}) |
| 566 @OnlyRunNativeCronet // No netlogs for pure java impl | 576 @OnlyRunNativeCronet // No netlogs for pure java impl |
| 567 public void testBoundedFileNetLog() throws Exception { | 577 public void testBoundedFileNetLog() throws Exception { |
| 568 Context context = getContext(); | 578 Context context = getContext(); |
| 569 File directory = new File(PathUtils.getDataDirectory()); | 579 File directory = new File(PathUtils.getDataDirectory()); |
| 570 File netLogDir = new File(directory, "NetLog"); | 580 File netLogDir = new File(directory, "NetLog"); |
| 571 assertFalse(netLogDir.exists()); | 581 assertFalse(netLogDir.exists()); |
| 572 assertTrue(netLogDir.mkdir()); | 582 assertTrue(netLogDir.mkdir()); |
| 573 File eventFile = new File(netLogDir, "event_file_0.json"); | 583 File eventFile = new File(netLogDir, "event_file_0.json"); |
| 574 CronetEngine cronetEngine = new CronetUrlRequestContext( | 584 CronetUrlRequestContext cronetEngine = new CronetUrlRequestContext( |
| 575 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 585 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 576 // Start NetLog immediately after the request context is created to make | 586 // Start NetLog immediately after the request context is created to make |
| 577 // sure that the call won't crash the app even when the native request | 587 // sure that the call won't crash the app even when the native request |
| 578 // context is not fully initialized. See crbug.com/470196. | 588 // context is not fully initialized. See crbug.com/470196. |
| 579 cronetEngine.startNetLogToDisk(netLogDir.getPath(), false, MAX_FILE_SIZE
); | 589 cronetEngine.startNetLogToDisk(netLogDir.getPath(), false, MAX_FILE_SIZE
); |
| 580 | 590 |
| 581 // Start a request. | 591 // Start a request. |
| 582 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 592 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 583 UrlRequest.Builder urlRequestBuilder = | 593 UrlRequest.Builder urlRequestBuilder = |
| 584 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 594 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 585 urlRequestBuilder.build().start(); | 595 urlRequestBuilder.build().start(); |
| 586 callback.blockForDone(); | 596 callback.blockForDone(); |
| 587 cronetEngine.stopNetLog(); | 597 cronetEngine.stopNetLog(); |
| 588 assertTrue(eventFile.exists()); | 598 assertTrue(eventFile.exists()); |
| 589 assertTrue(eventFile.length() != 0); | 599 assertTrue(eventFile.length() != 0); |
| 590 assertFalse(hasBytesInNetLog(eventFile)); | 600 assertFalse(hasBytesInNetLog(eventFile)); |
| 591 FileUtils.recursivelyDeleteFile(netLogDir); | 601 FileUtils.recursivelyDeleteFile(netLogDir); |
| 592 assertFalse(netLogDir.exists()); | 602 assertFalse(netLogDir.exists()); |
| 593 } | 603 } |
| 594 | 604 |
| 595 @SmallTest | 605 @SmallTest |
| 596 @Feature({"Cronet"}) | 606 @Feature({"Cronet"}) |
| 597 @OnlyRunNativeCronet // No netlogs for pure java impl | 607 @OnlyRunNativeCronet // No netlogs for pure java impl |
| 598 // Tests that if stopNetLog is not explicity called, CronetEngine.shutdown() | 608 // Tests that if stopNetLog is not explicity called, CronetEngine.shutdown() |
| 599 // will take care of it. crbug.com/623701. | 609 // will take care of it. crbug.com/623701. |
| 600 public void testNoStopNetLog() throws Exception { | 610 public void testNoStopNetLog() throws Exception { |
| 601 Context context = getContext(); | 611 Context context = getContext(); |
| 602 File directory = new File(PathUtils.getDataDirectory()); | 612 File directory = new File(PathUtils.getDataDirectory()); |
| 603 File file = File.createTempFile("cronet", "json", directory); | 613 File file = File.createTempFile("cronet", "json", directory); |
| 604 CronetEngine cronetEngine = new CronetUrlRequestContext( | 614 CronetUrlRequestContext cronetEngine = new CronetUrlRequestContext( |
| 605 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 615 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 606 cronetEngine.startNetLogToFile(file.getPath(), false); | 616 cronetEngine.startNetLogToFile(file.getPath(), false); |
| 607 | 617 |
| 608 // Start a request. | 618 // Start a request. |
| 609 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 619 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 610 UrlRequest.Builder urlRequestBuilder = | 620 UrlRequest.Builder urlRequestBuilder = |
| 611 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 621 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 612 urlRequestBuilder.build().start(); | 622 urlRequestBuilder.build().start(); |
| 613 callback.blockForDone(); | 623 callback.blockForDone(); |
| 614 // Shut down the engine without calling stopNetLog. | 624 // Shut down the engine without calling stopNetLog. |
| 615 cronetEngine.shutdown(); | 625 cronetEngine.shutdown(); |
| 616 assertTrue(file.exists()); | 626 assertTrue(file.exists()); |
| 617 assertTrue(file.length() != 0); | 627 assertTrue(file.length() != 0); |
| 618 assertFalse(hasBytesInNetLog(file)); | 628 assertFalse(hasBytesInNetLog(file)); |
| 619 assertTrue(file.delete()); | 629 assertTrue(file.delete()); |
| 620 assertTrue(!file.exists()); | 630 assertTrue(!file.exists()); |
| 621 } | 631 } |
| 622 | 632 |
| 623 @SmallTest | 633 @SmallTest |
| 624 @Feature({"Cronet"}) | 634 @Feature({"Cronet"}) |
| 625 @OnlyRunNativeCronet // No netlogs for pure java impl | 635 @OnlyRunNativeCronet // No netlogs for pure java impl |
| 626 // Tests that if stopNetLog is not explicity called, CronetEngine.shutdown() | 636 // Tests that if stopNetLog is not explicity called, CronetEngine.shutdown() |
| 627 // will take care of it. crbug.com/623701. | 637 // will take care of it. crbug.com/623701. |
| 628 public void testNoStopBoundedFileNetLog() throws Exception { | 638 public void testNoStopBoundedFileNetLog() throws Exception { |
| 629 Context context = getContext(); | 639 Context context = getContext(); |
| 630 File directory = new File(PathUtils.getDataDirectory()); | 640 File directory = new File(PathUtils.getDataDirectory()); |
| 631 File netLogDir = new File(directory, "NetLog"); | 641 File netLogDir = new File(directory, "NetLog"); |
| 632 assertFalse(netLogDir.exists()); | 642 assertFalse(netLogDir.exists()); |
| 633 assertTrue(netLogDir.mkdir()); | 643 assertTrue(netLogDir.mkdir()); |
| 634 File eventFile = new File(netLogDir, "event_file_0.json"); | 644 File eventFile = new File(netLogDir, "event_file_0.json"); |
| 635 CronetEngine cronetEngine = new CronetUrlRequestContext( | 645 CronetUrlRequestContext cronetEngine = new CronetUrlRequestContext( |
| 636 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 646 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 637 cronetEngine.startNetLogToDisk(netLogDir.getPath(), false, MAX_FILE_SIZE
); | 647 cronetEngine.startNetLogToDisk(netLogDir.getPath(), false, MAX_FILE_SIZE
); |
| 638 | 648 |
| 639 // Start a request. | 649 // Start a request. |
| 640 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 650 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 641 UrlRequest.Builder urlRequestBuilder = | 651 UrlRequest.Builder urlRequestBuilder = |
| 642 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 652 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 643 urlRequestBuilder.build().start(); | 653 urlRequestBuilder.build().start(); |
| 644 callback.blockForDone(); | 654 callback.blockForDone(); |
| 645 // Shut down the engine without calling stopNetLog. | 655 // Shut down the engine without calling stopNetLog. |
| 646 cronetEngine.shutdown(); | 656 cronetEngine.shutdown(); |
| 647 assertTrue(eventFile.exists()); | 657 assertTrue(eventFile.exists()); |
| 648 assertTrue(eventFile.length() != 0); | 658 assertTrue(eventFile.length() != 0); |
| 649 | 659 |
| 650 FileUtils.recursivelyDeleteFile(netLogDir); | 660 FileUtils.recursivelyDeleteFile(netLogDir); |
| 651 assertFalse(netLogDir.exists()); | 661 assertFalse(netLogDir.exists()); |
| 652 } | 662 } |
| 653 | 663 |
| 654 @SmallTest | 664 @SmallTest |
| 655 @Feature({"Cronet"}) | 665 @Feature({"Cronet"}) |
| 656 @OnlyRunNativeCronet | 666 @OnlyRunNativeCronet |
| 657 // Tests that NetLog contains events emitted by all live CronetEngines. | 667 // Tests that NetLog contains events emitted by all live CronetEngines. |
| 658 public void testNetLogContainEventsFromAllLiveEngines() throws Exception { | 668 public void testNetLogContainEventsFromAllLiveEngines() throws Exception { |
| 659 Context context = getContext(); | 669 Context context = getContext(); |
| 660 File directory = new File(PathUtils.getDataDirectory()); | 670 File directory = new File(PathUtils.getDataDirectory()); |
| 661 File file1 = File.createTempFile("cronet1", "json", directory); | 671 File file1 = File.createTempFile("cronet1", "json", directory); |
| 662 File file2 = File.createTempFile("cronet2", "json", directory); | 672 File file2 = File.createTempFile("cronet2", "json", directory); |
| 663 CronetEngine cronetEngine1 = new CronetUrlRequestContext( | 673 CronetEngine cronetEngine1 = new CronetUrlRequestContext( |
| 664 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 674 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 665 CronetEngine cronetEngine2 = new CronetUrlRequestContext( | 675 CronetEngine cronetEngine2 = new CronetUrlRequestContext( |
| 666 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 676 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 667 | 677 |
| 668 cronetEngine1.startNetLogToFile(file1.getPath(), false); | 678 cronetEngine1.startNetLogToFile(file1.getPath(), false); |
| 669 cronetEngine2.startNetLogToFile(file2.getPath(), false); | 679 cronetEngine2.startNetLogToFile(file2.getPath(), false); |
| 670 | 680 |
| 671 // Warm CronetEngine and make sure both CronetUrlRequestContexts are | 681 // Warm CronetEngine and make sure both CronetUrlRequestContexts are |
| 672 // initialized before testing the logs. | 682 // initialized before testing the logs. |
| 673 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200); | 683 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200); |
| 674 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200); | 684 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200); |
| 675 | 685 |
| 676 // Use cronetEngine1 to make a request to mUrl404. | 686 // Use cronetEngine1 to make a request to mUrl404. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 703 File netLogDir1 = new File(directory, "NetLog1"); | 713 File netLogDir1 = new File(directory, "NetLog1"); |
| 704 assertFalse(netLogDir1.exists()); | 714 assertFalse(netLogDir1.exists()); |
| 705 assertTrue(netLogDir1.mkdir()); | 715 assertTrue(netLogDir1.mkdir()); |
| 706 File netLogDir2 = new File(directory, "NetLog2"); | 716 File netLogDir2 = new File(directory, "NetLog2"); |
| 707 assertFalse(netLogDir2.exists()); | 717 assertFalse(netLogDir2.exists()); |
| 708 assertTrue(netLogDir2.mkdir()); | 718 assertTrue(netLogDir2.mkdir()); |
| 709 File eventFile1 = new File(netLogDir1, "event_file_0.json"); | 719 File eventFile1 = new File(netLogDir1, "event_file_0.json"); |
| 710 File eventFile2 = new File(netLogDir2, "event_file_0.json"); | 720 File eventFile2 = new File(netLogDir2, "event_file_0.json"); |
| 711 | 721 |
| 712 CronetUrlRequestContext cronetEngine1 = new CronetUrlRequestContext( | 722 CronetUrlRequestContext cronetEngine1 = new CronetUrlRequestContext( |
| 713 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 723 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 714 CronetUrlRequestContext cronetEngine2 = new CronetUrlRequestContext( | 724 CronetUrlRequestContext cronetEngine2 = new CronetUrlRequestContext( |
| 715 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 725 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 716 | 726 |
| 717 cronetEngine1.startNetLogToDisk(netLogDir1.getPath(), false, MAX_FILE_SI
ZE); | 727 cronetEngine1.startNetLogToDisk(netLogDir1.getPath(), false, MAX_FILE_SI
ZE); |
| 718 cronetEngine2.startNetLogToDisk(netLogDir2.getPath(), false, MAX_FILE_SI
ZE); | 728 cronetEngine2.startNetLogToDisk(netLogDir2.getPath(), false, MAX_FILE_SI
ZE); |
| 719 | 729 |
| 720 // Warm CronetEngine and make sure both CronetUrlRequestContexts are | 730 // Warm CronetEngine and make sure both CronetUrlRequestContexts are |
| 721 // initialized before testing the logs. | 731 // initialized before testing the logs. |
| 722 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200); | 732 makeRequestAndCheckStatus(cronetEngine1, mUrl, 200); |
| 723 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200); | 733 makeRequestAndCheckStatus(cronetEngine2, mUrl, 200); |
| 724 | 734 |
| 725 // Use cronetEngine1 to make a request to mUrl404. | 735 // Use cronetEngine1 to make a request to mUrl404. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 } | 801 } |
| 792 Executor directExecutor = new Executor() { | 802 Executor directExecutor = new Executor() { |
| 793 @Override | 803 @Override |
| 794 public void execute(Runnable command) { | 804 public void execute(Runnable command) { |
| 795 command.run(); | 805 command.run(); |
| 796 } | 806 } |
| 797 }; | 807 }; |
| 798 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); | 808 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); |
| 799 callback.setAllowDirectExecutor(true); | 809 callback.setAllowDirectExecutor(true); |
| 800 UrlRequest.Builder urlRequestBuilder = | 810 UrlRequest.Builder urlRequestBuilder = |
| 801 new UrlRequest.Builder(url, callback, directExecutor, testFramew
ork.mCronetEngine); | 811 testFramework.mCronetEngine.newUrlRequestBuilder(url, callback,
directExecutor); |
| 802 urlRequestBuilder.allowDirectExecutor(); | 812 urlRequestBuilder.allowDirectExecutor(); |
| 803 urlRequestBuilder.build().start(); | 813 urlRequestBuilder.build().start(); |
| 804 callback.blockForDone(); | 814 callback.blockForDone(); |
| 805 assertTrue(thrown.get() instanceof RuntimeException); | 815 assertTrue(thrown.get() instanceof RuntimeException); |
| 806 } | 816 } |
| 807 | 817 |
| 808 @SmallTest | 818 @SmallTest |
| 809 @Feature({"Cronet"}) | 819 @Feature({"Cronet"}) |
| 810 @OnlyRunNativeCronet | 820 @OnlyRunNativeCronet |
| 811 // Tests that if CronetEngine is shut down when reading from disk cache, | 821 // Tests that if CronetEngine is shut down when reading from disk cache, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 835 throw new RuntimeException("Unexpected"); | 845 throw new RuntimeException("Unexpected"); |
| 836 } | 846 } |
| 837 | 847 |
| 838 @Override | 848 @Override |
| 839 public void onFailed( | 849 public void onFailed( |
| 840 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio
n error) { | 850 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio
n error) { |
| 841 throw new RuntimeException("Unexpected"); | 851 throw new RuntimeException("Unexpected"); |
| 842 } | 852 } |
| 843 } | 853 } |
| 844 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); | 854 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); |
| 845 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 855 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 846 url, callback, callback.getExecutor(), testFramework.mCronetEngi
ne); | 856 url, callback, callback.getExecutor()); |
| 847 urlRequestBuilder.build().start(); | 857 urlRequestBuilder.build().start(); |
| 848 callback.blockForDone(); | 858 callback.blockForDone(); |
| 849 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 859 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 850 assertTrue(callback.mResponseInfo.wasCached()); | 860 assertTrue(callback.mResponseInfo.wasCached()); |
| 851 assertTrue(callback.mOnCanceledCalled); | 861 assertTrue(callback.mOnCanceledCalled); |
| 852 } | 862 } |
| 853 | 863 |
| 854 @SmallTest | 864 @SmallTest |
| 855 @Feature({"Cronet"}) | 865 @Feature({"Cronet"}) |
| 856 @OnlyRunNativeCronet | 866 @OnlyRunNativeCronet |
| 857 public void testNetLogAfterShutdown() throws Exception { | 867 public void testNetLogAfterShutdown() throws Exception { |
| 858 final CronetTestFramework testFramework = startCronetTestFramework(); | 868 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 859 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 869 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 860 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 870 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 861 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 871 mUrl, callback, callback.getExecutor()); |
| 862 urlRequestBuilder.build().start(); | 872 urlRequestBuilder.build().start(); |
| 863 callback.blockForDone(); | 873 callback.blockForDone(); |
| 864 testFramework.mCronetEngine.shutdown(); | 874 testFramework.mCronetEngine.shutdown(); |
| 865 | 875 |
| 866 File directory = new File(PathUtils.getDataDirectory()); | 876 File directory = new File(PathUtils.getDataDirectory()); |
| 867 File file = File.createTempFile("cronet", "json", directory); | 877 File file = File.createTempFile("cronet", "json", directory); |
| 868 try { | 878 try { |
| 869 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false)
; | 879 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false)
; |
| 870 fail("Should throw an exception."); | 880 fail("Should throw an exception."); |
| 871 } catch (Exception e) { | 881 } catch (Exception e) { |
| 872 assertEquals("Engine is shut down.", e.getMessage()); | 882 assertEquals("Engine is shut down.", e.getMessage()); |
| 873 } | 883 } |
| 874 assertFalse(hasBytesInNetLog(file)); | 884 assertFalse(hasBytesInNetLog(file)); |
| 875 assertTrue(file.delete()); | 885 assertTrue(file.delete()); |
| 876 assertTrue(!file.exists()); | 886 assertTrue(!file.exists()); |
| 877 } | 887 } |
| 878 | 888 |
| 879 @SmallTest | 889 @SmallTest |
| 880 @Feature({"Cronet"}) | 890 @Feature({"Cronet"}) |
| 881 @OnlyRunNativeCronet | 891 @OnlyRunNativeCronet |
| 882 public void testBoundedFileNetLogAfterShutdown() throws Exception { | 892 public void testBoundedFileNetLogAfterShutdown() throws Exception { |
| 883 final CronetTestFramework testFramework = startCronetTestFramework(); | 893 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 884 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 894 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 885 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 895 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 886 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 896 mUrl, callback, callback.getExecutor()); |
| 887 urlRequestBuilder.build().start(); | 897 urlRequestBuilder.build().start(); |
| 888 callback.blockForDone(); | 898 callback.blockForDone(); |
| 889 testFramework.mCronetEngine.shutdown(); | 899 testFramework.mCronetEngine.shutdown(); |
| 890 | 900 |
| 891 File directory = new File(PathUtils.getDataDirectory()); | 901 File directory = new File(PathUtils.getDataDirectory()); |
| 892 File netLogDir = new File(directory, "NetLog"); | 902 File netLogDir = new File(directory, "NetLog"); |
| 893 assertFalse(netLogDir.exists()); | 903 assertFalse(netLogDir.exists()); |
| 894 assertTrue(netLogDir.mkdir()); | 904 assertTrue(netLogDir.mkdir()); |
| 895 File constantsFile = new File(netLogDir, "constants.json"); | 905 File constantsFile = new File(netLogDir, "constants.json"); |
| 896 try { | 906 try { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 911 final CronetTestFramework testFramework = startCronetTestFramework(); | 921 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 912 File directory = new File(PathUtils.getDataDirectory()); | 922 File directory = new File(PathUtils.getDataDirectory()); |
| 913 File file = File.createTempFile("cronet", "json", directory); | 923 File file = File.createTempFile("cronet", "json", directory); |
| 914 // Start NetLog multiple times. | 924 // Start NetLog multiple times. |
| 915 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); | 925 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 916 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); | 926 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 917 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); | 927 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 918 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); | 928 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 919 // Start a request. | 929 // Start a request. |
| 920 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 930 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 921 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 931 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 922 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 932 mUrl, callback, callback.getExecutor()); |
| 923 urlRequestBuilder.build().start(); | 933 urlRequestBuilder.build().start(); |
| 924 callback.blockForDone(); | 934 callback.blockForDone(); |
| 925 testFramework.mCronetEngine.stopNetLog(); | 935 testFramework.mCronetEngine.stopNetLog(); |
| 926 assertTrue(file.exists()); | 936 assertTrue(file.exists()); |
| 927 assertTrue(file.length() != 0); | 937 assertTrue(file.length() != 0); |
| 928 assertFalse(hasBytesInNetLog(file)); | 938 assertFalse(hasBytesInNetLog(file)); |
| 929 assertTrue(file.delete()); | 939 assertTrue(file.delete()); |
| 930 assertTrue(!file.exists()); | 940 assertTrue(!file.exists()); |
| 931 } | 941 } |
| 932 | 942 |
| 933 @SmallTest | 943 @SmallTest |
| 934 @Feature({"Cronet"}) | 944 @Feature({"Cronet"}) |
| 935 public void testBoundedFileNetLogStartMultipleTimes() throws Exception { | 945 public void testBoundedFileNetLogStartMultipleTimes() throws Exception { |
| 936 final CronetTestFramework testFramework = startCronetTestFramework(); | 946 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 937 File directory = new File(PathUtils.getDataDirectory()); | 947 File directory = new File(PathUtils.getDataDirectory()); |
| 938 File netLogDir = new File(directory, "NetLog"); | 948 File netLogDir = new File(directory, "NetLog"); |
| 939 assertFalse(netLogDir.exists()); | 949 assertFalse(netLogDir.exists()); |
| 940 assertTrue(netLogDir.mkdir()); | 950 assertTrue(netLogDir.mkdir()); |
| 941 File eventFile = new File(netLogDir, "event_file_0.json"); | 951 File eventFile = new File(netLogDir, "event_file_0.json"); |
| 942 // Start NetLog multiple times. This should be equivalent to starting Ne
tLog | 952 // Start NetLog multiple times. This should be equivalent to starting Ne
tLog |
| 943 // once. Each subsequent start (without calling stopNetLog) should be a
no-op. | 953 // once. Each subsequent start (without calling stopNetLog) should be a
no-op. |
| 944 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); | 954 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); |
| 945 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); | 955 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); |
| 946 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); | 956 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); |
| 947 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); | 957 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); |
| 948 // Start a request. | 958 // Start a request. |
| 949 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 959 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 950 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 960 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 951 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 961 mUrl, callback, callback.getExecutor()); |
| 952 urlRequestBuilder.build().start(); | 962 urlRequestBuilder.build().start(); |
| 953 callback.blockForDone(); | 963 callback.blockForDone(); |
| 954 testFramework.mCronetEngine.stopNetLog(); | 964 testFramework.mCronetEngine.stopNetLog(); |
| 955 assertTrue(eventFile.exists()); | 965 assertTrue(eventFile.exists()); |
| 956 assertTrue(eventFile.length() != 0); | 966 assertTrue(eventFile.length() != 0); |
| 957 assertFalse(hasBytesInNetLog(eventFile)); | 967 assertFalse(hasBytesInNetLog(eventFile)); |
| 958 FileUtils.recursivelyDeleteFile(netLogDir); | 968 FileUtils.recursivelyDeleteFile(netLogDir); |
| 959 assertFalse(netLogDir.exists()); | 969 assertFalse(netLogDir.exists()); |
| 960 } | 970 } |
| 961 | 971 |
| 962 @SmallTest | 972 @SmallTest |
| 963 @Feature({"Cronet"}) | 973 @Feature({"Cronet"}) |
| 964 public void testNetLogStopMultipleTimes() throws Exception { | 974 public void testNetLogStopMultipleTimes() throws Exception { |
| 965 final CronetTestFramework testFramework = startCronetTestFramework(); | 975 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 966 File directory = new File(PathUtils.getDataDirectory()); | 976 File directory = new File(PathUtils.getDataDirectory()); |
| 967 File file = File.createTempFile("cronet", "json", directory); | 977 File file = File.createTempFile("cronet", "json", directory); |
| 968 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); | 978 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 969 // Start a request. | 979 // Start a request. |
| 970 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 980 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 971 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 981 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 972 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 982 mUrl, callback, callback.getExecutor()); |
| 973 urlRequestBuilder.build().start(); | 983 urlRequestBuilder.build().start(); |
| 974 callback.blockForDone(); | 984 callback.blockForDone(); |
| 975 // Stop NetLog multiple times. | 985 // Stop NetLog multiple times. |
| 976 testFramework.mCronetEngine.stopNetLog(); | 986 testFramework.mCronetEngine.stopNetLog(); |
| 977 testFramework.mCronetEngine.stopNetLog(); | 987 testFramework.mCronetEngine.stopNetLog(); |
| 978 testFramework.mCronetEngine.stopNetLog(); | 988 testFramework.mCronetEngine.stopNetLog(); |
| 979 testFramework.mCronetEngine.stopNetLog(); | 989 testFramework.mCronetEngine.stopNetLog(); |
| 980 testFramework.mCronetEngine.stopNetLog(); | 990 testFramework.mCronetEngine.stopNetLog(); |
| 981 assertTrue(file.exists()); | 991 assertTrue(file.exists()); |
| 982 assertTrue(file.length() != 0); | 992 assertTrue(file.length() != 0); |
| 983 assertFalse(hasBytesInNetLog(file)); | 993 assertFalse(hasBytesInNetLog(file)); |
| 984 assertTrue(file.delete()); | 994 assertTrue(file.delete()); |
| 985 assertTrue(!file.exists()); | 995 assertTrue(!file.exists()); |
| 986 } | 996 } |
| 987 | 997 |
| 988 @SmallTest | 998 @SmallTest |
| 989 @Feature({"Cronet"}) | 999 @Feature({"Cronet"}) |
| 990 public void testBoundedFileNetLogStopMultipleTimes() throws Exception { | 1000 public void testBoundedFileNetLogStopMultipleTimes() throws Exception { |
| 991 final CronetTestFramework testFramework = startCronetTestFramework(); | 1001 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 992 File directory = new File(PathUtils.getDataDirectory()); | 1002 File directory = new File(PathUtils.getDataDirectory()); |
| 993 File netLogDir = new File(directory, "NetLog"); | 1003 File netLogDir = new File(directory, "NetLog"); |
| 994 assertFalse(netLogDir.exists()); | 1004 assertFalse(netLogDir.exists()); |
| 995 assertTrue(netLogDir.mkdir()); | 1005 assertTrue(netLogDir.mkdir()); |
| 996 File eventFile = new File(netLogDir, "event_file_0.json"); | 1006 File eventFile = new File(netLogDir, "event_file_0.json"); |
| 997 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); | 1007 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false
, MAX_FILE_SIZE); |
| 998 // Start a request. | 1008 // Start a request. |
| 999 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1009 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1000 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 1010 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 1001 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 1011 mUrl, callback, callback.getExecutor()); |
| 1002 urlRequestBuilder.build().start(); | 1012 urlRequestBuilder.build().start(); |
| 1003 callback.blockForDone(); | 1013 callback.blockForDone(); |
| 1004 // Stop NetLog multiple times. This should be equivalent to stopping Net
Log once. | 1014 // Stop NetLog multiple times. This should be equivalent to stopping Net
Log once. |
| 1005 // Each subsequent stop (without calling startNetLogToDisk first) should
be a no-op. | 1015 // Each subsequent stop (without calling startNetLogToDisk first) should
be a no-op. |
| 1006 testFramework.mCronetEngine.stopNetLog(); | 1016 testFramework.mCronetEngine.stopNetLog(); |
| 1007 testFramework.mCronetEngine.stopNetLog(); | 1017 testFramework.mCronetEngine.stopNetLog(); |
| 1008 testFramework.mCronetEngine.stopNetLog(); | 1018 testFramework.mCronetEngine.stopNetLog(); |
| 1009 testFramework.mCronetEngine.stopNetLog(); | 1019 testFramework.mCronetEngine.stopNetLog(); |
| 1010 testFramework.mCronetEngine.stopNetLog(); | 1020 testFramework.mCronetEngine.stopNetLog(); |
| 1011 assertTrue(eventFile.exists()); | 1021 assertTrue(eventFile.exists()); |
| 1012 assertTrue(eventFile.length() != 0); | 1022 assertTrue(eventFile.length() != 0); |
| 1013 assertFalse(hasBytesInNetLog(eventFile)); | 1023 assertFalse(hasBytesInNetLog(eventFile)); |
| 1014 FileUtils.recursivelyDeleteFile(netLogDir); | 1024 FileUtils.recursivelyDeleteFile(netLogDir); |
| 1015 assertFalse(netLogDir.exists()); | 1025 assertFalse(netLogDir.exists()); |
| 1016 } | 1026 } |
| 1017 | 1027 |
| 1018 @SmallTest | 1028 @SmallTest |
| 1019 @Feature({"Cronet"}) | 1029 @Feature({"Cronet"}) |
| 1020 @OnlyRunNativeCronet | 1030 @OnlyRunNativeCronet |
| 1021 public void testNetLogWithBytes() throws Exception { | 1031 public void testNetLogWithBytes() throws Exception { |
| 1022 Context context = getContext(); | 1032 Context context = getContext(); |
| 1023 File directory = new File(PathUtils.getDataDirectory()); | 1033 File directory = new File(PathUtils.getDataDirectory()); |
| 1024 File file = File.createTempFile("cronet", "json", directory); | 1034 File file = File.createTempFile("cronet", "json", directory); |
| 1025 CronetEngine cronetEngine = new CronetUrlRequestContext( | 1035 CronetEngine cronetEngine = new CronetUrlRequestContext( |
| 1026 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 1036 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 1027 // Start NetLog with logAll as true. | 1037 // Start NetLog with logAll as true. |
| 1028 cronetEngine.startNetLogToFile(file.getPath(), true); | 1038 cronetEngine.startNetLogToFile(file.getPath(), true); |
| 1029 // Start a request. | 1039 // Start a request. |
| 1030 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1040 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1031 UrlRequest.Builder urlRequestBuilder = | 1041 UrlRequest.Builder urlRequestBuilder = |
| 1032 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 1042 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 1033 urlRequestBuilder.build().start(); | 1043 urlRequestBuilder.build().start(); |
| 1034 callback.blockForDone(); | 1044 callback.blockForDone(); |
| 1035 cronetEngine.stopNetLog(); | 1045 cronetEngine.stopNetLog(); |
| 1036 assertTrue(file.exists()); | 1046 assertTrue(file.exists()); |
| 1037 assertTrue(file.length() != 0); | 1047 assertTrue(file.length() != 0); |
| 1038 assertTrue(hasBytesInNetLog(file)); | 1048 assertTrue(hasBytesInNetLog(file)); |
| 1039 assertTrue(file.delete()); | 1049 assertTrue(file.delete()); |
| 1040 assertTrue(!file.exists()); | 1050 assertTrue(!file.exists()); |
| 1041 } | 1051 } |
| 1042 | 1052 |
| 1043 @SmallTest | 1053 @SmallTest |
| 1044 @Feature({"Cronet"}) | 1054 @Feature({"Cronet"}) |
| 1045 @OnlyRunNativeCronet | 1055 @OnlyRunNativeCronet |
| 1046 public void testBoundedFileNetLogWithBytes() throws Exception { | 1056 public void testBoundedFileNetLogWithBytes() throws Exception { |
| 1047 Context context = getContext(); | 1057 Context context = getContext(); |
| 1048 File directory = new File(PathUtils.getDataDirectory()); | 1058 File directory = new File(PathUtils.getDataDirectory()); |
| 1049 File netLogDir = new File(directory, "NetLog"); | 1059 File netLogDir = new File(directory, "NetLog"); |
| 1050 assertFalse(netLogDir.exists()); | 1060 assertFalse(netLogDir.exists()); |
| 1051 assertTrue(netLogDir.mkdir()); | 1061 assertTrue(netLogDir.mkdir()); |
| 1052 File eventFile = new File(netLogDir, "event_file_0.json"); | 1062 File eventFile = new File(netLogDir, "event_file_0.json"); |
| 1053 CronetUrlRequestContext cronetEngine = new CronetUrlRequestContext( | 1063 CronetUrlRequestContext cronetEngine = new CronetUrlRequestContext( |
| 1054 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 1064 new CronetEngineBuilderImpl(context).setLibraryName("cronet_test
s")); |
| 1055 // Start NetLog with logAll as true. | 1065 // Start NetLog with logAll as true. |
| 1056 cronetEngine.startNetLogToDisk(netLogDir.getPath(), true, MAX_FILE_SIZE)
; | 1066 cronetEngine.startNetLogToDisk(netLogDir.getPath(), true, MAX_FILE_SIZE)
; |
| 1057 // Start a request. | 1067 // Start a request. |
| 1058 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1068 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1059 UrlRequest.Builder urlRequestBuilder = | 1069 UrlRequest.Builder urlRequestBuilder = |
| 1060 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 1070 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 1061 urlRequestBuilder.build().start(); | 1071 urlRequestBuilder.build().start(); |
| 1062 callback.blockForDone(); | 1072 callback.blockForDone(); |
| 1063 cronetEngine.stopNetLog(); | 1073 cronetEngine.stopNetLog(); |
| 1064 | 1074 |
| 1065 assertTrue(eventFile.exists()); | 1075 assertTrue(eventFile.exists()); |
| 1066 assertTrue(eventFile.length() != 0); | 1076 assertTrue(eventFile.length() != 0); |
| 1067 assertTrue(hasBytesInNetLog(eventFile)); | 1077 assertTrue(hasBytesInNetLog(eventFile)); |
| 1068 FileUtils.recursivelyDeleteFile(netLogDir); | 1078 FileUtils.recursivelyDeleteFile(netLogDir); |
| 1069 assertFalse(netLogDir.exists()); | 1079 assertFalse(netLogDir.exists()); |
| 1070 } | 1080 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1089 } | 1099 } |
| 1090 | 1100 |
| 1091 /** | 1101 /** |
| 1092 * Helper method to make a request to {@code url}, wait for it to | 1102 * Helper method to make a request to {@code url}, wait for it to |
| 1093 * complete, and check that the status code is the same as {@code expectedSt
atusCode}. | 1103 * complete, and check that the status code is the same as {@code expectedSt
atusCode}. |
| 1094 */ | 1104 */ |
| 1095 private void makeRequestAndCheckStatus( | 1105 private void makeRequestAndCheckStatus( |
| 1096 CronetEngine engine, String url, int expectedStatusCode) { | 1106 CronetEngine engine, String url, int expectedStatusCode) { |
| 1097 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1107 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1098 UrlRequest request = | 1108 UrlRequest request = |
| 1099 new UrlRequest.Builder(url, callback, callback.getExecutor(), en
gine).build(); | 1109 engine.newUrlRequestBuilder(url, callback, callback.getExecutor(
)).build(); |
| 1100 request.start(); | 1110 request.start(); |
| 1101 callback.blockForDone(); | 1111 callback.blockForDone(); |
| 1102 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod
e()); | 1112 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod
e()); |
| 1103 } | 1113 } |
| 1104 | 1114 |
| 1105 private CronetTestFramework startCronetTestFrameworkWithCacheEnabled(int cac
heType) | 1115 private CronetTestFramework startCronetTestFrameworkWithCacheEnabled(int cac
heType) |
| 1106 throws Exception { | 1116 throws Exception { |
| 1107 String cacheTypeString = ""; | 1117 String cacheTypeString = ""; |
| 1108 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { | 1118 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { |
| 1109 cacheTypeString = CronetTestFramework.CACHE_DISK; | 1119 cacheTypeString = CronetTestFramework.CACHE_DISK; |
| 1110 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { | 1120 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { |
| 1111 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; | 1121 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; |
| 1112 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { | 1122 } else if (cacheType == HTTP_CACHE_IN_MEMORY) { |
| 1113 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; | 1123 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; |
| 1114 } | 1124 } |
| 1115 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri
ng}; | 1125 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri
ng}; |
| 1116 CronetTestFramework testFramework = | 1126 CronetTestFramework testFramework = |
| 1117 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL
ineArgs); | 1127 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL
ineArgs); |
| 1118 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 1128 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 1119 return testFramework; | 1129 return testFramework; |
| 1120 } | 1130 } |
| 1121 | 1131 |
| 1122 private void checkRequestCaching(CronetEngine engine, String url, boolean ex
pectCached) { | 1132 private void checkRequestCaching(CronetEngine engine, String url, boolean ex
pectCached) { |
| 1123 checkRequestCaching(engine, url, expectCached, false); | 1133 checkRequestCaching(engine, url, expectCached, false); |
| 1124 } | 1134 } |
| 1125 | 1135 |
| 1126 private void checkRequestCaching( | 1136 private void checkRequestCaching( |
| 1127 CronetEngine engine, String url, boolean expectCached, boolean disab
leCache) { | 1137 CronetEngine engine, String url, boolean expectCached, boolean disab
leCache) { |
| 1128 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1138 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1129 UrlRequest.Builder urlRequestBuilder = | 1139 UrlRequest.Builder urlRequestBuilder = |
| 1130 new UrlRequest.Builder(url, callback, callback.getExecutor(), en
gine); | 1140 engine.newUrlRequestBuilder(url, callback, callback.getExecutor(
)); |
| 1131 if (disableCache) { | 1141 if (disableCache) { |
| 1132 urlRequestBuilder.disableCache(); | 1142 urlRequestBuilder.disableCache(); |
| 1133 } | 1143 } |
| 1134 urlRequestBuilder.build().start(); | 1144 urlRequestBuilder.build().start(); |
| 1135 callback.blockForDone(); | 1145 callback.blockForDone(); |
| 1136 assertEquals(expectCached, callback.mResponseInfo.wasCached()); | 1146 assertEquals(expectCached, callback.mResponseInfo.wasCached()); |
| 1137 assertEquals("this is a cacheable file\n", callback.mResponseAsString); | 1147 assertEquals("this is a cacheable file\n", callback.mResponseAsString); |
| 1138 } | 1148 } |
| 1139 | 1149 |
| 1140 @SmallTest | 1150 @SmallTest |
| 1141 @Feature({"Cronet"}) | 1151 @Feature({"Cronet"}) |
| 1142 @OnlyRunNativeCronet | 1152 @OnlyRunNativeCronet |
| 1143 public void testEnableHttpCacheDisabled() throws Exception { | 1153 public void testEnableHttpCacheDisabled() throws Exception { |
| 1144 final CronetTestFramework testFramework = | 1154 final CronetTestFramework testFramework = |
| 1145 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT
TP_CACHE_DISABLED); | 1155 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT
TP_CACHE_DISABLED); |
| 1146 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 1156 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 1147 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1157 checkRequestCaching(testFramework.mCronetEngine, url, false); |
| 1148 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1158 checkRequestCaching(testFramework.mCronetEngine, url, false); |
| 1149 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1159 checkRequestCaching(testFramework.mCronetEngine, url, false); |
| 1150 } | 1160 } |
| 1151 | 1161 |
| 1152 @SmallTest | 1162 @SmallTest |
| 1153 @Feature({"Cronet"}) | 1163 @Feature({"Cronet"}) |
| 1154 public void testEnableHttpCacheInMemory() throws Exception { | 1164 public void testEnableHttpCacheInMemory() throws Exception { |
| 1155 final CronetTestFramework testFramework = | 1165 final CronetTestFramework testFramework = |
| 1156 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT
TP_CACHE_IN_MEMORY); | 1166 startCronetTestFrameworkWithCacheEnabled(HTTP_CACHE_IN_MEMORY); |
| 1157 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 1167 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 1158 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1168 checkRequestCaching(testFramework.mCronetEngine, url, false); |
| 1159 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1169 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1160 NativeTestServer.shutdownNativeTestServer(); | 1170 NativeTestServer.shutdownNativeTestServer(); |
| 1161 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1171 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1162 } | 1172 } |
| 1163 | 1173 |
| 1164 @SmallTest | 1174 @SmallTest |
| 1165 @Feature({"Cronet"}) | 1175 @Feature({"Cronet"}) |
| 1166 public void testEnableHttpCacheDisk() throws Exception { | 1176 public void testEnableHttpCacheDisk() throws Exception { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1199 // When cache is enabled, the second request is cached. | 1209 // When cache is enabled, the second request is cached. |
| 1200 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di
sable cache */); | 1210 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di
sable cache */); |
| 1201 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1211 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1202 | 1212 |
| 1203 // Shut down the server, next request should have a cached response. | 1213 // Shut down the server, next request should have a cached response. |
| 1204 NativeTestServer.shutdownNativeTestServer(); | 1214 NativeTestServer.shutdownNativeTestServer(); |
| 1205 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1215 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1206 | 1216 |
| 1207 // Cache is disabled after server is shut down, request should fail. | 1217 // Cache is disabled after server is shut down, request should fail. |
| 1208 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1218 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1209 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 1219 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 1210 url, callback, callback.getExecutor(), testFramework.mCronetEngi
ne); | 1220 url, callback, callback.getExecutor()); |
| 1211 urlRequestBuilder.disableCache(); | 1221 urlRequestBuilder.disableCache(); |
| 1212 urlRequestBuilder.build().start(); | 1222 urlRequestBuilder.build().start(); |
| 1213 callback.blockForDone(); | 1223 callback.blockForDone(); |
| 1214 assertNotNull(callback.mError); | 1224 assertNotNull(callback.mError); |
| 1215 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", | 1225 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", |
| 1216 callback.mError.getMessage()); | 1226 callback.mError.getMessage()); |
| 1217 } | 1227 } |
| 1218 | 1228 |
| 1219 @SmallTest | 1229 @SmallTest |
| 1220 @Feature({"Cronet"}) | 1230 @Feature({"Cronet"}) |
| 1221 public void testEnableHttpCacheDiskNewEngine() throws Exception { | 1231 public void testEnableHttpCacheDiskNewEngine() throws Exception { |
| 1222 final CronetTestFramework testFramework = | 1232 final CronetTestFramework testFramework = |
| 1223 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT
TP_CACHE_DISK); | 1233 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT
TP_CACHE_DISK); |
| 1224 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 1234 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 1225 checkRequestCaching(testFramework.mCronetEngine, url, false); | 1235 checkRequestCaching(testFramework.mCronetEngine, url, false); |
| 1226 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1236 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1227 NativeTestServer.shutdownNativeTestServer(); | 1237 NativeTestServer.shutdownNativeTestServer(); |
| 1228 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1238 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1229 | 1239 |
| 1230 // Shutdown original context and create another that uses the same cache
. | 1240 // Shutdown original context and create another that uses the same cache
. |
| 1231 testFramework.mCronetEngine.shutdown(); | 1241 testFramework.mCronetEngine.shutdown(); |
| 1232 testFramework.mCronetEngine = testFramework.getCronetEngineBuilder().bui
ld(); | 1242 testFramework.mCronetEngine = |
| 1243 (CronetEngineBase) testFramework.getCronetEngineBuilder().build(
); |
| 1233 checkRequestCaching(testFramework.mCronetEngine, url, true); | 1244 checkRequestCaching(testFramework.mCronetEngine, url, true); |
| 1234 } | 1245 } |
| 1235 | 1246 |
| 1236 @SmallTest | 1247 @SmallTest |
| 1237 @Feature({"Cronet"}) | 1248 @Feature({"Cronet"}) |
| 1238 public void testInitEngineAndStartRequest() { | 1249 public void testInitEngineAndStartRequest() { |
| 1239 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | 1250 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 1240 | 1251 |
| 1241 // Immediately make a request after initializing the engine. | 1252 // Immediately make a request after initializing the engine. |
| 1242 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 1253 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 1243 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1254 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1244 UrlRequest.Builder urlRequestBuilder = | 1255 UrlRequest.Builder urlRequestBuilder = |
| 1245 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 1256 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 1246 urlRequestBuilder.build().start(); | 1257 urlRequestBuilder.build().start(); |
| 1247 callback.blockForDone(); | 1258 callback.blockForDone(); |
| 1248 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1259 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1249 } | 1260 } |
| 1250 | 1261 |
| 1251 @SmallTest | 1262 @SmallTest |
| 1252 @Feature({"Cronet"}) | 1263 @Feature({"Cronet"}) |
| 1253 public void testEmptyGetCertVerifierData() { | 1264 public void testEmptyGetCertVerifierData() { |
| 1254 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | 1265 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 1255 | 1266 |
| 1256 // Immediately make a request after initializing the engine. | 1267 // Immediately make a request after initializing the engine. |
| 1257 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 1268 ExperimentalCronetEngine cronetEngine = testFramework.initCronetEngine()
; |
| 1258 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1269 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1259 UrlRequest.Builder urlRequestBuilder = | 1270 UrlRequest.Builder urlRequestBuilder = |
| 1260 new UrlRequest.Builder(mUrl, callback, callback.getExecutor(), c
ronetEngine); | 1271 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 1261 urlRequestBuilder.build().start(); | 1272 urlRequestBuilder.build().start(); |
| 1262 callback.blockForDone(); | 1273 callback.blockForDone(); |
| 1263 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1274 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1264 | 1275 |
| 1265 try { | 1276 try { |
| 1266 cronetEngine.getCertVerifierData(-1); | 1277 cronetEngine.getCertVerifierData(-1); |
| 1267 fail("Should throw an exception"); | 1278 fail("Should throw an exception"); |
| 1268 } catch (Exception e) { | 1279 } catch (Exception e) { |
| 1269 assertEquals("timeout must be a positive value", e.getMessage()); | 1280 assertEquals("timeout must be a positive value", e.getMessage()); |
| 1270 } | 1281 } |
| 1271 // Because mUrl is http, getCertVerifierData() will return empty data. | 1282 // Because mUrl is http, getCertVerifierData() will return empty data. |
| 1272 String data = cronetEngine.getCertVerifierData(100); | 1283 String data = cronetEngine.getCertVerifierData(100); |
| 1273 assertTrue(data.isEmpty()); | 1284 assertTrue(data.isEmpty()); |
| 1274 } | 1285 } |
| 1275 | 1286 |
| 1276 @SmallTest | 1287 @SmallTest |
| 1277 @Feature({"Cronet"}) | 1288 @Feature({"Cronet"}) |
| 1278 public void testInitEngineStartTwoRequests() throws Exception { | 1289 public void testInitEngineStartTwoRequests() throws Exception { |
| 1279 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | 1290 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 1280 | 1291 |
| 1281 // Make two requests after initializing the context. | 1292 // Make two requests after initializing the context. |
| 1282 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 1293 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 1283 int[] statusCodes = {0, 0}; | 1294 int[] statusCodes = {0, 0}; |
| 1284 String[] urls = {mUrl, mUrl404}; | 1295 String[] urls = {mUrl, mUrl404}; |
| 1285 for (int i = 0; i < 2; i++) { | 1296 for (int i = 0; i < 2; i++) { |
| 1286 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1297 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1287 UrlRequest.Builder urlRequestBuilder = | 1298 UrlRequest.Builder urlRequestBuilder = |
| 1288 new UrlRequest.Builder(urls[i], callback, callback.getExecut
or(), cronetEngine); | 1299 cronetEngine.newUrlRequestBuilder(urls[i], callback, callbac
k.getExecutor()); |
| 1289 urlRequestBuilder.build().start(); | 1300 urlRequestBuilder.build().start(); |
| 1290 callback.blockForDone(); | 1301 callback.blockForDone(); |
| 1291 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); | 1302 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); |
| 1292 } | 1303 } |
| 1293 assertEquals(200, statusCodes[0]); | 1304 assertEquals(200, statusCodes[0]); |
| 1294 assertEquals(404, statusCodes[1]); | 1305 assertEquals(404, statusCodes[1]); |
| 1295 } | 1306 } |
| 1296 | 1307 |
| 1297 @SmallTest | 1308 @SmallTest |
| 1298 @Feature({"Cronet"}) | 1309 @Feature({"Cronet"}) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1331 } | 1342 } |
| 1332 | 1343 |
| 1333 @SmallTest | 1344 @SmallTest |
| 1334 @Feature({"Cronet"}) | 1345 @Feature({"Cronet"}) |
| 1335 public void testInitDifferentEngines() throws Exception { | 1346 public void testInitDifferentEngines() throws Exception { |
| 1336 // Test that concurrently instantiating Cronet context's upon various | 1347 // Test that concurrently instantiating Cronet context's upon various |
| 1337 // different versions of the same Android Context does not cause crashes | 1348 // different versions of the same Android Context does not cause crashes |
| 1338 // like crbug.com/453845 | 1349 // like crbug.com/453845 |
| 1339 final CronetTestFramework testFramework = startCronetTestFramework(); | 1350 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 1340 CronetEngine firstEngine = | 1351 CronetEngine firstEngine = |
| 1341 new CronetUrlRequestContext(testFramework.createCronetEngineBuil
der(getContext())); | 1352 new CronetUrlRequestContext(CronetTestUtil.getCronetEngineBuilde
rImpl( |
| 1353 testFramework.createCronetEngineBuilder(getContext()))); |
| 1342 CronetEngine secondEngine = new CronetUrlRequestContext( | 1354 CronetEngine secondEngine = new CronetUrlRequestContext( |
| 1343 testFramework.createCronetEngineBuilder(getContext().getApplicat
ionContext())); | 1355 CronetTestUtil.getCronetEngineBuilderImpl(testFramework.createCr
onetEngineBuilder( |
| 1344 CronetEngine thirdEngine = new CronetUrlRequestContext( | 1356 getContext().getApplicationContext()))); |
| 1345 testFramework.createCronetEngineBuilder(new ContextWrapper(getCo
ntext()))); | 1357 CronetEngine thirdEngine = |
| 1358 new CronetUrlRequestContext(CronetTestUtil.getCronetEngineBuilde
rImpl( |
| 1359 testFramework.createCronetEngineBuilder(new ContextWrapp
er(getContext())))); |
| 1346 firstEngine.shutdown(); | 1360 firstEngine.shutdown(); |
| 1347 secondEngine.shutdown(); | 1361 secondEngine.shutdown(); |
| 1348 thirdEngine.shutdown(); | 1362 thirdEngine.shutdown(); |
| 1349 } | 1363 } |
| 1350 | 1364 |
| 1351 @SmallTest | 1365 @SmallTest |
| 1352 @Feature({"Cronet"}) | 1366 @Feature({"Cronet"}) |
| 1353 public void testGetGlobalMetricsDeltas() throws Exception { | 1367 public void testGetGlobalMetricsDeltas() throws Exception { |
| 1354 final CronetTestFramework testFramework = startCronetTestFramework(); | 1368 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 1355 | 1369 |
| 1356 byte delta1[] = testFramework.mCronetEngine.getGlobalMetricsDeltas(); | 1370 byte delta1[] = testFramework.mCronetEngine.getGlobalMetricsDeltas(); |
| 1357 | 1371 |
| 1358 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1372 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1359 UrlRequest.Builder builder = new UrlRequest.Builder( | 1373 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( |
| 1360 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng
ine); | 1374 mUrl, callback, callback.getExecutor()); |
| 1361 builder.build().start(); | 1375 builder.build().start(); |
| 1362 callback.blockForDone(); | 1376 callback.blockForDone(); |
| 1363 byte delta2[] = testFramework.mCronetEngine.getGlobalMetricsDeltas(); | 1377 byte delta2[] = testFramework.mCronetEngine.getGlobalMetricsDeltas(); |
| 1364 assertTrue(delta2.length != 0); | 1378 assertTrue(delta2.length != 0); |
| 1365 assertFalse(Arrays.equals(delta1, delta2)); | 1379 assertFalse(Arrays.equals(delta1, delta2)); |
| 1366 } | 1380 } |
| 1367 | 1381 |
| 1368 @SmallTest | 1382 @SmallTest |
| 1369 @Feature({"Cronet"}) | 1383 @Feature({"Cronet"}) |
| 1370 public void testCronetEngineBuilderConfig() throws Exception { | 1384 public void testCronetEngineBuilderConfig() throws Exception { |
| 1371 // This is to prompt load of native library. | 1385 // This is to prompt load of native library. |
| 1372 startCronetTestFramework(); | 1386 startCronetTestFramework(); |
| 1373 // Verify CronetEngine.Builder config is passed down accurately to nativ
e code. | 1387 // Verify CronetEngine.Builder config is passed down accurately to nativ
e code. |
| 1374 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 1388 CronetEngineBuilderImpl builder = new CronetEngineBuilderImpl(getContext
()); |
| 1375 builder.enableHttp2(false); | 1389 builder.enableHttp2(false); |
| 1376 builder.enableQuic(true); | 1390 builder.enableQuic(true); |
| 1377 builder.enableSdch(true); | 1391 builder.enableSdch(true); |
| 1378 builder.addQuicHint("example.com", 12, 34); | 1392 builder.addQuicHint("example.com", 12, 34); |
| 1379 builder.setCertVerifierData("test_cert_verifier_data"); | 1393 builder.setCertVerifierData("test_cert_verifier_data"); |
| 1380 builder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 54321
); | 1394 builder.enableHttpCache(HTTP_CACHE_IN_MEMORY, 54321); |
| 1381 builder.enableDataReductionProxy("abcd"); | 1395 builder.enableDataReductionProxy("abcd"); |
| 1382 builder.setUserAgent("efgh"); | 1396 builder.setUserAgent("efgh"); |
| 1383 builder.setExperimentalOptions("ijkl"); | 1397 builder.setExperimentalOptions("ijkl"); |
| 1384 builder.setDataReductionProxyOptions("mnop", "qrst", "uvwx"); | 1398 builder.setDataReductionProxyOptions("mnop", "qrst", "uvwx"); |
| 1385 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; | 1399 builder.setStoragePath(CronetTestFramework.getTestStorage(getContext()))
; |
| 1386 builder.enablePublicKeyPinningBypassForLocalTrustAnchors(false); | 1400 builder.enablePublicKeyPinningBypassForLocalTrustAnchors(false); |
| 1387 nativeVerifyUrlRequestContextConfig( | 1401 nativeVerifyUrlRequestContextConfig( |
| 1388 CronetUrlRequestContext.createNativeUrlRequestContextConfig(getC
ontext(), builder), | 1402 CronetUrlRequestContext.createNativeUrlRequestContextConfig(buil
der), |
| 1389 CronetTestFramework.getTestStorage(getContext())); | 1403 CronetTestFramework.getTestStorage(getContext())); |
| 1390 } | 1404 } |
| 1391 | 1405 |
| 1392 // Verifies that CronetEngine.Builder config from testCronetEngineBuilderCon
fig() is properly | 1406 // Verifies that CronetEngine.Builder config from testCronetEngineBuilderCon
fig() is properly |
| 1393 // translated to a native UrlRequestContextConfig. | 1407 // translated to a native UrlRequestContextConfig. |
| 1394 private static native void nativeVerifyUrlRequestContextConfig(long config,
String storagePath); | 1408 private static native void nativeVerifyUrlRequestContextConfig(long config,
String storagePath); |
| 1395 | 1409 |
| 1396 private static class TestBadLibraryLoader extends CronetEngine.Builder.Libra
ryLoader { | 1410 private static class TestBadLibraryLoader extends CronetEngine.Builder.Libra
ryLoader { |
| 1397 private boolean mWasCalled = false; | 1411 private boolean mWasCalled = false; |
| 1398 | 1412 |
| 1399 public void loadLibrary(String libName) { | 1413 public void loadLibrary(String libName) { |
| 1400 // Report that this method was called, but don't load the library | 1414 // Report that this method was called, but don't load the library |
| 1401 mWasCalled = true; | 1415 mWasCalled = true; |
| 1402 } | 1416 } |
| 1403 | 1417 |
| 1404 boolean wasCalled() { | 1418 boolean wasCalled() { |
| 1405 return mWasCalled; | 1419 return mWasCalled; |
| 1406 } | 1420 } |
| 1407 } | 1421 } |
| 1408 | 1422 |
| 1409 @SmallTest | 1423 @SmallTest |
| 1410 @Feature({"Cronet"}) | 1424 @Feature({"Cronet"}) |
| 1411 @OnlyRunNativeCronet | 1425 @OnlyRunNativeCronet |
| 1412 public void testSkipLibraryLoading() throws Exception { | 1426 public void testSkipLibraryLoading() throws Exception { |
| 1413 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 1427 CronetEngineBuilderImpl builder = new CronetEngineBuilderImpl(getContext
()); |
| 1414 TestBadLibraryLoader loader = new TestBadLibraryLoader(); | 1428 TestBadLibraryLoader loader = new TestBadLibraryLoader(); |
| 1415 builder.setLibraryLoader(loader).setLibraryName("cronet_tests"); | 1429 builder.setLibraryLoader(loader).setLibraryName("cronet_tests"); |
| 1416 try { | 1430 try { |
| 1417 // ensureInitialized() calls native code to check the version right
after library load | 1431 // ensureInitialized() calls native code to check the version right
after library load |
| 1418 // and will error with the message below if library loading was skip
ped | 1432 // and will error with the message below if library loading was skip
ped |
| 1419 CronetLibraryLoader.ensureInitialized(getContext(), builder); | 1433 CronetLibraryLoader.ensureInitialized(getContext(), builder); |
| 1420 fail("Native library should not be loaded"); | 1434 fail("Native library should not be loaded"); |
| 1421 } catch (UnsatisfiedLinkError e) { | 1435 } catch (UnsatisfiedLinkError e) { |
| 1422 assertTrue(loader.wasCalled()); | 1436 assertTrue(loader.wasCalled()); |
| 1423 } | 1437 } |
| 1424 } | 1438 } |
| 1425 | 1439 |
| 1426 // Creates a CronetEngine on another thread and then one on the main thread.
This shouldn't | 1440 // Creates a CronetEngine on another thread and then one on the main thread.
This shouldn't |
| 1427 // crash. | 1441 // crash. |
| 1428 @SmallTest | 1442 @SmallTest |
| 1429 @Feature({"Cronet"}) | 1443 @Feature({"Cronet"}) |
| 1430 public void testThreadedStartup() throws Exception { | 1444 public void testThreadedStartup() throws Exception { |
| 1431 final ConditionVariable otherThreadDone = new ConditionVariable(); | 1445 final ConditionVariable otherThreadDone = new ConditionVariable(); |
| 1432 final ConditionVariable uiThreadDone = new ConditionVariable(); | 1446 final ConditionVariable uiThreadDone = new ConditionVariable(); |
| 1433 new Handler(Looper.getMainLooper()).post(new Runnable() { | 1447 new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 1434 public void run() { | 1448 public void run() { |
| 1435 final CronetEngine.Builder builder = | 1449 final ExperimentalCronetEngine.Builder builder = |
| 1436 new CronetEngine.Builder(getContext()).setLibraryName("c
ronet_tests"); | 1450 new ExperimentalCronetEngine.Builder(getContext()); |
| 1451 CronetTestUtil.setLibraryName(builder, "cronet_tests"); |
| 1437 new Thread() { | 1452 new Thread() { |
| 1438 public void run() { | 1453 public void run() { |
| 1439 CronetEngine cronetEngine = builder.build(); | 1454 CronetEngine cronetEngine = builder.build(); |
| 1440 otherThreadDone.open(); | 1455 otherThreadDone.open(); |
| 1441 cronetEngine.shutdown(); | 1456 cronetEngine.shutdown(); |
| 1442 } | 1457 } |
| 1443 }.start(); | 1458 }.start(); |
| 1444 otherThreadDone.block(); | 1459 otherThreadDone.block(); |
| 1445 builder.build().shutdown(); | 1460 builder.build().shutdown(); |
| 1446 uiThreadDone.open(); | 1461 uiThreadDone.open(); |
| 1447 } | 1462 } |
| 1448 }); | 1463 }); |
| 1449 assertTrue(uiThreadDone.block(1000)); | 1464 assertTrue(uiThreadDone.block(1000)); |
| 1450 } | 1465 } |
| 1451 | 1466 |
| 1452 @SmallTest | 1467 @SmallTest |
| 1453 @Feature({"Cronet"}) | 1468 @Feature({"Cronet"}) |
| 1454 public void testHostResolverRules() throws Exception { | 1469 public void testHostResolverRules() throws Exception { |
| 1455 String resolverTestHostname = "some-weird-hostname"; | 1470 String resolverTestHostname = "some-weird-hostname"; |
| 1456 URL testUrl = new URL(mUrl); | 1471 URL testUrl = new URL(mUrl); |
| 1457 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC
ontext()); | 1472 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 1473 new ExperimentalCronetEngine.Builder(getContext()); |
| 1458 JSONObject hostResolverRules = new JSONObject().put( | 1474 JSONObject hostResolverRules = new JSONObject().put( |
| 1459 "host_resolver_rules", "MAP " + resolverTestHostname + " " + tes
tUrl.getHost()); | 1475 "host_resolver_rules", "MAP " + resolverTestHostname + " " + tes
tUrl.getHost()); |
| 1460 JSONObject experimentalOptions = | 1476 JSONObject experimentalOptions = |
| 1461 new JSONObject().put("HostResolverRules", hostResolverRules); | 1477 new JSONObject().put("HostResolverRules", hostResolverRules); |
| 1462 cronetEngineBuilder.setExperimentalOptions(experimentalOptions.toString(
)); | 1478 cronetEngineBuilder.setExperimentalOptions(experimentalOptions.toString(
)); |
| 1463 | 1479 |
| 1464 final CronetTestFramework testFramework = | 1480 final CronetTestFramework testFramework = |
| 1465 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, cron
etEngineBuilder); | 1481 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, cron
etEngineBuilder); |
| 1466 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 1482 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 1467 URL requestUrl = | 1483 URL requestUrl = |
| 1468 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl
.getFile()); | 1484 new URL("http", resolverTestHostname, testUrl.getPort(), testUrl
.getFile()); |
| 1469 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(requestUrl
.toString(), | 1485 UrlRequest.Builder urlRequestBuilder = testFramework.mCronetEngine.newUr
lRequestBuilder( |
| 1470 callback, callback.getExecutor(), testFramework.mCronetEngine); | 1486 requestUrl.toString(), callback, callback.getExecutor()); |
| 1471 urlRequestBuilder.build().start(); | 1487 urlRequestBuilder.build().start(); |
| 1472 callback.blockForDone(); | 1488 callback.blockForDone(); |
| 1473 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 1489 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 1474 } | 1490 } |
| 1475 } | 1491 } |
| OLD | NEW |