Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java

Issue 2254043002: Fix flaky tests in CronetUrlRequestContextTest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed mTestFramework member variable Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.chromium.base.FileUtils; 15 import org.chromium.base.FileUtils;
16 import org.chromium.base.Log;
17 import org.chromium.base.PathUtils; 16 import org.chromium.base.PathUtils;
18 import org.chromium.base.annotations.JNINamespace; 17 import org.chromium.base.annotations.JNINamespace;
19 import org.chromium.base.test.util.Feature; 18 import org.chromium.base.test.util.Feature;
20 import org.chromium.net.TestUrlRequestCallback.ResponseStep; 19 import org.chromium.net.TestUrlRequestCallback.ResponseStep;
21 import org.chromium.net.impl.CronetLibraryLoader; 20 import org.chromium.net.impl.CronetLibraryLoader;
22 import org.chromium.net.impl.CronetUrlRequestContext; 21 import org.chromium.net.impl.CronetUrlRequestContext;
23 import org.chromium.net.test.EmbeddedTestServer; 22 import org.chromium.net.test.EmbeddedTestServer;
24 23
25 import java.io.BufferedReader; 24 import java.io.BufferedReader;
26 import java.io.File; 25 import java.io.File;
27 import java.io.FileReader; 26 import java.io.FileReader;
28 import java.util.Arrays; 27 import java.util.Arrays;
29 import java.util.LinkedList; 28 import java.util.LinkedList;
30 import java.util.NoSuchElementException; 29 import java.util.NoSuchElementException;
31 import java.util.concurrent.Executor; 30 import java.util.concurrent.Executor;
32 import java.util.concurrent.Executors; 31 import java.util.concurrent.Executors;
33 import java.util.concurrent.ThreadFactory; 32 import java.util.concurrent.ThreadFactory;
34 33
35 /** 34 /**
36 * Test CronetEngine. 35 * Test CronetEngine.
37 */ 36 */
38 @JNINamespace("cronet") 37 @JNINamespace("cronet")
39 public class CronetUrlRequestContextTest extends CronetTestBase { 38 public class CronetUrlRequestContextTest extends CronetTestBase {
40 // URLs used for tests. 39 // URLs used for tests.
41 private static final String MOCK_CRONET_TEST_FAILED_URL = 40 private static final String MOCK_CRONET_TEST_FAILED_URL =
42 "http://mock.failed.request/-2"; 41 "http://mock.failed.request/-2";
43 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL =
44 "http://mock.http/success.txt"; 43 "http://mock.http/success.txt";
45 private static final String TAG = "RequestContextTest";
46 private static final int MAX_FILE_SIZE = 1000000000; 44 private static final int MAX_FILE_SIZE = 1000000000;
47 private static final int NUM_EVENT_FILES = 10; 45 private static final int NUM_EVENT_FILES = 10;
48 46
49 private EmbeddedTestServer mTestServer; 47 private EmbeddedTestServer mTestServer;
50 private String mUrl; 48 private String mUrl;
51 private String mUrl404; 49 private String mUrl404;
52 private String mUrl500; 50 private String mUrl500;
53 CronetTestFramework mTestFramework;
54 51
55 // Thread on which network quality listeners should be notified. 52 // Thread on which network quality listeners should be notified.
56 private Thread mNetworkQualityThread; 53 private Thread mNetworkQualityThread;
57 54
58 @Override 55 @Override
59 protected void setUp() throws Exception { 56 protected void setUp() throws Exception {
60 super.setUp(); 57 super.setUp();
61 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( )); 58 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext( ));
62 mUrl = mTestServer.getURL("/echo?status=200"); 59 mUrl = mTestServer.getURL("/echo?status=200");
63 mUrl404 = mTestServer.getURL("/echo?status=404"); 60 mUrl404 = mTestServer.getURL("/echo?status=404");
(...skipping 29 matching lines...) Expand all
93 new UrlRequest.Builder(mUrl, mCallback, mCallback.getExecuto r(), cronetEngine); 90 new UrlRequest.Builder(mUrl, mCallback, mCallback.getExecuto r(), cronetEngine);
94 urlRequestBuilder.build().start(); 91 urlRequestBuilder.build().start();
95 mCallback.blockForDone(); 92 mCallback.blockForDone();
96 } 93 }
97 } 94 }
98 95
99 /** 96 /**
100 * Callback that shutdowns the request context when request has succeeded 97 * Callback that shutdowns the request context when request has succeeded
101 * or failed. 98 * or failed.
102 */ 99 */
103 class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { 100 static class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback {
101 private final CronetEngine mCronetEngine;
102 private final ConditionVariable mCallbackCompletionBlock = new Condition Variable();
103
104 ShutdownTestUrlRequestCallback(CronetEngine cronetEngine) {
105 mCronetEngine = cronetEngine;
106 }
107
104 @Override 108 @Override
105 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { 109 public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
106 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed.
107 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() has started ");
108 super.onSucceeded(request, info); 110 super.onSucceeded(request, info);
109 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() before call ing shutdown"); 111 mCronetEngine.shutdown();
110 mTestFramework.mCronetEngine.shutdown(); 112 mCallbackCompletionBlock.open();
111 Log.d(TAG, "ShutdownTestUrlRequestCallback.hasFinished() has finishe d");
112 } 113 }
113 114
114 @Override 115 @Override
115 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) { 116 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) {
116 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed.
117 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has started");
118 super.onFailed(request, info, error); 117 super.onFailed(request, info, error);
119 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() before calling shutdown"); 118 mCronetEngine.shutdown();
120 mTestFramework.mCronetEngine.shutdown(); 119 mCallbackCompletionBlock.open();
121 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has finished." ); 120 }
121
122 void blockForCallbackToComplete() {
123 // Wait for request completion callback.
pauljensen 2016/08/18 16:39:55 move this comment up a line
kapishnikov 2016/08/18 16:55:06 Done.
124 mCallbackCompletionBlock.block();
122 } 125 }
123 } 126 }
124 127
125 static class TestExecutor implements Executor { 128 static class TestExecutor implements Executor {
126 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); 129 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> ();
127 130
128 @Override 131 @Override
129 public void execute(Runnable task) { 132 public void execute(Runnable task) {
130 mTaskQueue.add(task); 133 mTaskQueue.add(task);
131 } 134 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 @SuppressWarnings("deprecation") 170 @SuppressWarnings("deprecation")
168 public void testConfigUserAgent() throws Exception { 171 public void testConfigUserAgent() throws Exception {
169 String userAgentName = "User-Agent"; 172 String userAgentName = "User-Agent";
170 String userAgentValue = "User-Agent-Value"; 173 String userAgentValue = "User-Agent-Value";
171 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext()); 174 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext());
172 if (testingJavaImpl()) { 175 if (testingJavaImpl()) {
173 cronetEngineBuilder.enableLegacyMode(true); 176 cronetEngineBuilder.enableLegacyMode(true);
174 } 177 }
175 cronetEngineBuilder.setUserAgent(userAgentValue); 178 cronetEngineBuilder.setUserAgent(userAgentValue);
176 cronetEngineBuilder.setLibraryName("cronet_tests"); 179 cronetEngineBuilder.setLibraryName("cronet_tests");
177 mTestFramework = 180 final CronetTestFramework testFramework =
178 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(mUrl, cron etEngineBuilder); 181 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(mUrl, cron etEngineBuilder);
179 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re turns false if it's 182 NativeTestServer.shutdownNativeTestServer(); // startNativeTestServer re turns false if it's
180 // already running 183 // already running
181 assertTrue(NativeTestServer.startNativeTestServer(getContext())); 184 assertTrue(NativeTestServer.startNativeTestServer(getContext()));
182 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 185 TestUrlRequestCallback callback = new TestUrlRequestCallback();
183 UrlRequest.Builder urlRequestBuilder = 186 UrlRequest.Builder urlRequestBuilder =
184 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge ntName), callback, 187 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge ntName), callback,
185 callback.getExecutor(), mTestFramework.mCronetEngine); 188 callback.getExecutor(), testFramework.mCronetEngine);
186 urlRequestBuilder.build().start(); 189 urlRequestBuilder.build().start();
187 callback.blockForDone(); 190 callback.blockForDone();
188 assertEquals(userAgentValue, callback.mResponseAsString); 191 assertEquals(userAgentValue, callback.mResponseAsString);
189 } 192 }
190 193
191 @SmallTest 194 @SmallTest
192 @Feature({"Cronet"}) 195 @Feature({"Cronet"})
193 @OnlyRunNativeCronet 196 @OnlyRunNativeCronet
194 public void testDataReductionProxyEnabled() throws Exception { 197 public void testDataReductionProxyEnabled() throws Exception {
195 mTestFramework = startCronetTestFrameworkAndSkipLibraryInit(); 198 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit();
196 199
197 // Ensure native code is loaded before trying to start test server. 200 // Ensure native code is loaded before trying to start test server.
198 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu ild().shutdown(); 201 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu ild().shutdown();
199 202
200 assertTrue(NativeTestServer.startNativeTestServer(getContext())); 203 assertTrue(NativeTestServer.startNativeTestServer(getContext()));
201 if (!NativeTestServer.isDataReductionProxySupported()) { 204 if (!NativeTestServer.isDataReductionProxySupported()) {
202 return; 205 return;
203 } 206 }
204 String serverHostPort = NativeTestServer.getHostPort(); 207 String serverHostPort = NativeTestServer.getHostPort();
205 208
206 // Enable the Data Reduction Proxy and configure it to use the test 209 // Enable the Data Reduction Proxy and configure it to use the test
207 // server as its primary proxy, and to check successfully that this 210 // server as its primary proxy, and to check successfully that this
208 // proxy is OK to use. 211 // proxy is OK to use.
209 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext()); 212 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC ontext());
210 cronetEngineBuilder.enableDataReductionProxy("test-key"); 213 cronetEngineBuilder.enableDataReductionProxy("test-key");
211 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused .net:9999", 214 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused .net:9999",
212 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); 215 NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
213 cronetEngineBuilder.setLibraryName("cronet_tests"); 216 cronetEngineBuilder.setLibraryName("cronet_tests");
214 mTestFramework.mCronetEngine = cronetEngineBuilder.build(); 217 testFramework.mCronetEngine = cronetEngineBuilder.build();
215 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 218 TestUrlRequestCallback callback = new TestUrlRequestCallback();
216 219
217 // Construct and start a request that can only be returned by the test 220 // Construct and start a request that can only be returned by the test
218 // server. This request will fail if the configuration logic for the 221 // server. This request will fail if the configuration logic for the
219 // Data Reduction Proxy is not used. 222 // Data Reduction Proxy is not used.
220 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 223 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
221 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", callback, 224 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", callback,
222 callback.getExecutor(), mTestFramework.mCronetEngine); 225 callback.getExecutor(), testFramework.mCronetEngine);
223 urlRequestBuilder.build().start(); 226 urlRequestBuilder.build().start();
224 callback.blockForDone(); 227 callback.blockForDone();
225 228
226 // Verify that the request is successful and that the Data Reduction 229 // Verify that the request is successful and that the Data Reduction
227 // Proxy logic configured to use the test server as its proxy. 230 // Proxy logic configured to use the test server as its proxy.
228 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); 231 assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
229 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); 232 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer());
230 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt", 233 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt",
231 callback.mResponseInfo.getUrl()); 234 callback.mResponseInfo.getUrl());
232 } 235 }
233 236
234 @SmallTest 237 @SmallTest
235 @Feature({"Cronet"}) 238 @Feature({"Cronet"})
236 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion { 239 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion {
237 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context()); 240 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context());
238 mTestFramework = 241 final CronetTestFramework testFramework =
239 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder); 242 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder);
240 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); 243 Executor networkQualityExecutor = Executors.newSingleThreadExecutor();
241 TestNetworkQualityRttListener rttListener = 244 TestNetworkQualityRttListener rttListener =
242 new TestNetworkQualityRttListener(networkQualityExecutor); 245 new TestNetworkQualityRttListener(networkQualityExecutor);
243 TestNetworkQualityThroughputListener throughputListener = 246 TestNetworkQualityThroughputListener throughputListener =
244 new TestNetworkQualityThroughputListener(networkQualityExecutor, null); 247 new TestNetworkQualityThroughputListener(networkQualityExecutor, null);
245 try { 248 try {
246 mTestFramework.mCronetEngine.addRttListener(rttListener); 249 testFramework.mCronetEngine.addRttListener(rttListener);
247 fail("Should throw an exception."); 250 fail("Should throw an exception.");
248 } catch (IllegalStateException e) { 251 } catch (IllegalStateException e) {
249 } 252 }
250 try { 253 try {
251 mTestFramework.mCronetEngine.addThroughputListener(throughputListene r); 254 testFramework.mCronetEngine.addThroughputListener(throughputListener );
252 fail("Should throw an exception."); 255 fail("Should throw an exception.");
253 } catch (IllegalStateException e) { 256 } catch (IllegalStateException e) {
254 } 257 }
255 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 258 TestUrlRequestCallback callback = new TestUrlRequestCallback();
256 UrlRequest.Builder builder = new UrlRequest.Builder( 259 UrlRequest.Builder builder = new UrlRequest.Builder(
257 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 260 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
258 UrlRequest urlRequest = builder.build(); 261 UrlRequest urlRequest = builder.build();
259 262
260 urlRequest.start(); 263 urlRequest.start();
261 callback.blockForDone(); 264 callback.blockForDone();
262 assertEquals(0, rttListener.rttObservationCount()); 265 assertEquals(0, rttListener.rttObservationCount());
263 assertEquals(0, throughputListener.throughputObservationCount()); 266 assertEquals(0, throughputListener.throughputObservationCount());
264 mTestFramework.mCronetEngine.shutdown(); 267 testFramework.mCronetEngine.shutdown();
265 } 268 }
266 269
267 @SmallTest 270 @SmallTest
268 @Feature({"Cronet"}) 271 @Feature({"Cronet"})
269 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception { 272 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception {
270 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context()); 273 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context());
271 TestExecutor networkQualityExecutor = new TestExecutor(); 274 TestExecutor networkQualityExecutor = new TestExecutor();
272 TestNetworkQualityRttListener rttListener = 275 TestNetworkQualityRttListener rttListener =
273 new TestNetworkQualityRttListener(networkQualityExecutor); 276 new TestNetworkQualityRttListener(networkQualityExecutor);
274 mCronetEngineBuilder.enableNetworkQualityEstimator(true); 277 mCronetEngineBuilder.enableNetworkQualityEstimator(true);
275 mTestFramework = 278 final CronetTestFramework testFramework =
276 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder); 279 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder);
277 mTestFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting( true, true); 280 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t rue, true);
278 281
279 mTestFramework.mCronetEngine.addRttListener(rttListener); 282 testFramework.mCronetEngine.addRttListener(rttListener);
280 mTestFramework.mCronetEngine.removeRttListener(rttListener); 283 testFramework.mCronetEngine.removeRttListener(rttListener);
281 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 284 TestUrlRequestCallback callback = new TestUrlRequestCallback();
282 UrlRequest.Builder builder = new UrlRequest.Builder( 285 UrlRequest.Builder builder = new UrlRequest.Builder(
283 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 286 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
284 UrlRequest urlRequest = builder.build(); 287 UrlRequest urlRequest = builder.build();
285 urlRequest.start(); 288 urlRequest.start();
286 callback.blockForDone(); 289 callback.blockForDone();
287 networkQualityExecutor.runAllTasks(); 290 networkQualityExecutor.runAllTasks();
288 assertEquals(0, rttListener.rttObservationCount()); 291 assertEquals(0, rttListener.rttObservationCount());
289 mTestFramework.mCronetEngine.shutdown(); 292 testFramework.mCronetEngine.shutdown();
290 } 293 }
291 294
292 @SmallTest 295 @SmallTest
293 @Feature({"Cronet"}) 296 @Feature({"Cronet"})
294 public void testRealTimeNetworkQualityObservationsQuicDisabled() throws Exce ption { 297 public void testRealTimeNetworkQualityObservationsQuicDisabled() throws Exce ption {
295 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context()); 298 CronetEngine.Builder mCronetEngineBuilder = new CronetEngine.Builder(get Context());
296 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu torThreadFactory()); 299 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu torThreadFactory());
297 ConditionVariable waitForThroughput = new ConditionVariable(); 300 ConditionVariable waitForThroughput = new ConditionVariable();
298 TestNetworkQualityRttListener rttListener = 301 TestNetworkQualityRttListener rttListener =
299 new TestNetworkQualityRttListener(listenersExecutor); 302 new TestNetworkQualityRttListener(listenersExecutor);
300 TestNetworkQualityThroughputListener throughputListener = 303 TestNetworkQualityThroughputListener throughputListener =
301 new TestNetworkQualityThroughputListener(listenersExecutor, wait ForThroughput); 304 new TestNetworkQualityThroughputListener(listenersExecutor, wait ForThroughput);
302 mCronetEngineBuilder.enableHttp2(true).enableQuic(false).enableNetworkQu alityEstimator( 305 mCronetEngineBuilder.enableHttp2(true).enableQuic(false).enableNetworkQu alityEstimator(
303 true); 306 true);
304 mTestFramework = 307 final CronetTestFramework testFramework =
305 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder); 308 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro netEngineBuilder);
306 mTestFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting( true, true); 309 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t rue, true);
307 310
308 mTestFramework.mCronetEngine.addRttListener(rttListener); 311 testFramework.mCronetEngine.addRttListener(rttListener);
309 mTestFramework.mCronetEngine.addThroughputListener(throughputListener); 312 testFramework.mCronetEngine.addThroughputListener(throughputListener);
310 313
311 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 314 TestUrlRequestCallback callback = new TestUrlRequestCallback();
312 UrlRequest.Builder builder = new UrlRequest.Builder( 315 UrlRequest.Builder builder = new UrlRequest.Builder(
313 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 316 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
314 UrlRequest urlRequest = builder.build(); 317 UrlRequest urlRequest = builder.build();
315 urlRequest.start(); 318 urlRequest.start();
316 callback.blockForDone(); 319 callback.blockForDone();
317 320
318 // Throughput observation is posted to the network quality estimator on the network thread 321 // Throughput observation is posted to the network quality estimator on the network thread
319 // after the UrlRequest is completed. The observations are then eventual ly posted to 322 // after the UrlRequest is completed. The observations are then eventual ly posted to
320 // throughput listeners on the executor provided to network quality. 323 // throughput listeners on the executor provided to network quality.
321 waitForThroughput.block(); 324 waitForThroughput.block();
322 assertTrue(throughputListener.throughputObservationCount() > 0); 325 assertTrue(throughputListener.throughputObservationCount() > 0);
323 326
324 // Check RTT observation count after throughput observation has been rec eived. This ensures 327 // Check RTT observation count after throughput observation has been rec eived. This ensures
325 // that executor has finished posting the RTT observation to the RTT lis teners. 328 // that executor has finished posting the RTT observation to the RTT lis teners.
326 assertTrue(rttListener.rttObservationCount() > 0); 329 assertTrue(rttListener.rttObservationCount() > 0);
327 330
328 // NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST 331 // NETWORK_QUALITY_OBSERVATION_SOURCE_URL_REQUEST
329 assertTrue(rttListener.rttObservationCount(0) > 0); 332 assertTrue(rttListener.rttObservationCount(0) > 0);
330 333
331 // NETWORK_QUALITY_OBSERVATION_SOURCE_TCP 334 // NETWORK_QUALITY_OBSERVATION_SOURCE_TCP
332 assertTrue(rttListener.rttObservationCount(1) > 0); 335 assertTrue(rttListener.rttObservationCount(1) > 0);
333 336
334 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC 337 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC
335 assertEquals(0, rttListener.rttObservationCount(2)); 338 assertEquals(0, rttListener.rttObservationCount(2));
336 339
337 // Verify that the listeners were notified on the expected thread. 340 // Verify that the listeners were notified on the expected thread.
338 assertEquals(mNetworkQualityThread, rttListener.getThread()); 341 assertEquals(mNetworkQualityThread, rttListener.getThread());
339 assertEquals(mNetworkQualityThread, throughputListener.getThread()); 342 assertEquals(mNetworkQualityThread, throughputListener.getThread());
340 343
341 // Verify that effective connection type callback is received and 344 // Verify that effective connection type callback is received and
342 // effective connection type is correctly set. 345 // effective connection type is correctly set.
343 assertTrue(mTestFramework.mCronetEngine.getEffectiveConnectionType() 346 assertTrue(testFramework.mCronetEngine.getEffectiveConnectionType()
344 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN); 347 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
345 348
346 mTestFramework.mCronetEngine.shutdown(); 349 testFramework.mCronetEngine.shutdown();
347 } 350 }
348 351
349 @SmallTest 352 @SmallTest
350 @Feature({"Cronet"}) 353 @Feature({"Cronet"})
351 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 354 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972
352 @OnlyRunNativeCronet 355 @OnlyRunNativeCronet
353 public void testShutdown() throws Exception { 356 public void testShutdown() throws Exception {
354 Log.i(TAG, "testShutdown() has started"); 357 final CronetTestFramework testFramework = startCronetTestFramework();
355 mTestFramework = startCronetTestFramework(); 358 ShutdownTestUrlRequestCallback callback =
356 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); 359 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine);
357 // Block callback when response starts to verify that shutdown fails 360 // Block callback when response starts to verify that shutdown fails
358 // if there are active requests. 361 // if there are active requests.
359 callback.setAutoAdvance(false); 362 callback.setAutoAdvance(false);
360 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 363 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
361 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 364 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
362 UrlRequest urlRequest = urlRequestBuilder.build(); 365 UrlRequest urlRequest = urlRequestBuilder.build();
363 urlRequest.start(); 366 urlRequest.start();
364 try { 367 try {
365 mTestFramework.mCronetEngine.shutdown(); 368 testFramework.mCronetEngine.shutdown();
366 fail("Should throw an exception"); 369 fail("Should throw an exception");
367 } catch (Exception e) { 370 } catch (Exception e) {
368 assertEquals("Cannot shutdown with active requests.", 371 assertEquals("Cannot shutdown with active requests.",
369 e.getMessage()); 372 e.getMessage());
370 } 373 }
371 374
372 callback.waitForNextStep(); 375 callback.waitForNextStep();
373 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep); 376 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep);
374 try { 377 try {
375 mTestFramework.mCronetEngine.shutdown(); 378 testFramework.mCronetEngine.shutdown();
376 fail("Should throw an exception"); 379 fail("Should throw an exception");
377 } catch (Exception e) { 380 } catch (Exception e) {
378 assertEquals("Cannot shutdown with active requests.", 381 assertEquals("Cannot shutdown with active requests.",
379 e.getMessage()); 382 e.getMessage());
380 } 383 }
381 callback.startNextRead(urlRequest); 384 callback.startNextRead(urlRequest);
382 385
383 callback.waitForNextStep(); 386 callback.waitForNextStep();
384 assertEquals(ResponseStep.ON_READ_COMPLETED, callback.mResponseStep); 387 assertEquals(ResponseStep.ON_READ_COMPLETED, callback.mResponseStep);
385 try { 388 try {
386 mTestFramework.mCronetEngine.shutdown(); 389 testFramework.mCronetEngine.shutdown();
387 fail("Should throw an exception"); 390 fail("Should throw an exception");
388 } catch (Exception e) { 391 } catch (Exception e) {
389 assertEquals("Cannot shutdown with active requests.", 392 assertEquals("Cannot shutdown with active requests.",
390 e.getMessage()); 393 e.getMessage());
391 } 394 }
392 395
393 // May not have read all the data, in theory. Just enable auto-advance 396 // May not have read all the data, in theory. Just enable auto-advance
394 // and finish the request. 397 // and finish the request.
395 callback.setAutoAdvance(true); 398 callback.setAutoAdvance(true);
396 callback.startNextRead(urlRequest); 399 callback.startNextRead(urlRequest);
397 callback.blockForDone(); 400 callback.blockForDone();
398 // TODO: Remove sleep when http://crbug.com/596929 is fixed. 401 callback.blockForCallbackToComplete();
399 // The sleep gives the thread that shuts down the engine time to complet e. 402 callback.shutdownExecutor();
400 // See http://crbug.com/596929
401 Thread.sleep(3000);
402 Log.i(TAG, "testShutdown() has finished");
403 } 403 }
404 404
405 @SmallTest 405 @SmallTest
406 @Feature({"Cronet"}) 406 @Feature({"Cronet"})
407 @OnlyRunNativeCronet 407 @OnlyRunNativeCronet
408 public void testShutdownDuringInit() throws Exception { 408 public void testShutdownDuringInit() throws Exception {
409 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); 409 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit();
410 final ConditionVariable block = new ConditionVariable(false); 410 final ConditionVariable block = new ConditionVariable(false);
411 411
412 // Post a task to main thread to block until shutdown is called to test 412 // Post a task to main thread to block until shutdown is called to test
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 } 471 }
472 }; 472 };
473 new Handler(Looper.getMainLooper()).post(blockingTask); 473 new Handler(Looper.getMainLooper()).post(blockingTask);
474 // Wait for shutdown to complete on main thread. 474 // Wait for shutdown to complete on main thread.
475 block.block(); 475 block.block();
476 } 476 }
477 477
478 @SmallTest 478 @SmallTest
479 @Feature({"Cronet"}) 479 @Feature({"Cronet"})
480 public void testMultipleShutdown() throws Exception { 480 public void testMultipleShutdown() throws Exception {
481 mTestFramework = startCronetTestFramework(); 481 final CronetTestFramework testFramework = startCronetTestFramework();
482 try { 482 try {
483 mTestFramework.mCronetEngine.shutdown(); 483 testFramework.mCronetEngine.shutdown();
484 mTestFramework.mCronetEngine.shutdown(); 484 testFramework.mCronetEngine.shutdown();
485 fail("Should throw an exception"); 485 fail("Should throw an exception");
486 } catch (Exception e) { 486 } catch (Exception e) {
487 assertEquals("Engine is shut down.", e.getMessage()); 487 assertEquals("Engine is shut down.", e.getMessage());
488 } 488 }
489 } 489 }
490 490
491 @SmallTest 491 @SmallTest
492 @Feature({"Cronet"}) 492 @Feature({"Cronet"})
493 // TODO: Remove the annotation after fixing http://crbug.com/637972 493 // TODO: Remove the annotation after fixing http://crbug.com/637972
494 @OnlyRunNativeCronet 494 @OnlyRunNativeCronet
495 public void testShutdownAfterError() throws Exception { 495 public void testShutdownAfterError() throws Exception {
496 Log.i(TAG, "testShutdownAfterError() has started"); 496 final CronetTestFramework testFramework = startCronetTestFramework();
497 mTestFramework = startCronetTestFramework(); 497 ShutdownTestUrlRequestCallback callback =
498 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); 498 new ShutdownTestUrlRequestCallback(testFramework.mCronetEngine);
499 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL, 499 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL,
500 callback, callback.getExecutor(), mTestFramework.mCronetEngine); 500 callback, callback.getExecutor(), testFramework.mCronetEngine);
501 urlRequestBuilder.build().start(); 501 urlRequestBuilder.build().start();
502 callback.blockForDone(); 502 callback.blockForDone();
503 assertTrue(callback.mOnErrorCalled); 503 assertTrue(callback.mOnErrorCalled);
504 // TODO: Remove sleep when http://crbug.com/635025 is fixed. 504 callback.blockForCallbackToComplete();
505 // The sleep gives the thread that shuts down the engine time to complet e. 505 callback.shutdownExecutor();
506 // See http://crbug.com/637986
507 Thread.sleep(3000);
508 Log.i(TAG, "testShutdownAfterError() has finished");
509 } 506 }
510 507
511 @SmallTest 508 @SmallTest
512 @Feature({"Cronet"}) 509 @Feature({"Cronet"})
513 public void testShutdownAfterCancel() throws Exception { 510 public void testShutdownAfterCancel() throws Exception {
514 mTestFramework = startCronetTestFramework(); 511 final CronetTestFramework testFramework = startCronetTestFramework();
515 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 512 TestUrlRequestCallback callback = new TestUrlRequestCallback();
516 // Block callback when response starts to verify that shutdown fails 513 // Block callback when response starts to verify that shutdown fails
517 // if there are active requests. 514 // if there are active requests.
518 callback.setAutoAdvance(false); 515 callback.setAutoAdvance(false);
519 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 516 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
520 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 517 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
521 UrlRequest urlRequest = urlRequestBuilder.build(); 518 UrlRequest urlRequest = urlRequestBuilder.build();
522 urlRequest.start(); 519 urlRequest.start();
523 try { 520 try {
524 mTestFramework.mCronetEngine.shutdown(); 521 testFramework.mCronetEngine.shutdown();
525 fail("Should throw an exception"); 522 fail("Should throw an exception");
526 } catch (Exception e) { 523 } catch (Exception e) {
527 assertEquals("Cannot shutdown with active requests.", 524 assertEquals("Cannot shutdown with active requests.",
528 e.getMessage()); 525 e.getMessage());
529 } 526 }
530 callback.waitForNextStep(); 527 callback.waitForNextStep();
531 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep); 528 assertEquals(ResponseStep.ON_RESPONSE_STARTED, callback.mResponseStep);
532 urlRequest.cancel(); 529 urlRequest.cancel();
533 mTestFramework.mCronetEngine.shutdown(); 530 testFramework.mCronetEngine.shutdown();
534 } 531 }
535 532
536 @SmallTest 533 @SmallTest
537 @Feature({"Cronet"}) 534 @Feature({"Cronet"})
538 @OnlyRunNativeCronet // No netlogs for pure java impl 535 @OnlyRunNativeCronet // No netlogs for pure java impl
539 public void testNetLog() throws Exception { 536 public void testNetLog() throws Exception {
540 Context context = getContext(); 537 Context context = getContext();
541 File directory = new File(PathUtils.getDataDirectory(context)); 538 File directory = new File(PathUtils.getDataDirectory(context));
542 File file = File.createTempFile("cronet", "json", directory); 539 File file = File.createTempFile("cronet", "json", directory);
543 CronetEngine cronetEngine = new CronetUrlRequestContext( 540 CronetEngine cronetEngine = new CronetUrlRequestContext(
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 FileUtils.recursivelyDeleteFile(netLogDir2); 745 FileUtils.recursivelyDeleteFile(netLogDir2);
749 assertFalse(netLogDir2.exists()); 746 assertFalse(netLogDir2.exists());
750 } 747 }
751 748
752 @SmallTest 749 @SmallTest
753 @Feature({"Cronet"}) 750 @Feature({"Cronet"})
754 @OnlyRunNativeCronet 751 @OnlyRunNativeCronet
755 // Tests that if CronetEngine is shut down when reading from disk cache, 752 // Tests that if CronetEngine is shut down when reading from disk cache,
756 // there isn't a crash. See crbug.com/486120. 753 // there isn't a crash. See crbug.com/486120.
757 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception { 754 public void testShutDownEngineWhenReadingFromDiskCache() throws Exception {
758 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); 755 final CronetTestFramework testFramework =
756 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
759 String url = NativeTestServer.getFileURL("/cacheable.txt"); 757 String url = NativeTestServer.getFileURL("/cacheable.txt");
760 // Make a request to a cacheable resource. 758 // Make a request to a cacheable resource.
761 checkRequestCaching(url, false); 759 checkRequestCaching(testFramework.mCronetEngine, url, false);
762 760
763 // Shut down the server. 761 // Shut down the server.
764 NativeTestServer.shutdownNativeTestServer(); 762 NativeTestServer.shutdownNativeTestServer();
765 class CancelUrlRequestCallback extends TestUrlRequestCallback { 763 class CancelUrlRequestCallback extends TestUrlRequestCallback {
766 @Override 764 @Override
767 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) { 765 public void onResponseStarted(UrlRequest request, UrlResponseInfo in fo) {
768 super.onResponseStarted(request, info); 766 super.onResponseStarted(request, info);
769 request.cancel(); 767 request.cancel();
770 // Shut down CronetEngine immediately after request is destroyed . 768 // Shut down CronetEngine immediately after request is destroyed .
771 mTestFramework.mCronetEngine.shutdown(); 769 testFramework.mCronetEngine.shutdown();
772 } 770 }
773 771
774 @Override 772 @Override
775 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { 773 public void onSucceeded(UrlRequest request, UrlResponseInfo info) {
776 // onSucceeded will not happen, because the request is canceled 774 // onSucceeded will not happen, because the request is canceled
777 // after sending first read and the executor is single threaded. 775 // after sending first read and the executor is single threaded.
778 throw new RuntimeException("Unexpected"); 776 throw new RuntimeException("Unexpected");
779 } 777 }
780 778
781 @Override 779 @Override
782 public void onFailed( 780 public void onFailed(
783 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio n error) { 781 UrlRequest request, UrlResponseInfo info, UrlRequestExceptio n error) {
784 throw new RuntimeException("Unexpected"); 782 throw new RuntimeException("Unexpected");
785 } 783 }
786 } 784 }
787 CancelUrlRequestCallback callback = new CancelUrlRequestCallback(); 785 CancelUrlRequestCallback callback = new CancelUrlRequestCallback();
788 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 786 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
789 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); 787 url, callback, callback.getExecutor(), testFramework.mCronetEngi ne);
790 urlRequestBuilder.build().start(); 788 urlRequestBuilder.build().start();
791 callback.blockForDone(); 789 callback.blockForDone();
792 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); 790 assertEquals(200, callback.mResponseInfo.getHttpStatusCode());
793 assertTrue(callback.mResponseInfo.wasCached()); 791 assertTrue(callback.mResponseInfo.wasCached());
794 assertTrue(callback.mOnCanceledCalled); 792 assertTrue(callback.mOnCanceledCalled);
795 } 793 }
796 794
797 @SmallTest 795 @SmallTest
798 @Feature({"Cronet"}) 796 @Feature({"Cronet"})
799 @OnlyRunNativeCronet 797 @OnlyRunNativeCronet
800 public void testNetLogAfterShutdown() throws Exception { 798 public void testNetLogAfterShutdown() throws Exception {
801 mTestFramework = startCronetTestFramework(); 799 final CronetTestFramework testFramework = startCronetTestFramework();
802 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 800 TestUrlRequestCallback callback = new TestUrlRequestCallback();
803 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 801 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
804 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 802 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
805 urlRequestBuilder.build().start(); 803 urlRequestBuilder.build().start();
806 callback.blockForDone(); 804 callback.blockForDone();
807 mTestFramework.mCronetEngine.shutdown(); 805 testFramework.mCronetEngine.shutdown();
808 806
809 File directory = new File(PathUtils.getDataDirectory(getContext())); 807 File directory = new File(PathUtils.getDataDirectory(getContext()));
810 File file = File.createTempFile("cronet", "json", directory); 808 File file = File.createTempFile("cronet", "json", directory);
811 try { 809 try {
812 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false ); 810 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false) ;
813 fail("Should throw an exception."); 811 fail("Should throw an exception.");
814 } catch (Exception e) { 812 } catch (Exception e) {
815 assertEquals("Engine is shut down.", e.getMessage()); 813 assertEquals("Engine is shut down.", e.getMessage());
816 } 814 }
817 assertFalse(hasBytesInNetLog(file)); 815 assertFalse(hasBytesInNetLog(file));
818 assertTrue(file.delete()); 816 assertTrue(file.delete());
819 assertTrue(!file.exists()); 817 assertTrue(!file.exists());
820 } 818 }
821 819
822 @SmallTest 820 @SmallTest
823 @Feature({"Cronet"}) 821 @Feature({"Cronet"})
824 @OnlyRunNativeCronet 822 @OnlyRunNativeCronet
825 public void testBoundedFileNetLogAfterShutdown() throws Exception { 823 public void testBoundedFileNetLogAfterShutdown() throws Exception {
826 mTestFramework = startCronetTestFramework(); 824 final CronetTestFramework testFramework = startCronetTestFramework();
827 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 825 TestUrlRequestCallback callback = new TestUrlRequestCallback();
828 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 826 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
829 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 827 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
830 urlRequestBuilder.build().start(); 828 urlRequestBuilder.build().start();
831 callback.blockForDone(); 829 callback.blockForDone();
832 mTestFramework.mCronetEngine.shutdown(); 830 testFramework.mCronetEngine.shutdown();
833 831
834 File directory = new File(PathUtils.getDataDirectory(getContext())); 832 File directory = new File(PathUtils.getDataDirectory(getContext()));
835 File netLogDir = new File(directory, "NetLog"); 833 File netLogDir = new File(directory, "NetLog");
836 assertFalse(netLogDir.exists()); 834 assertFalse(netLogDir.exists());
837 assertTrue(netLogDir.mkdir()); 835 assertTrue(netLogDir.mkdir());
838 File constantsFile = new File(netLogDir, "constants.json"); 836 File constantsFile = new File(netLogDir, "constants.json");
839 try { 837 try {
840 mTestFramework.mCronetEngine.startNetLogToDisk( 838 testFramework.mCronetEngine.startNetLogToDisk(
841 netLogDir.getPath(), false, MAX_FILE_SIZE); 839 netLogDir.getPath(), false, MAX_FILE_SIZE);
842 fail("Should throw an exception."); 840 fail("Should throw an exception.");
843 } catch (Exception e) { 841 } catch (Exception e) {
844 assertEquals("Engine is shut down.", e.getMessage()); 842 assertEquals("Engine is shut down.", e.getMessage());
845 } 843 }
846 assertFalse(constantsFile.exists()); 844 assertFalse(constantsFile.exists());
847 FileUtils.recursivelyDeleteFile(netLogDir); 845 FileUtils.recursivelyDeleteFile(netLogDir);
848 assertFalse(netLogDir.exists()); 846 assertFalse(netLogDir.exists());
849 } 847 }
850 848
851 @SmallTest 849 @SmallTest
852 @Feature({"Cronet"}) 850 @Feature({"Cronet"})
853 public void testNetLogStartMultipleTimes() throws Exception { 851 public void testNetLogStartMultipleTimes() throws Exception {
854 mTestFramework = startCronetTestFramework(); 852 final CronetTestFramework testFramework = startCronetTestFramework();
855 File directory = new File(PathUtils.getDataDirectory(getContext())); 853 File directory = new File(PathUtils.getDataDirectory(getContext()));
856 File file = File.createTempFile("cronet", "json", directory); 854 File file = File.createTempFile("cronet", "json", directory);
857 // Start NetLog multiple times. 855 // Start NetLog multiple times.
858 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); 856 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false);
859 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); 857 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false);
860 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); 858 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false);
861 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); 859 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false);
862 // Start a request. 860 // Start a request.
863 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 861 TestUrlRequestCallback callback = new TestUrlRequestCallback();
864 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 862 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
865 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 863 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
866 urlRequestBuilder.build().start(); 864 urlRequestBuilder.build().start();
867 callback.blockForDone(); 865 callback.blockForDone();
868 mTestFramework.mCronetEngine.stopNetLog(); 866 testFramework.mCronetEngine.stopNetLog();
869 assertTrue(file.exists()); 867 assertTrue(file.exists());
870 assertTrue(file.length() != 0); 868 assertTrue(file.length() != 0);
871 assertFalse(hasBytesInNetLog(file)); 869 assertFalse(hasBytesInNetLog(file));
872 assertTrue(file.delete()); 870 assertTrue(file.delete());
873 assertTrue(!file.exists()); 871 assertTrue(!file.exists());
874 } 872 }
875 873
876 @SmallTest 874 @SmallTest
877 @Feature({"Cronet"}) 875 @Feature({"Cronet"})
878 public void testBoundedFileNetLogStartMultipleTimes() throws Exception { 876 public void testBoundedFileNetLogStartMultipleTimes() throws Exception {
879 mTestFramework = startCronetTestFramework(); 877 final CronetTestFramework testFramework = startCronetTestFramework();
880 File directory = new File(PathUtils.getDataDirectory(getContext())); 878 File directory = new File(PathUtils.getDataDirectory(getContext()));
881 File netLogDir = new File(directory, "NetLog"); 879 File netLogDir = new File(directory, "NetLog");
882 assertFalse(netLogDir.exists()); 880 assertFalse(netLogDir.exists());
883 assertTrue(netLogDir.mkdir()); 881 assertTrue(netLogDir.mkdir());
884 File eventFile = new File(netLogDir, "event_file_0.json"); 882 File eventFile = new File(netLogDir, "event_file_0.json");
885 // Start NetLog multiple times. This should be equivalent to starting Ne tLog 883 // Start NetLog multiple times. This should be equivalent to starting Ne tLog
886 // once. Each subsequent start (without calling stopNetLog) should be a no-op. 884 // once. Each subsequent start (without calling stopNetLog) should be a no-op.
887 mTestFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), fals e, MAX_FILE_SIZE); 885 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false , MAX_FILE_SIZE);
888 mTestFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), fals e, MAX_FILE_SIZE); 886 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false , MAX_FILE_SIZE);
889 mTestFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), fals e, MAX_FILE_SIZE); 887 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false , MAX_FILE_SIZE);
890 mTestFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), fals e, MAX_FILE_SIZE); 888 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false , MAX_FILE_SIZE);
891 // Start a request. 889 // Start a request.
892 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 890 TestUrlRequestCallback callback = new TestUrlRequestCallback();
893 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 891 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
894 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 892 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
895 urlRequestBuilder.build().start(); 893 urlRequestBuilder.build().start();
896 callback.blockForDone(); 894 callback.blockForDone();
897 mTestFramework.mCronetEngine.stopNetLog(); 895 testFramework.mCronetEngine.stopNetLog();
898 assertTrue(eventFile.exists()); 896 assertTrue(eventFile.exists());
899 assertTrue(eventFile.length() != 0); 897 assertTrue(eventFile.length() != 0);
900 assertFalse(hasBytesInNetLog(eventFile)); 898 assertFalse(hasBytesInNetLog(eventFile));
901 FileUtils.recursivelyDeleteFile(netLogDir); 899 FileUtils.recursivelyDeleteFile(netLogDir);
902 assertFalse(netLogDir.exists()); 900 assertFalse(netLogDir.exists());
903 } 901 }
904 902
905 @SmallTest 903 @SmallTest
906 @Feature({"Cronet"}) 904 @Feature({"Cronet"})
907 public void testNetLogStopMultipleTimes() throws Exception { 905 public void testNetLogStopMultipleTimes() throws Exception {
908 mTestFramework = startCronetTestFramework(); 906 final CronetTestFramework testFramework = startCronetTestFramework();
909 File directory = new File(PathUtils.getDataDirectory(getContext())); 907 File directory = new File(PathUtils.getDataDirectory(getContext()));
910 File file = File.createTempFile("cronet", "json", directory); 908 File file = File.createTempFile("cronet", "json", directory);
911 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); 909 testFramework.mCronetEngine.startNetLogToFile(file.getPath(), false);
912 // Start a request. 910 // Start a request.
913 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 911 TestUrlRequestCallback callback = new TestUrlRequestCallback();
914 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 912 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
915 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 913 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
916 urlRequestBuilder.build().start(); 914 urlRequestBuilder.build().start();
917 callback.blockForDone(); 915 callback.blockForDone();
918 // Stop NetLog multiple times. 916 // Stop NetLog multiple times.
919 mTestFramework.mCronetEngine.stopNetLog(); 917 testFramework.mCronetEngine.stopNetLog();
920 mTestFramework.mCronetEngine.stopNetLog(); 918 testFramework.mCronetEngine.stopNetLog();
921 mTestFramework.mCronetEngine.stopNetLog(); 919 testFramework.mCronetEngine.stopNetLog();
922 mTestFramework.mCronetEngine.stopNetLog(); 920 testFramework.mCronetEngine.stopNetLog();
923 mTestFramework.mCronetEngine.stopNetLog(); 921 testFramework.mCronetEngine.stopNetLog();
924 assertTrue(file.exists()); 922 assertTrue(file.exists());
925 assertTrue(file.length() != 0); 923 assertTrue(file.length() != 0);
926 assertFalse(hasBytesInNetLog(file)); 924 assertFalse(hasBytesInNetLog(file));
927 assertTrue(file.delete()); 925 assertTrue(file.delete());
928 assertTrue(!file.exists()); 926 assertTrue(!file.exists());
929 } 927 }
930 928
931 @SmallTest 929 @SmallTest
932 @Feature({"Cronet"}) 930 @Feature({"Cronet"})
933 public void testBoundedFileNetLogStopMultipleTimes() throws Exception { 931 public void testBoundedFileNetLogStopMultipleTimes() throws Exception {
934 mTestFramework = startCronetTestFramework(); 932 final CronetTestFramework testFramework = startCronetTestFramework();
935 File directory = new File(PathUtils.getDataDirectory(getContext())); 933 File directory = new File(PathUtils.getDataDirectory(getContext()));
936 File netLogDir = new File(directory, "NetLog"); 934 File netLogDir = new File(directory, "NetLog");
937 assertFalse(netLogDir.exists()); 935 assertFalse(netLogDir.exists());
938 assertTrue(netLogDir.mkdir()); 936 assertTrue(netLogDir.mkdir());
939 File eventFile = new File(netLogDir, "event_file_0.json"); 937 File eventFile = new File(netLogDir, "event_file_0.json");
940 mTestFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), fals e, MAX_FILE_SIZE); 938 testFramework.mCronetEngine.startNetLogToDisk(netLogDir.getPath(), false , MAX_FILE_SIZE);
941 // Start a request. 939 // Start a request.
942 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 940 TestUrlRequestCallback callback = new TestUrlRequestCallback();
943 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 941 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
944 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 942 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
945 urlRequestBuilder.build().start(); 943 urlRequestBuilder.build().start();
946 callback.blockForDone(); 944 callback.blockForDone();
947 // Stop NetLog multiple times. This should be equivalent to stopping Net Log once. 945 // Stop NetLog multiple times. This should be equivalent to stopping Net Log once.
948 // Each subsequent stop (without calling startNetLogToDisk first) should be a no-op. 946 // Each subsequent stop (without calling startNetLogToDisk first) should be a no-op.
949 mTestFramework.mCronetEngine.stopNetLog(); 947 testFramework.mCronetEngine.stopNetLog();
950 mTestFramework.mCronetEngine.stopNetLog(); 948 testFramework.mCronetEngine.stopNetLog();
951 mTestFramework.mCronetEngine.stopNetLog(); 949 testFramework.mCronetEngine.stopNetLog();
952 mTestFramework.mCronetEngine.stopNetLog(); 950 testFramework.mCronetEngine.stopNetLog();
953 mTestFramework.mCronetEngine.stopNetLog(); 951 testFramework.mCronetEngine.stopNetLog();
954 assertTrue(eventFile.exists()); 952 assertTrue(eventFile.exists());
955 assertTrue(eventFile.length() != 0); 953 assertTrue(eventFile.length() != 0);
956 assertFalse(hasBytesInNetLog(eventFile)); 954 assertFalse(hasBytesInNetLog(eventFile));
957 FileUtils.recursivelyDeleteFile(netLogDir); 955 FileUtils.recursivelyDeleteFile(netLogDir);
958 assertFalse(netLogDir.exists()); 956 assertFalse(netLogDir.exists());
959 } 957 }
960 958
961 @SmallTest 959 @SmallTest
962 @Feature({"Cronet"}) 960 @Feature({"Cronet"})
963 @OnlyRunNativeCronet 961 @OnlyRunNativeCronet
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1038 private void makeRequestAndCheckStatus( 1036 private void makeRequestAndCheckStatus(
1039 CronetEngine engine, String url, int expectedStatusCode) { 1037 CronetEngine engine, String url, int expectedStatusCode) {
1040 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1038 TestUrlRequestCallback callback = new TestUrlRequestCallback();
1041 UrlRequest request = 1039 UrlRequest request =
1042 new UrlRequest.Builder(url, callback, callback.getExecutor(), en gine).build(); 1040 new UrlRequest.Builder(url, callback, callback.getExecutor(), en gine).build();
1043 request.start(); 1041 request.start();
1044 callback.blockForDone(); 1042 callback.blockForDone();
1045 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e()); 1043 assertEquals(expectedStatusCode, callback.mResponseInfo.getHttpStatusCod e());
1046 } 1044 }
1047 1045
1048 private void enableCache(int cacheType) throws Exception { 1046 private CronetTestFramework startCronetTestFrameworkWithCacheEnabled(int cac heType)
1047 throws Exception {
1049 String cacheTypeString = ""; 1048 String cacheTypeString = "";
1050 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { 1049 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) {
1051 cacheTypeString = CronetTestFramework.CACHE_DISK; 1050 cacheTypeString = CronetTestFramework.CACHE_DISK;
1052 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { 1051 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) {
1053 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; 1052 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP;
1054 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { 1053 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) {
1055 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; 1054 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY;
1056 } 1055 }
1057 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng}; 1056 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri ng};
1058 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandLineArgs); 1057 CronetTestFramework testFramework =
1058 startCronetTestFrameworkWithUrlAndCommandLineArgs(null, commandL ineArgs);
1059 assertTrue(NativeTestServer.startNativeTestServer(getContext())); 1059 assertTrue(NativeTestServer.startNativeTestServer(getContext()));
1060 return testFramework;
1060 } 1061 }
1061 1062
1062 private void checkRequestCaching(String url, boolean expectCached) { 1063 private void checkRequestCaching(CronetEngine engine, String url, boolean ex pectCached) {
1063 checkRequestCaching(url, expectCached, false); 1064 checkRequestCaching(engine, url, expectCached, false);
1064 } 1065 }
1065 1066
1066 private void checkRequestCaching(String url, boolean expectCached, 1067 private void checkRequestCaching(
1067 boolean disableCache) { 1068 CronetEngine engine, String url, boolean expectCached, boolean disab leCache) {
1068 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1069 TestUrlRequestCallback callback = new TestUrlRequestCallback();
1069 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 1070 UrlRequest.Builder urlRequestBuilder =
1070 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); 1071 new UrlRequest.Builder(url, callback, callback.getExecutor(), en gine);
1071 if (disableCache) { 1072 if (disableCache) {
1072 urlRequestBuilder.disableCache(); 1073 urlRequestBuilder.disableCache();
1073 } 1074 }
1074 urlRequestBuilder.build().start(); 1075 urlRequestBuilder.build().start();
1075 callback.blockForDone(); 1076 callback.blockForDone();
1076 assertEquals(expectCached, callback.mResponseInfo.wasCached()); 1077 assertEquals(expectCached, callback.mResponseInfo.wasCached());
1077 assertEquals("this is a cacheable file\n", callback.mResponseAsString); 1078 assertEquals("this is a cacheable file\n", callback.mResponseAsString);
1078 } 1079 }
1079 1080
1080 @SmallTest 1081 @SmallTest
1081 @Feature({"Cronet"}) 1082 @Feature({"Cronet"})
1082 @OnlyRunNativeCronet 1083 @OnlyRunNativeCronet
1083 public void testEnableHttpCacheDisabled() throws Exception { 1084 public void testEnableHttpCacheDisabled() throws Exception {
1084 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED); 1085 final CronetTestFramework testFramework =
1086 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISABLED);
1085 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1087 String url = NativeTestServer.getFileURL("/cacheable.txt");
1086 checkRequestCaching(url, false); 1088 checkRequestCaching(testFramework.mCronetEngine, url, false);
1087 checkRequestCaching(url, false); 1089 checkRequestCaching(testFramework.mCronetEngine, url, false);
1088 checkRequestCaching(url, false); 1090 checkRequestCaching(testFramework.mCronetEngine, url, false);
1089 } 1091 }
1090 1092
1091 @SmallTest 1093 @SmallTest
1092 @Feature({"Cronet"}) 1094 @Feature({"Cronet"})
1093 public void testEnableHttpCacheInMemory() throws Exception { 1095 public void testEnableHttpCacheInMemory() throws Exception {
1094 enableCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY); 1096 final CronetTestFramework testFramework =
1097 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_IN_MEMORY);
1095 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1098 String url = NativeTestServer.getFileURL("/cacheable.txt");
1096 checkRequestCaching(url, false); 1099 checkRequestCaching(testFramework.mCronetEngine, url, false);
1097 checkRequestCaching(url, true); 1100 checkRequestCaching(testFramework.mCronetEngine, url, true);
1098 NativeTestServer.shutdownNativeTestServer(); 1101 NativeTestServer.shutdownNativeTestServer();
1099 checkRequestCaching(url, true); 1102 checkRequestCaching(testFramework.mCronetEngine, url, true);
1100 } 1103 }
1101 1104
1102 @SmallTest 1105 @SmallTest
1103 @Feature({"Cronet"}) 1106 @Feature({"Cronet"})
1104 public void testEnableHttpCacheDisk() throws Exception { 1107 public void testEnableHttpCacheDisk() throws Exception {
1105 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); 1108 final CronetTestFramework testFramework =
1109 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
1106 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1110 String url = NativeTestServer.getFileURL("/cacheable.txt");
1107 checkRequestCaching(url, false); 1111 checkRequestCaching(testFramework.mCronetEngine, url, false);
1108 checkRequestCaching(url, true); 1112 checkRequestCaching(testFramework.mCronetEngine, url, true);
1109 NativeTestServer.shutdownNativeTestServer(); 1113 NativeTestServer.shutdownNativeTestServer();
1110 checkRequestCaching(url, true); 1114 checkRequestCaching(testFramework.mCronetEngine, url, true);
1111 } 1115 }
1112 1116
1113 @SmallTest 1117 @SmallTest
1114 @Feature({"Cronet"}) 1118 @Feature({"Cronet"})
1115 @OnlyRunNativeCronet 1119 @OnlyRunNativeCronet
1116 public void testEnableHttpCacheDiskNoHttp() throws Exception { 1120 public void testEnableHttpCacheDiskNoHttp() throws Exception {
1117 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED); 1121 final CronetTestFramework testFramework =
1122 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISABLED);
1118 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1123 String url = NativeTestServer.getFileURL("/cacheable.txt");
1119 checkRequestCaching(url, false); 1124 checkRequestCaching(testFramework.mCronetEngine, url, false);
1120 checkRequestCaching(url, false); 1125 checkRequestCaching(testFramework.mCronetEngine, url, false);
1121 checkRequestCaching(url, false); 1126 checkRequestCaching(testFramework.mCronetEngine, url, false);
1122 } 1127 }
1123 1128
1124 @SmallTest 1129 @SmallTest
1125 @Feature({"Cronet"}) 1130 @Feature({"Cronet"})
1126 public void testDisableCache() throws Exception { 1131 public void testDisableCache() throws Exception {
1127 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); 1132 final CronetTestFramework testFramework =
1133 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
1128 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1134 String url = NativeTestServer.getFileURL("/cacheable.txt");
1129 1135
1130 // When cache is disabled, making a request does not write to the cache. 1136 // When cache is disabled, making a request does not write to the cache.
1131 checkRequestCaching(url, false, true /** disable cache */); 1137 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di sable cache */);
1132 checkRequestCaching(url, false); 1138 checkRequestCaching(testFramework.mCronetEngine, url, false);
1133 1139
1134 // When cache is enabled, the second request is cached. 1140 // When cache is enabled, the second request is cached.
1135 checkRequestCaching(url, false, true /** disable cache */); 1141 checkRequestCaching(testFramework.mCronetEngine, url, false, true /** di sable cache */);
1136 checkRequestCaching(url, true); 1142 checkRequestCaching(testFramework.mCronetEngine, url, true);
1137 1143
1138 // Shut down the server, next request should have a cached response. 1144 // Shut down the server, next request should have a cached response.
1139 NativeTestServer.shutdownNativeTestServer(); 1145 NativeTestServer.shutdownNativeTestServer();
1140 checkRequestCaching(url, true); 1146 checkRequestCaching(testFramework.mCronetEngine, url, true);
1141 1147
1142 // Cache is disabled after server is shut down, request should fail. 1148 // Cache is disabled after server is shut down, request should fail.
1143 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1149 TestUrlRequestCallback callback = new TestUrlRequestCallback();
1144 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( 1150 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(
1145 url, callback, callback.getExecutor(), mTestFramework.mCronetEng ine); 1151 url, callback, callback.getExecutor(), testFramework.mCronetEngi ne);
1146 urlRequestBuilder.disableCache(); 1152 urlRequestBuilder.disableCache();
1147 urlRequestBuilder.build().start(); 1153 urlRequestBuilder.build().start();
1148 callback.blockForDone(); 1154 callback.blockForDone();
1149 assertNotNull(callback.mError); 1155 assertNotNull(callback.mError);
1150 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ", 1156 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ",
1151 callback.mError.getMessage()); 1157 callback.mError.getMessage());
1152 } 1158 }
1153 1159
1154 @SmallTest 1160 @SmallTest
1155 @Feature({"Cronet"}) 1161 @Feature({"Cronet"})
1156 public void testEnableHttpCacheDiskNewEngine() throws Exception { 1162 public void testEnableHttpCacheDiskNewEngine() throws Exception {
1157 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); 1163 final CronetTestFramework testFramework =
1164 startCronetTestFrameworkWithCacheEnabled(CronetEngine.Builder.HT TP_CACHE_DISK);
1158 String url = NativeTestServer.getFileURL("/cacheable.txt"); 1165 String url = NativeTestServer.getFileURL("/cacheable.txt");
1159 checkRequestCaching(url, false); 1166 checkRequestCaching(testFramework.mCronetEngine, url, false);
1160 checkRequestCaching(url, true); 1167 checkRequestCaching(testFramework.mCronetEngine, url, true);
1161 NativeTestServer.shutdownNativeTestServer(); 1168 NativeTestServer.shutdownNativeTestServer();
1162 checkRequestCaching(url, true); 1169 checkRequestCaching(testFramework.mCronetEngine, url, true);
1163 1170
1164 // Shutdown original context and create another that uses the same cache . 1171 // Shutdown original context and create another that uses the same cache .
1165 mTestFramework.mCronetEngine.shutdown(); 1172 testFramework.mCronetEngine.shutdown();
1166 mTestFramework.mCronetEngine = mTestFramework.getCronetEngineBuilder().b uild(); 1173 testFramework.mCronetEngine = testFramework.getCronetEngineBuilder().bui ld();
1167 checkRequestCaching(url, true); 1174 checkRequestCaching(testFramework.mCronetEngine, url, true);
1168 } 1175 }
1169 1176
1170 @SmallTest 1177 @SmallTest
1171 @Feature({"Cronet"}) 1178 @Feature({"Cronet"})
1172 public void testInitEngineAndStartRequest() { 1179 public void testInitEngineAndStartRequest() {
1173 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra ryInit(); 1180 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra ryInit();
1174 1181
1175 // Immediately make a request after initializing the engine. 1182 // Immediately make a request after initializing the engine.
1176 CronetEngine cronetEngine = testFramework.initCronetEngine(); 1183 CronetEngine cronetEngine = testFramework.initCronetEngine();
1177 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1184 TestUrlRequestCallback callback = new TestUrlRequestCallback();
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1263 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); 1270 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode());
1264 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); 1271 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode());
1265 } 1272 }
1266 1273
1267 @SmallTest 1274 @SmallTest
1268 @Feature({"Cronet"}) 1275 @Feature({"Cronet"})
1269 public void testInitDifferentEngines() throws Exception { 1276 public void testInitDifferentEngines() throws Exception {
1270 // Test that concurrently instantiating Cronet context's upon various 1277 // Test that concurrently instantiating Cronet context's upon various
1271 // different versions of the same Android Context does not cause crashes 1278 // different versions of the same Android Context does not cause crashes
1272 // like crbug.com/453845 1279 // like crbug.com/453845
1273 mTestFramework = startCronetTestFramework(); 1280 final CronetTestFramework testFramework = startCronetTestFramework();
1274 CronetEngine firstEngine = 1281 CronetEngine firstEngine =
1275 new CronetUrlRequestContext(mTestFramework.createCronetEngineBui lder(getContext())); 1282 new CronetUrlRequestContext(testFramework.createCronetEngineBuil der(getContext()));
1276 CronetEngine secondEngine = new CronetUrlRequestContext( 1283 CronetEngine secondEngine = new CronetUrlRequestContext(
1277 mTestFramework.createCronetEngineBuilder(getContext().getApplica tionContext())); 1284 testFramework.createCronetEngineBuilder(getContext().getApplicat ionContext()));
1278 CronetEngine thirdEngine = new CronetUrlRequestContext( 1285 CronetEngine thirdEngine = new CronetUrlRequestContext(
1279 mTestFramework.createCronetEngineBuilder(new ContextWrapper(getC ontext()))); 1286 testFramework.createCronetEngineBuilder(new ContextWrapper(getCo ntext())));
1280 firstEngine.shutdown(); 1287 firstEngine.shutdown();
1281 secondEngine.shutdown(); 1288 secondEngine.shutdown();
1282 thirdEngine.shutdown(); 1289 thirdEngine.shutdown();
1283 } 1290 }
1284 1291
1285 @SmallTest 1292 @SmallTest
1286 @Feature({"Cronet"}) 1293 @Feature({"Cronet"})
1287 public void testGetGlobalMetricsDeltas() throws Exception { 1294 public void testGetGlobalMetricsDeltas() throws Exception {
1288 mTestFramework = startCronetTestFramework(); 1295 final CronetTestFramework testFramework = startCronetTestFramework();
1289 1296
1290 byte delta1[] = mTestFramework.mCronetEngine.getGlobalMetricsDeltas(); 1297 byte delta1[] = testFramework.mCronetEngine.getGlobalMetricsDeltas();
1291 1298
1292 TestUrlRequestCallback callback = new TestUrlRequestCallback(); 1299 TestUrlRequestCallback callback = new TestUrlRequestCallback();
1293 UrlRequest.Builder builder = new UrlRequest.Builder( 1300 UrlRequest.Builder builder = new UrlRequest.Builder(
1294 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); 1301 mUrl, callback, callback.getExecutor(), testFramework.mCronetEng ine);
1295 builder.build().start(); 1302 builder.build().start();
1296 callback.blockForDone(); 1303 callback.blockForDone();
1297 byte delta2[] = mTestFramework.mCronetEngine.getGlobalMetricsDeltas(); 1304 byte delta2[] = testFramework.mCronetEngine.getGlobalMetricsDeltas();
1298 assertTrue(delta2.length != 0); 1305 assertTrue(delta2.length != 0);
1299 assertFalse(Arrays.equals(delta1, delta2)); 1306 assertFalse(Arrays.equals(delta1, delta2));
1300 } 1307 }
1301 1308
1302 @SmallTest 1309 @SmallTest
1303 @Feature({"Cronet"}) 1310 @Feature({"Cronet"})
1304 public void testCronetEngineBuilderConfig() throws Exception { 1311 public void testCronetEngineBuilderConfig() throws Exception {
1305 // This is to prompt load of native library. 1312 // This is to prompt load of native library.
1306 startCronetTestFramework(); 1313 startCronetTestFramework();
1307 // Verify CronetEngine.Builder config is passed down accurately to nativ e code. 1314 // Verify CronetEngine.Builder config is passed down accurately to nativ e code.
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1376 } 1383 }
1377 }.start(); 1384 }.start();
1378 otherThreadDone.block(); 1385 otherThreadDone.block();
1379 builder.build().shutdown(); 1386 builder.build().shutdown();
1380 uiThreadDone.open(); 1387 uiThreadDone.open();
1381 } 1388 }
1382 }); 1389 });
1383 assertTrue(uiThreadDone.block(1000)); 1390 assertTrue(uiThreadDone.block(1000));
1384 } 1391 }
1385 } 1392 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698