| 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 16 matching lines...) Expand all Loading... |
| 27 */ | 27 */ |
| 28 public class CronetUrlRequestContextTest extends CronetTestBase { | 28 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 29 // URLs used for tests. | 29 // URLs used for tests. |
| 30 private static final String TEST_URL = "http://127.0.0.1:8000"; | 30 private static final String TEST_URL = "http://127.0.0.1:8000"; |
| 31 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; | 31 private static final String URL_404 = "http://127.0.0.1:8000/notfound404"; |
| 32 private static final String MOCK_CRONET_TEST_FAILED_URL = | 32 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 33 "http://mock.failed.request/-2"; | 33 "http://mock.failed.request/-2"; |
| 34 private static final String MOCK_CRONET_TEST_SUCCESS_URL = | 34 private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| 35 "http://mock.http/success.txt"; | 35 "http://mock.http/success.txt"; |
| 36 | 36 |
| 37 CronetTestActivity mActivity; | 37 CronetTestFramework mTestFramework; |
| 38 | 38 |
| 39 static class RequestThread extends Thread { | 39 static class RequestThread extends Thread { |
| 40 public TestUrlRequestListener mListener; | 40 public TestUrlRequestListener mListener; |
| 41 | 41 |
| 42 final CronetTestActivity mActivity; | 42 final CronetTestFramework mTestFramework; |
| 43 final String mUrl; | 43 final String mUrl; |
| 44 final ConditionVariable mRunBlocker; | 44 final ConditionVariable mRunBlocker; |
| 45 | 45 |
| 46 public RequestThread(CronetTestActivity activity, String url, | 46 public RequestThread( |
| 47 ConditionVariable runBlocker) { | 47 CronetTestFramework testFramework, String url, ConditionVariable
runBlocker) { |
| 48 mActivity = activity; | 48 mTestFramework = testFramework; |
| 49 mUrl = url; | 49 mUrl = url; |
| 50 mRunBlocker = runBlocker; | 50 mRunBlocker = runBlocker; |
| 51 } | 51 } |
| 52 | 52 |
| 53 @Override | 53 @Override |
| 54 public void run() { | 54 public void run() { |
| 55 mRunBlocker.block(); | 55 mRunBlocker.block(); |
| 56 CronetEngine cronetEngine = mActivity.initCronetEngine(); | 56 CronetEngine cronetEngine = mTestFramework.initCronetEngine(); |
| 57 mListener = new TestUrlRequestListener(); | 57 mListener = new TestUrlRequestListener(); |
| 58 UrlRequest.Builder urlRequestBuilder = | 58 UrlRequest.Builder urlRequestBuilder = |
| 59 new UrlRequest.Builder(mUrl, mListener, mListener.getExecuto
r(), cronetEngine); | 59 new UrlRequest.Builder(mUrl, mListener, mListener.getExecuto
r(), cronetEngine); |
| 60 urlRequestBuilder.build().start(); | 60 urlRequestBuilder.build().start(); |
| 61 mListener.blockForDone(); | 61 mListener.blockForDone(); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * Listener that shutdowns the request context when request has succeeded | 66 * Listener that shutdowns the request context when request has succeeded |
| 67 * or failed. | 67 * or failed. |
| 68 */ | 68 */ |
| 69 class ShutdownTestUrlRequestListener extends TestUrlRequestListener { | 69 class ShutdownTestUrlRequestListener extends TestUrlRequestListener { |
| 70 @Override | 70 @Override |
| 71 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { | 71 public void onSucceeded(UrlRequest request, UrlResponseInfo info) { |
| 72 super.onSucceeded(request, info); | 72 super.onSucceeded(request, info); |
| 73 mActivity.mCronetEngine.shutdown(); | 73 mTestFramework.mCronetEngine.shutdown(); |
| 74 } | 74 } |
| 75 | 75 |
| 76 @Override | 76 @Override |
| 77 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques
tException error) { | 77 public void onFailed(UrlRequest request, UrlResponseInfo info, UrlReques
tException error) { |
| 78 super.onFailed(request, info, error); | 78 super.onFailed(request, info, error); |
| 79 mActivity.mCronetEngine.shutdown(); | 79 mTestFramework.mCronetEngine.shutdown(); |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 | 82 |
| 83 static class TestExecutor implements Executor { | 83 static class TestExecutor implements Executor { |
| 84 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>
(); | 84 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>
(); |
| 85 | 85 |
| 86 @Override | 86 @Override |
| 87 public void execute(Runnable task) { | 87 public void execute(Runnable task) { |
| 88 mTaskQueue.add(task); | 88 mTaskQueue.add(task); |
| 89 } | 89 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 118 public int throughputObservationCount() { | 118 public int throughputObservationCount() { |
| 119 return mThroughputObservationCount; | 119 return mThroughputObservationCount; |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 @SmallTest | 123 @SmallTest |
| 124 @Feature({"Cronet"}) | 124 @Feature({"Cronet"}) |
| 125 public void testConfigUserAgent() throws Exception { | 125 public void testConfigUserAgent() throws Exception { |
| 126 String userAgentName = "User-Agent"; | 126 String userAgentName = "User-Agent"; |
| 127 String userAgentValue = "User-Agent-Value"; | 127 String userAgentValue = "User-Agent-Value"; |
| 128 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(mAct
ivity); | 128 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC
ontext()); |
| 129 cronetEngineBuilder.setUserAgent(userAgentValue); | 129 cronetEngineBuilder.setUserAgent(userAgentValue); |
| 130 cronetEngineBuilder.setLibraryName("cronet_tests"); | 130 cronetEngineBuilder.setLibraryName("cronet_tests"); |
| 131 String[] commandLineArgs = {CronetTestActivity.CONFIG_KEY, cronetEngineB
uilder.toString()}; | 131 String[] commandLineArgs = {CronetTestFramework.CONFIG_KEY, cronetEngine
Builder.toString()}; |
| 132 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(TEST_URL, | 132 mTestFramework = |
| 133 commandLineArgs); | 133 startCronetTestFrameworkWithUrlAndCommandLineArgs(TEST_URL, comm
andLineArgs); |
| 134 assertTrue(NativeTestServer.startNativeTestServer( | 134 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 135 getInstrumentation().getTargetContext())); | |
| 136 TestUrlRequestListener listener = new TestUrlRequestListener(); | 135 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 137 UrlRequest.Builder urlRequestBuilder = | 136 UrlRequest.Builder urlRequestBuilder = |
| 138 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge
ntName), listener, | 137 new UrlRequest.Builder(NativeTestServer.getEchoHeaderURL(userAge
ntName), listener, |
| 139 listener.getExecutor(), mActivity.mCronetEngine); | 138 listener.getExecutor(), mTestFramework.mCronetEngine); |
| 140 urlRequestBuilder.build().start(); | 139 urlRequestBuilder.build().start(); |
| 141 listener.blockForDone(); | 140 listener.blockForDone(); |
| 142 assertEquals(userAgentValue, listener.mResponseAsString); | 141 assertEquals(userAgentValue, listener.mResponseAsString); |
| 143 } | 142 } |
| 144 | 143 |
| 145 @SmallTest | 144 @SmallTest |
| 146 @Feature({"Cronet"}) | 145 @Feature({"Cronet"}) |
| 147 public void testDataReductionProxyEnabled() throws Exception { | 146 public void testDataReductionProxyEnabled() throws Exception { |
| 148 mActivity = launchCronetTestAppAndSkipFactoryInit(); | 147 mTestFramework = startCronetTestFrameworkAndSkipFactoryInit(); |
| 149 | 148 |
| 150 // Ensure native code is loaded before trying to start test server. | 149 // Ensure native code is loaded before trying to start test server. |
| 151 new CronetEngine.Builder(getInstrumentation().getTargetContext()) | 150 new CronetEngine.Builder(getContext()).setLibraryName("cronet_tests").bu
ild().shutdown(); |
| 152 .setLibraryName("cronet_tests") | |
| 153 .build() | |
| 154 .shutdown(); | |
| 155 | 151 |
| 156 assertTrue(NativeTestServer.startNativeTestServer( | 152 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 157 getInstrumentation().getTargetContext())); | |
| 158 if (!NativeTestServer.isDataReductionProxySupported()) { | 153 if (!NativeTestServer.isDataReductionProxySupported()) { |
| 159 return; | 154 return; |
| 160 } | 155 } |
| 161 String serverHostPort = NativeTestServer.getHostPort(); | 156 String serverHostPort = NativeTestServer.getHostPort(); |
| 162 | 157 |
| 163 // Enable the Data Reduction Proxy and configure it to use the test | 158 // Enable the Data Reduction Proxy and configure it to use the test |
| 164 // server as its primary proxy, and to check successfully that this | 159 // server as its primary proxy, and to check successfully that this |
| 165 // proxy is OK to use. | 160 // proxy is OK to use. |
| 166 CronetEngine.Builder cronetEngineBuilder = | 161 CronetEngine.Builder cronetEngineBuilder = new CronetEngine.Builder(getC
ontext()); |
| 167 new CronetEngine.Builder(getInstrumentation().getTargetContext()
); | |
| 168 cronetEngineBuilder.enableDataReductionProxy("test-key"); | 162 cronetEngineBuilder.enableDataReductionProxy("test-key"); |
| 169 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused
.net:9999", | 163 cronetEngineBuilder.setDataReductionProxyOptions(serverHostPort, "unused
.net:9999", |
| 170 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); | 164 NativeTestServer.getFileURL("/secureproxychecksuccess.txt")); |
| 171 cronetEngineBuilder.setLibraryName("cronet_tests"); | 165 cronetEngineBuilder.setLibraryName("cronet_tests"); |
| 172 mActivity.mCronetEngine = cronetEngineBuilder.build(); | 166 mTestFramework.mCronetEngine = cronetEngineBuilder.build(); |
| 173 TestUrlRequestListener listener = new TestUrlRequestListener(); | 167 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 174 | 168 |
| 175 // Construct and start a request that can only be returned by the test | 169 // Construct and start a request that can only be returned by the test |
| 176 // server. This request will fail if the configuration logic for the | 170 // server. This request will fail if the configuration logic for the |
| 177 // Data Reduction Proxy is not used. | 171 // Data Reduction Proxy is not used. |
| 178 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 172 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 179 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
listener, | 173 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt",
listener, |
| 180 listener.getExecutor(), mActivity.mCronetEngine); | 174 listener.getExecutor(), mTestFramework.mCronetEngine); |
| 181 urlRequestBuilder.build().start(); | 175 urlRequestBuilder.build().start(); |
| 182 listener.blockForDone(); | 176 listener.blockForDone(); |
| 183 | 177 |
| 184 // Verify that the request is successful and that the Data Reduction | 178 // Verify that the request is successful and that the Data Reduction |
| 185 // Proxy logic configured to use the test server as its proxy. | 179 // Proxy logic configured to use the test server as its proxy. |
| 186 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 180 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 187 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); | 181 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); |
| 188 assertEquals( | 182 assertEquals( |
| 189 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", | 183 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", |
| 190 listener.mResponseInfo.getUrl()); | 184 listener.mResponseInfo.getUrl()); |
| 191 } | 185 } |
| 192 | 186 |
| 193 @SmallTest | 187 @SmallTest |
| 194 @Feature({"Cronet"}) | 188 @Feature({"Cronet"}) |
| 195 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except
ion { | 189 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except
ion { |
| 196 mActivity = launchCronetTestApp(); | 190 mTestFramework = startCronetTestFramework(); |
| 197 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); | 191 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 198 try { | 192 try { |
| 199 mActivity.mCronetEngine.addRttListener(networkQualityListener); | 193 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 200 fail("Should throw an exception."); | 194 fail("Should throw an exception."); |
| 201 } catch (IllegalStateException e) { | 195 } catch (IllegalStateException e) { |
| 202 } | 196 } |
| 203 try { | 197 try { |
| 204 mActivity.mCronetEngine.addThroughputListener(networkQualityListener
); | 198 mTestFramework.mCronetEngine.addThroughputListener(networkQualityLis
tener); |
| 205 fail("Should throw an exception."); | 199 fail("Should throw an exception."); |
| 206 } catch (IllegalStateException e) { | 200 } catch (IllegalStateException e) { |
| 207 } | 201 } |
| 208 TestUrlRequestListener listener = new TestUrlRequestListener(); | 202 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 209 UrlRequest urlRequest = | 203 UrlRequest urlRequest = mTestFramework.mCronetEngine.createRequest( |
| 210 mActivity.mCronetEngine.createRequest(TEST_URL, listener, listen
er.getExecutor()); | 204 TEST_URL, listener, listener.getExecutor()); |
| 211 urlRequest.start(); | 205 urlRequest.start(); |
| 212 listener.blockForDone(); | 206 listener.blockForDone(); |
| 213 assertEquals(0, networkQualityListener.rttObservationCount()); | 207 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 214 assertEquals(0, networkQualityListener.throughputObservationCount()); | 208 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 215 mActivity.mCronetEngine.shutdown(); | 209 mTestFramework.mCronetEngine.shutdown(); |
| 216 } | 210 } |
| 217 | 211 |
| 218 @SmallTest | 212 @SmallTest |
| 219 @Feature({"Cronet"}) | 213 @Feature({"Cronet"}) |
| 220 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E
xception { | 214 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E
xception { |
| 221 mActivity = launchCronetTestApp(); | 215 mTestFramework = startCronetTestFramework(); |
| 222 TestExecutor testExecutor = new TestExecutor(); | 216 TestExecutor testExecutor = new TestExecutor(); |
| 223 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); | 217 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 224 mActivity.mCronetEngine.enableNetworkQualityEstimatorForTesting(true, tr
ue, testExecutor); | 218 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( |
| 225 mActivity.mCronetEngine.addRttListener(networkQualityListener); | 219 true, true, testExecutor); |
| 226 mActivity.mCronetEngine.addThroughputListener(networkQualityListener); | 220 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 227 mActivity.mCronetEngine.removeRttListener(networkQualityListener); | 221 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene
r); |
| 228 mActivity.mCronetEngine.removeThroughputListener(networkQualityListener)
; | 222 mTestFramework.mCronetEngine.removeRttListener(networkQualityListener); |
| 223 mTestFramework.mCronetEngine.removeThroughputListener(networkQualityList
ener); |
| 229 TestUrlRequestListener listener = new TestUrlRequestListener(); | 224 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 230 UrlRequest urlRequest = | 225 UrlRequest urlRequest = mTestFramework.mCronetEngine.createRequest( |
| 231 mActivity.mCronetEngine.createRequest(TEST_URL, listener, listen
er.getExecutor()); | 226 TEST_URL, listener, listener.getExecutor()); |
| 232 urlRequest.start(); | 227 urlRequest.start(); |
| 233 listener.blockForDone(); | 228 listener.blockForDone(); |
| 234 testExecutor.runAllTasks(); | 229 testExecutor.runAllTasks(); |
| 235 assertEquals(0, networkQualityListener.rttObservationCount()); | 230 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 236 assertEquals(0, networkQualityListener.throughputObservationCount()); | 231 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 237 mActivity.mCronetEngine.shutdown(); | 232 mTestFramework.mCronetEngine.shutdown(); |
| 238 } | 233 } |
| 239 | 234 |
| 240 @SmallTest | 235 @SmallTest |
| 241 @Feature({"Cronet"}) | 236 @Feature({"Cronet"}) |
| 242 public void testRealTimeNetworkQualityObservations() throws Exception { | 237 public void testRealTimeNetworkQualityObservations() throws Exception { |
| 243 mActivity = launchCronetTestApp(); | 238 mTestFramework = startCronetTestFramework(); |
| 244 TestExecutor testExecutor = new TestExecutor(); | 239 TestExecutor testExecutor = new TestExecutor(); |
| 245 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); | 240 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 246 mActivity.mCronetEngine.enableNetworkQualityEstimatorForTesting(true, tr
ue, testExecutor); | 241 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( |
| 247 mActivity.mCronetEngine.addRttListener(networkQualityListener); | 242 true, true, testExecutor); |
| 248 mActivity.mCronetEngine.addThroughputListener(networkQualityListener); | 243 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 244 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene
r); |
| 249 TestUrlRequestListener listener = new TestUrlRequestListener(); | 245 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 250 UrlRequest urlRequest = | 246 UrlRequest urlRequest = mTestFramework.mCronetEngine.createRequest( |
| 251 mActivity.mCronetEngine.createRequest(TEST_URL, listener, listen
er.getExecutor()); | 247 TEST_URL, listener, listener.getExecutor()); |
| 252 urlRequest.start(); | 248 urlRequest.start(); |
| 253 listener.blockForDone(); | 249 listener.blockForDone(); |
| 254 testExecutor.runAllTasks(); | 250 testExecutor.runAllTasks(); |
| 255 assertTrue(networkQualityListener.rttObservationCount() > 0); | 251 assertTrue(networkQualityListener.rttObservationCount() > 0); |
| 256 assertTrue(networkQualityListener.throughputObservationCount() > 0); | 252 assertTrue(networkQualityListener.throughputObservationCount() > 0); |
| 257 mActivity.mCronetEngine.shutdown(); | 253 mTestFramework.mCronetEngine.shutdown(); |
| 258 } | 254 } |
| 259 | 255 |
| 260 @SmallTest | 256 @SmallTest |
| 261 @Feature({"Cronet"}) | 257 @Feature({"Cronet"}) |
| 262 public void testShutdown() throws Exception { | 258 public void testShutdown() throws Exception { |
| 263 mActivity = launchCronetTestApp(); | 259 mTestFramework = startCronetTestFramework(); |
| 264 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); | 260 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |
| 265 // Block listener when response starts to verify that shutdown fails | 261 // Block listener when response starts to verify that shutdown fails |
| 266 // if there are active requests. | 262 // if there are active requests. |
| 267 listener.setAutoAdvance(false); | 263 listener.setAutoAdvance(false); |
| 268 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 264 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 269 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng
ine); | 265 TEST_URL, listener, listener.getExecutor(), mTestFramework.mCron
etEngine); |
| 270 UrlRequest urlRequest = urlRequestBuilder.build(); | 266 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 271 urlRequest.start(); | 267 urlRequest.start(); |
| 272 try { | 268 try { |
| 273 mActivity.mCronetEngine.shutdown(); | 269 mTestFramework.mCronetEngine.shutdown(); |
| 274 fail("Should throw an exception"); | 270 fail("Should throw an exception"); |
| 275 } catch (Exception e) { | 271 } catch (Exception e) { |
| 276 assertEquals("Cannot shutdown with active requests.", | 272 assertEquals("Cannot shutdown with active requests.", |
| 277 e.getMessage()); | 273 e.getMessage()); |
| 278 } | 274 } |
| 279 | 275 |
| 280 listener.waitForNextStep(); | 276 listener.waitForNextStep(); |
| 281 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); | 277 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); |
| 282 try { | 278 try { |
| 283 mActivity.mCronetEngine.shutdown(); | 279 mTestFramework.mCronetEngine.shutdown(); |
| 284 fail("Should throw an exception"); | 280 fail("Should throw an exception"); |
| 285 } catch (Exception e) { | 281 } catch (Exception e) { |
| 286 assertEquals("Cannot shutdown with active requests.", | 282 assertEquals("Cannot shutdown with active requests.", |
| 287 e.getMessage()); | 283 e.getMessage()); |
| 288 } | 284 } |
| 289 listener.startNextRead(urlRequest); | 285 listener.startNextRead(urlRequest); |
| 290 | 286 |
| 291 listener.waitForNextStep(); | 287 listener.waitForNextStep(); |
| 292 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep); | 288 assertEquals(ResponseStep.ON_READ_COMPLETED, listener.mResponseStep); |
| 293 try { | 289 try { |
| 294 mActivity.mCronetEngine.shutdown(); | 290 mTestFramework.mCronetEngine.shutdown(); |
| 295 fail("Should throw an exception"); | 291 fail("Should throw an exception"); |
| 296 } catch (Exception e) { | 292 } catch (Exception e) { |
| 297 assertEquals("Cannot shutdown with active requests.", | 293 assertEquals("Cannot shutdown with active requests.", |
| 298 e.getMessage()); | 294 e.getMessage()); |
| 299 } | 295 } |
| 300 | 296 |
| 301 // May not have read all the data, in theory. Just enable auto-advance | 297 // May not have read all the data, in theory. Just enable auto-advance |
| 302 // and finish the request. | 298 // and finish the request. |
| 303 listener.setAutoAdvance(true); | 299 listener.setAutoAdvance(true); |
| 304 listener.startNextRead(urlRequest); | 300 listener.startNextRead(urlRequest); |
| 305 listener.blockForDone(); | 301 listener.blockForDone(); |
| 306 } | 302 } |
| 307 | 303 |
| 308 @SmallTest | 304 @SmallTest |
| 309 @Feature({"Cronet"}) | 305 @Feature({"Cronet"}) |
| 310 public void testShutdownDuringInit() throws Exception { | 306 public void testShutdownDuringInit() throws Exception { |
| 311 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 307 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 312 final ConditionVariable block = new ConditionVariable(false); | 308 final ConditionVariable block = new ConditionVariable(false); |
| 313 | 309 |
| 314 // Post a task to main thread to block until shutdown is called to test | 310 // Post a task to main thread to block until shutdown is called to test |
| 315 // scenario when shutdown is called right after construction before | 311 // scenario when shutdown is called right after construction before |
| 316 // context is fully initialized on the main thread. | 312 // context is fully initialized on the main thread. |
| 317 Runnable blockingTask = new Runnable() { | 313 Runnable blockingTask = new Runnable() { |
| 318 public void run() { | 314 public void run() { |
| 319 try { | 315 try { |
| 320 block.block(); | 316 block.block(); |
| 321 } catch (Exception e) { | 317 } catch (Exception e) { |
| 322 fail("Caught " + e.getMessage()); | 318 fail("Caught " + e.getMessage()); |
| 323 } | 319 } |
| 324 } | 320 } |
| 325 }; | 321 }; |
| 326 // Ensure that test is not running on the main thread. | 322 // Ensure that test is not running on the main thread. |
| 327 assertTrue(Looper.getMainLooper() != Looper.myLooper()); | 323 assertTrue(Looper.getMainLooper() != Looper.myLooper()); |
| 328 new Handler(Looper.getMainLooper()).post(blockingTask); | 324 new Handler(Looper.getMainLooper()).post(blockingTask); |
| 329 | 325 |
| 330 // Create new request context, but its initialization on the main thread | 326 // Create new request context, but its initialization on the main thread |
| 331 // will be stuck behind blockingTask. | 327 // will be stuck behind blockingTask. |
| 332 final CronetEngine cronetEngine = activity.initCronetEngine(); | 328 final CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 333 // Unblock the main thread, so context gets initialized and shutdown on | 329 // Unblock the main thread, so context gets initialized and shutdown on |
| 334 // it. | 330 // it. |
| 335 block.open(); | 331 block.open(); |
| 336 // Shutdown will wait for init to complete on main thread. | 332 // Shutdown will wait for init to complete on main thread. |
| 337 cronetEngine.shutdown(); | 333 cronetEngine.shutdown(); |
| 338 // Verify that context is shutdown. | 334 // Verify that context is shutdown. |
| 339 try { | 335 try { |
| 340 cronetEngine.stopNetLog(); | 336 cronetEngine.stopNetLog(); |
| 341 fail("Should throw an exception."); | 337 fail("Should throw an exception."); |
| 342 } catch (Exception e) { | 338 } catch (Exception e) { |
| 343 assertEquals("Engine is shut down.", e.getMessage()); | 339 assertEquals("Engine is shut down.", e.getMessage()); |
| 344 } | 340 } |
| 345 } | 341 } |
| 346 | 342 |
| 347 @SmallTest | 343 @SmallTest |
| 348 @Feature({"Cronet"}) | 344 @Feature({"Cronet"}) |
| 349 public void testInitAndShutdownOnMainThread() throws Exception { | 345 public void testInitAndShutdownOnMainThread() throws Exception { |
| 350 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 346 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 351 final ConditionVariable block = new ConditionVariable(false); | 347 final ConditionVariable block = new ConditionVariable(false); |
| 352 | 348 |
| 353 // Post a task to main thread to init and shutdown on the main thread. | 349 // Post a task to main thread to init and shutdown on the main thread. |
| 354 Runnable blockingTask = new Runnable() { | 350 Runnable blockingTask = new Runnable() { |
| 355 public void run() { | 351 public void run() { |
| 356 // Create new request context, loading the library. | 352 // Create new request context, loading the library. |
| 357 final CronetEngine cronetEngine = activity.initCronetEngine(); | 353 final CronetEngine cronetEngine = testFramework.initCronetEngine
(); |
| 358 // Shutdown right after init. | 354 // Shutdown right after init. |
| 359 cronetEngine.shutdown(); | 355 cronetEngine.shutdown(); |
| 360 // Verify that context is shutdown. | 356 // Verify that context is shutdown. |
| 361 try { | 357 try { |
| 362 cronetEngine.stopNetLog(); | 358 cronetEngine.stopNetLog(); |
| 363 fail("Should throw an exception."); | 359 fail("Should throw an exception."); |
| 364 } catch (Exception e) { | 360 } catch (Exception e) { |
| 365 assertEquals("Engine is shut down.", e.getMessage()); | 361 assertEquals("Engine is shut down.", e.getMessage()); |
| 366 } | 362 } |
| 367 block.open(); | 363 block.open(); |
| 368 } | 364 } |
| 369 }; | 365 }; |
| 370 new Handler(Looper.getMainLooper()).post(blockingTask); | 366 new Handler(Looper.getMainLooper()).post(blockingTask); |
| 371 // Wait for shutdown to complete on main thread. | 367 // Wait for shutdown to complete on main thread. |
| 372 block.block(); | 368 block.block(); |
| 373 } | 369 } |
| 374 | 370 |
| 375 @SmallTest | 371 @SmallTest |
| 376 @Feature({"Cronet"}) | 372 @Feature({"Cronet"}) |
| 377 public void testMultipleShutdown() throws Exception { | 373 public void testMultipleShutdown() throws Exception { |
| 378 mActivity = launchCronetTestApp(); | 374 mTestFramework = startCronetTestFramework(); |
| 379 try { | 375 try { |
| 380 mActivity.mCronetEngine.shutdown(); | 376 mTestFramework.mCronetEngine.shutdown(); |
| 381 mActivity.mCronetEngine.shutdown(); | 377 mTestFramework.mCronetEngine.shutdown(); |
| 382 fail("Should throw an exception"); | 378 fail("Should throw an exception"); |
| 383 } catch (Exception e) { | 379 } catch (Exception e) { |
| 384 assertEquals("Engine is shut down.", e.getMessage()); | 380 assertEquals("Engine is shut down.", e.getMessage()); |
| 385 } | 381 } |
| 386 } | 382 } |
| 387 | 383 |
| 388 @SmallTest | 384 @SmallTest |
| 389 @Feature({"Cronet"}) | 385 @Feature({"Cronet"}) |
| 390 public void testShutdownAfterError() throws Exception { | 386 public void testShutdownAfterError() throws Exception { |
| 391 mActivity = launchCronetTestApp(); | 387 mTestFramework = startCronetTestFramework(); |
| 392 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); | 388 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |
| 393 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE
T_TEST_FAILED_URL, | 389 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder(MOCK_CRONE
T_TEST_FAILED_URL, |
| 394 listener, listener.getExecutor(), mActivity.mCronetEngine); | 390 listener, listener.getExecutor(), mTestFramework.mCronetEngine); |
| 395 urlRequestBuilder.build().start(); | 391 urlRequestBuilder.build().start(); |
| 396 listener.blockForDone(); | 392 listener.blockForDone(); |
| 397 assertTrue(listener.mOnErrorCalled); | 393 assertTrue(listener.mOnErrorCalled); |
| 398 } | 394 } |
| 399 | 395 |
| 400 @SmallTest | 396 @SmallTest |
| 401 @Feature({"Cronet"}) | 397 @Feature({"Cronet"}) |
| 402 public void testShutdownAfterCancel() throws Exception { | 398 public void testShutdownAfterCancel() throws Exception { |
| 403 mActivity = launchCronetTestApp(); | 399 mTestFramework = startCronetTestFramework(); |
| 404 TestUrlRequestListener listener = new TestUrlRequestListener(); | 400 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 405 // Block listener when response starts to verify that shutdown fails | 401 // Block listener when response starts to verify that shutdown fails |
| 406 // if there are active requests. | 402 // if there are active requests. |
| 407 listener.setAutoAdvance(false); | 403 listener.setAutoAdvance(false); |
| 408 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 404 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 409 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng
ine); | 405 TEST_URL, listener, listener.getExecutor(), mTestFramework.mCron
etEngine); |
| 410 UrlRequest urlRequest = urlRequestBuilder.build(); | 406 UrlRequest urlRequest = urlRequestBuilder.build(); |
| 411 urlRequest.start(); | 407 urlRequest.start(); |
| 412 try { | 408 try { |
| 413 mActivity.mCronetEngine.shutdown(); | 409 mTestFramework.mCronetEngine.shutdown(); |
| 414 fail("Should throw an exception"); | 410 fail("Should throw an exception"); |
| 415 } catch (Exception e) { | 411 } catch (Exception e) { |
| 416 assertEquals("Cannot shutdown with active requests.", | 412 assertEquals("Cannot shutdown with active requests.", |
| 417 e.getMessage()); | 413 e.getMessage()); |
| 418 } | 414 } |
| 419 listener.waitForNextStep(); | 415 listener.waitForNextStep(); |
| 420 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); | 416 assertEquals(ResponseStep.ON_RESPONSE_STARTED, listener.mResponseStep); |
| 421 urlRequest.cancel(); | 417 urlRequest.cancel(); |
| 422 mActivity.mCronetEngine.shutdown(); | 418 mTestFramework.mCronetEngine.shutdown(); |
| 423 } | 419 } |
| 424 | 420 |
| 425 @SmallTest | 421 @SmallTest |
| 426 @Feature({"Cronet"}) | 422 @Feature({"Cronet"}) |
| 427 public void testNetLog() throws Exception { | 423 public void testNetLog() throws Exception { |
| 428 Context context = getInstrumentation().getTargetContext(); | 424 Context context = getContext(); |
| 429 File directory = new File(PathUtils.getDataDirectory(context)); | 425 File directory = new File(PathUtils.getDataDirectory(context)); |
| 430 File file = File.createTempFile("cronet", "json", directory); | 426 File file = File.createTempFile("cronet", "json", directory); |
| 431 CronetEngine cronetEngine = new CronetUrlRequestContext( | 427 CronetEngine cronetEngine = new CronetUrlRequestContext( |
| 432 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 428 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); |
| 433 // Start NetLog immediately after the request context is created to make | 429 // Start NetLog immediately after the request context is created to make |
| 434 // sure that the call won't crash the app even when the native request | 430 // sure that the call won't crash the app even when the native request |
| 435 // context is not fully initialized. See crbug.com/470196. | 431 // context is not fully initialized. See crbug.com/470196. |
| 436 cronetEngine.startNetLogToFile(file.getPath(), false); | 432 cronetEngine.startNetLogToFile(file.getPath(), false); |
| 437 | 433 |
| 438 // Start a request. | 434 // Start a request. |
| 439 TestUrlRequestListener listener = new TestUrlRequestListener(); | 435 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 440 UrlRequest.Builder urlRequestBuilder = | 436 UrlRequest.Builder urlRequestBuilder = |
| 441 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); | 437 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); |
| 442 urlRequestBuilder.build().start(); | 438 urlRequestBuilder.build().start(); |
| 443 listener.blockForDone(); | 439 listener.blockForDone(); |
| 444 cronetEngine.stopNetLog(); | 440 cronetEngine.stopNetLog(); |
| 445 assertTrue(file.exists()); | 441 assertTrue(file.exists()); |
| 446 assertTrue(file.length() != 0); | 442 assertTrue(file.length() != 0); |
| 447 assertFalse(hasBytesInNetLog(file)); | 443 assertFalse(hasBytesInNetLog(file)); |
| 448 assertTrue(file.delete()); | 444 assertTrue(file.delete()); |
| 449 assertTrue(!file.exists()); | 445 assertTrue(!file.exists()); |
| 450 } | 446 } |
| 451 | 447 |
| 452 @SmallTest | 448 @SmallTest |
| 453 @Feature({"Cronet"}) | 449 @Feature({"Cronet"}) |
| 454 public void testNetLogAfterShutdown() throws Exception { | 450 public void testNetLogAfterShutdown() throws Exception { |
| 455 mActivity = launchCronetTestApp(); | 451 mTestFramework = startCronetTestFramework(); |
| 456 TestUrlRequestListener listener = new TestUrlRequestListener(); | 452 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 457 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 453 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 458 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng
ine); | 454 TEST_URL, listener, listener.getExecutor(), mTestFramework.mCron
etEngine); |
| 459 urlRequestBuilder.build().start(); | 455 urlRequestBuilder.build().start(); |
| 460 listener.blockForDone(); | 456 listener.blockForDone(); |
| 461 mActivity.mCronetEngine.shutdown(); | 457 mTestFramework.mCronetEngine.shutdown(); |
| 462 | 458 |
| 463 File directory = new File(PathUtils.getDataDirectory( | 459 File directory = new File(PathUtils.getDataDirectory(getContext())); |
| 464 getInstrumentation().getTargetContext())); | |
| 465 File file = File.createTempFile("cronet", "json", directory); | 460 File file = File.createTempFile("cronet", "json", directory); |
| 466 try { | 461 try { |
| 467 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 462 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false
); |
| 468 fail("Should throw an exception."); | 463 fail("Should throw an exception."); |
| 469 } catch (Exception e) { | 464 } catch (Exception e) { |
| 470 assertEquals("Engine is shut down.", e.getMessage()); | 465 assertEquals("Engine is shut down.", e.getMessage()); |
| 471 } | 466 } |
| 472 assertFalse(hasBytesInNetLog(file)); | 467 assertFalse(hasBytesInNetLog(file)); |
| 473 assertTrue(file.delete()); | 468 assertTrue(file.delete()); |
| 474 assertTrue(!file.exists()); | 469 assertTrue(!file.exists()); |
| 475 } | 470 } |
| 476 | 471 |
| 477 @SmallTest | 472 @SmallTest |
| 478 @Feature({"Cronet"}) | 473 @Feature({"Cronet"}) |
| 479 public void testNetLogStartMultipleTimes() throws Exception { | 474 public void testNetLogStartMultipleTimes() throws Exception { |
| 480 mActivity = launchCronetTestApp(); | 475 mTestFramework = startCronetTestFramework(); |
| 481 File directory = new File(PathUtils.getDataDirectory( | 476 File directory = new File(PathUtils.getDataDirectory(getContext())); |
| 482 getInstrumentation().getTargetContext())); | |
| 483 File file = File.createTempFile("cronet", "json", directory); | 477 File file = File.createTempFile("cronet", "json", directory); |
| 484 // Start NetLog multiple times. | 478 // Start NetLog multiple times. |
| 485 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 479 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 486 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 480 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 487 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 481 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 488 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 482 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 489 // Start a request. | 483 // Start a request. |
| 490 TestUrlRequestListener listener = new TestUrlRequestListener(); | 484 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 491 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 485 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 492 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng
ine); | 486 TEST_URL, listener, listener.getExecutor(), mTestFramework.mCron
etEngine); |
| 493 urlRequestBuilder.build().start(); | 487 urlRequestBuilder.build().start(); |
| 494 listener.blockForDone(); | 488 listener.blockForDone(); |
| 495 mActivity.mCronetEngine.stopNetLog(); | 489 mTestFramework.mCronetEngine.stopNetLog(); |
| 496 assertTrue(file.exists()); | 490 assertTrue(file.exists()); |
| 497 assertTrue(file.length() != 0); | 491 assertTrue(file.length() != 0); |
| 498 assertFalse(hasBytesInNetLog(file)); | 492 assertFalse(hasBytesInNetLog(file)); |
| 499 assertTrue(file.delete()); | 493 assertTrue(file.delete()); |
| 500 assertTrue(!file.exists()); | 494 assertTrue(!file.exists()); |
| 501 } | 495 } |
| 502 | 496 |
| 503 @SmallTest | 497 @SmallTest |
| 504 @Feature({"Cronet"}) | 498 @Feature({"Cronet"}) |
| 505 public void testNetLogStopMultipleTimes() throws Exception { | 499 public void testNetLogStopMultipleTimes() throws Exception { |
| 506 mActivity = launchCronetTestApp(); | 500 mTestFramework = startCronetTestFramework(); |
| 507 File directory = new File(PathUtils.getDataDirectory( | 501 File directory = new File(PathUtils.getDataDirectory(getContext())); |
| 508 getInstrumentation().getTargetContext())); | |
| 509 File file = File.createTempFile("cronet", "json", directory); | 502 File file = File.createTempFile("cronet", "json", directory); |
| 510 mActivity.mCronetEngine.startNetLogToFile(file.getPath(), false); | 503 mTestFramework.mCronetEngine.startNetLogToFile(file.getPath(), false); |
| 511 // Start a request. | 504 // Start a request. |
| 512 TestUrlRequestListener listener = new TestUrlRequestListener(); | 505 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 513 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 506 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 514 TEST_URL, listener, listener.getExecutor(), mActivity.mCronetEng
ine); | 507 TEST_URL, listener, listener.getExecutor(), mTestFramework.mCron
etEngine); |
| 515 urlRequestBuilder.build().start(); | 508 urlRequestBuilder.build().start(); |
| 516 listener.blockForDone(); | 509 listener.blockForDone(); |
| 517 // Stop NetLog multiple times. | 510 // Stop NetLog multiple times. |
| 518 mActivity.mCronetEngine.stopNetLog(); | 511 mTestFramework.mCronetEngine.stopNetLog(); |
| 519 mActivity.mCronetEngine.stopNetLog(); | 512 mTestFramework.mCronetEngine.stopNetLog(); |
| 520 mActivity.mCronetEngine.stopNetLog(); | 513 mTestFramework.mCronetEngine.stopNetLog(); |
| 521 mActivity.mCronetEngine.stopNetLog(); | 514 mTestFramework.mCronetEngine.stopNetLog(); |
| 522 mActivity.mCronetEngine.stopNetLog(); | 515 mTestFramework.mCronetEngine.stopNetLog(); |
| 523 assertTrue(file.exists()); | 516 assertTrue(file.exists()); |
| 524 assertTrue(file.length() != 0); | 517 assertTrue(file.length() != 0); |
| 525 assertFalse(hasBytesInNetLog(file)); | 518 assertFalse(hasBytesInNetLog(file)); |
| 526 assertTrue(file.delete()); | 519 assertTrue(file.delete()); |
| 527 assertTrue(!file.exists()); | 520 assertTrue(!file.exists()); |
| 528 } | 521 } |
| 529 | 522 |
| 530 @SmallTest | 523 @SmallTest |
| 531 @Feature({"Cronet"}) | 524 @Feature({"Cronet"}) |
| 532 public void testNetLogWithBytes() throws Exception { | 525 public void testNetLogWithBytes() throws Exception { |
| 533 Context context = getInstrumentation().getTargetContext(); | 526 Context context = getContext(); |
| 534 File directory = new File(PathUtils.getDataDirectory(context)); | 527 File directory = new File(PathUtils.getDataDirectory(context)); |
| 535 File file = File.createTempFile("cronet", "json", directory); | 528 File file = File.createTempFile("cronet", "json", directory); |
| 536 CronetEngine cronetEngine = new CronetUrlRequestContext( | 529 CronetEngine cronetEngine = new CronetUrlRequestContext( |
| 537 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); | 530 new CronetEngine.Builder(context).setLibraryName("cronet_tests")
); |
| 538 // Start NetLog with logAll as true. | 531 // Start NetLog with logAll as true. |
| 539 cronetEngine.startNetLogToFile(file.getPath(), true); | 532 cronetEngine.startNetLogToFile(file.getPath(), true); |
| 540 // Start a request. | 533 // Start a request. |
| 541 TestUrlRequestListener listener = new TestUrlRequestListener(); | 534 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 542 UrlRequest.Builder urlRequestBuilder = | 535 UrlRequest.Builder urlRequestBuilder = |
| 543 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); | 536 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 562 } | 555 } |
| 563 return false; | 556 return false; |
| 564 } finally { | 557 } finally { |
| 565 logReader.close(); | 558 logReader.close(); |
| 566 } | 559 } |
| 567 } | 560 } |
| 568 | 561 |
| 569 private void enableCache(int cacheType) throws Exception { | 562 private void enableCache(int cacheType) throws Exception { |
| 570 String cacheTypeString = ""; | 563 String cacheTypeString = ""; |
| 571 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { | 564 if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK) { |
| 572 cacheTypeString = CronetTestActivity.CACHE_DISK; | 565 cacheTypeString = CronetTestFramework.CACHE_DISK; |
| 573 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { | 566 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) { |
| 574 cacheTypeString = CronetTestActivity.CACHE_DISK_NO_HTTP; | 567 cacheTypeString = CronetTestFramework.CACHE_DISK_NO_HTTP; |
| 575 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { | 568 } else if (cacheType == CronetEngine.Builder.HTTP_CACHE_IN_MEMORY) { |
| 576 cacheTypeString = CronetTestActivity.CACHE_IN_MEMORY; | 569 cacheTypeString = CronetTestFramework.CACHE_IN_MEMORY; |
| 577 } | 570 } |
| 578 String[] commandLineArgs = {CronetTestActivity.CACHE_KEY, cacheTypeStrin
g}; | 571 String[] commandLineArgs = {CronetTestFramework.CACHE_KEY, cacheTypeStri
ng}; |
| 579 mActivity = launchCronetTestAppWithUrlAndCommandLineArgs(null, | 572 mTestFramework = startCronetTestFrameworkWithUrlAndCommandLineArgs(null,
commandLineArgs); |
| 580 commandLineArgs); | 573 assertTrue(NativeTestServer.startNativeTestServer(getContext())); |
| 581 assertTrue(NativeTestServer.startNativeTestServer( | |
| 582 getInstrumentation().getTargetContext())); | |
| 583 } | 574 } |
| 584 | 575 |
| 585 private void checkRequestCaching(String url, boolean expectCached) { | 576 private void checkRequestCaching(String url, boolean expectCached) { |
| 586 checkRequestCaching(url, expectCached, false); | 577 checkRequestCaching(url, expectCached, false); |
| 587 } | 578 } |
| 588 | 579 |
| 589 private void checkRequestCaching(String url, boolean expectCached, | 580 private void checkRequestCaching(String url, boolean expectCached, |
| 590 boolean disableCache) { | 581 boolean disableCache) { |
| 591 TestUrlRequestListener listener = new TestUrlRequestListener(); | 582 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 592 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 583 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 593 url, listener, listener.getExecutor(), mActivity.mCronetEngine); | 584 url, listener, listener.getExecutor(), mTestFramework.mCronetEng
ine); |
| 594 if (disableCache) { | 585 if (disableCache) { |
| 595 urlRequestBuilder.disableCache(); | 586 urlRequestBuilder.disableCache(); |
| 596 } | 587 } |
| 597 urlRequestBuilder.build().start(); | 588 urlRequestBuilder.build().start(); |
| 598 listener.blockForDone(); | 589 listener.blockForDone(); |
| 599 assertEquals(expectCached, listener.mResponseInfo.wasCached()); | 590 assertEquals(expectCached, listener.mResponseInfo.wasCached()); |
| 600 } | 591 } |
| 601 | 592 |
| 602 @SmallTest | 593 @SmallTest |
| 603 @Feature({"Cronet"}) | 594 @Feature({"Cronet"}) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 655 checkRequestCaching(url, false, true /** disable cache */); | 646 checkRequestCaching(url, false, true /** disable cache */); |
| 656 checkRequestCaching(url, true); | 647 checkRequestCaching(url, true); |
| 657 | 648 |
| 658 // Shut down the server, next request should have a cached response. | 649 // Shut down the server, next request should have a cached response. |
| 659 NativeTestServer.shutdownNativeTestServer(); | 650 NativeTestServer.shutdownNativeTestServer(); |
| 660 checkRequestCaching(url, true); | 651 checkRequestCaching(url, true); |
| 661 | 652 |
| 662 // Cache is disabled after server is shut down, request should fail. | 653 // Cache is disabled after server is shut down, request should fail. |
| 663 TestUrlRequestListener listener = new TestUrlRequestListener(); | 654 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 664 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( | 655 UrlRequest.Builder urlRequestBuilder = new UrlRequest.Builder( |
| 665 url, listener, listener.getExecutor(), mActivity.mCronetEngine); | 656 url, listener, listener.getExecutor(), mTestFramework.mCronetEng
ine); |
| 666 urlRequestBuilder.disableCache(); | 657 urlRequestBuilder.disableCache(); |
| 667 urlRequestBuilder.build().start(); | 658 urlRequestBuilder.build().start(); |
| 668 listener.blockForDone(); | 659 listener.blockForDone(); |
| 669 assertNotNull(listener.mError); | 660 assertNotNull(listener.mError); |
| 670 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", | 661 assertEquals("Exception in CronetUrlRequest: net::ERR_CONNECTION_REFUSED
", |
| 671 listener.mError.getMessage()); | 662 listener.mError.getMessage()); |
| 672 } | 663 } |
| 673 | 664 |
| 674 @SmallTest | 665 @SmallTest |
| 675 @Feature({"Cronet"}) | 666 @Feature({"Cronet"}) |
| 676 public void testEnableHttpCacheDiskNewEngine() throws Exception { | 667 public void testEnableHttpCacheDiskNewEngine() throws Exception { |
| 677 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); | 668 enableCache(CronetEngine.Builder.HTTP_CACHE_DISK); |
| 678 String url = NativeTestServer.getFileURL("/cacheable.txt"); | 669 String url = NativeTestServer.getFileURL("/cacheable.txt"); |
| 679 checkRequestCaching(url, false); | 670 checkRequestCaching(url, false); |
| 680 checkRequestCaching(url, true); | 671 checkRequestCaching(url, true); |
| 681 NativeTestServer.shutdownNativeTestServer(); | 672 NativeTestServer.shutdownNativeTestServer(); |
| 682 checkRequestCaching(url, true); | 673 checkRequestCaching(url, true); |
| 683 | 674 |
| 684 // Shutdown original context and create another that uses the same cache
. | 675 // Shutdown original context and create another that uses the same cache
. |
| 685 mActivity.mCronetEngine.shutdown(); | 676 mTestFramework.mCronetEngine.shutdown(); |
| 686 mActivity.mCronetEngine = mActivity.getCronetEngineBuilder().build(); | 677 mTestFramework.mCronetEngine = mTestFramework.getCronetEngineBuilder().b
uild(); |
| 687 checkRequestCaching(url, true); | 678 checkRequestCaching(url, true); |
| 688 } | 679 } |
| 689 | 680 |
| 690 @SmallTest | 681 @SmallTest |
| 691 @Feature({"Cronet"}) | 682 @Feature({"Cronet"}) |
| 692 public void testInitEngineAndStartRequest() { | 683 public void testInitEngineAndStartRequest() { |
| 693 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); | 684 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); |
| 694 | 685 |
| 695 // Immediately make a request after initializing the engine. | 686 // Immediately make a request after initializing the engine. |
| 696 CronetEngine cronetEngine = activity.initCronetEngine(); | 687 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 697 TestUrlRequestListener listener = new TestUrlRequestListener(); | 688 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 698 UrlRequest.Builder urlRequestBuilder = | 689 UrlRequest.Builder urlRequestBuilder = |
| 699 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); | 690 new UrlRequest.Builder(TEST_URL, listener, listener.getExecutor(
), cronetEngine); |
| 700 urlRequestBuilder.build().start(); | 691 urlRequestBuilder.build().start(); |
| 701 listener.blockForDone(); | 692 listener.blockForDone(); |
| 702 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 693 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
| 703 } | 694 } |
| 704 | 695 |
| 705 @SmallTest | 696 @SmallTest |
| 706 @Feature({"Cronet"}) | 697 @Feature({"Cronet"}) |
| 707 public void testInitEngineStartTwoRequests() throws Exception { | 698 public void testInitEngineStartTwoRequests() throws Exception { |
| 708 CronetTestActivity activity = launchCronetTestAppAndSkipFactoryInit(); | 699 CronetTestFramework testFramework = startCronetTestFrameworkAndSkipFacto
ryInit(); |
| 709 | 700 |
| 710 // Make two requests after initializing the context. | 701 // Make two requests after initializing the context. |
| 711 CronetEngine cronetEngine = activity.initCronetEngine(); | 702 CronetEngine cronetEngine = testFramework.initCronetEngine(); |
| 712 int[] statusCodes = {0, 0}; | 703 int[] statusCodes = {0, 0}; |
| 713 String[] urls = {TEST_URL, URL_404}; | 704 String[] urls = {TEST_URL, URL_404}; |
| 714 for (int i = 0; i < 2; i++) { | 705 for (int i = 0; i < 2; i++) { |
| 715 TestUrlRequestListener listener = new TestUrlRequestListener(); | 706 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 716 UrlRequest.Builder urlRequestBuilder = | 707 UrlRequest.Builder urlRequestBuilder = |
| 717 new UrlRequest.Builder(urls[i], listener, listener.getExecut
or(), cronetEngine); | 708 new UrlRequest.Builder(urls[i], listener, listener.getExecut
or(), cronetEngine); |
| 718 urlRequestBuilder.build().start(); | 709 urlRequestBuilder.build().start(); |
| 719 listener.blockForDone(); | 710 listener.blockForDone(); |
| 720 statusCodes[i] = listener.mResponseInfo.getHttpStatusCode(); | 711 statusCodes[i] = listener.mResponseInfo.getHttpStatusCode(); |
| 721 } | 712 } |
| 722 assertEquals(200, statusCodes[0]); | 713 assertEquals(200, statusCodes[0]); |
| 723 assertEquals(404, statusCodes[1]); | 714 assertEquals(404, statusCodes[1]); |
| 724 } | 715 } |
| 725 | 716 |
| 726 @SmallTest | 717 @SmallTest |
| 727 @Feature({"Cronet"}) | 718 @Feature({"Cronet"}) |
| 728 public void testInitTwoEnginesSimultaneously() throws Exception { | 719 public void testInitTwoEnginesSimultaneously() throws Exception { |
| 729 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 720 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 730 | 721 |
| 731 // Threads will block on runBlocker to ensure simultaneous execution. | 722 // Threads will block on runBlocker to ensure simultaneous execution. |
| 732 ConditionVariable runBlocker = new ConditionVariable(false); | 723 ConditionVariable runBlocker = new ConditionVariable(false); |
| 733 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker
); | 724 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); |
| 734 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker)
; | 725 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); |
| 735 | 726 |
| 736 thread1.start(); | 727 thread1.start(); |
| 737 thread2.start(); | 728 thread2.start(); |
| 738 runBlocker.open(); | 729 runBlocker.open(); |
| 739 thread1.join(); | 730 thread1.join(); |
| 740 thread2.join(); | 731 thread2.join(); |
| 741 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); | 732 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); |
| 742 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); | 733 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); |
| 743 } | 734 } |
| 744 | 735 |
| 745 @SmallTest | 736 @SmallTest |
| 746 @Feature({"Cronet"}) | 737 @Feature({"Cronet"}) |
| 747 public void testInitTwoEnginesInSequence() throws Exception { | 738 public void testInitTwoEnginesInSequence() throws Exception { |
| 748 final CronetTestActivity activity = launchCronetTestAppAndSkipFactoryIni
t(); | 739 final CronetTestFramework testFramework = startCronetTestFrameworkAndSki
pFactoryInit(); |
| 749 | 740 |
| 750 ConditionVariable runBlocker = new ConditionVariable(true); | 741 ConditionVariable runBlocker = new ConditionVariable(true); |
| 751 RequestThread thread1 = new RequestThread(activity, TEST_URL, runBlocker
); | 742 RequestThread thread1 = new RequestThread(testFramework, TEST_URL, runBl
ocker); |
| 752 RequestThread thread2 = new RequestThread(activity, URL_404, runBlocker)
; | 743 RequestThread thread2 = new RequestThread(testFramework, URL_404, runBlo
cker); |
| 753 | 744 |
| 754 thread1.start(); | 745 thread1.start(); |
| 755 thread1.join(); | 746 thread1.join(); |
| 756 thread2.start(); | 747 thread2.start(); |
| 757 thread2.join(); | 748 thread2.join(); |
| 758 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); | 749 assertEquals(200, thread1.mListener.mResponseInfo.getHttpStatusCode()); |
| 759 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); | 750 assertEquals(404, thread2.mListener.mResponseInfo.getHttpStatusCode()); |
| 760 } | 751 } |
| 761 | 752 |
| 762 @SmallTest | 753 @SmallTest |
| 763 @Feature({"Cronet"}) | 754 @Feature({"Cronet"}) |
| 764 public void testInitDifferentEngines() throws Exception { | 755 public void testInitDifferentEngines() throws Exception { |
| 765 // Test that concurrently instantiating Cronet context's upon various | 756 // Test that concurrently instantiating Cronet context's upon various |
| 766 // different versions of the same Android Context does not cause crashes | 757 // different versions of the same Android Context does not cause crashes |
| 767 // like crbug.com/453845 | 758 // like crbug.com/453845 |
| 768 mActivity = launchCronetTestApp(); | 759 mTestFramework = startCronetTestFramework(); |
| 769 CronetEngine firstEngine = | 760 CronetEngine firstEngine = |
| 770 new CronetUrlRequestContext(mActivity.createCronetEngineBuilder(
mActivity)); | 761 new CronetUrlRequestContext(mTestFramework.createCronetEngineBui
lder(getContext())); |
| 771 CronetEngine secondEngine = new CronetUrlRequestContext( | 762 CronetEngine secondEngine = new CronetUrlRequestContext( |
| 772 mActivity.createCronetEngineBuilder(mActivity.getApplicationCont
ext())); | 763 mTestFramework.createCronetEngineBuilder(getContext().getApplica
tionContext())); |
| 773 CronetEngine thirdEngine = new CronetUrlRequestContext( | 764 CronetEngine thirdEngine = new CronetUrlRequestContext( |
| 774 mActivity.createCronetEngineBuilder(new ContextWrapper(mActivity
))); | 765 mTestFramework.createCronetEngineBuilder(new ContextWrapper(getC
ontext()))); |
| 775 firstEngine.shutdown(); | 766 firstEngine.shutdown(); |
| 776 secondEngine.shutdown(); | 767 secondEngine.shutdown(); |
| 777 thirdEngine.shutdown(); | 768 thirdEngine.shutdown(); |
| 778 } | 769 } |
| 779 } | 770 } |
| OLD | NEW |