| 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.ContextWrapper; | 7 import android.content.ContextWrapper; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.test.util.Feature; | 10 import org.chromium.base.test.util.Feature; |
| 11 | 11 |
| 12 import java.util.HashMap; | 12 import java.util.HashMap; |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Tests that make sure ChromiumUrlRequestContext initialization will not | 15 * Tests that make sure ChromiumUrlRequestContext initialization will not |
| 16 * affect embedders' ability to make requests. | 16 * affect embedders' ability to make requests. |
| 17 */ | 17 */ |
| 18 @SuppressWarnings("deprecation") | 18 @SuppressWarnings("deprecation") |
| 19 public class ContextInitTest extends CronetTestBase { | 19 public class ContextInitTest extends CronetTestBase { |
| 20 // URL used for base tests. | 20 // URL used for base tests. |
| 21 private static final String URL = "http://127.0.0.1:8000"; | 21 private static final String URL = "http://127.0.0.1:8000"; |
| 22 // URL used for tests that return HTTP not found (404). | 22 // URL used for tests that return HTTP not found (404). |
| 23 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; | 23 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; |
| 24 | 24 |
| 25 @SmallTest | 25 @SmallTest |
| 26 @Feature({"Cronet"}) | 26 @Feature({"Cronet"}) |
| 27 public void testInitFactoryAndStartRequest() { | 27 public void testInitFactoryAndStartRequest() { |
| 28 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); | 28 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 29 | 29 |
| 30 // Immediately make a request after initializing the factory. | 30 // Immediately make a request after initializing the factory. |
| 31 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | 31 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 32 TestHttpUrlRequestListener listener = makeRequest(factory, URL); | 32 TestHttpUrlRequestListener listener = makeRequest(factory, URL); |
| 33 listener.blockForComplete(); | 33 listener.blockForComplete(); |
| 34 assertEquals(200, listener.mHttpStatusCode); | 34 assertEquals(200, listener.mHttpStatusCode); |
| 35 } | 35 } |
| 36 | 36 |
| 37 @SmallTest | 37 @SmallTest |
| 38 @Feature({"Cronet"}) | 38 @Feature({"Cronet"}) |
| 39 public void testInitFactoryStartRequestAndCancel() { | 39 public void testInitFactoryStartRequestAndCancel() { |
| 40 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); | 40 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 41 | 41 |
| 42 // Make a request and cancel it after initializing the factory. | 42 // Make a request and cancel it after initializing the factory. |
| 43 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | 43 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 44 HashMap<String, String> headers = new HashMap<String, String>(); | 44 HashMap<String, String> headers = new HashMap<String, String>(); |
| 45 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 45 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 46 HttpUrlRequest request = factory.createRequest( | 46 HttpUrlRequest request = factory.createRequest( |
| 47 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 47 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 48 request.start(); | 48 request.start(); |
| 49 request.cancel(); | 49 request.cancel(); |
| 50 listener.blockForComplete(); | 50 listener.blockForComplete(); |
| 51 assertEquals(0, listener.mHttpStatusCode); | 51 assertEquals(0, listener.mHttpStatusCode); |
| 52 } | 52 } |
| 53 | 53 |
| 54 @SmallTest | 54 @SmallTest |
| 55 @Feature({"Cronet"}) | 55 @Feature({"Cronet"}) |
| 56 public void testInitFactoryStartTwoRequests() throws Exception { | 56 public void testInitFactoryStartTwoRequests() throws Exception { |
| 57 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); | 57 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); |
| 58 | 58 |
| 59 // Make two request right after initializing the factory. | 59 // Make two request right after initializing the factory. |
| 60 int[] statusCodes = {0, 0}; | 60 int[] statusCodes = {0, 0}; |
| 61 String[] urls = {URL, URL_404}; | 61 String[] urls = {URL, URL_404}; |
| 62 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | 62 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 63 for (int i = 0; i < 2; i++) { | 63 for (int i = 0; i < 2; i++) { |
| 64 TestHttpUrlRequestListener listener = makeRequest(factory, urls[i]); | 64 TestHttpUrlRequestListener listener = makeRequest(factory, urls[i]); |
| 65 listener.blockForComplete(); | 65 listener.blockForComplete(); |
| 66 statusCodes[i] = listener.mHttpStatusCode; | 66 statusCodes[i] = listener.mHttpStatusCode; |
| 67 } | 67 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 public void run() { | 84 public void run() { |
| 85 HttpUrlRequestFactory factory = mTestFramework.initRequestFactory(); | 85 HttpUrlRequestFactory factory = mTestFramework.initRequestFactory(); |
| 86 mCallback = makeRequest(factory, mUrl); | 86 mCallback = makeRequest(factory, mUrl); |
| 87 mCallback.blockForComplete(); | 87 mCallback.blockForComplete(); |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| 91 @SmallTest | 91 @SmallTest |
| 92 @Feature({"Cronet"}) | 92 @Feature({"Cronet"}) |
| 93 public void testInitTwoFactoriesSimultaneously() throws Exception { | 93 public void testInitTwoFactoriesSimultaneously() throws Exception { |
| 94 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 94 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 95 | 95 |
| 96 RequestThread thread1 = new RequestThread(testFramework, URL); | 96 RequestThread thread1 = new RequestThread(testFramework, URL); |
| 97 RequestThread thread2 = new RequestThread(testFramework, URL_404); | 97 RequestThread thread2 = new RequestThread(testFramework, URL_404); |
| 98 | 98 |
| 99 thread1.start(); | 99 thread1.start(); |
| 100 thread2.start(); | 100 thread2.start(); |
| 101 thread1.join(); | 101 thread1.join(); |
| 102 thread2.join(); | 102 thread2.join(); |
| 103 assertEquals(200, thread1.mCallback.mHttpStatusCode); | 103 assertEquals(200, thread1.mCallback.mHttpStatusCode); |
| 104 assertEquals(404, thread2.mCallback.mHttpStatusCode); | 104 assertEquals(404, thread2.mCallback.mHttpStatusCode); |
| 105 } | 105 } |
| 106 | 106 |
| 107 @SmallTest | 107 @SmallTest |
| 108 @Feature({"Cronet"}) | 108 @Feature({"Cronet"}) |
| 109 public void testInitTwoFactoriesInSequence() throws Exception { | 109 public void testInitTwoFactoriesInSequence() throws Exception { |
| 110 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); | 110 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); |
| 111 | 111 |
| 112 RequestThread thread1 = new RequestThread(testFramework, URL); | 112 RequestThread thread1 = new RequestThread(testFramework, URL); |
| 113 RequestThread thread2 = new RequestThread(testFramework, URL_404); | 113 RequestThread thread2 = new RequestThread(testFramework, URL_404); |
| 114 | 114 |
| 115 thread1.start(); | 115 thread1.start(); |
| 116 thread1.join(); | 116 thread1.join(); |
| 117 thread2.start(); | 117 thread2.start(); |
| 118 thread2.join(); | 118 thread2.join(); |
| 119 assertEquals(200, thread1.mCallback.mHttpStatusCode); | 119 assertEquals(200, thread1.mCallback.mHttpStatusCode); |
| 120 assertEquals(404, thread2.mCallback.mHttpStatusCode); | 120 assertEquals(404, thread2.mCallback.mHttpStatusCode); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 144 getContext().getApplicationContext(), testFramework.getCronetEng
ineBuilder()); | 144 getContext().getApplicationContext(), testFramework.getCronetEng
ineBuilder()); |
| 145 HttpUrlRequestFactory thirdFactory = HttpUrlRequestFactory.createFactory
( | 145 HttpUrlRequestFactory thirdFactory = HttpUrlRequestFactory.createFactory
( |
| 146 new ContextWrapper(getContext()), testFramework.getCronetEngineB
uilder()); | 146 new ContextWrapper(getContext()), testFramework.getCronetEngineB
uilder()); |
| 147 // Meager attempt to extend lifetimes to ensure they're concurrently | 147 // Meager attempt to extend lifetimes to ensure they're concurrently |
| 148 // alive. | 148 // alive. |
| 149 firstFactory.getName(); | 149 firstFactory.getName(); |
| 150 secondFactory.getName(); | 150 secondFactory.getName(); |
| 151 thirdFactory.getName(); | 151 thirdFactory.getName(); |
| 152 } | 152 } |
| 153 } | 153 } |
| OLD | NEW |