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
);
97 // Try searching in the mixed list.
98 scoped_ptr
<Value
> sought_value(Value::CreateIntegerValue(42));
99 scoped_ptr
<Value
> not_found_value(Value::CreateBooleanValue(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
) {
109 // Passing a null buffer pointer doesn't yield a BinaryValue
110 scoped_ptr
<BinaryValue
> binary(BinaryValue::Create(buffer
, 0));
111 ASSERT_FALSE(binary
.get());
113 // If you want to represent an empty binary value, use a zero-length buffer.
114 buffer
= new char[1];
116 binary
.reset(BinaryValue::Create(buffer
, 0));
117 ASSERT_TRUE(binary
.get());
118 ASSERT_TRUE(binary
->GetBuffer());
119 ASSERT_EQ(buffer
, binary
->GetBuffer());
120 ASSERT_EQ(0U, binary
->GetSize());
122 // Test the common case of a non-empty buffer
123 buffer
= new char[15];
124 binary
.reset(BinaryValue::Create(buffer
, 15));
125 ASSERT_TRUE(binary
.get());
126 ASSERT_TRUE(binary
->GetBuffer());
127 ASSERT_EQ(buffer
, binary
->GetBuffer());
128 ASSERT_EQ(15U, binary
->GetSize());
130 char stack_buffer
[42];
131 memset(stack_buffer
, '!', 42);
132 binary
.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer
, 42));
133 ASSERT_TRUE(binary
.get());
134 ASSERT_TRUE(binary
->GetBuffer());
135 ASSERT_NE(stack_buffer
, binary
->GetBuffer());
136 ASSERT_EQ(42U, binary
->GetSize());
137 ASSERT_EQ(0, memcmp(stack_buffer
, binary
->GetBuffer(), binary
->GetSize()));
140 TEST(ValuesTest
, StringValue
) {
141 // Test overloaded CreateStringValue.
142 scoped_ptr
<Value
> narrow_value(Value::CreateStringValue("narrow"));
143 ASSERT_TRUE(narrow_value
.get());
144 ASSERT_TRUE(narrow_value
->IsType(Value::TYPE_STRING
));
145 scoped_ptr
<Value
> utf16_value(
146 Value::CreateStringValue(ASCIIToUTF16("utf16")));
147 ASSERT_TRUE(utf16_value
.get());
148 ASSERT_TRUE(utf16_value
->IsType(Value::TYPE_STRING
));
150 // Test overloaded GetString.
151 std::string narrow
= "http://google.com";
152 string16 utf16
= ASCIIToUTF16("http://google.com");
153 ASSERT_TRUE(narrow_value
->GetAsString(&narrow
));
154 ASSERT_TRUE(narrow_value
->GetAsString(&utf16
));
155 ASSERT_EQ(std::string("narrow"), narrow
);
156 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16
);
158 ASSERT_TRUE(utf16_value
->GetAsString(&narrow
));
159 ASSERT_TRUE(utf16_value
->GetAsString(&utf16
));
160 ASSERT_EQ(std::string("utf16"), narrow
);
161 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16
);
164 // This is a Value object that allows us to tell if it's been
165 // properly deleted by modifying the value of external flag on destruction.
166 class DeletionTestValue
: public Value
{
168 explicit DeletionTestValue(bool* deletion_flag
) : Value(TYPE_NULL
) {
169 Init(deletion_flag
); // Separate function so that we can use ASSERT_*
172 void Init(bool* deletion_flag
) {
173 ASSERT_TRUE(deletion_flag
);
174 deletion_flag_
= deletion_flag
;
175 *deletion_flag_
= false;
178 ~DeletionTestValue() {
179 *deletion_flag_
= true;
183 bool* deletion_flag_
;
186 TEST(ValuesTest
, ListDeletion
) {
187 bool deletion_flag
= true;
191 list
.Append(new DeletionTestValue(&deletion_flag
));
192 EXPECT_FALSE(deletion_flag
);
194 EXPECT_TRUE(deletion_flag
);
198 list
.Append(new DeletionTestValue(&deletion_flag
));
199 EXPECT_FALSE(deletion_flag
);
201 EXPECT_TRUE(deletion_flag
);
206 list
.Append(new DeletionTestValue(&deletion_flag
));
207 EXPECT_FALSE(deletion_flag
);
208 EXPECT_TRUE(list
.Set(0, Value::CreateNullValue()));
209 EXPECT_TRUE(deletion_flag
);
213 TEST(ValuesTest
, ListRemoval
) {
214 bool deletion_flag
= true;
215 Value
* removed_item
= NULL
;
219 list
.Append(new DeletionTestValue(&deletion_flag
));
220 EXPECT_FALSE(deletion_flag
);
221 EXPECT_EQ(1U, list
.GetSize());
222 EXPECT_FALSE(list
.Remove(std::numeric_limits
<size_t>::max(),
224 EXPECT_FALSE(list
.Remove(1, &removed_item
));
225 EXPECT_TRUE(list
.Remove(0, &removed_item
));
226 ASSERT_TRUE(removed_item
);
227 EXPECT_EQ(0U, list
.GetSize());
229 EXPECT_FALSE(deletion_flag
);
232 EXPECT_TRUE(deletion_flag
);
236 list
.Append(new DeletionTestValue(&deletion_flag
));
237 EXPECT_FALSE(deletion_flag
);
238 EXPECT_TRUE(list
.Remove(0, NULL
));
239 EXPECT_TRUE(deletion_flag
);
240 EXPECT_EQ(0U, list
.GetSize());
245 DeletionTestValue
* value
= new DeletionTestValue(&deletion_flag
);
247 EXPECT_FALSE(deletion_flag
);
249 list
.Remove(*value
, &index
);
250 EXPECT_EQ(0U, index
);
251 EXPECT_TRUE(deletion_flag
);
252 EXPECT_EQ(0U, list
.GetSize());
256 TEST(ValuesTest
, DictionaryDeletion
) {
257 std::string key
= "test";
258 bool deletion_flag
= true;
261 DictionaryValue dict
;
262 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
263 EXPECT_FALSE(deletion_flag
);
265 EXPECT_TRUE(deletion_flag
);
268 DictionaryValue dict
;
269 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
270 EXPECT_FALSE(deletion_flag
);
272 EXPECT_TRUE(deletion_flag
);
276 DictionaryValue dict
;
277 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
278 EXPECT_FALSE(deletion_flag
);
279 dict
.Set(key
, Value::CreateNullValue());
280 EXPECT_TRUE(deletion_flag
);
284 TEST(ValuesTest
, DictionaryRemoval
) {
285 std::string key
= "test";
286 bool deletion_flag
= true;
287 Value
* removed_item
= NULL
;
290 DictionaryValue dict
;
291 dict
.Set(key
, new DeletionTestValue(&deletion_flag
));
292 EXPECT_FALSE(deletion_flag
);
293 EXPECT_TRUE(dict
.HasKey(key
));
294 EXPECT_FALSE(dict
.Remove("absent key", &removed_item
));
295 EXPECT_TRUE(dict
.Remove(key
, &removed_item
));
296 EXPECT_FALSE(dict
.HasKey(key
));
297 ASSERT_TRUE(removed_item
);
299 EXPECT_FALSE(deletion_flag
);
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
, DeepCopy
) {
338 DictionaryValue original_dict
;
339 Value
* original_null
= Value::CreateNullValue();
340 original_dict
.Set("null", original_null
);
341 FundamentalValue
* original_bool
= Value::CreateBooleanValue(true);
342 original_dict
.Set("bool", original_bool
);
343 FundamentalValue
* original_int
= Value::CreateIntegerValue(42);
344 original_dict
.Set("int", original_int
);
345 FundamentalValue
* original_double
= Value::CreateDoubleValue(3.14);
346 original_dict
.Set("double", original_double
);
347 StringValue
* original_string
= Value::CreateStringValue("hello");
348 original_dict
.Set("string", original_string
);
349 StringValue
* original_string16
=
350 Value::CreateStringValue(ASCIIToUTF16("hello16"));
351 original_dict
.Set("string16", original_string16
);
353 char* original_buffer
= new char[42];
354 memset(original_buffer
, '!', 42);
355 BinaryValue
* original_binary
= BinaryValue::Create(original_buffer
, 42);
356 original_dict
.Set("binary", original_binary
);
358 ListValue
* original_list
= new ListValue();
359 FundamentalValue
* original_list_element_0
= Value::CreateIntegerValue(0);
360 original_list
->Append(original_list_element_0
);
361 FundamentalValue
* original_list_element_1
= Value::CreateIntegerValue(1);
362 original_list
->Append(original_list_element_1
);
363 original_dict
.Set("list", original_list
);
365 DictionaryValue
* original_nested_dictionary
= new DictionaryValue();
366 original_nested_dictionary
->Set("key", Value::CreateStringValue("value"));
367 original_dict
.Set("dictionary", original_nested_dictionary
);
369 scoped_ptr
<DictionaryValue
> copy_dict(original_dict
.DeepCopy());
370 ASSERT_TRUE(copy_dict
.get());
371 ASSERT_NE(copy_dict
.get(), &original_dict
);
373 Value
* copy_null
= NULL
;
374 ASSERT_TRUE(copy_dict
->Get("null", ©_null
));
375 ASSERT_TRUE(copy_null
);
376 ASSERT_NE(copy_null
, original_null
);
377 ASSERT_TRUE(copy_null
->IsType(Value::TYPE_NULL
));
379 Value
* copy_bool
= NULL
;
380 ASSERT_TRUE(copy_dict
->Get("bool", ©_bool
));
381 ASSERT_TRUE(copy_bool
);
382 ASSERT_NE(copy_bool
, original_bool
);
383 ASSERT_TRUE(copy_bool
->IsType(Value::TYPE_BOOLEAN
));
384 bool copy_bool_value
= false;
385 ASSERT_TRUE(copy_bool
->GetAsBoolean(©_bool_value
));
386 ASSERT_TRUE(copy_bool_value
);
388 Value
* copy_int
= NULL
;
389 ASSERT_TRUE(copy_dict
->Get("int", ©_int
));
390 ASSERT_TRUE(copy_int
);
391 ASSERT_NE(copy_int
, original_int
);
392 ASSERT_TRUE(copy_int
->IsType(Value::TYPE_INTEGER
));
393 int copy_int_value
= 0;
394 ASSERT_TRUE(copy_int
->GetAsInteger(©_int_value
));
395 ASSERT_EQ(42, copy_int_value
);
397 Value
* copy_double
= NULL
;
398 ASSERT_TRUE(copy_dict
->Get("double", ©_double
));
399 ASSERT_TRUE(copy_double
);
400 ASSERT_NE(copy_double
, original_double
);
401 ASSERT_TRUE(copy_double
->IsType(Value::TYPE_DOUBLE
));
402 double copy_double_value
= 0;
403 ASSERT_TRUE(copy_double
->GetAsDouble(©_double_value
));
404 ASSERT_EQ(3.14, copy_double_value
);
406 Value
* copy_string
= NULL
;
407 ASSERT_TRUE(copy_dict
->Get("string", ©_string
));
408 ASSERT_TRUE(copy_string
);
409 ASSERT_NE(copy_string
, original_string
);
410 ASSERT_TRUE(copy_string
->IsType(Value::TYPE_STRING
));
411 std::string copy_string_value
;
412 string16 copy_string16_value
;
413 ASSERT_TRUE(copy_string
->GetAsString(©_string_value
));
414 ASSERT_TRUE(copy_string
->GetAsString(©_string16_value
));
415 ASSERT_EQ(std::string("hello"), copy_string_value
);
416 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value
);
418 Value
* copy_string16
= NULL
;
419 ASSERT_TRUE(copy_dict
->Get("string16", ©_string16
));
420 ASSERT_TRUE(copy_string16
);
421 ASSERT_NE(copy_string16
, original_string16
);
422 ASSERT_TRUE(copy_string16
->IsType(Value::TYPE_STRING
));
423 ASSERT_TRUE(copy_string16
->GetAsString(©_string_value
));
424 ASSERT_TRUE(copy_string16
->GetAsString(©_string16_value
));
425 ASSERT_EQ(std::string("hello16"), copy_string_value
);
426 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value
);
428 Value
* copy_binary
= NULL
;
429 ASSERT_TRUE(copy_dict
->Get("binary", ©_binary
));
430 ASSERT_TRUE(copy_binary
);
431 ASSERT_NE(copy_binary
, original_binary
);
432 ASSERT_TRUE(copy_binary
->IsType(Value::TYPE_BINARY
));
433 ASSERT_NE(original_binary
->GetBuffer(),
434 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer());
435 ASSERT_EQ(original_binary
->GetSize(),
436 static_cast<BinaryValue
*>(copy_binary
)->GetSize());
437 ASSERT_EQ(0, memcmp(original_binary
->GetBuffer(),
438 static_cast<BinaryValue
*>(copy_binary
)->GetBuffer(),
439 original_binary
->GetSize()));
441 Value
* copy_value
= NULL
;
442 ASSERT_TRUE(copy_dict
->Get("list", ©_value
));
443 ASSERT_TRUE(copy_value
);
444 ASSERT_NE(copy_value
, original_list
);
445 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_LIST
));
446 ListValue
* copy_list
= NULL
;
447 ASSERT_TRUE(copy_value
->GetAsList(©_list
));
448 ASSERT_TRUE(copy_list
);
449 ASSERT_EQ(2U, copy_list
->GetSize());
451 Value
* copy_list_element_0
;
452 ASSERT_TRUE(copy_list
->Get(0, ©_list_element_0
));
453 ASSERT_TRUE(copy_list_element_0
);
454 ASSERT_NE(copy_list_element_0
, original_list_element_0
);
455 int copy_list_element_0_value
;
456 ASSERT_TRUE(copy_list_element_0
->GetAsInteger(©_list_element_0_value
));
457 ASSERT_EQ(0, copy_list_element_0_value
);
459 Value
* copy_list_element_1
;
460 ASSERT_TRUE(copy_list
->Get(1, ©_list_element_1
));
461 ASSERT_TRUE(copy_list_element_1
);
462 ASSERT_NE(copy_list_element_1
, original_list_element_1
);
463 int copy_list_element_1_value
;
464 ASSERT_TRUE(copy_list_element_1
->GetAsInteger(©_list_element_1_value
));
465 ASSERT_EQ(1, copy_list_element_1_value
);
468 ASSERT_TRUE(copy_dict
->Get("dictionary", ©_value
));
469 ASSERT_TRUE(copy_value
);
470 ASSERT_NE(copy_value
, original_nested_dictionary
);
471 ASSERT_TRUE(copy_value
->IsType(Value::TYPE_DICTIONARY
));
472 DictionaryValue
* copy_nested_dictionary
= NULL
;
473 ASSERT_TRUE(copy_value
->GetAsDictionary(©_nested_dictionary
));
474 ASSERT_TRUE(copy_nested_dictionary
);
475 EXPECT_TRUE(copy_nested_dictionary
->HasKey("key"));
478 TEST(ValuesTest
, Equals
) {
479 Value
* null1
= Value::CreateNullValue();
480 Value
* null2
= Value::CreateNullValue();
481 EXPECT_NE(null1
, null2
);
482 EXPECT_TRUE(null1
->Equals(null2
));
484 Value
* boolean
= Value::CreateBooleanValue(false);
485 EXPECT_FALSE(null1
->Equals(boolean
));
491 dv
.SetBoolean("a", false);
492 dv
.SetInteger("b", 2);
493 dv
.SetDouble("c", 2.5);
494 dv
.SetString("d1", "string");
495 dv
.SetString("d2", ASCIIToUTF16("http://google.com"));
496 dv
.Set("e", Value::CreateNullValue());
498 scoped_ptr
<DictionaryValue
> copy
;
499 copy
.reset(dv
.DeepCopy());
500 EXPECT_TRUE(dv
.Equals(copy
.get()));
502 ListValue
* list
= new ListValue
;
503 list
->Append(Value::CreateNullValue());
504 list
->Append(new DictionaryValue
);
507 EXPECT_FALSE(dv
.Equals(copy
.get()));
508 copy
->Set("f", list
->DeepCopy());
509 EXPECT_TRUE(dv
.Equals(copy
.get()));
511 list
->Append(Value::CreateBooleanValue(true));
512 EXPECT_FALSE(dv
.Equals(copy
.get()));
514 // Check if Equals detects differences in only the keys.
515 copy
.reset(dv
.DeepCopy());
516 EXPECT_TRUE(dv
.Equals(copy
.get()));
517 copy
->Remove("a", NULL
);
518 copy
->SetBoolean("aa", false);
519 EXPECT_FALSE(dv
.Equals(copy
.get()));
522 TEST(ValuesTest
, StaticEquals
) {
523 scoped_ptr
<Value
> null1(Value::CreateNullValue());
524 scoped_ptr
<Value
> null2(Value::CreateNullValue());
525 EXPECT_TRUE(Value::Equals(null1
.get(), null2
.get()));
526 EXPECT_TRUE(Value::Equals(NULL
, NULL
));
528 scoped_ptr
<Value
> i42(Value::CreateIntegerValue(42));
529 scoped_ptr
<Value
> j42(Value::CreateIntegerValue(42));
530 scoped_ptr
<Value
> i17(Value::CreateIntegerValue(17));
531 EXPECT_TRUE(Value::Equals(i42
.get(), i42
.get()));
532 EXPECT_TRUE(Value::Equals(j42
.get(), i42
.get()));
533 EXPECT_TRUE(Value::Equals(i42
.get(), j42
.get()));
534 EXPECT_FALSE(Value::Equals(i42
.get(), i17
.get()));
535 EXPECT_FALSE(Value::Equals(i42
.get(), NULL
));
536 EXPECT_FALSE(Value::Equals(NULL
, i42
.get()));
538 // NULL and Value::CreateNullValue() are intentionally different: We need
539 // support for NULL as a return value for "undefined" without caring for
540 // ownership of the pointer.
541 EXPECT_FALSE(Value::Equals(null1
.get(), NULL
));
542 EXPECT_FALSE(Value::Equals(NULL
, null1
.get()));
545 TEST(ValuesTest
, DeepCopyCovariantReturnTypes
) {
546 DictionaryValue original_dict
;
547 Value
* original_null
= Value::CreateNullValue();
548 original_dict
.Set("null", original_null
);
549 FundamentalValue
* original_bool
= Value::CreateBooleanValue(true);
550 original_dict
.Set("bool", original_bool
);
551 FundamentalValue
* original_int
= Value::CreateIntegerValue(42);
552 original_dict
.Set("int", original_int
);
553 FundamentalValue
* original_double
= Value::CreateDoubleValue(3.14);
554 original_dict
.Set("double", original_double
);
555 StringValue
* original_string
= Value::CreateStringValue("hello");
556 original_dict
.Set("string", original_string
);
557 StringValue
* original_string16
=
558 Value::CreateStringValue(ASCIIToUTF16("hello16"));
559 original_dict
.Set("string16", original_string16
);
561 char* original_buffer
= new char[42];
562 memset(original_buffer
, '!', 42);
563 BinaryValue
* original_binary
= BinaryValue::Create(original_buffer
, 42);
564 original_dict
.Set("binary", original_binary
);
566 ListValue
* original_list
= new ListValue();
567 FundamentalValue
* original_list_element_0
= Value::CreateIntegerValue(0);
568 original_list
->Append(original_list_element_0
);
569 FundamentalValue
* original_list_element_1
= Value::CreateIntegerValue(1);
570 original_list
->Append(original_list_element_1
);
571 original_dict
.Set("list", original_list
);
573 Value
* original_dict_value
= &original_dict
;
574 Value
* original_bool_value
= original_bool
;
575 Value
* original_int_value
= original_int
;
576 Value
* original_double_value
= original_double
;
577 Value
* original_string_value
= original_string
;
578 Value
* original_string16_value
= original_string16
;
579 Value
* original_binary_value
= original_binary
;
580 Value
* original_list_value
= original_list
;
582 scoped_ptr
<Value
> copy_dict_value(original_dict_value
->DeepCopy());
583 scoped_ptr
<Value
> copy_bool_value(original_bool_value
->DeepCopy());
584 scoped_ptr
<Value
> copy_int_value(original_int_value
->DeepCopy());
585 scoped_ptr
<Value
> copy_double_value(original_double_value
->DeepCopy());
586 scoped_ptr
<Value
> copy_string_value(original_string_value
->DeepCopy());
587 scoped_ptr
<Value
> copy_string16_value(original_string16_value
->DeepCopy());
588 scoped_ptr
<Value
> copy_binary_value(original_binary_value
->DeepCopy());
589 scoped_ptr
<Value
> copy_list_value(original_list_value
->DeepCopy());
591 EXPECT_TRUE(original_dict_value
->Equals(copy_dict_value
.get()));
592 EXPECT_TRUE(original_bool_value
->Equals(copy_bool_value
.get()));
593 EXPECT_TRUE(original_int_value
->Equals(copy_int_value
.get()));
594 EXPECT_TRUE(original_double_value
->Equals(copy_double_value
.get()));
595 EXPECT_TRUE(original_string_value
->Equals(copy_string_value
.get()));
596 EXPECT_TRUE(original_string16_value
->Equals(copy_string16_value
.get()));
597 EXPECT_TRUE(original_binary_value
->Equals(copy_binary_value
.get()));
598 EXPECT_TRUE(original_list_value
->Equals(copy_list_value
.get()));
601 TEST(ValuesTest
, RemoveEmptyChildren
) {
602 scoped_ptr
<DictionaryValue
> root(new DictionaryValue
);
603 // Remove empty lists and dictionaries.
604 root
->Set("empty_dict", new DictionaryValue
);
605 root
->Set("empty_list", new ListValue
);
606 root
->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue
);
607 root
.reset(root
->DeepCopyWithoutEmptyChildren());
608 EXPECT_TRUE(root
->empty());
610 // Make sure we don't prune too much.
611 root
->SetBoolean("bool", true);
612 root
->Set("empty_dict", new DictionaryValue
);
613 root
->SetString("empty_string", "");
614 root
.reset(root
->DeepCopyWithoutEmptyChildren());
615 EXPECT_EQ(2U, root
->size());
617 // Should do nothing.
618 root
.reset(root
->DeepCopyWithoutEmptyChildren());
619 EXPECT_EQ(2U, root
->size());
621 // Nested test cases. These should all reduce back to the bool and string
624 root
->Set("a.b.c.d.e", new DictionaryValue
);
625 root
.reset(root
->DeepCopyWithoutEmptyChildren());
626 EXPECT_EQ(2U, root
->size());
629 DictionaryValue
* inner
= new DictionaryValue
;
630 root
->Set("dict_with_emtpy_children", inner
);
631 inner
->Set("empty_dict", new DictionaryValue
);
632 inner
->Set("empty_list", new ListValue
);
633 root
.reset(root
->DeepCopyWithoutEmptyChildren());
634 EXPECT_EQ(2U, root
->size());
637 ListValue
* inner
= new ListValue
;
638 root
->Set("list_with_empty_children", inner
);
639 inner
->Append(new DictionaryValue
);
640 inner
->Append(new ListValue
);
641 root
.reset(root
->DeepCopyWithoutEmptyChildren());
642 EXPECT_EQ(2U, root
->size());
645 // Nested with siblings.
647 ListValue
* inner
= new ListValue
;
648 root
->Set("list_with_empty_children", inner
);
649 inner
->Append(new DictionaryValue
);
650 inner
->Append(new ListValue
);
651 DictionaryValue
* inner2
= new DictionaryValue
;
652 root
->Set("dict_with_empty_children", inner2
);
653 inner2
->Set("empty_dict", new DictionaryValue
);
654 inner2
->Set("empty_list", new ListValue
);
655 root
.reset(root
->DeepCopyWithoutEmptyChildren());
656 EXPECT_EQ(2U, root
->size());
659 // Make sure nested values don't get pruned.
661 ListValue
* inner
= new ListValue
;
662 root
->Set("list_with_empty_children", inner
);
663 ListValue
* inner2
= new ListValue
;
664 inner
->Append(new DictionaryValue
);
665 inner
->Append(inner2
);
666 inner2
->Append(Value::CreateStringValue("hello"));
667 root
.reset(root
->DeepCopyWithoutEmptyChildren());
668 EXPECT_EQ(3U, root
->size());
669 EXPECT_TRUE(root
->GetList("list_with_empty_children", &inner
));
670 EXPECT_EQ(1U, inner
->GetSize()); // Dictionary was pruned.
671 EXPECT_TRUE(inner
->GetList(0, &inner2
));
672 EXPECT_EQ(1U, inner2
->GetSize());
676 TEST(ValuesTest
, MergeDictionary
) {
677 scoped_ptr
<DictionaryValue
> base(new DictionaryValue
);
678 base
->SetString("base_key", "base_key_value_base");
679 base
->SetString("collide_key", "collide_key_value_base");
680 DictionaryValue
* base_sub_dict
= new DictionaryValue
;
681 base_sub_dict
->SetString("sub_base_key", "sub_base_key_value_base");
682 base_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_base");
683 base
->Set("sub_dict_key", base_sub_dict
);
685 scoped_ptr
<DictionaryValue
> merge(new DictionaryValue
);
686 merge
->SetString("merge_key", "merge_key_value_merge");
687 merge
->SetString("collide_key", "collide_key_value_merge");
688 DictionaryValue
* merge_sub_dict
= new DictionaryValue
;
689 merge_sub_dict
->SetString("sub_merge_key", "sub_merge_key_value_merge");
690 merge_sub_dict
->SetString("sub_collide_key", "sub_collide_key_value_merge");
691 merge
->Set("sub_dict_key", merge_sub_dict
);
693 base
->MergeDictionary(merge
.get());
695 EXPECT_EQ(4U, base
->size());
696 std::string base_key_value
;
697 EXPECT_TRUE(base
->GetString("base_key", &base_key_value
));
698 EXPECT_EQ("base_key_value_base", base_key_value
); // Base value preserved.
699 std::string collide_key_value
;
700 EXPECT_TRUE(base
->GetString("collide_key", &collide_key_value
));
701 EXPECT_EQ("collide_key_value_merge", collide_key_value
); // Replaced.
702 std::string merge_key_value
;
703 EXPECT_TRUE(base
->GetString("merge_key", &merge_key_value
));
704 EXPECT_EQ("merge_key_value_merge", merge_key_value
); // Merged in.
706 DictionaryValue
* res_sub_dict
;
707 EXPECT_TRUE(base
->GetDictionary("sub_dict_key", &res_sub_dict
));
708 EXPECT_EQ(3U, res_sub_dict
->size());
709 std::string sub_base_key_value
;
710 EXPECT_TRUE(res_sub_dict
->GetString("sub_base_key", &sub_base_key_value
));
711 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value
); // Preserved.
712 std::string sub_collide_key_value
;
713 EXPECT_TRUE(res_sub_dict
->GetString("sub_collide_key",
714 &sub_collide_key_value
));
715 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value
); // Replaced.
716 std::string sub_merge_key_value
;
717 EXPECT_TRUE(res_sub_dict
->GetString("sub_merge_key", &sub_merge_key_value
));
718 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value
); // Merged in.
721 TEST(ValuesTest
, DictionaryIterator
) {
722 DictionaryValue dict
;
723 for (DictionaryValue::Iterator
it(dict
); it
.HasNext(); it
.Advance()) {
727 StringValue
value1("value1");
728 dict
.Set("key1", value1
.DeepCopy());
730 for (DictionaryValue::Iterator
it(dict
); it
.HasNext(); it
.Advance()) {
732 EXPECT_EQ("key1", it
.key());
733 EXPECT_TRUE(value1
.Equals(&it
.value()));
738 StringValue
value2("value2");
739 dict
.Set("key2", value2
.DeepCopy());
740 bool seen2
= seen1
= false;
741 for (DictionaryValue::Iterator
it(dict
); it
.HasNext(); it
.Advance()) {
742 if (it
.key() == "key1") {
744 EXPECT_TRUE(value1
.Equals(&it
.value()));
746 } else if (it
.key() == "key2") {
748 EXPECT_TRUE(value2
.Equals(&it
.value()));