[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
tbansal | 420ae34 | 2017-06-06 07:31:40 | [diff] [blame] | 5 | #include <cmath> // For std::modf. |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 6 | #include <map> |
| 7 | #include <string> |
| 8 | |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 9 | #include "base/command_line.h" |
| 10 | #include "base/run_loop.h" |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
Devlin Cronin | 513398f | 2018-06-05 15:33:49 | [diff] [blame] | 12 | #include "base/test/metrics/histogram_tester.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 13 | #include "build/build_config.h" |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 14 | #include "content/browser/net/network_quality_observer_impl.h" |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 15 | #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 Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 22 | #include "net/dns/mock_host_resolver.h" |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 23 | #include "net/log/test_net_log.h" |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 24 | #include "net/nqe/effective_connection_type.h" |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 25 | #include "services/network/test/test_network_quality_tracker.h" |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 26 | |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | // Returns the total count of samples in |histogram|. |
| 30 | int 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 Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 39 | void 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 Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 52 | std::abs(expected_rtt.InMilliseconds() - got_rtt_milliseconds)) |
| 53 | << " expected_rtt=" << expected_rtt |
| 54 | << " got_rtt_milliseconds=" << got_rtt_milliseconds; |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void 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 Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 79 | EXPECT_GE((expected_kbps * 0.1) + 50, std::abs(expected_kbps - got_kbps)) |
| 80 | << " expected_kbps=" << expected_kbps << " got_kbps=" << got_kbps; |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 81 | } |
| 82 | |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 83 | } // namespace |
| 84 | |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 85 | namespace content { |
| 86 | |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 87 | class NetInfoBrowserTest : public content::ContentBrowserTest { |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 88 | 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] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 97 | protected: |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 98 | void SetUpCommandLine(base::CommandLine* command_line) override { |
[email protected] | 0c40da93 | 2014-07-30 17:43:45 | [diff] [blame] | 99 | // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this |
| 100 | // switch. |
Tarun Bansal | 1f332ebe | 2017-07-14 04:18:09 | [diff] [blame] | 101 | command_line->AppendSwitch(switches::kEnableNetworkInformationDownlinkMax); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 102 | |
| 103 | // TODO(jkarlin): Remove this once downlinkMax is no longer |
| 104 | // experimental. |
| 105 | command_line->AppendSwitch( |
| 106 | switches::kEnableExperimentalWebPlatformFeatures); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 107 | } |
| 108 | |
dcheng | 34187481 | 2015-01-23 23:56:59 | [diff] [blame] | 109 | void SetUp() override { |
jkarlin | e160f6b | 2015-07-31 17:04:08 | [diff] [blame] | 110 | net::NetworkChangeNotifier::SetTestNotificationsOnly(true); |
| 111 | |
| 112 | #if defined(OS_CHROMEOS) |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 113 | // 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] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 117 | #endif |
| 118 | |
jkarlin | e160f6b | 2015-07-31 17:04:08 | [diff] [blame] | 119 | content::ContentBrowserTest::SetUp(); |
| 120 | } |
| 121 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 122 | void SetUpOnMainThread() override { |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 123 | host_resolver()->AddRule("*", "127.0.0.1"); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 124 | base::RunLoop().RunUntilIdle(); |
| 125 | } |
| 126 | |
| 127 | static void SetConnectionType( |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 128 | net::NetworkChangeNotifier::ConnectionType type, |
| 129 | net::NetworkChangeNotifier::ConnectionSubtype subtype) { |
| 130 | net::NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests( |
zhuoyu.qian | 4d65140 | 2017-10-30 13:03:33 | [diff] [blame] | 131 | net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype( |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 132 | subtype), |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 133 | type); |
| 134 | base::RunLoop().RunUntilIdle(); |
| 135 | } |
| 136 | |
| 137 | std::string RunScriptExtractString(const std::string& script) { |
| 138 | std::string data; |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 139 | EXPECT_TRUE(ExecuteScriptAndExtractString(shell(), script, &data)); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 140 | return data; |
| 141 | } |
| 142 | |
| 143 | bool RunScriptExtractBool(const std::string& script) { |
| 144 | bool data; |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 145 | EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), script, &data)); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 146 | return data; |
| 147 | } |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 148 | |
| 149 | double RunScriptExtractDouble(const std::string& script) { |
| 150 | double data = 0.0; |
tbansal | 420ae34 | 2017-06-06 07:31:40 | [diff] [blame] | 151 | 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)); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 158 | return data; |
| 159 | } |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 160 | |
| 161 | private: |
| 162 | std::unique_ptr<network::TestNetworkQualityTracker> |
| 163 | test_network_quality_tracker_; |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 164 | }; |
| 165 | |
jkarlin | 0c80fd6 | 2015-12-04 19:38:41 | [diff] [blame] | 166 | // Make sure the type is correct when the page is first opened. |
| 167 | IN_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.qian | 4d65140 | 2017-10-30 13:03:33 | [diff] [blame] | 173 | EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype( |
jkarlin | 0c80fd6 | 2015-12-04 19:38:41 | [diff] [blame] | 174 | net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET), |
| 175 | RunScriptExtractDouble("getDownlinkMax()")); |
| 176 | } |
| 177 | |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 178 | // Make sure that type changes in the browser make their way to |
| 179 | // navigator.connection.type. |
| 180 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) { |
| 181 | NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 182 | SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI, |
| 183 | net::NetworkChangeNotifier::SUBTYPE_WIFI_N); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 184 | EXPECT_EQ("wifi", RunScriptExtractString("getType()")); |
zhuoyu.qian | 4d65140 | 2017-10-30 13:03:33 | [diff] [blame] | 185 | EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype( |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 186 | net::NetworkChangeNotifier::SUBTYPE_WIFI_N), |
| 187 | RunScriptExtractDouble("getDownlinkMax()")); |
| 188 | |
| 189 | SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 190 | net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 191 | EXPECT_EQ("ethernet", RunScriptExtractString("getType()")); |
zhuoyu.qian | 4d65140 | 2017-10-30 13:03:33 | [diff] [blame] | 192 | EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthMbpsForConnectionSubtype( |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 193 | net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET), |
| 194 | RunScriptExtractDouble("getDownlinkMax()")); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Make sure that type changes in the browser make their way to |
| 198 | // navigator.isOnline. |
| 199 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) { |
| 200 | NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 201 | SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 202 | net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 203 | EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 204 | SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE, |
| 205 | net::NetworkChangeNotifier::SUBTYPE_NONE); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 206 | EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 207 | SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI, |
| 208 | net::NetworkChangeNotifier::SUBTYPE_WIFI_N); |
[email protected] | 404f174 | 2014-06-23 13:30:11 | [diff] [blame] | 209 | EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); |
| 210 | } |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 211 | |
jkarlin | 0c80fd6 | 2015-12-04 19:38:41 | [diff] [blame] | 212 | // Creating a new render view shouldn't reinitialize Blink's |
| 213 | // NetworkStateNotifier. See https://crbug.com/535081. |
| 214 | IN_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. |
nick | adef4a5 | 2016-06-09 18:45:54 | [diff] [blame] | 225 | EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")")); |
jkarlin | 0c80fd6 | 2015-12-04 19:38:41 | [diff] [blame] | 226 | |
| 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 | |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 232 | // Verify that when the network quality notifications are not sent, the |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 233 | // Javascript API returns a valid estimate that is multiple of 50 msec and 50 |
tbansal | 420ae34 | 2017-06-06 07:31:40 | [diff] [blame] | 234 | // kbps. |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 235 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, |
| 236 | NetworkQualityEstimatorNotInitialized) { |
| 237 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 238 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 239 | |
| 240 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 241 | EXPECT_TRUE( |
| 242 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
| 243 | |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 244 | // When NQE is not initialized, the javascript calls should return default |
| 245 | // values. |
tbansal | 420ae34 | 2017-06-06 07:31:40 | [diff] [blame] | 246 | EXPECT_EQ(0, RunScriptExtractInt("getRtt()")); |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 247 | VerifyDownlinkKbps(10000, RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 248 | } |
| 249 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 250 | // Make sure the changes in the effective connection type are notified to the |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 251 | // render thread. |
| 252 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, |
Tarun Bansal | d10f17a0 | 2018-09-13 00:23:17 | [diff] [blame] | 253 | EffectiveConnectionTypeChangeNotified) { |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 254 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 255 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 256 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 257 | base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(1000)); |
| 258 | int32_t downstream_throughput_kbps = 300; |
| 259 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 260 | http_rtt, downstream_throughput_kbps); |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 261 | |
| 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 Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 276 | GetNetworkQualityTracker()->ReportEffectiveConnectionTypeForTesting( |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 277 | net::EFFECTIVE_CONNECTION_TYPE_2G); |
| 278 | base::RunLoop().RunUntilIdle(); |
| 279 | EXPECT_EQ("2g", RunScriptExtractString("getEffectiveType()")); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 280 | GetNetworkQualityTracker()->ReportEffectiveConnectionTypeForTesting( |
tbansal | b612c5d | 2017-05-25 18:53:06 | [diff] [blame] | 281 | 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); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 288 | } |
| 289 | |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 290 | // Make sure the changes in the network quality are notified to the render |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 291 | // thread, and the changed network quality is accessible via Javascript API. |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 292 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotified) { |
| 293 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 294 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 295 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 296 | 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); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 301 | |
| 302 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 303 | EXPECT_TRUE( |
| 304 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 305 | |
| 306 | FetchHistogramsFromChildProcesses(); |
| 307 | EXPECT_FALSE( |
| 308 | histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty()); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 309 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 310 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 311 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 312 | RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 313 | |
| 314 | // Verify that the network quality change is accessible via Javascript API. |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 315 | http_rtt = base::TimeDelta::FromSeconds(10); |
| 316 | downstream_throughput_kbps = 3000; |
| 317 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 318 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 319 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 320 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 321 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 322 | RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | // Make sure the changes in the network quality are rounded to the nearest |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 326 | // 50 milliseconds or 50 kbps. |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 327 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) { |
| 328 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 329 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 330 | |
| 331 | // Verify that the network quality is rounded properly. |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 332 | base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(103)); |
| 333 | int32_t downstream_throughput_kbps = 8303; |
| 334 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 335 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 336 | |
| 337 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 338 | EXPECT_TRUE( |
| 339 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 340 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 341 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 342 | RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 343 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 344 | http_rtt = base::TimeDelta::FromMilliseconds(1103); |
| 345 | downstream_throughput_kbps = 1307; |
| 346 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 347 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 348 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 349 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 350 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 351 | RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 352 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 353 | http_rtt = base::TimeDelta::FromMilliseconds(2112); |
| 354 | downstream_throughput_kbps = 2112; |
| 355 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 356 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 357 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 358 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 359 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 360 | RunScriptExtractDouble("getDownlink()") * 1000); |
| 361 | } |
| 362 | |
| 363 | // Make sure the network quality are rounded down when it exceeds the upper |
| 364 | // limit. |
| 365 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeUpperLimit) { |
| 366 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 367 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 368 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 369 | 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 Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 374 | |
| 375 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 376 | EXPECT_TRUE( |
| 377 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 378 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 379 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 380 | RunScriptExtractDouble("getDownlink()") * 1000); |
| 381 | } |
| 382 | |
| 383 | // Make sure the noise added to the network quality varies with the hostname. |
| 384 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityRandomized) { |
| 385 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 386 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 387 | |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 388 | 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 Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 393 | |
| 394 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 395 | |
| 396 | EXPECT_TRUE( |
| 397 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 398 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 399 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 400 | 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 Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 409 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 410 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 411 | 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 Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 426 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 427 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 428 | 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"; |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Make sure the minor changes (<10%) in the network quality are not notified. |
| 443 | IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) { |
| 444 | base::HistogramTester histogram_tester; |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 445 | NetworkQualityObserverImpl impl(GetNetworkQualityTracker()); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 446 | |
| 447 | // Verify that the network quality is rounded properly. |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 448 | base::TimeDelta http_rtt(base::TimeDelta::FromMilliseconds(1123)); |
| 449 | int32_t downstream_throughput_kbps = 1303; |
| 450 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 451 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 452 | |
| 453 | EXPECT_TRUE(embedded_test_server()->Start()); |
| 454 | EXPECT_TRUE( |
| 455 | NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html"))); |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 456 | VerifyRtt(http_rtt, RunScriptExtractInt("getRtt()")); |
| 457 | VerifyDownlinkKbps(downstream_throughput_kbps, |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 458 | RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 459 | |
| 460 | // All the 3 metrics change by less than 10%. So, the observers are not |
| 461 | // notified. |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 462 | http_rtt = base::TimeDelta::FromMilliseconds(1223); |
| 463 | downstream_throughput_kbps = 1403; |
| 464 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 465 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 466 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 467 | VerifyRtt(base::TimeDelta::FromMilliseconds(1100), |
| 468 | RunScriptExtractInt("getRtt()")); |
| 469 | VerifyDownlinkKbps(1300, RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 470 | |
tbansal | 7545111 | 2017-06-18 23:27:40 | [diff] [blame] | 471 | // HTTP RTT has changed by more than 10% from the last notified value of |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 472 | // |network_quality_1|. The observers should be notified. |
Tarun Bansal | 86b3922 | 2018-09-21 02:07:32 | [diff] [blame] | 473 | http_rtt = base::TimeDelta::FromMilliseconds(2223); |
| 474 | downstream_throughput_kbps = 1403; |
| 475 | GetNetworkQualityTracker()->ReportRTTsAndThroughputForTesting( |
| 476 | http_rtt, downstream_throughput_kbps); |
tbansal | 99d8aeb | 2017-05-22 19:12:58 | [diff] [blame] | 477 | base::RunLoop().RunUntilIdle(); |
Tarun Bansal | f609e92 | 2017-08-16 18:20:08 | [diff] [blame] | 478 | VerifyRtt(base::TimeDelta::FromMilliseconds(2200), |
| 479 | RunScriptExtractInt("getRtt()")); |
| 480 | VerifyDownlinkKbps(1400, RunScriptExtractDouble("getDownlink()") * 1000); |
tbansal | 15973c3 | 2017-05-10 18:40:44 | [diff] [blame] | 481 | } |
| 482 | |
jkarlin | 300a1d2 | 2015-09-18 19:32:52 | [diff] [blame] | 483 | } // namespace content |