| 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; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 urlRequestBuilder.build().start(); | 139 urlRequestBuilder.build().start(); |
| 140 callback.blockForDone(); | 140 callback.blockForDone(); |
| 141 assertEquals(userAgentValue, callback.mResponseAsString); | 141 assertEquals(userAgentValue, callback.mResponseAsString); |
| 142 } | 142 } |
| 143 | 143 |
| 144 @SmallTest | 144 @SmallTest |
| 145 @Feature({"Cronet"}) | 145 @Feature({"Cronet"}) |
| 146 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed. | 146 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed. |
| 147 @SuppressWarnings("deprecation") | 147 @SuppressWarnings("deprecation") |
| 148 public void testDataReductionProxyEnabled() throws Exception { | 148 public void testDataReductionProxyEnabled() throws Exception { |
| 149 mTestFramework = startCronetTestFrameworkAndSkipFactoryInit(); | 149 mTestFramework = startCronetTestFrameworkAndSkipLibraryInit(); |
| 150 | 150 |
| 151 // Ensure native code is loaded before trying to start test server. | 151 // Ensure native code is loaded before trying to start test server. |
| 152 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu
ild().shutdown(); | 152 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu
ild().shutdown(); |
| 153 | 153 |
| 154 assertTrue(NativeTestServer.startNativeTestServer(getContext())); | 154 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 155 if (!NativeTestServer.isDataReductionProxySupported()) { | 155 if (!NativeTestServer.isDataReductionProxySupported()) { |
| 156 return; | 156 return; |
| 157 } | 157 } |
| 158 String serverHostPort = NativeTestServer.getHostPort(); | 158 String serverHostPort = NativeTestServer.getHostPort(); |
| 159 | 159 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // May not have read all the data, in theory. Just enable auto-advance | 304 // May not have read all the data, in theory. Just enable auto-advance |
| 305 // and finish the request. | 305 // and finish the request. |
| 306 callback.setAutoAdvance(true); | 306 callback.setAutoAdvance(true); |
| 307 callback.startNextRead(urlRequest); | 307 callback.startNextRead(urlRequest); |
| 308 callback.blockForDone(); | 308 callback.blockForDone(); |
| 309 } | 309 } |
| 310 | 310 |
| 311 @SmallTest | 311 @SmallTest |
| 312 @Feature({"Cronet"}) | 312 @Feature({"Cronet"}) |
| 313 public void testShutdownDuringInit() throws Exception { | 313 public void testShutdownDuringInit() throws Exception { |
| 314 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 314 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 315 final ConditionVariable block = new ConditionVariable(false); | 315 final ConditionVariable block = new ConditionVariable(false); |
| 316 | 316 |
| 317 // Post a task to main thread to block until shutdown is called to test | 317 // Post a task to main thread to block until shutdown is called to test |
| 318 // scenario when shutdown is called right after construction before | 318 // scenario when shutdown is called right after construction before |
| 319 // context is fully initialized on the main thread. | 319 // context is fully initialized on the main thread. |
| 320 Runnable blockingTask = new Runnable() { | 320 Runnable blockingTask = new Runnable() { |
| 321 public void run() { | 321 public void run() { |
| 322 try { | 322 try { |
| 323 block.block(); | 323 block.block(); |
| 324 } catch (Exception e) { | 324 } catch (Exception e) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 343 cronetEngine.stopNetLog(); | 343 cronetEngine.stopNetLog(); |
| 344 fail("Should throw an exception."); | 344 fail("Should throw an exception."); |
| 345 } catch (Exception e) { | 345 } catch (Exception e) { |
| 346 assertEquals("Engine is shut down.", e.getMessage()); | 346 assertEquals("Engine is shut down.", e.getMessage()); |
| 347 } | 347 } |
| 348 } | 348 } |
| 349 | 349 |
| 350 @SmallTest | 350 @SmallTest |
| 351 @Feature({"Cronet"}) | 351 @Feature({"Cronet"}) |
| 352 public void testInitAndShutdownOnMainThread() throws Exception { | 352 public void testInitAndShutdownOnMainThread() throws Exception { |
| 353 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 353 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 354 final ConditionVariable block = new ConditionVariable(false); | 354 final ConditionVariable block = new ConditionVariable(false); |
| 355 | 355 |
| 356 // Post a task to main thread to init and shutdown on the main thread. | 356 // Post a task to main thread to init and shutdown on the main thread. |
| 357 Runnable blockingTask = new Runnable() { | 357 Runnable blockingTask = new Runnable() { |
| 358 public void run() { | 358 public void run() { |
| 359 // Create new request context, loading the library. | 359 // Create new request context, loading the library. |
| 360 final CronetEngine cronetEngine = testFramework.initCronetEngine
(); | 360 final CronetEngine cronetEngine = testFramework.initCronetEngine
(); |
| 361 // Shutdown right after init. | 361 // Shutdown right after init. |
| 362 cronetEngine.shutdown(); | 362 cronetEngine.shutdown(); |
| 363 // Verify that context is shutdown. | 363 // Verify that context is shutdown. |
| (...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 681 | 681 |
| 682 // Shutdown original context and create another that uses the same cache
. | 682 // Shutdown original context and create another that uses the same cache
. |
| 683 mTestFramework.mCronetEngine.shutdown(); | 683 mTestFramework.mCronetEngine.shutdown(); |
| 684 mTestFramework.mCronetEngine = mTestFramework.getCronetEngineBuilder().b
uild(); | 684 mTestFramework.mCronetEngine = mTestFramework.getCronetEngineBuilder().b
uild(); |
| 685 checkRequestCaching(url, true); | 685 checkRequestCaching(url, true); |
| 686 } | 686 } |
| 687 | 687 |
| 688 @SmallTest | 688 @SmallTest |
| 689 @Feature({"Cronet"}) | 689 @Feature({"Cronet"}) |
| 690 public void testInitEngineAndStartRequest() { | 690 public void testInitEngineAndStartRequest() { |
| 691 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); | 691 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 692 | 692 |
| 693 // Immediately make a request after initializing the engine. | 693 // Immediately make a request after initializing the engine. |
| 694 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 694 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 695 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 695 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 696 UrlRequest.Builder urlRequestBuilder = | 696 UrlRequest.Builder urlRequestBuilder = |
| 697 new UrlRequest.Builder(TEST_URL, callback, callback.getExecutor(
), cronetEngine); | 697 new UrlRequest.Builder(TEST_URL, callback, callback.getExecutor(
), cronetEngine); |
| 698 urlRequestBuilder.build().start(); | 698 urlRequestBuilder.build().start(); |
| 699 callback.blockForDone(); | 699 callback.blockForDone(); |
| 700 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 700 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 701 } | 701 } |
| 702 | 702 |
| 703 @SmallTest | 703 @SmallTest |
| 704 @Feature({"Cronet"}) | 704 @Feature({"Cronet"}) |
| 705 public void testInitEngineStartTwoRequests() throws Exception { | 705 public void testInitEngineStartTwoRequests() throws Exception { |
| 706 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); | 706 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 707 | 707 |
| 708 // Make two requests after initializing the context. | 708 // Make two requests after initializing the context. |
| 709 CronetEngine cronetEngine = testFramework.initCronetEngine(); | 709 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 710 int[] statusCodes = {0, 0}; | 710 int[] statusCodes = {0, 0}; |
| 711 String[] urls = {TEST_URL, URL_404}; | 711 String[] urls = {TEST_URL, URL_404}; |
| 712 for (int i = 0; i < 2; i++) { | 712 for (int i = 0; i < 2; i++) { |
| 713 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 713 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 714 UrlRequest.Builder urlRequestBuilder = | 714 UrlRequest.Builder urlRequestBuilder = |
| 715 new UrlRequest.Builder(urls[i], callback, callback.getExecut
or(), cronetEngine); | 715 new UrlRequest.Builder(urls[i], callback, callback.getExecut
or(), cronetEngine); |
| 716 urlRequestBuilder.build().start(); | 716 urlRequestBuilder.build().start(); |
| 717 callback.blockForDone(); | 717 callback.blockForDone(); |
| 718 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); | 718 statusCodes[i] = callback.mResponseInfo.getHttpStatusCode(); |
| 719 } | 719 } |
| 720 assertEquals(200, statusCodes[0]); | 720 assertEquals(200, statusCodes[0]); |
| 721 assertEquals(404, statusCodes[1]); | 721 assertEquals(404, statusCodes[1]); |
| 722 } | 722 } |
| 723 | 723 |
| 724 @SmallTest | 724 @SmallTest |
| 725 @Feature({"Cronet"}) | 725 @Feature({"Cronet"}) |
| 726 public void testInitTwoEnginesSimultaneously() throws Exception { | 726 public void testInitTwoEnginesSimultaneously() throws Exception { |
| 727 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 727 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 728 | 728 |
| 729 // Threads will block on runBlocker to ensure simultaneous execution. | 729 // Threads will block on runBlocker to ensure simultaneous execution. |
| 730 ConditionVariable runBlocker = new ConditionVariable(false); | 730 ConditionVariable runBlocker = new ConditionVariable(false); |
| 731 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); | 731 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); |
| 732 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); | 732 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); |
| 733 | 733 |
| 734 thread1.start(); | 734 thread1.start(); |
| 735 thread2.start(); | 735 thread2.start(); |
| 736 runBlocker.open(); | 736 runBlocker.open(); |
| 737 thread1.join(); | 737 thread1.join(); |
| 738 thread2.join(); | 738 thread2.join(); |
| 739 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); | 739 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); |
| 740 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); | 740 assertEquals(404, thread2.mCallback.mResponseInfo.getHttpStatusCode()); |
| 741 } | 741 } |
| 742 | 742 |
| 743 @SmallTest | 743 @SmallTest |
| 744 @Feature({"Cronet"}) | 744 @Feature({"Cronet"}) |
| 745 public void testInitTwoEnginesInSequence() throws Exception { | 745 public void testInitTwoEnginesInSequence() throws Exception { |
| 746 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 746 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 747 | 747 |
| 748 ConditionVariable runBlocker = new ConditionVariable(true); | 748 ConditionVariable runBlocker = new ConditionVariable(true); |
| 749 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); | 749 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); |
| 750 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); | 750 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); |
| 751 | 751 |
| 752 thread1.start(); | 752 thread1.start(); |
| 753 thread1.join(); | 753 thread1.join(); |
| 754 thread2.start(); | 754 thread2.start(); |
| 755 thread2.join(); | 755 thread2.join(); |
| 756 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); | 756 assertEquals(200, thread1.mCallback.mResponseInfo.getHttpStatusCode()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 785 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 785 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 786 UrlRequest.Builder builder = new UrlRequest.Builder( | 786 UrlRequest.Builder builder = new UrlRequest.Builder( |
| 787 TEST_URL, callback, callback.getExecutor(), mTestFramework.mCron
etEngine); | 787 TEST_URL, callback, callback.getExecutor(), mTestFramework.mCron
etEngine); |
| 788 builder.build().start(); | 788 builder.build().start(); |
| 789 callback.blockForDone(); | 789 callback.blockForDone(); |
| 790 byte delta2[] = mTestFramework.mCronetEngine.getGlobalMetricsDeltas(); | 790 byte delta2[] = mTestFramework.mCronetEngine.getGlobalMetricsDeltas(); |
| 791 assertTrue(delta2.length != 0); | 791 assertTrue(delta2.length != 0); |
| 792 assertFalse(Arrays.equals(delta1, delta2)); | 792 assertFalse(Arrays.equals(delta1, delta2)); |
| 793 } | 793 } |
| 794 } | 794 } |
| OLD | NEW |