Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.ContextWrapper; | 8 import android.content.ContextWrapper; |
| 9 import android.os.ConditionVariable; | 9 import android.os.ConditionVariable; |
| 10 import android.os.Handler; | 10 import android.os.Handler; |
| 11 import android.os.Looper; | 11 import android.os.Looper; |
| 12 import android.os.StrictMode; | 12 import android.os.StrictMode; |
| 13 import android.test.suitebuilder.annotation.SmallTest; | 13 import android.test.suitebuilder.annotation.SmallTest; |
| 14 | 14 |
| 15 import org.chromium.base.FileUtils; | 15 import org.chromium.base.FileUtils; |
| 16 import org.chromium.base.Log; | |
| 16 import org.chromium.base.PathUtils; | 17 import org.chromium.base.PathUtils; |
| 17 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
| 18 import org.chromium.base.test.util.Feature; | 19 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.net.TestUrlRequestCallback.ResponseStep; | 20 import org.chromium.net.TestUrlRequestCallback.ResponseStep; |
| 20 import org.chromium.net.impl.CronetLibraryLoader; | 21 import org.chromium.net.impl.CronetLibraryLoader; |
| 21 import org.chromium.net.impl.CronetUrlRequestContext; | 22 import org.chromium.net.impl.CronetUrlRequestContext; |
| 22 import org.chromium.net.test.EmbeddedTestServer; | 23 import org.chromium.net.test.EmbeddedTestServer; |
| 23 | 24 |
| 24 import java.io.BufferedReader; | 25 import java.io.BufferedReader; |
| 25 import java.io.File; | 26 import java.io.File; |
| 26 import java.io.FileReader; | 27 import java.io.FileReader; |
| 27 import java.util.Arrays; | 28 import java.util.Arrays; |
| 28 import java.util.LinkedList; | 29 import java.util.LinkedList; |
| 29 import java.util.NoSuchElementException; | 30 import java.util.NoSuchElementException; |
| 30 import java.util.concurrent.Executor; | 31 import java.util.concurrent.Executor; |
| 31 import java.util.concurrent.Executors; | 32 import java.util.concurrent.Executors; |
| 32 import java.util.concurrent.ThreadFactory; | 33 import java.util.concurrent.ThreadFactory; |
| 33 | 34 |
| 34 /** | 35 /** |
| 35 * Test CronetEngine. | 36 * Test CronetEngine. |
| 36 */ | 37 */ |
| 37 @JNINamespace("cronet") | 38 @JNINamespace("cronet") |
| 38 public class CronetUrlRequestContextTest extends CronetTestBase { | 39 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 39 // URLs used for tests. | 40 // URLs used for tests. |
| 40 private static final String MOCK_CRONET_TEST_FAILED_URL = | 41 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 41 "http://mock.failed.request/-2"; | 42 "http://mock.failed.request/-2"; |
| 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = | 43 private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| 43 "http://mock.http/success.txt"; | 44 "http://mock.http/success.txt"; |
| 45 private static final String TAG = "cr_RequestCtxTest"; | |
|
xunjieli
2016/08/16 16:28:46
cr prefix is automatically added now. Just fyi. No
| |
| 44 private static final int MAX_FILE_SIZE = 1000000000; | 46 private static final int MAX_FILE_SIZE = 1000000000; |
| 45 private static final int NUM_EVENT_FILES = 10; | 47 private static final int NUM_EVENT_FILES = 10; |
| 46 | 48 |
| 47 private EmbeddedTestServer mTestServer; | 49 private EmbeddedTestServer mTestServer; |
| 48 private String mUrl; | 50 private String mUrl; |
| 49 private String mUrl404; | 51 private String mUrl404; |
| 50 private String mUrl500; | 52 private String mUrl500; |
| 51 CronetTestFramework mTestFramework; | 53 CronetTestFramework mTestFramework; |
| 52 | 54 |
| 53 // Thread on which network quality listeners should be notified. | 55 // Thread on which network quality listeners should be notified. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 } | 96 } |
| 95 } | 97 } |
| 96 | 98 |
| 97 /** | 99 /** |
| 98 * Callback that shutdowns the request context when request has succeeded | 100 * Callback that shutdowns the request context when request has succeeded |
| 99 * or failed. | 101 * or failed. |
| 100 */ | 102 */ |
| 101 class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { | 103 class ShutdownTestUrlRequestCallback extends TestUrlRequestCallback { |
| 102 @Override | 104 @Override |
| 103 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 105 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 106 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed. | |
| 107 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() has started "); | |
| 104 super.onSucceeded(request, info); | 108 super.onSucceeded(request, info); |
| 109 Log.d(TAG, "ShutdownTestUrlRequestCallback.onSucceeded() before call ing shutdown"); | |
| 105 mTestFramework.mCronetEngine.shutdown(); | 110 mTestFramework.mCronetEngine.shutdown(); |
| 111 Log.d(TAG, "ShutdownTestUrlRequestCallback.hasFinished() has finishe d"); | |
| 106 } | 112 } |
| 107 | 113 |
| 108 @Override | 114 @Override |
| 109 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) { | 115 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques tException error) { |
| 116 // TODO: Remove logging when http://crbug.com/596929 & http://crbug. com/635025 is fixed. | |
| 117 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has started"); | |
| 110 super.onFailed(request, info, error); | 118 super.onFailed(request, info, error); |
| 119 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() before calling shutdown"); | |
| 111 mTestFramework.mCronetEngine.shutdown(); | 120 mTestFramework.mCronetEngine.shutdown(); |
| 121 Log.d(TAG, "ShutdownTestUrlRequestCallback.onFailed() has finished." ); | |
| 112 } | 122 } |
| 113 } | 123 } |
| 114 | 124 |
| 115 static class TestExecutor implements Executor { | 125 static class TestExecutor implements Executor { |
| 116 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); | 126 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable> (); |
| 117 | 127 |
| 118 @Override | 128 @Override |
| 119 public void execute(Runnable task) { | 129 public void execute(Runnable task) { |
| 120 mTaskQueue.add(task); | 130 mTaskQueue.add(task); |
| 121 } | 131 } |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 // Verify that effective connection type callback is received and | 341 // Verify that effective connection type callback is received and |
| 332 // effective connection type is correctly set. | 342 // effective connection type is correctly set. |
| 333 assertTrue(mTestFramework.mCronetEngine.getEffectiveConnectionType() | 343 assertTrue(mTestFramework.mCronetEngine.getEffectiveConnectionType() |
| 334 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN); | 344 != EffectiveConnectionType.EFFECTIVE_CONNECTION_TYPE_UNKNOWN); |
| 335 | 345 |
| 336 mTestFramework.mCronetEngine.shutdown(); | 346 mTestFramework.mCronetEngine.shutdown(); |
| 337 } | 347 } |
| 338 | 348 |
| 339 @SmallTest | 349 @SmallTest |
| 340 @Feature({"Cronet"}) | 350 @Feature({"Cronet"}) |
| 351 // TODO: Remove the annotation after fixing http://crbug.com/637979 & http:/ /crbug.com/637972 | |
| 352 @OnlyRunNativeCronet | |
| 341 public void testShutdown() throws Exception { | 353 public void testShutdown() throws Exception { |
| 354 Log.i(TAG, "testShutdown() has started"); | |
| 342 mTestFramework = startCronetTestFramework(); | 355 mTestFramework = startCronetTestFramework(); |
| 343 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); | 356 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); |
| 344 // Block callback when response starts to verify that shutdown fails | 357 // Block callback when response starts to verify that shutdown fails |
| 345 // if there are active requests. | 358 // if there are active requests. |
| 346 callback.setAutoAdvance(false); | 359 callback.setAutoAdvance(false); |
| 347 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 360 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 348 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); | 361 mUrl, callback, callback.getExecutor(), mTestFramework.mCronetEn gine); |
| 349 UrlRequest urlRequest = urlRequestBuilder.build(); | 362 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 350 urlRequest.start(); | 363 urlRequest.start(); |
| 351 try { | 364 try { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 375 } catch (Exception e) { | 388 } catch (Exception e) { |
| 376 assertEquals("Cannot shutdown with active requests.", | 389 assertEquals("Cannot shutdown with active requests.", |
| 377 e.getMessage()); | 390 e.getMessage()); |
| 378 } | 391 } |
| 379 | 392 |
| 380 // May not have read all the data, in theory. Just enable auto-advance | 393 // May not have read all the data, in theory. Just enable auto-advance |
| 381 // and finish the request. | 394 // and finish the request. |
| 382 callback.setAutoAdvance(true); | 395 callback.setAutoAdvance(true); |
| 383 callback.startNextRead(urlRequest); | 396 callback.startNextRead(urlRequest); |
| 384 callback.blockForDone(); | 397 callback.blockForDone(); |
| 398 // TODO: Remove sleep when http://crbug.com/596929 is fixed. | |
| 399 // The sleep gives the thread that shuts down the engine time to complet e. | |
| 400 // See http://crbug.com/596929 | |
| 401 Thread.sleep(3000); | |
| 402 Log.i(TAG, "testShutdown() has finished"); | |
| 385 } | 403 } |
| 386 | 404 |
| 387 @SmallTest | 405 @SmallTest |
| 388 @Feature({"Cronet"}) | 406 @Feature({"Cronet"}) |
| 389 @OnlyRunNativeCronet | 407 @OnlyRunNativeCronet |
| 390 public void testShutdownDuringInit() throws Exception { | 408 public void testShutdownDuringInit() throws Exception { |
| 391 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); | 409 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki pLibraryInit(); |
| 392 final ConditionVariable block = new ConditionVariable(false); | 410 final ConditionVariable block = new ConditionVariable(false); |
| 393 | 411 |
| 394 // Post a task to main thread to block until shutdown is called to test | 412 // Post a task to main thread to block until shutdown is called to test |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 mTestFramework.mCronetEngine.shutdown(); | 483 mTestFramework.mCronetEngine.shutdown(); |
| 466 mTestFramework.mCronetEngine.shutdown(); | 484 mTestFramework.mCronetEngine.shutdown(); |
| 467 fail("Should throw an exception"); | 485 fail("Should throw an exception"); |
| 468 } catch (Exception e) { | 486 } catch (Exception e) { |
| 469 assertEquals("Engine is shut down.", e.getMessage()); | 487 assertEquals("Engine is shut down.", e.getMessage()); |
| 470 } | 488 } |
| 471 } | 489 } |
| 472 | 490 |
| 473 @SmallTest | 491 @SmallTest |
| 474 @Feature({"Cronet"}) | 492 @Feature({"Cronet"}) |
| 493 // TODO: Remove the annotation after fixing http://crbug.com/637972 | |
| 494 @OnlyRunNativeCronet | |
| 475 public void testShutdownAfterError() throws Exception { | 495 public void testShutdownAfterError() throws Exception { |
| 496 Log.i(TAG, "testShutdownAfterError() has started"); | |
| 476 mTestFramework = startCronetTestFramework(); | 497 mTestFramework = startCronetTestFramework(); |
| 477 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); | 498 TestUrlRequestCallback callback = new ShutdownTestUrlRequestCallback(); |
| 478 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL, | 499 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE T_TEST_FAILED_URL, |
| 479 callback, callback.getExecutor(), mTestFramework.mCronetEngine); | 500 callback, callback.getExecutor(), mTestFramework.mCronetEngine); |
| 480 urlRequestBuilder.build().start(); | 501 urlRequestBuilder.build().start(); |
| 481 callback.blockForDone(); | 502 callback.blockForDone(); |
| 482 assertTrue(callback.mOnErrorCalled); | 503 assertTrue(callback.mOnErrorCalled); |
| 504 // TODO: Remove sleep when http://crbug.com/635025 is fixed. | |
| 505 // The sleep gives the thread that shuts down the engine time to complet e. | |
| 506 // See http://crbug.com/637986 | |
| 507 Thread.sleep(3000); | |
| 508 Log.i(TAG, "testShutdownAfterError() has finished"); | |
| 483 } | 509 } |
| 484 | 510 |
| 485 @SmallTest | 511 @SmallTest |
| 486 @Feature({"Cronet"}) | 512 @Feature({"Cronet"}) |
| 487 public void testShutdownAfterCancel() throws Exception { | 513 public void testShutdownAfterCancel() throws Exception { |
| 488 mTestFramework = startCronetTestFramework(); | 514 mTestFramework = startCronetTestFramework(); |
| 489 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 515 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 490 // Block callback when response starts to verify that shutdown fails | 516 // Block callback when response starts to verify that shutdown fails |
| 491 // if there are active requests. | 517 // if there are active requests. |
| 492 callback.setAutoAdvance(false); | 518 callback.setAutoAdvance(false); |
| (...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1350 } | 1376 } |
| 1351 }.start(); | 1377 }.start(); |
| 1352 otherThreadDone.block(); | 1378 otherThreadDone.block(); |
| 1353 builder.build().shutdown(); | 1379 builder.build().shutdown(); |
| 1354 uiThreadDone.open(); | 1380 uiThreadDone.open(); |
| 1355 } | 1381 } |
| 1356 }); | 1382 }); |
| 1357 assertTrue(uiThreadDone.block(1000)); | 1383 assertTrue(uiThreadDone.block(1000)); |
| 1358 } | 1384 } |
| 1359 } | 1385 } |
| OLD | NEW |