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