Remove migrateNetworkPredictionPreferences().
[chromium-blink-merge.git] / chromeos / dbus / shill_device_client_unittest.cc
blob164f5920c9874157b9aefca5099ba7c4f5976d14
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_device_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 kExampleDevicePath[] = "/foo/bar";
24 // Expects the reader to have a string and a bool.
25 void ExpectStringAndBoolArguments(const std::string& expected_string,
26 bool expected_bool,
27 dbus::MessageReader* reader) {
28 std::string arg1;
29 ASSERT_TRUE(reader->PopString(&arg1));
30 EXPECT_EQ(expected_string, arg1);
31 bool arg2 = false;
32 ASSERT_TRUE(reader->PopBool(&arg2));
33 EXPECT_EQ(expected_bool, arg2);
34 EXPECT_FALSE(reader->HasMoreData());
37 // Expects the reader to have two strings.
38 void ExpectTwoStringArguments(const std::string& expected_string1,
39 const std::string& expected_string2,
40 dbus::MessageReader* reader) {
41 std::string arg1;
42 ASSERT_TRUE(reader->PopString(&arg1));
43 EXPECT_EQ(expected_string1, arg1);
44 std::string arg2;
45 ASSERT_TRUE(reader->PopString(&arg2));
46 EXPECT_EQ(expected_string2, arg2);
47 EXPECT_FALSE(reader->HasMoreData());
50 } // namespace
52 class ShillDeviceClientTest : public ShillClientUnittestBase {
53 public:
54 ShillDeviceClientTest()
55 : ShillClientUnittestBase(shill::kFlimflamDeviceInterface,
56 dbus::ObjectPath(kExampleDevicePath)) {
59 void SetUp() override {
60 ShillClientUnittestBase::SetUp();
61 // Create a client with the mock bus.
62 client_.reset(ShillDeviceClient::Create());
63 client_->Init(mock_bus_.get());
64 // Run the message loop to run the signal connection result callback.
65 message_loop_.RunUntilIdle();
68 void TearDown() override { ShillClientUnittestBase::TearDown(); }
70 protected:
71 scoped_ptr<ShillDeviceClient> client_;
74 TEST_F(ShillDeviceClientTest, PropertyChanged) {
75 const bool kValue = true;
76 // Create a signal.
77 dbus::Signal signal(shill::kFlimflamDeviceInterface,
78 shill::kMonitorPropertyChanged);
79 dbus::MessageWriter writer(&signal);
80 writer.AppendString(shill::kCellularAllowRoamingProperty);
81 writer.AppendVariantOfBool(kValue);
83 // Set expectations.
84 const base::FundamentalValue value(kValue);
85 MockPropertyChangeObserver observer;
86 EXPECT_CALL(observer,
87 OnPropertyChanged(shill::kCellularAllowRoamingProperty,
88 ValueEq(ByRef(value)))).Times(1);
90 // Add the observer
91 client_->AddPropertyChangedObserver(
92 dbus::ObjectPath(kExampleDevicePath),
93 &observer);
95 // Run the signal callback.
96 SendPropertyChangedSignal(&signal);
98 // Remove the observer.
99 client_->RemovePropertyChangedObserver(
100 dbus::ObjectPath(kExampleDevicePath),
101 &observer);
103 EXPECT_CALL(observer,
104 OnPropertyChanged(shill::kCellularAllowRoamingProperty,
105 ValueEq(ByRef(value)))).Times(0);
107 // Run the signal callback again and make sure the observer isn't called.
108 SendPropertyChangedSignal(&signal);
111 TEST_F(ShillDeviceClientTest, GetProperties) {
112 const bool kValue = true;
113 // Create response.
114 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
115 dbus::MessageWriter writer(response.get());
116 dbus::MessageWriter array_writer(NULL);
117 writer.OpenArray("{sv}", &array_writer);
118 dbus::MessageWriter entry_writer(NULL);
119 array_writer.OpenDictEntry(&entry_writer);
120 entry_writer.AppendString(shill::kCellularAllowRoamingProperty);
121 entry_writer.AppendVariantOfBool(kValue);
122 array_writer.CloseContainer(&entry_writer);
123 writer.CloseContainer(&array_writer);
125 // Set expectations.
126 base::DictionaryValue value;
127 value.SetWithoutPathExpansion(shill::kCellularAllowRoamingProperty,
128 new base::FundamentalValue(kValue));
129 PrepareForMethodCall(shill::kGetPropertiesFunction,
130 base::Bind(&ExpectNoArgument),
131 response.get());
132 // Call method.
133 client_->GetProperties(dbus::ObjectPath(kExampleDevicePath),
134 base::Bind(&ExpectDictionaryValueResult, &value));
135 // Run the message loop.
136 message_loop_.RunUntilIdle();
139 TEST_F(ShillDeviceClientTest, ProposeScan) {
140 // Create response.
141 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
143 // Set expectations.
144 PrepareForMethodCall(shill::kProposeScanFunction,
145 base::Bind(&ExpectNoArgument),
146 response.get());
147 // Call method.
148 client_->ProposeScan(dbus::ObjectPath(kExampleDevicePath),
149 base::Bind(&ExpectNoResultValue));
150 // Run the message loop.
151 message_loop_.RunUntilIdle();
154 TEST_F(ShillDeviceClientTest, SetProperty) {
155 const bool kValue = true;
156 // Create response.
157 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
159 // Set expectations.
160 const base::FundamentalValue value(kValue);
161 PrepareForMethodCall(shill::kSetPropertyFunction,
162 base::Bind(&ExpectStringAndValueArguments,
163 shill::kCellularAllowRoamingProperty,
164 &value),
165 response.get());
166 // Call method.
167 MockClosure mock_closure;
168 MockErrorCallback mock_error_callback;
169 client_->SetProperty(dbus::ObjectPath(kExampleDevicePath),
170 shill::kCellularAllowRoamingProperty,
171 value,
172 mock_closure.GetCallback(),
173 mock_error_callback.GetCallback());
174 EXPECT_CALL(mock_closure, Run()).Times(1);
175 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
177 // Run the message loop.
178 message_loop_.RunUntilIdle();
181 TEST_F(ShillDeviceClientTest, ClearProperty) {
182 // Create response.
183 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
185 // Set expectations.
186 PrepareForMethodCall(shill::kClearPropertyFunction,
187 base::Bind(&ExpectStringArgument,
188 shill::kCellularAllowRoamingProperty),
189 response.get());
190 // Call method.
191 client_->ClearProperty(dbus::ObjectPath(kExampleDevicePath),
192 shill::kCellularAllowRoamingProperty,
193 base::Bind(&ExpectNoResultValue));
194 // Run the message loop.
195 message_loop_.RunUntilIdle();
198 TEST_F(ShillDeviceClientTest, AddIPConfig) {
199 const dbus::ObjectPath expected_result("/result/path");
200 // Create response.
201 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
202 dbus::MessageWriter writer(response.get());
203 writer.AppendObjectPath(expected_result);
205 // Set expectations.
206 PrepareForMethodCall(shill::kAddIPConfigFunction,
207 base::Bind(&ExpectStringArgument, shill::kTypeDHCP),
208 response.get());
209 // Call method.
210 client_->AddIPConfig(dbus::ObjectPath(kExampleDevicePath),
211 shill::kTypeDHCP,
212 base::Bind(&ExpectObjectPathResult, expected_result));
213 // Run the message loop.
214 message_loop_.RunUntilIdle();
217 TEST_F(ShillDeviceClientTest, RequirePin) {
218 const char kPin[] = "123456";
219 const bool kRequired = true;
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(shill::kRequirePinFunction,
227 base::Bind(&ExpectStringAndBoolArguments,
228 kPin,
229 kRequired),
230 response.get());
231 EXPECT_CALL(mock_closure, Run()).Times(1);
232 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
233 // Call method.
234 client_->RequirePin(dbus::ObjectPath(kExampleDevicePath),
235 kPin,
236 kRequired,
237 mock_closure.GetCallback(),
238 mock_error_callback.GetCallback());
239 // Run the message loop.
240 message_loop_.RunUntilIdle();
243 TEST_F(ShillDeviceClientTest, EnterPin) {
244 const char kPin[] = "123456";
245 // Create response.
246 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
248 // Set expectations.
249 MockClosure mock_closure;
250 MockErrorCallback mock_error_callback;
251 PrepareForMethodCall(shill::kEnterPinFunction,
252 base::Bind(&ExpectStringArgument,
253 kPin),
254 response.get());
255 EXPECT_CALL(mock_closure, Run()).Times(1);
256 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
258 // Call method.
259 client_->EnterPin(dbus::ObjectPath(kExampleDevicePath),
260 kPin,
261 mock_closure.GetCallback(),
262 mock_error_callback.GetCallback());
263 // Run the message loop.
264 message_loop_.RunUntilIdle();
267 TEST_F(ShillDeviceClientTest, UnblockPin) {
268 const char kPuk[] = "987654";
269 const char kPin[] = "123456";
270 // Create response.
271 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
273 // Set expectations.
274 MockClosure mock_closure;
275 MockErrorCallback mock_error_callback;
276 PrepareForMethodCall(shill::kUnblockPinFunction,
277 base::Bind(&ExpectTwoStringArguments, kPuk, kPin),
278 response.get());
279 EXPECT_CALL(mock_closure, Run()).Times(1);
280 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
282 // Call method.
283 client_->UnblockPin(dbus::ObjectPath(kExampleDevicePath),
284 kPuk,
285 kPin,
286 mock_closure.GetCallback(),
287 mock_error_callback.GetCallback());
288 // Run the message loop.
289 message_loop_.RunUntilIdle();
292 TEST_F(ShillDeviceClientTest, ChangePin) {
293 const char kOldPin[] = "123456";
294 const char kNewPin[] = "234567";
295 // Create response.
296 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
298 // Set expectations.
299 MockClosure mock_closure;
300 MockErrorCallback mock_error_callback;
301 PrepareForMethodCall(shill::kChangePinFunction,
302 base::Bind(&ExpectTwoStringArguments,
303 kOldPin,
304 kNewPin),
305 response.get());
306 EXPECT_CALL(mock_closure, Run()).Times(1);
307 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
309 // Call method.
310 client_->ChangePin(dbus::ObjectPath(kExampleDevicePath),
311 kOldPin,
312 kNewPin,
313 mock_closure.GetCallback(),
314 mock_error_callback.GetCallback());
315 // Run the message loop.
316 message_loop_.RunUntilIdle();
319 TEST_F(ShillDeviceClientTest, Register) {
320 const char kNetworkId[] = "networkid";
321 // Create response.
322 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
324 // Set expectations.
325 MockClosure mock_closure;
326 MockErrorCallback mock_error_callback;
327 PrepareForMethodCall(shill::kRegisterFunction,
328 base::Bind(&ExpectStringArgument, kNetworkId),
329 response.get());
330 EXPECT_CALL(mock_closure, Run()).Times(1);
331 EXPECT_CALL(mock_error_callback, Run(_, _)).Times(0);
333 // Call method.
334 client_->Register(dbus::ObjectPath(kExampleDevicePath),
335 kNetworkId,
336 mock_closure.GetCallback(),
337 mock_error_callback.GetCallback());
338 // Run the message loop.
339 message_loop_.RunUntilIdle();
342 TEST_F(ShillDeviceClientTest, SetCarrier) {
343 const char kCarrier[] = "carrier";
344 // Create response.
345 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
347 // Set expectations.
348 MockClosure mock_closure;
349 MockErrorCallback mock_error_callback;
350 PrepareForMethodCall(shill::kSetCarrierFunction,
351 base::Bind(&ExpectStringArgument, kCarrier),
352 response.get());
353 EXPECT_CALL(mock_closure, Run()).Times(1);
354 // Call method.
355 client_->SetCarrier(dbus::ObjectPath(kExampleDevicePath),
356 kCarrier,
357 mock_closure.GetCallback(),
358 mock_error_callback.GetCallback());
359 // Run the message loop.
360 message_loop_.RunUntilIdle();
363 TEST_F(ShillDeviceClientTest, Reset) {
364 // Create response.
365 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
367 // Set expectations.
368 MockClosure mock_closure;
369 MockErrorCallback mock_error_callback;
370 PrepareForMethodCall(shill::kResetFunction,
371 base::Bind(&ExpectNoArgument),
372 response.get());
373 EXPECT_CALL(mock_closure, Run()).Times(1);
374 // Call method.
375 client_->Reset(dbus::ObjectPath(kExampleDevicePath),
376 mock_closure.GetCallback(),
377 mock_error_callback.GetCallback());
378 // Run the message loop.
379 message_loop_.RunUntilIdle();
382 } // namespace chromeos