• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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"
14 
15 using testing::_;
16 using testing::ByRef;
17 
18 namespace chromeos {
19 
20 namespace {
21 
22 const char kExampleServicePath[] = "/foo/bar";
23 
24 }  // namespace
25 
26 class ShillServiceClientTest : public ShillClientUnittestBase {
27  public:
ShillServiceClientTest()28   ShillServiceClientTest()
29       : ShillClientUnittestBase(shill::kFlimflamServiceInterface,
30                                    dbus::ObjectPath(kExampleServicePath)) {
31   }
32 
SetUp()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();
40   }
41 
TearDown()42   virtual void TearDown() {
43     ShillClientUnittestBase::TearDown();
44   }
45 
46  protected:
47   scoped_ptr<ShillServiceClient> client_;
48 };
49 
TEST_F(ShillServiceClientTest,PropertyChanged)50 TEST_F(ShillServiceClientTest, PropertyChanged) {
51   const int kValue = 42;
52   // Create a signal.
53   dbus::Signal signal(shill::kFlimflamServiceInterface,
54                       shill::kMonitorPropertyChanged);
55   dbus::MessageWriter writer(&signal);
56   writer.AppendString(shill::kSignalStrengthProperty);
57   writer.AppendVariantOfByte(kValue);
58 
59   // Set expectations.
60   const base::FundamentalValue value(kValue);
61   MockPropertyChangeObserver observer;
62   EXPECT_CALL(observer,
63               OnPropertyChanged(
64                   shill::kSignalStrengthProperty,
65                   ValueEq(ByRef(value)))).Times(1);
66 
67   // Add the observer
68   client_->AddPropertyChangedObserver(
69       dbus::ObjectPath(kExampleServicePath),
70       &observer);
71 
72   // Run the signal callback.
73   SendPropertyChangedSignal(&signal);
74 
75   // Remove the observer.
76   client_->RemovePropertyChangedObserver(
77       dbus::ObjectPath(kExampleServicePath),
78       &observer);
79 
80   EXPECT_CALL(observer, OnPropertyChanged(_, _)).Times(0);
81 
82   // Run the signal callback again and make sure the observer isn't called.
83   SendPropertyChangedSignal(&signal);
84 }
85 
TEST_F(ShillServiceClientTest,GetProperties)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(shill::kSignalStrengthProperty);
96   entry_writer.AppendVariantOfByte(kValue);
97   array_writer.CloseContainer(&entry_writer);
98   writer.CloseContainer(&array_writer);
99 
100   // Set expectations.
101   base::DictionaryValue value;
102   value.SetWithoutPathExpansion(shill::kSignalStrengthProperty,
103                                 base::Value::CreateIntegerValue(kValue));
104   PrepareForMethodCall(shill::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();
112 }
113 
TEST_F(ShillServiceClientTest,SetProperty)114 TEST_F(ShillServiceClientTest, SetProperty) {
115   const char kValue[] = "passphrase";
116   // Create response.
117   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
118 
119   // Set expectations.
120   const base::StringValue value(kValue);
121   PrepareForMethodCall(shill::kSetPropertyFunction,
122                        base::Bind(&ExpectStringAndValueArguments,
123                                   shill::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                        shill::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);
136 
137   // Run the message loop.
138   message_loop_.RunUntilIdle();
139 }
140 
TEST_F(ShillServiceClientTest,SetProperties)141 TEST_F(ShillServiceClientTest, SetProperties) {
142   // Create response.
143   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
144 
145   // Set expectations.
146   scoped_ptr<base::DictionaryValue> arg(CreateExampleServiceProperties());
147   PrepareForMethodCall(shill::kSetPropertiesFunction,
148                        base::Bind(&ExpectDictionaryValueArgument, arg.get()),
149                        response.get());
150 
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);
160 
161   // Run the message loop.
162   message_loop_.RunUntilIdle();
163 }
164 
TEST_F(ShillServiceClientTest,ClearProperty)165 TEST_F(ShillServiceClientTest, ClearProperty) {
166   // Create response.
167   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
168 
169   // Set expectations.
170   PrepareForMethodCall(shill::kClearPropertyFunction,
171                        base::Bind(&ExpectStringArgument,
172                                   shill::kPassphraseProperty),
173                        response.get());
174   // Call method.
175   MockClosure mock_closure;
176   MockErrorCallback mock_error_callback;
177   client_->ClearProperty(dbus::ObjectPath(kExampleServicePath),
178                          shill::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);
183 
184   // Run the message loop.
185   message_loop_.RunUntilIdle();
186 }
187 
TEST_F(ShillServiceClientTest,ClearProperties)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);
197 
198   // Set expectations.
199   std::vector<std::string> keys;
200   keys.push_back(shill::kPassphraseProperty);
201   keys.push_back(shill::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);
214 
215   // Run the message loop.
216   message_loop_.RunUntilIdle();
217 }
218 
TEST_F(ShillServiceClientTest,Connect)219 TEST_F(ShillServiceClientTest, Connect) {
220   // Create response.
221   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
222 
223   // Set expectations.
224   MockClosure mock_closure;
225   MockErrorCallback mock_error_callback;
226   PrepareForMethodCall(shill::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());
234 
235   // Run the message loop.
236   message_loop_.RunUntilIdle();
237 }
238 
TEST_F(ShillServiceClientTest,Disconnect)239 TEST_F(ShillServiceClientTest, Disconnect) {
240   // Create response.
241   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
242 
243   // Set expectations.
244   PrepareForMethodCall(shill::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);
255 
256   // Run the message loop.
257   message_loop_.RunUntilIdle();
258 }
259 
TEST_F(ShillServiceClientTest,Remove)260 TEST_F(ShillServiceClientTest, Remove) {
261   // Create response.
262   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
263 
264   // Set expectations.
265   PrepareForMethodCall(shill::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);
276 
277   // Run the message loop.
278   message_loop_.RunUntilIdle();
279 }
280 
TEST_F(ShillServiceClientTest,ActivateCellularModem)281 TEST_F(ShillServiceClientTest, ActivateCellularModem) {
282   const char kCarrier[] = "carrier";
283   // Create response.
284   scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
285 
286   // Set expectations.
287   PrepareForMethodCall(shill::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);
299 
300   // Run the message loop.
301   message_loop_.RunUntilIdle();
302 }
303 
304 }  // namespace chromeos
305