Fix a type mismatch on Windows caused by r201738.
[chromium-blink-merge.git] / chromeos / dbus / shill_service_client_unittest.cc
blob736c215a96c1f61f2cb24ac7e8eaa23613f39f81
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 mock_bus_));
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, ClearProperty) {
142 // Create response.
143 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
145 // Set expectations.
146 PrepareForMethodCall(flimflam::kClearPropertyFunction,
147 base::Bind(&ExpectStringArgument,
148 flimflam::kPassphraseProperty),
149 response.get());
150 // Call method.
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) {
165 // Create response.
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);
174 // Set expectations.
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),
180 response.get());
181 // Call method.
182 MockListValueCallback mock_list_value_callback;
183 MockErrorCallback mock_error_callback;
184 client_->ClearProperties(dbus::ObjectPath(kExampleServicePath),
185 keys,
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) {
196 // Create response.
197 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
199 // Set expectations.
200 MockClosure mock_closure;
201 MockErrorCallback mock_error_callback;
202 PrepareForMethodCall(flimflam::kConnectFunction,
203 base::Bind(&ExpectNoArgument),
204 response.get());
205 EXPECT_CALL(mock_closure, Run()).Times(1);
206 // Call method.
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) {
216 // Create response.
217 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
219 // Set expectations.
220 PrepareForMethodCall(flimflam::kDisconnectFunction,
221 base::Bind(&ExpectNoArgument),
222 response.get());
223 // Call method.
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) {
237 // Create response.
238 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
240 // Set expectations.
241 PrepareForMethodCall(flimflam::kRemoveServiceFunction,
242 base::Bind(&ExpectNoArgument),
243 response.get());
244 // Call method.
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";
259 // Create response.
260 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
262 // Set expectations.
263 PrepareForMethodCall(flimflam::kActivateCellularModemFunction,
264 base::Bind(&ExpectStringArgument, kCarrier),
265 response.get());
266 // Call method.
267 MockClosure mock_closure;
268 MockErrorCallback mock_error_callback;
269 client_->ActivateCellularModem(dbus::ObjectPath(kExampleServicePath),
270 kCarrier,
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";
282 // Create response.
283 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
285 // Set expectations.
286 PrepareForMethodCall(flimflam::kActivateCellularModemFunction,
287 base::Bind(&ExpectStringArgument, kCarrier),
288 response.get());
289 // Call method.
290 const bool result = client_->CallActivateCellularModemAndBlock(
291 dbus::ObjectPath(kExampleServicePath), kCarrier);
292 EXPECT_TRUE(result);
295 } // namespace chromeos