Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / net_info_browsertest.cc
blobcea69620487ab2cb1cfcf68343d3a5431a928bc2
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 "content/public/common/content_switches.h"
8 #include "content/public/test/browser_test_utils.h"
9 #include "content/public/test/content_browser_test.h"
10 #include "content/public/test/content_browser_test_utils.h"
11 #include "content/shell/browser/shell.h"
12 #include "net/base/network_change_notifier.h"
13 #include "net/base/network_change_notifier_factory.h"
15 class NetInfoBrowserTest : public content::ContentBrowserTest {
16 protected:
17 void SetUpCommandLine(base::CommandLine* command_line) override {
18 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this
19 // switch.
20 command_line->AppendSwitch(switches::kEnableNetworkInformation);
23 #if defined(OS_CHROMEOS)
24 virtual void SetUp() override {
25 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore
26 // doesn't get created in content_browsertests. Insert a mock
27 // NetworkChangeNotifier.
28 net::NetworkChangeNotifier::CreateMock();
29 content::ContentBrowserTest::SetUp();
31 #endif
33 void SetUpOnMainThread() override {
34 net::NetworkChangeNotifier::SetTestNotificationsOnly(true);
35 base::RunLoop().RunUntilIdle();
38 static void SetConnectionType(
39 net::NetworkChangeNotifier::ConnectionType type) {
40 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
41 type);
42 base::RunLoop().RunUntilIdle();
45 std::string RunScriptExtractString(const std::string& script) {
46 std::string data;
47 EXPECT_TRUE(
48 ExecuteScriptAndExtractString(shell()->web_contents(), script, &data));
49 return data;
52 bool RunScriptExtractBool(const std::string& script) {
53 bool data;
54 EXPECT_TRUE(
55 ExecuteScriptAndExtractBool(shell()->web_contents(), script, &data));
56 return data;
60 // Make sure that type changes in the browser make their way to
61 // navigator.connection.type.
62 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) {
63 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
64 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
65 EXPECT_EQ("wifi", RunScriptExtractString("getType()"));
66 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
67 EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
70 // Make sure that type changes in the browser make their way to
71 // navigator.isOnline.
72 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) {
73 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
74 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
75 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
76 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE);
77 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
78 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
79 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));