Updating trunk VERSION from 833.0 to 834.0
[chromium-blink-merge.git] / base / values_unittest.cc
blob78163fd8ab597836877404901493f59256937624
1 // Copyright (c) 2011 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 <limits>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/string16.h"
9 #include "base/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 TEST(ValuesTest, Basic) {
16 // Test basic dictionary getting/setting
17 DictionaryValue settings;
18 std::string homepage = "http://google.com";
19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
20 ASSERT_EQ(std::string("http://google.com"), homepage);
22 ASSERT_FALSE(settings.Get("global", NULL));
23 settings.Set("global", Value::CreateBooleanValue(true));
24 ASSERT_TRUE(settings.Get("global", NULL));
25 settings.SetString("global.homepage", "http://scurvy.com");
26 ASSERT_TRUE(settings.Get("global", NULL));
27 homepage = "http://google.com";
28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
29 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
31 // Test storing a dictionary in a list.
32 ListValue* toolbar_bookmarks;
33 ASSERT_FALSE(
34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
36 toolbar_bookmarks = new ListValue;
37 settings.Set("global.toolbar.bookmarks", toolbar_bookmarks);
38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
40 DictionaryValue* new_bookmark = new DictionaryValue;
41 new_bookmark->SetString("name", "Froogle");
42 new_bookmark->SetString("url", "http://froogle.com");
43 toolbar_bookmarks->Append(new_bookmark);
45 ListValue* bookmark_list;
46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
47 DictionaryValue* bookmark;
48 ASSERT_EQ(1U, bookmark_list->GetSize());
49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
50 std::string bookmark_name = "Unnamed";
51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
52 ASSERT_EQ(std::string("Froogle"), bookmark_name);
53 std::string bookmark_url;
54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
58 TEST(ValuesTest, List) {
59 scoped_ptr<ListValue> mixed_list(new ListValue());
60 mixed_list->Set(0, Value::CreateBooleanValue(true));
61 mixed_list->Set(1, Value::CreateIntegerValue(42));
62 mixed_list->Set(2, Value::CreateDoubleValue(88.8));
63 mixed_list->Set(3, Value::CreateStringValue("foo"));
64 ASSERT_EQ(4u, mixed_list->GetSize());
66 Value *value = NULL;
67 bool bool_value = false;
68 int int_value = 0;
69 double double_value = 0.0;
70 std::string string_value;
72 ASSERT_FALSE(mixed_list->Get(4, &value));
74 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
75 ASSERT_EQ(0, int_value);
76 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
77 ASSERT_FALSE(bool_value);
78 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
79 ASSERT_EQ("", string_value);
80 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
81 ASSERT_EQ(0, int_value);
82 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
83 ASSERT_FALSE(bool_value);
85 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
86 ASSERT_TRUE(bool_value);
87 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
88 ASSERT_EQ(42, int_value);
89 // implicit conversion from Integer to Double should be possible.
90 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
91 ASSERT_EQ(42, double_value);
92 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
93 ASSERT_EQ(88.8, double_value);
94 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
95 ASSERT_EQ("foo", string_value);
98 TEST(ValuesTest, BinaryValue) {
99 char* buffer = NULL;
100 // Passing a null buffer pointer doesn't yield a BinaryValue
101 scoped_ptr<BinaryValue> binary(BinaryValue::Create(buffer, 0));
102 ASSERT_FALSE(binary.get());
104 // If you want to represent an empty binary value, use a zero-length buffer.
105 buffer = new char[1];
106 ASSERT_TRUE(buffer);
107 binary.reset(BinaryValue::Create(buffer, 0));
108 ASSERT_TRUE(binary.get());
109 ASSERT_TRUE(binary->GetBuffer());
110 ASSERT_EQ(buffer, binary->GetBuffer());
111 ASSERT_EQ(0U, binary->GetSize());
113 // Test the common case of a non-empty buffer
114 buffer = new char[15];
115 binary.reset(BinaryValue::Create(buffer, 15));
116 ASSERT_TRUE(binary.get());
117 ASSERT_TRUE(binary->GetBuffer());
118 ASSERT_EQ(buffer, binary->GetBuffer());
119 ASSERT_EQ(15U, binary->GetSize());
121 char stack_buffer[42];
122 memset(stack_buffer, '!', 42);
123 binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42));
124 ASSERT_TRUE(binary.get());
125 ASSERT_TRUE(binary->GetBuffer());
126 ASSERT_NE(stack_buffer, binary->GetBuffer());
127 ASSERT_EQ(42U, binary->GetSize());
128 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
131 TEST(ValuesTest, StringValue) {
132 // Test overloaded CreateStringValue.
133 scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow"));
134 ASSERT_TRUE(narrow_value.get());
135 ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING));
136 scoped_ptr<Value> utf16_value(
137 Value::CreateStringValue(ASCIIToUTF16("utf16")));
138 ASSERT_TRUE(utf16_value.get());
139 ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING));
141 // Test overloaded GetString.
142 std::string narrow = "http://google.com";
143 string16 utf16 = ASCIIToUTF16("http://google.com");
144 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
145 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
146 ASSERT_EQ(std::string("narrow"), narrow);
147 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
149 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
150 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
151 ASSERT_EQ(std::string("utf16"), narrow);
152 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
155 // This is a Value object that allows us to tell if it's been
156 // properly deleted by modifying the value of external flag on destruction.
157 class DeletionTestValue : public Value {
158 public:
159 explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) {
160 Init(deletion_flag); // Separate function so that we can use ASSERT_*
163 void Init(bool* deletion_flag) {
164 ASSERT_TRUE(deletion_flag);
165 deletion_flag_ = deletion_flag;
166 *deletion_flag_ = false;
169 ~DeletionTestValue() {
170 *deletion_flag_ = true;
173 private:
174 bool* deletion_flag_;
177 TEST(ValuesTest, ListDeletion) {
178 bool deletion_flag = true;
181 ListValue list;
182 list.Append(new DeletionTestValue(&deletion_flag));
183 EXPECT_FALSE(deletion_flag);
185 EXPECT_TRUE(deletion_flag);
188 ListValue list;
189 list.Append(new DeletionTestValue(&deletion_flag));
190 EXPECT_FALSE(deletion_flag);
191 list.Clear();
192 EXPECT_TRUE(deletion_flag);
196 ListValue list;
197 list.Append(new DeletionTestValue(&deletion_flag));
198 EXPECT_FALSE(deletion_flag);
199 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
200 EXPECT_TRUE(deletion_flag);
204 TEST(ValuesTest, ListRemoval) {
205 bool deletion_flag = true;
206 Value* removed_item = NULL;
209 ListValue list;
210 list.Append(new DeletionTestValue(&deletion_flag));
211 EXPECT_FALSE(deletion_flag);
212 EXPECT_EQ(1U, list.GetSize());
213 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
214 &removed_item));
215 EXPECT_FALSE(list.Remove(1, &removed_item));
216 EXPECT_TRUE(list.Remove(0, &removed_item));
217 ASSERT_TRUE(removed_item);
218 EXPECT_EQ(0U, list.GetSize());
220 EXPECT_FALSE(deletion_flag);
221 delete removed_item;
222 removed_item = NULL;
223 EXPECT_TRUE(deletion_flag);
226 ListValue list;
227 list.Append(new DeletionTestValue(&deletion_flag));
228 EXPECT_FALSE(deletion_flag);
229 EXPECT_TRUE(list.Remove(0, NULL));
230 EXPECT_TRUE(deletion_flag);
231 EXPECT_EQ(0U, list.GetSize());
235 ListValue list;
236 DeletionTestValue* value = new DeletionTestValue(&deletion_flag);
237 list.Append(value);
238 EXPECT_FALSE(deletion_flag);
239 EXPECT_EQ(0, list.Remove(*value));
240 EXPECT_TRUE(deletion_flag);
241 EXPECT_EQ(0U, list.GetSize());
245 TEST(ValuesTest, DictionaryDeletion) {
246 std::string key = "test";
247 bool deletion_flag = true;
250 DictionaryValue dict;
251 dict.Set(key, new DeletionTestValue(&deletion_flag));
252 EXPECT_FALSE(deletion_flag);
254 EXPECT_TRUE(deletion_flag);
257 DictionaryValue dict;
258 dict.Set(key, new DeletionTestValue(&deletion_flag));
259 EXPECT_FALSE(deletion_flag);
260 dict.Clear();
261 EXPECT_TRUE(deletion_flag);
265 DictionaryValue dict;
266 dict.Set(key, new DeletionTestValue(&deletion_flag));
267 EXPECT_FALSE(deletion_flag);
268 dict.Set(key, Value::CreateNullValue());
269 EXPECT_TRUE(deletion_flag);
273 TEST(ValuesTest, DictionaryRemoval) {
274 std::string key = "test";
275 bool deletion_flag = true;
276 Value* removed_item = NULL;
279 DictionaryValue dict;
280 dict.Set(key, new DeletionTestValue(&deletion_flag));
281 EXPECT_FALSE(deletion_flag);
282 EXPECT_TRUE(dict.HasKey(key));
283 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
284 EXPECT_TRUE(dict.Remove(key, &removed_item));
285 EXPECT_FALSE(dict.HasKey(key));
286 ASSERT_TRUE(removed_item);
288 EXPECT_FALSE(deletion_flag);
289 delete removed_item;
290 removed_item = NULL;
291 EXPECT_TRUE(deletion_flag);
294 DictionaryValue dict;
295 dict.Set(key, new DeletionTestValue(&deletion_flag));
296 EXPECT_FALSE(deletion_flag);
297 EXPECT_TRUE(dict.HasKey(key));
298 EXPECT_TRUE(dict.Remove(key, NULL));
299 EXPECT_TRUE(deletion_flag);
300 EXPECT_FALSE(dict.HasKey(key));
304 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
305 DictionaryValue dict;
306 dict.Set("this.is.expanded", Value::CreateNullValue());
307 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
309 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
310 EXPECT_TRUE(dict.HasKey("this"));
311 Value* value1;
312 EXPECT_TRUE(dict.Get("this", &value1));
313 DictionaryValue* value2;
314 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
315 EXPECT_EQ(value1, value2);
316 EXPECT_EQ(1U, value2->size());
318 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
319 Value* value3;
320 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
321 Value* value4;
322 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
323 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
326 TEST(ValuesTest, DeepCopy) {
327 DictionaryValue original_dict;
328 Value* original_null = Value::CreateNullValue();
329 original_dict.Set("null", original_null);
330 FundamentalValue* original_bool = Value::CreateBooleanValue(true);
331 original_dict.Set("bool", original_bool);
332 FundamentalValue* original_int = Value::CreateIntegerValue(42);
333 original_dict.Set("int", original_int);
334 FundamentalValue* original_double = Value::CreateDoubleValue(3.14);
335 original_dict.Set("double", original_double);
336 StringValue* original_string = Value::CreateStringValue("hello");
337 original_dict.Set("string", original_string);
338 StringValue* original_string16 =
339 Value::CreateStringValue(ASCIIToUTF16("hello16"));
340 original_dict.Set("string16", original_string16);
342 char* original_buffer = new char[42];
343 memset(original_buffer, '!', 42);
344 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42);
345 original_dict.Set("binary", original_binary);
347 ListValue* original_list = new ListValue();
348 FundamentalValue* original_list_element_0 = Value::CreateIntegerValue(0);
349 original_list->Append(original_list_element_0);
350 FundamentalValue* original_list_element_1 = Value::CreateIntegerValue(1);
351 original_list->Append(original_list_element_1);
352 original_dict.Set("list", original_list);
354 scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy());
355 ASSERT_TRUE(copy_dict.get());
356 ASSERT_NE(copy_dict.get(), &original_dict);
358 Value* copy_null = NULL;
359 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
360 ASSERT_TRUE(copy_null);
361 ASSERT_NE(copy_null, original_null);
362 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
364 Value* copy_bool = NULL;
365 ASSERT_TRUE(copy_dict->Get("bool", &copy_bool));
366 ASSERT_TRUE(copy_bool);
367 ASSERT_NE(copy_bool, original_bool);
368 ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
369 bool copy_bool_value = false;
370 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
371 ASSERT_TRUE(copy_bool_value);
373 Value* copy_int = NULL;
374 ASSERT_TRUE(copy_dict->Get("int", &copy_int));
375 ASSERT_TRUE(copy_int);
376 ASSERT_NE(copy_int, original_int);
377 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
378 int copy_int_value = 0;
379 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
380 ASSERT_EQ(42, copy_int_value);
382 Value* copy_double = NULL;
383 ASSERT_TRUE(copy_dict->Get("double", &copy_double));
384 ASSERT_TRUE(copy_double);
385 ASSERT_NE(copy_double, original_double);
386 ASSERT_TRUE(copy_double->IsType(Value::TYPE_DOUBLE));
387 double copy_double_value = 0;
388 ASSERT_TRUE(copy_double->GetAsDouble(&copy_double_value));
389 ASSERT_EQ(3.14, copy_double_value);
391 Value* copy_string = NULL;
392 ASSERT_TRUE(copy_dict->Get("string", &copy_string));
393 ASSERT_TRUE(copy_string);
394 ASSERT_NE(copy_string, original_string);
395 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
396 std::string copy_string_value;
397 string16 copy_string16_value;
398 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
399 ASSERT_TRUE(copy_string->GetAsString(&copy_string16_value));
400 ASSERT_EQ(std::string("hello"), copy_string_value);
401 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
403 Value* copy_string16 = NULL;
404 ASSERT_TRUE(copy_dict->Get("string16", &copy_string16));
405 ASSERT_TRUE(copy_string16);
406 ASSERT_NE(copy_string16, original_string16);
407 ASSERT_TRUE(copy_string16->IsType(Value::TYPE_STRING));
408 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value));
409 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value));
410 ASSERT_EQ(std::string("hello16"), copy_string_value);
411 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
413 Value* copy_binary = NULL;
414 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
415 ASSERT_TRUE(copy_binary);
416 ASSERT_NE(copy_binary, original_binary);
417 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
418 ASSERT_NE(original_binary->GetBuffer(),
419 static_cast<BinaryValue*>(copy_binary)->GetBuffer());
420 ASSERT_EQ(original_binary->GetSize(),
421 static_cast<BinaryValue*>(copy_binary)->GetSize());
422 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
423 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
424 original_binary->GetSize()));
426 Value* copy_value = NULL;
427 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
428 ASSERT_TRUE(copy_value);
429 ASSERT_NE(copy_value, original_list);
430 ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
431 ListValue* copy_list = static_cast<ListValue*>(copy_value);
432 ASSERT_EQ(2U, copy_list->GetSize());
434 Value* copy_list_element_0;
435 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
436 ASSERT_TRUE(copy_list_element_0);
437 ASSERT_NE(copy_list_element_0, original_list_element_0);
438 int copy_list_element_0_value;
439 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
440 ASSERT_EQ(0, copy_list_element_0_value);
442 Value* copy_list_element_1;
443 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
444 ASSERT_TRUE(copy_list_element_1);
445 ASSERT_NE(copy_list_element_1, original_list_element_1);
446 int copy_list_element_1_value;
447 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
448 ASSERT_EQ(1, copy_list_element_1_value);
451 TEST(ValuesTest, Equals) {
452 Value* null1 = Value::CreateNullValue();
453 Value* null2 = Value::CreateNullValue();
454 EXPECT_NE(null1, null2);
455 EXPECT_TRUE(null1->Equals(null2));
457 Value* boolean = Value::CreateBooleanValue(false);
458 EXPECT_FALSE(null1->Equals(boolean));
459 delete null1;
460 delete null2;
461 delete boolean;
463 DictionaryValue dv;
464 dv.SetBoolean("a", false);
465 dv.SetInteger("b", 2);
466 dv.SetDouble("c", 2.5);
467 dv.SetString("d1", "string");
468 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
469 dv.Set("e", Value::CreateNullValue());
471 scoped_ptr<DictionaryValue> copy;
472 copy.reset(dv.DeepCopy());
473 EXPECT_TRUE(dv.Equals(copy.get()));
475 ListValue* list = new ListValue;
476 list->Append(Value::CreateNullValue());
477 list->Append(new DictionaryValue);
478 dv.Set("f", list);
480 EXPECT_FALSE(dv.Equals(copy.get()));
481 copy->Set("f", list->DeepCopy());
482 EXPECT_TRUE(dv.Equals(copy.get()));
484 list->Append(Value::CreateBooleanValue(true));
485 EXPECT_FALSE(dv.Equals(copy.get()));
487 // Check if Equals detects differences in only the keys.
488 copy.reset(dv.DeepCopy());
489 EXPECT_TRUE(dv.Equals(copy.get()));
490 copy->Remove("a", NULL);
491 copy->SetBoolean("aa", false);
492 EXPECT_FALSE(dv.Equals(copy.get()));
495 TEST(ValuesTest, StaticEquals) {
496 scoped_ptr<Value> null1(Value::CreateNullValue());
497 scoped_ptr<Value> null2(Value::CreateNullValue());
498 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
499 EXPECT_TRUE(Value::Equals(NULL, NULL));
501 scoped_ptr<Value> i42(Value::CreateIntegerValue(42));
502 scoped_ptr<Value> j42(Value::CreateIntegerValue(42));
503 scoped_ptr<Value> i17(Value::CreateIntegerValue(17));
504 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
505 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
506 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
507 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
508 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
509 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
511 // NULL and Value::CreateNullValue() are intentionally different: We need
512 // support for NULL as a return value for "undefined" without caring for
513 // ownership of the pointer.
514 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
515 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
518 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
519 DictionaryValue original_dict;
520 Value* original_null = Value::CreateNullValue();
521 original_dict.Set("null", original_null);
522 FundamentalValue* original_bool = Value::CreateBooleanValue(true);
523 original_dict.Set("bool", original_bool);
524 FundamentalValue* original_int = Value::CreateIntegerValue(42);
525 original_dict.Set("int", original_int);
526 FundamentalValue* original_double = Value::CreateDoubleValue(3.14);
527 original_dict.Set("double", original_double);
528 StringValue* original_string = Value::CreateStringValue("hello");
529 original_dict.Set("string", original_string);
530 StringValue* original_string16 =
531 Value::CreateStringValue(ASCIIToUTF16("hello16"));
532 original_dict.Set("string16", original_string16);
534 char* original_buffer = new char[42];
535 memset(original_buffer, '!', 42);
536 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42);
537 original_dict.Set("binary", original_binary);
539 ListValue* original_list = new ListValue();
540 FundamentalValue* original_list_element_0 = Value::CreateIntegerValue(0);
541 original_list->Append(original_list_element_0);
542 FundamentalValue* original_list_element_1 = Value::CreateIntegerValue(1);
543 original_list->Append(original_list_element_1);
544 original_dict.Set("list", original_list);
546 Value* original_dict_value = &original_dict;
547 Value* original_bool_value = original_bool;
548 Value* original_int_value = original_int;
549 Value* original_double_value = original_double;
550 Value* original_string_value = original_string;
551 Value* original_string16_value = original_string16;
552 Value* original_binary_value = original_binary;
553 Value* original_list_value = original_list;
555 scoped_ptr<Value> copy_dict_value(original_dict_value->DeepCopy());
556 scoped_ptr<Value> copy_bool_value(original_bool_value->DeepCopy());
557 scoped_ptr<Value> copy_int_value(original_int_value->DeepCopy());
558 scoped_ptr<Value> copy_double_value(original_double_value->DeepCopy());
559 scoped_ptr<Value> copy_string_value(original_string_value->DeepCopy());
560 scoped_ptr<Value> copy_string16_value(original_string16_value->DeepCopy());
561 scoped_ptr<Value> copy_binary_value(original_binary_value->DeepCopy());
562 scoped_ptr<Value> copy_list_value(original_list_value->DeepCopy());
564 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get()));
565 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get()));
566 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get()));
567 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get()));
568 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get()));
569 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get()));
570 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get()));
571 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get()));
574 TEST(ValuesTest, RemoveEmptyChildren) {
575 scoped_ptr<DictionaryValue> root(new DictionaryValue);
576 // Remove empty lists and dictionaries.
577 root->Set("empty_dict", new DictionaryValue);
578 root->Set("empty_list", new ListValue);
579 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
580 root.reset(root->DeepCopyWithoutEmptyChildren());
581 EXPECT_TRUE(root->empty());
583 // Make sure we don't prune too much.
584 root->SetBoolean("bool", true);
585 root->Set("empty_dict", new DictionaryValue);
586 root->SetString("empty_string", "");
587 root.reset(root->DeepCopyWithoutEmptyChildren());
588 EXPECT_EQ(2U, root->size());
590 // Should do nothing.
591 root.reset(root->DeepCopyWithoutEmptyChildren());
592 EXPECT_EQ(2U, root->size());
594 // Nested test cases. These should all reduce back to the bool and string
595 // set above.
597 root->Set("a.b.c.d.e", new DictionaryValue);
598 root.reset(root->DeepCopyWithoutEmptyChildren());
599 EXPECT_EQ(2U, root->size());
602 DictionaryValue* inner = new DictionaryValue;
603 root->Set("dict_with_emtpy_children", inner);
604 inner->Set("empty_dict", new DictionaryValue);
605 inner->Set("empty_list", new ListValue);
606 root.reset(root->DeepCopyWithoutEmptyChildren());
607 EXPECT_EQ(2U, root->size());
610 ListValue* inner = new ListValue;
611 root->Set("list_with_empty_children", inner);
612 inner->Append(new DictionaryValue);
613 inner->Append(new ListValue);
614 root.reset(root->DeepCopyWithoutEmptyChildren());
615 EXPECT_EQ(2U, root->size());
618 // Nested with siblings.
620 ListValue* inner = new ListValue;
621 root->Set("list_with_empty_children", inner);
622 inner->Append(new DictionaryValue);
623 inner->Append(new ListValue);
624 DictionaryValue* inner2 = new DictionaryValue;
625 root->Set("dict_with_empty_children", inner2);
626 inner2->Set("empty_dict", new DictionaryValue);
627 inner2->Set("empty_list", new ListValue);
628 root.reset(root->DeepCopyWithoutEmptyChildren());
629 EXPECT_EQ(2U, root->size());
632 // Make sure nested values don't get pruned.
634 ListValue* inner = new ListValue;
635 root->Set("list_with_empty_children", inner);
636 ListValue* inner2 = new ListValue;
637 inner->Append(new DictionaryValue);
638 inner->Append(inner2);
639 inner2->Append(Value::CreateStringValue("hello"));
640 root.reset(root->DeepCopyWithoutEmptyChildren());
641 EXPECT_EQ(3U, root->size());
642 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner));
643 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
644 EXPECT_TRUE(inner->GetList(0, &inner2));
645 EXPECT_EQ(1U, inner2->GetSize());
649 TEST(ValuesTest, MergeDictionary) {
650 scoped_ptr<DictionaryValue> base(new DictionaryValue);
651 base->SetString("base_key", "base_key_value_base");
652 base->SetString("collide_key", "collide_key_value_base");
653 DictionaryValue* base_sub_dict = new DictionaryValue;
654 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
655 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
656 base->Set("sub_dict_key", base_sub_dict);
658 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
659 merge->SetString("merge_key", "merge_key_value_merge");
660 merge->SetString("collide_key", "collide_key_value_merge");
661 DictionaryValue* merge_sub_dict = new DictionaryValue;
662 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
663 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
664 merge->Set("sub_dict_key", merge_sub_dict);
666 base->MergeDictionary(merge.get());
668 EXPECT_EQ(4U, base->size());
669 std::string base_key_value;
670 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
671 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
672 std::string collide_key_value;
673 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
674 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
675 std::string merge_key_value;
676 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
677 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
679 DictionaryValue* res_sub_dict;
680 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
681 EXPECT_EQ(3U, res_sub_dict->size());
682 std::string sub_base_key_value;
683 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
684 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
685 std::string sub_collide_key_value;
686 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
687 &sub_collide_key_value));
688 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
689 std::string sub_merge_key_value;
690 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
691 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
694 } // namespace base