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 #include <cmath> // For std::modf. | |
| 5 #include <map> | 6 #include <map> |
| 6 #include <string> | 7 #include <string> |
| 7 | 8 |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/test/histogram_tester.h" | 12 #include "base/test/histogram_tester.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "content/browser/net/network_quality_observer_impl.h" | 14 #include "content/browser/net/network_quality_observer_impl.h" |
| 14 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 bool data; | 89 bool data; |
| 89 EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), script, &data)); | 90 EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), script, &data)); |
| 90 return data; | 91 return data; |
| 91 } | 92 } |
| 92 | 93 |
| 93 double RunScriptExtractDouble(const std::string& script) { | 94 double RunScriptExtractDouble(const std::string& script) { |
| 94 double data = 0.0; | 95 double data = 0.0; |
| 95 EXPECT_TRUE(base::StringToDouble(RunScriptExtractString(script), &data)); | 96 EXPECT_TRUE(base::StringToDouble(RunScriptExtractString(script), &data)); |
| 96 return data; | 97 return data; |
| 97 } | 98 } |
| 99 | |
| 100 int RunScriptExtractInt(const std::string& script) { | |
| 101 int data = 0; | |
| 102 EXPECT_TRUE(base::StringToInt(RunScriptExtractString(script), &data)); | |
|
nasko
2017/06/01 22:16:13
ExecuteScriptAndExtractInt?
tbansal1
2017/06/01 23:26:46
Done.
| |
| 103 return data; | |
| 104 } | |
| 98 }; | 105 }; |
| 99 | 106 |
| 100 // Make sure the type is correct when the page is first opened. | 107 // Make sure the type is correct when the page is first opened. |
| 101 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, VerifyNetworkStateInitialized) { | 108 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, VerifyNetworkStateInitialized) { |
| 102 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET, | 109 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 103 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); | 110 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); |
| 104 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); | 111 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); |
| 105 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); | 112 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); |
| 106 EXPECT_EQ("ethernet", RunScriptExtractString("getType()")); | 113 EXPECT_EQ("ethernet", RunScriptExtractString("getType()")); |
| 107 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( | 114 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 | 164 |
| 158 // Open the same page in a new window on the same process. | 165 // Open the same page in a new window on the same process. |
| 159 EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")")); | 166 EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")")); |
| 160 | 167 |
| 161 // The network state should not have reinitialized to what it was when opening | 168 // The network state should not have reinitialized to what it was when opening |
| 162 // the first window (online). | 169 // the first window (online). |
| 163 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); | 170 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); |
| 164 } | 171 } |
| 165 | 172 |
| 166 // Verify that when the network quality notifications are not sent, the | 173 // Verify that when the network quality notifications are not sent, the |
| 167 // Javascript API returns invalid estimate. | 174 // Javascript API returns a valid estimate that is multiple of 25 msec and 25 |
| 175 // kbps. | |
| 168 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, | 176 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, |
| 169 NetworkQualityEstimatorNotInitialized) { | 177 NetworkQualityEstimatorNotInitialized) { |
| 170 base::HistogramTester histogram_tester; | 178 base::HistogramTester histogram_tester; |
| 171 net::TestNetworkQualityEstimator estimator( | 179 net::TestNetworkQualityEstimator estimator( |
| 172 nullptr, std::map<std::string, std::string>(), false, false, true, true, | 180 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| 173 base::MakeUnique<net::BoundTestNetLog>()); | 181 base::MakeUnique<net::BoundTestNetLog>()); |
| 174 NetworkQualityObserverImpl impl(&estimator); | 182 NetworkQualityObserverImpl impl(&estimator); |
| 175 | 183 |
| 176 EXPECT_TRUE(embedded_test_server()->Start()); | 184 EXPECT_TRUE(embedded_test_server()->Start()); |
| 177 EXPECT_TRUE( | 185 EXPECT_TRUE( |
| 178 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); | 186 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 179 | 187 |
| 180 EXPECT_EQ(0, RunScriptExtractDouble("getRtt()")); | 188 EXPECT_EQ(0, RunScriptExtractInt("getRtt()")); |
|
nasko
2017/06/01 22:16:12
Why can't ExecuteScriptAndExtractInt be used?
tbansal1
2017/06/01 23:26:46
Can be. RunScriptExtractInt() is slightly easier t
| |
| 181 EXPECT_EQ(std::numeric_limits<double>::infinity(), | 189 EXPECT_EQ(0, RunScriptExtractInt("getRtt()") % 25); |
| 182 RunScriptExtractDouble("getDownlink()")); | 190 |
| 191 double downlink_mbps = RunScriptExtractDouble("getDownlink()"); | |
|
nasko
2017/06/01 22:16:13
Similarly ExecuteScriptAndExtractDouble?
tbansal1
2017/06/01 23:26:46
Same as above. Helper method RunScriptExtractDoubl
| |
| 192 EXPECT_LE(0, downlink_mbps); | |
| 193 | |
| 194 double fraction_part, int_part; | |
| 195 fraction_part = std::modf(downlink_mbps, &int_part); | |
| 196 // If |fraction_part| is 0, it implies |downlink_mbps| is a multiple of 1 | |
| 197 // Mbps, and hence it is a multiple of 25 kbps. | |
| 198 EXPECT_TRUE(static_cast<int>(downlink_mbps * 1000) % 25 == 0 || | |
|
nasko
2017/06/01 22:16:12
Shouldn't we be dividing the mbps to get kbps inst
tbansal1
2017/06/01 23:26:46
nope, multiplying it by 1000 would give kbps.
We w
nasko
2017/06/03 00:09:47
Well, when you have mbps and multiply by 1000, you
tbansal1
2017/06/05 13:52:20
We probably are.
|downlink_mbps| is a double. Let'
nasko
2017/06/05 23:43:01
Thanks for the explanation. If you were to only mu
| |
| 199 fraction_part == 0); | |
| 200 | |
| 183 EXPECT_EQ("4g", RunScriptExtractString("getEffectiveType()")); | 201 EXPECT_EQ("4g", RunScriptExtractString("getEffectiveType()")); |
| 184 } | 202 } |
| 185 | 203 |
| 186 // Make sure the changes in the effective connection typeare notified to the | 204 // Make sure the changes in the effective connection typeare notified to the |
| 187 // render thread. | 205 // render thread. |
| 188 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, | 206 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, |
| 189 EffectiveConnectionTypeChangeNotfied) { | 207 EffectiveConnectionTypeChangeNotfied) { |
| 190 base::HistogramTester histogram_tester; | 208 base::HistogramTester histogram_tester; |
| 191 net::TestNetworkQualityEstimator estimator( | 209 net::TestNetworkQualityEstimator estimator( |
| 192 nullptr, std::map<std::string, std::string>(), false, false, true, true, | 210 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 | 260 |
| 243 EXPECT_TRUE(embedded_test_server()->Start()); | 261 EXPECT_TRUE(embedded_test_server()->Start()); |
| 244 EXPECT_TRUE( | 262 EXPECT_TRUE( |
| 245 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); | 263 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 246 | 264 |
| 247 FetchHistogramsFromChildProcesses(); | 265 FetchHistogramsFromChildProcesses(); |
| 248 EXPECT_FALSE( | 266 EXPECT_FALSE( |
| 249 histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty()); | 267 histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty()); |
| 250 | 268 |
| 251 EXPECT_EQ(network_quality_1.transport_rtt().InMilliseconds(), | 269 EXPECT_EQ(network_quality_1.transport_rtt().InMilliseconds(), |
| 252 RunScriptExtractDouble("getRtt()")); | 270 RunScriptExtractInt("getRtt()")); |
| 253 EXPECT_EQ( | 271 EXPECT_EQ( |
| 254 static_cast<double>(network_quality_1.downstream_throughput_kbps()) / | 272 static_cast<double>(network_quality_1.downstream_throughput_kbps()) / |
| 255 1000, | 273 1000, |
| 256 RunScriptExtractDouble("getDownlink()")); | 274 RunScriptExtractDouble("getDownlink()")); |
| 257 | 275 |
| 258 // Verify that the network quality change is accessible via Javascript API. | 276 // Verify that the network quality change is accessible via Javascript API. |
| 259 net::nqe::internal::NetworkQuality network_quality_2( | 277 net::nqe::internal::NetworkQuality network_quality_2( |
| 260 base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(20), 3000); | 278 base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(20), 3000); |
| 261 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 279 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 262 network_quality_2); | 280 network_quality_2); |
| 263 base::RunLoop().RunUntilIdle(); | 281 base::RunLoop().RunUntilIdle(); |
| 264 EXPECT_EQ(network_quality_2.transport_rtt().InMilliseconds(), | 282 EXPECT_EQ(network_quality_2.transport_rtt().InMilliseconds(), |
| 265 RunScriptExtractDouble("getRtt()")); | 283 RunScriptExtractInt("getRtt()")); |
| 266 EXPECT_EQ( | 284 EXPECT_EQ( |
| 267 static_cast<double>(network_quality_2.downstream_throughput_kbps()) / | 285 static_cast<double>(network_quality_2.downstream_throughput_kbps()) / |
| 268 1000, | 286 1000, |
| 269 RunScriptExtractDouble("getDownlink()")); | 287 RunScriptExtractDouble("getDownlink()")); |
| 270 } | 288 } |
| 271 | 289 |
| 272 // Make sure the changes in the network quality are rounded to the nearest | 290 // Make sure the changes in the network quality are rounded to the nearest |
| 273 // 25 milliseconds or 25 kbps. | 291 // 25 milliseconds or 25 kbps. |
| 274 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) { | 292 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) { |
| 275 base::HistogramTester histogram_tester; | 293 base::HistogramTester histogram_tester; |
| 276 net::TestNetworkQualityEstimator estimator( | 294 net::TestNetworkQualityEstimator estimator( |
| 277 std::unique_ptr<net::ExternalEstimateProvider>(), | 295 std::unique_ptr<net::ExternalEstimateProvider>(), |
| 278 std::map<std::string, std::string>(), false, false, true, true, | 296 std::map<std::string, std::string>(), false, false, true, true, |
| 279 base::MakeUnique<net::BoundTestNetLog>()); | 297 base::MakeUnique<net::BoundTestNetLog>()); |
| 280 NetworkQualityObserverImpl impl(&estimator); | 298 NetworkQualityObserverImpl impl(&estimator); |
| 281 | 299 |
| 282 // Verify that the network quality is rounded properly. | 300 // Verify that the network quality is rounded properly. |
| 283 net::nqe::internal::NetworkQuality network_quality_1( | 301 net::nqe::internal::NetworkQuality network_quality_1( |
| 284 base::TimeDelta::FromMilliseconds(123), | 302 base::TimeDelta::FromMilliseconds(123), |
| 285 base::TimeDelta::FromMilliseconds(212), 303); | 303 base::TimeDelta::FromMilliseconds(212), 303); |
| 286 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 304 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 287 network_quality_1); | 305 network_quality_1); |
| 288 | 306 |
| 289 EXPECT_TRUE(embedded_test_server()->Start()); | 307 EXPECT_TRUE(embedded_test_server()->Start()); |
| 290 EXPECT_TRUE( | 308 EXPECT_TRUE( |
| 291 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); | 309 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 292 EXPECT_EQ(200, RunScriptExtractDouble("getRtt()")); | 310 EXPECT_EQ(200, RunScriptExtractInt("getRtt()")); |
| 293 EXPECT_EQ(0.300, RunScriptExtractDouble("getDownlink()")); | 311 EXPECT_EQ(0.300, RunScriptExtractDouble("getDownlink()")); |
| 294 | 312 |
| 295 net::nqe::internal::NetworkQuality network_quality_2( | 313 net::nqe::internal::NetworkQuality network_quality_2( |
| 296 base::TimeDelta::FromMilliseconds(123), | 314 base::TimeDelta::FromMilliseconds(123), |
| 297 base::TimeDelta::FromMilliseconds(1217), 1317); | 315 base::TimeDelta::FromMilliseconds(1217), 1317); |
| 298 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 316 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 299 network_quality_2); | 317 network_quality_2); |
| 300 base::RunLoop().RunUntilIdle(); | 318 base::RunLoop().RunUntilIdle(); |
| 301 EXPECT_EQ(1225, RunScriptExtractDouble("getRtt()")); | 319 EXPECT_EQ(1225, RunScriptExtractInt("getRtt()")); |
| 302 EXPECT_EQ(1.325, RunScriptExtractDouble("getDownlink()")); | 320 EXPECT_EQ(1.325, RunScriptExtractDouble("getDownlink()")); |
| 303 | 321 |
| 304 net::nqe::internal::NetworkQuality network_quality_3( | 322 net::nqe::internal::NetworkQuality network_quality_3( |
| 305 base::TimeDelta::FromMilliseconds(12), | 323 base::TimeDelta::FromMilliseconds(12), |
| 306 base::TimeDelta::FromMilliseconds(12), 12); | 324 base::TimeDelta::FromMilliseconds(12), 12); |
| 307 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 325 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 308 network_quality_3); | 326 network_quality_3); |
| 309 base::RunLoop().RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
| 310 EXPECT_EQ(0, RunScriptExtractDouble("getRtt()")); | 328 EXPECT_EQ(0, RunScriptExtractInt("getRtt()")); |
| 311 EXPECT_EQ(0, RunScriptExtractDouble("getDownlink()")); | 329 EXPECT_EQ(0, RunScriptExtractDouble("getDownlink()")); |
| 312 } | 330 } |
| 313 | 331 |
| 314 // Make sure the minor changes (<10%) in the network quality are not notified. | 332 // Make sure the minor changes (<10%) in the network quality are not notified. |
| 315 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) { | 333 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) { |
| 316 base::HistogramTester histogram_tester; | 334 base::HistogramTester histogram_tester; |
| 317 net::TestNetworkQualityEstimator estimator( | 335 net::TestNetworkQualityEstimator estimator( |
| 318 nullptr, std::map<std::string, std::string>(), false, false, true, true, | 336 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| 319 base::MakeUnique<net::BoundTestNetLog>()); | 337 base::MakeUnique<net::BoundTestNetLog>()); |
| 320 NetworkQualityObserverImpl impl(&estimator); | 338 NetworkQualityObserverImpl impl(&estimator); |
| 321 | 339 |
| 322 // Verify that the network quality is rounded properly. | 340 // Verify that the network quality is rounded properly. |
| 323 net::nqe::internal::NetworkQuality network_quality_1( | 341 net::nqe::internal::NetworkQuality network_quality_1( |
| 324 base::TimeDelta::FromMilliseconds(1123), | 342 base::TimeDelta::FromMilliseconds(1123), |
| 325 base::TimeDelta::FromMilliseconds(1212), 1303); | 343 base::TimeDelta::FromMilliseconds(1212), 1303); |
| 326 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 344 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 327 network_quality_1); | 345 network_quality_1); |
| 328 | 346 |
| 329 EXPECT_TRUE(embedded_test_server()->Start()); | 347 EXPECT_TRUE(embedded_test_server()->Start()); |
| 330 EXPECT_TRUE( | 348 EXPECT_TRUE( |
| 331 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); | 349 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 332 EXPECT_EQ(1200, RunScriptExtractDouble("getRtt()")); | 350 EXPECT_EQ(1200, RunScriptExtractInt("getRtt()")); |
| 333 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); | 351 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); |
| 334 | 352 |
| 335 // All the 3 metrics change by less than 10%. So, the observers are not | 353 // All the 3 metrics change by less than 10%. So, the observers are not |
| 336 // notified. | 354 // notified. |
| 337 net::nqe::internal::NetworkQuality network_quality_2( | 355 net::nqe::internal::NetworkQuality network_quality_2( |
| 338 base::TimeDelta::FromMilliseconds(1223), | 356 base::TimeDelta::FromMilliseconds(1223), |
| 339 base::TimeDelta::FromMilliseconds(1312), 1403); | 357 base::TimeDelta::FromMilliseconds(1312), 1403); |
| 340 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 358 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 341 network_quality_2); | 359 network_quality_2); |
| 342 base::RunLoop().RunUntilIdle(); | 360 base::RunLoop().RunUntilIdle(); |
| 343 EXPECT_EQ(1200, RunScriptExtractDouble("getRtt()")); | 361 EXPECT_EQ(1200, RunScriptExtractInt("getRtt()")); |
| 344 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); | 362 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); |
| 345 | 363 |
| 346 // Transport RTT has changed by more than 10% from the last notified value of | 364 // Transport RTT has changed by more than 10% from the last notified value of |
| 347 // |network_quality_1|. The observers should be notified. | 365 // |network_quality_1|. The observers should be notified. |
| 348 net::nqe::internal::NetworkQuality network_quality_3( | 366 net::nqe::internal::NetworkQuality network_quality_3( |
| 349 base::TimeDelta::FromMilliseconds(1223), | 367 base::TimeDelta::FromMilliseconds(1223), |
| 350 base::TimeDelta::FromMilliseconds(2312), 1403); | 368 base::TimeDelta::FromMilliseconds(2312), 1403); |
| 351 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 369 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 352 network_quality_3); | 370 network_quality_3); |
| 353 base::RunLoop().RunUntilIdle(); | 371 base::RunLoop().RunUntilIdle(); |
| 354 EXPECT_EQ(2300, RunScriptExtractDouble("getRtt()")); | 372 EXPECT_EQ(2300, RunScriptExtractInt("getRtt()")); |
| 355 EXPECT_EQ(1.400, RunScriptExtractDouble("getDownlink()")); | 373 EXPECT_EQ(1.400, RunScriptExtractDouble("getDownlink()")); |
| 356 } | 374 } |
| 357 | 375 |
| 358 } // namespace content | 376 } // namespace content |
| OLD | NEW |