Only fsync leveldb's directory when the manifest is being updated.
[chromium-blink-merge.git] / chromeos / dbus / shill_device_client_unittest.cc
blobd8f127a7e4319495dbacd9a1776ee7d41ad639a3
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.
5 #include "base/bind.h"
6 #include "base/values.h"
7 #include "chromeos/dbus/shill_client_unittest_base.h"
8 #include "chromeos/dbus/shill_device_client.h"
9 #include "dbus/message.h"
10 #include "dbus/object_path.h"
11 #include "dbus/values_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
15 using testing::_;
16 using testing::ByRef;
18 namespace chromeos {
20 namespace {
22 const char kExampleDevicePath[] = "/foo/bar";
24 // Expects the reader to have a string and a bool.
25 void ExpectStringAndBoolArguments(const std::string& expected_string,
26 bool expected_bool,
27 dbus::MessageReader* reader) {
28 std::string arg1;
29 ASSERT_TRUE(reader->PopString(&arg1));
30 EXPECT_EQ(expected_string, arg1);
31 bool arg2 = false;
32 ASSERT_TRUE(reader->PopBool(&arg2));
33 EXPECT_EQ(expected_bool, arg2);
34 EXPECT_FALSE(reader->HasMoreData());
37 // Expects the reader to have two strings.
38 void ExpectTwoStringArguments(const std::string& expected_string1,
39 const std::string& expected_string2,
40 dbus::MessageReader* reader) {
41 std::string arg1;
42 ASSERT_TRUE(reader->PopString(&arg1));
43 EXPECT_EQ(expected_string1, arg1);
44 std::string arg2;
45 ASSERT_TRUE(reader->PopString(&arg2));
46 EXPECT_EQ(expected_string2, arg2);
47 EXPECT_FALSE(reader->HasMoreData());
50 } // namespace
52 class ShillDeviceClientTest : public ShillClientUnittestBase {
53 public:
54 ShillDeviceClientTest()
55 : ShillClientUnittestBase(flimflam::kFlimflamDeviceInterface,
56 dbus::ObjectPath(kExampleDevicePath)) {
59 virtual void SetUp() {
60 ShillClientUnittestBase::SetUp();
61 // Create a client with the mock bus.
62 client_.reset(ShillDeviceClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION,
63 mock_bus_));
64 // Run the message loop to run the signal connection result callback.
65 message_loop_.RunUntilIdle();
68 virtual void TearDown() {
69 ShillClientUnittestBase::TearDown();
72 protected:
73 scoped_ptr<ShillDeviceClient> client_;
76 TEST_F(ShillDeviceClientTest, PropertyChanged) {
77 const bool kValue = true;
78 // Create a signal.
79 dbus::Signal signal(flimflam::kFlimflamDeviceInterface,
80 flimflam::kMonitorPropertyChanged);
81 dbus::MessageWriter writer(&signal);
82 writer.AppendString(flimflam::kCellularAllowRoamingProperty);
83 writer.AppendVariantOfBool(kValue);
85 // Set expectations.
86 const base::FundamentalValue value(kValue);
87 MockPropertyChangeObserver observer;
88 EXPECT_CALL(observer,
89 OnPropertyChanged(
90 flimflam::kCellularAllowRoamingProperty,
91 ValueEq(ByRef(value)))).Times(1);
93 // Add the observer
94 client_->AddPropertyChangedObserver(
95 dbus::ObjectPath(kExampleDevicePath),
96 &observer);
98 // Run the signal callback.
99 SendPropertyChangedSignal(&signal);
101 // Remove the observer.
102 client_->RemovePropertyChangedObserver(
103 dbus::ObjectPath(kExampleDevicePath),
104 &observer);
106 EXPECT_CALL(observer,
107 OnPropertyChanged(
108 flimflam::kCellularAllowRoamingProperty,
109 ValueEq(ByRef(value)))).Times(0);
111 // Run the signal callback again and make sure the observer isn't called.
112 SendPropertyChangedSignal(&signal);
115 TEST_F(ShillDeviceClientTest, GetProperties) {
116 const bool kValue = true;
117 // Create response.
118 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
119 dbus::MessageWriter writer(response.get());
120 dbus::MessageWriter array_writer(NULL);
121 writer.OpenArray("{sv}", &array_writer);
122 dbus::MessageWriter entry_writer(NULL);
123 array_writer.OpenDictEntry(&entry_writer);
124 entry_writer.AppendString(flimflam::kCellularAllowRoamingProperty);
125 entry_writer.AppendVariantOfBool(kValue);
126 array_writer.CloseContainer(&entry_writer);
127 writer.CloseContainer(&array_writer);
129 // Set expectations.
130 base::DictionaryValue value;
131 value.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty,
132 base::Value::CreateBooleanValue(kValue));
133 PrepareForMethodCall(flimflam::kGetPropertiesFunction,
134 base::Bind(&ExpectNoArgument),
135 response.get());
136 // Call method.
137 client_->GetProperties(dbus::ObjectPath(kExampleDevicePath),
138 base::Bind(&ExpectDictionaryValueResult, &value));
139 // Run the message loop.
140 message_loop_.RunUntilIdle();
143 TEST_F(ShillDeviceClientTest, CallGetPropertiesAndBlock) {
144 const bool kValue = true;
145 // Create response.
146 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
147 dbus::MessageWriter writer(response.get());
148 dbus::MessageWriter array_writer(NULL);
149 writer.OpenArray("{sv}", &array_writer);
150 dbus::MessageWriter entry_writer(NULL);
151 array_writer.OpenDictEntry(&entry_writer);
152 entry_writer.AppendString(flimflam::kCellularAllowRoamingProperty);
153 entry_writer.AppendVariantOfBool(kValue);
154 array_writer.CloseContainer(&entry_writer);
155 writer.CloseContainer(&array_writer);
157 // Set expectations.
158 base::DictionaryValue value;
159 value.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty,
160 base::Value::CreateBooleanValue(kValue));
161 PrepareForMethodCall(flimflam::kGetPropertiesFunction,
162 base::Bind(&ExpectNoArgument),
163 response.get());
164 // Call method.
165 scoped_ptr<base::DictionaryValue> result(
166 client_->CallGetPropertiesAndBlock(dbus::ObjectPath(kExampleDevicePath)));
167 ASSERT_TRUE(result.get());
168 EXPECT_TRUE(result->Equals(&value));
171 TEST_F(ShillDeviceClientTest, ProposeScan) {
172 // Create response.
173 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
175 // Set expectations.
176 PrepareForMethodCall(flimflam::kProposeScanFunction,
177 base::Bind(&ExpectNoArgument),
178 response.get());
179 // Call method.
180 client_->ProposeScan(dbus::ObjectPath(kExampleDevicePath),
181 base::Bind(&ExpectNoResultValue));
182 // Run the message loop.
183 message_loop_.RunUntilIdle();
186 TEST_F(ShillDeviceClientTest, SetProperty) {
187 const bool kValue = true;
188 // Create response.
189 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
191 // Set expectations.
192 const base::FundamentalValue value(kValue);
193 PrepareForMethodCall(flimflam::kSetPropertyFunction,
194 base::Bind(&ExpectStringAndValueArguments,
195 flimflam::kCellularAllowRoamingProperty,
196 &value),
197 response.get());
198 // Call method.
199 MockClosure mock_closure;
200 MockErrorCallback mock_error_callback;
201 client_->SetProperty(dbus::ObjectPath(kExampleDevicePath),
202 flimflam::kCellularAllowRoamingProperty,
203 value,
204 mock_closure.GetCallback(),
205 mock_error_callback.GetCallback());
206 EXPECT_CALL(mock_closure, Run()).Times(1);
207 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
209 // Run the message loop.
210 message_loop_.RunUntilIdle();
213 TEST_F(ShillDeviceClientTest, ClearProperty) {
214 // Create response.
215 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
217 // Set expectations.
218 PrepareForMethodCall(flimflam::kClearPropertyFunction,
219 base::Bind(&ExpectStringArgument,
220 flimflam::kCellularAllowRoamingProperty),
221 response.get());
222 // Call method.
223 client_->ClearProperty(dbus::ObjectPath(kExampleDevicePath),
224 flimflam::kCellularAllowRoamingProperty,
225 base::Bind(&ExpectNoResultValue));
226 // Run the message loop.
227 message_loop_.RunUntilIdle();
230 TEST_F(ShillDeviceClientTest, AddIPConfig) {
231 const dbus::ObjectPath expected_result("/result/path");
232 // Create response.
233 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
234 dbus::MessageWriter writer(response.get());
235 writer.AppendObjectPath(expected_result);
237 // Set expectations.
238 PrepareForMethodCall(flimflam::kAddIPConfigFunction,
239 base::Bind(&ExpectStringArgument, flimflam::kTypeDHCP),
240 response.get());
241 // Call method.
242 client_->AddIPConfig(dbus::ObjectPath(kExampleDevicePath),
243 flimflam::kTypeDHCP,
244 base::Bind(&ExpectObjectPathResult, expected_result));
245 // Run the message loop.
246 message_loop_.RunUntilIdle();
249 TEST_F(ShillDeviceClientTest, RequirePin) {
250 const char kPin[] = "123456";
251 const bool kRequired = true;
252 // Create response.
253 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
255 // Set expectations.
256 MockClosure mock_closure;
257 MockErrorCallback mock_error_callback;
258 PrepareForMethodCall(flimflam::kRequirePinFunction,
259 base::Bind(&ExpectStringAndBoolArguments,
260 kPin,
261 kRequired),
262 response.get());
263 EXPECT_CALL(mock_closure, Run()).Times(1);
264 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
265 // Call method.
266 client_->RequirePin(dbus::ObjectPath(kExampleDevicePath),
267 kPin,
268 kRequired,
269 mock_closure.GetCallback(),
270 mock_error_callback.GetCallback());
271 // Run the message loop.
272 message_loop_.RunUntilIdle();
275 TEST_F(ShillDeviceClientTest, EnterPin) {
276 const char kPin[] = "123456";
277 // Create response.
278 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
280 // Set expectations.
281 MockClosure mock_closure;
282 MockErrorCallback mock_error_callback;
283 PrepareForMethodCall(flimflam::kEnterPinFunction,
284 base::Bind(&ExpectStringArgument,
285 kPin),
286 response.get());
287 EXPECT_CALL(mock_closure, Run()).Times(1);
288 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
290 // Call method.
291 client_->EnterPin(dbus::ObjectPath(kExampleDevicePath),
292 kPin,
293 mock_closure.GetCallback(),
294 mock_error_callback.GetCallback());
295 // Run the message loop.
296 message_loop_.RunUntilIdle();
299 TEST_F(ShillDeviceClientTest, UnblockPin) {
300 const char kPuk[] = "987654";
301 const char kPin[] = "123456";
302 // Create response.
303 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
305 // Set expectations.
306 MockClosure mock_closure;
307 MockErrorCallback mock_error_callback;
308 PrepareForMethodCall(flimflam::kUnblockPinFunction,
309 base::Bind(&ExpectTwoStringArguments, kPuk, kPin),
310 response.get());
311 EXPECT_CALL(mock_closure, Run()).Times(1);
312 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
314 // Call method.
315 client_->UnblockPin(dbus::ObjectPath(kExampleDevicePath),
316 kPuk,
317 kPin,
318 mock_closure.GetCallback(),
319 mock_error_callback.GetCallback());
320 // Run the message loop.
321 message_loop_.RunUntilIdle();
324 TEST_F(ShillDeviceClientTest, ChangePin) {
325 const char kOldPin[] = "123456";
326 const char kNewPin[] = "234567";
327 // Create response.
328 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
330 // Set expectations.
331 MockClosure mock_closure;
332 MockErrorCallback mock_error_callback;
333 PrepareForMethodCall(flimflam::kChangePinFunction,
334 base::Bind(&ExpectTwoStringArguments,
335 kOldPin,
336 kNewPin),
337 response.get());
338 EXPECT_CALL(mock_closure, Run()).Times(1);
339 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
341 // Call method.
342 client_->ChangePin(dbus::ObjectPath(kExampleDevicePath),
343 kOldPin,
344 kNewPin,
345 mock_closure.GetCallback(),
346 mock_error_callback.GetCallback());
347 // Run the message loop.
348 message_loop_.RunUntilIdle();
351 TEST_F(ShillDeviceClientTest, Register) {
352 const char kNetworkId[] = "networkid";
353 // Create response.
354 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
356 // Set expectations.
357 MockClosure mock_closure;
358 MockErrorCallback mock_error_callback;
359 PrepareForMethodCall(flimflam::kRegisterFunction,
360 base::Bind(&ExpectStringArgument, kNetworkId),
361 response.get());
362 EXPECT_CALL(mock_closure, Run()).Times(1);
363 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
365 // Call method.
366 client_->Register(dbus::ObjectPath(kExampleDevicePath),
367 kNetworkId,
368 mock_closure.GetCallback(),
369 mock_error_callback.GetCallback());
370 // Run the message loop.
371 message_loop_.RunUntilIdle();
374 TEST_F(ShillDeviceClientTest, SetCarrier) {
375 const char kCarrier[] = "carrier";
376 // Create response.
377 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
379 // Set expectations.
380 MockClosure mock_closure;
381 MockErrorCallback mock_error_callback;
382 PrepareForMethodCall(shill::kSetCarrierFunction,
383 base::Bind(&ExpectStringArgument, kCarrier),
384 response.get());
385 EXPECT_CALL(mock_closure, Run()).Times(1);
386 // Call method.
387 client_->SetCarrier(dbus::ObjectPath(kExampleDevicePath),
388 kCarrier,
389 mock_closure.GetCallback(),
390 mock_error_callback.GetCallback());
391 // Run the message loop.
392 message_loop_.RunUntilIdle();
395 TEST_F(ShillDeviceClientTest, Reset) {
396 // Create response.
397 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
399 // Set expectations.
400 MockClosure mock_closure;
401 MockErrorCallback mock_error_callback;
402 PrepareForMethodCall(shill::kResetFunction,
403 base::Bind(&ExpectNoArgument),
404 response.get());
405 EXPECT_CALL(mock_closure, Run()).Times(1);
406 // Call method.
407 client_->Reset(dbus::ObjectPath(kExampleDevicePath),
408 mock_closure.GetCallback(),
409 mock_error_callback.GetCallback());
410 // Run the message loop.
411 message_loop_.RunUntilIdle();
414 } // namespace chromeos