Simple Cache: transactional index in subdirectory
[chromium-blink-merge.git] / chromeos / dbus / shill_service_client_unittest.cc
blob9d70eabf0e9e0cf326279f3cf21958b38f3d7f66
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.
5 #include "base/bind.h"
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"
15 using testing::_;
16 using testing::ByRef;
18 namespace chromeos {
20 namespace {
22 const char kExampleServicePath[] = "/foo/bar";
24 } // namespace
26 class ShillServiceClientTest : public ShillClientUnittestBase {
27 public:
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();
46 protected:
47 scoped_ptr<ShillServiceClient> client_;
50 TEST_F(ShillServiceClientTest, PropertyChanged) {
51 const int kValue = 42;
52 // Create a signal.
53 dbus::Signal signal(flimflam::kFlimflamServiceInterface,
54 flimflam::kMonitorPropertyChanged);
55 dbus::MessageWriter writer(&signal);
56 writer.AppendString(flimflam::kSignalStrengthProperty);
57 writer.AppendVariantOfByte(kValue);
59 // Set expectations.
60 const base::FundamentalValue value(kValue);
61 MockPropertyChangeObserver observer;
62 EXPECT_CALL(observer,
63 OnPropertyChanged(
64 flimflam::kSignalStrengthProperty,
65 ValueEq(ByRef(value)))).Times(1);
67 // Add the observer
68 client_->AddPropertyChangedObserver(
69 dbus::ObjectPath(kExampleServicePath),
70 &observer);
72 // Run the signal callback.
73 SendPropertyChangedSignal(&signal);
75 // Remove the observer.
76 client_->RemovePropertyChangedObserver(
77 dbus::ObjectPath(kExampleServicePath),
78 &observer);
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;
88 // Create response.
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);
100 // Set expectations.
101 base::DictionaryValue value;
102 value.SetWithoutPathExpansion(flimflam::kSignalStrengthProperty,
103 base::Value::CreateIntegerValue(kValue));
104 PrepareForMethodCall(flimflam::kGetPropertiesFunction,
105 base::Bind(&ExpectNoArgument),
106 response.get());
107 // Call method.
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";
116 // Create response.
117 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
119 // Set expectations.
120 const base::StringValue value(kValue);
121 PrepareForMethodCall(flimflam::kSetPropertyFunction,
122 base::Bind(&ExpectStringAndValueArguments,
123 flimflam::kPassphraseProperty,
124 &value),
125 response.get());
126 // Call method.
127 MockClosure mock_closure;
128 MockErrorCallback mock_error_callback;
129 client_->SetProperty(dbus::ObjectPath(kExampleServicePath),
130 flimflam::kPassphraseProperty,
131 value,
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) {
142 // Create response.
143 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
145 // Set expectations.
146 scoped_ptr<base::DictionaryValue> arg(CreateExampleServiceProperties());
147 PrepareForMethodCall(shill::kSetPropertiesFunction,
148 base::Bind(&ExpectDictionaryValueArgument, arg.get()),
149 response.get());
151 // Call method.
152 MockClosure mock_closure;
153 MockErrorCallback mock_error_callback;
154 client_->SetProperties(dbus::ObjectPath(kExampleServicePath),
155 *arg,
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) {
166 // Create response.
167 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
169 // Set expectations.
170 PrepareForMethodCall(flimflam::kClearPropertyFunction,
171 base::Bind(&ExpectStringArgument,
172 flimflam::kPassphraseProperty),
173 response.get());
174 // Call method.
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) {
189 // Create response.
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);
198 // Set expectations.
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),
204 response.get());
205 // Call method.
206 MockListValueCallback mock_list_value_callback;
207 MockErrorCallback mock_error_callback;
208 client_->ClearProperties(dbus::ObjectPath(kExampleServicePath),
209 keys,
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) {
220 // Create response.
221 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
223 // Set expectations.
224 MockClosure mock_closure;
225 MockErrorCallback mock_error_callback;
226 PrepareForMethodCall(flimflam::kConnectFunction,
227 base::Bind(&ExpectNoArgument),
228 response.get());
229 EXPECT_CALL(mock_closure, Run()).Times(1);
230 // Call method.
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) {
240 // Create response.
241 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
243 // Set expectations.
244 PrepareForMethodCall(flimflam::kDisconnectFunction,
245 base::Bind(&ExpectNoArgument),
246 response.get());
247 // Call method.
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) {
261 // Create response.
262 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
264 // Set expectations.
265 PrepareForMethodCall(flimflam::kRemoveServiceFunction,
266 base::Bind(&ExpectNoArgument),
267 response.get());
268 // Call method.
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";
283 // Create response.
284 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
286 // Set expectations.
287 PrepareForMethodCall(flimflam::kActivateCellularModemFunction,
288 base::Bind(&ExpectStringArgument, kCarrier),
289 response.get());
290 // Call method.
291 MockClosure mock_closure;
292 MockErrorCallback mock_error_callback;
293 client_->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath),
294 kCarrier,
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