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.
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/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", new FundamentalValue(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, new FundamentalValue(true));
61 mixed_list
->Set(1, new FundamentalValue(42));
62 mixed_list
->Set(2, new FundamentalValue(88.8));
63 mixed_list
->Set(3, new StringValue("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
);
97 // Try searching in the mixed list.
98 base::FundamentalValue
sought_value(42);
99 base::FundamentalValue
not_found_value(false);
101 ASSERT_NE(mixed_list
->end(), mixed_list
->Find(sought_value
));
102 ASSERT_TRUE((*mixed_list
->Find(sought_value
))->GetAsInteger(&int_value
));
103 ASSERT_EQ(42, int_value
);
104 ASSERT_EQ(mixed_list
->end(), mixed_list
->Find(not_found_value
));
107 TEST(ValuesTest
, BinaryValue
) {
108 // Default constructor creates a BinaryValue with a null buffer and size 0.
109 scoped_ptr
<BinaryValue
> binary(new BinaryValue());
110 ASSERT_TRUE(binary
.get());
111 ASSERT_EQ(NULL
, binary
->GetBuffer());
112 ASSERT_EQ(0U, binary
->GetSize());
114 // Test the common case of a non-empty buffer
115 char* buffer
= new char[15];
116 binary
.reset(new BinaryValue(scoped_ptr
<char[]>(buffer
), 15));
117 ASSERT_TRUE(binary
.get());
118 ASSERT_TRUE(binary
->GetBuffer());
119 ASSERT_EQ(buffer
, binary
->GetBuffer());
120 ASSERT_EQ(15U, binary
->GetSize());
122 char stack_buffer
[42];
123 memset(stack_buffer
, '!', 42);
124 binary
.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer
, 42));
125 ASSERT_TRUE(binary
.get());
126 ASSERT_TRUE(binary
->GetBuffer());
127 ASSERT_NE(stack_buffer
, binary
->GetBuffer());
128 ASSERT_EQ(42U, binary
->GetSize());
129 ASSERT_EQ(0, memcmp(stack_buffer
, binary
->GetBuffer(), binary
->GetSize()));
132 TEST(ValuesTest
, StringValue
) {
133 // Test overloaded StringValue constructor.
134 scoped_ptr
<Value
> narrow_value(new StringValue("narrow"));
135 ASSERT_TRUE(narrow_value
.get());
136 ASSERT_TRUE(narrow_value
->IsType(Value::TYPE_STRING
));
137 scoped_ptr
<Value
> utf16_value(new StringValue(ASCIIToUTF16("utf16")));
138 ASSERT_TRUE(utf16_value
.get());
139 ASSERT_TRUE(utf16_value
->IsType(Value::TYPE_STRING
));
141 // Test overloaded GetAsString.
142 std::string narrow
= "http://google.com";
143 string16 utf16
= ASCIIToUTF16("http://google.com");
144 const StringValue
* string_value
= NULL
;
145 ASSERT_TRUE(narrow_value
->GetAsString(&narrow
));
146 ASSERT_TRUE(narrow_value
->GetAsString(&utf16
));
147 ASSERT_TRUE(narrow_value
->GetAsString(&string_value
));
148 ASSERT_EQ(std::string("narrow"), narrow
);
149 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16
);
150 ASSERT_EQ(string_value
->GetString(), narrow
);
152 ASSERT_TRUE(utf16_value
->GetAsString(&narrow
));
153 ASSERT_TRUE(utf16_value
->GetAsString(&utf16
));
154 ASSERT_TRUE(utf16_value
->GetAsString(&string_value
));
155 ASSERT_EQ(std::string("utf16"), narrow
);
156 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16
);
157 ASSERT_EQ(string_value
->GetString(), narrow
);
159 // Don't choke on NULL values.
160 ASSERT_TRUE(narrow_value
->GetAsString(static_cast<string16
*>(NULL
)));
161 ASSERT_TRUE(narrow_value
->GetAsString(static_cast<std::string
*>(NULL
)));
162 ASSERT_TRUE(narrow_value
->GetAsString(
163 static_cast<const StringValue
**>(NULL
)));
166 // This is a Value object that allows us to tell if it's been
167 // properly deleted by modifying the value of external flag on destruction.
168 class DeletionTestValue
: public Value
{
170 explicit DeletionTestValue(bool* deletion_flag
) : Value(TYPE_NULL
) {
171 Init(deletion_flag
); // Separate function so that we can use ASSERT_*
174 void Init(bool* deletion_flag
) {
175 ASSERT_TRUE(deletion_flag
);
176 deletion_flag_
= deletion_flag
;
177 *deletion_flag_
= false;
180 virtual ~DeletionTestValue() {
181 *deletion_flag_
= true;
185 bool* deletion_flag_
;
188 TEST(ValuesTest
, ListDeletion
) {
189 bool deletion_flag
= true;
193 list
.Append(new DeletionTestValue(&deletion_flag
));
194 EXPECT_FALSE(deletion_flag
);
196 EXPECT_TRUE(deletion_flag
);
200 list
.Append(new DeletionTestValue(&deletion_flag
));
201 EXPECT_FALSE(deletion_flag
);
203 EXPECT_TRUE(deletion_flag
);
208 list
.Append(new DeletionTestValue(&deletion_flag
));
209 EXPECT_FALSE(deletion_flag
);
210 EXPECT_TRUE(list
.Set(0, Value::CreateNullValue()));
211 EXPECT_TRUE(deletion_flag
);
215 TEST(ValuesTest
, ListRemoval
) {
216 bool deletion_flag
= true;
217 scoped_ptr
<Value
> removed_item
;
221 list
.Append(new DeletionTestValue(&deletion_flag
));
222 EXPECT_FALSE(deletion_flag
);
223 EXPECT_EQ(1U, list
.GetSize());
224 EXPECT_FALSE(list
.Remove(std::numeric_limits
<size_t>::max(),
226 EXPECT_FALSE(list
.Remove(1, &removed_item
));
227 EXPECT_TRUE(list
.Remove(0, &removed_item
));
228 ASSERT_TRUE(removed_item
);
229 EXPECT_EQ(0U, list
.GetSize());
231 EXPECT_FALSE(deletion_flag
);
232 removed_item
.reset();
233 EXPECT_TRUE(deletion_flag
);
237 list
.Append(new DeletionTestValue(&deletion_flag
));
238 EXPECT_FALSE(deletion_flag
);
239 EXPECT_TRUE(list
.Remove(0, NULL
));
240 EXPECT_TRUE(deletion_flag
);
241 EXPECT_EQ(0U, list
.GetSize());
246 DeletionTestValue
* value
= new DeletionTestValue(&deletion_flag
);
248 EXPECT_FALSE(deletion_flag
);
250 list
.Remove(*value
, &index
);
251 EXPECT_EQ(0U, index
);
252 EXPECT_TRUE(deletion_flag
);
253 EXPECT_EQ(0U, list
.GetSize());
257 TEST(ValuesTest
, DictionaryDeletion
) {
258 std::string key
= "test";
259 bool deletion_flag
= true;
262 DictionaryValue dict
;
263 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
264 EXPECT_FALSE(deletion_flag
);
266 EXPECT_TRUE(deletion_flag
);
269 DictionaryValue dict
;
270 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
271 EXPECT_FALSE(deletion_flag
);
273 EXPECT_TRUE(deletion_flag
);
277 DictionaryValue dict
;
278 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
279 EXPECT_FALSE(deletion_flag
);
280 dict
.Set(key
, Value::CreateNullValue());
281 EXPECT_TRUE(deletion_flag
);
285 TEST(ValuesTest
, DictionaryRemoval
) {
286 std::string key
= "test";
287 bool deletion_flag
= true;
288 scoped_ptr
<Value
> removed_item
;
291 DictionaryValue dict
;
292 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
293 EXPECT_FALSE(deletion_flag
);
294 EXPECT_TRUE(dict
.HasKey(key
));
295 EXPECT_FALSE(dict
.Remove("absent key", &removed_item
));
296 EXPECT_TRUE(dict
.Remove(key
, &removed_item
));
297 EXPECT_FALSE(dict
.HasKey(key
));
298 ASSERT_TRUE(removed_item
);
300 EXPECT_FALSE(deletion_flag
);
301 removed_item
.reset();
302 EXPECT_TRUE(deletion_flag
);
305 DictionaryValue dict
;
306 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
307 EXPECT_FALSE(deletion_flag
);
308 EXPECT_TRUE(dict
.HasKey(key
));
309 EXPECT_TRUE(dict
.Remove(key
, NULL
));
310 EXPECT_TRUE(deletion_flag
);
311 EXPECT_FALSE(dict
.HasKey(key
));
315 TEST(ValuesTest
, DictionaryWithoutPathExpansion
) {
316 DictionaryValue dict
;
317 dict
.Set("this.is.expanded", Value::CreateNullValue());
318 dict
.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
320 EXPECT_FALSE(dict
.HasKey("this.is.expanded"));
321 EXPECT_TRUE(dict
.HasKey("this"));
323 EXPECT_TRUE(dict
.Get("this", &value1
));
324 DictionaryValue
* value2
;
325 ASSERT_TRUE(dict
.GetDictionaryWithoutPathExpansion("this", &value2
));
326 EXPECT_EQ(value1
, value2
);
327 EXPECT_EQ(1U, value2
->size());
329 EXPECT_TRUE(dict
.HasKey("this.isnt.expanded"));
331 EXPECT_FALSE(dict
.Get("this.isnt.expanded", &value3
));
333 ASSERT_TRUE(dict
.GetWithoutPathExpansion("this.isnt.expanded", &value4
));
334 EXPECT_EQ(Value::TYPE_NULL
, value4
->GetType());
337 TEST(ValuesTest
, DictionaryRemovePath
) {
338 DictionaryValue dict
;
339 dict
.Set("a.long.way.down", new FundamentalValue(1));
340 dict
.Set("a.long.key.path", new FundamentalValue(true));
342 scoped_ptr
<Value
> removed_item
;
343 EXPECT_TRUE(dict
.RemovePath("a.long.way.down", &removed_item
));
344 ASSERT_TRUE(removed_item
);
345 EXPECT_TRUE(removed_item
->IsType(base::Value::TYPE_INTEGER
));
346 EXPECT_FALSE(dict
.HasKey("a.long.way.down"));
347 EXPECT_FALSE(dict
.HasKey("a.long.way"));
348 EXPECT_TRUE(dict
.Get("a.long.key.path", NULL
));
350 removed_item
.reset();
351 EXPECT_FALSE(dict
.RemovePath("a.long.way.down", &removed_item
));
352 EXPECT_FALSE(removed_item
);
353 EXPECT_TRUE(dict
.Get("a.long.key.path", NULL
));
355 removed_item
.reset();
356 EXPECT_TRUE(dict
.RemovePath("a.long.key.path", &removed_item
));
357 ASSERT_TRUE(removed_item
);
358 EXPECT_TRUE(removed_item
->IsType(base::Value::TYPE_BOOLEAN
));
359 EXPECT_TRUE(dict
.empty());
362 TEST(ValuesTest
, DeepCopy
) {
363 DictionaryValue original_dict
;
364 Value
* original_null
= Value::CreateNullValue();
365 original_dict
.Set("null", original_null
);
366 FundamentalValue
* original_bool
= new FundamentalValue(true);
367 original_dict
.Set("bool", original_bool
);
368 FundamentalValue
* original_int
= new FundamentalValue(42);
369 original_dict
.Set("int", original_int
);
370 FundamentalValue
* original_double
= new FundamentalValue(3.14);
371 original_dict
.Set("double", original_double
);
372 StringValue
* original_string
= new StringValue("hello");
373 original_dict
.Set("string", original_string
);
374 StringValue
* original_string16
= new StringValue(ASCIIToUTF16("hello16"));
375 original_dict
.Set("string16", original_string16
);
377 scoped_ptr
<char[]> original_buffer(new char[42]);
378 memset(original_buffer
.get(), '!', 42);
379 BinaryValue
* original_binary
= new BinaryValue(original_buffer
.Pass(), 42);
380 original_dict
.Set("binary", original_binary
);
382 ListValue
* original_list
= new ListValue();
383 FundamentalValue
* original_list_element_0
= new FundamentalValue(0);
384 original_list
->Append(original_list_element_0
);
385 FundamentalValue
* original_list_element_1
= new FundamentalValue(1);
386 original_list
->Append(original_list_element_1
);
387 original_dict
.Set("list", original_list
);
389 DictionaryValue
* original_nested_dictionary
= new DictionaryValue();
390 original_nested_dictionary
->Set("key", new StringValue("value"));
391 original_dict
.Set("dictionary", original_nested_dictionary
);
393 scoped_ptr
<DictionaryValue
> copy_dict(original_dict
.DeepCopy());
394 ASSERT_TRUE(copy_dict
.get());
395 ASSERT_NE(copy_dict
.get(), &original_dict
);
397 Value
* copy_null
= NULL
;
398 ASSERT_TRUE(copy_dict
->Get("null", ©_null
));
399 ASSERT_TRUE(copy_null
);
400 ASSERT_NE(copy_null
, original_null
);
401 ASSERT_TRUE(copy_null
->IsType(Value::TYPE_NULL
));
403 Value
* copy_bool
= NULL
;
404 ASSERT_TRUE(copy_dict
->Get("bool", ©_bool
));
405 ASSERT_TRUE(copy_bool
);
406 ASSERT_NE(copy_bool
, original_bool
);
407 ASSERT_TRUE(copy_bool
->IsType(Value::TYPE_BOOLEAN
));
408 bool copy_bool_value
= false;
409 ASSERT_TRUE(copy_bool
->GetAsBoolean(©_bool_value
));
410 ASSERT_TRUE(copy_bool_value
);
412 Value
* copy_int
= NULL
;
413 ASSERT_TRUE(copy_dict
->Get("int", ©_int
));
414 ASSERT_TRUE(copy_int
);
415 ASSERT_NE(copy_int
, original_int
);
416 ASSERT_TRUE(copy_int
->IsType(Value::TYPE_INTEGER
));
417 int copy_int_value
= 0;
418 ASSERT_TRUE(copy_int
->GetAsInteger(©_int_value
));
419 ASSERT_EQ(42, copy_int_value
);
421 Value
* copy_double
= NULL
;
422 ASSERT_TRUE(copy_dict
->Get("double", ©_double
));
423 ASSERT_TRUE(copy_double
);
424 ASSERT_NE(copy_double
, original_double
);
425 ASSERT_TRUE(copy_double
->IsType(Value::TYPE_DOUBLE
));
426 double copy_double_value
= 0;
427 ASSERT_TRUE(copy_double
->GetAsDouble(©_double_value
));
428 ASSERT_EQ(3.14, copy_double_value
);
430 Value
* copy_string
= NULL
;
431 ASSERT_TRUE(copy_dict
->Get("string", ©_string
));
432 ASSERT_TRUE(copy_string
);
433 ASSERT_NE(copy_string
, original_string
);
434 ASSERT_TRUE(copy_string
->IsType(Value::TYPE_STRING
));
435 std::string copy_string_value
;
436 string16 copy_string16_value
;
437 ASSERT_TRUE(copy_string
->GetAsString(©_string_value
));
438 ASSERT_TRUE(copy_string
->GetAsString(©_string16_value
));
439 ASSERT_EQ(std::string("hello"), copy_string_value
);
440 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value
);
442 Value
* copy_string16
= NULL
;
443 ASSERT_TRUE(copy_dict
->Get("string16", ©_string16
));
444 ASSERT_TRUE(copy_string16
);
445 ASSERT_NE(copy_string16
, original_string16
);
446 ASSERT_TRUE(copy_string16
->IsType(Value::TYPE_STRING
));
447 ASSERT_TRUE(copy_string16
->GetAsString(©_string_value
));
448 ASSERT_TRUE(copy_string16
->GetAsString(©_string16_value
));
449 ASSERT_EQ(std::string("hello16"), copy_string_value
);
450 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value
);
452 Value
* copy_binary
= NULL
;
453 ASSERT_TRUE(copy_dict
->Get("binary", ©_binary
));
454 ASSERT_TRUE(copy_binary
);
455 ASSERT_NE(copy_binary
, original_binary
);
456 ASSERT_TRUE(copy_binary
->IsType(Value::TYPE_BINARY
));
457 ASSERT_NE(original_binary
->GetBuffer(),
458 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer());
459 ASSERT_EQ(original_binary
->GetSize(),
460 static_cast<BinaryValue
*>(copy_binary
)->GetSize());
461 ASSERT_EQ(0, memcmp(original_binary
->GetBuffer(),
462 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer(),
463 original_binary
->GetSize()));
465 Value
* copy_value
= NULL
;
466 ASSERT_TRUE(copy_dict
->Get("list", ©_value
));
467 ASSERT_TRUE(copy_value
);
468 ASSERT_NE(copy_value
, original_list
);
469 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_LIST
));
470 ListValue
* copy_list
= NULL
;
471 ASSERT_TRUE(copy_value
->GetAsList(©_list
));
472 ASSERT_TRUE(copy_list
);
473 ASSERT_EQ(2U, copy_list
->GetSize());
475 Value
* copy_list_element_0
;
476 ASSERT_TRUE(copy_list
->Get(0, ©_list_element_0
));
477 ASSERT_TRUE(copy_list_element_0
);
478 ASSERT_NE(copy_list_element_0
, original_list_element_0
);
479 int copy_list_element_0_value
;
480 ASSERT_TRUE(copy_list_element_0
->GetAsInteger(©_list_element_0_value
));
481 ASSERT_EQ(0, copy_list_element_0_value
);
483 Value
* copy_list_element_1
;
484 ASSERT_TRUE(copy_list
->Get(1, ©_list_element_1
));
485 ASSERT_TRUE(copy_list_element_1
);
486 ASSERT_NE(copy_list_element_1
, original_list_element_1
);
487 int copy_list_element_1_value
;
488 ASSERT_TRUE(copy_list_element_1
->GetAsInteger(©_list_element_1_value
));
489 ASSERT_EQ(1, copy_list_element_1_value
);
492 ASSERT_TRUE(copy_dict
->Get("dictionary", ©_value
));
493 ASSERT_TRUE(copy_value
);
494 ASSERT_NE(copy_value
, original_nested_dictionary
);
495 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_DICTIONARY
));
496 DictionaryValue
* copy_nested_dictionary
= NULL
;
497 ASSERT_TRUE(copy_value
->GetAsDictionary(©_nested_dictionary
));
498 ASSERT_TRUE(copy_nested_dictionary
);
499 EXPECT_TRUE(copy_nested_dictionary
->HasKey("key"));
502 TEST(ValuesTest
, Equals
) {
503 Value
* null1
= Value::CreateNullValue();
504 Value
* null2
= Value::CreateNullValue();
505 EXPECT_NE(null1
, null2
);
506 EXPECT_TRUE(null1
->Equals(null2
));
508 Value
* boolean
= new FundamentalValue(false);
509 EXPECT_FALSE(null1
->Equals(boolean
));
515 dv
.SetBoolean("a", false);
516 dv
.SetInteger("b", 2);
517 dv
.SetDouble("c", 2.5);
518 dv
.SetString("d1", "string");
519 dv
.SetString("d2", ASCIIToUTF16("http://google.com"));
520 dv
.Set("e", Value::CreateNullValue());
522 scoped_ptr
<DictionaryValue
> copy
;
523 copy
.reset(dv
.DeepCopy());
524 EXPECT_TRUE(dv
.Equals(copy
.get()));
526 ListValue
* list
= new ListValue
;
527 list
->Append(Value::CreateNullValue());
528 list
->Append(new DictionaryValue
);
531 EXPECT_FALSE(dv
.Equals(copy
.get()));
532 copy
->Set("f", list
->DeepCopy());
533 EXPECT_TRUE(dv
.Equals(copy
.get()));
535 list
->Append(new FundamentalValue(true));
536 EXPECT_FALSE(dv
.Equals(copy
.get()));
538 // Check if Equals detects differences in only the keys.
539 copy
.reset(dv
.DeepCopy());
540 EXPECT_TRUE(dv
.Equals(copy
.get()));
541 copy
->Remove("a", NULL
);
542 copy
->SetBoolean("aa", false);
543 EXPECT_FALSE(dv
.Equals(copy
.get()));
546 TEST(ValuesTest
, StaticEquals
) {
547 scoped_ptr
<Value
> null1(Value::CreateNullValue());
548 scoped_ptr
<Value
> null2(Value::CreateNullValue());
549 EXPECT_TRUE(Value::Equals(null1
.get(), null2
.get()));
550 EXPECT_TRUE(Value::Equals(NULL
, NULL
));
552 scoped_ptr
<Value
> i42(new FundamentalValue(42));
553 scoped_ptr
<Value
> j42(new FundamentalValue(42));
554 scoped_ptr
<Value
> i17(new FundamentalValue(17));
555 EXPECT_TRUE(Value::Equals(i42
.get(), i42
.get()));
556 EXPECT_TRUE(Value::Equals(j42
.get(), i42
.get()));
557 EXPECT_TRUE(Value::Equals(i42
.get(), j42
.get()));
558 EXPECT_FALSE(Value::Equals(i42
.get(), i17
.get()));
559 EXPECT_FALSE(Value::Equals(i42
.get(), NULL
));
560 EXPECT_FALSE(Value::Equals(NULL
, i42
.get()));
562 // NULL and Value::CreateNullValue() are intentionally different: We need
563 // support for NULL as a return value for "undefined" without caring for
564 // ownership of the pointer.
565 EXPECT_FALSE(Value::Equals(null1
.get(), NULL
));
566 EXPECT_FALSE(Value::Equals(NULL
, null1
.get()));
569 TEST(ValuesTest
, DeepCopyCovariantReturnTypes
) {
570 DictionaryValue original_dict
;
571 Value
* original_null
= Value::CreateNullValue();
572 original_dict
.Set("null", original_null
);
573 FundamentalValue
* original_bool
= new FundamentalValue(true);
574 original_dict
.Set("bool", original_bool
);
575 FundamentalValue
* original_int
= new FundamentalValue(42);
576 original_dict
.Set("int", original_int
);
577 FundamentalValue
* original_double
= new FundamentalValue(3.14);
578 original_dict
.Set("double", original_double
);
579 StringValue
* original_string
= new StringValue("hello");
580 original_dict
.Set("string", original_string
);
581 StringValue
* original_string16
= new StringValue(ASCIIToUTF16("hello16"));
582 original_dict
.Set("string16", original_string16
);
584 scoped_ptr
<char[]> original_buffer(new char[42]);
585 memset(original_buffer
.get(), '!', 42);
586 BinaryValue
* original_binary
= new BinaryValue(original_buffer
.Pass(), 42);
587 original_dict
.Set("binary", original_binary
);
589 ListValue
* original_list
= new ListValue();
590 FundamentalValue
* original_list_element_0
= new FundamentalValue(0);
591 original_list
->Append(original_list_element_0
);
592 FundamentalValue
* original_list_element_1
= new FundamentalValue(1);
593 original_list
->Append(original_list_element_1
);
594 original_dict
.Set("list", original_list
);
596 Value
* original_dict_value
= &original_dict
;
597 Value
* original_bool_value
= original_bool
;
598 Value
* original_int_value
= original_int
;
599 Value
* original_double_value
= original_double
;
600 Value
* original_string_value
= original_string
;
601 Value
* original_string16_value
= original_string16
;
602 Value
* original_binary_value
= original_binary
;
603 Value
* original_list_value
= original_list
;
605 scoped_ptr
<Value
> copy_dict_value(original_dict_value
->DeepCopy());
606 scoped_ptr
<Value
> copy_bool_value(original_bool_value
->DeepCopy());
607 scoped_ptr
<Value
> copy_int_value(original_int_value
->DeepCopy());
608 scoped_ptr
<Value
> copy_double_value(original_double_value
->DeepCopy());
609 scoped_ptr
<Value
> copy_string_value(original_string_value
->DeepCopy());
610 scoped_ptr
<Value
> copy_string16_value(original_string16_value
->DeepCopy());
611 scoped_ptr
<Value
> copy_binary_value(original_binary_value
->DeepCopy());
612 scoped_ptr
<Value
> copy_list_value(original_list_value
->DeepCopy());
614 EXPECT_TRUE(original_dict_value
->Equals(copy_dict_value
.get()));
615 EXPECT_TRUE(original_bool_value
->Equals(copy_bool_value
.get()));
616 EXPECT_TRUE(original_int_value
->Equals(copy_int_value
.get()));
617 EXPECT_TRUE(original_double_value
->Equals(copy_double_value
.get()));
618 EXPECT_TRUE(original_string_value
->Equals(copy_string_value
.get()));
619 EXPECT_TRUE(original_string16_value
->Equals(copy_string16_value
.get()));
620 EXPECT_TRUE(original_binary_value
->Equals(copy_binary_value
.get()));
621 EXPECT_TRUE(original_list_value
->Equals(copy_list_value
.get()));
624 TEST(ValuesTest
, RemoveEmptyChildren
) {
625 scoped_ptr
<DictionaryValue
> root(new DictionaryValue
);
626 // Remove empty lists and dictionaries.
627 root
->Set("empty_dict", new DictionaryValue
);
628 root
->Set("empty_list", new ListValue
);
629 root
->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue
);
630 root
.reset(root
->DeepCopyWithoutEmptyChildren());
631 EXPECT_TRUE(root
->empty());
633 // Make sure we don't prune too much.
634 root
->SetBoolean("bool", true);
635 root
->Set("empty_dict", new DictionaryValue
);
636 root
->SetString("empty_string", std::string());
637 root
.reset(root
->DeepCopyWithoutEmptyChildren());
638 EXPECT_EQ(2U, root
->size());
640 // Should do nothing.
641 root
.reset(root
->DeepCopyWithoutEmptyChildren());
642 EXPECT_EQ(2U, root
->size());
644 // Nested test cases. These should all reduce back to the bool and string
647 root
->Set("a.b.c.d.e", new DictionaryValue
);
648 root
.reset(root
->DeepCopyWithoutEmptyChildren());
649 EXPECT_EQ(2U, root
->size());
652 DictionaryValue
* inner
= new DictionaryValue
;
653 root
->Set("dict_with_emtpy_children", inner
);
654 inner
->Set("empty_dict", new DictionaryValue
);
655 inner
->Set("empty_list", new ListValue
);
656 root
.reset(root
->DeepCopyWithoutEmptyChildren());
657 EXPECT_EQ(2U, root
->size());
660 ListValue
* inner
= new ListValue
;
661 root
->Set("list_with_empty_children", inner
);
662 inner
->Append(new DictionaryValue
);
663 inner
->Append(new ListValue
);
664 root
.reset(root
->DeepCopyWithoutEmptyChildren());
665 EXPECT_EQ(2U, root
->size());
668 // Nested with siblings.
670 ListValue
* inner
= new ListValue
;
671 root
->Set("list_with_empty_children", inner
);
672 inner
->Append(new DictionaryValue
);
673 inner
->Append(new ListValue
);
674 DictionaryValue
* inner2
= new DictionaryValue
;
675 root
->Set("dict_with_empty_children", inner2
);
676 inner2
->Set("empty_dict", new DictionaryValue
);
677 inner2
->Set("empty_list", new ListValue
);
678 root
.reset(root
->DeepCopyWithoutEmptyChildren());
679 EXPECT_EQ(2U, root
->size());
682 // Make sure nested values don't get pruned.
684 ListValue
* inner
= new ListValue
;
685 root
->Set("list_with_empty_children", inner
);
686 ListValue
* inner2
= new ListValue
;
687 inner
->Append(new DictionaryValue
);
688 inner
->Append(inner2
);
689 inner2
->Append(new StringValue("hello"));
690 root
.reset(root
->DeepCopyWithoutEmptyChildren());
691 EXPECT_EQ(3U, root
->size());
692 EXPECT_TRUE(root
->GetList("list_with_empty_children", &inner
));
693 EXPECT_EQ(1U, inner
->GetSize()); // Dictionary was pruned.
694 EXPECT_TRUE(inner
->GetList(0, &inner2
));
695 EXPECT_EQ(1U, inner2
->GetSize());
699 TEST(ValuesTest
, MergeDictionary
) {
700 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
701 base
->SetString("base_key", "base_key_value_base");
702 base
->SetString("collide_key", "collide_key_value_base");
703 DictionaryValue
* base_sub_dict
= new DictionaryValue
;
704 base_sub_dict
->SetString("sub_base_key", "sub_base_key_value_base");
705 base_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_base");
706 base
->Set("sub_dict_key", base_sub_dict
);
708 scoped_ptr
<DictionaryValue
> merge(new DictionaryValue
);
709 merge
->SetString("merge_key", "merge_key_value_merge");
710 merge
->SetString("collide_key", "collide_key_value_merge");
711 DictionaryValue
* merge_sub_dict
= new DictionaryValue
;
712 merge_sub_dict
->SetString("sub_merge_key", "sub_merge_key_value_merge");
713 merge_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_merge");
714 merge
->Set("sub_dict_key", merge_sub_dict
);
716 base
->MergeDictionary(merge
.get());
718 EXPECT_EQ(4U, base
->size());
719 std::string base_key_value
;
720 EXPECT_TRUE(base
->GetString("base_key", &base_key_value
));
721 EXPECT_EQ("base_key_value_base", base_key_value
); // Base value preserved.
722 std::string collide_key_value
;
723 EXPECT_TRUE(base
->GetString("collide_key", &collide_key_value
));
724 EXPECT_EQ("collide_key_value_merge", collide_key_value
); // Replaced.
725 std::string merge_key_value
;
726 EXPECT_TRUE(base
->GetString("merge_key", &merge_key_value
));
727 EXPECT_EQ("merge_key_value_merge", merge_key_value
); // Merged in.
729 DictionaryValue
* res_sub_dict
;
730 EXPECT_TRUE(base
->GetDictionary("sub_dict_key", &res_sub_dict
));
731 EXPECT_EQ(3U, res_sub_dict
->size());
732 std::string sub_base_key_value
;
733 EXPECT_TRUE(res_sub_dict
->GetString("sub_base_key", &sub_base_key_value
));
734 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value
); // Preserved.
735 std::string sub_collide_key_value
;
736 EXPECT_TRUE(res_sub_dict
->GetString("sub_collide_key",
737 &sub_collide_key_value
));
738 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value
); // Replaced.
739 std::string sub_merge_key_value
;
740 EXPECT_TRUE(res_sub_dict
->GetString("sub_merge_key", &sub_merge_key_value
));
741 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value
); // Merged in.
744 TEST(ValuesTest
, MergeDictionaryDeepCopy
) {
745 DictionaryValue
* child
= new DictionaryValue
;
746 child
->SetString("test", "value");
747 EXPECT_EQ(1U, child
->size());
750 EXPECT_TRUE(child
->GetString("test", &value
));
751 EXPECT_EQ("value", value
);
753 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
754 base
->Set("dict", child
);
755 EXPECT_EQ(1U, base
->size());
757 DictionaryValue
* ptr
;
758 EXPECT_TRUE(base
->GetDictionary("dict", &ptr
));
759 EXPECT_EQ(child
, ptr
);
761 scoped_ptr
<DictionaryValue
> merged(new DictionaryValue
);
762 merged
->MergeDictionary(base
.get());
763 EXPECT_EQ(1U, merged
->size());
764 EXPECT_TRUE(merged
->GetDictionary("dict", &ptr
));
765 EXPECT_NE(child
, ptr
);
766 EXPECT_TRUE(ptr
->GetString("test", &value
));
767 EXPECT_EQ("value", value
);
769 child
->SetString("test", "overwrite");
771 EXPECT_TRUE(ptr
->GetString("test", &value
));
772 EXPECT_EQ("value", value
);
775 TEST(ValuesTest
, DictionaryIterator
) {
776 DictionaryValue dict
;
777 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
781 StringValue
value1("value1");
782 dict
.Set("key1", value1
.DeepCopy());
784 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
786 EXPECT_EQ("key1", it
.key());
787 EXPECT_TRUE(value1
.Equals(&it
.value()));
792 StringValue
value2("value2");
793 dict
.Set("key2", value2
.DeepCopy());
794 bool seen2
= seen1
= false;
795 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
796 if (it
.key() == "key1") {
798 EXPECT_TRUE(value1
.Equals(&it
.value()));
800 } else if (it
.key() == "key2") {
802 EXPECT_TRUE(value2
.Equals(&it
.value()));