blob: e506f12bbfe3d590e5c44bef82a1720dc05f68ec [file] [log] [blame]
[email protected]404f1742014-06-23 13:30:111// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
tbansal420ae342017-06-06 07:31:405#include <cmath> // For std::modf.
tbansal99d8aeb2017-05-22 19:12:586#include <map>
7#include <string>
8
[email protected]404f1742014-06-23 13:30:119#include "base/command_line.h"
10#include "base/run_loop.h"
jkarlin300a1d22015-09-18 19:32:5211#include "base/strings/string_number_conversions.h"
Devlin Cronin513398f2018-06-05 15:33:4912#include "base/test/metrics/histogram_tester.h"
avib7348942015-12-25 20:57:1013#include "build/build_config.h"
tbansal15973c32017-05-10 18:40:4414#include "content/browser/net/network_quality_observer_impl.h"
[email protected]404f1742014-06-23 13:30:1115#include "content/public/common/content_switches.h"
16#include "content/public/test/browser_test_utils.h"
17#include "content/public/test/content_browser_test.h"
18#include "content/public/test/content_browser_test_utils.h"
19#include "content/shell/browser/shell.h"
20#include "net/base/network_change_notifier.h"
21#include "net/base/network_change_notifier_factory.h"
Tarun Bansalf609e922017-08-16 18:20:0822#include "net/dns/mock_host_resolver.h"
tbansal99d8aeb2017-05-22 19:12:5823#include "net/log/test_net_log.h"
tbansalb612c5d2017-05-25 18:53:0624#include "net/nqe/effective_connection_type.h"
Tarun Bansal86b39222018-09-21 02:07:3225#include "services/network/test/test_network_quality_tracker.h"
[email protected]404f1742014-06-23 13:30:1126
tbansalb612c5d2017-05-25 18:53:0627namespace {
28
29// Returns the total count of samples in |histogram|.
30int GetTotalSampleCount(base::HistogramTester* tester,
31 const std::string& histogram) {
32 int count = 0;
33 std::vector<base::Bucket> buckets = tester->GetAllSamples(histogram);
34 for (const auto& bucket : buckets)
35 count += bucket.count;
36 return count;
37}
38
Tarun Bansalf609e922017-08-16 18:20:0839void VerifyRtt(base::TimeDelta expected_rtt, int32_t got_rtt_milliseconds) {
40 EXPECT_EQ(0, got_rtt_milliseconds % 50)
41 << " got_rtt_milliseconds=" << got_rtt_milliseconds;
42
43 if (expected_rtt > base::TimeDelta::FromMilliseconds(3000))
44 expected_rtt = base::TimeDelta::FromMilliseconds(3000);
45
46 // The difference between the actual and the estimate value should be within
47 // 10%. Add 50 (bucket size used in Blink) to account for the cases when the
48 // sample may spill over to the next bucket due to the added noise of 10%.
49 // For example, if sample is 300 msec, after adding noise, it may become 330,
50 // and after rounding off, it would spill over to the next bucket of 350 msec.
51 EXPECT_GE((expected_rtt.InMilliseconds() * 0.1) + 50,
Tarun Bansal86b39222018-09-21 02:07:3252 std::abs(expected_rtt.InMilliseconds() - got_rtt_milliseconds))
53 << " expected_rtt=" << expected_rtt
54 << " got_rtt_milliseconds=" << got_rtt_milliseconds;
Tarun Bansalf609e922017-08-16 18:20:0855}
56
57void VerifyDownlinkKbps(double expected_kbps, double got_kbps) {
58 // First verify that |got_kbps| is a multiple of 50.
59 int quotient = static_cast<int>(got_kbps / 50);
60 // |mod| is the remainder left after dividing |got_kbps| by 50 while
61 // restricting the quotient to integer. For example, if |got_kbps| is
62 // 1050, then mod will be 0. If |got_kbps| is 1030, mod will be 30.
63 double mod = got_kbps - 50 * quotient;
64 EXPECT_LE(0.0, mod);
65 EXPECT_GT(50.0, mod);
66 // It is possible that |mod| is not exactly 0 because of floating point
67 // computations. e.g., |got_kbps| may be 99.999999, in which case |mod|
68 // will be 49.999999.
69 EXPECT_TRUE(mod < (1e-5) || (50 - mod) < 1e-5) << " got_kbps=" << got_kbps;
70
71 if (expected_kbps > 10000)
72 expected_kbps = 10000;
73
74 // The difference between the actual and the estimate value should be within
75 // 10%. Add 50 (bucket size used in Blink) to account for the cases when the
76 // sample may spill over to the next bucket due to the added noise of 10%.
77 // For example, if sample is 300 kbps, after adding noise, it may become 330,
78 // and after rounding off, it would spill over to the next bucket of 350 kbps.
Tarun Bansal86b39222018-09-21 02:07:3279 EXPECT_GE((expected_kbps * 0.1) + 50, std::abs(expected_kbps - got_kbps))
80 << " expected_kbps=" << expected_kbps << " got_kbps=" << got_kbps;
Tarun Bansalf609e922017-08-16 18:20:0881}
82
tbansalb612c5d2017-05-25 18:53:0683} // namespace
84
jkarlin300a1d22015-09-18 19:32:5285namespace content {
86
[email protected]404f1742014-06-23 13:30:1187class NetInfoBrowserTest : public content::ContentBrowserTest {
Tarun Bansal86b39222018-09-21 02:07:3288 public:
89 NetInfoBrowserTest()
90 : test_network_quality_tracker_(
91 std::make_unique<network::TestNetworkQualityTracker>()) {}
92
93 network::NetworkQualityTracker* GetNetworkQualityTracker() const {
94 return test_network_quality_tracker_.get();
95 }
96
[email protected]404f1742014-06-23 13:30:1197 protected:
dchengc2282aa2014-10-21 12:07:5898 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]0c40da932014-07-30 17:43:4599 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this
100 // switch.
Tarun Bansal1f332ebe2017-07-14 04:18:09101 command_line->AppendSwitch(switches::kEnableNetworkInformationDownlinkMax);
jkarlin300a1d22015-09-18 19:32:52102
103 // TODO(jkarlin): Remove this once downlinkMax is no longer
104 // experimental.
105 command_line->AppendSwitch(
106 switches::kEnableExperimentalWebPlatformFeatures);
[email protected]404f1742014-06-23 13:30:11107 }
108
dcheng341874812015-01-23 23:56:59109 void SetUp() override {
jkarline160f6b2015-07-31 17:04:08110 net::NetworkChangeNotifier::SetTestNotificationsOnly(true);
111
112#if defined(OS_CHROMEOS)
[email protected]404f1742014-06-23 13:30:11113 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore
114 // doesn't get created in content_browsertests. Insert a mock
115 // NetworkChangeNotifier.
116 net::NetworkChangeNotifier::CreateMock();
[email protected]404f1742014-06-23 13:30:11117#endif
118
jkarline160f6b2015-07-31 17:04:08119 content::ContentBrowserTest::SetUp();
120 }
121
dchengc2282aa2014-10-21 12:07:58122 void SetUpOnMainThread() override {
Tarun Bansalf609e922017-08-16 18:20:08123 host_resolver()->AddRule("*", "127.0.0.1");
[email protected]404f1742014-06-23 13:30:11124 base::RunLoop().RunUntilIdle();
125 }
126
127 static void SetConnectionType(
jkarlin300a1d22015-09-18 19:32:52128 net::NetworkChangeNotifier::ConnectionType type,
129 net::NetworkChangeNotifier::ConnectionSubtype subtype) {
130 net::NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests(
zhuoyu.qian4d651402017-10-30 13:03:33131 net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
jkarlin300a1d22015-09-18 19:32:52132 subtype),
[email protected]404f1742014-06-23 13:30:11133 type);
134 base::RunLoop().RunUntilIdle();
135 }
136
137 std::string RunScriptExtractString(const std::string& script) {
138 std::string data;
nickadef4a52016-06-09 18:45:54139 EXPECT_TRUE(ExecuteScriptAndExtractString(shell(), script, &data));
[email protected]404f1742014-06-23 13:30:11140 return data;
141 }
142
143 bool RunScriptExtractBool(const std::string& script) {
144 bool data;
nickadef4a52016-06-09 18:45:54145 EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), script, &data));
[email protected]404f1742014-06-23 13:30:11146 return data;
147 }
jkarlin300a1d22015-09-18 19:32:52148
149 double RunScriptExtractDouble(const std::string& script) {
150 double data = 0.0;
tbansal420ae342017-06-06 07:31:40151 EXPECT_TRUE(ExecuteScriptAndExtractDouble(shell(), script, &data));
152 return data;
153 }
154
155 int RunScriptExtractInt(const std::string& script) {
156 int data = 0;
157 EXPECT_TRUE(ExecuteScriptAndExtractInt(shell(), script, &data));
jkarlin300a1d22015-09-18 19:32:52158 return data;
159 }
Tarun Bansal86b39222018-09-21 02:07:32160
161 private:
162 std::unique_ptr<network::TestNetworkQualityTracker>
163 test_network_quality_tracker_;
[email protected]404f1742014-06-23 13:30:11164};
165
jkarlin0c80fd62015-12-04 19:38:41166// Make sure the type is correct when the page is first opened.
167IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, VerifyNetworkStateInitialized) {
168 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
169 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
170 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
171 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
172 EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
zhuoyu.qian4d651402017-10-30 13:03:33173 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
jkarlin0c80fd62015-12-04 19:38:41174 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET),
175 RunScriptExtractDouble("getDownlinkMax()"));
176}
177
[email protected]404f1742014-06-23 13:30:11178// Make sure that type changes in the browser make their way to
179// navigator.connection.type.
180IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) {
181 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
jkarlin300a1d22015-09-18 19:32:52182 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI,
183 net::NetworkChangeNotifier::SUBTYPE_WIFI_N);
[email protected]404f1742014-06-23 13:30:11184 EXPECT_EQ("wifi", RunScriptExtractString("getType()"));
zhuoyu.qian4d651402017-10-30 13:03:33185 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
jkarlin300a1d22015-09-18 19:32:52186 net::NetworkChangeNotifier::SUBTYPE_WIFI_N),
187 RunScriptExtractDouble("getDownlinkMax()"));
188
189 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
190 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
[email protected]404f1742014-06-23 13:30:11191 EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
zhuoyu.qian4d651402017-10-30 13:03:33192 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype(
jkarlin300a1d22015-09-18 19:32:52193 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET),
194 RunScriptExtractDouble("getDownlinkMax()"));
[email protected]404f1742014-06-23 13:30:11195}
196
197// Make sure that type changes in the browser make their way to
198// navigator.isOnline.
199IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) {
200 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
jkarlin300a1d22015-09-18 19:32:52201 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
202 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
[email protected]404f1742014-06-23 13:30:11203 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
jkarlin300a1d22015-09-18 19:32:52204 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE,
205 net::NetworkChangeNotifier::SUBTYPE_NONE);
[email protected]404f1742014-06-23 13:30:11206 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
jkarlin300a1d22015-09-18 19:32:52207 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI,
208 net::NetworkChangeNotifier::SUBTYPE_WIFI_N);
[email protected]404f1742014-06-23 13:30:11209 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
210}
jkarlin300a1d22015-09-18 19:32:52211
jkarlin0c80fd62015-12-04 19:38:41212// Creating a new render view shouldn't reinitialize Blink's
213// NetworkStateNotifier. See https://crbug.com/535081.
214IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, TwoRenderViewsInOneProcess) {
215 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
216 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
217 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
218 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
219
220 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE,
221 net::NetworkChangeNotifier::SUBTYPE_NONE);
222 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
223
224 // Open the same page in a new window on the same process.
nickadef4a52016-06-09 18:45:54225 EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")"));
jkarlin0c80fd62015-12-04 19:38:41226
227 // The network state should not have reinitialized to what it was when opening
228 // the first window (online).
229 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
230}
231
tbansal99d8aeb2017-05-22 19:12:58232// Verify that when the network quality notifications are not sent, the
Tarun Bansalf609e922017-08-16 18:20:08233// Javascript API returns a valid estimate that is multiple of 50 msec and 50
tbansal420ae342017-06-06 07:31:40234// kbps.
tbansal99d8aeb2017-05-22 19:12:58235IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest,
236 NetworkQualityEstimatorNotInitialized) {
237 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32238 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
tbansal99d8aeb2017-05-22 19:12:58239
240 EXPECT_TRUE(embedded_test_server()->Start());
241 EXPECT_TRUE(
242 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
243
Tarun Bansalf609e922017-08-16 18:20:08244 // When NQE is not initialized, the javascript calls should return default
245 // values.
tbansal420ae342017-06-06 07:31:40246 EXPECT_EQ(0, RunScriptExtractInt("getRtt()"));
Tarun Bansalf609e922017-08-16 18:20:08247 VerifyDownlinkKbps(10000, RunScriptExtractDouble("getDownlink()") * 1000);
tbansalb612c5d2017-05-25 18:53:06248}
249
Tarun Bansal86b39222018-09-21 02:07:32250// Make sure the changes in the effective connection type are notified to the
tbansalb612c5d2017-05-25 18:53:06251// render thread.
252IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest,
Tarun Bansald10f17a02018-09-13 00:23:17253 EffectiveConnectionTypeChangeNotified) {
tbansalb612c5d2017-05-25 18:53:06254 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32255 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
tbansalb612c5d2017-05-25 18:53:06256
Tarun Bansal86b39222018-09-21 02:07:32257 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(1000));
258 int32_t downstream_throughput_kbps = 300;
259 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
260 http_rtt, downstream_throughput_kbps);
tbansalb612c5d2017-05-25 18:53:06261
262 EXPECT_TRUE(embedded_test_server()->Start());
263 EXPECT_TRUE(
264 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
265
266 FetchHistogramsFromChildProcesses();
267
268 int samples =
269 GetTotalSampleCount(&histogram_tester, "NQE.RenderThreadNotified");
270 EXPECT_LT(0, samples);
271
272 // Change effective connection type so that the renderer process is notified.
273 // Changing the effective connection type from 2G to 3G is guaranteed to
274 // generate the notification to the renderers, irrespective of the current
275 // effective connection type.
Tarun Bansal86b39222018-09-21 02:07:32276 GetNetworkQualityTracker()->ReportEffectiveConnectionTypeForTesting(
tbansalb612c5d2017-05-25 18:53:06277 net::EFFECTIVE_CONNECTION_TYPE_2G);
278 base::RunLoop().RunUntilIdle();
279 EXPECT_EQ("2g", RunScriptExtractString("getEffectiveType()"));
Tarun Bansal86b39222018-09-21 02:07:32280 GetNetworkQualityTracker()->ReportEffectiveConnectionTypeForTesting(
tbansalb612c5d2017-05-25 18:53:06281 net::EFFECTIVE_CONNECTION_TYPE_3G);
282 base::RunLoop().RunUntilIdle();
283 EXPECT_EQ("3g", RunScriptExtractString("getEffectiveType()"));
284 FetchHistogramsFromChildProcesses();
285 base::RunLoop().RunUntilIdle();
286 EXPECT_GT(GetTotalSampleCount(&histogram_tester, "NQE.RenderThreadNotified"),
287 samples);
tbansal99d8aeb2017-05-22 19:12:58288}
289
tbansal15973c32017-05-10 18:40:44290// Make sure the changes in the network quality are notified to the render
tbansal99d8aeb2017-05-22 19:12:58291// thread, and the changed network quality is accessible via Javascript API.
tbansal15973c32017-05-10 18:40:44292IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotified) {
293 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32294 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
tbansal15973c32017-05-10 18:40:44295
Tarun Bansal86b39222018-09-21 02:07:32296 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(1000));
297 int32_t downstream_throughput_kbps = 300;
298
299 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
300 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58301
302 EXPECT_TRUE(embedded_test_server()->Start());
303 EXPECT_TRUE(
304 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
tbansal15973c32017-05-10 18:40:44305
306 FetchHistogramsFromChildProcesses();
307 EXPECT_FALSE(
308 histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty());
tbansal99d8aeb2017-05-22 19:12:58309
Tarun Bansal86b39222018-09-21 02:07:32310 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
311 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08312 RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58313
314 // Verify that the network quality change is accessible via Javascript API.
Tarun Bansal86b39222018-09-21 02:07:32315 http_rtt = base::TimeDelta::FromSeconds(10);
316 downstream_throughput_kbps = 3000;
317 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
318 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58319 base::RunLoop().RunUntilIdle();
Tarun Bansal86b39222018-09-21 02:07:32320 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
321 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08322 RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58323}
324
325// Make sure the changes in the network quality are rounded to the nearest
Tarun Bansalf609e922017-08-16 18:20:08326// 50 milliseconds or 50 kbps.
tbansal99d8aeb2017-05-22 19:12:58327IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) {
328 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32329 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
tbansal99d8aeb2017-05-22 19:12:58330
331 // Verify that the network quality is rounded properly.
Tarun Bansal86b39222018-09-21 02:07:32332 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(103));
333 int32_t downstream_throughput_kbps = 8303;
334 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
335 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58336
337 EXPECT_TRUE(embedded_test_server()->Start());
338 EXPECT_TRUE(
339 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32340 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
341 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08342 RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58343
Tarun Bansal86b39222018-09-21 02:07:32344 http_rtt = base::TimeDelta::FromMilliseconds(1103);
345 downstream_throughput_kbps = 1307;
346 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
347 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58348 base::RunLoop().RunUntilIdle();
Tarun Bansal86b39222018-09-21 02:07:32349 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
350 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08351 RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58352
Tarun Bansal86b39222018-09-21 02:07:32353 http_rtt = base::TimeDelta::FromMilliseconds(2112);
354 downstream_throughput_kbps = 2112;
355 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
356 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58357 base::RunLoop().RunUntilIdle();
Tarun Bansal86b39222018-09-21 02:07:32358 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
359 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08360 RunScriptExtractDouble("getDownlink()") * 1000);
361}
362
363// Make sure the network quality are rounded down when it exceeds the upper
364// limit.
365IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeUpperLimit) {
366 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32367 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
Tarun Bansalf609e922017-08-16 18:20:08368
Tarun Bansal86b39222018-09-21 02:07:32369 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(12003));
370 int32_t downstream_throughput_kbps = 30300;
371
372 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
373 http_rtt, downstream_throughput_kbps);
Tarun Bansalf609e922017-08-16 18:20:08374
375 EXPECT_TRUE(embedded_test_server()->Start());
376 EXPECT_TRUE(
377 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32378 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
379 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08380 RunScriptExtractDouble("getDownlink()") * 1000);
381}
382
383// Make sure the noise added to the network quality varies with the hostname.
384IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityRandomized) {
385 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32386 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
Tarun Bansalf609e922017-08-16 18:20:08387
Tarun Bansal86b39222018-09-21 02:07:32388 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(2000));
389 int32_t downstream_throughput_kbps = 3000;
390
391 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
392 http_rtt, downstream_throughput_kbps);
Tarun Bansalf609e922017-08-16 18:20:08393
394 EXPECT_TRUE(embedded_test_server()->Start());
395
396 EXPECT_TRUE(
397 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32398 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
399 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08400 RunScriptExtractDouble("getDownlink()") * 1000);
401
402 const int32_t rtt_noise_milliseconds = RunScriptExtractInt("getRtt()") - 2000;
403 const int32_t downlink_noise_kbps =
404 RunScriptExtractDouble("getDownlink()") * 1000 - 3000;
405
406 // When the hostname is not changed, the noise should not change.
407 EXPECT_TRUE(
408 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32409 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
410 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08411 RunScriptExtractDouble("getDownlink()") * 1000);
412 EXPECT_EQ(rtt_noise_milliseconds, RunScriptExtractInt("getRtt()") - 2000);
413 EXPECT_EQ(downlink_noise_kbps,
414 RunScriptExtractDouble("getDownlink()") * 1000 - 3000);
415
416 // Verify that changing the hostname changes the noise. It is possible that
417 // the hash of a different host also maps to the same bucket among 20 buckets.
418 // Try 10 different hosts. This reduces the probability of failure of this
419 // test to (1/20)^10 = 9,7 * 10^-14.
420 for (size_t i = 0; i < 10; ++i) {
421 // The noise added is a function of the hostname. Varying the hostname
422 // should vary the noise.
423 std::string fake_hostname = "example" + base::IntToString(i) + ".com";
424 EXPECT_TRUE(NavigateToURL(shell(), embedded_test_server()->GetURL(
425 fake_hostname, "/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32426 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
427 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08428 RunScriptExtractDouble("getDownlink()") * 1000);
429
430 int32_t new_rtt_noise_milliseconds = RunScriptExtractInt("getRtt()") - 2000;
431 int32_t new_downlink_noise_kbps =
432 RunScriptExtractDouble("getDownlink()") * 1000 - 3000;
433
434 if (rtt_noise_milliseconds != new_rtt_noise_milliseconds &&
435 downlink_noise_kbps != new_downlink_noise_kbps) {
436 return;
437 }
438 }
439 NOTREACHED() << "Noise not added to the network quality estimates";
tbansal99d8aeb2017-05-22 19:12:58440}
441
442// Make sure the minor changes (<10%) in the network quality are not notified.
443IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) {
444 base::HistogramTester histogram_tester;
Tarun Bansal86b39222018-09-21 02:07:32445 NetworkQualityObserverImpl impl(GetNetworkQualityTracker());
tbansal99d8aeb2017-05-22 19:12:58446
447 // Verify that the network quality is rounded properly.
Tarun Bansal86b39222018-09-21 02:07:32448 base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(1123));
449 int32_t downstream_throughput_kbps = 1303;
450 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
451 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58452
453 EXPECT_TRUE(embedded_test_server()->Start());
454 EXPECT_TRUE(
455 NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
Tarun Bansal86b39222018-09-21 02:07:32456 VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()"));
457 VerifyDownlinkKbps(downstream_throughput_kbps,
Tarun Bansalf609e922017-08-16 18:20:08458 RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58459
460 // All the 3 metrics change by less than 10%. So, the observers are not
461 // notified.
Tarun Bansal86b39222018-09-21 02:07:32462 http_rtt = base::TimeDelta::FromMilliseconds(1223);
463 downstream_throughput_kbps = 1403;
464 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
465 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58466 base::RunLoop().RunUntilIdle();
Tarun Bansalf609e922017-08-16 18:20:08467 VerifyRtt(base::TimeDelta::FromMilliseconds(1100),
468 RunScriptExtractInt("getRtt()"));
469 VerifyDownlinkKbps(1300, RunScriptExtractDouble("getDownlink()") * 1000);
tbansal99d8aeb2017-05-22 19:12:58470
tbansal75451112017-06-18 23:27:40471 // HTTP RTT has changed by more than 10% from the last notified value of
tbansal99d8aeb2017-05-22 19:12:58472 // |network_quality_1|. The observers should be notified.
Tarun Bansal86b39222018-09-21 02:07:32473 http_rtt = base::TimeDelta::FromMilliseconds(2223);
474 downstream_throughput_kbps = 1403;
475 GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting(
476 http_rtt, downstream_throughput_kbps);
tbansal99d8aeb2017-05-22 19:12:58477 base::RunLoop().RunUntilIdle();
Tarun Bansalf609e922017-08-16 18:20:08478 VerifyRtt(base::TimeDelta::FromMilliseconds(2200),
479 RunScriptExtractInt("getRtt()"));
480 VerifyDownlinkKbps(1400, RunScriptExtractDouble("getDownlink()") * 1000);
tbansal15973c32017-05-10 18:40:44481}
482
jkarlin300a1d22015-09-18 19:32:52483} // namespace content