OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "testing/gmock/include/gmock/gmock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace net { | |
17 | |
18 using testing::_; | |
19 using testing::DoAll; | |
20 using testing::InvokeWithoutArgs; | |
21 using testing::Return; | |
22 using testing::SaveArg; | |
23 | |
24 class NetworkChangeNotifierLinuxTest : public testing::Test { | |
25 protected: | |
26 // A subset of the NetworkManager-defined constants used in | |
27 // the tests below. See network_change_notifier_linux.cc | |
28 // for the full list. | |
29 enum { | |
30 NM_STATE_DISCONNECTED = 20, | |
31 NM_STATE_DISCONNECTING = 30, | |
32 NM_STATE_CONNECTED_SITE = 70, | |
33 NM_STATE_CONNECTED_GLOBAL = 70 | |
34 }; | |
35 | |
36 NetworkChangeNotifierLinuxTest() | |
37 : initialized_(false, false) {} | |
38 | |
39 virtual void SetUp() { | |
40 dbus::Bus::Options options; | |
41 options.bus_type = dbus::Bus::SYSTEM; | |
42 mock_bus_ = new dbus::MockBus(options); | |
43 | |
44 mock_object_proxy_ = new dbus::MockObjectProxy(mock_bus_.get(), | |
45 "service_name", | |
46 "service_path"); | |
47 EXPECT_CALL(*mock_bus_, GetObjectProxy(_, _)) | |
48 .WillOnce(Return(mock_object_proxy_.get())); | |
49 | |
50 EXPECT_CALL(*mock_object_proxy_, CallMethod(_, _, _)) | |
51 .WillOnce(SaveArg<2>(&response_callback_)); | |
52 EXPECT_CALL(*mock_object_proxy_, ConnectToSignal(_, _, _, _)) | |
53 .WillOnce( | |
54 DoAll( | |
55 SaveArg<2>(&signal_callback_), | |
56 InvokeWithoutArgs( | |
57 this, | |
58 &NetworkChangeNotifierLinuxTest::Initialize))); | |
59 | |
60 notifier_.reset(NetworkChangeNotifierLinux::CreateForTest(mock_bus_.get())); | |
61 | |
62 initialized_.Wait(); | |
63 } | |
64 | |
65 void Initialize() { | |
66 notifier_thread_proxy_ = base::MessageLoopProxy::current(); | |
67 initialized_.Signal(); | |
68 } | |
69 | |
70 void RunOnNotifierThread(const base::Closure& callback) { | |
71 base::WaitableEvent event(false, false); | |
72 notifier_thread_proxy_->PostTask(FROM_HERE, base::Bind( | |
73 &RunOnNotifierThreadHelper, callback, &event)); | |
74 event.Wait(); | |
75 // Run any tasks queued on the main thread, e.g. by | |
76 // ObserverListThreadSafe. | |
77 MessageLoop::current()->RunAllPending(); | |
78 } | |
79 | |
80 void SendResponse(uint32 state) { | |
81 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
82 dbus::MessageWriter writer(response.get()); | |
83 writer.AppendUint32(state); | |
84 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | |
85 } | |
86 | |
87 void SendSignal(uint32 state) { | |
88 dbus::Signal signal("org.freedesktop.NetworkManager", "StateChanged"); | |
89 dbus::MessageWriter writer(&signal); | |
90 writer.AppendUint32(state); | |
91 RunOnNotifierThread(base::Bind(signal_callback_, &signal)); | |
92 } | |
93 | |
94 dbus::ObjectProxy::ResponseCallback response_callback_; | |
95 dbus::ObjectProxy::SignalCallback signal_callback_; | |
96 | |
97 // Allows creating a new NetworkChangeNotifier. Must be created before | |
98 // |notifier_| and destroyed after it to avoid DCHECK failures. | |
99 NetworkChangeNotifier::DisableForTest disable_for_test_; | |
100 scoped_ptr<NetworkChangeNotifier> notifier_; | |
101 | |
102 private: | |
103 static void RunOnNotifierThreadHelper(const base::Closure& callback, | |
104 base::WaitableEvent* event) { | |
105 callback.Run(); | |
106 event->Signal(); | |
107 } | |
108 | |
109 base::WaitableEvent initialized_; | |
110 | |
111 // Valid only after initialized_ is signaled. | |
112 scoped_refptr<base::MessageLoopProxy> notifier_thread_proxy_; | |
113 | |
114 scoped_refptr<dbus::MockBus> mock_bus_; | |
115 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_; | |
116 }; | |
117 | |
118 namespace { | |
119 | |
120 class OfflineObserver : public NetworkChangeNotifier::OnlineStateObserver { | |
121 public: | |
122 OfflineObserver() | |
123 : notification_count(0), | |
124 last_online_value(true) { | |
125 NetworkChangeNotifier::AddOnlineStateObserver(this); | |
126 } | |
127 | |
128 ~OfflineObserver() { | |
129 NetworkChangeNotifier::RemoveOnlineStateObserver(this); | |
130 } | |
131 | |
132 virtual void OnOnlineStateChanged(bool online) OVERRIDE { | |
133 notification_count++; | |
134 last_online_value = online; | |
135 } | |
136 | |
137 int notification_count; | |
138 bool last_online_value; | |
139 }; | |
140 | |
141 TEST_F(NetworkChangeNotifierLinuxTest, Offline) { | |
142 SendResponse(NM_STATE_DISCONNECTED); | |
143 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
144 } | |
145 | |
146 TEST_F(NetworkChangeNotifierLinuxTest, Online) { | |
147 SendResponse(NM_STATE_CONNECTED_GLOBAL); | |
148 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
149 } | |
150 | |
151 TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) { | |
152 OfflineObserver observer; | |
153 | |
154 SendResponse(NM_STATE_DISCONNECTED); | |
155 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
156 EXPECT_EQ(0, observer.notification_count); | |
157 | |
158 SendSignal(NM_STATE_CONNECTED_GLOBAL); | |
159 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
160 EXPECT_EQ(1, observer.notification_count); | |
161 EXPECT_TRUE(observer.last_online_value); | |
162 } | |
163 | |
164 TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) { | |
165 OfflineObserver observer; | |
166 | |
167 SendResponse(NM_STATE_CONNECTED_GLOBAL); | |
168 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
169 EXPECT_EQ(0, observer.notification_count); | |
170 | |
171 SendSignal(NM_STATE_DISCONNECTED); | |
172 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
173 EXPECT_EQ(1, observer.notification_count); | |
174 EXPECT_FALSE(observer.last_online_value); | |
175 | |
176 SendSignal(NM_STATE_CONNECTED_GLOBAL); | |
177 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
178 EXPECT_EQ(2, observer.notification_count); | |
179 EXPECT_TRUE(observer.last_online_value); | |
180 } | |
181 | |
182 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) { | |
183 OfflineObserver observer; | |
184 | |
185 SendResponse(NM_STATE_CONNECTED_SITE); | |
186 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
187 EXPECT_EQ(0, observer.notification_count); | |
188 | |
189 SendSignal(NM_STATE_CONNECTED_GLOBAL); | |
190 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
191 EXPECT_EQ(0, observer.notification_count); | |
192 } | |
193 | |
194 TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) { | |
195 OfflineObserver observer; | |
196 | |
197 SendResponse(NM_STATE_DISCONNECTING); | |
198 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
199 EXPECT_EQ(0, observer.notification_count); | |
200 | |
201 SendSignal(NM_STATE_DISCONNECTED); | |
202 EXPECT_TRUE(NetworkChangeNotifier::IsOffline()); | |
203 EXPECT_EQ(0, observer.notification_count); | |
204 } | |
205 | |
206 TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) { | |
207 RunOnNotifierThread(base::Bind( | |
208 response_callback_, static_cast<dbus::Response*>(NULL))); | |
209 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
210 } | |
211 | |
212 TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) { | |
213 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
214 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | |
215 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
216 } | |
217 | |
218 TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) { | |
219 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | |
220 dbus::MessageWriter writer(response.get()); | |
221 writer.AppendUint16(20); // Uint16 instead of the expected Uint32 | |
222 RunOnNotifierThread(base::Bind(response_callback_, response.get())); | |
223 EXPECT_FALSE(NetworkChangeNotifier::IsOffline()); | |
224 } | |
225 | |
226 } // namespace | |
227 } // namespace net | |
OLD | NEW |