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
, ProposeScan
) {
145 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
148 PrepareForMethodCall(flimflam::kProposeScanFunction
,
149 base::Bind(&ExpectNoArgument
),
152 client_
->ProposeScan(dbus::ObjectPath(kExampleDevicePath
),
153 base::Bind(&ExpectNoResultValue
));
154 // Run the message loop.
155 message_loop_
.RunUntilIdle();
158 TEST_F(ShillDeviceClientTest
, SetProperty
) {
159 const bool kValue
= true;
161 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
164 const base::FundamentalValue
value(kValue
);
165 PrepareForMethodCall(flimflam::kSetPropertyFunction
,
166 base::Bind(&ExpectStringAndValueArguments
,
167 flimflam::kCellularAllowRoamingProperty
,
171 MockClosure mock_closure
;
172 MockErrorCallback mock_error_callback
;
173 client_
->SetProperty(dbus::ObjectPath(kExampleDevicePath
),
174 flimflam::kCellularAllowRoamingProperty
,
176 mock_closure
.GetCallback(),
177 mock_error_callback
.GetCallback());
178 EXPECT_CALL(mock_closure
, Run()).Times(1);
179 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
181 // Run the message loop.
182 message_loop_
.RunUntilIdle();
185 TEST_F(ShillDeviceClientTest
, ClearProperty
) {
187 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
190 PrepareForMethodCall(flimflam::kClearPropertyFunction
,
191 base::Bind(&ExpectStringArgument
,
192 flimflam::kCellularAllowRoamingProperty
),
195 client_
->ClearProperty(dbus::ObjectPath(kExampleDevicePath
),
196 flimflam::kCellularAllowRoamingProperty
,
197 base::Bind(&ExpectNoResultValue
));
198 // Run the message loop.
199 message_loop_
.RunUntilIdle();
202 TEST_F(ShillDeviceClientTest
, AddIPConfig
) {
203 const dbus::ObjectPath
expected_result("/result/path");
205 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
206 dbus::MessageWriter
writer(response
.get());
207 writer
.AppendObjectPath(expected_result
);
210 PrepareForMethodCall(flimflam::kAddIPConfigFunction
,
211 base::Bind(&ExpectStringArgument
, flimflam::kTypeDHCP
),
214 client_
->AddIPConfig(dbus::ObjectPath(kExampleDevicePath
),
216 base::Bind(&ExpectObjectPathResult
, expected_result
));
217 // Run the message loop.
218 message_loop_
.RunUntilIdle();
221 TEST_F(ShillDeviceClientTest
, RequirePin
) {
222 const char kPin
[] = "123456";
223 const bool kRequired
= true;
225 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
228 MockClosure mock_closure
;
229 MockErrorCallback mock_error_callback
;
230 PrepareForMethodCall(flimflam::kRequirePinFunction
,
231 base::Bind(&ExpectStringAndBoolArguments
,
235 EXPECT_CALL(mock_closure
, Run()).Times(1);
236 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
238 client_
->RequirePin(dbus::ObjectPath(kExampleDevicePath
),
241 mock_closure
.GetCallback(),
242 mock_error_callback
.GetCallback());
243 // Run the message loop.
244 message_loop_
.RunUntilIdle();
247 TEST_F(ShillDeviceClientTest
, EnterPin
) {
248 const char kPin
[] = "123456";
250 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
253 MockClosure mock_closure
;
254 MockErrorCallback mock_error_callback
;
255 PrepareForMethodCall(flimflam::kEnterPinFunction
,
256 base::Bind(&ExpectStringArgument
,
259 EXPECT_CALL(mock_closure
, Run()).Times(1);
260 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
263 client_
->EnterPin(dbus::ObjectPath(kExampleDevicePath
),
265 mock_closure
.GetCallback(),
266 mock_error_callback
.GetCallback());
267 // Run the message loop.
268 message_loop_
.RunUntilIdle();
271 TEST_F(ShillDeviceClientTest
, UnblockPin
) {
272 const char kPuk
[] = "987654";
273 const char kPin
[] = "123456";
275 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
278 MockClosure mock_closure
;
279 MockErrorCallback mock_error_callback
;
280 PrepareForMethodCall(flimflam::kUnblockPinFunction
,
281 base::Bind(&ExpectTwoStringArguments
, kPuk
, kPin
),
283 EXPECT_CALL(mock_closure
, Run()).Times(1);
284 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
287 client_
->UnblockPin(dbus::ObjectPath(kExampleDevicePath
),
290 mock_closure
.GetCallback(),
291 mock_error_callback
.GetCallback());
292 // Run the message loop.
293 message_loop_
.RunUntilIdle();
296 TEST_F(ShillDeviceClientTest
, ChangePin
) {
297 const char kOldPin
[] = "123456";
298 const char kNewPin
[] = "234567";
300 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
303 MockClosure mock_closure
;
304 MockErrorCallback mock_error_callback
;
305 PrepareForMethodCall(flimflam::kChangePinFunction
,
306 base::Bind(&ExpectTwoStringArguments
,
310 EXPECT_CALL(mock_closure
, Run()).Times(1);
311 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
314 client_
->ChangePin(dbus::ObjectPath(kExampleDevicePath
),
317 mock_closure
.GetCallback(),
318 mock_error_callback
.GetCallback());
319 // Run the message loop.
320 message_loop_
.RunUntilIdle();
323 TEST_F(ShillDeviceClientTest
, Register
) {
324 const char kNetworkId
[] = "networkid";
326 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
329 MockClosure mock_closure
;
330 MockErrorCallback mock_error_callback
;
331 PrepareForMethodCall(flimflam::kRegisterFunction
,
332 base::Bind(&ExpectStringArgument
, kNetworkId
),
334 EXPECT_CALL(mock_closure
, Run()).Times(1);
335 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
338 client_
->Register(dbus::ObjectPath(kExampleDevicePath
),
340 mock_closure
.GetCallback(),
341 mock_error_callback
.GetCallback());
342 // Run the message loop.
343 message_loop_
.RunUntilIdle();
346 TEST_F(ShillDeviceClientTest
, SetCarrier
) {
347 const char kCarrier
[] = "carrier";
349 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
352 MockClosure mock_closure
;
353 MockErrorCallback mock_error_callback
;
354 PrepareForMethodCall(shill::kSetCarrierFunction
,
355 base::Bind(&ExpectStringArgument
, kCarrier
),
357 EXPECT_CALL(mock_closure
, Run()).Times(1);
359 client_
->SetCarrier(dbus::ObjectPath(kExampleDevicePath
),
361 mock_closure
.GetCallback(),
362 mock_error_callback
.GetCallback());
363 // Run the message loop.
364 message_loop_
.RunUntilIdle();
367 TEST_F(ShillDeviceClientTest
, Reset
) {
369 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
372 MockClosure mock_closure
;
373 MockErrorCallback mock_error_callback
;
374 PrepareForMethodCall(shill::kResetFunction
,
375 base::Bind(&ExpectNoArgument
),
377 EXPECT_CALL(mock_closure
, Run()).Times(1);
379 client_
->Reset(dbus::ObjectPath(kExampleDevicePath
),
380 mock_closure
.GetCallback(),
381 mock_error_callback
.GetCallback());
382 // Run the message loop.
383 message_loop_
.RunUntilIdle();
386 } // namespace chromeos