OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.ContextWrapper; | 8 import android.content.ContextWrapper; |
9 import android.os.ConditionVariable; | 9 import android.os.ConditionVariable; |
10 import android.os.Handler; | 10 import android.os.Handler; |
11 import android.os.Looper; | 11 import android.os.Looper; |
12 import android.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 CronetUrlRequestContext. |
mef
2015/09/25 21:32:19
CronetEngine
pauljensen
2015/09/28 14:18:12
Done. I had left this as CronetUrlRequestContext
| |
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()); |
57 urlRequest.start(); | 57 cronetEngine.executeRequest(builder); |
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(); |
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 = new UrlRequest.Builder( |
99 NativeTestServer.getEchoHeaderURL(userAgentName), listener, | 97 NativeTestServer.getEchoHeaderURL(userAgentName), listener, list ener.getExecutor()); |
100 listener.getExecutor()); | 98 mActivity.mCronetEngine.executeRequest(builder); |
mef
2015/09/25 21:32:19
I wonder if anybody is using a pattern where they
pauljensen
2015/09/28 14:18:12
Potentially but they can now create the UrlRequest
| |
101 urlRequest.start(); | |
102 listener.blockForDone(); | 99 listener.blockForDone(); |
103 assertEquals(userAgentValue, listener.mResponseAsString); | 100 assertEquals(userAgentValue, listener.mResponseAsString); |
104 } | 101 } |
105 | 102 |
106 @SmallTest | 103 @SmallTest |
107 @Feature({"Cronet"}) | 104 @Feature({"Cronet"}) |
108 public void testDataReductionProxyEnabled() throws Exception { | 105 public void testDataReductionProxyEnabled() throws Exception { |
109 mActivity = launchCronetTestAppAndSkipFactoryInit(); | 106 mActivity = launchCronetTestAppAndSkipFactoryInit(); |
110 | 107 |
111 // Ensure native code is loaded before trying to start test server. | 108 // Ensure native code is loaded before trying to start test server. |
112 UrlRequestContext.createContext( | 109 new CronetEngine.Builder() |
113 getInstrumentation().getTargetContext(), | 110 .setLibraryName("cronet_tests") |
114 new UrlRequestContextConfig().setLibraryName("cronet_tests")) | 111 .build(getInstrumentation().getTargetContext()) |
115 .shutdown(); | 112 .shutdown(); |
116 | 113 |
117 assertTrue(NativeTestServer.startNativeTestServer( | 114 assertTrue(NativeTestServer.startNativeTestServer( |
118 getInstrumentation().getTargetContext())); | 115 getInstrumentation().getTargetContext())); |
119 if (!NativeTestServer.isDataReductionProxySupported()) { | 116 if (!NativeTestServer.isDataReductionProxySupported()) { |
120 return; | 117 return; |
121 } | 118 } |
122 String serverHostPort = NativeTestServer.getHostPort(); | 119 String serverHostPort = NativeTestServer.getHostPort(); |
123 | 120 |
124 // Enable the Data Reduction Proxy and configure it to use the test | 121 // Enable the Data Reduction Proxy and configure it to use the test |
125 // server as its primary proxy, and to check successfully that this | 122 // server as its primary proxy, and to check successfully that this |
126 // proxy is OK to use. | 123 // proxy is OK to use. |
127 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 124 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(); |
128 config.enableDataReductionProxy("test-key"); | 125 cronetEngineBuilder.enableDataReductionProxy("test-key"); |
129 config.setDataReductionProxyOptions( | 126 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused .net:9999", |
130 serverHostPort, "unused.net:9999", | |
131 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); | 127 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); |
132 config.setLibraryName("cronet_tests"); | 128 cronetEngineBuilder.setLibraryName("cronet_tests"); |
133 mActivity.mUrlRequestContext = UrlRequestContext.createContext( | 129 mActivity.mCronetEngine = |
134 getInstrumentation().getTargetContext(), config); | 130 cronetEngineBuilder.build(getInstrumentation().getTargetContext( )); |
135 TestUrlRequestListener listener = new TestUrlRequestListener(); | 131 TestUrlRequestListener listener = new TestUrlRequestListener(); |
136 | 132 |
137 // Construct and start a request that can only be returned by the test | 133 // 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 | 134 // server. This request will fail if the configuration logic for the |
139 // Data Reduction Proxy is not used. | 135 // Data Reduction Proxy is not used. |
140 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 136 UrlRequest.Builder builder = new UrlRequest.Builder( |
141 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", | 137 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", listener, |
142 listener, listener.getExecutor()); | 138 listener.getExecutor()); |
143 urlRequest.start(); | 139 mActivity.mCronetEngine.executeRequest(builder); |
144 listener.blockForDone(); | 140 listener.blockForDone(); |
145 | 141 |
146 // Verify that the request is successful and that the Data Reduction | 142 // Verify that the request is successful and that the Data Reduction |
147 // Proxy logic configured to use the test server as its proxy. | 143 // Proxy logic configured to use the test server as its proxy. |
148 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 144 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
149 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); | 145 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); |
150 assertEquals( | 146 assertEquals( |
151 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", | 147 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", |
152 listener.mResponseInfo.getUrl()); | 148 listener.mResponseInfo.getUrl()); |
153 } | 149 } |
154 | 150 |
155 @SmallTest | 151 @SmallTest |
156 @Feature({"Cronet"}) | 152 @Feature({"Cronet"}) |
157 public void testShutdown() throws Exception { | 153 public void testShutdown() throws Exception { |
158 mActivity = launchCronetTestApp(); | 154 mActivity = launchCronetTestApp(); |
159 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); | 155 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |
160 // Block listener when response starts to verify that shutdown fails | 156 // Block listener when response starts to verify that shutdown fails |
161 // if there are active requests. | 157 // if there are active requests. |
162 listener.setAutoAdvance(false); | 158 listener.setAutoAdvance(false); |
163 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 159 UrlRequest.Builder builder = |
164 TEST_URL, listener, listener.getExecutor()); | 160 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
165 urlRequest.start(); | 161 UrlRequest urlRequest = mActivity.mCronetEngine.executeRequest(builder); |
166 try { | 162 try { |
167 mActivity.mUrlRequestContext.shutdown(); | 163 mActivity.mCronetEngine.shutdown(); |
168 fail("Should throw an exception"); | 164 fail("Should throw an exception"); |
169 } catch (Exception e) { | 165 } catch (Exception e) { |
170 assertEquals("Cannot shutdown with active requests.", | 166 assertEquals("Cannot shutdown with active requests.", |
171 e.getMessage()); | 167 e.getMessage()); |
172 } | 168 } |
173 | 169 |
174 listener.waitForNextStep(); | 170 listener.waitForNextStep(); |
175 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); | 171 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); |
176 try { | 172 try { |
177 mActivity.mUrlRequestContext.shutdown(); | 173 mActivity.mCronetEngine.shutdown(); |
178 fail("Should throw an exception"); | 174 fail("Should throw an exception"); |
179 } catch (Exception e) { | 175 } catch (Exception e) { |
180 assertEquals("Cannot shutdown with active requests.", | 176 assertEquals("Cannot shutdown with active requests.", |
181 e.getMessage()); | 177 e.getMessage()); |
182 } | 178 } |
183 listener.startNextRead(urlRequest); | 179 listener.startNextRead(urlRequest); |
184 | 180 |
185 listener.waitForNextStep(); | 181 listener.waitForNextStep(); |
186 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep); | 182 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep); |
187 try { | 183 try { |
188 mActivity.mUrlRequestContext.shutdown(); | 184 mActivity.mCronetEngine.shutdown(); |
189 fail("Should throw an exception"); | 185 fail("Should throw an exception"); |
190 } catch (Exception e) { | 186 } catch (Exception e) { |
191 assertEquals("Cannot shutdown with active requests.", | 187 assertEquals("Cannot shutdown with active requests.", |
192 e.getMessage()); | 188 e.getMessage()); |
193 } | 189 } |
194 | 190 |
195 // May not have read all the data, in theory. Just enable auto-advance | 191 // May not have read all the data, in theory. Just enable auto-advance |
196 // and finish the request. | 192 // and finish the request. |
197 listener.setAutoAdvance(true); | 193 listener.setAutoAdvance(true); |
198 listener.startNextRead(urlRequest); | 194 listener.startNextRead(urlRequest); |
(...skipping 17 matching lines...) Expand all Loading... | |
216 fail("Caught " + e.getMessage()); | 212 fail("Caught " + e.getMessage()); |
217 } | 213 } |
218 } | 214 } |
219 }; | 215 }; |
220 // Ensure that test is not running on the main thread. | 216 // Ensure that test is not running on the main thread. |
221 assertTrue(Looper.getMainLooper() != Looper.myLooper()); | 217 assertTrue(Looper.getMainLooper() != Looper.myLooper()); |
222 new Handler(Looper.getMainLooper()).post(blockingTask); | 218 new Handler(Looper.getMainLooper()).post(blockingTask); |
223 | 219 |
224 // Create new request context, but its initialization on the main thread | 220 // Create new request context, but its initialization on the main thread |
225 // will be stuck behind blockingTask. | 221 // will be stuck behind blockingTask. |
226 final UrlRequestContext requestContext = activity.initRequestContext(); | 222 final CronetEngine cronetEngine = activity.initCronetEngine(); |
227 // Unblock the main thread, so context gets initialized and shutdown on | 223 // Unblock the main thread, so context gets initialized and shutdown on |
228 // it. | 224 // it. |
229 block.open(); | 225 block.open(); |
230 // Shutdown will wait for init to complete on main thread. | 226 // Shutdown will wait for init to complete on main thread. |
231 requestContext.shutdown(); | 227 cronetEngine.shutdown(); |
232 // Verify that context is shutdown. | 228 // Verify that context is shutdown. |
233 try { | 229 try { |
234 requestContext.stopNetLog(); | 230 cronetEngine.stopNetLog(); |
235 fail("Should throw an exception."); | 231 fail("Should throw an exception."); |
236 } catch (Exception e) { | 232 } catch (Exception e) { |
237 assertEquals("Context is shut down.", e.getMessage()); | 233 assertEquals("Context is shut down.", e.getMessage()); |
238 } | 234 } |
239 } | 235 } |
240 | 236 |
241 @SmallTest | 237 @SmallTest |
242 @Feature({"Cronet"}) | 238 @Feature({"Cronet"}) |
243 public void testInitAndShutdownOnMainThread() throws Exception { | 239 public void testInitAndShutdownOnMainThread() throws Exception { |
244 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t(); | 240 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni t(); |
245 final ConditionVariable block = new ConditionVariable(false); | 241 final ConditionVariable block = new ConditionVariable(false); |
246 | 242 |
247 // Post a task to main thread to init and shutdown on the main thread. | 243 // Post a task to main thread to init and shutdown on the main thread. |
248 Runnable blockingTask = new Runnable() { | 244 Runnable blockingTask = new Runnable() { |
249 public void run() { | 245 public void run() { |
250 // Create new request context, loading the library. | 246 // Create new request context, loading the library. |
251 final UrlRequestContext requestContext = activity.initRequestCon text(); | 247 final CronetEngine cronetEngine = activity.initCronetEngine(); |
252 // Shutdown right after init. | 248 // Shutdown right after init. |
253 requestContext.shutdown(); | 249 cronetEngine.shutdown(); |
254 // Verify that context is shutdown. | 250 // Verify that context is shutdown. |
255 try { | 251 try { |
256 requestContext.stopNetLog(); | 252 cronetEngine.stopNetLog(); |
257 fail("Should throw an exception."); | 253 fail("Should throw an exception."); |
258 } catch (Exception e) { | 254 } catch (Exception e) { |
259 assertEquals("Context is shut down.", e.getMessage()); | 255 assertEquals("Context is shut down.", e.getMessage()); |
260 } | 256 } |
261 block.open(); | 257 block.open(); |
262 } | 258 } |
263 }; | 259 }; |
264 new Handler(Looper.getMainLooper()).post(blockingTask); | 260 new Handler(Looper.getMainLooper()).post(blockingTask); |
265 // Wait for shutdown to complete on main thread. | 261 // Wait for shutdown to complete on main thread. |
266 block.block(); | 262 block.block(); |
267 } | 263 } |
268 | 264 |
269 @SmallTest | 265 @SmallTest |
270 @Feature({"Cronet"}) | 266 @Feature({"Cronet"}) |
271 public void testMultipleShutdown() throws Exception { | 267 public void testMultipleShutdown() throws Exception { |
272 mActivity = launchCronetTestApp(); | 268 mActivity = launchCronetTestApp(); |
273 try { | 269 try { |
274 mActivity.mUrlRequestContext.shutdown(); | 270 mActivity.mCronetEngine.shutdown(); |
275 mActivity.mUrlRequestContext.shutdown(); | 271 mActivity.mCronetEngine.shutdown(); |
276 fail("Should throw an exception"); | 272 fail("Should throw an exception"); |
277 } catch (Exception e) { | 273 } catch (Exception e) { |
278 assertEquals("Context is shut down.", | 274 assertEquals("Context is shut down.", |
279 e.getMessage()); | 275 e.getMessage()); |
280 } | 276 } |
281 } | 277 } |
282 | 278 |
283 @SmallTest | 279 @SmallTest |
284 @Feature({"Cronet"}) | 280 @Feature({"Cronet"}) |
285 public void testShutdownAfterError() throws Exception { | 281 public void testShutdownAfterError() throws Exception { |
286 mActivity = launchCronetTestApp(); | 282 mActivity = launchCronetTestApp(); |
287 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); | 283 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |
288 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 284 UrlRequest.Builder builder = new UrlRequest.Builder( |
289 MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor()); | 285 MOCK_CRONET_TEST_FAILED_URL, listener, listener.getExecutor()); |
290 urlRequest.start(); | 286 mActivity.mCronetEngine.executeRequest(builder); |
291 listener.blockForDone(); | 287 listener.blockForDone(); |
292 assertTrue(listener.mOnErrorCalled); | 288 assertTrue(listener.mOnErrorCalled); |
293 } | 289 } |
294 | 290 |
295 @SmallTest | 291 @SmallTest |
296 @Feature({"Cronet"}) | 292 @Feature({"Cronet"}) |
297 public void testShutdownAfterCancel() throws Exception { | 293 public void testShutdownAfterCancel() throws Exception { |
298 mActivity = launchCronetTestApp(); | 294 mActivity = launchCronetTestApp(); |
299 TestUrlRequestListener listener = new TestUrlRequestListener(); | 295 TestUrlRequestListener listener = new TestUrlRequestListener(); |
300 // Block listener when response starts to verify that shutdown fails | 296 // Block listener when response starts to verify that shutdown fails |
301 // if there are active requests. | 297 // if there are active requests. |
302 listener.setAutoAdvance(false); | 298 listener.setAutoAdvance(false); |
303 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 299 UrlRequest.Builder builder = |
304 TEST_URL, listener, listener.getExecutor()); | 300 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
305 urlRequest.start(); | 301 UrlRequest urlRequest = mActivity.mCronetEngine.executeRequest(builder); |
306 try { | 302 try { |
307 mActivity.mUrlRequestContext.shutdown(); | 303 mActivity.mCronetEngine.shutdown(); |
308 fail("Should throw an exception"); | 304 fail("Should throw an exception"); |
309 } catch (Exception e) { | 305 } catch (Exception e) { |
310 assertEquals("Cannot shutdown with active requests.", | 306 assertEquals("Cannot shutdown with active requests.", |
311 e.getMessage()); | 307 e.getMessage()); |
312 } | 308 } |
313 listener.waitForNextStep(); | 309 listener.waitForNextStep(); |
314 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); | 310 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); |
315 urlRequest.cancel(); | 311 urlRequest.cancel(); |
316 mActivity.mUrlRequestContext.shutdown(); | 312 mActivity.mCronetEngine.shutdown(); |
317 } | 313 } |
318 | 314 |
319 @SmallTest | 315 @SmallTest |
320 @Feature({"Cronet"}) | 316 @Feature({"Cronet"}) |
321 public void testNetLog() throws Exception { | 317 public void testNetLog() throws Exception { |
322 Context context = getInstrumentation().getTargetContext(); | 318 Context context = getInstrumentation().getTargetContext(); |
323 File directory = new File(PathUtils.getDataDirectory(context)); | 319 File directory = new File(PathUtils.getDataDirectory(context)); |
324 File file = File.createTempFile("cronet", "json", directory); | 320 File file = File.createTempFile("cronet", "json", directory); |
325 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( | 321 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( |
326 context, | 322 context, new CronetEngine.Builder().setLibraryName("cronet_tests ")); |
327 new UrlRequestContextConfig().setLibraryName("cronet_tests")); | |
328 // Start NetLog immediately after the request context is created to make | 323 // 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 | 324 // sure that the call won't crash the app even when the native request |
330 // context is not fully initialized. See crbug.com/470196. | 325 // context is not fully initialized. See crbug.com/470196. |
331 requestContext.startNetLogToFile(file.getPath(), false); | 326 requestContext.startNetLogToFile(file.getPath(), false); |
332 | 327 |
333 // Start a request. | 328 // Start a request. |
334 TestUrlRequestListener listener = new TestUrlRequestListener(); | 329 TestUrlRequestListener listener = new TestUrlRequestListener(); |
335 UrlRequest request = requestContext.createRequest( | 330 UrlRequest.Builder builder = |
336 TEST_URL, listener, listener.getExecutor()); | 331 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
337 request.start(); | 332 requestContext.executeRequest(builder); |
338 listener.blockForDone(); | 333 listener.blockForDone(); |
339 requestContext.stopNetLog(); | 334 requestContext.stopNetLog(); |
340 assertTrue(file.exists()); | 335 assertTrue(file.exists()); |
341 assertTrue(file.length() != 0); | 336 assertTrue(file.length() != 0); |
342 assertFalse(hasBytesInNetLog(file)); | 337 assertFalse(hasBytesInNetLog(file)); |
343 assertTrue(file.delete()); | 338 assertTrue(file.delete()); |
344 assertTrue(!file.exists()); | 339 assertTrue(!file.exists()); |
345 } | 340 } |
346 | 341 |
347 @SmallTest | 342 @SmallTest |
348 @Feature({"Cronet"}) | 343 @Feature({"Cronet"}) |
349 public void testNetLogAfterShutdown() throws Exception { | 344 public void testNetLogAfterShutdown() throws Exception { |
350 mActivity = launchCronetTestApp(); | 345 mActivity = launchCronetTestApp(); |
351 TestUrlRequestListener listener = new TestUrlRequestListener(); | 346 TestUrlRequestListener listener = new TestUrlRequestListener(); |
352 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 347 UrlRequest.Builder builder = |
353 TEST_URL, listener, listener.getExecutor()); | 348 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
354 urlRequest.start(); | 349 mActivity.mCronetEngine.executeRequest(builder); |
355 listener.blockForDone(); | 350 listener.blockForDone(); |
356 mActivity.mUrlRequestContext.shutdown(); | 351 mActivity.mCronetEngine.shutdown(); |
357 | 352 |
358 File directory = new File(PathUtils.getDataDirectory( | 353 File directory = new File(PathUtils.getDataDirectory( |
359 getInstrumentation().getTargetContext())); | 354 getInstrumentation().getTargetContext())); |
360 File file = File.createTempFile("cronet", "json", directory); | 355 File file = File.createTempFile("cronet", "json", directory); |
361 try { | 356 try { |
362 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), | 357 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
363 false); | |
364 fail("Should throw an exception."); | 358 fail("Should throw an exception."); |
365 } catch (Exception e) { | 359 } catch (Exception e) { |
366 assertEquals("Context is shut down.", e.getMessage()); | 360 assertEquals("Context is shut down.", e.getMessage()); |
367 } | 361 } |
368 assertFalse(hasBytesInNetLog(file)); | 362 assertFalse(hasBytesInNetLog(file)); |
369 assertTrue(file.delete()); | 363 assertTrue(file.delete()); |
370 assertTrue(!file.exists()); | 364 assertTrue(!file.exists()); |
371 } | 365 } |
372 | 366 |
373 @SmallTest | 367 @SmallTest |
374 @Feature({"Cronet"}) | 368 @Feature({"Cronet"}) |
375 public void testNetLogStartMultipleTimes() throws Exception { | 369 public void testNetLogStartMultipleTimes() throws Exception { |
376 mActivity = launchCronetTestApp(); | 370 mActivity = launchCronetTestApp(); |
377 File directory = new File(PathUtils.getDataDirectory( | 371 File directory = new File(PathUtils.getDataDirectory( |
378 getInstrumentation().getTargetContext())); | 372 getInstrumentation().getTargetContext())); |
379 File file = File.createTempFile("cronet", "json", directory); | 373 File file = File.createTempFile("cronet", "json", directory); |
380 // Start NetLog multiple times. | 374 // Start NetLog multiple times. |
381 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); | 375 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
382 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); | 376 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
383 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); | 377 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
384 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); | 378 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
385 // Start a request. | 379 // Start a request. |
386 TestUrlRequestListener listener = new TestUrlRequestListener(); | 380 TestUrlRequestListener listener = new TestUrlRequestListener(); |
387 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 381 UrlRequest.Builder builder = |
388 TEST_URL, listener, listener.getExecutor()); | 382 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
389 urlRequest.start(); | 383 mActivity.mCronetEngine.executeRequest(builder); |
390 listener.blockForDone(); | 384 listener.blockForDone(); |
391 mActivity.mUrlRequestContext.stopNetLog(); | 385 mActivity.mCronetEngine.stopNetLog(); |
392 assertTrue(file.exists()); | 386 assertTrue(file.exists()); |
393 assertTrue(file.length() != 0); | 387 assertTrue(file.length() != 0); |
394 assertFalse(hasBytesInNetLog(file)); | 388 assertFalse(hasBytesInNetLog(file)); |
395 assertTrue(file.delete()); | 389 assertTrue(file.delete()); |
396 assertTrue(!file.exists()); | 390 assertTrue(!file.exists()); |
397 } | 391 } |
398 | 392 |
399 @SmallTest | 393 @SmallTest |
400 @Feature({"Cronet"}) | 394 @Feature({"Cronet"}) |
401 public void testNetLogStopMultipleTimes() throws Exception { | 395 public void testNetLogStopMultipleTimes() throws Exception { |
402 mActivity = launchCronetTestApp(); | 396 mActivity = launchCronetTestApp(); |
403 File directory = new File(PathUtils.getDataDirectory( | 397 File directory = new File(PathUtils.getDataDirectory( |
404 getInstrumentation().getTargetContext())); | 398 getInstrumentation().getTargetContext())); |
405 File file = File.createTempFile("cronet", "json", directory); | 399 File file = File.createTempFile("cronet", "json", directory); |
406 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); | 400 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); |
407 // Start a request. | 401 // Start a request. |
408 TestUrlRequestListener listener = new TestUrlRequestListener(); | 402 TestUrlRequestListener listener = new TestUrlRequestListener(); |
409 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 403 UrlRequest.Builder builder = |
410 TEST_URL, listener, listener.getExecutor()); | 404 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
411 urlRequest.start(); | 405 mActivity.mCronetEngine.executeRequest(builder); |
412 listener.blockForDone(); | 406 listener.blockForDone(); |
413 // Stop NetLog multiple times. | 407 // Stop NetLog multiple times. |
414 mActivity.mUrlRequestContext.stopNetLog(); | 408 mActivity.mCronetEngine.stopNetLog(); |
415 mActivity.mUrlRequestContext.stopNetLog(); | 409 mActivity.mCronetEngine.stopNetLog(); |
416 mActivity.mUrlRequestContext.stopNetLog(); | 410 mActivity.mCronetEngine.stopNetLog(); |
417 mActivity.mUrlRequestContext.stopNetLog(); | 411 mActivity.mCronetEngine.stopNetLog(); |
418 mActivity.mUrlRequestContext.stopNetLog(); | 412 mActivity.mCronetEngine.stopNetLog(); |
419 assertTrue(file.exists()); | 413 assertTrue(file.exists()); |
420 assertTrue(file.length() != 0); | 414 assertTrue(file.length() != 0); |
421 assertFalse(hasBytesInNetLog(file)); | 415 assertFalse(hasBytesInNetLog(file)); |
422 assertTrue(file.delete()); | 416 assertTrue(file.delete()); |
423 assertTrue(!file.exists()); | 417 assertTrue(!file.exists()); |
424 } | 418 } |
425 | 419 |
426 @SmallTest | 420 @SmallTest |
427 @Feature({"Cronet"}) | 421 @Feature({"Cronet"}) |
428 public void testNetLogWithBytes() throws Exception { | 422 public void testNetLogWithBytes() throws Exception { |
429 Context context = getInstrumentation().getTargetContext(); | 423 Context context = getInstrumentation().getTargetContext(); |
430 File directory = new File(PathUtils.getDataDirectory(context)); | 424 File directory = new File(PathUtils.getDataDirectory(context)); |
431 File file = File.createTempFile("cronet", "json", directory); | 425 File file = File.createTempFile("cronet", "json", directory); |
432 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( | 426 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( |
433 context, | 427 context, new CronetEngine.Builder().setLibraryName("cronet_tests ")); |
434 new UrlRequestContextConfig().setLibraryName("cronet_tests")); | |
435 // Start NetLog with logAll as true. | 428 // Start NetLog with logAll as true. |
436 requestContext.startNetLogToFile(file.getPath(), true); | 429 requestContext.startNetLogToFile(file.getPath(), true); |
437 // Start a request. | 430 // Start a request. |
438 TestUrlRequestListener listener = new TestUrlRequestListener(); | 431 TestUrlRequestListener listener = new TestUrlRequestListener(); |
439 UrlRequest request = requestContext.createRequest( | 432 UrlRequest.Builder builder = |
440 TEST_URL, listener, listener.getExecutor()); | 433 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor( )); |
441 request.start(); | 434 requestContext.executeRequest(builder); |
442 listener.blockForDone(); | 435 listener.blockForDone(); |
443 requestContext.stopNetLog(); | 436 requestContext.stopNetLog(); |
444 assertTrue(file.exists()); | 437 assertTrue(file.exists()); |
445 assertTrue(file.length() != 0); | 438 assertTrue(file.length() != 0); |
446 assertTrue(hasBytesInNetLog(file)); | 439 assertTrue(hasBytesInNetLog(file)); |
447 assertTrue(file.delete()); | 440 assertTrue(file.delete()); |
448 assertTrue(!file.exists()); | 441 assertTrue(!file.exists()); |
449 } | 442 } |
450 | 443 |
451 private boolean hasBytesInNetLog(File logFile) throws Exception { | 444 private boolean hasBytesInNetLog(File logFile) throws Exception { |
452 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); | 445 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); |
453 try { | 446 try { |
454 String logLine; | 447 String logLine; |
455 while ((logLine = logReader.readLine()) != null) { | 448 while ((logLine = logReader.readLine()) != null) { |
456 if (logLine.contains("\"hex_encoded_bytes\"")) { | 449 if (logLine.contains("\"hex_encoded_bytes\"")) { |
457 return true; | 450 return true; |
458 } | 451 } |
459 } | 452 } |
460 return false; | 453 return false; |
461 } finally { | 454 } finally { |
462 logReader.close(); | 455 logReader.close(); |
463 } | 456 } |
464 } | 457 } |
465 | 458 |
466 private void enableCache(int cacheType) throws Exception { | 459 private void enableCache(int cacheType) throws Exception { |
467 String cacheTypeString = ""; | 460 String cacheTypeString = ""; |
468 if (cacheType == UrlRequestContextConfig.HTTP_CACHE_DISK) { | 461 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { |
469 cacheTypeString = CronetTestActivity.CACHE_DISK; | 462 cacheTypeString = CronetTestActivity.CACHE_DISK; |
470 } else if (cacheType == UrlRequestContextConfig.HTTP_CACHE_DISK_NO_HTTP) { | 463 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { |
471 cacheTypeString = CronetTestActivity.CACHE_DISK_NO_HTTP; | 464 cacheTypeString = CronetTestActivity.CACHE_DISK_NO_HTTP; |
472 } else if (cacheType == UrlRequestContextConfig.HTTP_CACHE_IN_MEMORY) { | 465 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { |
473 cacheTypeString = CronetTestActivity.CACHE_IN_MEMORY; | 466 cacheTypeString = CronetTestActivity.CACHE_IN_MEMORY; |
474 } | 467 } |
475 String[] commandLineArgs = {CronetTestActivity.CACHE_KEY, cacheTypeStrin g}; | 468 String[] commandLineArgs = {CronetTestActivity.CACHE_KEY, cacheTypeStrin g}; |
476 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, | 469 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, |
477 commandLineArgs); | 470 commandLineArgs); |
478 assertTrue(NativeTestServer.startNativeTestServer( | 471 assertTrue(NativeTestServer.startNativeTestServer( |
479 getInstrumentation().getTargetContext())); | 472 getInstrumentation().getTargetContext())); |
480 } | 473 } |
481 | 474 |
482 private void checkRequestCaching(String url, boolean expectCached) { | 475 private void checkRequestCaching(String url, boolean expectCached) { |
483 checkRequestCaching(url, expectCached, false); | 476 checkRequestCaching(url, expectCached, false); |
484 } | 477 } |
485 | 478 |
486 private void checkRequestCaching(String url, boolean expectCached, | 479 private void checkRequestCaching(String url, boolean expectCached, |
487 boolean disableCache) { | 480 boolean disableCache) { |
488 TestUrlRequestListener listener = new TestUrlRequestListener(); | 481 TestUrlRequestListener listener = new TestUrlRequestListener(); |
489 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 482 UrlRequest.Builder builder = new UrlRequest.Builder(url, listener, liste ner.getExecutor()); |
490 url, listener, listener.getExecutor()); | |
491 if (disableCache) { | 483 if (disableCache) { |
492 urlRequest.disableCache(); | 484 builder.disableCache(); |
493 } | 485 } |
494 urlRequest.start(); | 486 mActivity.mCronetEngine.executeRequest(builder); |
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(url, listener, liste ner.getExecutor()); |
562 url, listener, listener.getExecutor()); | 554 builder.disableCache(); |
563 urlRequest.disableCache(); | 555 mActivity.mCronetEngine.executeRequest(builder); |
564 urlRequest.start(); | |
565 listener.blockForDone(); | 556 listener.blockForDone(); |
566 assertNotNull(listener.mError); | 557 assertNotNull(listener.mError); |
567 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ", | 558 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED ", |
568 listener.mError.getMessage()); | 559 listener.mError.getMessage()); |
569 } | 560 } |
570 | 561 |
571 @SmallTest | 562 @SmallTest |
572 @Feature({"Cronet"}) | 563 @Feature({"Cronet"}) |
573 public void testEnableHttpCacheDiskNewContext() throws Exception { | 564 public void testEnableHttpCacheDiskNewContext() throws Exception { |
574 enableCache(UrlRequestContextConfig.HTTP_CACHE_DISK); | 565 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); |
575 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 566 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
576 checkRequestCaching(url, false); | 567 checkRequestCaching(url, false); |
577 checkRequestCaching(url, true); | 568 checkRequestCaching(url, true); |
578 NativeTestServer.shutdownNativeTestServer(); | 569 NativeTestServer.shutdownNativeTestServer(); |
579 checkRequestCaching(url, true); | 570 checkRequestCaching(url, true); |
580 | 571 |
581 // Shutdown original context and create another that uses the same cache . | 572 // Shutdown original context and create another that uses the same cache . |
582 mActivity.mUrlRequestContext.shutdown(); | 573 mActivity.mCronetEngine.shutdown(); |
583 mActivity.mUrlRequestContext = UrlRequestContext.createContext( | 574 mActivity.mCronetEngine = |
584 getInstrumentation().getTargetContext(), mActivity.getContextCon fig()); | 575 mActivity.getCronetEngineBuilder().build(getInstrumentation().ge tTargetContext()); |
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 testInitContextAndStartRequest() { |
mef
2015/09/25 21:32:19
testInitEngineAndStartRequest
pauljensen
2015/09/28 14:18:12
Done.
| |
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 context. |
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( )); |
598 urlRequest.start(); | 589 cronetEngine.executeRequest(builder); |
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 testInitContextStartTwoRequests() throws Exception { |
mef
2015/09/25 21:32:19
testInitEngineAndStartTwoRequests
Context => Engi
pauljensen
2015/09/28 14:18:12
Done.
| |
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()); |
616 urlRequest.start(); | 607 UrlRequest urlRequest = cronetEngine.executeRequest(builder); |
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 testInitTwoContextsSimultaneously() throws Exception { |
(...skipping 25 matching lines...) Expand all Loading... | |
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 testInitDifferentContexts() throws Exception { |
mef
2015/09/25 21:32:19
Should this be testInitDifferentEngines?
pauljensen
2015/09/28 14:18:12
Done.
| |
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 CronetUrlRequestContext firstContext = |
668 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi g()); | 659 new CronetUrlRequestContext(mActivity, mActivity.getCronetEngine Builder()); |
669 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( | 660 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( |
670 mActivity.getApplicationContext(), mActivity.getContextConfig()) ; | 661 mActivity.getApplicationContext(), mActivity.getCronetEngineBuil der()); |
671 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( | 662 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( |
672 new ContextWrapper(mActivity), mActivity.getContextConfig()); | 663 new ContextWrapper(mActivity), mActivity.getCronetEngineBuilder( )); |
673 firstContext.shutdown(); | 664 firstContext.shutdown(); |
674 secondContext.shutdown(); | 665 secondContext.shutdown(); |
675 thirdContext.shutdown(); | 666 thirdContext.shutdown(); |
676 } | 667 } |
677 } | 668 } |
OLD | NEW |