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

Side by Side Diff: chrome/browser/net/discovery_network_monitor_unittest.cc

Issue 2750453002: Add DiscoveryNetworkMonitor implementation (Closed)
Patch Set: Created 3 years, 9 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/net/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
47 static bool should_rebind_to_network_change_mock;
48
49 content::TestBrowserThreadBundle test_browser_thread_bundle;
50 MockDiscoveryObserver mock_observer;
51
52 std::vector<NetworkInfo> fake_ethernet_info{
53 {{0, net::NetworkChangeNotifier::CONNECTION_ETHERNET,
54 std::string("enp0s2"), std::string("ethernet1"),
55 net::IPAddress({10, 0, 0, 2})}}};
56 std::vector<NetworkInfo> fake_wifi_info{
57 {{0, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s0"),
58 std::string("wifi1"), net::IPAddress({10, 0, 0, 2})},
59 {1, net::NetworkChangeNotifier::CONNECTION_WIFI, std::string("wlp3s1"),
60 std::string("wifi2"), net::IPAddress({10, 0, 1, 2})}}};
61
62 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier =
63 base::WrapUnique(net::NetworkChangeNotifier::CreateMock());
64
65 std::vector<NetworkInfo> fake_network_info;
66 DiscoveryNetworkMonitor* discovery_network_monitor =
67 DiscoveryNetworkMonitor::GetInstanceForTest(
68 base::Bind(&FakeGetNetworkInfo, base::ConstRef(fake_network_info)));
69 };
70
71 bool DiscoveryNetworkMonitorTest::should_rebind_to_network_change_mock = false;
72
73 } // namespace
74
75 TEST_F(DiscoveryNetworkMonitorTest, NetworkIdIsConsistent) {
76 fake_network_info = fake_ethernet_info;
77 std::string current_network_id;
78
79 auto capture_network_id =
80 [&current_network_id](const DiscoveryNetworkMonitor& monitor) {
81 DCHECK_CURRENTLY_ON(BrowserThread::IO);
82 current_network_id = monitor.GetNetworkId();
83 };
84 discovery_network_monitor->AddObserver(&mock_observer);
85 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
86 .WillOnce(Invoke(capture_network_id));
87
88 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
89 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
90 base::RunLoop().RunUntilIdle();
91
92 std::string ethernet_network_id = current_network_id;
93
94 fake_network_info.clear();
95 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
96 .WillOnce(Invoke(capture_network_id));
97
98 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
99 net::NetworkChangeNotifier::CONNECTION_NONE);
100 base::RunLoop().RunUntilIdle();
101
102 fake_network_info = fake_wifi_info;
103 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
104 .WillOnce(Invoke(capture_network_id));
105
106 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
107 net::NetworkChangeNotifier::CONNECTION_WIFI);
108 base::RunLoop().RunUntilIdle();
109
110 std::string wifi_network_id = current_network_id;
111 fake_network_info = fake_ethernet_info;
112 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
113 .WillOnce(Invoke(capture_network_id));
114
115 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
116 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
117 base::RunLoop().RunUntilIdle();
118
119 EXPECT_EQ(ethernet_network_id, current_network_id);
120 EXPECT_NE(ethernet_network_id, wifi_network_id);
121
122 discovery_network_monitor->RemoveObserver(&mock_observer);
123 }
124
125 TEST_F(DiscoveryNetworkMonitorTest, RemoveObserverStopsNotifications) {
126 fake_network_info = fake_ethernet_info;
127
128 discovery_network_monitor->AddObserver(&mock_observer);
129 EXPECT_CALL(mock_observer, OnNetworksChanged(_))
130 .WillOnce(Invoke([](const DiscoveryNetworkMonitor& monitor) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 }));
133
134 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
135 net::NetworkChangeNotifier::CONNECTION_ETHERNET);
136 base::RunLoop().RunUntilIdle();
137
138 discovery_network_monitor->RemoveObserver(&mock_observer);
139 fake_network_info.clear();
140
141 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
142 net::NetworkChangeNotifier::CONNECTION_NONE);
143 base::RunLoop().RunUntilIdle();
144 }
145
146 TEST_F(DiscoveryNetworkMonitorTest,
147 ForceNetworkInfoRefreshIndependentOfChangeObserver) {
148 fake_network_info = fake_ethernet_info;
149
150 discovery_network_monitor->AddObserver(&mock_observer);
151 auto force_refresh_callback = []() {
152 DiscoveryNetworkMonitor* discovery_network_monitor =
153 DiscoveryNetworkMonitor::GetInstance();
154 EXPECT_NE(std::string("disconnected"),
155 discovery_network_monitor->GetNetworkId());
156 EXPECT_NE(std::string("unknown"),
157 discovery_network_monitor->GetNetworkId());
158 };
159
160 EXPECT_EQ(std::string("disconnected"),
161 discovery_network_monitor->GetNetworkId());
162 discovery_network_monitor->ForceNetworkInfoRefresh(
163 base::Bind(force_refresh_callback));
164 base::RunLoop().RunUntilIdle();
165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698