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