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 CreateStringValue.
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 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 virtual ~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 scoped_ptr
<Value
> removed_item
;
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
);
221 removed_item
.reset();
222 EXPECT_TRUE(deletion_flag
);
226 list
.Append(new DeletionTestValue(&deletion_flag
));
227 EXPECT_FALSE(deletion_flag
);
228 EXPECT_TRUE(list
.Remove(0, NULL
));
229 EXPECT_TRUE(deletion_flag
);
230 EXPECT_EQ(0U, list
.GetSize());
235 DeletionTestValue
* value
= new DeletionTestValue(&deletion_flag
);
237 EXPECT_FALSE(deletion_flag
);
239 list
.Remove(*value
, &index
);
240 EXPECT_EQ(0U, index
);
241 EXPECT_TRUE(deletion_flag
);
242 EXPECT_EQ(0U, list
.GetSize());
246 TEST(ValuesTest
, DictionaryDeletion
) {
247 std::string key
= "test";
248 bool deletion_flag
= true;
251 DictionaryValue dict
;
252 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
253 EXPECT_FALSE(deletion_flag
);
255 EXPECT_TRUE(deletion_flag
);
258 DictionaryValue dict
;
259 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
260 EXPECT_FALSE(deletion_flag
);
262 EXPECT_TRUE(deletion_flag
);
266 DictionaryValue dict
;
267 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
268 EXPECT_FALSE(deletion_flag
);
269 dict
.Set(key
, Value::CreateNullValue());
270 EXPECT_TRUE(deletion_flag
);
274 TEST(ValuesTest
, DictionaryRemoval
) {
275 std::string key
= "test";
276 bool deletion_flag
= true;
277 scoped_ptr
<Value
> removed_item
;
280 DictionaryValue dict
;
281 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
282 EXPECT_FALSE(deletion_flag
);
283 EXPECT_TRUE(dict
.HasKey(key
));
284 EXPECT_FALSE(dict
.Remove("absent key", &removed_item
));
285 EXPECT_TRUE(dict
.Remove(key
, &removed_item
));
286 EXPECT_FALSE(dict
.HasKey(key
));
287 ASSERT_TRUE(removed_item
);
289 EXPECT_FALSE(deletion_flag
);
290 removed_item
.reset();
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"));
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"));
320 EXPECT_FALSE(dict
.Get("this.isnt.expanded", &value3
));
322 ASSERT_TRUE(dict
.GetWithoutPathExpansion("this.isnt.expanded", &value4
));
323 EXPECT_EQ(Value::TYPE_NULL
, value4
->GetType());
326 TEST(ValuesTest
, DictionaryRemovePath
) {
327 DictionaryValue dict
;
328 dict
.Set("a.long.way.down", Value::CreateIntegerValue(1));
329 dict
.Set("a.long.key.path", Value::CreateBooleanValue(true));
331 scoped_ptr
<Value
> removed_item
;
332 EXPECT_TRUE(dict
.RemovePath("a.long.way.down", &removed_item
));
333 ASSERT_TRUE(removed_item
);
334 EXPECT_TRUE(removed_item
->IsType(base::Value::TYPE_INTEGER
));
335 EXPECT_FALSE(dict
.HasKey("a.long.way.down"));
336 EXPECT_FALSE(dict
.HasKey("a.long.way"));
337 EXPECT_TRUE(dict
.Get("a.long.key.path", NULL
));
339 removed_item
.reset();
340 EXPECT_FALSE(dict
.RemovePath("a.long.way.down", &removed_item
));
341 EXPECT_FALSE(removed_item
);
342 EXPECT_TRUE(dict
.Get("a.long.key.path", NULL
));
344 removed_item
.reset();
345 EXPECT_TRUE(dict
.RemovePath("a.long.key.path", &removed_item
));
346 ASSERT_TRUE(removed_item
);
347 EXPECT_TRUE(removed_item
->IsType(base::Value::TYPE_BOOLEAN
));
348 EXPECT_TRUE(dict
.empty());
351 TEST(ValuesTest
, DeepCopy
) {
352 DictionaryValue original_dict
;
353 Value
* original_null
= Value::CreateNullValue();
354 original_dict
.Set("null", original_null
);
355 FundamentalValue
* original_bool
= new FundamentalValue(true);
356 original_dict
.Set("bool", original_bool
);
357 FundamentalValue
* original_int
= new FundamentalValue(42);
358 original_dict
.Set("int", original_int
);
359 FundamentalValue
* original_double
= new FundamentalValue(3.14);
360 original_dict
.Set("double", original_double
);
361 StringValue
* original_string
= new StringValue("hello");
362 original_dict
.Set("string", original_string
);
363 StringValue
* original_string16
= new StringValue(ASCIIToUTF16("hello16"));
364 original_dict
.Set("string16", original_string16
);
366 scoped_ptr
<char[]> original_buffer(new char[42]);
367 memset(original_buffer
.get(), '!', 42);
368 BinaryValue
* original_binary
= new BinaryValue(original_buffer
.Pass(), 42);
369 original_dict
.Set("binary", original_binary
);
371 ListValue
* original_list
= new ListValue();
372 FundamentalValue
* original_list_element_0
= new FundamentalValue(0);
373 original_list
->Append(original_list_element_0
);
374 FundamentalValue
* original_list_element_1
= new FundamentalValue(1);
375 original_list
->Append(original_list_element_1
);
376 original_dict
.Set("list", original_list
);
378 DictionaryValue
* original_nested_dictionary
= new DictionaryValue();
379 original_nested_dictionary
->Set("key", new StringValue("value"));
380 original_dict
.Set("dictionary", original_nested_dictionary
);
382 scoped_ptr
<DictionaryValue
> copy_dict(original_dict
.DeepCopy());
383 ASSERT_TRUE(copy_dict
.get());
384 ASSERT_NE(copy_dict
.get(), &original_dict
);
386 Value
* copy_null
= NULL
;
387 ASSERT_TRUE(copy_dict
->Get("null", ©_null
));
388 ASSERT_TRUE(copy_null
);
389 ASSERT_NE(copy_null
, original_null
);
390 ASSERT_TRUE(copy_null
->IsType(Value::TYPE_NULL
));
392 Value
* copy_bool
= NULL
;
393 ASSERT_TRUE(copy_dict
->Get("bool", ©_bool
));
394 ASSERT_TRUE(copy_bool
);
395 ASSERT_NE(copy_bool
, original_bool
);
396 ASSERT_TRUE(copy_bool
->IsType(Value::TYPE_BOOLEAN
));
397 bool copy_bool_value
= false;
398 ASSERT_TRUE(copy_bool
->GetAsBoolean(©_bool_value
));
399 ASSERT_TRUE(copy_bool_value
);
401 Value
* copy_int
= NULL
;
402 ASSERT_TRUE(copy_dict
->Get("int", ©_int
));
403 ASSERT_TRUE(copy_int
);
404 ASSERT_NE(copy_int
, original_int
);
405 ASSERT_TRUE(copy_int
->IsType(Value::TYPE_INTEGER
));
406 int copy_int_value
= 0;
407 ASSERT_TRUE(copy_int
->GetAsInteger(©_int_value
));
408 ASSERT_EQ(42, copy_int_value
);
410 Value
* copy_double
= NULL
;
411 ASSERT_TRUE(copy_dict
->Get("double", ©_double
));
412 ASSERT_TRUE(copy_double
);
413 ASSERT_NE(copy_double
, original_double
);
414 ASSERT_TRUE(copy_double
->IsType(Value::TYPE_DOUBLE
));
415 double copy_double_value
= 0;
416 ASSERT_TRUE(copy_double
->GetAsDouble(©_double_value
));
417 ASSERT_EQ(3.14, copy_double_value
);
419 Value
* copy_string
= NULL
;
420 ASSERT_TRUE(copy_dict
->Get("string", ©_string
));
421 ASSERT_TRUE(copy_string
);
422 ASSERT_NE(copy_string
, original_string
);
423 ASSERT_TRUE(copy_string
->IsType(Value::TYPE_STRING
));
424 std::string copy_string_value
;
425 string16 copy_string16_value
;
426 ASSERT_TRUE(copy_string
->GetAsString(©_string_value
));
427 ASSERT_TRUE(copy_string
->GetAsString(©_string16_value
));
428 ASSERT_EQ(std::string("hello"), copy_string_value
);
429 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value
);
431 Value
* copy_string16
= NULL
;
432 ASSERT_TRUE(copy_dict
->Get("string16", ©_string16
));
433 ASSERT_TRUE(copy_string16
);
434 ASSERT_NE(copy_string16
, original_string16
);
435 ASSERT_TRUE(copy_string16
->IsType(Value::TYPE_STRING
));
436 ASSERT_TRUE(copy_string16
->GetAsString(©_string_value
));
437 ASSERT_TRUE(copy_string16
->GetAsString(©_string16_value
));
438 ASSERT_EQ(std::string("hello16"), copy_string_value
);
439 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value
);
441 Value
* copy_binary
= NULL
;
442 ASSERT_TRUE(copy_dict
->Get("binary", ©_binary
));
443 ASSERT_TRUE(copy_binary
);
444 ASSERT_NE(copy_binary
, original_binary
);
445 ASSERT_TRUE(copy_binary
->IsType(Value::TYPE_BINARY
));
446 ASSERT_NE(original_binary
->GetBuffer(),
447 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer());
448 ASSERT_EQ(original_binary
->GetSize(),
449 static_cast<BinaryValue
*>(copy_binary
)->GetSize());
450 ASSERT_EQ(0, memcmp(original_binary
->GetBuffer(),
451 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer(),
452 original_binary
->GetSize()));
454 Value
* copy_value
= NULL
;
455 ASSERT_TRUE(copy_dict
->Get("list", ©_value
));
456 ASSERT_TRUE(copy_value
);
457 ASSERT_NE(copy_value
, original_list
);
458 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_LIST
));
459 ListValue
* copy_list
= NULL
;
460 ASSERT_TRUE(copy_value
->GetAsList(©_list
));
461 ASSERT_TRUE(copy_list
);
462 ASSERT_EQ(2U, copy_list
->GetSize());
464 Value
* copy_list_element_0
;
465 ASSERT_TRUE(copy_list
->Get(0, ©_list_element_0
));
466 ASSERT_TRUE(copy_list_element_0
);
467 ASSERT_NE(copy_list_element_0
, original_list_element_0
);
468 int copy_list_element_0_value
;
469 ASSERT_TRUE(copy_list_element_0
->GetAsInteger(©_list_element_0_value
));
470 ASSERT_EQ(0, copy_list_element_0_value
);
472 Value
* copy_list_element_1
;
473 ASSERT_TRUE(copy_list
->Get(1, ©_list_element_1
));
474 ASSERT_TRUE(copy_list_element_1
);
475 ASSERT_NE(copy_list_element_1
, original_list_element_1
);
476 int copy_list_element_1_value
;
477 ASSERT_TRUE(copy_list_element_1
->GetAsInteger(©_list_element_1_value
));
478 ASSERT_EQ(1, copy_list_element_1_value
);
481 ASSERT_TRUE(copy_dict
->Get("dictionary", ©_value
));
482 ASSERT_TRUE(copy_value
);
483 ASSERT_NE(copy_value
, original_nested_dictionary
);
484 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_DICTIONARY
));
485 DictionaryValue
* copy_nested_dictionary
= NULL
;
486 ASSERT_TRUE(copy_value
->GetAsDictionary(©_nested_dictionary
));
487 ASSERT_TRUE(copy_nested_dictionary
);
488 EXPECT_TRUE(copy_nested_dictionary
->HasKey("key"));
491 TEST(ValuesTest
, Equals
) {
492 Value
* null1
= Value::CreateNullValue();
493 Value
* null2
= Value::CreateNullValue();
494 EXPECT_NE(null1
, null2
);
495 EXPECT_TRUE(null1
->Equals(null2
));
497 Value
* boolean
= new FundamentalValue(false);
498 EXPECT_FALSE(null1
->Equals(boolean
));
504 dv
.SetBoolean("a", false);
505 dv
.SetInteger("b", 2);
506 dv
.SetDouble("c", 2.5);
507 dv
.SetString("d1", "string");
508 dv
.SetString("d2", ASCIIToUTF16("http://google.com"));
509 dv
.Set("e", Value::CreateNullValue());
511 scoped_ptr
<DictionaryValue
> copy
;
512 copy
.reset(dv
.DeepCopy());
513 EXPECT_TRUE(dv
.Equals(copy
.get()));
515 ListValue
* list
= new ListValue
;
516 list
->Append(Value::CreateNullValue());
517 list
->Append(new DictionaryValue
);
520 EXPECT_FALSE(dv
.Equals(copy
.get()));
521 copy
->Set("f", list
->DeepCopy());
522 EXPECT_TRUE(dv
.Equals(copy
.get()));
524 list
->Append(new FundamentalValue(true));
525 EXPECT_FALSE(dv
.Equals(copy
.get()));
527 // Check if Equals detects differences in only the keys.
528 copy
.reset(dv
.DeepCopy());
529 EXPECT_TRUE(dv
.Equals(copy
.get()));
530 copy
->Remove("a", NULL
);
531 copy
->SetBoolean("aa", false);
532 EXPECT_FALSE(dv
.Equals(copy
.get()));
535 TEST(ValuesTest
, StaticEquals
) {
536 scoped_ptr
<Value
> null1(Value::CreateNullValue());
537 scoped_ptr
<Value
> null2(Value::CreateNullValue());
538 EXPECT_TRUE(Value::Equals(null1
.get(), null2
.get()));
539 EXPECT_TRUE(Value::Equals(NULL
, NULL
));
541 scoped_ptr
<Value
> i42(new FundamentalValue(42));
542 scoped_ptr
<Value
> j42(new FundamentalValue(42));
543 scoped_ptr
<Value
> i17(new FundamentalValue(17));
544 EXPECT_TRUE(Value::Equals(i42
.get(), i42
.get()));
545 EXPECT_TRUE(Value::Equals(j42
.get(), i42
.get()));
546 EXPECT_TRUE(Value::Equals(i42
.get(), j42
.get()));
547 EXPECT_FALSE(Value::Equals(i42
.get(), i17
.get()));
548 EXPECT_FALSE(Value::Equals(i42
.get(), NULL
));
549 EXPECT_FALSE(Value::Equals(NULL
, i42
.get()));
551 // NULL and Value::CreateNullValue() are intentionally different: We need
552 // support for NULL as a return value for "undefined" without caring for
553 // ownership of the pointer.
554 EXPECT_FALSE(Value::Equals(null1
.get(), NULL
));
555 EXPECT_FALSE(Value::Equals(NULL
, null1
.get()));
558 TEST(ValuesTest
, DeepCopyCovariantReturnTypes
) {
559 DictionaryValue original_dict
;
560 Value
* original_null
= Value::CreateNullValue();
561 original_dict
.Set("null", original_null
);
562 FundamentalValue
* original_bool
= new FundamentalValue(true);
563 original_dict
.Set("bool", original_bool
);
564 FundamentalValue
* original_int
= new FundamentalValue(42);
565 original_dict
.Set("int", original_int
);
566 FundamentalValue
* original_double
= new FundamentalValue(3.14);
567 original_dict
.Set("double", original_double
);
568 StringValue
* original_string
= new StringValue("hello");
569 original_dict
.Set("string", original_string
);
570 StringValue
* original_string16
= new StringValue(ASCIIToUTF16("hello16"));
571 original_dict
.Set("string16", original_string16
);
573 scoped_ptr
<char[]> original_buffer(new char[42]);
574 memset(original_buffer
.get(), '!', 42);
575 BinaryValue
* original_binary
= new BinaryValue(original_buffer
.Pass(), 42);
576 original_dict
.Set("binary", original_binary
);
578 ListValue
* original_list
= new ListValue();
579 FundamentalValue
* original_list_element_0
= new FundamentalValue(0);
580 original_list
->Append(original_list_element_0
);
581 FundamentalValue
* original_list_element_1
= new FundamentalValue(1);
582 original_list
->Append(original_list_element_1
);
583 original_dict
.Set("list", original_list
);
585 Value
* original_dict_value
= &original_dict
;
586 Value
* original_bool_value
= original_bool
;
587 Value
* original_int_value
= original_int
;
588 Value
* original_double_value
= original_double
;
589 Value
* original_string_value
= original_string
;
590 Value
* original_string16_value
= original_string16
;
591 Value
* original_binary_value
= original_binary
;
592 Value
* original_list_value
= original_list
;
594 scoped_ptr
<Value
> copy_dict_value(original_dict_value
->DeepCopy());
595 scoped_ptr
<Value
> copy_bool_value(original_bool_value
->DeepCopy());
596 scoped_ptr
<Value
> copy_int_value(original_int_value
->DeepCopy());
597 scoped_ptr
<Value
> copy_double_value(original_double_value
->DeepCopy());
598 scoped_ptr
<Value
> copy_string_value(original_string_value
->DeepCopy());
599 scoped_ptr
<Value
> copy_string16_value(original_string16_value
->DeepCopy());
600 scoped_ptr
<Value
> copy_binary_value(original_binary_value
->DeepCopy());
601 scoped_ptr
<Value
> copy_list_value(original_list_value
->DeepCopy());
603 EXPECT_TRUE(original_dict_value
->Equals(copy_dict_value
.get()));
604 EXPECT_TRUE(original_bool_value
->Equals(copy_bool_value
.get()));
605 EXPECT_TRUE(original_int_value
->Equals(copy_int_value
.get()));
606 EXPECT_TRUE(original_double_value
->Equals(copy_double_value
.get()));
607 EXPECT_TRUE(original_string_value
->Equals(copy_string_value
.get()));
608 EXPECT_TRUE(original_string16_value
->Equals(copy_string16_value
.get()));
609 EXPECT_TRUE(original_binary_value
->Equals(copy_binary_value
.get()));
610 EXPECT_TRUE(original_list_value
->Equals(copy_list_value
.get()));
613 TEST(ValuesTest
, RemoveEmptyChildren
) {
614 scoped_ptr
<DictionaryValue
> root(new DictionaryValue
);
615 // Remove empty lists and dictionaries.
616 root
->Set("empty_dict", new DictionaryValue
);
617 root
->Set("empty_list", new ListValue
);
618 root
->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue
);
619 root
.reset(root
->DeepCopyWithoutEmptyChildren());
620 EXPECT_TRUE(root
->empty());
622 // Make sure we don't prune too much.
623 root
->SetBoolean("bool", true);
624 root
->Set("empty_dict", new DictionaryValue
);
625 root
->SetString("empty_string", std::string());
626 root
.reset(root
->DeepCopyWithoutEmptyChildren());
627 EXPECT_EQ(2U, root
->size());
629 // Should do nothing.
630 root
.reset(root
->DeepCopyWithoutEmptyChildren());
631 EXPECT_EQ(2U, root
->size());
633 // Nested test cases. These should all reduce back to the bool and string
636 root
->Set("a.b.c.d.e", new DictionaryValue
);
637 root
.reset(root
->DeepCopyWithoutEmptyChildren());
638 EXPECT_EQ(2U, root
->size());
641 DictionaryValue
* inner
= new DictionaryValue
;
642 root
->Set("dict_with_emtpy_children", inner
);
643 inner
->Set("empty_dict", new DictionaryValue
);
644 inner
->Set("empty_list", new ListValue
);
645 root
.reset(root
->DeepCopyWithoutEmptyChildren());
646 EXPECT_EQ(2U, root
->size());
649 ListValue
* inner
= new ListValue
;
650 root
->Set("list_with_empty_children", inner
);
651 inner
->Append(new DictionaryValue
);
652 inner
->Append(new ListValue
);
653 root
.reset(root
->DeepCopyWithoutEmptyChildren());
654 EXPECT_EQ(2U, root
->size());
657 // Nested with siblings.
659 ListValue
* inner
= new ListValue
;
660 root
->Set("list_with_empty_children", inner
);
661 inner
->Append(new DictionaryValue
);
662 inner
->Append(new ListValue
);
663 DictionaryValue
* inner2
= new DictionaryValue
;
664 root
->Set("dict_with_empty_children", inner2
);
665 inner2
->Set("empty_dict", new DictionaryValue
);
666 inner2
->Set("empty_list", new ListValue
);
667 root
.reset(root
->DeepCopyWithoutEmptyChildren());
668 EXPECT_EQ(2U, root
->size());
671 // Make sure nested values don't get pruned.
673 ListValue
* inner
= new ListValue
;
674 root
->Set("list_with_empty_children", inner
);
675 ListValue
* inner2
= new ListValue
;
676 inner
->Append(new DictionaryValue
);
677 inner
->Append(inner2
);
678 inner2
->Append(new StringValue("hello"));
679 root
.reset(root
->DeepCopyWithoutEmptyChildren());
680 EXPECT_EQ(3U, root
->size());
681 EXPECT_TRUE(root
->GetList("list_with_empty_children", &inner
));
682 EXPECT_EQ(1U, inner
->GetSize()); // Dictionary was pruned.
683 EXPECT_TRUE(inner
->GetList(0, &inner2
));
684 EXPECT_EQ(1U, inner2
->GetSize());
688 TEST(ValuesTest
, MergeDictionary
) {
689 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
690 base
->SetString("base_key", "base_key_value_base");
691 base
->SetString("collide_key", "collide_key_value_base");
692 DictionaryValue
* base_sub_dict
= new DictionaryValue
;
693 base_sub_dict
->SetString("sub_base_key", "sub_base_key_value_base");
694 base_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_base");
695 base
->Set("sub_dict_key", base_sub_dict
);
697 scoped_ptr
<DictionaryValue
> merge(new DictionaryValue
);
698 merge
->SetString("merge_key", "merge_key_value_merge");
699 merge
->SetString("collide_key", "collide_key_value_merge");
700 DictionaryValue
* merge_sub_dict
= new DictionaryValue
;
701 merge_sub_dict
->SetString("sub_merge_key", "sub_merge_key_value_merge");
702 merge_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_merge");
703 merge
->Set("sub_dict_key", merge_sub_dict
);
705 base
->MergeDictionary(merge
.get());
707 EXPECT_EQ(4U, base
->size());
708 std::string base_key_value
;
709 EXPECT_TRUE(base
->GetString("base_key", &base_key_value
));
710 EXPECT_EQ("base_key_value_base", base_key_value
); // Base value preserved.
711 std::string collide_key_value
;
712 EXPECT_TRUE(base
->GetString("collide_key", &collide_key_value
));
713 EXPECT_EQ("collide_key_value_merge", collide_key_value
); // Replaced.
714 std::string merge_key_value
;
715 EXPECT_TRUE(base
->GetString("merge_key", &merge_key_value
));
716 EXPECT_EQ("merge_key_value_merge", merge_key_value
); // Merged in.
718 DictionaryValue
* res_sub_dict
;
719 EXPECT_TRUE(base
->GetDictionary("sub_dict_key", &res_sub_dict
));
720 EXPECT_EQ(3U, res_sub_dict
->size());
721 std::string sub_base_key_value
;
722 EXPECT_TRUE(res_sub_dict
->GetString("sub_base_key", &sub_base_key_value
));
723 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value
); // Preserved.
724 std::string sub_collide_key_value
;
725 EXPECT_TRUE(res_sub_dict
->GetString("sub_collide_key",
726 &sub_collide_key_value
));
727 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value
); // Replaced.
728 std::string sub_merge_key_value
;
729 EXPECT_TRUE(res_sub_dict
->GetString("sub_merge_key", &sub_merge_key_value
));
730 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value
); // Merged in.
733 TEST(ValuesTest
, MergeDictionaryDeepCopy
) {
734 DictionaryValue
* child
= new DictionaryValue
;
735 child
->SetString("test", "value");
736 EXPECT_EQ(1U, child
->size());
739 EXPECT_TRUE(child
->GetString("test", &value
));
740 EXPECT_EQ("value", value
);
742 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
743 base
->Set("dict", child
);
744 EXPECT_EQ(1U, base
->size());
746 DictionaryValue
* ptr
;
747 EXPECT_TRUE(base
->GetDictionary("dict", &ptr
));
748 EXPECT_EQ(child
, ptr
);
750 scoped_ptr
<DictionaryValue
> merged(new DictionaryValue
);
751 merged
->MergeDictionary(base
.get());
752 EXPECT_EQ(1U, merged
->size());
753 EXPECT_TRUE(merged
->GetDictionary("dict", &ptr
));
754 EXPECT_NE(child
, ptr
);
755 EXPECT_TRUE(ptr
->GetString("test", &value
));
756 EXPECT_EQ("value", value
);
758 child
->SetString("test", "overwrite");
760 EXPECT_TRUE(ptr
->GetString("test", &value
));
761 EXPECT_EQ("value", value
);
764 TEST(ValuesTest
, DictionaryIterator
) {
765 DictionaryValue dict
;
766 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
770 StringValue
value1("value1");
771 dict
.Set("key1", value1
.DeepCopy());
773 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
775 EXPECT_EQ("key1", it
.key());
776 EXPECT_TRUE(value1
.Equals(&it
.value()));
781 StringValue
value2("value2");
782 dict
.Set("key2", value2
.DeepCopy());
783 bool seen2
= seen1
= false;
784 for (DictionaryValue::Iterator
it(dict
); !it
.IsAtEnd(); it
.Advance()) {
785 if (it
.key() == "key1") {
787 EXPECT_TRUE(value1
.Equals(&it
.value()));
789 } else if (it
.key() == "key2") {
791 EXPECT_TRUE(value2
.Equals(&it
.value()));