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(shill::kFlimflamDeviceInterface
,
56 dbus::ObjectPath(kExampleDevicePath
)) {
59 void SetUp() override
{
60 ShillClientUnittestBase::SetUp();
61 // Create a client with the mock bus.
62 client_
.reset(ShillDeviceClient::Create());
63 client_
->Init(mock_bus_
.get());
64 // Run the message loop to run the signal connection result callback.
65 message_loop_
.RunUntilIdle();
68 void TearDown() override
{ ShillClientUnittestBase::TearDown(); }
71 scoped_ptr
<ShillDeviceClient
> client_
;
74 TEST_F(ShillDeviceClientTest
, PropertyChanged
) {
75 const bool kValue
= true;
77 dbus::Signal
signal(shill::kFlimflamDeviceInterface
,
78 shill::kMonitorPropertyChanged
);
79 dbus::MessageWriter
writer(&signal
);
80 writer
.AppendString(shill::kCellularAllowRoamingProperty
);
81 writer
.AppendVariantOfBool(kValue
);
84 const base::FundamentalValue
value(kValue
);
85 MockPropertyChangeObserver observer
;
87 OnPropertyChanged(shill::kCellularAllowRoamingProperty
,
88 ValueEq(ByRef(value
)))).Times(1);
91 client_
->AddPropertyChangedObserver(
92 dbus::ObjectPath(kExampleDevicePath
),
95 // Run the signal callback.
96 SendPropertyChangedSignal(&signal
);
98 // Remove the observer.
99 client_
->RemovePropertyChangedObserver(
100 dbus::ObjectPath(kExampleDevicePath
),
103 EXPECT_CALL(observer
,
104 OnPropertyChanged(shill::kCellularAllowRoamingProperty
,
105 ValueEq(ByRef(value
)))).Times(0);
107 // Run the signal callback again and make sure the observer isn't called.
108 SendPropertyChangedSignal(&signal
);
111 TEST_F(ShillDeviceClientTest
, GetProperties
) {
112 const bool kValue
= true;
114 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
115 dbus::MessageWriter
writer(response
.get());
116 dbus::MessageWriter
array_writer(NULL
);
117 writer
.OpenArray("{sv}", &array_writer
);
118 dbus::MessageWriter
entry_writer(NULL
);
119 array_writer
.OpenDictEntry(&entry_writer
);
120 entry_writer
.AppendString(shill::kCellularAllowRoamingProperty
);
121 entry_writer
.AppendVariantOfBool(kValue
);
122 array_writer
.CloseContainer(&entry_writer
);
123 writer
.CloseContainer(&array_writer
);
126 base::DictionaryValue value
;
127 value
.SetWithoutPathExpansion(shill::kCellularAllowRoamingProperty
,
128 new base::FundamentalValue(kValue
));
129 PrepareForMethodCall(shill::kGetPropertiesFunction
,
130 base::Bind(&ExpectNoArgument
),
133 client_
->GetProperties(dbus::ObjectPath(kExampleDevicePath
),
134 base::Bind(&ExpectDictionaryValueResult
, &value
));
135 // Run the message loop.
136 message_loop_
.RunUntilIdle();
139 TEST_F(ShillDeviceClientTest
, ProposeScan
) {
141 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
144 PrepareForMethodCall(shill::kProposeScanFunction
,
145 base::Bind(&ExpectNoArgument
),
148 client_
->ProposeScan(dbus::ObjectPath(kExampleDevicePath
),
149 base::Bind(&ExpectNoResultValue
));
150 // Run the message loop.
151 message_loop_
.RunUntilIdle();
154 TEST_F(ShillDeviceClientTest
, SetProperty
) {
155 const bool kValue
= true;
157 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
160 const base::FundamentalValue
value(kValue
);
161 PrepareForMethodCall(shill::kSetPropertyFunction
,
162 base::Bind(&ExpectStringAndValueArguments
,
163 shill::kCellularAllowRoamingProperty
,
167 MockClosure mock_closure
;
168 MockErrorCallback mock_error_callback
;
169 client_
->SetProperty(dbus::ObjectPath(kExampleDevicePath
),
170 shill::kCellularAllowRoamingProperty
,
172 mock_closure
.GetCallback(),
173 mock_error_callback
.GetCallback());
174 EXPECT_CALL(mock_closure
, Run()).Times(1);
175 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
177 // Run the message loop.
178 message_loop_
.RunUntilIdle();
181 TEST_F(ShillDeviceClientTest
, ClearProperty
) {
183 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
186 PrepareForMethodCall(shill::kClearPropertyFunction
,
187 base::Bind(&ExpectStringArgument
,
188 shill::kCellularAllowRoamingProperty
),
191 client_
->ClearProperty(dbus::ObjectPath(kExampleDevicePath
),
192 shill::kCellularAllowRoamingProperty
,
193 base::Bind(&ExpectNoResultValue
));
194 // Run the message loop.
195 message_loop_
.RunUntilIdle();
198 TEST_F(ShillDeviceClientTest
, AddIPConfig
) {
199 const dbus::ObjectPath
expected_result("/result/path");
201 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
202 dbus::MessageWriter
writer(response
.get());
203 writer
.AppendObjectPath(expected_result
);
206 PrepareForMethodCall(shill::kAddIPConfigFunction
,
207 base::Bind(&ExpectStringArgument
, shill::kTypeDHCP
),
210 client_
->AddIPConfig(dbus::ObjectPath(kExampleDevicePath
),
212 base::Bind(&ExpectObjectPathResult
, expected_result
));
213 // Run the message loop.
214 message_loop_
.RunUntilIdle();
217 TEST_F(ShillDeviceClientTest
, RequirePin
) {
218 const char kPin
[] = "123456";
219 const bool kRequired
= true;
221 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
224 MockClosure mock_closure
;
225 MockErrorCallback mock_error_callback
;
226 PrepareForMethodCall(shill::kRequirePinFunction
,
227 base::Bind(&ExpectStringAndBoolArguments
,
231 EXPECT_CALL(mock_closure
, Run()).Times(1);
232 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
234 client_
->RequirePin(dbus::ObjectPath(kExampleDevicePath
),
237 mock_closure
.GetCallback(),
238 mock_error_callback
.GetCallback());
239 // Run the message loop.
240 message_loop_
.RunUntilIdle();
243 TEST_F(ShillDeviceClientTest
, EnterPin
) {
244 const char kPin
[] = "123456";
246 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
249 MockClosure mock_closure
;
250 MockErrorCallback mock_error_callback
;
251 PrepareForMethodCall(shill::kEnterPinFunction
,
252 base::Bind(&ExpectStringArgument
,
255 EXPECT_CALL(mock_closure
, Run()).Times(1);
256 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
259 client_
->EnterPin(dbus::ObjectPath(kExampleDevicePath
),
261 mock_closure
.GetCallback(),
262 mock_error_callback
.GetCallback());
263 // Run the message loop.
264 message_loop_
.RunUntilIdle();
267 TEST_F(ShillDeviceClientTest
, UnblockPin
) {
268 const char kPuk
[] = "987654";
269 const char kPin
[] = "123456";
271 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
274 MockClosure mock_closure
;
275 MockErrorCallback mock_error_callback
;
276 PrepareForMethodCall(shill::kUnblockPinFunction
,
277 base::Bind(&ExpectTwoStringArguments
, kPuk
, kPin
),
279 EXPECT_CALL(mock_closure
, Run()).Times(1);
280 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
283 client_
->UnblockPin(dbus::ObjectPath(kExampleDevicePath
),
286 mock_closure
.GetCallback(),
287 mock_error_callback
.GetCallback());
288 // Run the message loop.
289 message_loop_
.RunUntilIdle();
292 TEST_F(ShillDeviceClientTest
, ChangePin
) {
293 const char kOldPin
[] = "123456";
294 const char kNewPin
[] = "234567";
296 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
299 MockClosure mock_closure
;
300 MockErrorCallback mock_error_callback
;
301 PrepareForMethodCall(shill::kChangePinFunction
,
302 base::Bind(&ExpectTwoStringArguments
,
306 EXPECT_CALL(mock_closure
, Run()).Times(1);
307 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
310 client_
->ChangePin(dbus::ObjectPath(kExampleDevicePath
),
313 mock_closure
.GetCallback(),
314 mock_error_callback
.GetCallback());
315 // Run the message loop.
316 message_loop_
.RunUntilIdle();
319 TEST_F(ShillDeviceClientTest
, Register
) {
320 const char kNetworkId
[] = "networkid";
322 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
325 MockClosure mock_closure
;
326 MockErrorCallback mock_error_callback
;
327 PrepareForMethodCall(shill::kRegisterFunction
,
328 base::Bind(&ExpectStringArgument
, kNetworkId
),
330 EXPECT_CALL(mock_closure
, Run()).Times(1);
331 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
334 client_
->Register(dbus::ObjectPath(kExampleDevicePath
),
336 mock_closure
.GetCallback(),
337 mock_error_callback
.GetCallback());
338 // Run the message loop.
339 message_loop_
.RunUntilIdle();
342 TEST_F(ShillDeviceClientTest
, SetCarrier
) {
343 const char kCarrier
[] = "carrier";
345 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
348 MockClosure mock_closure
;
349 MockErrorCallback mock_error_callback
;
350 PrepareForMethodCall(shill::kSetCarrierFunction
,
351 base::Bind(&ExpectStringArgument
, kCarrier
),
353 EXPECT_CALL(mock_closure
, Run()).Times(1);
355 client_
->SetCarrier(dbus::ObjectPath(kExampleDevicePath
),
357 mock_closure
.GetCallback(),
358 mock_error_callback
.GetCallback());
359 // Run the message loop.
360 message_loop_
.RunUntilIdle();
363 TEST_F(ShillDeviceClientTest
, Reset
) {
365 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
368 MockClosure mock_closure
;
369 MockErrorCallback mock_error_callback
;
370 PrepareForMethodCall(shill::kResetFunction
,
371 base::Bind(&ExpectNoArgument
),
373 EXPECT_CALL(mock_closure
, Run()).Times(1);
375 client_
->Reset(dbus::ObjectPath(kExampleDevicePath
),
376 mock_closure
.GetCallback(),
377 mock_error_callback
.GetCallback());
378 // Run the message loop.
379 message_loop_
.RunUntilIdle();
382 } // namespace chromeos