| 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 public class ContextInitTest extends CronetTestBase { | 18 public class ContextInitTest extends CronetTestBase { |
| 19 // URL used for base tests. | 19 // URL used for base tests. |
| 20 private static final String URL = "http://127.0.0.1:8000"; | 20 private static final String URL = "http://127.0.0.1:8000"; |
| 21 // URL used for tests that return HTTP not found (404). | 21 // URL used for tests that return HTTP not found (404). |
| 22 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; | 22 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; |
| 23 | 23 |
| 24 @SmallTest | 24 @SmallTest |
| 25 @Feature({"Cronet"}) | 25 @Feature({"Cronet"}) |
| 26 public void testInitFactoryAndStartRequest() { | 26 public void testInitFactoryAndStartRequest() { |
| 27 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); | 27 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); |
| 28 | 28 |
| 29 // Immediately make a request after initializing the factory. | 29 // Immediately make a request after initializing the factory. |
| 30 HttpUrlRequestFactory factory = activity.initRequestFactory(); | 30 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 31 TestHttpUrlRequestListener listener = makeRequest(factory, URL); | 31 TestHttpUrlRequestListener listener = makeRequest(factory, URL); |
| 32 listener.blockForComplete(); | 32 listener.blockForComplete(); |
| 33 assertEquals(200, listener.mHttpStatusCode); | 33 assertEquals(200, listener.mHttpStatusCode); |
| 34 } | 34 } |
| 35 | 35 |
| 36 @SmallTest | 36 @SmallTest |
| 37 @Feature({"Cronet"}) | 37 @Feature({"Cronet"}) |
| 38 public void testInitFactoryStartRequestAndCancel() { | 38 public void testInitFactoryStartRequestAndCancel() { |
| 39 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); | 39 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); |
| 40 | 40 |
| 41 // Make a request and cancel it after initializing the factory. | 41 // Make a request and cancel it after initializing the factory. |
| 42 HttpUrlRequestFactory factory = activity.initRequestFactory(); | 42 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 43 HashMap<String, String> headers = new HashMap<String, String>(); | 43 HashMap<String, String> headers = new HashMap<String, String>(); |
| 44 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 44 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 45 HttpUrlRequest request = factory.createRequest( | 45 HttpUrlRequest request = factory.createRequest( |
| 46 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 46 URL, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 47 request.start(); | 47 request.start(); |
| 48 request.cancel(); | 48 request.cancel(); |
| 49 listener.blockForComplete(); | 49 listener.blockForComplete(); |
| 50 assertEquals(0, listener.mHttpStatusCode); | 50 assertEquals(0, listener.mHttpStatusCode); |
| 51 } | 51 } |
| 52 | 52 |
| 53 @SmallTest | 53 @SmallTest |
| 54 @Feature({"Cronet"}) | 54 @Feature({"Cronet"}) |
| 55 public void testInitFactoryStartTwoRequests() throws Exception { | 55 public void testInitFactoryStartTwoRequests() throws Exception { |
| 56 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); | 56 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); |
| 57 | 57 |
| 58 // Make two request right after initializing the factory. | 58 // Make two request right after initializing the factory. |
| 59 int[] statusCodes = {0, 0}; | 59 int[] statusCodes = {0, 0}; |
| 60 String[] urls = {URL, URL_404}; | 60 String[] urls = {URL, URL_404}; |
| 61 HttpUrlRequestFactory factory = activity.initRequestFactory(); | 61 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); |
| 62 for (int i = 0; i < 2; i++) { | 62 for (int i = 0; i < 2; i++) { |
| 63 TestHttpUrlRequestListener listener = makeRequest(factory, urls[i]); | 63 TestHttpUrlRequestListener listener = makeRequest(factory, urls[i]); |
| 64 listener.blockForComplete(); | 64 listener.blockForComplete(); |
| 65 statusCodes[i] = listener.mHttpStatusCode; | 65 statusCodes[i] = listener.mHttpStatusCode; |
| 66 } | 66 } |
| 67 assertEquals(200, statusCodes[0]); | 67 assertEquals(200, statusCodes[0]); |
| 68 assertEquals(404, statusCodes[1]); | 68 assertEquals(404, statusCodes[1]); |
| 69 } | 69 } |
| 70 | 70 |
| 71 class RequestThread extends Thread { | 71 class RequestThread extends Thread { |
| 72 public TestHttpUrlRequestListener mListener; | 72 public TestHttpUrlRequestListener mListener; |
| 73 | 73 |
| 74 final CronetTestActivity mActivity; | 74 final CronetTestFramework mTestFramework; |
| 75 final String mUrl; | 75 final String mUrl; |
| 76 | 76 |
| 77 public RequestThread(CronetTestActivity activity, String url) { | 77 public RequestThread(CronetTestFramework testFramework, String url) { |
| 78 mActivity = activity; | 78 mTestFramework = testFramework; |
| 79 mUrl = url; | 79 mUrl = url; |
| 80 } | 80 } |
| 81 | 81 |
| 82 @Override | 82 @Override |
| 83 public void run() { | 83 public void run() { |
| 84 HttpUrlRequestFactory factory = mActivity.initRequestFactory(); | 84 HttpUrlRequestFactory factory = mTestFramework.initRequestFactory(); |
| 85 mListener = makeRequest(factory, mUrl); | 85 mListener = makeRequest(factory, mUrl); |
| 86 mListener.blockForComplete(); | 86 mListener.blockForComplete(); |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 @SmallTest | 90 @SmallTest |
| 91 @Feature({"Cronet"}) | 91 @Feature({"Cronet"}) |
| 92 public void testInitTwoFactoriesSimultaneously() throws Exception { | 92 public void testInitTwoFactoriesSimultaneously() throws Exception { |
| 93 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 93 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 94 | 94 |
| 95 RequestThread thread1 = new RequestThread(activity, URL); | 95 RequestThread thread1 = new RequestThread(testFramework, URL); |
| 96 RequestThread thread2 = new RequestThread(activity, URL_404); | 96 RequestThread thread2 = new RequestThread(testFramework, URL_404); |
| 97 | 97 |
| 98 thread1.start(); | 98 thread1.start(); |
| 99 thread2.start(); | 99 thread2.start(); |
| 100 thread1.join(); | 100 thread1.join(); |
| 101 thread2.join(); | 101 thread2.join(); |
| 102 assertEquals(200, thread1.mListener.mHttpStatusCode); | 102 assertEquals(200, thread1.mListener.mHttpStatusCode); |
| 103 assertEquals(404, thread2.mListener.mHttpStatusCode); | 103 assertEquals(404, thread2.mListener.mHttpStatusCode); |
| 104 } | 104 } |
| 105 | 105 |
| 106 @SmallTest | 106 @SmallTest |
| 107 @Feature({"Cronet"}) | 107 @Feature({"Cronet"}) |
| 108 public void testInitTwoFactoriesInSequence() throws Exception { | 108 public void testInitTwoFactoriesInSequence() throws Exception { |
| 109 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 109 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 110 | 110 |
| 111 RequestThread thread1 = new RequestThread(activity, URL); | 111 RequestThread thread1 = new RequestThread(testFramework, URL); |
| 112 RequestThread thread2 = new RequestThread(activity, URL_404); | 112 RequestThread thread2 = new RequestThread(testFramework, URL_404); |
| 113 | 113 |
| 114 thread1.start(); | 114 thread1.start(); |
| 115 thread1.join(); | 115 thread1.join(); |
| 116 thread2.start(); | 116 thread2.start(); |
| 117 thread2.join(); | 117 thread2.join(); |
| 118 assertEquals(200, thread1.mListener.mHttpStatusCode); | 118 assertEquals(200, thread1.mListener.mHttpStatusCode); |
| 119 assertEquals(404, thread2.mListener.mHttpStatusCode); | 119 assertEquals(404, thread2.mListener.mHttpStatusCode); |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Helper function to make a request. | 122 // Helper function to make a request. |
| 123 private TestHttpUrlRequestListener makeRequest( | 123 private TestHttpUrlRequestListener makeRequest( |
| 124 HttpUrlRequestFactory factory, String url) { | 124 HttpUrlRequestFactory factory, String url) { |
| 125 HashMap<String, String> headers = new HashMap<String, String>(); | 125 HashMap<String, String> headers = new HashMap<String, String>(); |
| 126 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | 126 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); |
| 127 HttpUrlRequest request = factory.createRequest( | 127 HttpUrlRequest request = factory.createRequest( |
| 128 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | 128 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); |
| 129 request.start(); | 129 request.start(); |
| 130 return listener; | 130 return listener; |
| 131 } | 131 } |
| 132 | 132 |
| 133 @SmallTest | 133 @SmallTest |
| 134 @Feature({"Cronet"}) | 134 @Feature({"Cronet"}) |
| 135 public void testInitDifferentContexts() throws Exception { | 135 public void testInitDifferentContexts() throws Exception { |
| 136 // Test that concurrently instantiating ChromiumUrlRequestContext's upon | 136 // Test that concurrently instantiating ChromiumUrlRequestContext's upon |
| 137 // various different versions of the same Android Context does not cause | 137 // various different versions of the same Android Context does not cause |
| 138 // crashes like crbug.com/453845 | 138 // crashes like crbug.com/453845 |
| 139 final CronetTestActivity activity = launchCronetTestApp(); | 139 final CronetTestFramework testFramework = startCronetTestFramework(); |
| 140 HttpUrlRequestFactory firstFactory = | 140 HttpUrlRequestFactory firstFactory = HttpUrlRequestFactory.createFactory
( |
| 141 HttpUrlRequestFactory.createFactory(activity, activity.getCronet
EngineBuilder()); | 141 getContext(), testFramework.getCronetEngineBuilder()); |
| 142 HttpUrlRequestFactory secondFactory = HttpUrlRequestFactory.createFactor
y( | 142 HttpUrlRequestFactory secondFactory = HttpUrlRequestFactory.createFactor
y( |
| 143 activity.getApplicationContext(), activity.getCronetEngineBuilde
r()); | 143 getContext().getApplicationContext(), testFramework.getCronetEng
ineBuilder()); |
| 144 HttpUrlRequestFactory thirdFactory = HttpUrlRequestFactory.createFactory
( | 144 HttpUrlRequestFactory thirdFactory = HttpUrlRequestFactory.createFactory
( |
| 145 new ContextWrapper(activity), activity.getCronetEngineBuilder())
; | 145 new ContextWrapper(getContext()), testFramework.getCronetEngineB
uilder()); |
| 146 // Meager attempt to extend lifetimes to ensure they're concurrently | 146 // Meager attempt to extend lifetimes to ensure they're concurrently |
| 147 // alive. | 147 // alive. |
| 148 firstFactory.getName(); | 148 firstFactory.getName(); |
| 149 secondFactory.getName(); | 149 secondFactory.getName(); |
| 150 thirdFactory.getName(); | 150 thirdFactory.getName(); |
| 151 } | 151 } |
| 152 } | 152 } |
| OLD | NEW |