Chromium Code Reviews| 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 static org.chromium.base.CollectionUtil.newHashSet; | 7 import static org.chromium.base.CollectionUtil.newHashSet; |
| 8 | 8 |
| 9 import android.content.Context; | 9 import android.content.Context; |
| 10 import android.content.ContextWrapper; | 10 import android.content.ContextWrapper; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 import org.chromium.net.test.EmbeddedTestServer; | 22 import org.chromium.net.test.EmbeddedTestServer; |
| 23 | 23 |
| 24 import java.io.BufferedReader; | 24 import java.io.BufferedReader; |
| 25 import java.io.File; | 25 import java.io.File; |
| 26 import java.io.FileReader; | 26 import java.io.FileReader; |
| 27 import java.util.Arrays; | 27 import java.util.Arrays; |
| 28 import java.util.HashSet; | 28 import java.util.HashSet; |
| 29 import java.util.LinkedList; | 29 import java.util.LinkedList; |
| 30 import java.util.NoSuchElementException; | 30 import java.util.NoSuchElementException; |
| 31 import java.util.concurrent.Executor; | 31 import java.util.concurrent.Executor; |
| 32 import java.util.concurrent.Executors; | |
| 32 | 33 |
| 33 /** | 34 /** |
| 34 * Test CronetEngine. | 35 * Test CronetEngine. |
| 35 */ | 36 */ |
| 36 @JNINamespace("cronet") | 37 @JNINamespace("cronet") |
| 37 public class CronetUrlRequestContextTest extends CronetTestBase { | 38 public class CronetUrlRequestContextTest extends CronetTestBase { |
| 38 // URLs used for tests. | 39 // URLs used for tests. |
| 39 private static final String MOCK_CRONET_TEST_FAILED_URL = | 40 private static final String MOCK_CRONET_TEST_FAILED_URL = |
| 40 "http://mock.failed.request/-2"; | 41 "http://mock.failed.request/-2"; |
| 41 private static final String MOCK_CRONET_TEST_SUCCESS_URL = | 42 private static final String MOCK_CRONET_TEST_SUCCESS_URL = |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 } | 122 } |
| 122 } catch (NoSuchElementException e) { | 123 } catch (NoSuchElementException e) { |
| 123 } | 124 } |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 |
| 127 static class TestNetworkQualityListener | 128 static class TestNetworkQualityListener |
| 128 implements NetworkQualityRttListener, NetworkQualityThroughputListen er { | 129 implements NetworkQualityRttListener, NetworkQualityThroughputListen er { |
| 129 int mRttObservationCount; | 130 int mRttObservationCount; |
| 130 int mThroughputObservationCount; | 131 int mThroughputObservationCount; |
| 132 ConditionVariable mWaitForThroughput = null; | |
|
xunjieli
2016/05/26 17:55:30
nit: maybe get rid of "=null", and add "final" key
tbansal1
2016/05/26 18:02:59
Done.
| |
| 133 | |
| 134 TestNetworkQualityListener(ConditionVariable waitForThroughput) { | |
| 135 mWaitForThroughput = waitForThroughput; | |
| 136 } | |
| 131 | 137 |
| 132 @Override | 138 @Override |
| 133 public void onRttObservation(int rttMs, long when, int source) { | 139 public void onRttObservation(int rttMs, long when, int source) { |
| 134 mRttObservationCount++; | 140 mRttObservationCount++; |
| 135 } | 141 } |
| 136 | 142 |
| 137 @Override | 143 @Override |
| 138 public void onThroughputObservation(int throughputKbps, long when, int s ource) { | 144 public void onThroughputObservation(int throughputKbps, long when, int s ource) { |
| 145 if (mWaitForThroughput != null) { | |
| 146 mWaitForThroughput.open(); | |
| 147 } | |
| 139 mThroughputObservationCount++; | 148 mThroughputObservationCount++; |
| 140 } | 149 } |
| 141 | 150 |
| 142 public int rttObservationCount() { | 151 public int rttObservationCount() { |
| 143 return mRttObservationCount; | 152 return mRttObservationCount; |
| 144 } | 153 } |
| 145 | 154 |
| 146 public int throughputObservationCount() { | 155 public int throughputObservationCount() { |
| 147 return mThroughputObservationCount; | 156 return mThroughputObservationCount; |
| 148 } | 157 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 // Verify that the request is successful and that the Data Reduction | 222 // Verify that the request is successful and that the Data Reduction |
| 214 // Proxy logic configured to use the test server as its proxy. | 223 // Proxy logic configured to use the test server as its proxy. |
| 215 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); | 224 assertEquals(200, callback.mResponseInfo.getHttpStatusCode()); |
| 216 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); | 225 assertEquals(serverHostPort, callback.mResponseInfo.getProxyServer()); |
| 217 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt", | 226 assertEquals("http://DomainThatDoesnt.Resolve/datareductionproxysuccess. txt", |
| 218 callback.mResponseInfo.getUrl()); | 227 callback.mResponseInfo.getUrl()); |
| 219 } | 228 } |
| 220 | 229 |
| 221 @SmallTest | 230 @SmallTest |
| 222 @Feature({"Cronet"}) | 231 @Feature({"Cronet"}) |
| 223 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed. | |
| 224 @SuppressWarnings("deprecation") | |
| 225 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion { | 232 public void testRealTimeNetworkQualityObservationsNotEnabled() throws Except ion { |
| 226 mTestFramework = startCronetTestFramework(); | 233 mTestFramework = startCronetTestFramework(); |
| 227 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); | 234 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(null); |
| 228 try { | 235 try { |
| 229 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); | 236 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 230 fail("Should throw an exception."); | 237 fail("Should throw an exception."); |
| 231 } catch (IllegalStateException e) { | 238 } catch (IllegalStateException e) { |
| 232 } | 239 } |
| 233 try { | 240 try { |
| 234 mTestFramework.mCronetEngine.addThroughputListener(networkQualityLis tener); | 241 mTestFramework.mCronetEngine.addThroughputListener(networkQualityLis tener); |
| 235 fail("Should throw an exception."); | 242 fail("Should throw an exception."); |
| 236 } catch (IllegalStateException e) { | 243 } catch (IllegalStateException e) { |
| 237 } | 244 } |
| 238 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 245 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 239 UrlRequest urlRequest = | 246 UrlRequest urlRequest = |
| 240 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); | 247 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); |
| 241 urlRequest.start(); | 248 urlRequest.start(); |
| 242 callback.blockForDone(); | 249 callback.blockForDone(); |
| 243 assertEquals(0, networkQualityListener.rttObservationCount()); | 250 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 244 assertEquals(0, networkQualityListener.throughputObservationCount()); | 251 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 245 mTestFramework.mCronetEngine.shutdown(); | 252 mTestFramework.mCronetEngine.shutdown(); |
| 246 } | 253 } |
| 247 | 254 |
| 248 @SmallTest | 255 @SmallTest |
| 249 @Feature({"Cronet"}) | 256 @Feature({"Cronet"}) |
| 250 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed. | |
| 251 @SuppressWarnings("deprecation") | |
| 252 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception { | 257 public void testRealTimeNetworkQualityObservationsListenerRemoved() throws E xception { |
| 253 mTestFramework = startCronetTestFramework(); | 258 mTestFramework = startCronetTestFramework(); |
| 254 TestExecutor testExecutor = new TestExecutor(); | 259 TestExecutor testExecutor = new TestExecutor(); |
| 255 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); | 260 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(null); |
| 256 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( | 261 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( |
| 257 true, true, testExecutor); | 262 true, true, testExecutor); |
| 258 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); | 263 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 259 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); | 264 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); |
| 260 mTestFramework.mCronetEngine.removeRttListener(networkQualityListener); | 265 mTestFramework.mCronetEngine.removeRttListener(networkQualityListener); |
| 261 mTestFramework.mCronetEngine.removeThroughputListener(networkQualityList ener); | 266 mTestFramework.mCronetEngine.removeThroughputListener(networkQualityList ener); |
| 262 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 267 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 263 UrlRequest urlRequest = | 268 UrlRequest urlRequest = |
| 264 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); | 269 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); |
| 265 urlRequest.start(); | 270 urlRequest.start(); |
| 266 callback.blockForDone(); | 271 callback.blockForDone(); |
| 267 testExecutor.runAllTasks(); | 272 testExecutor.runAllTasks(); |
| 268 assertEquals(0, networkQualityListener.rttObservationCount()); | 273 assertEquals(0, networkQualityListener.rttObservationCount()); |
| 269 assertEquals(0, networkQualityListener.throughputObservationCount()); | 274 assertEquals(0, networkQualityListener.throughputObservationCount()); |
| 270 mTestFramework.mCronetEngine.shutdown(); | 275 mTestFramework.mCronetEngine.shutdown(); |
| 271 } | 276 } |
| 272 | 277 |
| 273 /* | |
| 274 @SmallTest | 278 @SmallTest |
| 275 @Feature({"Cronet"}) | 279 @Feature({"Cronet"}) |
| 276 // TODO(xunjieli): Remove annotation after crbug.com/539519 is fixed. | |
| 277 @SuppressWarnings("deprecation") | |
| 278 */ | |
| 279 @FlakyTest(message = "http://crbug.com/614227") | |
| 280 public void testRealTimeNetworkQualityObservations() throws Exception { | 280 public void testRealTimeNetworkQualityObservations() throws Exception { |
| 281 mTestFramework = startCronetTestFramework(); | 281 mTestFramework = startCronetTestFramework(); |
| 282 TestExecutor testExecutor = new TestExecutor(); | 282 Executor testExecutor = Executors.newSingleThreadExecutor(); |
| 283 TestNetworkQualityListener networkQualityListener = new TestNetworkQuali tyListener(); | 283 ConditionVariable waitForThroughput = new ConditionVariable(); |
| 284 TestNetworkQualityListener networkQualityListener = | |
| 285 new TestNetworkQualityListener(waitForThroughput); | |
| 284 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( | 286 mTestFramework.mCronetEngine.enableNetworkQualityEstimatorForTesting( |
| 285 true, true, testExecutor); | 287 true, true, testExecutor); |
| 286 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); | 288 mTestFramework.mCronetEngine.addRttListener(networkQualityListener); |
| 287 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); | 289 mTestFramework.mCronetEngine.addThroughputListener(networkQualityListene r); |
| 288 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 290 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 289 UrlRequest urlRequest = | 291 UrlRequest urlRequest = |
| 290 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); | 292 mTestFramework.mCronetEngine.createRequest(mUrl, callback, callb ack.getExecutor()); |
| 291 urlRequest.start(); | 293 urlRequest.start(); |
| 292 callback.blockForDone(); | 294 callback.blockForDone(); |
| 293 testExecutor.runAllTasks(); | 295 |
| 296 // Throughput observation is posted to the network quality estimator and its observers | |
| 297 // after the UrlRequest is completed. | |
| 298 waitForThroughput.block(); | |
|
xunjieli
2016/05/26 17:55:30
Is throughtput observation always going to be post
tbansal1
2016/05/26 18:02:59
Yes.
xunjieli
2016/05/26 18:06:49
Please add a comment above in "public void onThrou
tbansal1
2016/05/26 18:21:40
I expanded on the comment here, instead of in onTh
| |
| 294 assertTrue(networkQualityListener.rttObservationCount() > 0); | 299 assertTrue(networkQualityListener.rttObservationCount() > 0); |
| 295 assertTrue(networkQualityListener.throughputObservationCount() > 0); | 300 assertTrue(networkQualityListener.throughputObservationCount() > 0); |
| 296 mTestFramework.mCronetEngine.shutdown(); | 301 mTestFramework.mCronetEngine.shutdown(); |
| 297 } | 302 } |
| 298 | 303 |
| 299 private static class TestRequestFinishedListener | 304 private static class TestRequestFinishedListener |
| 300 implements CronetEngine.RequestFinishedListener { | 305 implements CronetEngine.RequestFinishedListener { |
| 301 private UrlRequestInfo mRequestInfo = null; | 306 private UrlRequestInfo mRequestInfo = null; |
| 302 | 307 |
| 303 @Override | 308 @Override |
| (...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1149 } | 1154 } |
| 1150 }.start(); | 1155 }.start(); |
| 1151 otherThreadDone.block(); | 1156 otherThreadDone.block(); |
| 1152 builder.build().shutdown(); | 1157 builder.build().shutdown(); |
| 1153 uiThreadDone.open(); | 1158 uiThreadDone.open(); |
| 1154 } | 1159 } |
| 1155 }); | 1160 }); |
| 1156 assertTrue(uiThreadDone.block(1000)); | 1161 assertTrue(uiThreadDone.block(1000)); |
| 1157 } | 1162 } |
| 1158 } | 1163 } |
| OLD | NEW |