| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.net; | |
| 6 | |
| 7 import android.content.ContextWrapper; | |
| 8 import android.test.suitebuilder.annotation.SmallTest; | |
| 9 | |
| 10 import org.chromium.base.test.util.Feature; | |
| 11 import org.chromium.net.test.EmbeddedTestServer; | |
| 12 | |
| 13 import java.util.HashMap; | |
| 14 | |
| 15 /** | |
| 16 * Tests that make sure ChromiumUrlRequestContext initialization will not | |
| 17 * affect embedders' ability to make requests. | |
| 18 */ | |
| 19 @SuppressWarnings("deprecation") | |
| 20 public class ContextInitTest extends CronetTestBase { | |
| 21 private EmbeddedTestServer mTestServer; | |
| 22 private String mUrl; | |
| 23 private String mUrl404; | |
| 24 | |
| 25 @Override | |
| 26 protected void setUp() throws Exception { | |
| 27 super.setUp(); | |
| 28 mTestServer = EmbeddedTestServer.createAndStartDefaultServer(getContext(
)); | |
| 29 mUrl = mTestServer.getURL("/echo?status=200"); | |
| 30 mUrl404 = mTestServer.getURL("/echo?status=404"); | |
| 31 } | |
| 32 | |
| 33 @Override | |
| 34 protected void tearDown() throws Exception { | |
| 35 mTestServer.stopAndDestroyServer(); | |
| 36 super.tearDown(); | |
| 37 } | |
| 38 | |
| 39 @SmallTest | |
| 40 @Feature({"Cronet"}) | |
| 41 public void testInitFactoryAndStartRequest() { | |
| 42 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | |
| 43 | |
| 44 // Immediately make a request after initializing the factory. | |
| 45 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | |
| 46 TestHttpUrlRequestListener listener = makeRequest(factory, mUrl); | |
| 47 listener.blockForComplete(); | |
| 48 assertEquals(200, listener.mHttpStatusCode); | |
| 49 } | |
| 50 | |
| 51 @SmallTest | |
| 52 @Feature({"Cronet"}) | |
| 53 public void testInitFactoryStartRequestAndCancel() { | |
| 54 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | |
| 55 | |
| 56 // Make a request and cancel it after initializing the factory. | |
| 57 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | |
| 58 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 59 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 60 HttpUrlRequest request = factory.createRequest( | |
| 61 mUrl, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener)
; | |
| 62 request.start(); | |
| 63 request.cancel(); | |
| 64 listener.blockForComplete(); | |
| 65 assertEquals(0, listener.mHttpStatusCode); | |
| 66 } | |
| 67 | |
| 68 @SmallTest | |
| 69 @Feature({"Cronet"}) | |
| 70 public void testInitFactoryStartTwoRequests() throws Exception { | |
| 71 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipLibra
ryInit(); | |
| 72 | |
| 73 // Make two request right after initializing the factory. | |
| 74 int[] statusCodes = {0, 0}; | |
| 75 String[] urls = {mUrl, mUrl404}; | |
| 76 HttpUrlRequestFactory factory = testFramework.initRequestFactory(); | |
| 77 for (int i = 0; i < 2; i++) { | |
| 78 TestHttpUrlRequestListener listener = makeRequest(factory, urls[i]); | |
| 79 listener.blockForComplete(); | |
| 80 statusCodes[i] = listener.mHttpStatusCode; | |
| 81 } | |
| 82 assertEquals(200, statusCodes[0]); | |
| 83 assertEquals(404, statusCodes[1]); | |
| 84 } | |
| 85 | |
| 86 class RequestThread extends Thread { | |
| 87 public TestHttpUrlRequestListener mCallback; | |
| 88 | |
| 89 final CronetTestFramework mTestFramework; | |
| 90 final String mUrl; | |
| 91 | |
| 92 public RequestThread(CronetTestFramework testFramework, String url) { | |
| 93 mTestFramework = testFramework; | |
| 94 mUrl = url; | |
| 95 } | |
| 96 | |
| 97 @Override | |
| 98 public void run() { | |
| 99 HttpUrlRequestFactory factory = mTestFramework.initRequestFactory(); | |
| 100 mCallback = makeRequest(factory, mUrl); | |
| 101 mCallback.blockForComplete(); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 @SmallTest | |
| 106 @Feature({"Cronet"}) | |
| 107 public void testInitTwoFactoriesSimultaneously() throws Exception { | |
| 108 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); | |
| 109 | |
| 110 RequestThread thread1 = new RequestThread(testFramework, mUrl); | |
| 111 RequestThread thread2 = new RequestThread(testFramework, mUrl404); | |
| 112 | |
| 113 thread1.start(); | |
| 114 thread2.start(); | |
| 115 thread1.join(); | |
| 116 thread2.join(); | |
| 117 assertEquals(200, thread1.mCallback.mHttpStatusCode); | |
| 118 assertEquals(404, thread2.mCallback.mHttpStatusCode); | |
| 119 } | |
| 120 | |
| 121 @SmallTest | |
| 122 @Feature({"Cronet"}) | |
| 123 public void testInitTwoFactoriesInSequence() throws Exception { | |
| 124 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pLibraryInit(); | |
| 125 | |
| 126 RequestThread thread1 = new RequestThread(testFramework, mUrl); | |
| 127 RequestThread thread2 = new RequestThread(testFramework, mUrl404); | |
| 128 | |
| 129 thread1.start(); | |
| 130 thread1.join(); | |
| 131 thread2.start(); | |
| 132 thread2.join(); | |
| 133 assertEquals(200, thread1.mCallback.mHttpStatusCode); | |
| 134 assertEquals(404, thread2.mCallback.mHttpStatusCode); | |
| 135 } | |
| 136 | |
| 137 // Helper function to make a request. | |
| 138 private TestHttpUrlRequestListener makeRequest( | |
| 139 HttpUrlRequestFactory factory, String url) { | |
| 140 HashMap<String, String> headers = new HashMap<String, String>(); | |
| 141 TestHttpUrlRequestListener listener = new TestHttpUrlRequestListener(); | |
| 142 HttpUrlRequest request = factory.createRequest( | |
| 143 url, HttpUrlRequest.REQUEST_PRIORITY_MEDIUM, headers, listener); | |
| 144 request.start(); | |
| 145 return listener; | |
| 146 } | |
| 147 | |
| 148 @SmallTest | |
| 149 @Feature({"Cronet"}) | |
| 150 public void testInitDifferentContexts() throws Exception { | |
| 151 // Test that concurrently instantiating ChromiumUrlRequestContext's upon | |
| 152 // various different versions of the same Android Context does not cause | |
| 153 // crashes like crbug.com/453845 | |
| 154 final CronetTestFramework testFramework = startCronetTestFramework(); | |
| 155 HttpUrlRequestFactory firstFactory = HttpUrlRequestFactory.createFactory
( | |
| 156 getContext(), testFramework.getCronetEngineBuilder()); | |
| 157 HttpUrlRequestFactory secondFactory = HttpUrlRequestFactory.createFactor
y( | |
| 158 getContext().getApplicationContext(), testFramework.getCronetEng
ineBuilder()); | |
| 159 HttpUrlRequestFactory thirdFactory = HttpUrlRequestFactory.createFactory
( | |
| 160 new ContextWrapper(getContext()), testFramework.getCronetEngineB
uilder()); | |
| 161 // Meager attempt to extend lifetimes to ensure they're concurrently | |
| 162 // alive. | |
| 163 firstFactory.getName(); | |
| 164 secondFactory.getName(); | |
| 165 thirdFactory.getName(); | |
| 166 } | |
| 167 } | |
| OLD | NEW |