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 virtual void SetUp() {
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 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(shill::kFlimflamServiceInterface
,
54 shill::kMonitorPropertyChanged
);
55 dbus::MessageWriter
writer(&signal
);
56 writer
.AppendString(shill::kSignalStrengthProperty
);
57 writer
.AppendVariantOfByte(kValue
);
60 const base::FundamentalValue
value(kValue
);
61 MockPropertyChangeObserver observer
;
64 shill::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(shill::kSignalStrengthProperty
);
96 entry_writer
.AppendVariantOfByte(kValue
);
97 array_writer
.CloseContainer(&entry_writer
);
98 writer
.CloseContainer(&array_writer
);
101 base::DictionaryValue value
;
102 value
.SetWithoutPathExpansion(shill::kSignalStrengthProperty
,
103 new base::FundamentalValue(kValue
));
104 PrepareForMethodCall(shill::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(shill::kSetPropertyFunction
,
122 base::Bind(&ExpectStringAndValueArguments
,
123 shill::kPassphraseProperty
,
127 MockClosure mock_closure
;
128 MockErrorCallback mock_error_callback
;
129 client_
->SetProperty(dbus::ObjectPath(kExampleServicePath
),
130 shill::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 // Use a variant valued dictionary rather than a string valued one.
148 const bool string_valued
= false;
149 PrepareForMethodCall(
150 shill::kSetPropertiesFunction
,
151 base::Bind(&ExpectDictionaryValueArgument
, arg
.get(), string_valued
),
155 MockClosure mock_closure
;
156 MockErrorCallback mock_error_callback
;
157 client_
->SetProperties(dbus::ObjectPath(kExampleServicePath
),
159 mock_closure
.GetCallback(),
160 mock_error_callback
.GetCallback());
161 EXPECT_CALL(mock_closure
, Run()).Times(1);
162 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
164 // Run the message loop.
165 message_loop_
.RunUntilIdle();
168 TEST_F(ShillServiceClientTest
, ClearProperty
) {
170 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
173 PrepareForMethodCall(shill::kClearPropertyFunction
,
174 base::Bind(&ExpectStringArgument
,
175 shill::kPassphraseProperty
),
178 MockClosure mock_closure
;
179 MockErrorCallback mock_error_callback
;
180 client_
->ClearProperty(dbus::ObjectPath(kExampleServicePath
),
181 shill::kPassphraseProperty
,
182 mock_closure
.GetCallback(),
183 mock_error_callback
.GetCallback());
184 EXPECT_CALL(mock_closure
, Run()).Times(1);
185 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
187 // Run the message loop.
188 message_loop_
.RunUntilIdle();
191 TEST_F(ShillServiceClientTest
, ClearProperties
) {
193 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
194 dbus::MessageWriter
writer(response
.get());
195 dbus::MessageWriter
array_writer(NULL
);
196 writer
.OpenArray("b", &array_writer
);
197 array_writer
.AppendBool(true);
198 array_writer
.AppendBool(true);
199 writer
.CloseContainer(&array_writer
);
202 std::vector
<std::string
> keys
;
203 keys
.push_back(shill::kPassphraseProperty
);
204 keys
.push_back(shill::kSignalStrengthProperty
);
205 PrepareForMethodCall(shill::kClearPropertiesFunction
,
206 base::Bind(&ExpectArrayOfStringsArgument
, keys
),
209 MockListValueCallback mock_list_value_callback
;
210 MockErrorCallback mock_error_callback
;
211 client_
->ClearProperties(dbus::ObjectPath(kExampleServicePath
),
213 mock_list_value_callback
.GetCallback(),
214 mock_error_callback
.GetCallback());
215 EXPECT_CALL(mock_list_value_callback
, Run(_
)).Times(1);
216 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
218 // Run the message loop.
219 message_loop_
.RunUntilIdle();
222 TEST_F(ShillServiceClientTest
, Connect
) {
224 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
227 MockClosure mock_closure
;
228 MockErrorCallback mock_error_callback
;
229 PrepareForMethodCall(shill::kConnectFunction
,
230 base::Bind(&ExpectNoArgument
),
232 EXPECT_CALL(mock_closure
, Run()).Times(1);
234 client_
->Connect(dbus::ObjectPath(kExampleServicePath
),
235 mock_closure
.GetCallback(),
236 mock_error_callback
.GetCallback());
238 // Run the message loop.
239 message_loop_
.RunUntilIdle();
242 TEST_F(ShillServiceClientTest
, Disconnect
) {
244 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
247 PrepareForMethodCall(shill::kDisconnectFunction
,
248 base::Bind(&ExpectNoArgument
),
251 MockClosure mock_closure
;
252 MockErrorCallback mock_error_callback
;
253 client_
->Disconnect(dbus::ObjectPath(kExampleServicePath
),
254 mock_closure
.GetCallback(),
255 mock_error_callback
.GetCallback());
256 EXPECT_CALL(mock_closure
, Run()).Times(1);
257 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
259 // Run the message loop.
260 message_loop_
.RunUntilIdle();
263 TEST_F(ShillServiceClientTest
, Remove
) {
265 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
268 PrepareForMethodCall(shill::kRemoveServiceFunction
,
269 base::Bind(&ExpectNoArgument
),
272 MockClosure mock_closure
;
273 MockErrorCallback mock_error_callback
;
274 client_
->Remove(dbus::ObjectPath(kExampleServicePath
),
275 mock_closure
.GetCallback(),
276 mock_error_callback
.GetCallback());
277 EXPECT_CALL(mock_closure
, Run()).Times(1);
278 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
280 // Run the message loop.
281 message_loop_
.RunUntilIdle();
284 TEST_F(ShillServiceClientTest
, ActivateCellularModem
) {
285 const char kCarrier
[] = "carrier";
287 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
290 PrepareForMethodCall(shill::kActivateCellularModemFunction
,
291 base::Bind(&ExpectStringArgument
, kCarrier
),
294 MockClosure mock_closure
;
295 MockErrorCallback mock_error_callback
;
296 client_
->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath
),
298 mock_closure
.GetCallback(),
299 mock_error_callback
.GetCallback());
300 EXPECT_CALL(mock_closure
, Run()).Times(1);
301 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
303 // Run the message loop.
304 message_loop_
.RunUntilIdle();
307 } // namespace chromeos