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
,
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
, ClearProperty
) {
143 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
146 PrepareForMethodCall(flimflam::kClearPropertyFunction
,
147 base::Bind(&ExpectStringArgument
,
148 flimflam::kPassphraseProperty
),
151 MockClosure mock_closure
;
152 MockErrorCallback mock_error_callback
;
153 client_
->ClearProperty(dbus::ObjectPath(kExampleServicePath
),
154 flimflam::kPassphraseProperty
,
155 mock_closure
.GetCallback(),
156 mock_error_callback
.GetCallback());
157 EXPECT_CALL(mock_closure
, Run()).Times(1);
158 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
160 // Run the message loop.
161 message_loop_
.RunUntilIdle();
164 TEST_F(ShillServiceClientTest
, ClearProperties
) {
166 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
167 dbus::MessageWriter
writer(response
.get());
168 dbus::MessageWriter
array_writer(NULL
);
169 writer
.OpenArray("b", &array_writer
);
170 array_writer
.AppendBool(true);
171 array_writer
.AppendBool(true);
172 writer
.CloseContainer(&array_writer
);
175 std::vector
<std::string
> keys
;
176 keys
.push_back(flimflam::kPassphraseProperty
);
177 keys
.push_back(flimflam::kSignalStrengthProperty
);
178 PrepareForMethodCall(shill::kClearPropertiesFunction
,
179 base::Bind(&ExpectArrayOfStringsArgument
, keys
),
182 MockListValueCallback mock_list_value_callback
;
183 MockErrorCallback mock_error_callback
;
184 client_
->ClearProperties(dbus::ObjectPath(kExampleServicePath
),
186 mock_list_value_callback
.GetCallback(),
187 mock_error_callback
.GetCallback());
188 EXPECT_CALL(mock_list_value_callback
, Run(_
)).Times(1);
189 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
191 // Run the message loop.
192 message_loop_
.RunUntilIdle();
195 TEST_F(ShillServiceClientTest
, Connect
) {
197 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
200 MockClosure mock_closure
;
201 MockErrorCallback mock_error_callback
;
202 PrepareForMethodCall(flimflam::kConnectFunction
,
203 base::Bind(&ExpectNoArgument
),
205 EXPECT_CALL(mock_closure
, Run()).Times(1);
207 client_
->Connect(dbus::ObjectPath(kExampleServicePath
),
208 mock_closure
.GetCallback(),
209 mock_error_callback
.GetCallback());
211 // Run the message loop.
212 message_loop_
.RunUntilIdle();
215 TEST_F(ShillServiceClientTest
, Disconnect
) {
217 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
220 PrepareForMethodCall(flimflam::kDisconnectFunction
,
221 base::Bind(&ExpectNoArgument
),
224 MockClosure mock_closure
;
225 MockErrorCallback mock_error_callback
;
226 client_
->Disconnect(dbus::ObjectPath(kExampleServicePath
),
227 mock_closure
.GetCallback(),
228 mock_error_callback
.GetCallback());
229 EXPECT_CALL(mock_closure
, Run()).Times(1);
230 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
232 // Run the message loop.
233 message_loop_
.RunUntilIdle();
236 TEST_F(ShillServiceClientTest
, Remove
) {
238 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
241 PrepareForMethodCall(flimflam::kRemoveServiceFunction
,
242 base::Bind(&ExpectNoArgument
),
245 MockClosure mock_closure
;
246 MockErrorCallback mock_error_callback
;
247 client_
->Remove(dbus::ObjectPath(kExampleServicePath
),
248 mock_closure
.GetCallback(),
249 mock_error_callback
.GetCallback());
250 EXPECT_CALL(mock_closure
, Run()).Times(1);
251 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
253 // Run the message loop.
254 message_loop_
.RunUntilIdle();
257 TEST_F(ShillServiceClientTest
, ActivateCellularModem
) {
258 const char kCarrier
[] = "carrier";
260 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
263 PrepareForMethodCall(flimflam::kActivateCellularModemFunction
,
264 base::Bind(&ExpectStringArgument
, kCarrier
),
267 MockClosure mock_closure
;
268 MockErrorCallback mock_error_callback
;
269 client_
->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath
),
271 mock_closure
.GetCallback(),
272 mock_error_callback
.GetCallback());
273 EXPECT_CALL(mock_closure
, Run()).Times(1);
274 EXPECT_CALL(mock_error_callback
, Run(_
, _
)).Times(0);
276 // Run the message loop.
277 message_loop_
.RunUntilIdle();
280 TEST_F(ShillServiceClientTest
, CallActivateCellularModemAndBlock
) {
281 const char kCarrier
[] = "carrier";
283 scoped_ptr
<dbus::Response
> response(dbus::Response::CreateEmpty());
286 PrepareForMethodCall(flimflam::kActivateCellularModemFunction
,
287 base::Bind(&ExpectStringArgument
, kCarrier
),
290 const bool result
= client_
->CallActivateCellularModemAndBlock(
291 dbus::ObjectPath(kExampleServicePath
), kCarrier
);
295 } // namespace chromeos