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.
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"
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
;
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());
67 bool bool_value
= false;
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
) {
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];
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
{
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;
174 bool* deletion_flag_
;
177 TEST(ValuesTest
, ListDeletion
) {
178 bool deletion_flag
= true;
182 list
.Append(new DeletionTestValue(&deletion_flag
));
183 EXPECT_FALSE(deletion_flag
);
185 EXPECT_TRUE(deletion_flag
);
189 list
.Append(new DeletionTestValue(&deletion_flag
));
190 EXPECT_FALSE(deletion_flag
);
192 EXPECT_TRUE(deletion_flag
);
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
;
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(),
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
);
223 EXPECT_TRUE(deletion_flag
);
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());
236 DeletionTestValue
* value
= new DeletionTestValue(&deletion_flag
);
238 EXPECT_FALSE(deletion_flag
);
240 list
.Remove(*value
, &index
);
241 EXPECT_EQ(0U, index
);
242 EXPECT_TRUE(deletion_flag
);
243 EXPECT_EQ(0U, list
.GetSize());
247 TEST(ValuesTest
, DictionaryDeletion
) {
248 std::string key
= "test";
249 bool deletion_flag
= true;
252 DictionaryValue dict
;
253 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
254 EXPECT_FALSE(deletion_flag
);
256 EXPECT_TRUE(deletion_flag
);
259 DictionaryValue dict
;
260 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
261 EXPECT_FALSE(deletion_flag
);
263 EXPECT_TRUE(deletion_flag
);
267 DictionaryValue dict
;
268 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
269 EXPECT_FALSE(deletion_flag
);
270 dict
.Set(key
, Value::CreateNullValue());
271 EXPECT_TRUE(deletion_flag
);
275 TEST(ValuesTest
, DictionaryRemoval
) {
276 std::string key
= "test";
277 bool deletion_flag
= true;
278 Value
* removed_item
= NULL
;
281 DictionaryValue dict
;
282 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
283 EXPECT_FALSE(deletion_flag
);
284 EXPECT_TRUE(dict
.HasKey(key
));
285 EXPECT_FALSE(dict
.Remove("absent key", &removed_item
));
286 EXPECT_TRUE(dict
.Remove(key
, &removed_item
));
287 EXPECT_FALSE(dict
.HasKey(key
));
288 ASSERT_TRUE(removed_item
);
290 EXPECT_FALSE(deletion_flag
);
293 EXPECT_TRUE(deletion_flag
);
296 DictionaryValue dict
;
297 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
298 EXPECT_FALSE(deletion_flag
);
299 EXPECT_TRUE(dict
.HasKey(key
));
300 EXPECT_TRUE(dict
.Remove(key
, NULL
));
301 EXPECT_TRUE(deletion_flag
);
302 EXPECT_FALSE(dict
.HasKey(key
));
306 TEST(ValuesTest
, DictionaryWithoutPathExpansion
) {
307 DictionaryValue dict
;
308 dict
.Set("this.is.expanded", Value::CreateNullValue());
309 dict
.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
311 EXPECT_FALSE(dict
.HasKey("this.is.expanded"));
312 EXPECT_TRUE(dict
.HasKey("this"));
314 EXPECT_TRUE(dict
.Get("this", &value1
));
315 DictionaryValue
* value2
;
316 ASSERT_TRUE(dict
.GetDictionaryWithoutPathExpansion("this", &value2
));
317 EXPECT_EQ(value1
, value2
);
318 EXPECT_EQ(1U, value2
->size());
320 EXPECT_TRUE(dict
.HasKey("this.isnt.expanded"));
322 EXPECT_FALSE(dict
.Get("this.isnt.expanded", &value3
));
324 ASSERT_TRUE(dict
.GetWithoutPathExpansion("this.isnt.expanded", &value4
));
325 EXPECT_EQ(Value::TYPE_NULL
, value4
->GetType());
328 TEST(ValuesTest
, DeepCopy
) {
329 DictionaryValue original_dict
;
330 Value
* original_null
= Value::CreateNullValue();
331 original_dict
.Set("null", original_null
);
332 FundamentalValue
* original_bool
= Value::CreateBooleanValue(true);
333 original_dict
.Set("bool", original_bool
);
334 FundamentalValue
* original_int
= Value::CreateIntegerValue(42);
335 original_dict
.Set("int", original_int
);
336 FundamentalValue
* original_double
= Value::CreateDoubleValue(3.14);
337 original_dict
.Set("double", original_double
);
338 StringValue
* original_string
= Value::CreateStringValue("hello");
339 original_dict
.Set("string", original_string
);
340 StringValue
* original_string16
=
341 Value::CreateStringValue(ASCIIToUTF16("hello16"));
342 original_dict
.Set("string16", original_string16
);
344 char* original_buffer
= new char[42];
345 memset(original_buffer
, '!', 42);
346 BinaryValue
* original_binary
= BinaryValue::Create(original_buffer
, 42);
347 original_dict
.Set("binary", original_binary
);
349 ListValue
* original_list
= new ListValue();
350 FundamentalValue
* original_list_element_0
= Value::CreateIntegerValue(0);
351 original_list
->Append(original_list_element_0
);
352 FundamentalValue
* original_list_element_1
= Value::CreateIntegerValue(1);
353 original_list
->Append(original_list_element_1
);
354 original_dict
.Set("list", original_list
);
356 scoped_ptr
<DictionaryValue
> copy_dict(original_dict
.DeepCopy());
357 ASSERT_TRUE(copy_dict
.get());
358 ASSERT_NE(copy_dict
.get(), &original_dict
);
360 Value
* copy_null
= NULL
;
361 ASSERT_TRUE(copy_dict
->Get("null", ©_null
));
362 ASSERT_TRUE(copy_null
);
363 ASSERT_NE(copy_null
, original_null
);
364 ASSERT_TRUE(copy_null
->IsType(Value::TYPE_NULL
));
366 Value
* copy_bool
= NULL
;
367 ASSERT_TRUE(copy_dict
->Get("bool", ©_bool
));
368 ASSERT_TRUE(copy_bool
);
369 ASSERT_NE(copy_bool
, original_bool
);
370 ASSERT_TRUE(copy_bool
->IsType(Value::TYPE_BOOLEAN
));
371 bool copy_bool_value
= false;
372 ASSERT_TRUE(copy_bool
->GetAsBoolean(©_bool_value
));
373 ASSERT_TRUE(copy_bool_value
);
375 Value
* copy_int
= NULL
;
376 ASSERT_TRUE(copy_dict
->Get("int", ©_int
));
377 ASSERT_TRUE(copy_int
);
378 ASSERT_NE(copy_int
, original_int
);
379 ASSERT_TRUE(copy_int
->IsType(Value::TYPE_INTEGER
));
380 int copy_int_value
= 0;
381 ASSERT_TRUE(copy_int
->GetAsInteger(©_int_value
));
382 ASSERT_EQ(42, copy_int_value
);
384 Value
* copy_double
= NULL
;
385 ASSERT_TRUE(copy_dict
->Get("double", ©_double
));
386 ASSERT_TRUE(copy_double
);
387 ASSERT_NE(copy_double
, original_double
);
388 ASSERT_TRUE(copy_double
->IsType(Value::TYPE_DOUBLE
));
389 double copy_double_value
= 0;
390 ASSERT_TRUE(copy_double
->GetAsDouble(©_double_value
));
391 ASSERT_EQ(3.14, copy_double_value
);
393 Value
* copy_string
= NULL
;
394 ASSERT_TRUE(copy_dict
->Get("string", ©_string
));
395 ASSERT_TRUE(copy_string
);
396 ASSERT_NE(copy_string
, original_string
);
397 ASSERT_TRUE(copy_string
->IsType(Value::TYPE_STRING
));
398 std::string copy_string_value
;
399 string16 copy_string16_value
;
400 ASSERT_TRUE(copy_string
->GetAsString(©_string_value
));
401 ASSERT_TRUE(copy_string
->GetAsString(©_string16_value
));
402 ASSERT_EQ(std::string("hello"), copy_string_value
);
403 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value
);
405 Value
* copy_string16
= NULL
;
406 ASSERT_TRUE(copy_dict
->Get("string16", ©_string16
));
407 ASSERT_TRUE(copy_string16
);
408 ASSERT_NE(copy_string16
, original_string16
);
409 ASSERT_TRUE(copy_string16
->IsType(Value::TYPE_STRING
));
410 ASSERT_TRUE(copy_string16
->GetAsString(©_string_value
));
411 ASSERT_TRUE(copy_string16
->GetAsString(©_string16_value
));
412 ASSERT_EQ(std::string("hello16"), copy_string_value
);
413 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value
);
415 Value
* copy_binary
= NULL
;
416 ASSERT_TRUE(copy_dict
->Get("binary", ©_binary
));
417 ASSERT_TRUE(copy_binary
);
418 ASSERT_NE(copy_binary
, original_binary
);
419 ASSERT_TRUE(copy_binary
->IsType(Value::TYPE_BINARY
));
420 ASSERT_NE(original_binary
->GetBuffer(),
421 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer());
422 ASSERT_EQ(original_binary
->GetSize(),
423 static_cast<BinaryValue
*>(copy_binary
)->GetSize());
424 ASSERT_EQ(0, memcmp(original_binary
->GetBuffer(),
425 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer(),
426 original_binary
->GetSize()));
428 Value
* copy_value
= NULL
;
429 ASSERT_TRUE(copy_dict
->Get("list", ©_value
));
430 ASSERT_TRUE(copy_value
);
431 ASSERT_NE(copy_value
, original_list
);
432 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_LIST
));
433 ListValue
* copy_list
= static_cast<ListValue
*>(copy_value
);
434 ASSERT_EQ(2U, copy_list
->GetSize());
436 Value
* copy_list_element_0
;
437 ASSERT_TRUE(copy_list
->Get(0, ©_list_element_0
));
438 ASSERT_TRUE(copy_list_element_0
);
439 ASSERT_NE(copy_list_element_0
, original_list_element_0
);
440 int copy_list_element_0_value
;
441 ASSERT_TRUE(copy_list_element_0
->GetAsInteger(©_list_element_0_value
));
442 ASSERT_EQ(0, copy_list_element_0_value
);
444 Value
* copy_list_element_1
;
445 ASSERT_TRUE(copy_list
->Get(1, ©_list_element_1
));
446 ASSERT_TRUE(copy_list_element_1
);
447 ASSERT_NE(copy_list_element_1
, original_list_element_1
);
448 int copy_list_element_1_value
;
449 ASSERT_TRUE(copy_list_element_1
->GetAsInteger(©_list_element_1_value
));
450 ASSERT_EQ(1, copy_list_element_1_value
);
453 TEST(ValuesTest
, Equals
) {
454 Value
* null1
= Value::CreateNullValue();
455 Value
* null2
= Value::CreateNullValue();
456 EXPECT_NE(null1
, null2
);
457 EXPECT_TRUE(null1
->Equals(null2
));
459 Value
* boolean
= Value::CreateBooleanValue(false);
460 EXPECT_FALSE(null1
->Equals(boolean
));
466 dv
.SetBoolean("a", false);
467 dv
.SetInteger("b", 2);
468 dv
.SetDouble("c", 2.5);
469 dv
.SetString("d1", "string");
470 dv
.SetString("d2", ASCIIToUTF16("http://google.com"));
471 dv
.Set("e", Value::CreateNullValue());
473 scoped_ptr
<DictionaryValue
> copy
;
474 copy
.reset(dv
.DeepCopy());
475 EXPECT_TRUE(dv
.Equals(copy
.get()));
477 ListValue
* list
= new ListValue
;
478 list
->Append(Value::CreateNullValue());
479 list
->Append(new DictionaryValue
);
482 EXPECT_FALSE(dv
.Equals(copy
.get()));
483 copy
->Set("f", list
->DeepCopy());
484 EXPECT_TRUE(dv
.Equals(copy
.get()));
486 list
->Append(Value::CreateBooleanValue(true));
487 EXPECT_FALSE(dv
.Equals(copy
.get()));
489 // Check if Equals detects differences in only the keys.
490 copy
.reset(dv
.DeepCopy());
491 EXPECT_TRUE(dv
.Equals(copy
.get()));
492 copy
->Remove("a", NULL
);
493 copy
->SetBoolean("aa", false);
494 EXPECT_FALSE(dv
.Equals(copy
.get()));
497 TEST(ValuesTest
, StaticEquals
) {
498 scoped_ptr
<Value
> null1(Value::CreateNullValue());
499 scoped_ptr
<Value
> null2(Value::CreateNullValue());
500 EXPECT_TRUE(Value::Equals(null1
.get(), null2
.get()));
501 EXPECT_TRUE(Value::Equals(NULL
, NULL
));
503 scoped_ptr
<Value
> i42(Value::CreateIntegerValue(42));
504 scoped_ptr
<Value
> j42(Value::CreateIntegerValue(42));
505 scoped_ptr
<Value
> i17(Value::CreateIntegerValue(17));
506 EXPECT_TRUE(Value::Equals(i42
.get(), i42
.get()));
507 EXPECT_TRUE(Value::Equals(j42
.get(), i42
.get()));
508 EXPECT_TRUE(Value::Equals(i42
.get(), j42
.get()));
509 EXPECT_FALSE(Value::Equals(i42
.get(), i17
.get()));
510 EXPECT_FALSE(Value::Equals(i42
.get(), NULL
));
511 EXPECT_FALSE(Value::Equals(NULL
, i42
.get()));
513 // NULL and Value::CreateNullValue() are intentionally different: We need
514 // support for NULL as a return value for "undefined" without caring for
515 // ownership of the pointer.
516 EXPECT_FALSE(Value::Equals(null1
.get(), NULL
));
517 EXPECT_FALSE(Value::Equals(NULL
, null1
.get()));
520 TEST(ValuesTest
, DeepCopyCovariantReturnTypes
) {
521 DictionaryValue original_dict
;
522 Value
* original_null
= Value::CreateNullValue();
523 original_dict
.Set("null", original_null
);
524 FundamentalValue
* original_bool
= Value::CreateBooleanValue(true);
525 original_dict
.Set("bool", original_bool
);
526 FundamentalValue
* original_int
= Value::CreateIntegerValue(42);
527 original_dict
.Set("int", original_int
);
528 FundamentalValue
* original_double
= Value::CreateDoubleValue(3.14);
529 original_dict
.Set("double", original_double
);
530 StringValue
* original_string
= Value::CreateStringValue("hello");
531 original_dict
.Set("string", original_string
);
532 StringValue
* original_string16
=
533 Value::CreateStringValue(ASCIIToUTF16("hello16"));
534 original_dict
.Set("string16", original_string16
);
536 char* original_buffer
= new char[42];
537 memset(original_buffer
, '!', 42);
538 BinaryValue
* original_binary
= BinaryValue::Create(original_buffer
, 42);
539 original_dict
.Set("binary", original_binary
);
541 ListValue
* original_list
= new ListValue();
542 FundamentalValue
* original_list_element_0
= Value::CreateIntegerValue(0);
543 original_list
->Append(original_list_element_0
);
544 FundamentalValue
* original_list_element_1
= Value::CreateIntegerValue(1);
545 original_list
->Append(original_list_element_1
);
546 original_dict
.Set("list", original_list
);
548 Value
* original_dict_value
= &original_dict
;
549 Value
* original_bool_value
= original_bool
;
550 Value
* original_int_value
= original_int
;
551 Value
* original_double_value
= original_double
;
552 Value
* original_string_value
= original_string
;
553 Value
* original_string16_value
= original_string16
;
554 Value
* original_binary_value
= original_binary
;
555 Value
* original_list_value
= original_list
;
557 scoped_ptr
<Value
> copy_dict_value(original_dict_value
->DeepCopy());
558 scoped_ptr
<Value
> copy_bool_value(original_bool_value
->DeepCopy());
559 scoped_ptr
<Value
> copy_int_value(original_int_value
->DeepCopy());
560 scoped_ptr
<Value
> copy_double_value(original_double_value
->DeepCopy());
561 scoped_ptr
<Value
> copy_string_value(original_string_value
->DeepCopy());
562 scoped_ptr
<Value
> copy_string16_value(original_string16_value
->DeepCopy());
563 scoped_ptr
<Value
> copy_binary_value(original_binary_value
->DeepCopy());
564 scoped_ptr
<Value
> copy_list_value(original_list_value
->DeepCopy());
566 EXPECT_TRUE(original_dict_value
->Equals(copy_dict_value
.get()));
567 EXPECT_TRUE(original_bool_value
->Equals(copy_bool_value
.get()));
568 EXPECT_TRUE(original_int_value
->Equals(copy_int_value
.get()));
569 EXPECT_TRUE(original_double_value
->Equals(copy_double_value
.get()));
570 EXPECT_TRUE(original_string_value
->Equals(copy_string_value
.get()));
571 EXPECT_TRUE(original_string16_value
->Equals(copy_string16_value
.get()));
572 EXPECT_TRUE(original_binary_value
->Equals(copy_binary_value
.get()));
573 EXPECT_TRUE(original_list_value
->Equals(copy_list_value
.get()));
576 TEST(ValuesTest
, RemoveEmptyChildren
) {
577 scoped_ptr
<DictionaryValue
> root(new DictionaryValue
);
578 // Remove empty lists and dictionaries.
579 root
->Set("empty_dict", new DictionaryValue
);
580 root
->Set("empty_list", new ListValue
);
581 root
->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue
);
582 root
.reset(root
->DeepCopyWithoutEmptyChildren());
583 EXPECT_TRUE(root
->empty());
585 // Make sure we don't prune too much.
586 root
->SetBoolean("bool", true);
587 root
->Set("empty_dict", new DictionaryValue
);
588 root
->SetString("empty_string", "");
589 root
.reset(root
->DeepCopyWithoutEmptyChildren());
590 EXPECT_EQ(2U, root
->size());
592 // Should do nothing.
593 root
.reset(root
->DeepCopyWithoutEmptyChildren());
594 EXPECT_EQ(2U, root
->size());
596 // Nested test cases. These should all reduce back to the bool and string
599 root
->Set("a.b.c.d.e", new DictionaryValue
);
600 root
.reset(root
->DeepCopyWithoutEmptyChildren());
601 EXPECT_EQ(2U, root
->size());
604 DictionaryValue
* inner
= new DictionaryValue
;
605 root
->Set("dict_with_emtpy_children", inner
);
606 inner
->Set("empty_dict", new DictionaryValue
);
607 inner
->Set("empty_list", new ListValue
);
608 root
.reset(root
->DeepCopyWithoutEmptyChildren());
609 EXPECT_EQ(2U, root
->size());
612 ListValue
* inner
= new ListValue
;
613 root
->Set("list_with_empty_children", inner
);
614 inner
->Append(new DictionaryValue
);
615 inner
->Append(new ListValue
);
616 root
.reset(root
->DeepCopyWithoutEmptyChildren());
617 EXPECT_EQ(2U, root
->size());
620 // Nested with siblings.
622 ListValue
* inner
= new ListValue
;
623 root
->Set("list_with_empty_children", inner
);
624 inner
->Append(new DictionaryValue
);
625 inner
->Append(new ListValue
);
626 DictionaryValue
* inner2
= new DictionaryValue
;
627 root
->Set("dict_with_empty_children", inner2
);
628 inner2
->Set("empty_dict", new DictionaryValue
);
629 inner2
->Set("empty_list", new ListValue
);
630 root
.reset(root
->DeepCopyWithoutEmptyChildren());
631 EXPECT_EQ(2U, root
->size());
634 // Make sure nested values don't get pruned.
636 ListValue
* inner
= new ListValue
;
637 root
->Set("list_with_empty_children", inner
);
638 ListValue
* inner2
= new ListValue
;
639 inner
->Append(new DictionaryValue
);
640 inner
->Append(inner2
);
641 inner2
->Append(Value::CreateStringValue("hello"));
642 root
.reset(root
->DeepCopyWithoutEmptyChildren());
643 EXPECT_EQ(3U, root
->size());
644 EXPECT_TRUE(root
->GetList("list_with_empty_children", &inner
));
645 EXPECT_EQ(1U, inner
->GetSize()); // Dictionary was pruned.
646 EXPECT_TRUE(inner
->GetList(0, &inner2
));
647 EXPECT_EQ(1U, inner2
->GetSize());
651 TEST(ValuesTest
, MergeDictionary
) {
652 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
653 base
->SetString("base_key", "base_key_value_base");
654 base
->SetString("collide_key", "collide_key_value_base");
655 DictionaryValue
* base_sub_dict
= new DictionaryValue
;
656 base_sub_dict
->SetString("sub_base_key", "sub_base_key_value_base");
657 base_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_base");
658 base
->Set("sub_dict_key", base_sub_dict
);
660 scoped_ptr
<DictionaryValue
> merge(new DictionaryValue
);
661 merge
->SetString("merge_key", "merge_key_value_merge");
662 merge
->SetString("collide_key", "collide_key_value_merge");
663 DictionaryValue
* merge_sub_dict
= new DictionaryValue
;
664 merge_sub_dict
->SetString("sub_merge_key", "sub_merge_key_value_merge");
665 merge_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_merge");
666 merge
->Set("sub_dict_key", merge_sub_dict
);
668 base
->MergeDictionary(merge
.get());
670 EXPECT_EQ(4U, base
->size());
671 std::string base_key_value
;
672 EXPECT_TRUE(base
->GetString("base_key", &base_key_value
));
673 EXPECT_EQ("base_key_value_base", base_key_value
); // Base value preserved.
674 std::string collide_key_value
;
675 EXPECT_TRUE(base
->GetString("collide_key", &collide_key_value
));
676 EXPECT_EQ("collide_key_value_merge", collide_key_value
); // Replaced.
677 std::string merge_key_value
;
678 EXPECT_TRUE(base
->GetString("merge_key", &merge_key_value
));
679 EXPECT_EQ("merge_key_value_merge", merge_key_value
); // Merged in.
681 DictionaryValue
* res_sub_dict
;
682 EXPECT_TRUE(base
->GetDictionary("sub_dict_key", &res_sub_dict
));
683 EXPECT_EQ(3U, res_sub_dict
->size());
684 std::string sub_base_key_value
;
685 EXPECT_TRUE(res_sub_dict
->GetString("sub_base_key", &sub_base_key_value
));
686 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value
); // Preserved.
687 std::string sub_collide_key_value
;
688 EXPECT_TRUE(res_sub_dict
->GetString("sub_collide_key",
689 &sub_collide_key_value
));
690 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value
); // Replaced.
691 std::string sub_merge_key_value
;
692 EXPECT_TRUE(res_sub_dict
->GetString("sub_merge_key", &sub_merge_key_value
));
693 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value
); // Merged in.