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(flimflam::kFlimflamServiceInterface
,
30 dbus::ObjectPath(kExampleServicePath
)) {
33 virtual void SetUp() {
34 ShillClientUnittestBase::SetUp();
35 // Create a client with the mock bus.
36 client_
.reset(ShillServiceClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION
));
37 client_
->Init(mock_bus_
.get());
38 // Run the message loop to run the signal connection result callback.
39 message_loop_
.RunUntilIdle();
42 virtual void TearDown() {
43 ShillClientUnittestBase::TearDown();
47 scoped_ptr
<ShillServiceClient
> client_
;
50 TEST_F(ShillServiceClientTest
, PropertyChanged
) {
51 const int kValue
= 42;
53 dbus::Signal
signal(flimflam::kFlimflamServiceInterface
,
54 flimflam::kMonitorPropertyChanged
);
55 dbus::MessageWriter
writer(&signal
);
56 writer
.AppendString(flimflam::kSignalStrengthProperty
);
57 writer
.AppendVariantOfByte(kValue
);
60 const base::FundamentalValue
value(kValue
);
61 MockPropertyChangeObserver observer
;
64 flimflam::kSignalStrengthProperty
,
65 ValueEq(ByRef(value
)))).Times(1);
68 client_
->AddPropertyChangedObserver(
69 dbus::ObjectPath(kExampleServicePath
),
72 // Run the signal callback.
73 SendPropertyChangedSignal(&signal
);
75 // Remove the observer.
76 client_
->RemovePropertyChangedObserver(
77 dbus::ObjectPath(kExampleServicePath
),
80 EXPECT_CALL(observer
, OnPropertyChanged(_
, _
)).Times(0);
82 // Run the signal callback again and make sure the observer isn't called.
83 SendPropertyChangedSignal(&signal
);
86 TEST_F(ShillServiceClientTest
, GetProperties
) {
87 const int kValue
= 42;
89 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
90 dbus::MessageWriter
writer(response
.get());
91 dbus::MessageWriter
array_writer(NULL
);
92 writer
.OpenArray("{sv}", &array_writer
);
93 dbus::MessageWriter
entry_writer(NULL
);
94 array_writer
.OpenDictEntry(&entry_writer
);
95 entry_writer
.AppendString(flimflam::kSignalStrengthProperty
);
96 entry_writer
.AppendVariantOfByte(kValue
);
97 array_writer
.CloseContainer(&entry_writer
);
98 writer
.CloseContainer(&array_writer
);
101 base::DictionaryValue value
;
102 value
.SetWithoutPathExpansion(flimflam::kSignalStrengthProperty
,
103 base::Value::CreateIntegerValue(kValue
));
104 PrepareForMethodCall(flimflam::kGetPropertiesFunction
,
105 base::Bind(&ExpectNoArgument
),
108 client_
->GetProperties(dbus::ObjectPath(kExampleServicePath
),
109 base::Bind(&ExpectDictionaryValueResult
, &value
));
110 // Run the message loop.
111 message_loop_
.RunUntilIdle();
114 TEST_F(ShillServiceClientTest
, SetProperty
) {
115 const char kValue
[] = "passphrase";
117 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
120 const base::StringValue
value(kValue
);
121 PrepareForMethodCall(flimflam::kSetPropertyFunction
,
122 base::Bind(&ExpectStringAndValueArguments
,
123 flimflam::kPassphraseProperty
,
127 MockClosure mock_closure
;
128 MockErrorCallback mock_error_callback
;
129 client_
->SetProperty(dbus::ObjectPath(kExampleServicePath
),
130 flimflam::kPassphraseProperty
,
132 mock_closure
.GetCallback(),
133 mock_error_callback
.GetCallback());
134 EXPECT_CALL(mock_closure
, Run()).Times(1);
135 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
137 // Run the message loop.
138 message_loop_
.RunUntilIdle();
141 TEST_F(ShillServiceClientTest
, SetProperties
) {
143 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
146 scoped_ptr
<base::DictionaryValue
> arg(CreateExampleServiceProperties());
147 PrepareForMethodCall(shill::kSetPropertiesFunction
,
148 base::Bind(&ExpectDictionaryValueArgument
, arg
.get()),
152 MockClosure mock_closure
;
153 MockErrorCallback mock_error_callback
;
154 client_
->SetProperties(dbus::ObjectPath(kExampleServicePath
),
156 mock_closure
.GetCallback(),
157 mock_error_callback
.GetCallback());
158 EXPECT_CALL(mock_closure
, Run()).Times(1);
159 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
161 // Run the message loop.
162 message_loop_
.RunUntilIdle();
165 TEST_F(ShillServiceClientTest
, ClearProperty
) {
167 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
170 PrepareForMethodCall(flimflam::kClearPropertyFunction
,
171 base::Bind(&ExpectStringArgument
,
172 flimflam::kPassphraseProperty
),
175 MockClosure mock_closure
;
176 MockErrorCallback mock_error_callback
;
177 client_
->ClearProperty(dbus::ObjectPath(kExampleServicePath
),
178 flimflam::kPassphraseProperty
,
179 mock_closure
.GetCallback(),
180 mock_error_callback
.GetCallback());
181 EXPECT_CALL(mock_closure
, Run()).Times(1);
182 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
184 // Run the message loop.
185 message_loop_
.RunUntilIdle();
188 TEST_F(ShillServiceClientTest
, ClearProperties
) {
190 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
191 dbus::MessageWriter
writer(response
.get());
192 dbus::MessageWriter
array_writer(NULL
);
193 writer
.OpenArray("b", &array_writer
);
194 array_writer
.AppendBool(true);
195 array_writer
.AppendBool(true);
196 writer
.CloseContainer(&array_writer
);
199 std::vector
<std::string
> keys
;
200 keys
.push_back(flimflam::kPassphraseProperty
);
201 keys
.push_back(flimflam::kSignalStrengthProperty
);
202 PrepareForMethodCall(shill::kClearPropertiesFunction
,
203 base::Bind(&ExpectArrayOfStringsArgument
, keys
),
206 MockListValueCallback mock_list_value_callback
;
207 MockErrorCallback mock_error_callback
;
208 client_
->ClearProperties(dbus::ObjectPath(kExampleServicePath
),
210 mock_list_value_callback
.GetCallback(),
211 mock_error_callback
.GetCallback());
212 EXPECT_CALL(mock_list_value_callback
, Run(_
)).Times(1);
213 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
215 // Run the message loop.
216 message_loop_
.RunUntilIdle();
219 TEST_F(ShillServiceClientTest
, Connect
) {
221 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
224 MockClosure mock_closure
;
225 MockErrorCallback mock_error_callback
;
226 PrepareForMethodCall(flimflam::kConnectFunction
,
227 base::Bind(&ExpectNoArgument
),
229 EXPECT_CALL(mock_closure
, Run()).Times(1);
231 client_
->Connect(dbus::ObjectPath(kExampleServicePath
),
232 mock_closure
.GetCallback(),
233 mock_error_callback
.GetCallback());
235 // Run the message loop.
236 message_loop_
.RunUntilIdle();
239 TEST_F(ShillServiceClientTest
, Disconnect
) {
241 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
244 PrepareForMethodCall(flimflam::kDisconnectFunction
,
245 base::Bind(&ExpectNoArgument
),
248 MockClosure mock_closure
;
249 MockErrorCallback mock_error_callback
;
250 client_
->Disconnect(dbus::ObjectPath(kExampleServicePath
),
251 mock_closure
.GetCallback(),
252 mock_error_callback
.GetCallback());
253 EXPECT_CALL(mock_closure
, Run()).Times(1);
254 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
256 // Run the message loop.
257 message_loop_
.RunUntilIdle();
260 TEST_F(ShillServiceClientTest
, Remove
) {
262 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
265 PrepareForMethodCall(flimflam::kRemoveServiceFunction
,
266 base::Bind(&ExpectNoArgument
),
269 MockClosure mock_closure
;
270 MockErrorCallback mock_error_callback
;
271 client_
->Remove(dbus::ObjectPath(kExampleServicePath
),
272 mock_closure
.GetCallback(),
273 mock_error_callback
.GetCallback());
274 EXPECT_CALL(mock_closure
, Run()).Times(1);
275 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
277 // Run the message loop.
278 message_loop_
.RunUntilIdle();
281 TEST_F(ShillServiceClientTest
, ActivateCellularModem
) {
282 const char kCarrier
[] = "carrier";
284 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
287 PrepareForMethodCall(flimflam::kActivateCellularModemFunction
,
288 base::Bind(&ExpectStringArgument
, kCarrier
),
291 MockClosure mock_closure
;
292 MockErrorCallback mock_error_callback
;
293 client_
->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath
),
295 mock_closure
.GetCallback(),
296 mock_error_callback
.GetCallback());
297 EXPECT_CALL(mock_closure
, Run()).Times(1);
298 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
300 // Run the message loop.
301 message_loop_
.RunUntilIdle();
304 } // namespace chromeos