1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cmath> // For std::modf.
#include <map>
#include <string>
#include "base/command_line.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/histogram_tester.h"
#include "build/build_config.h"
#include "content/browser/net/network_quality_observer_impl.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "net/base/network_change_notifier.h"
#include "net/base/network_change_notifier_factory.h"
#include "net/log/test_net_log.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator_test_util.h"
namespace {
// Returns the total count of samples in |histogram|.
int GetTotalSampleCount(base::HistogramTester* tester,
const std::string& histogram) {
int count = 0;
std::vector<base::Bucket> buckets = tester->GetAllSamples(histogram);
for (const auto& bucket : buckets)
count += bucket.count;
return count;
}
} // namespace
namespace content {
class NetInfoBrowserTest : public content::ContentBrowserTest {
protected:
void SetUpCommandLine(base::CommandLine* command_line) override {
// TODO(jkarlin): Once NetInfo is enabled on all platforms remove this
// switch.
command_line->AppendSwitch(switches::kEnableNetworkInformation);
// TODO(jkarlin): Remove this once downlinkMax is no longer
// experimental.
command_line->AppendSwitch(
switches::kEnableExperimentalWebPlatformFeatures);
}
void SetUp() override {
net::NetworkChangeNotifier::SetTestNotificationsOnly(true);
#if defined(OS_CHROMEOS)
// ChromeOS's NetworkChangeNotifier isn't known to content and therefore
// doesn't get created in content_browsertests. Insert a mock
// NetworkChangeNotifier.
net::NetworkChangeNotifier::CreateMock();
#endif
content::ContentBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
base::RunLoop().RunUntilIdle();
}
static void SetConnectionType(
net::NetworkChangeNotifier::ConnectionType type,
net::NetworkChangeNotifier::ConnectionSubtype subtype) {
net::NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests(
net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
subtype),
type);
base::RunLoop().RunUntilIdle();
}
std::string RunScriptExtractString(const std::string& script) {
std::string data;
EXPECT_TRUE(ExecuteScriptAndExtractString(shell(), script, &data));
return data;
}
bool RunScriptExtractBool(const std::string& script) {
bool data;
EXPECT_TRUE(ExecuteScriptAndExtractBool(shell(), script, &data));
return data;
}
double RunScriptExtractDouble(const std::string& script) {
double data = 0.0;
EXPECT_TRUE(ExecuteScriptAndExtractDouble(shell(), script, &data));
return data;
}
int RunScriptExtractInt(const std::string& script) {
int data = 0;
EXPECT_TRUE(ExecuteScriptAndExtractInt(shell(), script, &data));
return data;
}
};
// Make sure the type is correct when the page is first opened.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, VerifyNetworkStateInitialized) {
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET),
RunScriptExtractDouble("getDownlinkMax()"));
}
// Make sure that type changes in the browser make their way to
// navigator.connection.type.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) {
NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI,
net::NetworkChangeNotifier::SUBTYPE_WIFI_N);
EXPECT_EQ("wifi", RunScriptExtractString("getType()"));
EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
net::NetworkChangeNotifier::SUBTYPE_WIFI_N),
RunScriptExtractDouble("getDownlinkMax()"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET),
RunScriptExtractDouble("getDownlinkMax()"));
}
// Make sure that type changes in the browser make their way to
// navigator.isOnline.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) {
NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE,
net::NetworkChangeNotifier::SUBTYPE_NONE);
EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI,
net::NetworkChangeNotifier::SUBTYPE_WIFI_N);
EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
}
// Creating a new render view shouldn't reinitialize Blink's
// NetworkStateNotifier. See https://crbug.com/535081.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, TwoRenderViewsInOneProcess) {
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET,
net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET);
NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE,
net::NetworkChangeNotifier::SUBTYPE_NONE);
EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
// Open the same page in a new window on the same process.
EXPECT_TRUE(ExecuteScript(shell(), "window.open(\"net_info.html\")"));
// The network state should not have reinitialized to what it was when opening
// the first window (online).
EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
}
// Verify that when the network quality notifications are not sent, the
// Javascript API returns a valid estimate that is multiple of 25 msec and 25
// kbps.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest,
NetworkQualityEstimatorNotInitialized) {
base::HistogramTester histogram_tester;
net::TestNetworkQualityEstimator estimator(
nullptr, std::map<std::string, std::string>(), false, false, true, true,
base::MakeUnique<net::BoundTestNetLog>());
NetworkQualityObserverImpl impl(&estimator);
EXPECT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
EXPECT_EQ(0, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(0, RunScriptExtractInt("getRtt()") % 25);
double downlink_mbps = RunScriptExtractDouble("getDownlink()");
EXPECT_LE(0, downlink_mbps);
// Verify that |downlink_mbps| is a multiple of 25 kbps. For example, a value
// of 1.250 mbps satisfies that constraint, but a value of 1.254 mbps does
// not.
double fraction_part, int_part;
fraction_part = std::modf(downlink_mbps, &int_part);
// If |fraction_part| is a multiple of 0.025, it implies |downlink_mbps| is
// also a multiple of 0.025, and hence |downlink_mbps| is a multiple of 25
// kbps.
EXPECT_EQ(0, static_cast<int>(fraction_part * 1000) % 25);
EXPECT_EQ("4g", RunScriptExtractString("getEffectiveType()"));
}
// Make sure the changes in the effective connection typeare notified to the
// render thread.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest,
EffectiveConnectionTypeChangeNotfied) {
base::HistogramTester histogram_tester;
net::TestNetworkQualityEstimator estimator(
nullptr, std::map<std::string, std::string>(), false, false, true, true,
base::MakeUnique<net::BoundTestNetLog>());
NetworkQualityObserverImpl impl(&estimator);
net::nqe::internal::NetworkQuality network_quality_1(
base::TimeDelta::FromSeconds(1), base::TimeDelta::FromSeconds(2), 300);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_1);
EXPECT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
FetchHistogramsFromChildProcesses();
int samples =
GetTotalSampleCount(&histogram_tester, "NQE.RenderThreadNotified");
EXPECT_LT(0, samples);
// Change effective connection type so that the renderer process is notified.
// Changing the effective connection type from 2G to 3G is guaranteed to
// generate the notification to the renderers, irrespective of the current
// effective connection type.
estimator.NotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_2G);
base::RunLoop().RunUntilIdle();
EXPECT_EQ("2g", RunScriptExtractString("getEffectiveType()"));
estimator.NotifyObserversOfEffectiveConnectionType(
net::EFFECTIVE_CONNECTION_TYPE_3G);
base::RunLoop().RunUntilIdle();
EXPECT_EQ("3g", RunScriptExtractString("getEffectiveType()"));
FetchHistogramsFromChildProcesses();
base::RunLoop().RunUntilIdle();
EXPECT_GT(GetTotalSampleCount(&histogram_tester, "NQE.RenderThreadNotified"),
samples);
}
// Make sure the changes in the network quality are notified to the render
// thread, and the changed network quality is accessible via Javascript API.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotified) {
base::HistogramTester histogram_tester;
net::TestNetworkQualityEstimator estimator(
nullptr, std::map<std::string, std::string>(), false, false, true, true,
base::MakeUnique<net::BoundTestNetLog>());
NetworkQualityObserverImpl impl(&estimator);
net::nqe::internal::NetworkQuality network_quality_1(
base::TimeDelta::FromSeconds(1), base::TimeDelta::FromSeconds(2), 300);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_1);
EXPECT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
FetchHistogramsFromChildProcesses();
EXPECT_FALSE(
histogram_tester.GetAllSamples("NQE.RenderThreadNotified").empty());
EXPECT_EQ(network_quality_1.transport_rtt().InMilliseconds(),
RunScriptExtractInt("getRtt()"));
EXPECT_EQ(
static_cast<double>(network_quality_1.downstream_throughput_kbps()) /
1000,
RunScriptExtractDouble("getDownlink()"));
// Verify that the network quality change is accessible via Javascript API.
net::nqe::internal::NetworkQuality network_quality_2(
base::TimeDelta::FromSeconds(10), base::TimeDelta::FromSeconds(20), 3000);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_2);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(network_quality_2.transport_rtt().InMilliseconds(),
RunScriptExtractInt("getRtt()"));
EXPECT_EQ(
static_cast<double>(network_quality_2.downstream_throughput_kbps()) /
1000,
RunScriptExtractDouble("getDownlink()"));
}
// Make sure the changes in the network quality are rounded to the nearest
// 25 milliseconds or 25 kbps.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeRounded) {
base::HistogramTester histogram_tester;
net::TestNetworkQualityEstimator estimator(
std::unique_ptr<net::ExternalEstimateProvider>(),
std::map<std::string, std::string>(), false, false, true, true,
base::MakeUnique<net::BoundTestNetLog>());
NetworkQualityObserverImpl impl(&estimator);
// Verify that the network quality is rounded properly.
net::nqe::internal::NetworkQuality network_quality_1(
base::TimeDelta::FromMilliseconds(123),
base::TimeDelta::FromMilliseconds(212), 303);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_1);
EXPECT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
EXPECT_EQ(200, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(0.300, RunScriptExtractDouble("getDownlink()"));
net::nqe::internal::NetworkQuality network_quality_2(
base::TimeDelta::FromMilliseconds(123),
base::TimeDelta::FromMilliseconds(1217), 1317);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_2);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1225, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(1.325, RunScriptExtractDouble("getDownlink()"));
net::nqe::internal::NetworkQuality network_quality_3(
base::TimeDelta::FromMilliseconds(12),
base::TimeDelta::FromMilliseconds(12), 12);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_3);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(0, RunScriptExtractDouble("getDownlink()"));
}
// Make sure the minor changes (<10%) in the network quality are not notified.
IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkQualityChangeNotNotified) {
base::HistogramTester histogram_tester;
net::TestNetworkQualityEstimator estimator(
nullptr, std::map<std::string, std::string>(), false, false, true, true,
base::MakeUnique<net::BoundTestNetLog>());
NetworkQualityObserverImpl impl(&estimator);
// Verify that the network quality is rounded properly.
net::nqe::internal::NetworkQuality network_quality_1(
base::TimeDelta::FromMilliseconds(1123),
base::TimeDelta::FromMilliseconds(1212), 1303);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_1);
EXPECT_TRUE(embedded_test_server()->Start());
EXPECT_TRUE(
NavigateToURL(shell(), embedded_test_server()->GetURL("/net_info.html")));
EXPECT_EQ(1200, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()"));
// All the 3 metrics change by less than 10%. So, the observers are not
// notified.
net::nqe::internal::NetworkQuality network_quality_2(
base::TimeDelta::FromMilliseconds(1223),
base::TimeDelta::FromMilliseconds(1312), 1403);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_2);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1200, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(1.300, RunScriptExtractDouble("getDownlink()"));
// Transport RTT has changed by more than 10% from the last notified value of
// |network_quality_1|. The observers should be notified.
net::nqe::internal::NetworkQuality network_quality_3(
base::TimeDelta::FromMilliseconds(1223),
base::TimeDelta::FromMilliseconds(2312), 1403);
estimator.NotifyObserversOfRTTOrThroughputEstimatesComputed(
network_quality_3);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2300, RunScriptExtractInt("getRtt()"));
EXPECT_EQ(1.400, RunScriptExtractDouble("getDownlink()"));
}
} // namespace content
|