Roll src/third_party/WebKit e353f22:4d062fa (svn 200353:200356)
[chromium-blink-merge.git] / content / browser / net_info_browsertest.cc
blob67d939176a2c0e29520bf9a4af4ff83e5a0c1211
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 void SetUp() override {
24 net::NetworkChangeNotifier::SetTestNotificationsOnly(true);
26 #if defined(OS_CHROMEOS)
27 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore
28 // doesn't get created in content_browsertests. Insert a mock
29 // NetworkChangeNotifier.
30 net::NetworkChangeNotifier::CreateMock();
31 #endif
33 content::ContentBrowserTest::SetUp();
36 void SetUpOnMainThread() override {
37 base::RunLoop().RunUntilIdle();
40 static void SetConnectionType(
41 net::NetworkChangeNotifier::ConnectionType type) {
42 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
43 type);
44 base::RunLoop().RunUntilIdle();
47 std::string RunScriptExtractString(const std::string& script) {
48 std::string data;
49 EXPECT_TRUE(
50 ExecuteScriptAndExtractString(shell()->web_contents(), script, &data));
51 return data;
54 bool RunScriptExtractBool(const std::string& script) {
55 bool data;
56 EXPECT_TRUE(
57 ExecuteScriptAndExtractBool(shell()->web_contents(), script, &data));
58 return data;
62 // Make sure that type changes in the browser make their way to
63 // navigator.connection.type.
64 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) {
65 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
66 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
67 EXPECT_EQ("wifi", RunScriptExtractString("getType()"));
68 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
69 EXPECT_EQ("ethernet", RunScriptExtractString("getType()"));
72 // Make sure that type changes in the browser make their way to
73 // navigator.isOnline.
74 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) {
75 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html"));
76 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET);
77 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));
78 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE);
79 EXPECT_FALSE(RunScriptExtractBool("getOnLine()"));
80 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
81 EXPECT_TRUE(RunScriptExtractBool("getOnLine()"));