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

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

Issue 2750453002: Add DiscoveryNetworkMonitor implementation (Closed)
Patch Set: Respond to imcheng's comments Created 3 years, 6 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 class DiscoveryNetworkMonitorTest : public testing::Test {
26 protected:
27 void SetUp() override {
28 fake_network_info.clear();
29 discovery_network_monitor->SetNetworkInfoFunctionForTest(
30 &FakeGetNetworkInfo);
31
32 // NetworkChangeNotifier provides singleton behavior while simultaneously
imcheng 2017/05/31 21:19:56 Sorry if this has already been discussed -- can we
btolsch 2017/06/05 22:38:33 Done.
33 // allowing the creator to own the instance. This makes it possible to
34 // reset it for each test, however this isn't possible with LazyInstance<>,
35 // which DiscoveryNetworkMonitor uses. It's also not possible to just leave
36 // one NetworkChangeNotifier mock alive for all the tests because the thread
37 // bundle is being recreated in each fixture which causes their task runners
38 // to become different.
39 if (should_rebind_to_network_change_mock) {
40 discovery_network_monitor->RebindNetworkChangeObserverForTest();
41 } else {
42 should_rebind_to_network_change_mock = true;
43 }
44
45 // Clear the network info list and network ID before each test since the
46 // monitor is a singleton.
47 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
48 net::NetworkChangeNotifier::CONNECTION_NONE);
49 base::RunLoop().RunUntilIdle();
50 }
51
52 static std::vector<DiscoveryNetworkInfo> FakeGetNetworkInfo() {
53 return fake_network_info;
54 }
55
56 static bool should_rebind_to_network_change_mock;
57
58 content::TestBrowserThreadBundle test_browser_thread_bundle;
59 MockDiscoveryObserver mock_observer;
60
61 std::vector<DiscoveryNetworkInfo> fake_ethernet_info{
62 {{std::string("enp0s2"), std::string("ethernet1")}}};
63 std::vector<DiscoveryNetworkInfo> fake_wifi_info{
64 {DiscoveryNetworkInfo{std::string("wlp3s0"), std::string("wifi1")},
65 DiscoveryNetworkInfo{std::string("wlp3s1"), std::string("wifi2")}}};
66
67 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier =
68 base::WrapUnique(net::NetworkChangeNotifier::CreateMock());
69
70 static std::vector<DiscoveryNetworkInfo> fake_network_info;
71 DiscoveryNetworkMonitor* discovery_network_monitor =
72 DiscoveryNetworkMonitor::GetInstance();
73 };
74
75 // static
76 bool DiscoveryNetworkMonitorTest::should_rebind_to_network_change_mock = false;
77 // static
78 std::vector<DiscoveryNetworkInfo>
79 DiscoveryNetworkMonitorTest::fake_network_info;
80
81 } // namespace
82
83 TEST_F(DiscoveryNetworkMonitorTest, NetworkIdIsConsistent) {
84 fake_network_info = fake_ethernet_info;
85 std::string current_network_id;
86
87 auto capture_network_id =
88 [&current_network_id](const DiscoveryNetworkMonitor& monitor) {
89 DCHECK_CURRENTLY_ON(BrowserThread::IO);
90 current_network_id = monitor.GetNetworkId();
91 };
92 discovery_network_monitor->AddObserver(&mock_observer);
93 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
94 .WillOnce(Invoke(capture_network_id));
95
96 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
97 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
98 base::RunLoop().RunUntilIdle();
99
100 std::string ethernet_network_id = current_network_id;
101
102 fake_network_info.clear();
103 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
104 .WillOnce(Invoke(capture_network_id));
105
106 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
107 net::NetworkChangeNotifier::CONNECTION_NONE);
108 base::RunLoop().RunUntilIdle();
109
110 fake_network_info = fake_wifi_info;
111 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
112 .WillOnce(Invoke(capture_network_id));
113
114 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
115 net::NetworkChangeNotifier::CONNECTION_WIFI);
116 base::RunLoop().RunUntilIdle();
117
118 std::string wifi_network_id = current_network_id;
119 fake_network_info = fake_ethernet_info;
120 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
121 .WillOnce(Invoke(capture_network_id));
122
123 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
124 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
125 base::RunLoop().RunUntilIdle();
126
127 EXPECT_EQ(ethernet_network_id, current_network_id);
128 EXPECT_NE(ethernet_network_id, wifi_network_id);
129
130 discovery_network_monitor->RemoveObserver(&mock_observer);
131 }
132
133 TEST_F(DiscoveryNetworkMonitorTest, RemoveObserverStopsNotifications) {
134 fake_network_info = fake_ethernet_info;
135
136 discovery_network_monitor->AddObserver(&mock_observer);
137 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
138 .WillOnce(Invoke([](const DiscoveryNetworkMonitor& monitor) {
139 DCHECK_CURRENTLY_ON(BrowserThread::IO);
140 }));
141
142 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
143 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
144 base::RunLoop().RunUntilIdle();
145
146 discovery_network_monitor->RemoveObserver(&mock_observer);
147 fake_network_info.clear();
148
149 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
150 net::NetworkChangeNotifier::CONNECTION_NONE);
151 base::RunLoop().RunUntilIdle();
152 }
153
154 TEST_F(DiscoveryNetworkMonitorTest, RefreshIndependentOfChangeObserver) {
155 fake_network_info = fake_ethernet_info;
156
157 discovery_network_monitor->AddObserver(&mock_observer);
158 auto force_refresh_callback = []() {
159 DiscoveryNetworkMonitor* discovery_network_monitor =
160 DiscoveryNetworkMonitor::GetInstance();
161 EXPECT_NE(std::string(DiscoveryNetworkMonitor::kNetworkIdDisconnected),
162 discovery_network_monitor->GetNetworkId());
163 EXPECT_NE(std::string(DiscoveryNetworkMonitor::kNetworkIdUnknown),
164 discovery_network_monitor->GetNetworkId());
165 };
166
167 EXPECT_EQ(std::string(DiscoveryNetworkMonitor::kNetworkIdDisconnected),
168 discovery_network_monitor->GetNetworkId());
169 discovery_network_monitor->Refresh(base::Bind(force_refresh_callback));
170 base::RunLoop().RunUntilIdle();
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698