Index: components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java |
diff --git a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java |
index d9cc5578e7169c741dd1b7ec1095a251996df6bc..1a6d4ea0eb42547f6c8924004617207060f90770 100644 |
--- a/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java |
+++ b/components/cronet/android/test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java |
@@ -18,6 +18,9 @@ import org.chromium.net.TestUrlRequestListener.ResponseStep; |
import java.io.BufferedReader; |
import java.io.File; |
import java.io.FileReader; |
+import java.util.LinkedList; |
+import java.util.NoSuchElementException; |
+import java.util.concurrent.Executor; |
/** |
* Test CronetUrlRequestContext. |
@@ -79,6 +82,46 @@ public class CronetUrlRequestContextTest extends CronetTestBase { |
} |
} |
+ static class TestExecutor implements Executor { |
+ private final LinkedList<Runnable> mTaskQueue = new LinkedList<Runnable>(); |
+ |
+ @Override |
+ public void execute(Runnable task) { |
+ mTaskQueue.add(task); |
+ } |
+ |
+ public void runAllTasks() { |
+ try { |
+ while (mTaskQueue.size() > 0) { |
+ mTaskQueue.remove().run(); |
+ } |
+ } catch (NoSuchElementException e) { |
+ } |
+ } |
+ } |
+ |
+ static class TestNetworkQualityObserver |
+ implements NetworkQualityRTTObserver, NetworkQualityThroughputObserver { |
+ int mRTTObservationCount; |
+ int mThroughputObservationCount; |
+ |
+ public void onRTTObservation(int rttMs, long when, int source) { |
+ mRTTObservationCount++; |
+ } |
+ |
+ public void onThroughputObservation(int throughputKbps, long when, int source) { |
+ mThroughputObservationCount++; |
+ } |
+ |
+ public int rttObservationCount() { |
+ return mRTTObservationCount; |
+ } |
+ |
+ public int throughputObservationCount() { |
+ return mThroughputObservationCount; |
+ } |
+ } |
+ |
@SmallTest |
@Feature({"Cronet"}) |
public void testConfigUserAgent() throws Exception { |
@@ -154,6 +197,65 @@ public class CronetUrlRequestContextTest extends CronetTestBase { |
@SmallTest |
@Feature({"Cronet"}) |
+ public void testRealTimeNetworkQualityObservationsNotEnabled() throws Exception { |
+ mActivity = launchCronetTestApp(); |
+ TestNetworkQualityObserver networkQualityObserver = new TestNetworkQualityObserver(); |
+ mActivity.mUrlRequestContext.addRTTObserver(networkQualityObserver); |
+ mActivity.mUrlRequestContext.addThroughputObserver(networkQualityObserver); |
+ TestUrlRequestListener listener = new TestUrlRequestListener(); |
+ UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
+ TEST_URL, listener, listener.getExecutor()); |
+ urlRequest.start(); |
+ listener.blockForDone(); |
+ assertEquals(0, networkQualityObserver.rttObservationCount()); |
+ assertEquals(0, networkQualityObserver.throughputObservationCount()); |
+ mActivity.mUrlRequestContext.shutdown(); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
+ public void testRealTimeNetworkQualityObservationsObserverRemoved() throws Exception { |
+ mActivity = launchCronetTestApp(); |
+ TestExecutor testExecutor = new TestExecutor(); |
+ TestNetworkQualityObserver networkQualityObserver = new TestNetworkQualityObserver(); |
+ mActivity.mUrlRequestContext.enableNetworkQualityEstimator(true, true, testExecutor); |
+ mActivity.mUrlRequestContext.addRTTObserver(networkQualityObserver); |
+ mActivity.mUrlRequestContext.addThroughputObserver(networkQualityObserver); |
+ mActivity.mUrlRequestContext.removeRTTObserver(networkQualityObserver); |
+ mActivity.mUrlRequestContext.removeThroughputObserver(networkQualityObserver); |
+ TestUrlRequestListener listener = new TestUrlRequestListener(); |
+ UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
+ TEST_URL, listener, listener.getExecutor()); |
+ urlRequest.start(); |
+ listener.blockForDone(); |
+ testExecutor.runAllTasks(); |
+ assertEquals(0, networkQualityObserver.rttObservationCount()); |
+ assertEquals(0, networkQualityObserver.throughputObservationCount()); |
+ mActivity.mUrlRequestContext.shutdown(); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
+ public void testRealTimeNetworkQualityObservations() throws Exception { |
+ mActivity = launchCronetTestApp(); |
+ TestExecutor testExecutor = new TestExecutor(); |
+ TestNetworkQualityObserver networkQualityObserver = new TestNetworkQualityObserver(); |
+ mActivity.mUrlRequestContext.enableNetworkQualityEstimator(true, true, testExecutor); |
+ mActivity.mUrlRequestContext.addRTTObserver(networkQualityObserver); |
+ mActivity.mUrlRequestContext.addThroughputObserver(networkQualityObserver); |
+ TestUrlRequestListener listener = new TestUrlRequestListener(); |
+ UrlRequest urlRequest = mActivity.mUrlRequestContext.createRequest( |
+ TEST_URL, listener, listener.getExecutor()); |
+ urlRequest.start(); |
+ listener.blockForDone(); |
+ testExecutor.runAllTasks(); |
+ assertTrue(networkQualityObserver.rttObservationCount() > 0); |
+ assertTrue(networkQualityObserver.throughputObservationCount() > 0); |
+ mActivity.mUrlRequestContext.shutdown(); |
+ } |
+ |
+ @SmallTest |
+ @Feature({"Cronet"}) |
public void testShutdown() throws Exception { |
mActivity = launchCronetTestApp(); |
TestUrlRequestListener listener = new ShutdownTestUrlRequestListener(); |