OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/values_util.h" | 5 #include "dbus/values_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 dictionary_value.SetDouble(kKey3, kDoubleValue); | 275 dictionary_value.SetDouble(kKey3, kDoubleValue); |
276 dictionary_value.SetString(kKey4, kStringValue); | 276 dictionary_value.SetString(kKey4, kStringValue); |
277 | 277 |
278 // Pop a dictinoary. | 278 // Pop a dictinoary. |
279 dbus::MessageReader reader(response.get()); | 279 dbus::MessageReader reader(response.get()); |
280 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); | 280 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
281 EXPECT_TRUE(value.get() != NULL); | 281 EXPECT_TRUE(value.get() != NULL); |
282 EXPECT_TRUE(value->Equals(&dictionary_value)); | 282 EXPECT_TRUE(value->Equals(&dictionary_value)); |
283 } | 283 } |
284 | 284 |
| 285 TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { |
| 286 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 287 // Append a dictionary. |
| 288 dbus::MessageWriter writer(response.get()); |
| 289 dbus::MessageWriter sub_writer(NULL); |
| 290 dbus::MessageWriter entry_writer(NULL); |
| 291 writer.OpenArray("{sv}", &sub_writer); |
| 292 sub_writer.OpenDictEntry(&entry_writer); |
| 293 const std::string kKey1 = "www.example.com"; // String including dots. |
| 294 entry_writer.AppendString(kKey1); |
| 295 const bool kBoolValue = true; |
| 296 entry_writer.AppendVariantOfBool(kBoolValue); |
| 297 sub_writer.CloseContainer(&entry_writer); |
| 298 sub_writer.OpenDictEntry(&entry_writer); |
| 299 const std::string kKey2 = ".example"; // String starting with a dot. |
| 300 entry_writer.AppendString(kKey2); |
| 301 const int32 kInt32Value = -45; |
| 302 entry_writer.AppendVariantOfInt32(kInt32Value); |
| 303 sub_writer.CloseContainer(&entry_writer); |
| 304 sub_writer.OpenDictEntry(&entry_writer); |
| 305 const std::string kKey3 = "example."; // String ending with a dot. |
| 306 entry_writer.AppendString(kKey3); |
| 307 const double kDoubleValue = 4.9; |
| 308 entry_writer.AppendVariantOfDouble(kDoubleValue); |
| 309 sub_writer.CloseContainer(&entry_writer); |
| 310 writer.CloseContainer(&sub_writer); |
| 311 |
| 312 // Create the expected value. |
| 313 DictionaryValue dictionary_value; |
| 314 dictionary_value.SetWithoutPathExpansion( |
| 315 kKey1, Value::CreateBooleanValue(kBoolValue)); |
| 316 dictionary_value.SetWithoutPathExpansion( |
| 317 kKey2, Value::CreateIntegerValue(kInt32Value)); |
| 318 dictionary_value.SetWithoutPathExpansion( |
| 319 kKey3, Value::CreateDoubleValue(kDoubleValue)); |
| 320 |
| 321 // Pop a dictinoary. |
| 322 dbus::MessageReader reader(response.get()); |
| 323 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
| 324 EXPECT_TRUE(value.get() != NULL); |
| 325 EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 326 } |
| 327 |
285 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { | 328 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
286 // Create test data. | 329 // Create test data. |
287 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; | 330 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; |
288 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); | 331 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); |
289 std::vector<double> keys(values.size()); | 332 std::vector<double> keys(values.size()); |
290 for (size_t i = 0; i != values.size(); ++i) | 333 for (size_t i = 0; i != values.size(); ++i) |
291 keys[i] = sqrt(values[i]); | 334 keys[i] = sqrt(values[i]); |
292 | 335 |
293 // Append a dictionary. | 336 // Append a dictionary. |
294 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 337 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
295 dbus::MessageWriter writer(response.get()); | 338 dbus::MessageWriter writer(response.get()); |
296 dbus::MessageWriter sub_writer(NULL); | 339 dbus::MessageWriter sub_writer(NULL); |
297 writer.OpenArray("{di}", &sub_writer); | 340 writer.OpenArray("{di}", &sub_writer); |
298 for (size_t i = 0; i != values.size(); ++i) { | 341 for (size_t i = 0; i != values.size(); ++i) { |
299 dbus::MessageWriter entry_writer(NULL); | 342 dbus::MessageWriter entry_writer(NULL); |
300 sub_writer.OpenDictEntry(&entry_writer); | 343 sub_writer.OpenDictEntry(&entry_writer); |
301 entry_writer.AppendDouble(keys[i]); | 344 entry_writer.AppendDouble(keys[i]); |
302 entry_writer.AppendInt32(values[i]); | 345 entry_writer.AppendInt32(values[i]); |
303 sub_writer.CloseContainer(&entry_writer); | 346 sub_writer.CloseContainer(&entry_writer); |
304 } | 347 } |
305 writer.CloseContainer(&sub_writer); | 348 writer.CloseContainer(&sub_writer); |
306 | 349 |
307 // Create the expected value. | 350 // Create the expected value. |
308 DictionaryValue dictionary_value; | 351 DictionaryValue dictionary_value; |
309 for (size_t i = 0; i != values.size(); ++i) { | 352 for (size_t i = 0; i != values.size(); ++i) { |
310 scoped_ptr<Value> key_value(Value::CreateDoubleValue(keys[i])); | 353 scoped_ptr<Value> key_value(Value::CreateDoubleValue(keys[i])); |
311 std::string key_string; | 354 std::string key_string; |
312 base::JSONWriter::Write(key_value.get(), &key_string); | 355 base::JSONWriter::Write(key_value.get(), &key_string); |
313 dictionary_value.SetInteger(key_string, values[i]); | 356 dictionary_value.SetWithoutPathExpansion( |
| 357 key_string, Value::CreateIntegerValue(values[i])); |
314 } | 358 } |
315 | 359 |
316 // Pop a dictionary. | 360 // Pop a dictionary. |
317 dbus::MessageReader reader(response.get()); | 361 dbus::MessageReader reader(response.get()); |
318 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); | 362 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
319 EXPECT_TRUE(value.get() != NULL); | 363 EXPECT_TRUE(value.get() != NULL); |
320 EXPECT_TRUE(value->Equals(&dictionary_value)); | 364 EXPECT_TRUE(value->Equals(&dictionary_value)); |
321 } | 365 } |
OLD | NEW |