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.
5 #include "base/command_line.h"
6 #include "base/run_loop.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "content/public/common/content_switches.h"
9 #include "content/public/test/browser_test_utils.h"
10 #include "content/public/test/content_browser_test.h"
11 #include "content/public/test/content_browser_test_utils.h"
12 #include "content/shell/browser/shell.h"
13 #include "net/base/network_change_notifier.h"
14 #include "net/base/network_change_notifier_factory.h"
18 class NetInfoBrowserTest
: public content::ContentBrowserTest
{
20 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
21 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this
23 command_line
->AppendSwitch(switches::kEnableNetworkInformation
);
25 // TODO(jkarlin): Remove this once downlinkMax is no longer
27 command_line
->AppendSwitch(
28 switches::kEnableExperimentalWebPlatformFeatures
);
31 void SetUp() override
{
32 net::NetworkChangeNotifier::SetTestNotificationsOnly(true);
34 #if defined(OS_CHROMEOS)
35 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore
36 // doesn't get created in content_browsertests. Insert a mock
37 // NetworkChangeNotifier.
38 net::NetworkChangeNotifier::CreateMock();
41 content::ContentBrowserTest::SetUp();
44 void SetUpOnMainThread() override
{
45 base::RunLoop().RunUntilIdle();
48 static void SetConnectionType(
49 net::NetworkChangeNotifier::ConnectionType type
,
50 net::NetworkChangeNotifier::ConnectionSubtype subtype
) {
51 net::NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests(
52 net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
55 base::RunLoop().RunUntilIdle();
58 std::string
RunScriptExtractString(const std::string
& script
) {
61 ExecuteScriptAndExtractString(shell()->web_contents(), script
, &data
));
65 bool RunScriptExtractBool(const std::string
& script
) {
68 ExecuteScriptAndExtractBool(shell()->web_contents(), script
, &data
));
72 double RunScriptExtractDouble(const std::string
& script
) {
74 EXPECT_TRUE(base::StringToDouble(RunScriptExtractString(script
), &data
));
79 // Make sure that type changes in the browser make their way to
80 // navigator.connection.type.
81 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest
, NetworkChangePlumbsToNavigator
) {
82 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
83 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI
,
84 net::NetworkChangeNotifier::SUBTYPE_WIFI_N
);
85 EXPECT_EQ("wifi", RunScriptExtractString("getType()"));
86 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
87 net::NetworkChangeNotifier::SUBTYPE_WIFI_N
),
88 RunScriptExtractDouble("getDownlinkMax()"));
90 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET
,
91 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET
);
92 EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
93 EXPECT_EQ(net::NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(
94 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET
),
95 RunScriptExtractDouble("getDownlinkMax()"));
98 // Make sure that type changes in the browser make their way to
99 // navigator.isOnline.
100 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest
, IsOnline
) {
101 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
102 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET
,
103 net::NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET
);
104 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
105 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE
,
106 net::NetworkChangeNotifier::SUBTYPE_NONE
);
107 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
108 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI
,
109 net::NetworkChangeNotifier::SUBTYPE_WIFI_N
);
110 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
113 } // namespace content