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; |
11 import android.os.Looper; | 11 import android.os.Looper; |
12 import android.test.suitebuilder.annotation.SmallTest; | 12 import android.test.suitebuilder.annotation.SmallTest; |
13 | 13 |
14 import org.chromium.base.PathUtils; | 14 import org.chromium.base.PathUtils; |
15 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
16 import org.chromium.net.TestUrlRequestListener.ResponseStep; | 16 import org.chromium.net.TestUrlRequestListener.ResponseStep; |
17 | 17 |
18 import java.io.BufferedReader; | 18 import java.io.BufferedReader; |
19 import java.io.File; | 19 import java.io.File; |
20 import java.io.FileReader; | 20 import java.io.FileReader; |
| 21 import java.util.LinkedList; |
| 22 import java.util.NoSuchElementException; |
| 23 import java.util.concurrent.Executor; |
21 | 24 |
22 /** | 25 /** |
23 * Test CronetUrlRequestContext. | 26 * Test CronetUrlRequestContext. |
24 */ | 27 */ |
25 public class CronetUrlRequestContextTest extends CronetTestBase { | 28 public class CronetUrlRequestContextTest extends CronetTestBase { |
26 // URLs used for tests. | 29 // URLs used for tests. |
27 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"; |
28 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"; |
29 private static final String MOCK_CRONET_TEST_FAILED_URL = | 32 private static final String MOCK_CRONET_TEST_FAILED_URL = |
30 "http://mock.failed.request/-2"; | 33 "http://mock.failed.request/-2"; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 75 |
73 @Override | 76 @Override |
74 public void onFailed(UrlRequest request, | 77 public void onFailed(UrlRequest request, |
75 ResponseInfo info, | 78 ResponseInfo info, |
76 UrlRequestException error) { | 79 UrlRequestException error) { |
77 super.onFailed(request, info, error); | 80 super.onFailed(request, info, error); |
78 mActivity.mUrlRequestContext.shutdown(); | 81 mActivity.mUrlRequestContext.shutdown(); |
79 } | 82 } |
80 } | 83 } |
81 | 84 |
| 85 static class TestExecutor implements Executor { |
| 86 private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>
(); |
| 87 |
| 88 @Override |
| 89 public void execute(Runnable task) { |
| 90 mTaskQueue.add(task); |
| 91 } |
| 92 |
| 93 public void runAllTasks() { |
| 94 try { |
| 95 while (mTaskQueue.size() > 0) { |
| 96 mTaskQueue.remove().run(); |
| 97 } |
| 98 } catch (NoSuchElementException e) { |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 static class TestNetworkQualityListener |
| 104 implements NetworkQualityRttListener, NetworkQualityThroughputListen
er { |
| 105 int mRttObservationCount; |
| 106 int mThroughputObservationCount; |
| 107 |
| 108 public void onRttObservation(int rttMs, long when, int source) { |
| 109 mRttObservationCount++; |
| 110 } |
| 111 |
| 112 public void onThroughputObservation(int throughputKbps, long when, int s
ource) { |
| 113 mThroughputObservationCount++; |
| 114 } |
| 115 |
| 116 public int rttObservationCount() { |
| 117 return mRttObservationCount; |
| 118 } |
| 119 |
| 120 public int throughputObservationCount() { |
| 121 return mThroughputObservationCount; |
| 122 } |
| 123 } |
| 124 |
82 @SmallTest | 125 @SmallTest |
83 @Feature({"Cronet"}) | 126 @Feature({"Cronet"}) |
84 public void testConfigUserAgent() throws Exception { | 127 public void testConfigUserAgent() throws Exception { |
85 String userAgentName = "User-Agent"; | 128 String userAgentName = "User-Agent"; |
86 String userAgentValue = "User-Agent-Value"; | 129 String userAgentValue = "User-Agent-Value"; |
87 UrlRequestContextConfig config = new UrlRequestContextConfig(); | 130 UrlRequestContextConfig config = new UrlRequestContextConfig(); |
88 config.setUserAgent(userAgentValue); | 131 config.setUserAgent(userAgentValue); |
89 config.setLibraryName("cronet_tests"); | 132 config.setLibraryName("cronet_tests"); |
90 String[] commandLineArgs = { | 133 String[] commandLineArgs = { |
91 CronetTestActivity.CONFIG_KEY, config.toString() | 134 CronetTestActivity.CONFIG_KEY, config.toString() |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // Proxy logic configured to use the test server as its proxy. | 190 // Proxy logic configured to use the test server as its proxy. |
148 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); | 191 assertEquals(200, listener.mResponseInfo.getHttpStatusCode()); |
149 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); | 192 assertEquals(serverHostPort, listener.mResponseInfo.getProxyServer()); |
150 assertEquals( | 193 assertEquals( |
151 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", | 194 "http://DomainThatDoesnt.Resolve/datareductionproxysuccess.txt", |
152 listener.mResponseInfo.getUrl()); | 195 listener.mResponseInfo.getUrl()); |
153 } | 196 } |
154 | 197 |
155 @SmallTest | 198 @SmallTest |
156 @Feature({"Cronet"}) | 199 @Feature({"Cronet"}) |
| 200 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except
ion { |
| 201 mActivity = launchCronetTestApp(); |
| 202 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 203 try { |
| 204 mActivity.mUrlRequestContext.addRttListener(networkQualityListener); |
| 205 fail("Should throw an exception."); |
| 206 } catch (IllegalStateException e) { |
| 207 } |
| 208 try { |
| 209 mActivity.mUrlRequestContext.addThroughputListener(networkQualityLis
tener); |
| 210 fail("Should throw an exception."); |
| 211 } catch (IllegalStateException e) { |
| 212 } |
| 213 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 214 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 215 TEST_URL, listener, listener.getExecutor()); |
| 216 urlRequest.start(); |
| 217 listener.blockForDone(); |
| 218 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 219 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 220 mActivity.mUrlRequestContext.shutdown(); |
| 221 } |
| 222 |
| 223 @SmallTest |
| 224 @Feature({"Cronet"}) |
| 225 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E
xception { |
| 226 mActivity = launchCronetTestApp(); |
| 227 TestExecutor testExecutor = new TestExecutor(); |
| 228 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 229 mActivity.mUrlRequestContext.enableNetworkQualityEstimatorForTesting( |
| 230 true, true, testExecutor); |
| 231 mActivity.mUrlRequestContext.addRttListener(networkQualityListener); |
| 232 mActivity.mUrlRequestContext.addThroughputListener(networkQualityListene
r); |
| 233 mActivity.mUrlRequestContext.removeRttListener(networkQualityListener); |
| 234 mActivity.mUrlRequestContext.removeThroughputListener(networkQualityList
ener); |
| 235 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 236 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 237 TEST_URL, listener, listener.getExecutor()); |
| 238 urlRequest.start(); |
| 239 listener.blockForDone(); |
| 240 testExecutor.runAllTasks(); |
| 241 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 242 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 243 mActivity.mUrlRequestContext.shutdown(); |
| 244 } |
| 245 |
| 246 @SmallTest |
| 247 @Feature({"Cronet"}) |
| 248 public void testRealTimeNetworkQualityObservations() throws Exception { |
| 249 mActivity = launchCronetTestApp(); |
| 250 TestExecutor testExecutor = new TestExecutor(); |
| 251 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali
tyListener(); |
| 252 mActivity.mUrlRequestContext.enableNetworkQualityEstimatorForTesting( |
| 253 true, true, testExecutor); |
| 254 mActivity.mUrlRequestContext.addRttListener(networkQualityListener); |
| 255 mActivity.mUrlRequestContext.addThroughputListener(networkQualityListene
r); |
| 256 TestUrlRequestListener listener = new TestUrlRequestListener(); |
| 257 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
| 258 TEST_URL, listener, listener.getExecutor()); |
| 259 urlRequest.start(); |
| 260 listener.blockForDone(); |
| 261 testExecutor.runAllTasks(); |
| 262 assertTrue(networkQualityListener.rttObservationCount() > 0); |
| 263 assertTrue(networkQualityListener.throughputObservationCount() > 0); |
| 264 mActivity.mUrlRequestContext.shutdown(); |
| 265 } |
| 266 |
| 267 @SmallTest |
| 268 @Feature({"Cronet"}) |
157 public void testShutdown() throws Exception { | 269 public void testShutdown() throws Exception { |
158 mActivity = launchCronetTestApp(); | 270 mActivity = launchCronetTestApp(); |
159 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); | 271 TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |
160 // Block listener when response starts to verify that shutdown fails | 272 // Block listener when response starts to verify that shutdown fails |
161 // if there are active requests. | 273 // if there are active requests. |
162 listener.setAutoAdvance(false); | 274 listener.setAutoAdvance(false); |
163 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( | 275 UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
164 TEST_URL, listener, listener.getExecutor()); | 276 TEST_URL, listener, listener.getExecutor()); |
165 urlRequest.start(); | 277 urlRequest.start(); |
166 try { | 278 try { |
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi
g()); | 780 new CronetUrlRequestContext(mActivity, mActivity.getContextConfi
g()); |
669 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( | 781 CronetUrlRequestContext secondContext = new CronetUrlRequestContext( |
670 mActivity.getApplicationContext(), mActivity.getContextConfig())
; | 782 mActivity.getApplicationContext(), mActivity.getContextConfig())
; |
671 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( | 783 CronetUrlRequestContext thirdContext = new CronetUrlRequestContext( |
672 new ContextWrapper(mActivity), mActivity.getContextConfig()); | 784 new ContextWrapper(mActivity), mActivity.getContextConfig()); |
673 firstContext.shutdown(); | 785 firstContext.shutdown(); |
674 secondContext.shutdown(); | 786 secondContext.shutdown(); |
675 thirdContext.shutdown(); | 787 thirdContext.shutdown(); |
676 } | 788 } |
677 } | 789 } |
OLD | NEW |