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.
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"
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
,
27 dbus::MessageReader
* reader
) {
29 ASSERT_TRUE(reader
->PopString(&arg1
));
30 EXPECT_EQ(expected_string
, arg1
);
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
) {
42 ASSERT_TRUE(reader
->PopString(&arg1
));
43 EXPECT_EQ(expected_string1
, arg1
);
45 ASSERT_TRUE(reader
->PopString(&arg2
));
46 EXPECT_EQ(expected_string2
, arg2
);
47 EXPECT_FALSE(reader
->HasMoreData());
52 class ShillDeviceClientTest
: public ShillClientUnittestBase
{
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
,
64 // Run the message loop to run the signal connection result callback.
65 message_loop_
.RunUntilIdle();
68 virtual void TearDown() {
69 ShillClientUnittestBase::TearDown();
73 scoped_ptr
<ShillDeviceClient
> client_
;
76 TEST_F(ShillDeviceClientTest
, PropertyChanged
) {
77 const bool kValue
= true;
79 dbus::Signal
signal(flimflam::kFlimflamDeviceInterface
,
80 flimflam::kMonitorPropertyChanged
);
81 dbus::MessageWriter
writer(&signal
);
82 writer
.AppendString(flimflam::kCellularAllowRoamingProperty
);
83 writer
.AppendVariantOfBool(kValue
);
86 const base::FundamentalValue
value(kValue
);
87 MockPropertyChangeObserver observer
;
90 flimflam::kCellularAllowRoamingProperty
,
91 ValueEq(ByRef(value
)))).Times(1);
94 client_
->AddPropertyChangedObserver(
95 dbus::ObjectPath(kExampleDevicePath
),
98 // Run the signal callback.
99 SendPropertyChangedSignal(&signal
);
101 // Remove the observer.
102 client_
->RemovePropertyChangedObserver(
103 dbus::ObjectPath(kExampleDevicePath
),
106 EXPECT_CALL(observer
,
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;
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
);
130 base::DictionaryValue value
;
131 value
.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty
,
132 base::Value::CreateBooleanValue(kValue
));
133 PrepareForMethodCall(flimflam::kGetPropertiesFunction
,
134 base::Bind(&ExpectNoArgument
),
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;
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
);
158 base::DictionaryValue value
;
159 value
.SetWithoutPathExpansion(flimflam::kCellularAllowRoamingProperty
,
160 base::Value::CreateBooleanValue(kValue
));
161 PrepareForMethodCall(flimflam::kGetPropertiesFunction
,
162 base::Bind(&ExpectNoArgument
),
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
) {
173 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
176 PrepareForMethodCall(flimflam::kProposeScanFunction
,
177 base::Bind(&ExpectNoArgument
),
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;
189 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
192 const base::FundamentalValue
value(kValue
);
193 PrepareForMethodCall(flimflam::kSetPropertyFunction
,
194 base::Bind(&ExpectStringAndValueArguments
,
195 flimflam::kCellularAllowRoamingProperty
,
199 MockClosure mock_closure
;
200 MockErrorCallback mock_error_callback
;
201 client_
->SetProperty(dbus::ObjectPath(kExampleDevicePath
),
202 flimflam::kCellularAllowRoamingProperty
,
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
) {
215 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
218 PrepareForMethodCall(flimflam::kClearPropertyFunction
,
219 base::Bind(&ExpectStringArgument
,
220 flimflam::kCellularAllowRoamingProperty
),
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");
233 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
234 dbus::MessageWriter
writer(response
.get());
235 writer
.AppendObjectPath(expected_result
);
238 PrepareForMethodCall(flimflam::kAddIPConfigFunction
,
239 base::Bind(&ExpectStringArgument
, flimflam::kTypeDHCP
),
242 client_
->AddIPConfig(dbus::ObjectPath(kExampleDevicePath
),
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;
253 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
256 MockClosure mock_closure
;
257 MockErrorCallback mock_error_callback
;
258 PrepareForMethodCall(flimflam::kRequirePinFunction
,
259 base::Bind(&ExpectStringAndBoolArguments
,
263 EXPECT_CALL(mock_closure
, Run()).Times(1);
264 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
266 client_
->RequirePin(dbus::ObjectPath(kExampleDevicePath
),
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";
278 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
281 MockClosure mock_closure
;
282 MockErrorCallback mock_error_callback
;
283 PrepareForMethodCall(flimflam::kEnterPinFunction
,
284 base::Bind(&ExpectStringArgument
,
287 EXPECT_CALL(mock_closure
, Run()).Times(1);
288 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
291 client_
->EnterPin(dbus::ObjectPath(kExampleDevicePath
),
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";
303 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
306 MockClosure mock_closure
;
307 MockErrorCallback mock_error_callback
;
308 PrepareForMethodCall(flimflam::kUnblockPinFunction
,
309 base::Bind(&ExpectTwoStringArguments
, kPuk
, kPin
),
311 EXPECT_CALL(mock_closure
, Run()).Times(1);
312 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
315 client_
->UnblockPin(dbus::ObjectPath(kExampleDevicePath
),
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";
328 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
331 MockClosure mock_closure
;
332 MockErrorCallback mock_error_callback
;
333 PrepareForMethodCall(flimflam::kChangePinFunction
,
334 base::Bind(&ExpectTwoStringArguments
,
338 EXPECT_CALL(mock_closure
, Run()).Times(1);
339 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
342 client_
->ChangePin(dbus::ObjectPath(kExampleDevicePath
),
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";
354 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
357 MockClosure mock_closure
;
358 MockErrorCallback mock_error_callback
;
359 PrepareForMethodCall(flimflam::kRegisterFunction
,
360 base::Bind(&ExpectStringArgument
, kNetworkId
),
362 EXPECT_CALL(mock_closure
, Run()).Times(1);
363 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
366 client_
->Register(dbus::ObjectPath(kExampleDevicePath
),
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";
377 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
380 MockClosure mock_closure
;
381 MockErrorCallback mock_error_callback
;
382 PrepareForMethodCall(shill::kSetCarrierFunction
,
383 base::Bind(&ExpectStringArgument
, kCarrier
),
385 EXPECT_CALL(mock_closure
, Run()).Times(1);
387 client_
->SetCarrier(dbus::ObjectPath(kExampleDevicePath
),
389 mock_closure
.GetCallback(),
390 mock_error_callback
.GetCallback());
391 // Run the message loop.
392 message_loop_
.RunUntilIdle();
395 TEST_F(ShillDeviceClientTest
, Reset
) {
397 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
400 MockClosure mock_closure
;
401 MockErrorCallback mock_error_callback
;
402 PrepareForMethodCall(shill::kResetFunction
,
403 base::Bind(&ExpectNoArgument
),
405 EXPECT_CALL(mock_closure
, Run()).Times(1);
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