[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / chromeos / network / network_configuration_handler_unittest.cc
blob4727cea177909d2cffdabbef1c1a097ad1ac2acb
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/json/json_writer.h"
7 #include "base/message_loop.h"
8 #include "base/string_piece.h"
9 #include "base/values.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/mock_dbus_thread_manager.h"
12 #include "chromeos/dbus/mock_shill_manager_client.h"
13 #include "chromeos/dbus/mock_shill_service_client.h"
14 #include "chromeos/network/network_configuration_handler.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using ::testing::_;
19 using ::testing::Invoke;
20 using ::testing::Pointee;
21 using ::testing::Return;
22 using ::testing::SaveArg;
23 using ::testing::StrEq;
25 // Matcher to match base::Value.
26 MATCHER_P(IsEqualTo, value, "") { return arg.Equals(value); }
28 namespace chromeos {
30 namespace {
32 static std::string PrettyJson(const base::DictionaryValue& value) {
33 std::string pretty;
34 base::JSONWriter::WriteWithOptions(&value,
35 base::JSONWriter::OPTIONS_PRETTY_PRINT,
36 &pretty);
37 return pretty;
40 void DictionaryValueCallback(
41 const std::string& expected_id,
42 const std::string& expected_json,
43 const std::string& service_path,
44 const base::DictionaryValue& dictionary) {
45 std::string dict_str = PrettyJson(dictionary);
46 EXPECT_EQ(expected_json, dict_str);
47 EXPECT_EQ(expected_id, service_path);
50 void ErrorCallback(bool error_expected,
51 const std::string& expected_id,
52 const std::string& error_name,
53 const scoped_ptr<base::DictionaryValue> error_data) {
54 EXPECT_TRUE(error_expected) << "Unexpected error: " << error_name
55 << " with associated data: \n"
56 << PrettyJson(*error_data);
59 void StringResultCallback(const std::string& expected_result,
60 const std::string& result) {
61 EXPECT_EQ(expected_result, result);
64 void DBusErrorCallback(const std::string& error_name,
65 const std::string& error_message) {
66 EXPECT_TRUE(false) << "DBus Error: " << error_name << "("
67 << error_message << ")";
70 } // namespace
72 class NetworkConfigurationHandlerTest : public testing::Test {
73 public:
74 NetworkConfigurationHandlerTest()
75 : mock_manager_client_(NULL),
76 mock_service_client_(NULL),
77 dictionary_value_result_(NULL) {}
78 virtual ~NetworkConfigurationHandlerTest() {}
80 virtual void SetUp() OVERRIDE {
81 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager;
82 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus())
83 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL)));
84 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
85 mock_manager_client_ =
86 mock_dbus_thread_manager->mock_shill_manager_client();
87 mock_service_client_ =
88 mock_dbus_thread_manager->mock_shill_service_client();
90 // Initialize DBusThreadManager with a stub implementation.
91 configuration_handler_.reset(new NetworkConfigurationHandler);
92 message_loop_.RunUntilIdle();
95 virtual void TearDown() OVERRIDE {
96 configuration_handler_.reset();
97 DBusThreadManager::Shutdown();
100 // Handles responses for GetProperties method calls.
101 void OnGetProperties(
102 const dbus::ObjectPath& path,
103 const ShillClientHelper::DictionaryValueCallback& callback) {
104 callback.Run(DBUS_METHOD_CALL_SUCCESS, *dictionary_value_result_);
107 // Handles responses for SetProperties method calls.
108 void OnSetProperties(const base::DictionaryValue& properties,
109 const base::Closure& callback,
110 const ShillClientHelper::ErrorCallback& error_callback) {
111 callback.Run();
114 // Handles responses for ClearProperties method calls.
115 void OnClearProperties(
116 const dbus::ObjectPath& service_path,
117 const std::vector<std::string>& names,
118 const ShillClientHelper::ListValueCallback& callback,
119 const ShillClientHelper::ErrorCallback& error_callback) {
120 base::ListValue result;
121 result.AppendBoolean(true);
122 callback.Run(result);
125 // Handles responses for ClearProperties method calls, and simulates an error
126 // result.
127 void OnClearPropertiesError(
128 const dbus::ObjectPath& service_path,
129 const std::vector<std::string>& names,
130 const ShillClientHelper::ListValueCallback& callback,
131 const ShillClientHelper::ErrorCallback& error_callback) {
132 base::ListValue result;
133 result.AppendBoolean(false);
134 callback.Run(result);
137 void OnConnect(const dbus::ObjectPath& service_path,
138 const base::Closure& callback,
139 const ShillClientHelper::ErrorCallback& error_callback) {
140 callback.Run();
143 void OnDisconnect(const dbus::ObjectPath& service_path,
144 const base::Closure& callback,
145 const ShillClientHelper::ErrorCallback& error_callback) {
146 callback.Run();
149 void OnGetService(const base::DictionaryValue& properties,
150 const ObjectPathCallback& callback,
151 const ShillClientHelper::ErrorCallback& error_callback) {
152 callback.Run(dbus::ObjectPath("/service/2"));
155 void OnRemove(const dbus::ObjectPath& service_path,
156 const base::Closure& callback,
157 const ShillClientHelper::ErrorCallback& error_callback) {
158 callback.Run();
161 protected:
162 scoped_ptr<NetworkConfigurationHandler> configuration_handler_;
163 MockShillManagerClient* mock_manager_client_;
164 MockShillServiceClient* mock_service_client_;
165 MessageLoop message_loop_;
166 base::DictionaryValue* dictionary_value_result_;
169 TEST_F(NetworkConfigurationHandlerTest, GetProperties) {
170 std::string service_path = "/service/1";
171 std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
172 std::string networkName = "MyNetwork";
173 std::string key = "SSID";
174 scoped_ptr<base::StringValue> networkNameValue(
175 base::Value::CreateStringValue(networkName));
177 base::DictionaryValue value;
178 value.Set(key, base::Value::CreateStringValue(networkName));
179 dictionary_value_result_ = &value;
180 EXPECT_CALL(*mock_service_client_,
181 SetProperty(dbus::ObjectPath(service_path), key,
182 IsEqualTo(networkNameValue.get()), _, _)).Times(1);
183 DBusThreadManager::Get()->GetShillServiceClient()->SetProperty(
184 dbus::ObjectPath(service_path), key, *networkNameValue,
185 base::Bind(&base::DoNothing),
186 base::Bind(&DBusErrorCallback));
187 message_loop_.RunUntilIdle();
189 ShillServiceClient::DictionaryValueCallback get_properties_callback;
190 EXPECT_CALL(*mock_service_client_,
191 GetProperties(_, _)).WillOnce(
192 Invoke(this,
193 &NetworkConfigurationHandlerTest::OnGetProperties));
194 configuration_handler_->GetProperties(
195 service_path,
196 base::Bind(&DictionaryValueCallback,
197 service_path,
198 expected_json),
199 base::Bind(&ErrorCallback, false, service_path));
200 message_loop_.RunUntilIdle();
203 TEST_F(NetworkConfigurationHandlerTest, SetProperties) {
204 std::string service_path = "/service/1";
205 std::string networkName = "MyNetwork";
206 std::string key = "SSID";
207 scoped_ptr<base::StringValue> networkNameValue(
208 base::Value::CreateStringValue(networkName));
210 base::DictionaryValue value;
211 value.Set(key, base::Value::CreateStringValue(networkName));
212 dictionary_value_result_ = &value;
213 EXPECT_CALL(*mock_manager_client_,
214 ConfigureService(_, _, _)).WillOnce(
215 Invoke(this,
216 &NetworkConfigurationHandlerTest::OnSetProperties));
217 configuration_handler_->SetProperties(
218 service_path,
219 value,
220 base::Bind(&base::DoNothing),
221 base::Bind(&ErrorCallback, false, service_path));
222 message_loop_.RunUntilIdle();
225 TEST_F(NetworkConfigurationHandlerTest, ClearProperties) {
226 std::string service_path = "/service/1";
227 std::string networkName = "MyNetwork";
228 std::string key = "SSID";
229 scoped_ptr<base::StringValue> networkNameValue(
230 base::Value::CreateStringValue(networkName));
232 // First set up a value to clear.
233 base::DictionaryValue value;
234 value.Set(key, base::Value::CreateStringValue(networkName));
235 dictionary_value_result_ = &value;
236 EXPECT_CALL(*mock_manager_client_,
237 ConfigureService(_, _, _)).WillOnce(
238 Invoke(this,
239 &NetworkConfigurationHandlerTest::OnSetProperties));
240 configuration_handler_->SetProperties(
241 service_path,
242 value,
243 base::Bind(&base::DoNothing),
244 base::Bind(&ErrorCallback, false, service_path));
245 message_loop_.RunUntilIdle();
247 // Now clear it.
248 std::vector<std::string> values_to_clear;
249 values_to_clear.push_back(key);
250 EXPECT_CALL(*mock_service_client_,
251 ClearProperties(_, _, _, _)).WillOnce(
252 Invoke(this,
253 &NetworkConfigurationHandlerTest::OnClearProperties));
254 configuration_handler_->ClearProperties(
255 service_path,
256 values_to_clear,
257 base::Bind(&base::DoNothing),
258 base::Bind(&ErrorCallback, false, service_path));
259 message_loop_.RunUntilIdle();
262 TEST_F(NetworkConfigurationHandlerTest, ClearPropertiesError) {
263 std::string service_path = "/service/1";
264 std::string networkName = "MyNetwork";
265 std::string key = "SSID";
266 scoped_ptr<base::StringValue> networkNameValue(
267 base::Value::CreateStringValue(networkName));
269 // First set up a value to clear.
270 base::DictionaryValue value;
271 value.Set(key, base::Value::CreateStringValue(networkName));
272 dictionary_value_result_ = &value;
273 EXPECT_CALL(*mock_manager_client_,
274 ConfigureService(_, _, _)).WillOnce(
275 Invoke(this,
276 &NetworkConfigurationHandlerTest::OnSetProperties));
277 configuration_handler_->SetProperties(
278 service_path,
279 value,
280 base::Bind(&base::DoNothing),
281 base::Bind(&ErrorCallback, false, service_path));
282 message_loop_.RunUntilIdle();
284 // Now clear it.
285 std::vector<std::string> values_to_clear;
286 values_to_clear.push_back(key);
287 EXPECT_CALL(
288 *mock_service_client_,
289 ClearProperties(_, _, _, _)).WillOnce(
290 Invoke(this,
291 &NetworkConfigurationHandlerTest::OnClearPropertiesError));
292 configuration_handler_->ClearProperties(
293 service_path,
294 values_to_clear,
295 base::Bind(&base::DoNothing),
296 base::Bind(&ErrorCallback, true, service_path));
297 message_loop_.RunUntilIdle();
300 TEST_F(NetworkConfigurationHandlerTest, Connect) {
301 std::string service_path = "/service/1";
303 EXPECT_CALL(*mock_service_client_,
304 Connect(_, _, _)).WillOnce(
305 Invoke(this,
306 &NetworkConfigurationHandlerTest::OnConnect));
307 configuration_handler_->Connect(
308 service_path,
309 base::Bind(&base::DoNothing),
310 base::Bind(&ErrorCallback, false, service_path));
311 message_loop_.RunUntilIdle();
314 TEST_F(NetworkConfigurationHandlerTest, Disconnect) {
315 std::string service_path = "/service/1";
317 EXPECT_CALL(*mock_service_client_,
318 Disconnect(_, _, _)).WillOnce(
319 Invoke(this,
320 &NetworkConfigurationHandlerTest::OnDisconnect));
321 configuration_handler_->Disconnect(
322 service_path,
323 base::Bind(&base::DoNothing),
324 base::Bind(&ErrorCallback, false, service_path));
325 message_loop_.RunUntilIdle();
328 TEST_F(NetworkConfigurationHandlerTest, CreateConfiguration) {
329 std::string expected_json = "{\n \"SSID\": \"MyNetwork\"\n}\n";
330 std::string networkName = "MyNetwork";
331 std::string key = "SSID";
332 scoped_ptr<base::StringValue> networkNameValue(
333 base::Value::CreateStringValue(networkName));
334 base::DictionaryValue value;
335 value.Set(key, base::Value::CreateStringValue(networkName));
337 EXPECT_CALL(
338 *mock_manager_client_,
339 GetService(_, _, _)).WillOnce(
340 Invoke(this,
341 &NetworkConfigurationHandlerTest::OnGetService));
342 configuration_handler_->CreateConfiguration(
343 value,
344 base::Bind(&StringResultCallback, std::string("/service/2")),
345 base::Bind(&ErrorCallback, false, std::string("")));
346 message_loop_.RunUntilIdle();
349 TEST_F(NetworkConfigurationHandlerTest, RemoveConfiguration) {
350 std::string service_path = "/service/1";
352 EXPECT_CALL(
353 *mock_service_client_,
354 Remove(_, _, _)).WillOnce(
355 Invoke(this,
356 &NetworkConfigurationHandlerTest::OnRemove));
357 configuration_handler_->RemoveConfiguration(
358 service_path,
359 base::Bind(&base::DoNothing),
360 base::Bind(&ErrorCallback, false, service_path));
361 message_loop_.RunUntilIdle();
364 } // namespace chromeos