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_service_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 kExampleServicePath
[] = "/foo/bar";
26 class ShillServiceClientTest
: public ShillClientUnittestBase
{
28 ShillServiceClientTest()
29 : ShillClientUnittestBase(shill::kFlimflamServiceInterface
,
30 dbus::ObjectPath(kExampleServicePath
)) {
33 void SetUp() override
{
34 ShillClientUnittestBase::SetUp();
35 // Create a client with the mock bus.
36 client_
.reset(ShillServiceClient::Create());
37 client_
->Init(mock_bus_
.get());
38 // Run the message loop to run the signal connection result callback.
39 message_loop_
.RunUntilIdle();
42 void TearDown() override
{ ShillClientUnittestBase::TearDown(); }
45 scoped_ptr
<ShillServiceClient
> client_
;
48 TEST_F(ShillServiceClientTest
, PropertyChanged
) {
49 const int kValue
= 42;
51 dbus::Signal
signal(shill::kFlimflamServiceInterface
,
52 shill::kMonitorPropertyChanged
);
53 dbus::MessageWriter
writer(&signal
);
54 writer
.AppendString(shill::kSignalStrengthProperty
);
55 writer
.AppendVariantOfByte(kValue
);
58 const base::FundamentalValue
value(kValue
);
59 MockPropertyChangeObserver observer
;
62 shill::kSignalStrengthProperty
,
63 ValueEq(ByRef(value
)))).Times(1);
66 client_
->AddPropertyChangedObserver(
67 dbus::ObjectPath(kExampleServicePath
),
70 // Run the signal callback.
71 SendPropertyChangedSignal(&signal
);
73 // Remove the observer.
74 client_
->RemovePropertyChangedObserver(
75 dbus::ObjectPath(kExampleServicePath
),
78 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
80 // Run the signal callback again and make sure the observer isn't called.
81 SendPropertyChangedSignal(&signal
);
84 TEST_F(ShillServiceClientTest
, GetProperties
) {
85 const int kValue
= 42;
87 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
88 dbus::MessageWriter
writer(response
.get());
89 dbus::MessageWriter
array_writer(NULL
);
90 writer
.OpenArray("{sv}", &array_writer
);
91 dbus::MessageWriter
entry_writer(NULL
);
92 array_writer
.OpenDictEntry(&entry_writer
);
93 entry_writer
.AppendString(shill::kSignalStrengthProperty
);
94 entry_writer
.AppendVariantOfByte(kValue
);
95 array_writer
.CloseContainer(&entry_writer
);
96 writer
.CloseContainer(&array_writer
);
99 base::DictionaryValue value
;
100 value
.SetWithoutPathExpansion(shill::kSignalStrengthProperty
,
101 new base::FundamentalValue(kValue
));
102 PrepareForMethodCall(shill::kGetPropertiesFunction
,
103 base::Bind(&ExpectNoArgument
),
106 client_
->GetProperties(dbus::ObjectPath(kExampleServicePath
),
107 base::Bind(&ExpectDictionaryValueResult
, &value
));
108 // Run the message loop.
109 message_loop_
.RunUntilIdle();
112 TEST_F(ShillServiceClientTest
, SetProperty
) {
113 const char kValue
[] = "passphrase";
115 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
118 const base::StringValue
value(kValue
);
119 PrepareForMethodCall(shill::kSetPropertyFunction
,
120 base::Bind(&ExpectStringAndValueArguments
,
121 shill::kPassphraseProperty
,
125 MockClosure mock_closure
;
126 MockErrorCallback mock_error_callback
;
127 client_
->SetProperty(dbus::ObjectPath(kExampleServicePath
),
128 shill::kPassphraseProperty
,
130 mock_closure
.GetCallback(),
131 mock_error_callback
.GetCallback());
132 EXPECT_CALL(mock_closure
, Run()).Times(1);
133 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
135 // Run the message loop.
136 message_loop_
.RunUntilIdle();
139 TEST_F(ShillServiceClientTest
, SetProperties
) {
141 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
144 scoped_ptr
<base::DictionaryValue
> arg(CreateExampleServiceProperties());
145 // Use a variant valued dictionary rather than a string valued one.
146 const bool string_valued
= false;
147 PrepareForMethodCall(
148 shill::kSetPropertiesFunction
,
149 base::Bind(&ExpectDictionaryValueArgument
, arg
.get(), string_valued
),
153 MockClosure mock_closure
;
154 MockErrorCallback mock_error_callback
;
155 client_
->SetProperties(dbus::ObjectPath(kExampleServicePath
),
157 mock_closure
.GetCallback(),
158 mock_error_callback
.GetCallback());
159 EXPECT_CALL(mock_closure
, Run()).Times(1);
160 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
162 // Run the message loop.
163 message_loop_
.RunUntilIdle();
166 TEST_F(ShillServiceClientTest
, ClearProperty
) {
168 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
171 PrepareForMethodCall(shill::kClearPropertyFunction
,
172 base::Bind(&ExpectStringArgument
,
173 shill::kPassphraseProperty
),
176 MockClosure mock_closure
;
177 MockErrorCallback mock_error_callback
;
178 client_
->ClearProperty(dbus::ObjectPath(kExampleServicePath
),
179 shill::kPassphraseProperty
,
180 mock_closure
.GetCallback(),
181 mock_error_callback
.GetCallback());
182 EXPECT_CALL(mock_closure
, Run()).Times(1);
183 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
185 // Run the message loop.
186 message_loop_
.RunUntilIdle();
189 TEST_F(ShillServiceClientTest
, ClearProperties
) {
191 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
192 dbus::MessageWriter
writer(response
.get());
193 dbus::MessageWriter
array_writer(NULL
);
194 writer
.OpenArray("b", &array_writer
);
195 array_writer
.AppendBool(true);
196 array_writer
.AppendBool(true);
197 writer
.CloseContainer(&array_writer
);
200 std::vector
<std::string
> keys
;
201 keys
.push_back(shill::kPassphraseProperty
);
202 keys
.push_back(shill::kSignalStrengthProperty
);
203 PrepareForMethodCall(shill::kClearPropertiesFunction
,
204 base::Bind(&ExpectArrayOfStringsArgument
, keys
),
207 MockListValueCallback mock_list_value_callback
;
208 MockErrorCallback mock_error_callback
;
209 client_
->ClearProperties(dbus::ObjectPath(kExampleServicePath
),
211 mock_list_value_callback
.GetCallback(),
212 mock_error_callback
.GetCallback());
213 EXPECT_CALL(mock_list_value_callback
, Run(_
)).Times(1);
214 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
216 // Run the message loop.
217 message_loop_
.RunUntilIdle();
220 TEST_F(ShillServiceClientTest
, Connect
) {
222 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
225 MockClosure mock_closure
;
226 MockErrorCallback mock_error_callback
;
227 PrepareForMethodCall(shill::kConnectFunction
,
228 base::Bind(&ExpectNoArgument
),
230 EXPECT_CALL(mock_closure
, Run()).Times(1);
232 client_
->Connect(dbus::ObjectPath(kExampleServicePath
),
233 mock_closure
.GetCallback(),
234 mock_error_callback
.GetCallback());
236 // Run the message loop.
237 message_loop_
.RunUntilIdle();
240 TEST_F(ShillServiceClientTest
, Disconnect
) {
242 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
245 PrepareForMethodCall(shill::kDisconnectFunction
,
246 base::Bind(&ExpectNoArgument
),
249 MockClosure mock_closure
;
250 MockErrorCallback mock_error_callback
;
251 client_
->Disconnect(dbus::ObjectPath(kExampleServicePath
),
252 mock_closure
.GetCallback(),
253 mock_error_callback
.GetCallback());
254 EXPECT_CALL(mock_closure
, Run()).Times(1);
255 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
257 // Run the message loop.
258 message_loop_
.RunUntilIdle();
261 TEST_F(ShillServiceClientTest
, Remove
) {
263 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
266 PrepareForMethodCall(shill::kRemoveServiceFunction
,
267 base::Bind(&ExpectNoArgument
),
270 MockClosure mock_closure
;
271 MockErrorCallback mock_error_callback
;
272 client_
->Remove(dbus::ObjectPath(kExampleServicePath
),
273 mock_closure
.GetCallback(),
274 mock_error_callback
.GetCallback());
275 EXPECT_CALL(mock_closure
, Run()).Times(1);
276 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
278 // Run the message loop.
279 message_loop_
.RunUntilIdle();
282 TEST_F(ShillServiceClientTest
, ActivateCellularModem
) {
283 const char kCarrier
[] = "carrier";
285 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
288 PrepareForMethodCall(shill::kActivateCellularModemFunction
,
289 base::Bind(&ExpectStringArgument
, kCarrier
),
292 MockClosure mock_closure
;
293 MockErrorCallback mock_error_callback
;
294 client_
->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath
),
296 mock_closure
.GetCallback(),
297 mock_error_callback
.GetCallback());
298 EXPECT_CALL(mock_closure
, Run()).Times(1);
299 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
301 // Run the message loop.
302 message_loop_
.RunUntilIdle();
305 } // namespace chromeos