| 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.File; | 19 import java.io.File; |
| 20 import java.io.FileReader; |
| 19 | 21 |
| 20 /** | 22 /** |
| 21 * Test CronetUrlRequestContext. | 23 * Test CronetUrlRequestContext. |
| 22 */ | 24 */ |
| 23 public class CronetUrlRequestContextTest extends CronetTestBase { | 25 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 24 // URLs used for tests. | 26 // URLs used for tests. |
| 25 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"; |
| 26 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"; |
| 27 private static final String MOCK_CRONET_TEST_FAILED_URL = | 29 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 28 "http://mock.failed.request/-2"; | 30 "http://mock.failed.request/-2"; |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 public void testNetLog() throws Exception { | 272 public void testNetLog() throws Exception { |
| 271 Context context = getInstrumentation().getTargetContext(); | 273 Context context = getInstrumentation().getTargetContext(); |
| 272 File directory = new File(PathUtils.getDataDirectory(context)); | 274 File directory = new File(PathUtils.getDataDirectory(context)); |
| 273 File file = File.createTempFile("cronet", "json", directory); | 275 File file = File.createTempFile("cronet", "json", directory); |
| 274 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( | 276 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( |
| 275 context, | 277 context, |
| 276 new UrlRequestContextConfig().setLibraryName("cronet_tests")); | 278 new UrlRequestContextConfig().setLibraryName("cronet_tests")); |
| 277 // Start NetLog immediately after the request context is created to make | 279 // Start NetLog immediately after the request context is created to make |
| 278 // sure that the call won't crash the app even when the native request | 280 // sure that the call won't crash the app even when the native request |
| 279 // context is not fully initialized. See crbug.com/470196. | 281 // context is not fully initialized. See crbug.com/470196. |
| 280 requestContext.startNetLogToFile(file.getPath()); | 282 requestContext.startNetLogToFile(file.getPath(), false); |
| 281 | 283 |
| 282 // Start a request. | 284 // Start a request. |
| 283 TestUrlRequestListener listener = new TestUrlRequestListener(); | 285 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 284 UrlRequest request = requestContext.createRequest( | 286 UrlRequest request = requestContext.createRequest( |
| 285 TEST_URL, listener, listener.getExecutor()); | 287 TEST_URL, listener, listener.getExecutor()); |
| 286 request.start(); | 288 request.start(); |
| 287 listener.blockForDone(); | 289 listener.blockForDone(); |
| 288 requestContext.stopNetLog(); | 290 requestContext.stopNetLog(); |
| 289 assertTrue(file.exists()); | 291 assertTrue(file.exists()); |
| 290 assertTrue(file.length() != 0); | 292 assertTrue(file.length() != 0); |
| 293 assertFalse(hasBytesInNetLog(file)); |
| 291 assertTrue(file.delete()); | 294 assertTrue(file.delete()); |
| 292 assertTrue(!file.exists()); | 295 assertTrue(!file.exists()); |
| 293 } | 296 } |
| 294 | 297 |
| 295 @SmallTest | 298 @SmallTest |
| 296 @Feature({"Cronet"}) | 299 @Feature({"Cronet"}) |
| 297 public void testNetLogAfterShutdown() throws Exception { | 300 public void testNetLogAfterShutdown() throws Exception { |
| 298 mActivity = launchCronetTestApp(); | 301 mActivity = launchCronetTestApp(); |
| 299 TestUrlRequestListener listener = new TestUrlRequestListener(); | 302 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 300 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 303 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 301 TEST_URL, listener, listener.getExecutor()); | 304 TEST_URL, listener, listener.getExecutor()); |
| 302 urlRequest.start(); | 305 urlRequest.start(); |
| 303 listener.blockForDone(); | 306 listener.blockForDone(); |
| 304 mActivity.mUrlRequestContext.shutdown(); | 307 mActivity.mUrlRequestContext.shutdown(); |
| 305 | 308 |
| 306 File directory = new File(PathUtils.getDataDirectory( | 309 File directory = new File(PathUtils.getDataDirectory( |
| 307 getInstrumentation().getTargetContext())); | 310 getInstrumentation().getTargetContext())); |
| 308 File file = File.createTempFile("cronet", "json", directory); | 311 File file = File.createTempFile("cronet", "json", directory); |
| 309 try { | 312 try { |
| 310 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 313 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), |
| 314 false); |
| 311 fail("Should throw an exception."); | 315 fail("Should throw an exception."); |
| 312 } catch (Exception e) { | 316 } catch (Exception e) { |
| 313 assertEquals("Context is shut down.", e.getMessage()); | 317 assertEquals("Context is shut down.", e.getMessage()); |
| 314 } | 318 } |
| 319 assertFalse(hasBytesInNetLog(file)); |
| 315 assertTrue(file.delete()); | 320 assertTrue(file.delete()); |
| 316 assertTrue(!file.exists()); | 321 assertTrue(!file.exists()); |
| 317 } | 322 } |
| 318 | 323 |
| 319 @SmallTest | 324 @SmallTest |
| 320 @Feature({"Cronet"}) | 325 @Feature({"Cronet"}) |
| 321 public void testNetLogStartMultipleTimes() throws Exception { | 326 public void testNetLogStartMultipleTimes() throws Exception { |
| 322 mActivity = launchCronetTestApp(); | 327 mActivity = launchCronetTestApp(); |
| 323 File directory = new File(PathUtils.getDataDirectory( | 328 File directory = new File(PathUtils.getDataDirectory( |
| 324 getInstrumentation().getTargetContext())); | 329 getInstrumentation().getTargetContext())); |
| 325 File file = File.createTempFile("cronet", "json", directory); | 330 File file = File.createTempFile("cronet", "json", directory); |
| 326 // Start NetLog multiple times. | 331 // Start NetLog multiple times. |
| 327 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 332 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); |
| 328 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 333 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); |
| 329 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 334 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); |
| 330 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 335 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); |
| 331 // Start a request. | 336 // Start a request. |
| 332 TestUrlRequestListener listener = new TestUrlRequestListener(); | 337 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 333 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 338 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 334 TEST_URL, listener, listener.getExecutor()); | 339 TEST_URL, listener, listener.getExecutor()); |
| 335 urlRequest.start(); | 340 urlRequest.start(); |
| 336 listener.blockForDone(); | 341 listener.blockForDone(); |
| 337 mActivity.mUrlRequestContext.stopNetLog(); | 342 mActivity.mUrlRequestContext.stopNetLog(); |
| 338 assertTrue(file.exists()); | 343 assertTrue(file.exists()); |
| 339 assertTrue(file.length() != 0); | 344 assertTrue(file.length() != 0); |
| 345 assertFalse(hasBytesInNetLog(file)); |
| 340 assertTrue(file.delete()); | 346 assertTrue(file.delete()); |
| 341 assertTrue(!file.exists()); | 347 assertTrue(!file.exists()); |
| 342 } | 348 } |
| 343 | 349 |
| 344 @SmallTest | 350 @SmallTest |
| 345 @Feature({"Cronet"}) | 351 @Feature({"Cronet"}) |
| 346 public void testNetLogStopMultipleTimes() throws Exception { | 352 public void testNetLogStopMultipleTimes() throws Exception { |
| 347 mActivity = launchCronetTestApp(); | 353 mActivity = launchCronetTestApp(); |
| 348 File directory = new File(PathUtils.getDataDirectory( | 354 File directory = new File(PathUtils.getDataDirectory( |
| 349 getInstrumentation().getTargetContext())); | 355 getInstrumentation().getTargetContext())); |
| 350 File file = File.createTempFile("cronet", "json", directory); | 356 File file = File.createTempFile("cronet", "json", directory); |
| 351 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath()); | 357 mActivity.mUrlRequestContext.startNetLogToFile(file.getPath(), false); |
| 352 // Start a request. | 358 // Start a request. |
| 353 TestUrlRequestListener listener = new TestUrlRequestListener(); | 359 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 354 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 360 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 355 TEST_URL, listener, listener.getExecutor()); | 361 TEST_URL, listener, listener.getExecutor()); |
| 356 urlRequest.start(); | 362 urlRequest.start(); |
| 357 listener.blockForDone(); | 363 listener.blockForDone(); |
| 358 // Stop NetLog multiple times. | 364 // Stop NetLog multiple times. |
| 359 mActivity.mUrlRequestContext.stopNetLog(); | 365 mActivity.mUrlRequestContext.stopNetLog(); |
| 360 mActivity.mUrlRequestContext.stopNetLog(); | 366 mActivity.mUrlRequestContext.stopNetLog(); |
| 361 mActivity.mUrlRequestContext.stopNetLog(); | 367 mActivity.mUrlRequestContext.stopNetLog(); |
| 362 mActivity.mUrlRequestContext.stopNetLog(); | 368 mActivity.mUrlRequestContext.stopNetLog(); |
| 363 mActivity.mUrlRequestContext.stopNetLog(); | 369 mActivity.mUrlRequestContext.stopNetLog(); |
| 364 assertTrue(file.exists()); | 370 assertTrue(file.exists()); |
| 365 assertTrue(file.length() != 0); | 371 assertTrue(file.length() != 0); |
| 372 assertFalse(hasBytesInNetLog(file)); |
| 366 assertTrue(file.delete()); | 373 assertTrue(file.delete()); |
| 367 assertTrue(!file.exists()); | 374 assertTrue(!file.exists()); |
| 368 } | 375 } |
| 369 | 376 |
| 377 @SmallTest |
| 378 @Feature({"Cronet"}) |
| 379 public void testNetLogWithBytes() throws Exception { |
| 380 Context context = getInstrumentation().getTargetContext(); |
| 381 File directory = new File(PathUtils.getDataDirectory(context)); |
| 382 File file = File.createTempFile("cronet", "json", directory); |
| 383 CronetUrlRequestContext requestContext = new CronetUrlRequestContext( |
| 384 context, |
| 385 new UrlRequestContextConfig().setLibraryName("cronet_tests")); |
| 386 // Start NetLog with logAll as true. |
| 387 requestContext.startNetLogToFile(file.getPath(), true); |
| 388 // Start a request. |
| 389 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 390 UrlRequest request = requestContext.createRequest( |
| 391 TEST_URL, listener, listener.getExecutor()); |
| 392 request.start(); |
| 393 listener.blockForDone(); |
| 394 requestContext.stopNetLog(); |
| 395 assertTrue(file.exists()); |
| 396 assertTrue(file.length() != 0); |
| 397 assertTrue(hasBytesInNetLog(file)); |
| 398 assertTrue(file.delete()); |
| 399 assertTrue(!file.exists()); |
| 400 } |
| 401 |
| 402 private boolean hasBytesInNetLog(File logFile) throws Exception { |
| 403 BufferedReader logReader = new BufferedReader(new FileReader(logFile)); |
| 404 try { |
| 405 String logLine; |
| 406 while ((logLine = logReader.readLine()) != null) { |
| 407 if (logLine.contains("\"hex_encoded_bytes\"")) { |
| 408 return true; |
| 409 } |
| 410 } |
| 411 return false; |
| 412 } finally { |
| 413 logReader.close(); |
| 414 } |
| 415 } |
| 416 |
| 370 private void enableCache(UrlRequestContextConfig.HttpCache cacheType) | 417 private void enableCache(UrlRequestContextConfig.HttpCache cacheType) |
| 371 throws Exception { | 418 throws Exception { |
| 372 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 419 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
| 373 config.setLibraryName("cronet_tests"); | 420 config.setLibraryName("cronet_tests"); |
| 374 if (cacheType == UrlRequestContextConfig.HttpCache.DISK | 421 if (cacheType == UrlRequestContextConfig.HttpCache.DISK |
| 375 || cacheType == UrlRequestContextConfig.HttpCache.DISK_NO_HTTP)
{ | 422 || cacheType == UrlRequestContextConfig.HttpCache.DISK_NO_HTTP)
{ |
| 376 config.setStoragePath(prepareTestStorage()); | 423 config.setStoragePath(prepareTestStorage()); |
| 377 } | 424 } |
| 378 | 425 |
| 379 config.enableHttpCache(cacheType, 1000 * 1024); | 426 config.enableHttpCache(cacheType, 1000 * 1024); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi
g()); | 634 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi
g()); |
| 588 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( | 635 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( |
| 589 mActivity.getApplicationContext(), mActivity.getContextConfig())
; | 636 mActivity.getApplicationContext(), mActivity.getContextConfig())
; |
| 590 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( | 637 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( |
| 591 new ContextWrapper(mActivity), mActivity.getContextConfig()); | 638 new ContextWrapper(mActivity), mActivity.getContextConfig()); |
| 592 firstContext.shutdown(); | 639 firstContext.shutdown(); |
| 593 secondContext.shutdown(); | 640 secondContext.shutdown(); |
| 594 thirdContext.shutdown(); | 641 thirdContext.shutdown(); |
| 595 } | 642 } |
| 596 } | 643 } |
| OLD | NEW |