| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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.os.StrictMode; | 7 import android.os.StrictMode; |
| 8 import android.support.test.filters.SmallTest; | 8 import android.support.test.filters.SmallTest; |
| 9 | 9 |
| 10 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 }); | 64 }); |
| 65 return mNetworkQualityThread; | 65 return mNetworkQualityThread; |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 @SmallTest | 69 @SmallTest |
| 70 @Feature({"Cronet"}) | 70 @Feature({"Cronet"}) |
| 71 public void testNotEnabled() throws Exception { | 71 public void testNotEnabled() throws Exception { |
| 72 ExperimentalCronetEngine.Builder mCronetEngineBuilder = | 72 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 73 new ExperimentalCronetEngine.Builder(getContext()); | 73 new ExperimentalCronetEngine.Builder(getContext()); |
| 74 final CronetTestFramework testFramework = | 74 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build(
); |
| 75 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | |
| 76 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); | 75 Executor networkQualityExecutor = Executors.newSingleThreadExecutor(); |
| 77 TestNetworkQualityRttListener rttListener = | 76 TestNetworkQualityRttListener rttListener = |
| 78 new TestNetworkQualityRttListener(networkQualityExecutor); | 77 new TestNetworkQualityRttListener(networkQualityExecutor); |
| 79 TestNetworkQualityThroughputListener throughputListener = | 78 TestNetworkQualityThroughputListener throughputListener = |
| 80 new TestNetworkQualityThroughputListener(networkQualityExecutor)
; | 79 new TestNetworkQualityThroughputListener(networkQualityExecutor)
; |
| 81 try { | 80 try { |
| 82 testFramework.mCronetEngine.addRttListener(rttListener); | 81 cronetEngine.addRttListener(rttListener); |
| 83 fail("Should throw an exception."); | 82 fail("Should throw an exception."); |
| 84 } catch (IllegalStateException e) { | 83 } catch (IllegalStateException e) { |
| 85 } | 84 } |
| 86 try { | 85 try { |
| 87 testFramework.mCronetEngine.addThroughputListener(throughputListener
); | 86 cronetEngine.addThroughputListener(throughputListener); |
| 88 fail("Should throw an exception."); | 87 fail("Should throw an exception."); |
| 89 } catch (IllegalStateException e) { | 88 } catch (IllegalStateException e) { |
| 90 } | 89 } |
| 91 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 90 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 92 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( | 91 UrlRequest.Builder builder = |
| 93 mUrl, callback, callback.getExecutor()); | 92 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 94 UrlRequest urlRequest = builder.build(); | 93 UrlRequest urlRequest = builder.build(); |
| 95 | 94 |
| 96 urlRequest.start(); | 95 urlRequest.start(); |
| 97 callback.blockForDone(); | 96 callback.blockForDone(); |
| 98 assertEquals(0, rttListener.rttObservationCount()); | 97 assertEquals(0, rttListener.rttObservationCount()); |
| 99 assertEquals(0, throughputListener.throughputObservationCount()); | 98 assertEquals(0, throughputListener.throughputObservationCount()); |
| 100 testFramework.mCronetEngine.shutdown(); | 99 cronetEngine.shutdown(); |
| 101 } | 100 } |
| 102 | 101 |
| 103 @SmallTest | 102 @SmallTest |
| 104 @Feature({"Cronet"}) | 103 @Feature({"Cronet"}) |
| 105 public void testListenerRemoved() throws Exception { | 104 public void testListenerRemoved() throws Exception { |
| 106 ExperimentalCronetEngine.Builder mCronetEngineBuilder = | 105 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 107 new ExperimentalCronetEngine.Builder(getContext()); | 106 new ExperimentalCronetEngine.Builder(getContext()); |
| 108 TestExecutor networkQualityExecutor = new TestExecutor(); | 107 TestExecutor networkQualityExecutor = new TestExecutor(); |
| 109 TestNetworkQualityRttListener rttListener = | 108 TestNetworkQualityRttListener rttListener = |
| 110 new TestNetworkQualityRttListener(networkQualityExecutor); | 109 new TestNetworkQualityRttListener(networkQualityExecutor); |
| 111 mCronetEngineBuilder.enableNetworkQualityEstimator(true); | 110 cronetEngineBuilder.enableNetworkQualityEstimator(true); |
| 112 final CronetTestFramework testFramework = | 111 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build(
); |
| 113 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | 112 cronetEngine.configureNetworkQualityEstimatorForTesting(true, true, fals
e); |
| 114 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true, false); | |
| 115 | 113 |
| 116 testFramework.mCronetEngine.addRttListener(rttListener); | 114 cronetEngine.addRttListener(rttListener); |
| 117 testFramework.mCronetEngine.removeRttListener(rttListener); | 115 cronetEngine.removeRttListener(rttListener); |
| 118 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 116 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 119 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( | 117 UrlRequest.Builder builder = |
| 120 mUrl, callback, callback.getExecutor()); | 118 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 121 UrlRequest urlRequest = builder.build(); | 119 UrlRequest urlRequest = builder.build(); |
| 122 urlRequest.start(); | 120 urlRequest.start(); |
| 123 callback.blockForDone(); | 121 callback.blockForDone(); |
| 124 networkQualityExecutor.runAllTasks(); | 122 networkQualityExecutor.runAllTasks(); |
| 125 assertEquals(0, rttListener.rttObservationCount()); | 123 assertEquals(0, rttListener.rttObservationCount()); |
| 126 testFramework.mCronetEngine.shutdown(); | 124 cronetEngine.shutdown(); |
| 127 } | 125 } |
| 128 | 126 |
| 129 // Returns whether a file contains a particular string. | 127 // Returns whether a file contains a particular string. |
| 130 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") | 128 @SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE") |
| 131 private boolean fileContainsString(String filename, String content) throws I
OException { | 129 private boolean fileContainsString(String filename, String content) throws I
OException { |
| 132 File file = | 130 File file = |
| 133 new File(CronetTestFramework.getTestStorage(getContext()) + "/pr
efs/" + filename); | 131 new File(CronetTestFramework.getTestStorage(getContext()) + "/pr
efs/" + filename); |
| 134 FileInputStream fileInputStream = new FileInputStream(file); | 132 FileInputStream fileInputStream = new FileInputStream(file); |
| 135 byte[] data = new byte[(int) file.length()]; | 133 byte[] data = new byte[(int) file.length()]; |
| 136 fileInputStream.read(data); | 134 fileInputStream.read(data); |
| 137 fileInputStream.close(); | 135 fileInputStream.close(); |
| 138 return new String(data, "UTF-8").contains(content); | 136 return new String(data, "UTF-8").contains(content); |
| 139 } | 137 } |
| 140 | 138 |
| 141 @SmallTest | 139 @SmallTest |
| 142 @Feature({"Cronet"}) | 140 @Feature({"Cronet"}) |
| 143 @DisabledTest(message = "Disabled due to flaky assert. See crbug.com/710626"
) | 141 @DisabledTest(message = "Disabled due to flaky assert. See crbug.com/710626"
) |
| 144 public void testQuicDisabled() throws Exception { | 142 public void testQuicDisabled() throws Exception { |
| 145 ExperimentalCronetEngine.Builder mCronetEngineBuilder = | 143 ExperimentalCronetEngine.Builder cronetEngineBuilder = |
| 146 new ExperimentalCronetEngine.Builder(getContext()); | 144 new ExperimentalCronetEngine.Builder(getContext()); |
| 147 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0; | 145 assert RttThroughputValues.INVALID_RTT_THROUGHPUT < 0; |
| 148 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu
torThreadFactory()); | 146 Executor listenersExecutor = Executors.newSingleThreadExecutor(new Execu
torThreadFactory()); |
| 149 TestNetworkQualityRttListener rttListener = | 147 TestNetworkQualityRttListener rttListener = |
| 150 new TestNetworkQualityRttListener(listenersExecutor); | 148 new TestNetworkQualityRttListener(listenersExecutor); |
| 151 TestNetworkQualityThroughputListener throughputListener = | 149 TestNetworkQualityThroughputListener throughputListener = |
| 152 new TestNetworkQualityThroughputListener(listenersExecutor); | 150 new TestNetworkQualityThroughputListener(listenersExecutor); |
| 153 mCronetEngineBuilder.enableNetworkQualityEstimator(true).enableHttp2(tru
e).enableQuic( | 151 cronetEngineBuilder.enableNetworkQualityEstimator(true).enableHttp2(true
).enableQuic(false); |
| 154 false); | 152 cronetEngineBuilder.setStoragePath(CronetTestFramework.getTestStorage(ge
tContext())); |
| 155 mCronetEngineBuilder.setStoragePath(CronetTestFramework.getTestStorage(g
etContext())); | 153 final ExperimentalCronetEngine cronetEngine = cronetEngineBuilder.build(
); |
| 156 final CronetTestFramework testFramework = | 154 cronetEngine.configureNetworkQualityEstimatorForTesting(true, true, true
); |
| 157 startCronetTestFrameworkWithUrlAndCronetEngineBuilder(null, mCro
netEngineBuilder); | |
| 158 testFramework.mCronetEngine.configureNetworkQualityEstimatorForTesting(t
rue, true, true); | |
| 159 | 155 |
| 160 testFramework.mCronetEngine.addRttListener(rttListener); | 156 cronetEngine.addRttListener(rttListener); |
| 161 testFramework.mCronetEngine.addThroughputListener(throughputListener); | 157 cronetEngine.addThroughputListener(throughputListener); |
| 162 | 158 |
| 163 HistogramDelta writeCountHistogram = new HistogramDelta("NQE.Prefs.Write
Count", 1); | 159 HistogramDelta writeCountHistogram = new HistogramDelta("NQE.Prefs.Write
Count", 1); |
| 164 assertEquals(0, writeCountHistogram.getDelta()); // Sanity check. | 160 assertEquals(0, writeCountHistogram.getDelta()); // Sanity check. |
| 165 | 161 |
| 166 HistogramDelta readCountHistogram = new HistogramDelta("NQE.Prefs.ReadCo
unt", 1); | 162 HistogramDelta readCountHistogram = new HistogramDelta("NQE.Prefs.ReadCo
unt", 1); |
| 167 assertEquals(0, readCountHistogram.getDelta()); // Sanity check. | 163 assertEquals(0, readCountHistogram.getDelta()); // Sanity check. |
| 168 | 164 |
| 169 TestUrlRequestCallback callback = new TestUrlRequestCallback(); | 165 TestUrlRequestCallback callback = new TestUrlRequestCallback(); |
| 170 UrlRequest.Builder builder = testFramework.mCronetEngine.newUrlRequestBu
ilder( | 166 UrlRequest.Builder builder = |
| 171 mUrl, callback, callback.getExecutor()); | 167 cronetEngine.newUrlRequestBuilder(mUrl, callback, callback.getEx
ecutor()); |
| 172 UrlRequest urlRequest = builder.build(); | 168 UrlRequest urlRequest = builder.build(); |
| 173 urlRequest.start(); | 169 urlRequest.start(); |
| 174 callback.blockForDone(); | 170 callback.blockForDone(); |
| 175 | 171 |
| 176 // Throughput observation is posted to the network quality estimator on
the network thread | 172 // Throughput observation is posted to the network quality estimator on
the network thread |
| 177 // after the UrlRequest is completed. The observations are then eventual
ly posted to | 173 // after the UrlRequest is completed. The observations are then eventual
ly posted to |
| 178 // throughput listeners on the executor provided to network quality. | 174 // throughput listeners on the executor provided to network quality. |
| 179 throughputListener.waitUntilFirstThroughputObservationReceived(); | 175 throughputListener.waitUntilFirstThroughputObservationReceived(); |
| 180 | 176 |
| 181 // Wait for RTT observation (at the URL request layer) to be posted. | 177 // Wait for RTT observation (at the URL request layer) to be posted. |
| (...skipping 16 matching lines...) Expand all Loading... |
| 198 | 194 |
| 199 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC | 195 // NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC |
| 200 assertEquals(0, rttListener.rttObservationCount(2)); | 196 assertEquals(0, rttListener.rttObservationCount(2)); |
| 201 | 197 |
| 202 // Verify that the listeners were notified on the expected thread. | 198 // Verify that the listeners were notified on the expected thread. |
| 203 assertEquals(mNetworkQualityThread, rttListener.getThread()); | 199 assertEquals(mNetworkQualityThread, rttListener.getThread()); |
| 204 assertEquals(mNetworkQualityThread, throughputListener.getThread()); | 200 assertEquals(mNetworkQualityThread, throughputListener.getThread()); |
| 205 | 201 |
| 206 // Verify that effective connection type callback is received and | 202 // Verify that effective connection type callback is received and |
| 207 // effective connection type is correctly set. | 203 // effective connection type is correctly set. |
| 208 assertTrue(testFramework.mCronetEngine.getEffectiveConnectionType() | 204 assertTrue( |
| 209 != EffectiveConnectionType.TYPE_UNKNOWN); | 205 cronetEngine.getEffectiveConnectionType() != EffectiveConnection
Type.TYPE_UNKNOWN); |
| 210 | 206 |
| 211 // Verify that the HTTP RTT, transport RTT and downstream throughput | 207 // Verify that the HTTP RTT, transport RTT and downstream throughput |
| 212 // estimates are available. | 208 // estimates are available. |
| 213 assertTrue(testFramework.mCronetEngine.getHttpRttMs() >= 0); | 209 assertTrue(cronetEngine.getHttpRttMs() >= 0); |
| 214 assertTrue(testFramework.mCronetEngine.getTransportRttMs() >= 0); | 210 assertTrue(cronetEngine.getTransportRttMs() >= 0); |
| 215 assertTrue(testFramework.mCronetEngine.getDownstreamThroughputKbps() >=
0); | 211 assertTrue(cronetEngine.getDownstreamThroughputKbps() >= 0); |
| 216 | 212 |
| 217 // Verify that the cached estimates were written to the prefs. | 213 // Verify that the cached estimates were written to the prefs. |
| 218 while (true) { | 214 while (true) { |
| 219 Log.i(TAG, "Still waiting for pref file update....."); | 215 Log.i(TAG, "Still waiting for pref file update....."); |
| 220 Thread.sleep(12000); | 216 Thread.sleep(12000); |
| 221 try { | 217 try { |
| 222 if (fileContainsString("local_prefs.json", "network_qualities"))
{ | 218 if (fileContainsString("local_prefs.json", "network_qualities"))
{ |
| 223 break; | 219 break; |
| 224 } | 220 } |
| 225 } catch (FileNotFoundException e) { | 221 } catch (FileNotFoundException e) { |
| 226 // Ignored this exception since the file will only be created wh
en updates are | 222 // Ignored this exception since the file will only be created wh
en updates are |
| 227 // flushed to the disk. | 223 // flushed to the disk. |
| 228 } | 224 } |
| 229 } | 225 } |
| 230 assertTrue(fileContainsString("local_prefs.json", "network_qualities")); | 226 assertTrue(fileContainsString("local_prefs.json", "network_qualities")); |
| 231 | 227 |
| 232 testFramework.mCronetEngine.shutdown(); | 228 cronetEngine.shutdown(); |
| 233 assertTrue(writeCountHistogram.getDelta() > 0); | 229 assertTrue(writeCountHistogram.getDelta() > 0); |
| 234 } | 230 } |
| 235 } | 231 } |
| OLD | NEW |