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

Side by Side Diff: net/base/network_change_notifier_linux_unittest.cc

Issue 11359141: Use Netlink instead of NetworkManager (via D-bus) to monitor network (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address szym's second round of unittest comments Created 8 years 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 | Annotate | Revision Log
« no previous file with comments | « net/base/network_change_notifier_linux.cc ('k') | net/net.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "net/base/network_change_notifier_linux.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop_proxy.h"
9 #include "base/synchronization/waitable_event.h"
10 #include "dbus/mock_bus.h"
11 #include "dbus/mock_object_proxy.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace net {
18
19 using testing::_;
20 using testing::DoAll;
21 using testing::InvokeWithoutArgs;
22 using testing::Return;
23 using testing::SaveArg;
24
25 class NetworkChangeNotifierLinuxTest : public testing::Test {
26 protected:
27 // A subset of the NetworkManager-defined constants used in
28 // the tests below. See network_change_notifier_linux.cc
29 // for the full list.
30 enum {
31 NM_STATE_DISCONNECTED = 20,
32 NM_STATE_DISCONNECTING = 30,
33 NM_STATE_CONNECTED_SITE = 70,
34 NM_STATE_CONNECTED_GLOBAL = 70
35 };
36
37 NetworkChangeNotifierLinuxTest()
38 : initialized_(false, false) {}
39
40 virtual void SetUp() {
41 dbus::Bus::Options options;
42 options.bus_type = dbus::Bus::SYSTEM;
43 mock_bus_ = new dbus::MockBus(options);
44
45 mock_object_proxy_ = new dbus::MockObjectProxy(
46 mock_bus_.get(),
47 "service_name",
48 dbus::ObjectPath("service_path"));
49 EXPECT_CALL(*mock_bus_, GetObjectProxyWithOptions(_, _, _))
50 .WillOnce(Return(mock_object_proxy_.get()));
51
52 EXPECT_CALL(*mock_object_proxy_, CallMethod(_, _, _))
53 .WillOnce(SaveArg<2>(&response_callback_));
54 EXPECT_CALL(*mock_object_proxy_, ConnectToSignal(_, _, _, _))
55 .WillOnce(
56 DoAll(
57 SaveArg<2>(&signal_callback_),
58 InvokeWithoutArgs(
59 this,
60 &NetworkChangeNotifierLinuxTest::Initialize)));
61
62 notifier_.reset(NetworkChangeNotifierLinux::CreateForTest(mock_bus_.get()));
63
64 initialized_.Wait();
65 }
66
67 void Initialize() {
68 notifier_thread_proxy_ = base::MessageLoopProxy::current();
69 initialized_.Signal();
70 }
71
72 void RunOnNotifierThread(const base::Closure& callback) {
73 base::WaitableEvent event(false, false);
74 notifier_thread_proxy_->PostTask(FROM_HERE, base::Bind(
75 &RunOnNotifierThreadHelper, callback, &event));
76 event.Wait();
77 // Run any tasks queued on the main thread, e.g. by
78 // ObserverListThreadSafe.
79 MessageLoop::current()->RunUntilIdle();
80 }
81
82 void SendResponse(uint32 state) {
83 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
84 dbus::MessageWriter writer(response.get());
85 writer.AppendVariantOfUint32(state);
86 RunOnNotifierThread(base::Bind(response_callback_, response.get()));
87 }
88
89 void SendSignal(uint32 state) {
90 dbus::Signal signal("org.freedesktop.NetworkManager", "StateChanged");
91 dbus::MessageWriter writer(&signal);
92 writer.AppendUint32(state);
93 RunOnNotifierThread(base::Bind(signal_callback_, &signal));
94 }
95
96 dbus::ObjectProxy::ResponseCallback response_callback_;
97 dbus::ObjectProxy::SignalCallback signal_callback_;
98
99 // Allows creating a new NetworkChangeNotifier. Must be created before
100 // |notifier_| and destroyed after it to avoid DCHECK failures.
101 NetworkChangeNotifier::DisableForTest disable_for_test_;
102 scoped_ptr<NetworkChangeNotifier> notifier_;
103
104 private:
105 static void RunOnNotifierThreadHelper(const base::Closure& callback,
106 base::WaitableEvent* event) {
107 callback.Run();
108 event->Signal();
109 }
110
111 base::WaitableEvent initialized_;
112
113 // Valid only after initialized_ is signaled.
114 scoped_refptr<base::MessageLoopProxy> notifier_thread_proxy_;
115
116 scoped_refptr<dbus::MockBus> mock_bus_;
117 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_;
118 };
119
120 namespace {
121
122 class OfflineObserver : public NetworkChangeNotifier::ConnectionTypeObserver {
123 public:
124 OfflineObserver()
125 : notification_count(0),
126 last_online_value(true) {
127 NetworkChangeNotifier::AddConnectionTypeObserver(this);
128 }
129
130 ~OfflineObserver() {
131 NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
132 }
133
134 virtual void OnConnectionTypeChanged(
135 NetworkChangeNotifier::ConnectionType type) OVERRIDE {
136 notification_count++;
137 last_online_value = type != NetworkChangeNotifier::CONNECTION_NONE;
138 }
139
140 int notification_count;
141 bool last_online_value;
142 };
143
144 TEST_F(NetworkChangeNotifierLinuxTest, Offline) {
145 SendResponse(NM_STATE_DISCONNECTED);
146 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
147 NetworkChangeNotifier::GetConnectionType());
148 }
149
150 TEST_F(NetworkChangeNotifierLinuxTest, Online) {
151 SendResponse(NM_STATE_CONNECTED_GLOBAL);
152 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
153 NetworkChangeNotifier::GetConnectionType());}
154
155 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) {
156 OfflineObserver observer;
157
158 SendResponse(NM_STATE_DISCONNECTED);
159 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
160 NetworkChangeNotifier::GetConnectionType());
161 EXPECT_EQ(0, observer.notification_count);
162
163 SendSignal(NM_STATE_CONNECTED_GLOBAL);
164 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
165 NetworkChangeNotifier::GetConnectionType());
166 EXPECT_EQ(1, observer.notification_count);
167 EXPECT_TRUE(observer.last_online_value);
168 }
169
170 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) {
171 OfflineObserver observer;
172
173 SendResponse(NM_STATE_CONNECTED_GLOBAL);
174 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
175 NetworkChangeNotifier::GetConnectionType());
176 EXPECT_EQ(0, observer.notification_count);
177
178 SendSignal(NM_STATE_DISCONNECTED);
179 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
180 NetworkChangeNotifier::GetConnectionType());
181 EXPECT_EQ(1, observer.notification_count);
182 EXPECT_FALSE(observer.last_online_value);
183
184 SendSignal(NM_STATE_CONNECTED_GLOBAL);
185 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
186 NetworkChangeNotifier::GetConnectionType());
187 EXPECT_EQ(2, observer.notification_count);
188 EXPECT_TRUE(observer.last_online_value);
189 }
190
191 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) {
192 OfflineObserver observer;
193
194 SendResponse(NM_STATE_CONNECTED_SITE);
195 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
196 NetworkChangeNotifier::GetConnectionType());
197 EXPECT_EQ(0, observer.notification_count);
198
199 SendSignal(NM_STATE_CONNECTED_GLOBAL);
200 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
201 NetworkChangeNotifier::GetConnectionType());
202 EXPECT_EQ(0, observer.notification_count);
203 }
204
205 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) {
206 OfflineObserver observer;
207
208 SendResponse(NM_STATE_DISCONNECTING);
209 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
210 NetworkChangeNotifier::GetConnectionType());
211 EXPECT_EQ(0, observer.notification_count);
212
213 SendSignal(NM_STATE_DISCONNECTED);
214 EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
215 NetworkChangeNotifier::GetConnectionType());
216 EXPECT_EQ(0, observer.notification_count);
217 }
218
219 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) {
220 RunOnNotifierThread(base::Bind(
221 response_callback_, static_cast<dbus::Response*>(NULL)));
222 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
223 NetworkChangeNotifier::GetConnectionType());
224 }
225
226 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) {
227 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
228 RunOnNotifierThread(base::Bind(response_callback_, response.get()));
229 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
230 NetworkChangeNotifier::GetConnectionType());
231 }
232
233 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) {
234 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
235 dbus::MessageWriter writer(response.get());
236 writer.AppendUint16(20); // Uint16 instead of the expected Uint32
237 RunOnNotifierThread(base::Bind(response_callback_, response.get()));
238 EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
239 NetworkChangeNotifier::GetConnectionType());
240 }
241
242 } // namespace
243 } // namespace net
OLDNEW
« no previous file with comments | « net/base/network_change_notifier_linux.cc ('k') | net/net.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698