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 <map> |
| 6 #include <string> |
| 7 |
5 #include "base/command_line.h" | 8 #include "base/command_line.h" |
6 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
7 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
8 #include "base/test/histogram_tester.h" | 11 #include "base/test/histogram_tester.h" |
9 #include "build/build_config.h" | 12 #include "build/build_config.h" |
10 #include "content/browser/net/network_quality_observer_impl.h" | 13 #include "content/browser/net/network_quality_observer_impl.h" |
11 #include "content/public/common/content_switches.h" | 14 #include "content/public/common/content_switches.h" |
12 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" |
13 #include "content/public/test/content_browser_test.h" | 16 #include "content/public/test/content_browser_test.h" |
14 #include "content/public/test/content_browser_test_utils.h" | 17 #include "content/public/test/content_browser_test_utils.h" |
15 #include "content/shell/browser/shell.h" | 18 #include "content/shell/browser/shell.h" |
16 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
17 #include "net/base/network_change_notifier_factory.h" | 20 #include "net/base/network_change_notifier_factory.h" |
| 21 #include "net/log/test_net_log.h" |
18 #include "net/nqe/network_quality_estimator_test_util.h" | 22 #include "net/nqe/network_quality_estimator_test_util.h" |
19 | 23 |
20 namespace content { | 24 namespace content { |
21 | 25 |
22 class NetInfoBrowserTest : public content::ContentBrowserTest { | 26 class NetInfoBrowserTest : public content::ContentBrowserTest { |
23 protected: | 27 protected: |
24 void SetUpCommandLine(base::CommandLine* command_line) override { | 28 void SetUpCommandLine(base::CommandLine* command_line) override { |
25 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this | 29 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this |
26 // switch. | 30 // switch. |
27 command_line->AppendSwitch(switches::kEnableNetworkInformation); | 31 command_line->AppendSwitch(switches::kEnableNetworkInformation); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); | 141 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); |
138 | 142 |
139 // Open the same page in a new window on the same process. | 143 // Open the same page in a new window on the same process. |
140 EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")")); | 144 EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")")); |
141 | 145 |
142 // The network state should not have reinitialized to what it was when opening | 146 // The network state should not have reinitialized to what it was when opening |
143 // the first window (online). | 147 // the first window (online). |
144 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); | 148 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); |
145 } | 149 } |
146 | 150 |
| 151 // Verify that when the network quality notifications are not sent, the |
| 152 // Javascript API returns invalid estimate. |
| 153 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, |
| 154 NetworkQualityEstimatorNotInitialized) { |
| 155 base::HistogramTester histogram_tester; |
| 156 net::TestNetworkQualityEstimator estimator( |
| 157 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| 158 base::MakeUnique<net::BoundTestNetLog>()); |
| 159 NetworkQualityObserverImpl impl(&estimator); |
| 160 |
| 161 EXPECT_TRUE(embedded_test_server()->Start()); |
| 162 EXPECT_TRUE( |
| 163 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 164 |
| 165 EXPECT_EQ(-1, RunScriptExtractDouble("getRtt()")); |
| 166 EXPECT_EQ(-0.001, RunScriptExtractDouble("getDownlink()")); |
| 167 } |
| 168 |
147 // Make sure the changes in the network quality are notified to the render | 169 // Make sure the changes in the network quality are notified to the render |
148 // thread. | 170 // thread, and the changed network quality is accessible via Javascript API. |
149 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotified) { | 171 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotified) { |
150 base::HistogramTester histogram_tester; | 172 base::HistogramTester histogram_tester; |
151 net::TestNetworkQualityEstimator estimator; | 173 net::TestNetworkQualityEstimator estimator( |
| 174 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| 175 base::MakeUnique<net::BoundTestNetLog>()); |
152 NetworkQualityObserverImpl impl(&estimator); | 176 NetworkQualityObserverImpl impl(&estimator); |
| 177 |
| 178 net::nqe::internal::NetworkQuality network_quality_1( |
| 179 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromSeconds(2), 300); |
153 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( | 180 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
154 net::nqe::internal::NetworkQuality(base::TimeDelta::FromSeconds(1), | 181 network_quality_1); |
155 base::TimeDelta::FromSeconds(2), 3)); | |
156 | 182 |
157 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); | 183 EXPECT_TRUE(embedded_test_server()->Start()); |
| 184 EXPECT_TRUE( |
| 185 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
158 | 186 |
159 FetchHistogramsFromChildProcesses(); | 187 FetchHistogramsFromChildProcesses(); |
160 EXPECT_FALSE( | 188 EXPECT_FALSE( |
161 histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty()); | 189 histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty()); |
| 190 |
| 191 EXPECT_EQ(network_quality_1.transport_rtt().InMilliseconds(), |
| 192 RunScriptExtractDouble("getRtt()")); |
| 193 EXPECT_EQ( |
| 194 static_cast<double>(network_quality_1.downstream_throughput_kbps()) / |
| 195 1000, |
| 196 RunScriptExtractDouble("getDownlink()")); |
| 197 |
| 198 // Verify that the network quality change is accessible via Javascript API. |
| 199 net::nqe::internal::NetworkQuality network_quality_2( |
| 200 base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(20), 3000); |
| 201 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 202 network_quality_2); |
| 203 base::RunLoop().RunUntilIdle(); |
| 204 EXPECT_EQ(network_quality_2.transport_rtt().InMilliseconds(), |
| 205 RunScriptExtractDouble("getRtt()")); |
| 206 EXPECT_EQ( |
| 207 static_cast<double>(network_quality_2.downstream_throughput_kbps()) / |
| 208 1000, |
| 209 RunScriptExtractDouble("getDownlink()")); |
| 210 } |
| 211 |
| 212 // Make sure the changes in the network quality are rounded to the nearest |
| 213 // 25 milliseconds or 25 kbps. |
| 214 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) { |
| 215 base::HistogramTester histogram_tester; |
| 216 net::TestNetworkQualityEstimator estimator( |
| 217 std::unique_ptr<net::ExternalEstimateProvider>(), |
| 218 std::map<std::string, std::string>(), false, false, true, true, |
| 219 base::MakeUnique<net::BoundTestNetLog>()); |
| 220 NetworkQualityObserverImpl impl(&estimator); |
| 221 |
| 222 // Verify that the network quality is rounded properly. |
| 223 net::nqe::internal::NetworkQuality network_quality_1( |
| 224 base::TimeDelta::FromMilliseconds(123), |
| 225 base::TimeDelta::FromMilliseconds(212), 303); |
| 226 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 227 network_quality_1); |
| 228 |
| 229 EXPECT_TRUE(embedded_test_server()->Start()); |
| 230 EXPECT_TRUE( |
| 231 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 232 EXPECT_EQ(200, RunScriptExtractDouble("getRtt()")); |
| 233 EXPECT_EQ(0.300, RunScriptExtractDouble("getDownlink()")); |
| 234 |
| 235 net::nqe::internal::NetworkQuality network_quality_2( |
| 236 base::TimeDelta::FromMilliseconds(123), |
| 237 base::TimeDelta::FromMilliseconds(1217), 1317); |
| 238 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 239 network_quality_2); |
| 240 base::RunLoop().RunUntilIdle(); |
| 241 EXPECT_EQ(1225, RunScriptExtractDouble("getRtt()")); |
| 242 EXPECT_EQ(1.325, RunScriptExtractDouble("getDownlink()")); |
| 243 |
| 244 net::nqe::internal::NetworkQuality network_quality_3( |
| 245 base::TimeDelta::FromMilliseconds(12), |
| 246 base::TimeDelta::FromMilliseconds(12), 12); |
| 247 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 248 network_quality_3); |
| 249 base::RunLoop().RunUntilIdle(); |
| 250 EXPECT_EQ(0, RunScriptExtractDouble("getRtt()")); |
| 251 EXPECT_EQ(0, RunScriptExtractDouble("getDownlink()")); |
| 252 } |
| 253 |
| 254 // Make sure the minor changes (<10%) in the network quality are not notified. |
| 255 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) { |
| 256 base::HistogramTester histogram_tester; |
| 257 net::TestNetworkQualityEstimator estimator( |
| 258 nullptr, std::map<std::string, std::string>(), false, false, true, true, |
| 259 base::MakeUnique<net::BoundTestNetLog>()); |
| 260 NetworkQualityObserverImpl impl(&estimator); |
| 261 |
| 262 // Verify that the network quality is rounded properly. |
| 263 net::nqe::internal::NetworkQuality network_quality_1( |
| 264 base::TimeDelta::FromMilliseconds(1123), |
| 265 base::TimeDelta::FromMilliseconds(1212), 1303); |
| 266 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 267 network_quality_1); |
| 268 |
| 269 EXPECT_TRUE(embedded_test_server()->Start()); |
| 270 EXPECT_TRUE( |
| 271 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 272 EXPECT_EQ(1200, RunScriptExtractDouble("getRtt()")); |
| 273 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); |
| 274 |
| 275 // All the 3 metrics change by less than 10%. So, the observers are not |
| 276 // notified. |
| 277 net::nqe::internal::NetworkQuality network_quality_2( |
| 278 base::TimeDelta::FromMilliseconds(1223), |
| 279 base::TimeDelta::FromMilliseconds(1312), 1403); |
| 280 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 281 network_quality_2); |
| 282 base::RunLoop().RunUntilIdle(); |
| 283 EXPECT_EQ(1200, RunScriptExtractDouble("getRtt()")); |
| 284 EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()")); |
| 285 |
| 286 // Transport RTT has changed by more than 10% from the last notified value of |
| 287 // |network_quality_1|. The observers should be notified. |
| 288 net::nqe::internal::NetworkQuality network_quality_3( |
| 289 base::TimeDelta::FromMilliseconds(1223), |
| 290 base::TimeDelta::FromMilliseconds(2312), 1403); |
| 291 estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed( |
| 292 network_quality_3); |
| 293 base::RunLoop().RunUntilIdle(); |
| 294 EXPECT_EQ(2300, RunScriptExtractDouble("getRtt()")); |
| 295 EXPECT_EQ(1.400, RunScriptExtractDouble("getDownlink()")); |
162 } | 296 } |
163 | 297 |
164 } // namespace content | 298 } // namespace content |
OLD | NEW |