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

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

Issue 1363723002: [Cronet] Create Builders, rename UrlRequestContext to CronetEngine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: make old API work without renaming Created 5 years, 2 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
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.test.suitebuilder.annotation.SmallTest; 12 import android.test.suitebuilder.annotation.SmallTest;
13 13
14 import org.chromium.base.PathUtils; 14 import org.chromium.base.PathUtils;
15 import org.chromium.base.test.util.Feature; 15 import org.chromium.base.test.util.Feature;
16 import org.chromium.net.TestUrlRequestListener.ResponseStep; 16 import org.chromium.net.TestUrlRequestListener.ResponseStep;
17 17
18 import java.io.BufferedReader; 18 import java.io.BufferedReader;
19 import java.io.File; 19 import java.io.File;
20 import java.io.FileReader; 20 import java.io.FileReader;
21 21
22 /** 22 /**
23 * Test CronetUrlRequestContext. 23 * Test CronetEngine.
24 */ 24 */
25 public class CronetUrlRequestContextTest extends CronetTestBase { 25 public class CronetUrlRequestContextTest extends CronetTestBase {
26 // URLs used for tests. 26 // URLs used for tests.
27 private static final String TEST_URL = "http://127.0.0.1:8000"; 27 private static final String TEST_URL = "http://127.0.0.1:8000";
28 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; 28 private static final String URL_404 = "http://127.0.0.1:8000/notfound404";
29 private static final String MOCK_CRONET_TEST_FAILED_URL = 29 private static final String MOCK_CRONET_TEST_FAILED_URL =
30 "http://mock.failed.request/-2"; 30 "http://mock.failed.request/-2";
31 private static final String MOCK_CRONET_TEST_SUCCESS_URL = 31 private static final String MOCK_CRONET_TEST_SUCCESS_URL =
32 "http://mock.http/success.txt"; 32 "http://mock.http/success.txt";
33 33
34 CronetTestActivity mActivity; 34 CronetTestActivity mActivity;
35 35
36 static class RequestThread extends Thread { 36 static class RequestThread extends Thread {
37 public TestUrlRequestListener mListener; 37 public TestUrlRequestListener mListener;
38 38
39 final CronetTestActivity mActivity; 39 final CronetTestActivity mActivity;
40 final String mUrl; 40 final String mUrl;
41 final ConditionVariable mRunBlocker; 41 final ConditionVariable mRunBlocker;
42 42
43 public RequestThread(CronetTestActivity activity, String url, 43 public RequestThread(CronetTestActivity activity, String url,
44 ConditionVariable runBlocker) { 44 ConditionVariable runBlocker) {
45 mActivity = activity; 45 mActivity = activity;
46 mUrl = url; 46 mUrl = url;
47 mRunBlocker = runBlocker; 47 mRunBlocker = runBlocker;
48 } 48 }
49 49
50 @Override 50 @Override
51 public void run() { 51 public void run() {
52 mRunBlocker.block(); 52 mRunBlocker.block();
53 UrlRequestContext requestContext = mActivity.initRequestContext(); 53 CronetEngine cronetEngine = mActivity.initCronetEngine();
54 mListener = new TestUrlRequestListener(); 54 mListener = new TestUrlRequestListener();
55 UrlRequest urlRequest = 55 UrlRequest.Builder builder =
56 requestContext.createRequest(mUrl, mListener, mListener.getE xecutor()); 56 new UrlRequest.Builder(mUrl, mListener, mListener.getExecuto r(), cronetEngine);
57 urlRequest.start(); 57 builder.build().start();
58 mListener.blockForDone(); 58 mListener.blockForDone();
59 } 59 }
60 } 60 }
61 61
62 /** 62 /**
63 * Listener that shutdowns the request context when request has succeeded 63 * Listener that shutdowns the request context when request has succeeded
64 * or failed. 64 * or failed.
65 */ 65 */
66 class ShutdownTestUrlRequestListener extends TestUrlRequestListener { 66 class ShutdownTestUrlRequestListener extends TestUrlRequestListener {
67 @Override 67 @Override
68 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { 68 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) {
69 super.onSucceeded(request, info); 69 super.onSucceeded(request, info);
70 mActivity.mUrlRequestContext.shutdown(); 70 mActivity.mCronetEngine.shutdown();
71 } 71 }
72 72
73 @Override 73 @Override
74 public void onFailed(UrlRequest request, 74 public void onFailed(UrlRequest request,
75 ResponseInfo info, 75 ResponseInfo info,
76 UrlRequestException error) { 76 UrlRequestException error) {
77 super.onFailed(request, info, error); 77 super.onFailed(request, info, error);
78 mActivity.mUrlRequestContext.shutdown(); 78 mActivity.mCronetEngine.shutdown();
79 } 79 }
80 } 80 }
81 81
82 @SmallTest 82 @SmallTest
83 @Feature({"Cronet"}) 83 @Feature({"Cronet"})
84 public void testConfigUserAgent() throws Exception { 84 public void testConfigUserAgent() throws Exception {
85 String userAgentName = "User-Agent"; 85 String userAgentName = "User-Agent";
86 String userAgentValue = "User-Agent-Value"; 86 String userAgentValue = "User-Agent-Value";
87 UrlRequestContextConfig config = new UrlRequestContextConfig(); 87 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(mAct ivity);
88 config.setUserAgent(userAgentValue); 88 cronetEngineBuilder.setUserAgent(userAgentValue);
89 config.setLibraryName("cronet_tests"); 89 cronetEngineBuilder.setLibraryName("cronet_tests");
90 String[] commandLineArgs = { 90 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, cronetEngineB uilder.toString()};
91 CronetTestActivity.CONFIG_KEY, config.toString()
92 };
93 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, 91 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL,
94 commandLineArgs); 92 commandLineArgs);
95 assertTrue(NativeTestServer.startNativeTestServer( 93 assertTrue(NativeTestServer.startNativeTestServer(
96 getInstrumentation().getTargetContext())); 94 getInstrumentation().getTargetContext()));
97 TestUrlRequestListener listener = new TestUrlRequestListener(); 95 TestUrlRequestListener listener = new TestUrlRequestListener();
98 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 96 UrlRequest.Builder builder =
99 NativeTestServer.getEchoHeaderURL(userAgentName), listener, 97 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge ntName), listener,
100 listener.getExecutor()); 98 listener.getExecutor(), mActivity.mCronetEngine);
101 urlRequest.start(); 99 builder.build().start();
102 listener.blockForDone(); 100 listener.blockForDone();
103 assertEquals(userAgentValue, listener.mResponseAsString); 101 assertEquals(userAgentValue, listener.mResponseAsString);
104 } 102 }
105 103
106 @SmallTest 104 @SmallTest
107 @Feature({"Cronet"}) 105 @Feature({"Cronet"})
108 public void testDataReductionProxyEnabled() throws Exception { 106 public void testDataReductionProxyEnabled() throws Exception {
109 mActivity = launchCronetTestAppAndSkipFactoryInit(); 107 mActivity = launchCronetTestAppAndSkipFactoryInit();
110 108
111 // Ensure native code is loaded before trying to start test server. 109 // Ensure native code is loaded before trying to start test server.
112 UrlRequestContext.createContext( 110 new CronetEngine.Builder(mActivity).setLibraryName("cronet_tests").build ().shutdown();
mef 2015/10/02 16:46:58 is mActivity a correct target context here? Should
pauljensen 2015/10/02 18:23:15 I should have left this one as getInstrumentation(
113 getInstrumentation().getTargetContext(),
114 new UrlRequestContextConfig().setLibraryName("cronet_tests"))
115 .shutdown();
116 111
117 assertTrue(NativeTestServer.startNativeTestServer( 112 assertTrue(NativeTestServer.startNativeTestServer(
118 getInstrumentation().getTargetContext())); 113 getInstrumentation().getTargetContext()));
119 if (!NativeTestServer.isDataReductionProxySupported()) { 114 if (!NativeTestServer.isDataReductionProxySupported()) {
120 return; 115 return;
121 } 116 }
122 String serverHostPort = NativeTestServer.getHostPort(); 117 String serverHostPort = NativeTestServer.getHostPort();
123 118
124 // Enable the Data Reduction Proxy and configure it to use the test 119 // Enable the Data Reduction Proxy and configure it to use the test
125 // server as its primary proxy, and to check successfully that this 120 // server as its primary proxy, and to check successfully that this
126 // proxy is OK to use. 121 // proxy is OK to use.
127 UrlRequestContextConfig config = new UrlRequestContextConfig(); 122 CronetEngine.Builder cronetEngineBuilder =
128 config.enableDataReductionProxy("test-key"); 123 new CronetEngine.Builder(getInstrumentation().getTargetContext() );
129 config.setDataReductionProxyOptions( 124 cronetEngineBuilder.enableDataReductionProxy("test-key");
130 serverHostPort, "unused.net:9999", 125 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused .net:9999",
131 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); 126 NativeTestServer.getFileURL("/secureproxychecksuccess.txt"));
132 config.setLibraryName("cronet_tests"); 127 cronetEngineBuilder.setLibraryName("cronet_tests");
133 mActivity.mUrlRequestContext = UrlRequestContext.createContext( 128 mActivity.mCronetEngine = cronetEngineBuilder.build();
134 getInstrumentation().getTargetContext(), config);
135 TestUrlRequestListener listener = new TestUrlRequestListener(); 129 TestUrlRequestListener listener = new TestUrlRequestListener();
136 130
137 // Construct and start a request that can only be returned by the test 131 // Construct and start a request that can only be returned by the test
138 // server. This request will fail if the configuration logic for the 132 // server. This request will fail if the configuration logic for the
139 // Data Reduction Proxy is not used. 133 // Data Reduction Proxy is not used.
140 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 134 UrlRequest.Builder builder = new UrlRequest.Builder(
141 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", 135 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", listener,
142 listener, listener.getExecutor()); 136 listener.getExecutor(), mActivity.mCronetEngine);
143 urlRequest.start(); 137 builder.build().start();
144 listener.blockForDone(); 138 listener.blockForDone();
145 139
146 // Verify that the request is successful and that the Data Reduction 140 // Verify that the request is successful and that the Data Reduction
147 // Proxy logic configured to use the test server as its proxy. 141 // Proxy logic configured to use the test server as its proxy.
148 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 142 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
149 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); 143 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer());
150 assertEquals( 144 assertEquals(
151 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", 145 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
152 listener.mResponseInfo.getUrl()); 146 listener.mResponseInfo.getUrl());
153 } 147 }
154 148
155 @SmallTest 149 @SmallTest
156 @Feature({"Cronet"}) 150 @Feature({"Cronet"})
157 public void testShutdown() throws Exception { 151 public void testShutdown() throws Exception {
158 mActivity = launchCronetTestApp(); 152 mActivity = launchCronetTestApp();
159 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); 153 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
160 // Block listener when response starts to verify that shutdown fails 154 // Block listener when response starts to verify that shutdown fails
161 // if there are active requests. 155 // if there are active requests.
162 listener.setAutoAdvance(false); 156 listener.setAutoAdvance(false);
163 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 157 UrlRequest.Builder builder = new UrlRequest.Builder(
mef 2015/10/02 16:46:58 minor nit: I'd call it urlRequestBuilder as 'build
pauljensen 2015/10/02 18:23:15 Done.
164 TEST_URL, listener, listener.getExecutor()); 158 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng ine);
159 UrlRequest urlRequest = builder.build();
165 urlRequest.start(); 160 urlRequest.start();
166 try { 161 try {
167 mActivity.mUrlRequestContext.shutdown(); 162 mActivity.mCronetEngine.shutdown();
168 fail("Should throw an exception"); 163 fail("Should throw an exception");
169 } catch (Exception e) { 164 } catch (Exception e) {
170 assertEquals("Cannot shutdown with active requests.", 165 assertEquals("Cannot shutdown with active requests.",
171 e.getMessage()); 166 e.getMessage());
172 } 167 }
173 168
174 listener.waitForNextStep(); 169 listener.waitForNextStep();
175 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); 170 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
176 try { 171 try {
177 mActivity.mUrlRequestContext.shutdown(); 172 mActivity.mCronetEngine.shutdown();
178 fail("Should throw an exception"); 173 fail("Should throw an exception");
179 } catch (Exception e) { 174 } catch (Exception e) {
180 assertEquals("Cannot shutdown with active requests.", 175 assertEquals("Cannot shutdown with active requests.",
181 e.getMessage()); 176 e.getMessage());
182 } 177 }
183 listener.startNextRead(urlRequest); 178 listener.startNextRead(urlRequest);
184 179
185 listener.waitForNextStep(); 180 listener.waitForNextStep();
186 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep); 181 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep);
187 try { 182 try {
188 mActivity.mUrlRequestContext.shutdown(); 183 mActivity.mCronetEngine.shutdown();
189 fail("Should throw an exception"); 184 fail("Should throw an exception");
190 } catch (Exception e) { 185 } catch (Exception e) {
191 assertEquals("Cannot shutdown with active requests.", 186 assertEquals("Cannot shutdown with active requests.",
192 e.getMessage()); 187 e.getMessage());
193 } 188 }
194 189
195 // May not have read all the data, in theory. Just enable auto-advance 190 // May not have read all the data, in theory. Just enable auto-advance
196 // and finish the request. 191 // and finish the request.
197 listener.setAutoAdvance(true); 192 listener.setAutoAdvance(true);
198 listener.startNextRead(urlRequest); 193 listener.startNextRead(urlRequest);
(...skipping 17 matching lines...) Expand all
216 fail("Caught " + e.getMessage()); 211 fail("Caught " + e.getMessage());
217 } 212 }
218 } 213 }
219 }; 214 };
220 // Ensure that test is not running on the main thread. 215 // Ensure that test is not running on the main thread.
221 assertTrue(Looper.getMainLooper() != Looper.myLooper()); 216 assertTrue(Looper.getMainLooper() != Looper.myLooper());
222 new Handler(Looper.getMainLooper()).post(blockingTask); 217 new Handler(Looper.getMainLooper()).post(blockingTask);
223 218
224 // Create new request context, but its initialization on the main thread 219 // Create new request context, but its initialization on the main thread
225 // will be stuck behind blockingTask. 220 // will be stuck behind blockingTask.
226 final UrlRequestContext requestContext = activity.initRequestContext(); 221 final CronetEngine cronetEngine = activity.initCronetEngine();
227 // Unblock the main thread, so context gets initialized and shutdown on 222 // Unblock the main thread, so context gets initialized and shutdown on
228 // it. 223 // it.
229 block.open(); 224 block.open();
230 // Shutdown will wait for init to complete on main thread. 225 // Shutdown will wait for init to complete on main thread.
231 requestContext.shutdown(); 226 cronetEngine.shutdown();
232 // Verify that context is shutdown. 227 // Verify that context is shutdown.
233 try { 228 try {
234 requestContext.stopNetLog(); 229 cronetEngine.stopNetLog();
235 fail("Should throw an exception."); 230 fail("Should throw an exception.");
236 } catch (Exception e) { 231 } catch (Exception e) {
237 assertEquals("Context is shut down.", e.getMessage()); 232 assertEquals("Engine is shut down.", e.getMessage());
238 } 233 }
239 } 234 }
240 235
241 @SmallTest 236 @SmallTest
242 @Feature({"Cronet"}) 237 @Feature({"Cronet"})
243 public void testInitAndShutdownOnMainThread() throws Exception { 238 public void testInitAndShutdownOnMainThread() throws Exception {
244 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t(); 239 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t();
245 final ConditionVariable block = new ConditionVariable(false); 240 final ConditionVariable block = new ConditionVariable(false);
246 241
247 // Post a task to main thread to init and shutdown on the main thread. 242 // Post a task to main thread to init and shutdown on the main thread.
248 Runnable blockingTask = new Runnable() { 243 Runnable blockingTask = new Runnable() {
249 public void run() { 244 public void run() {
250 // Create new request context, loading the library. 245 // Create new request context, loading the library.
251 final UrlRequestContext requestContext = activity.initRequestCon text(); 246 final CronetEngine cronetEngine = activity.initCronetEngine();
252 // Shutdown right after init. 247 // Shutdown right after init.
253 requestContext.shutdown(); 248 cronetEngine.shutdown();
254 // Verify that context is shutdown. 249 // Verify that context is shutdown.
255 try { 250 try {
256 requestContext.stopNetLog(); 251 cronetEngine.stopNetLog();
257 fail("Should throw an exception."); 252 fail("Should throw an exception.");
258 } catch (Exception e) { 253 } catch (Exception e) {
259 assertEquals("Context is shut down.", e.getMessage()); 254 assertEquals("Engine is shut down.", e.getMessage());
260 } 255 }
261 block.open(); 256 block.open();
262 } 257 }
263 }; 258 };
264 new Handler(Looper.getMainLooper()).post(blockingTask); 259 new Handler(Looper.getMainLooper()).post(blockingTask);
265 // Wait for shutdown to complete on main thread. 260 // Wait for shutdown to complete on main thread.
266 block.block(); 261 block.block();
267 } 262 }
268 263
269 @SmallTest 264 @SmallTest
270 @Feature({"Cronet"}) 265 @Feature({"Cronet"})
271 public void testMultipleShutdown() throws Exception { 266 public void testMultipleShutdown() throws Exception {
272 mActivity = launchCronetTestApp(); 267 mActivity = launchCronetTestApp();
273 try { 268 try {
274 mActivity.mUrlRequestContext.shutdown(); 269 mActivity.mCronetEngine.shutdown();
275 mActivity.mUrlRequestContext.shutdown(); 270 mActivity.mCronetEngine.shutdown();
276 fail("Should throw an exception"); 271 fail("Should throw an exception");
277 } catch (Exception e) { 272 } catch (Exception e) {
278 assertEquals("Context is shut down.", 273 assertEquals("Engine is shut down.", e.getMessage());
279 e.getMessage());
280 } 274 }
281 } 275 }
282 276
283 @SmallTest 277 @SmallTest
284 @Feature({"Cronet"}) 278 @Feature({"Cronet"})
285 public void testShutdownAfterError() throws Exception { 279 public void testShutdownAfterError() throws Exception {
286 mActivity = launchCronetTestApp(); 280 mActivity = launchCronetTestApp();
287 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); 281 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener();
288 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 282 UrlRequest.Builder builder = new UrlRequest.Builder(MOCK_CRONET_TEST_FAI LED_URL, listener,
289 MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor()); 283 listener.getExecutor(), mActivity.mCronetEngine);
290 urlRequest.start(); 284 builder.build().start();
291 listener.blockForDone(); 285 listener.blockForDone();
292 assertTrue(listener.mOnErrorCalled); 286 assertTrue(listener.mOnErrorCalled);
293 } 287 }
294 288
295 @SmallTest 289 @SmallTest
296 @Feature({"Cronet"}) 290 @Feature({"Cronet"})
297 public void testShutdownAfterCancel() throws Exception { 291 public void testShutdownAfterCancel() throws Exception {
298 mActivity = launchCronetTestApp(); 292 mActivity = launchCronetTestApp();
299 TestUrlRequestListener listener = new TestUrlRequestListener(); 293 TestUrlRequestListener listener = new TestUrlRequestListener();
300 // Block listener when response starts to verify that shutdown fails 294 // Block listener when response starts to verify that shutdown fails
301 // if there are active requests. 295 // if there are active requests.
302 listener.setAutoAdvance(false); 296 listener.setAutoAdvance(false);
303 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 297 UrlRequest.Builder builder = new UrlRequest.Builder(
304 TEST_URL, listener, listener.getExecutor()); 298 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng ine);
299 UrlRequest urlRequest = builder.build();
305 urlRequest.start(); 300 urlRequest.start();
306 try { 301 try {
307 mActivity.mUrlRequestContext.shutdown(); 302 mActivity.mCronetEngine.shutdown();
308 fail("Should throw an exception"); 303 fail("Should throw an exception");
309 } catch (Exception e) { 304 } catch (Exception e) {
310 assertEquals("Cannot shutdown with active requests.", 305 assertEquals("Cannot shutdown with active requests.",
311 e.getMessage()); 306 e.getMessage());
312 } 307 }
313 listener.waitForNextStep(); 308 listener.waitForNextStep();
314 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); 309 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep);
315 urlRequest.cancel(); 310 urlRequest.cancel();
316 mActivity.mUrlRequestContext.shutdown(); 311 mActivity.mCronetEngine.shutdown();
317 } 312 }
318 313
319 @SmallTest 314 @SmallTest
320 @Feature({"Cronet"}) 315 @Feature({"Cronet"})
321 public void testNetLog() throws Exception { 316 public void testNetLog() throws Exception {
322 Context context = getInstrumentation().getTargetContext(); 317 Context context = getInstrumentation().getTargetContext();
323 File directory = new File(PathUtils.getDataDirectory(context)); 318 File directory = new File(PathUtils.getDataDirectory(context));
324 File file = File.createTempFile("cronet", "json", directory); 319 File file = File.createTempFile("cronet", "json", directory);
325 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( 320 CronetEngine cronetEngine = new CronetUrlRequestContext(
326 context, 321 new CronetEngine.Builder(context).setLibraryName("cronet_tests") );
327 new UrlRequestContextConfig().setLibraryName("cronet_tests"));
328 // Start NetLog immediately after the request context is created to make 322 // Start NetLog immediately after the request context is created to make
329 // sure that the call won't crash the app even when the native request 323 // sure that the call won't crash the app even when the native request
330 // context is not fully initialized. See crbug.com/470196. 324 // context is not fully initialized. See crbug.com/470196.
331 requestContext.startNetLogToFile(file.getPath(), false); 325 cronetEngine.startNetLogToFile(file.getPath(), false);
332 326
333 // Start a request. 327 // Start a request.
334 TestUrlRequestListener listener = new TestUrlRequestListener(); 328 TestUrlRequestListener listener = new TestUrlRequestListener();
335 UrlRequest request = requestContext.createRequest( 329 UrlRequest.Builder builder =
336 TEST_URL, listener, listener.getExecutor()); 330 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( ), cronetEngine);
337 request.start(); 331 builder.build().start();
338 listener.blockForDone(); 332 listener.blockForDone();
339 requestContext.stopNetLog(); 333 cronetEngine.stopNetLog();
340 assertTrue(file.exists()); 334 assertTrue(file.exists());
341 assertTrue(file.length() != 0); 335 assertTrue(file.length() != 0);
342 assertFalse(hasBytesInNetLog(file)); 336 assertFalse(hasBytesInNetLog(file));
343 assertTrue(file.delete()); 337 assertTrue(file.delete());
344 assertTrue(!file.exists()); 338 assertTrue(!file.exists());
345 } 339 }
346 340
347 @SmallTest 341 @SmallTest
348 @Feature({"Cronet"}) 342 @Feature({"Cronet"})
349 public void testNetLogAfterShutdown() throws Exception { 343 public void testNetLogAfterShutdown() throws Exception {
350 mActivity = launchCronetTestApp(); 344 mActivity = launchCronetTestApp();
351 TestUrlRequestListener listener = new TestUrlRequestListener(); 345 TestUrlRequestListener listener = new TestUrlRequestListener();
352 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 346 UrlRequest.Builder builder = new UrlRequest.Builder(
353 TEST_URL, listener, listener.getExecutor()); 347 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng ine);
354 urlRequest.start(); 348 builder.build().start();
355 listener.blockForDone(); 349 listener.blockForDone();
356 mActivity.mUrlRequestContext.shutdown(); 350 mActivity.mCronetEngine.shutdown();
357 351
358 File directory = new File(PathUtils.getDataDirectory( 352 File directory = new File(PathUtils.getDataDirectory(
359 getInstrumentation().getTargetContext())); 353 getInstrumentation().getTargetContext()));
360 File file = File.createTempFile("cronet", "json", directory); 354 File file = File.createTempFile("cronet", "json", directory);
361 try { 355 try {
362 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), 356 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
363 false);
364 fail("Should throw an exception."); 357 fail("Should throw an exception.");
365 } catch (Exception e) { 358 } catch (Exception e) {
366 assertEquals("Context is shut down.", e.getMessage()); 359 assertEquals("Engine is shut down.", e.getMessage());
367 } 360 }
368 assertFalse(hasBytesInNetLog(file)); 361 assertFalse(hasBytesInNetLog(file));
369 assertTrue(file.delete()); 362 assertTrue(file.delete());
370 assertTrue(!file.exists()); 363 assertTrue(!file.exists());
371 } 364 }
372 365
373 @SmallTest 366 @SmallTest
374 @Feature({"Cronet"}) 367 @Feature({"Cronet"})
375 public void testNetLogStartMultipleTimes() throws Exception { 368 public void testNetLogStartMultipleTimes() throws Exception {
376 mActivity = launchCronetTestApp(); 369 mActivity = launchCronetTestApp();
377 File directory = new File(PathUtils.getDataDirectory( 370 File directory = new File(PathUtils.getDataDirectory(
378 getInstrumentation().getTargetContext())); 371 getInstrumentation().getTargetContext()));
379 File file = File.createTempFile("cronet", "json", directory); 372 File file = File.createTempFile("cronet", "json", directory);
380 // Start NetLog multiple times. 373 // Start NetLog multiple times.
381 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); 374 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
382 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); 375 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
383 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); 376 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
384 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); 377 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
385 // Start a request. 378 // Start a request.
386 TestUrlRequestListener listener = new TestUrlRequestListener(); 379 TestUrlRequestListener listener = new TestUrlRequestListener();
387 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 380 UrlRequest.Builder builder = new UrlRequest.Builder(
388 TEST_URL, listener, listener.getExecutor()); 381 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng ine);
389 urlRequest.start(); 382 builder.build().start();
390 listener.blockForDone(); 383 listener.blockForDone();
391 mActivity.mUrlRequestContext.stopNetLog(); 384 mActivity.mCronetEngine.stopNetLog();
392 assertTrue(file.exists()); 385 assertTrue(file.exists());
393 assertTrue(file.length() != 0); 386 assertTrue(file.length() != 0);
394 assertFalse(hasBytesInNetLog(file)); 387 assertFalse(hasBytesInNetLog(file));
395 assertTrue(file.delete()); 388 assertTrue(file.delete());
396 assertTrue(!file.exists()); 389 assertTrue(!file.exists());
397 } 390 }
398 391
399 @SmallTest 392 @SmallTest
400 @Feature({"Cronet"}) 393 @Feature({"Cronet"})
401 public void testNetLogStopMultipleTimes() throws Exception { 394 public void testNetLogStopMultipleTimes() throws Exception {
402 mActivity = launchCronetTestApp(); 395 mActivity = launchCronetTestApp();
403 File directory = new File(PathUtils.getDataDirectory( 396 File directory = new File(PathUtils.getDataDirectory(
404 getInstrumentation().getTargetContext())); 397 getInstrumentation().getTargetContext()));
405 File file = File.createTempFile("cronet", "json", directory); 398 File file = File.createTempFile("cronet", "json", directory);
406 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); 399 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false);
407 // Start a request. 400 // Start a request.
408 TestUrlRequestListener listener = new TestUrlRequestListener(); 401 TestUrlRequestListener listener = new TestUrlRequestListener();
409 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 402 UrlRequest.Builder builder = new UrlRequest.Builder(
410 TEST_URL, listener, listener.getExecutor()); 403 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng ine);
411 urlRequest.start(); 404 builder.build().start();
412 listener.blockForDone(); 405 listener.blockForDone();
413 // Stop NetLog multiple times. 406 // Stop NetLog multiple times.
414 mActivity.mUrlRequestContext.stopNetLog(); 407 mActivity.mCronetEngine.stopNetLog();
415 mActivity.mUrlRequestContext.stopNetLog(); 408 mActivity.mCronetEngine.stopNetLog();
416 mActivity.mUrlRequestContext.stopNetLog(); 409 mActivity.mCronetEngine.stopNetLog();
417 mActivity.mUrlRequestContext.stopNetLog(); 410 mActivity.mCronetEngine.stopNetLog();
418 mActivity.mUrlRequestContext.stopNetLog(); 411 mActivity.mCronetEngine.stopNetLog();
419 assertTrue(file.exists()); 412 assertTrue(file.exists());
420 assertTrue(file.length() != 0); 413 assertTrue(file.length() != 0);
421 assertFalse(hasBytesInNetLog(file)); 414 assertFalse(hasBytesInNetLog(file));
422 assertTrue(file.delete()); 415 assertTrue(file.delete());
423 assertTrue(!file.exists()); 416 assertTrue(!file.exists());
424 } 417 }
425 418
426 @SmallTest 419 @SmallTest
427 @Feature({"Cronet"}) 420 @Feature({"Cronet"})
428 public void testNetLogWithBytes() throws Exception { 421 public void testNetLogWithBytes() throws Exception {
429 Context context = getInstrumentation().getTargetContext(); 422 Context context = getInstrumentation().getTargetContext();
430 File directory = new File(PathUtils.getDataDirectory(context)); 423 File directory = new File(PathUtils.getDataDirectory(context));
431 File file = File.createTempFile("cronet", "json", directory); 424 File file = File.createTempFile("cronet", "json", directory);
432 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( 425 CronetEngine cronetEngine = new CronetUrlRequestContext(
433 context, 426 new CronetEngine.Builder(context).setLibraryName("cronet_tests") );
434 new UrlRequestContextConfig().setLibraryName("cronet_tests"));
435 // Start NetLog with logAll as true. 427 // Start NetLog with logAll as true.
436 requestContext.startNetLogToFile(file.getPath(), true); 428 cronetEngine.startNetLogToFile(file.getPath(), true);
437 // Start a request. 429 // Start a request.
438 TestUrlRequestListener listener = new TestUrlRequestListener(); 430 TestUrlRequestListener listener = new TestUrlRequestListener();
439 UrlRequest request = requestContext.createRequest( 431 UrlRequest.Builder builder =
440 TEST_URL, listener, listener.getExecutor()); 432 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( ), cronetEngine);
441 request.start(); 433 builder.build().start();
442 listener.blockForDone(); 434 listener.blockForDone();
443 requestContext.stopNetLog(); 435 cronetEngine.stopNetLog();
444 assertTrue(file.exists()); 436 assertTrue(file.exists());
445 assertTrue(file.length() != 0); 437 assertTrue(file.length() != 0);
446 assertTrue(hasBytesInNetLog(file)); 438 assertTrue(hasBytesInNetLog(file));
447 assertTrue(file.delete()); 439 assertTrue(file.delete());
448 assertTrue(!file.exists()); 440 assertTrue(!file.exists());
449 } 441 }
450 442
451 private boolean hasBytesInNetLog(File logFile) throws Exception { 443 private boolean hasBytesInNetLog(File logFile) throws Exception {
452 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); 444 BufferedReader logReader = new BufferedReader(new FileReader(logFile));
453 try { 445 try {
454 String logLine; 446 String logLine;
455 while ((logLine = logReader.readLine()) != null) { 447 while ((logLine = logReader.readLine()) != null) {
456 if (logLine.contains("\"hex_encoded_bytes\"")) { 448 if (logLine.contains("\"hex_encoded_bytes\"")) {
457 return true; 449 return true;
458 } 450 }
459 } 451 }
460 return false; 452 return false;
461 } finally { 453 } finally {
462 logReader.close(); 454 logReader.close();
463 } 455 }
464 } 456 }
465 457
466 private void enableCache(int cacheType) throws Exception { 458 private void enableCache(int cacheType) throws Exception {
467 String cacheTypeString = ""; 459 String cacheTypeString = "";
468 if (cacheType == UrlRequestContextConfig.HTTP_CACHE_DISK) { 460 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) {
469 cacheTypeString = CronetTestActivity.CACHE_DISK; 461 cacheTypeString = CronetTestActivity.CACHE_DISK;
470 } else if (cacheType == UrlRequestContextConfig.HTTP_CACHE_DISK_NO_HTTP) { 462 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) {
471 cacheTypeString = CronetTestActivity.CACHE_DISK_NO_HTTP; 463 cacheTypeString = CronetTestActivity.CACHE_DISK_NO_HTTP;
472 } else if (cacheType == UrlRequestContextConfig.HTTP_CACHE_IN_MEMORY) { 464 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) {
473 cacheTypeString = CronetTestActivity.CACHE_IN_MEMORY; 465 cacheTypeString = CronetTestActivity.CACHE_IN_MEMORY;
474 } 466 }
475 String[] commandLineArgs = {CronetTestActivity.CACHE_KEY, cacheTypeStrin g}; 467 String[] commandLineArgs = {CronetTestActivity.CACHE_KEY, cacheTypeStrin g};
476 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, 468 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null,
477 commandLineArgs); 469 commandLineArgs);
478 assertTrue(NativeTestServer.startNativeTestServer( 470 assertTrue(NativeTestServer.startNativeTestServer(
479 getInstrumentation().getTargetContext())); 471 getInstrumentation().getTargetContext()));
480 } 472 }
481 473
482 private void checkRequestCaching(String url, boolean expectCached) { 474 private void checkRequestCaching(String url, boolean expectCached) {
483 checkRequestCaching(url, expectCached, false); 475 checkRequestCaching(url, expectCached, false);
484 } 476 }
485 477
486 private void checkRequestCaching(String url, boolean expectCached, 478 private void checkRequestCaching(String url, boolean expectCached,
487 boolean disableCache) { 479 boolean disableCache) {
488 TestUrlRequestListener listener = new TestUrlRequestListener(); 480 TestUrlRequestListener listener = new TestUrlRequestListener();
489 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 481 UrlRequest.Builder builder = new UrlRequest.Builder(
490 url, listener, listener.getExecutor()); 482 url, listener, listener.getExecutor(), mActivity.mCronetEngine);
491 if (disableCache) { 483 if (disableCache) {
492 urlRequest.disableCache(); 484 builder.disableCache();
493 } 485 }
494 urlRequest.start(); 486 builder.build().start();
495 listener.blockForDone(); 487 listener.blockForDone();
496 assertEquals(expectCached, listener.mResponseInfo.wasCached()); 488 assertEquals(expectCached, listener.mResponseInfo.wasCached());
497 } 489 }
498 490
499 @SmallTest 491 @SmallTest
500 @Feature({"Cronet"}) 492 @Feature({"Cronet"})
501 public void testEnableHttpCacheDisabled() throws Exception { 493 public void testEnableHttpCacheDisabled() throws Exception {
502 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISABLED); 494 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED);
503 String url = NativeTestServer.getFileURL("/cacheable.txt"); 495 String url = NativeTestServer.getFileURL("/cacheable.txt");
504 checkRequestCaching(url, false); 496 checkRequestCaching(url, false);
505 checkRequestCaching(url, false); 497 checkRequestCaching(url, false);
506 checkRequestCaching(url, false); 498 checkRequestCaching(url, false);
507 } 499 }
508 500
509 @SmallTest 501 @SmallTest
510 @Feature({"Cronet"}) 502 @Feature({"Cronet"})
511 public void testEnableHttpCacheInMemory() throws Exception { 503 public void testEnableHttpCacheInMemory() throws Exception {
512 enableCache(UrlRequestContextConfig.HTTP_CACHE_IN_MEMORY); 504 enableCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY);
513 String url = NativeTestServer.getFileURL("/cacheable.txt"); 505 String url = NativeTestServer.getFileURL("/cacheable.txt");
514 checkRequestCaching(url, false); 506 checkRequestCaching(url, false);
515 checkRequestCaching(url, true); 507 checkRequestCaching(url, true);
516 NativeTestServer.shutdownNativeTestServer(); 508 NativeTestServer.shutdownNativeTestServer();
517 checkRequestCaching(url, true); 509 checkRequestCaching(url, true);
518 } 510 }
519 511
520 @SmallTest 512 @SmallTest
521 @Feature({"Cronet"}) 513 @Feature({"Cronet"})
522 public void testEnableHttpCacheDisk() throws Exception { 514 public void testEnableHttpCacheDisk() throws Exception {
523 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISK); 515 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
524 String url = NativeTestServer.getFileURL("/cacheable.txt"); 516 String url = NativeTestServer.getFileURL("/cacheable.txt");
525 checkRequestCaching(url, false); 517 checkRequestCaching(url, false);
526 checkRequestCaching(url, true); 518 checkRequestCaching(url, true);
527 NativeTestServer.shutdownNativeTestServer(); 519 NativeTestServer.shutdownNativeTestServer();
528 checkRequestCaching(url, true); 520 checkRequestCaching(url, true);
529 } 521 }
530 522
531 @SmallTest 523 @SmallTest
532 @Feature({"Cronet"}) 524 @Feature({"Cronet"})
533 public void testEnableHttpCacheDiskNoHttp() throws Exception { 525 public void testEnableHttpCacheDiskNoHttp() throws Exception {
534 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISABLED); 526 enableCache(CronetEngine.Builder.HTTP_CACHE_DISABLED);
535 String url = NativeTestServer.getFileURL("/cacheable.txt"); 527 String url = NativeTestServer.getFileURL("/cacheable.txt");
536 checkRequestCaching(url, false); 528 checkRequestCaching(url, false);
537 checkRequestCaching(url, false); 529 checkRequestCaching(url, false);
538 checkRequestCaching(url, false); 530 checkRequestCaching(url, false);
539 } 531 }
540 532
541 @SmallTest 533 @SmallTest
542 @Feature({"Cronet"}) 534 @Feature({"Cronet"})
543 public void testDisableCache() throws Exception { 535 public void testDisableCache() throws Exception {
544 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISK); 536 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
545 String url = NativeTestServer.getFileURL("/cacheable.txt"); 537 String url = NativeTestServer.getFileURL("/cacheable.txt");
546 538
547 // When cache is disabled, making a request does not write to the cache. 539 // When cache is disabled, making a request does not write to the cache.
548 checkRequestCaching(url, false, true /** disable cache */); 540 checkRequestCaching(url, false, true /** disable cache */);
549 checkRequestCaching(url, false); 541 checkRequestCaching(url, false);
550 542
551 // When cache is enabled, the second request is cached. 543 // When cache is enabled, the second request is cached.
552 checkRequestCaching(url, false, true /** disable cache */); 544 checkRequestCaching(url, false, true /** disable cache */);
553 checkRequestCaching(url, true); 545 checkRequestCaching(url, true);
554 546
555 // Shut down the server, next request should have a cached response. 547 // Shut down the server, next request should have a cached response.
556 NativeTestServer.shutdownNativeTestServer(); 548 NativeTestServer.shutdownNativeTestServer();
557 checkRequestCaching(url, true); 549 checkRequestCaching(url, true);
558 550
559 // Cache is disabled after server is shut down, request should fail. 551 // Cache is disabled after server is shut down, request should fail.
560 TestUrlRequestListener listener = new TestUrlRequestListener(); 552 TestUrlRequestListener listener = new TestUrlRequestListener();
561 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( 553 UrlRequest.Builder builder = new UrlRequest.Builder(
562 url, listener, listener.getExecutor()); 554 url, listener, listener.getExecutor(), mActivity.mCronetEngine);
563 urlRequest.disableCache(); 555 builder.disableCache();
564 urlRequest.start(); 556 builder.build().start();
565 listener.blockForDone(); 557 listener.blockForDone();
566 assertNotNull(listener.mError); 558 assertNotNull(listener.mError);
567 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ", 559 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ",
568 listener.mError.getMessage()); 560 listener.mError.getMessage());
569 } 561 }
570 562
571 @SmallTest 563 @SmallTest
572 @Feature({"Cronet"}) 564 @Feature({"Cronet"})
573 public void testEnableHttpCacheDiskNewContext() throws Exception { 565 public void testEnableHttpCacheDiskNewEngine() throws Exception {
574 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISK); 566 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK);
575 String url = NativeTestServer.getFileURL("/cacheable.txt"); 567 String url = NativeTestServer.getFileURL("/cacheable.txt");
576 checkRequestCaching(url, false); 568 checkRequestCaching(url, false);
577 checkRequestCaching(url, true); 569 checkRequestCaching(url, true);
578 NativeTestServer.shutdownNativeTestServer(); 570 NativeTestServer.shutdownNativeTestServer();
579 checkRequestCaching(url, true); 571 checkRequestCaching(url, true);
580 572
581 // Shutdown original context and create another that uses the same cache . 573 // Shutdown original context and create another that uses the same cache .
582 mActivity.mUrlRequestContext.shutdown(); 574 mActivity.mCronetEngine.shutdown();
583 mActivity.mUrlRequestContext = UrlRequestContext.createContext( 575 mActivity.mCronetEngine = mActivity.getCronetEngineBuilder().build();
584 getInstrumentation().getTargetContext(), mActivity.getContextCon fig());
585 checkRequestCaching(url, true); 576 checkRequestCaching(url, true);
586 } 577 }
587 578
588 @SmallTest 579 @SmallTest
589 @Feature({"Cronet"}) 580 @Feature({"Cronet"})
590 public void testInitContextAndStartRequest() { 581 public void testInitEngineAndStartRequest() {
591 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); 582 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();
592 583
593 // Immediately make a request after initializing the context. 584 // Immediately make a request after initializing the engine.
594 UrlRequestContext requestContext = activity.initRequestContext(); 585 CronetEngine cronetEngine = activity.initCronetEngine();
595 TestUrlRequestListener listener = new TestUrlRequestListener(); 586 TestUrlRequestListener listener = new TestUrlRequestListener();
596 UrlRequest urlRequest = 587 UrlRequest.Builder builder =
597 requestContext.createRequest(TEST_URL, listener, listener.getExe cutor()); 588 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( ), cronetEngine);
598 urlRequest.start(); 589 builder.build().start();
599 listener.blockForDone(); 590 listener.blockForDone();
600 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); 591 assertEquals(200, listener.mResponseInfo.getHttpStatusCode());
601 } 592 }
602 593
603 @SmallTest 594 @SmallTest
604 @Feature({"Cronet"}) 595 @Feature({"Cronet"})
605 public void testInitContextStartTwoRequests() throws Exception { 596 public void testInitEngineStartTwoRequests() throws Exception {
606 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); 597 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit();
607 598
608 // Make two requests after initializing the context. 599 // Make two requests after initializing the context.
609 UrlRequestContext requestContext = activity.initRequestContext(); 600 CronetEngine cronetEngine = activity.initCronetEngine();
610 int[] statusCodes = {0, 0}; 601 int[] statusCodes = {0, 0};
611 String[] urls = {TEST_URL, URL_404}; 602 String[] urls = {TEST_URL, URL_404};
612 for (int i = 0; i < 2; i++) { 603 for (int i = 0; i < 2; i++) {
613 TestUrlRequestListener listener = new TestUrlRequestListener(); 604 TestUrlRequestListener listener = new TestUrlRequestListener();
614 UrlRequest urlRequest = 605 UrlRequest.Builder builder =
615 requestContext.createRequest(urls[i], listener, listener.get Executor()); 606 new UrlRequest.Builder(urls[i], listener, listener.getExecut or(), cronetEngine);
616 urlRequest.start(); 607 builder.build().start();
617 listener.blockForDone(); 608 listener.blockForDone();
618 statusCodes[i] = listener.mResponseInfo.getHttpStatusCode(); 609 statusCodes[i] = listener.mResponseInfo.getHttpStatusCode();
619 } 610 }
620 assertEquals(200, statusCodes[0]); 611 assertEquals(200, statusCodes[0]);
621 assertEquals(404, statusCodes[1]); 612 assertEquals(404, statusCodes[1]);
622 } 613 }
623 614
624 @SmallTest 615 @SmallTest
625 @Feature({"Cronet"}) 616 @Feature({"Cronet"})
626 public void testInitTwoContextsSimultaneously() throws Exception { 617 public void testInitTwoEnginesSimultaneously() throws Exception {
627 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t(); 618 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t();
628 619
629 // Threads will block on runBlocker to ensure simultaneous execution. 620 // Threads will block on runBlocker to ensure simultaneous execution.
630 ConditionVariable runBlocker = new ConditionVariable(false); 621 ConditionVariable runBlocker = new ConditionVariable(false);
631 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker ); 622 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker );
632 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker) ; 623 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker) ;
633 624
634 thread1.start(); 625 thread1.start();
635 thread2.start(); 626 thread2.start();
636 runBlocker.open(); 627 runBlocker.open();
637 thread1.join(); 628 thread1.join();
638 thread2.join(); 629 thread2.join();
639 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); 630 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode());
640 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); 631 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode());
641 } 632 }
642 633
643 @SmallTest 634 @SmallTest
644 @Feature({"Cronet"}) 635 @Feature({"Cronet"})
645 public void testInitTwoContextsInSequence() throws Exception { 636 public void testInitTwoEnginesInSequence() throws Exception {
646 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t(); 637 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t();
647 638
648 ConditionVariable runBlocker = new ConditionVariable(true); 639 ConditionVariable runBlocker = new ConditionVariable(true);
649 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker ); 640 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker );
650 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker) ; 641 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker) ;
651 642
652 thread1.start(); 643 thread1.start();
653 thread1.join(); 644 thread1.join();
654 thread2.start(); 645 thread2.start();
655 thread2.join(); 646 thread2.join();
656 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); 647 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode());
657 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); 648 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode());
658 } 649 }
659 650
660 @SmallTest 651 @SmallTest
661 @Feature({"Cronet"}) 652 @Feature({"Cronet"})
662 public void testInitDifferentContexts() throws Exception { 653 public void testInitDifferentEngines() throws Exception {
663 // Test that concurrently instantiating Cronet context's upon various 654 // Test that concurrently instantiating Cronet context's upon various
664 // different versions of the same Android Context does not cause crashes 655 // different versions of the same Android Context does not cause crashes
665 // like crbug.com/453845 656 // like crbug.com/453845
666 mActivity = launchCronetTestApp(); 657 mActivity = launchCronetTestApp();
667 CronetUrlRequestContext firstContext = 658 CronetEngine firstEngine =
668 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi g()); 659 new CronetUrlRequestContext(mActivity.createCronetEngineBuilder( mActivity));
669 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( 660 CronetEngine secondEngine = new CronetUrlRequestContext(
670 mActivity.getApplicationContext(), mActivity.getContextConfig()) ; 661 mActivity.createCronetEngineBuilder(mActivity.getApplicationCont ext()));
671 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( 662 CronetEngine thirdEngine = new CronetUrlRequestContext(
672 new ContextWrapper(mActivity), mActivity.getContextConfig()); 663 mActivity.createCronetEngineBuilder(new ContextWrapper(mActivity )));
673 firstContext.shutdown(); 664 firstEngine.shutdown();
674 secondContext.shutdown(); 665 secondEngine.shutdown();
675 thirdContext.shutdown(); 666 thirdEngine.shutdown();
676 } 667 }
677 } 668 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698