Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/strings/string_number_conversions.h" | |
| 7 #include "content/public/common/content_switches.h" | 8 #include "content/public/common/content_switches.h" |
| 8 #include "content/public/test/browser_test_utils.h" | 9 #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.h" |
| 10 #include "content/public/test/content_browser_test_utils.h" | 11 #include "content/public/test/content_browser_test_utils.h" |
| 11 #include "content/shell/browser/shell.h" | 12 #include "content/shell/browser/shell.h" |
| 12 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
| 13 #include "net/base/network_change_notifier_factory.h" | 14 #include "net/base/network_change_notifier_factory.h" |
| 14 | 15 |
| 16 namespace content { | |
| 17 | |
| 18 using net::NetworkChangeNotifier; | |
|
jochen (gone - plz use gerrit)
2015/09/16 11:26:18
please no using statements
jkarlin
2015/09/16 12:09:36
Done.
| |
| 19 | |
| 15 class NetInfoBrowserTest : public content::ContentBrowserTest { | 20 class NetInfoBrowserTest : public content::ContentBrowserTest { |
| 16 protected: | 21 protected: |
| 17 void SetUpCommandLine(base::CommandLine* command_line) override { | 22 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 18 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this | 23 // TODO(jkarlin): Once NetInfo is enabled on all platforms remove this |
| 19 // switch. | 24 // switch. |
| 20 command_line->AppendSwitch(switches::kEnableNetworkInformation); | 25 command_line->AppendSwitch(switches::kEnableNetworkInformation); |
| 26 | |
| 27 // TODO(jkarlin): Remove this once downlinkMax is no longer | |
| 28 // experimental. | |
| 29 command_line->AppendSwitch( | |
| 30 switches::kEnableExperimentalWebPlatformFeatures); | |
| 21 } | 31 } |
| 22 | 32 |
| 23 void SetUp() override { | 33 void SetUp() override { |
| 24 net::NetworkChangeNotifier::SetTestNotificationsOnly(true); | 34 NetworkChangeNotifier::SetTestNotificationsOnly(true); |
| 25 | 35 |
| 26 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 27 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore | 37 // ChromeOS's NetworkChangeNotifier isn't known to content and therefore |
| 28 // doesn't get created in content_browsertests. Insert a mock | 38 // doesn't get created in content_browsertests. Insert a mock |
| 29 // NetworkChangeNotifier. | 39 // NetworkChangeNotifier. |
| 30 net::NetworkChangeNotifier::CreateMock(); | 40 NetworkChangeNotifier::CreateMock(); |
| 31 #endif | 41 #endif |
| 32 | 42 |
| 33 content::ContentBrowserTest::SetUp(); | 43 content::ContentBrowserTest::SetUp(); |
| 34 } | 44 } |
| 35 | 45 |
| 36 void SetUpOnMainThread() override { | 46 void SetUpOnMainThread() override { |
| 37 base::RunLoop().RunUntilIdle(); | 47 base::RunLoop().RunUntilIdle(); |
| 38 } | 48 } |
| 39 | 49 |
| 40 static void SetConnectionType( | 50 static void SetConnectionType( |
| 41 net::NetworkChangeNotifier::ConnectionType type) { | 51 NetworkChangeNotifier::ConnectionType type, |
| 42 net::NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( | 52 NetworkChangeNotifier::ConnectionSubtype subtype) { |
| 43 type); | 53 NetworkChangeNotifier::NotifyObserversOfMaxBandwidthChangeForTests( |
| 54 type, | |
| 55 NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype(subtype)); | |
| 44 base::RunLoop().RunUntilIdle(); | 56 base::RunLoop().RunUntilIdle(); |
| 45 } | 57 } |
| 46 | 58 |
| 47 std::string RunScriptExtractString(const std::string& script) { | 59 std::string RunScriptExtractString(const std::string& script) { |
| 48 std::string data; | 60 std::string data; |
| 49 EXPECT_TRUE( | 61 EXPECT_TRUE( |
| 50 ExecuteScriptAndExtractString(shell()->web_contents(), script, &data)); | 62 ExecuteScriptAndExtractString(shell()->web_contents(), script, &data)); |
| 51 return data; | 63 return data; |
| 52 } | 64 } |
| 53 | 65 |
| 54 bool RunScriptExtractBool(const std::string& script) { | 66 bool RunScriptExtractBool(const std::string& script) { |
| 55 bool data; | 67 bool data; |
| 56 EXPECT_TRUE( | 68 EXPECT_TRUE( |
| 57 ExecuteScriptAndExtractBool(shell()->web_contents(), script, &data)); | 69 ExecuteScriptAndExtractBool(shell()->web_contents(), script, &data)); |
| 58 return data; | 70 return data; |
| 59 } | 71 } |
| 72 | |
| 73 double RunScriptExtractDouble(const std::string& script) { | |
| 74 double data = 0.0; | |
| 75 EXPECT_TRUE(base::StringToDouble(RunScriptExtractString(script), &data)); | |
| 76 return data; | |
| 77 } | |
| 60 }; | 78 }; |
| 61 | 79 |
| 62 // Make sure that type changes in the browser make their way to | 80 // Make sure that type changes in the browser make their way to |
| 63 // navigator.connection.type. | 81 // navigator.connection.type. |
| 64 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) { | 82 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, NetworkChangePlumbsToNavigator) { |
| 65 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); | 83 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); |
| 66 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); | 84 SetConnectionType(NetworkChangeNotifier::CONNECTION_WIFI, |
| 85 NetworkChangeNotifier::SUBTYPE_WIFI_N); | |
| 67 EXPECT_EQ("wifi", RunScriptExtractString("getType()")); | 86 EXPECT_EQ("wifi", RunScriptExtractString("getType()")); |
| 68 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 87 EXPECT_EQ(NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( |
| 88 NetworkChangeNotifier::SUBTYPE_WIFI_N), | |
| 89 RunScriptExtractDouble("getDownlinkMax()")); | |
| 90 | |
| 91 SetConnectionType(NetworkChangeNotifier::CONNECTION_ETHERNET, | |
| 92 NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); | |
| 69 EXPECT_EQ("ethernet", RunScriptExtractString("getType()")); | 93 EXPECT_EQ("ethernet", RunScriptExtractString("getType()")); |
| 94 EXPECT_EQ(NetworkChangeNotifier::GetMaxBandwidthForConnectionSubtype( | |
| 95 NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET), | |
| 96 RunScriptExtractDouble("getDownlinkMax()")); | |
| 70 } | 97 } |
| 71 | 98 |
| 72 // Make sure that type changes in the browser make their way to | 99 // Make sure that type changes in the browser make their way to |
| 73 // navigator.isOnline. | 100 // navigator.isOnline. |
| 74 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) { | 101 IN_PROC_BROWSER_TEST_F(NetInfoBrowserTest, IsOnline) { |
| 75 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); | 102 NavigateToURL(shell(), content::GetTestUrl("", "net_info.html")); |
| 76 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_ETHERNET); | 103 SetConnectionType(NetworkChangeNotifier::CONNECTION_ETHERNET, |
| 104 NetworkChangeNotifier::SUBTYPE_GIGABIT_ETHERNET); | |
| 77 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); | 105 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); |
| 78 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_NONE); | 106 SetConnectionType(NetworkChangeNotifier::CONNECTION_NONE, |
| 107 NetworkChangeNotifier::SUBTYPE_NONE); | |
| 79 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); | 108 EXPECT_FALSE(RunScriptExtractBool("getOnLine()")); |
| 80 SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); | 109 SetConnectionType(NetworkChangeNotifier::CONNECTION_WIFI, |
| 110 NetworkChangeNotifier::SUBTYPE_WIFI_N); | |
| 81 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); | 111 EXPECT_TRUE(RunScriptExtractBool("getOnLine()")); |
| 82 } | 112 } |
| 113 | |
| 114 } // namespace content | |
| OLD | NEW |