Index: chrome/browser/media/router/discovery/discovery_network_monitor_unittest.cc |
diff --git a/chrome/browser/media/router/discovery/discovery_network_monitor_unittest.cc b/chrome/browser/media/router/discovery/discovery_network_monitor_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dcb9ae069d6c9d8c460112b1736098c4bae844e3 |
--- /dev/null |
+++ b/chrome/browser/media/router/discovery/discovery_network_monitor_unittest.cc |
@@ -0,0 +1,168 @@ |
+// Copyright (c) 2017 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 "chrome/browser/media/router/discovery/discovery_network_monitor.h" |
+ |
+#include "base/run_loop.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/test/test_browser_thread_bundle.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+using content::BrowserThread; |
+ |
+using testing::_; |
+using testing::Invoke; |
+ |
+class MockDiscoveryObserver : public DiscoveryNetworkMonitor::Observer { |
+ public: |
+ MOCK_METHOD1(OnNetworksChanged, void(const DiscoveryNetworkMonitor&)); |
+}; |
+ |
+std::vector<NetworkInfo> FakeGetNetworkInfo( |
+ const std::vector<NetworkInfo>& network_info_list) { |
+ return network_info_list; |
+} |
+ |
+class DiscoveryNetworkMonitorTest : public testing::Test { |
+ protected: |
+ void SetUp() override { |
+ // NetworkChangeNotifier provides singleton behavior while simultaneously |
+ // allowing the creator to own the instance. This makes it possible to |
+ // reset it for each test, however this isn't possible with LazyInstance<>, |
+ // which DiscoveryNetworkMonitor uses. It's also not possible to just leave |
+ // one NetworkChangeNotifier mock alive for all the tests because the thread |
+ // bundle is being recreated in each fixture which causes their task runners |
+ // to become different. |
+ if (should_rebind_to_network_change_mock) { |
+ discovery_network_monitor->RebindNetworkChangeObserverForTest(); |
+ } else { |
+ should_rebind_to_network_change_mock = true; |
+ } |
+ |
+ // Clear the network info list and network ID before each test since the |
+ // monitor is a singleton. |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_NONE); |
+ base::RunLoop().RunUntilIdle(); |
+ } |
+ |
+ static bool should_rebind_to_network_change_mock; |
+ |
+ content::TestBrowserThreadBundle test_browser_thread_bundle; |
+ MockDiscoveryObserver mock_observer; |
+ |
+ std::vector<NetworkInfo> fake_ethernet_info{ |
+ {{0, net::NetworkChangeNotifier::CONNECTION_ETHERNET, |
+ std::string("enp0s2"), std::string("ethernet1")}}}; |
+ std::vector<NetworkInfo> fake_wifi_info{ |
+ {{0, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s0"), |
+ std::string("wifi1")}, |
+ {1, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s1"), |
+ std::string("wifi2")}}}; |
+ |
+ std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier = |
+ base::WrapUnique(net::NetworkChangeNotifier::CreateMock()); |
+ |
+ std::vector<NetworkInfo> fake_network_info; |
+ DiscoveryNetworkMonitor* discovery_network_monitor = |
+ DiscoveryNetworkMonitor::GetInstanceForTest( |
+ base::Bind(&FakeGetNetworkInfo, base::ConstRef(fake_network_info))); |
+}; |
+ |
+bool DiscoveryNetworkMonitorTest::should_rebind_to_network_change_mock = false; |
+ |
+} // namespace |
+ |
+TEST_F(DiscoveryNetworkMonitorTest, NetworkIdIsConsistent) { |
+ fake_network_info = fake_ethernet_info; |
+ std::string current_network_id; |
+ |
+ auto capture_network_id = |
+ [¤t_network_id](const DiscoveryNetworkMonitor& monitor) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ current_network_id = monitor.GetNetworkId(); |
+ }; |
+ discovery_network_monitor->AddObserver(&mock_observer); |
+ EXPECT_CALL(mock_observer, OnNetworksChanged(_)) |
+ .WillOnce(Invoke(capture_network_id)); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::string ethernet_network_id = current_network_id; |
+ |
+ fake_network_info.clear(); |
+ EXPECT_CALL(mock_observer, OnNetworksChanged(_)) |
+ .WillOnce(Invoke(capture_network_id)); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_NONE); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ fake_network_info = fake_wifi_info; |
+ EXPECT_CALL(mock_observer, OnNetworksChanged(_)) |
+ .WillOnce(Invoke(capture_network_id)); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_WIFI); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ std::string wifi_network_id = current_network_id; |
+ fake_network_info = fake_ethernet_info; |
+ EXPECT_CALL(mock_observer, OnNetworksChanged(_)) |
+ .WillOnce(Invoke(capture_network_id)); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ EXPECT_EQ(ethernet_network_id, current_network_id); |
+ EXPECT_NE(ethernet_network_id, wifi_network_id); |
+ |
+ discovery_network_monitor->RemoveObserver(&mock_observer); |
+} |
+ |
+TEST_F(DiscoveryNetworkMonitorTest, RemoveObserverStopsNotifications) { |
+ fake_network_info = fake_ethernet_info; |
+ |
+ discovery_network_monitor->AddObserver(&mock_observer); |
+ EXPECT_CALL(mock_observer, OnNetworksChanged(_)) |
+ .WillOnce(Invoke([](const DiscoveryNetworkMonitor& monitor) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ })); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_ETHERNET); |
+ base::RunLoop().RunUntilIdle(); |
+ |
+ discovery_network_monitor->RemoveObserver(&mock_observer); |
+ fake_network_info.clear(); |
+ |
+ net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
+ net::NetworkChangeNotifier::CONNECTION_NONE); |
+ base::RunLoop().RunUntilIdle(); |
+} |
+ |
+TEST_F(DiscoveryNetworkMonitorTest, RefreshIndependentOfChangeObserver) { |
+ fake_network_info = fake_ethernet_info; |
+ |
+ discovery_network_monitor->AddObserver(&mock_observer); |
+ auto force_refresh_callback = []() { |
+ DiscoveryNetworkMonitor* discovery_network_monitor = |
+ DiscoveryNetworkMonitor::GetInstance(); |
+ EXPECT_NE(std::string("disconnected"), |
+ discovery_network_monitor->GetNetworkId()); |
+ EXPECT_NE(std::string("unknown"), |
+ discovery_network_monitor->GetNetworkId()); |
+ }; |
+ |
+ EXPECT_EQ(std::string("disconnected"), |
+ discovery_network_monitor->GetNetworkId()); |
+ discovery_network_monitor->Refresh(base::Bind(force_refresh_callback)); |
+ base::RunLoop().RunUntilIdle(); |
+} |