Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/media/router/discovery/discovery_network_monitor_unittest.cc

Issue 2750453002: Add DiscoveryNetworkMonitor implementation (Closed)
Patch Set: Remove extension test files Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 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.
4
5 #include "chrome/browser/media/router/discovery/discovery_network_monitor.h"
6
7 #include "base/run_loop.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 using content::BrowserThread;
16
17 using testing::_;
18 using testing::Invoke;
19
20 class MockDiscoveryObserver : public DiscoveryNetworkMonitor::Observer {
21 public:
22 MOCK_METHOD1(OnNetworksChanged, void(const DiscoveryNetworkMonitor&));
23 };
24
25 std::vector<NetworkInfo> FakeGetNetworkInfo(
26 const std::vector<NetworkInfo>& network_info_list) {
27 return network_info_list;
28 }
29
30 class DiscoveryNetworkMonitorTest : public testing::Test {
31 protected:
32 void SetUp() override {
33 // NetworkChangeNotifier provides singleton behavior while simultaneously
34 // allowing the creator to own the instance. This makes it possible to
35 // reset it for each test, however this isn't possible with LazyInstance<>,
36 // which DiscoveryNetworkMonitor uses. It's also not possible to just leave
37 // one NetworkChangeNotifier mock alive for all the tests because the thread
38 // bundle is being recreated in each fixture which causes their task runners
39 // to become different.
40 if (should_rebind_to_network_change_mock) {
41 discovery_network_monitor->RebindNetworkChangeObserverForTest();
42 } else {
43 should_rebind_to_network_change_mock = true;
44 }
45
46 // Clear the network info list and network ID before each test since the
47 // monitor is a singleton.
48 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
49 net::NetworkChangeNotifier::CONNECTION_NONE);
50 base::RunLoop().RunUntilIdle();
51 }
52
53 static bool should_rebind_to_network_change_mock;
54
55 content::TestBrowserThreadBundle test_browser_thread_bundle;
56 MockDiscoveryObserver mock_observer;
57
58 std::vector<NetworkInfo> fake_ethernet_info{
59 {{0, net::NetworkChangeNotifier::CONNECTION_ETHERNET,
60 std::string("enp0s2"), std::string("ethernet1")}}};
61 std::vector<NetworkInfo> fake_wifi_info{
62 {{0, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s0"),
63 std::string("wifi1")},
64 {1, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s1"),
65 std::string("wifi2")}}};
66
67 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier =
68 base::WrapUnique(net::NetworkChangeNotifier::CreateMock());
69
70 std::vector<NetworkInfo> fake_network_info;
71 DiscoveryNetworkMonitor* discovery_network_monitor =
72 DiscoveryNetworkMonitor::GetInstanceForTest(
73 base::Bind(&FakeGetNetworkInfo, base::ConstRef(fake_network_info)));
74 };
75
76 bool DiscoveryNetworkMonitorTest::should_rebind_to_network_change_mock = false;
77
78 } // namespace
79
80 TEST_F(DiscoveryNetworkMonitorTest, NetworkIdIsConsistent) {
81 fake_network_info = fake_ethernet_info;
82 std::string current_network_id;
83
84 auto capture_network_id =
85 [&current_network_id](const DiscoveryNetworkMonitor& monitor) {
86 DCHECK_CURRENTLY_ON(BrowserThread::IO);
87 current_network_id = monitor.GetNetworkId();
88 };
89 discovery_network_monitor->AddObserver(&mock_observer);
90 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
91 .WillOnce(Invoke(capture_network_id));
92
93 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
94 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
95 base::RunLoop().RunUntilIdle();
96
97 std::string ethernet_network_id = current_network_id;
98
99 fake_network_info.clear();
100 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
101 .WillOnce(Invoke(capture_network_id));
102
103 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
104 net::NetworkChangeNotifier::CONNECTION_NONE);
105 base::RunLoop().RunUntilIdle();
106
107 fake_network_info = fake_wifi_info;
108 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
109 .WillOnce(Invoke(capture_network_id));
110
111 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
112 net::NetworkChangeNotifier::CONNECTION_WIFI);
113 base::RunLoop().RunUntilIdle();
114
115 std::string wifi_network_id = current_network_id;
116 fake_network_info = fake_ethernet_info;
117 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
118 .WillOnce(Invoke(capture_network_id));
119
120 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
121 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
122 base::RunLoop().RunUntilIdle();
123
124 EXPECT_EQ(ethernet_network_id, current_network_id);
125 EXPECT_NE(ethernet_network_id, wifi_network_id);
126
127 discovery_network_monitor->RemoveObserver(&mock_observer);
128 }
129
130 TEST_F(DiscoveryNetworkMonitorTest, RemoveObserverStopsNotifications) {
131 fake_network_info = fake_ethernet_info;
132
133 discovery_network_monitor->AddObserver(&mock_observer);
134 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
135 .WillOnce(Invoke([](const DiscoveryNetworkMonitor& monitor) {
136 DCHECK_CURRENTLY_ON(BrowserThread::IO);
137 }));
138
139 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
140 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
141 base::RunLoop().RunUntilIdle();
142
143 discovery_network_monitor->RemoveObserver(&mock_observer);
144 fake_network_info.clear();
145
146 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
147 net::NetworkChangeNotifier::CONNECTION_NONE);
148 base::RunLoop().RunUntilIdle();
149 }
150
151 TEST_F(DiscoveryNetworkMonitorTest, RefreshIndependentOfChangeObserver) {
152 fake_network_info = fake_ethernet_info;
153
154 discovery_network_monitor->AddObserver(&mock_observer);
155 auto force_refresh_callback = []() {
156 DiscoveryNetworkMonitor* discovery_network_monitor =
157 DiscoveryNetworkMonitor::GetInstance();
158 EXPECT_NE(std::string("disconnected"),
159 discovery_network_monitor->GetNetworkId());
160 EXPECT_NE(std::string("unknown"),
161 discovery_network_monitor->GetNetworkId());
162 };
163
164 EXPECT_EQ(std::string("disconnected"),
165 discovery_network_monitor->GetNetworkId());
166 discovery_network_monitor->Refresh(base::Bind(force_refresh_callback));
167 base::RunLoop().RunUntilIdle();
168 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698