Move media/base/simd/x86inc.asm to third_party/x86inc
[chromium-blink-merge.git] / base / values_unittest.cc
blob98bc73c9a1b7ee50613ec562cd233a3eca23cccf
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.
5 #include <limits>
7 #include "base/memory/scoped_ptr.h"
8 #include "base/string16.h"
9 #include "base/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace base {
15 TEST(ValuesTest, Basic) {
16 // Test basic dictionary getting/setting
17 DictionaryValue settings;
18 std::string homepage = "http://google.com";
19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
20 ASSERT_EQ(std::string("http://google.com"), homepage);
22 ASSERT_FALSE(settings.Get("global", NULL));
23 settings.Set("global", 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;
33 ASSERT_FALSE(
34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
36 toolbar_bookmarks = new ListValue;
37 settings.Set("global.toolbar.bookmarks", toolbar_bookmarks);
38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
40 DictionaryValue* new_bookmark = new DictionaryValue;
41 new_bookmark->SetString("name", "Froogle");
42 new_bookmark->SetString("url", "http://froogle.com");
43 toolbar_bookmarks->Append(new_bookmark);
45 ListValue* bookmark_list;
46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
47 DictionaryValue* bookmark;
48 ASSERT_EQ(1U, bookmark_list->GetSize());
49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
50 std::string bookmark_name = "Unnamed";
51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
52 ASSERT_EQ(std::string("Froogle"), bookmark_name);
53 std::string bookmark_url;
54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
58 TEST(ValuesTest, List) {
59 scoped_ptr<ListValue> mixed_list(new ListValue());
60 mixed_list->Set(0, 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());
66 Value *value = NULL;
67 bool bool_value = false;
68 int int_value = 0;
69 double double_value = 0.0;
70 std::string string_value;
72 ASSERT_FALSE(mixed_list->Get(4, &value));
74 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
75 ASSERT_EQ(0, int_value);
76 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
77 ASSERT_FALSE(bool_value);
78 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
79 ASSERT_EQ("", string_value);
80 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
81 ASSERT_EQ(0, int_value);
82 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
83 ASSERT_FALSE(bool_value);
85 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
86 ASSERT_TRUE(bool_value);
87 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
88 ASSERT_EQ(42, int_value);
89 // implicit conversion from Integer to Double should be possible.
90 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
91 ASSERT_EQ(42, double_value);
92 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
93 ASSERT_EQ(88.8, double_value);
94 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
95 ASSERT_EQ("foo", string_value);
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 {
158 public:
159 explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) {
160 Init(deletion_flag); // Separate function so that we can use ASSERT_*
163 void Init(bool* deletion_flag) {
164 ASSERT_TRUE(deletion_flag);
165 deletion_flag_ = deletion_flag;
166 *deletion_flag_ = false;
169 virtual ~DeletionTestValue() {
170 *deletion_flag_ = true;
173 private:
174 bool* deletion_flag_;
177 TEST(ValuesTest, ListDeletion) {
178 bool deletion_flag = true;
181 ListValue list;
182 list.Append(new DeletionTestValue(&deletion_flag));
183 EXPECT_FALSE(deletion_flag);
185 EXPECT_TRUE(deletion_flag);
188 ListValue list;
189 list.Append(new DeletionTestValue(&deletion_flag));
190 EXPECT_FALSE(deletion_flag);
191 list.Clear();
192 EXPECT_TRUE(deletion_flag);
196 ListValue list;
197 list.Append(new DeletionTestValue(&deletion_flag));
198 EXPECT_FALSE(deletion_flag);
199 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
200 EXPECT_TRUE(deletion_flag);
204 TEST(ValuesTest, ListRemoval) {
205 bool deletion_flag = true;
206 Value* removed_item = NULL;
209 ListValue list;
210 list.Append(new DeletionTestValue(&deletion_flag));
211 EXPECT_FALSE(deletion_flag);
212 EXPECT_EQ(1U, list.GetSize());
213 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
214 &removed_item));
215 EXPECT_FALSE(list.Remove(1, &removed_item));
216 EXPECT_TRUE(list.Remove(0, &removed_item));
217 ASSERT_TRUE(removed_item);
218 EXPECT_EQ(0U, list.GetSize());
220 EXPECT_FALSE(deletion_flag);
221 delete removed_item;
222 removed_item = NULL;
223 EXPECT_TRUE(deletion_flag);
226 ListValue list;
227 list.Append(new DeletionTestValue(&deletion_flag));
228 EXPECT_FALSE(deletion_flag);
229 EXPECT_TRUE(list.Remove(0, NULL));
230 EXPECT_TRUE(deletion_flag);
231 EXPECT_EQ(0U, list.GetSize());
235 ListValue list;
236 DeletionTestValue* value = new DeletionTestValue(&deletion_flag);
237 list.Append(value);
238 EXPECT_FALSE(deletion_flag);
239 size_t index = 0;
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);
262 dict.Clear();
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);
291 delete removed_item;
292 removed_item = NULL;
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"));
313 Value* value1;
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"));
321 Value* value3;
322 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
323 Value* value4;
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 = new FundamentalValue(true);
333 original_dict.Set("bool", original_bool);
334 FundamentalValue* original_int = new FundamentalValue(42);
335 original_dict.Set("int", original_int);
336 FundamentalValue* original_double = new FundamentalValue(3.14);
337 original_dict.Set("double", original_double);
338 StringValue* original_string = new StringValue("hello");
339 original_dict.Set("string", original_string);
340 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16"));
341 original_dict.Set("string16", original_string16);
343 scoped_ptr<char[]> original_buffer(new char[42]);
344 memset(original_buffer.get(), '!', 42);
345 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42);
346 original_dict.Set("binary", original_binary);
348 ListValue* original_list = new ListValue();
349 FundamentalValue* original_list_element_0 = new FundamentalValue(0);
350 original_list->Append(original_list_element_0);
351 FundamentalValue* original_list_element_1 = new FundamentalValue(1);
352 original_list->Append(original_list_element_1);
353 original_dict.Set("list", original_list);
355 DictionaryValue* original_nested_dictionary = new DictionaryValue();
356 original_nested_dictionary->Set("key", new StringValue("value"));
357 original_dict.Set("dictionary", original_nested_dictionary);
359 scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy());
360 ASSERT_TRUE(copy_dict.get());
361 ASSERT_NE(copy_dict.get(), &original_dict);
363 Value* copy_null = NULL;
364 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
365 ASSERT_TRUE(copy_null);
366 ASSERT_NE(copy_null, original_null);
367 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
369 Value* copy_bool = NULL;
370 ASSERT_TRUE(copy_dict->Get("bool", &copy_bool));
371 ASSERT_TRUE(copy_bool);
372 ASSERT_NE(copy_bool, original_bool);
373 ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
374 bool copy_bool_value = false;
375 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
376 ASSERT_TRUE(copy_bool_value);
378 Value* copy_int = NULL;
379 ASSERT_TRUE(copy_dict->Get("int", &copy_int));
380 ASSERT_TRUE(copy_int);
381 ASSERT_NE(copy_int, original_int);
382 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
383 int copy_int_value = 0;
384 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
385 ASSERT_EQ(42, copy_int_value);
387 Value* copy_double = NULL;
388 ASSERT_TRUE(copy_dict->Get("double", &copy_double));
389 ASSERT_TRUE(copy_double);
390 ASSERT_NE(copy_double, original_double);
391 ASSERT_TRUE(copy_double->IsType(Value::TYPE_DOUBLE));
392 double copy_double_value = 0;
393 ASSERT_TRUE(copy_double->GetAsDouble(&copy_double_value));
394 ASSERT_EQ(3.14, copy_double_value);
396 Value* copy_string = NULL;
397 ASSERT_TRUE(copy_dict->Get("string", &copy_string));
398 ASSERT_TRUE(copy_string);
399 ASSERT_NE(copy_string, original_string);
400 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
401 std::string copy_string_value;
402 string16 copy_string16_value;
403 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
404 ASSERT_TRUE(copy_string->GetAsString(&copy_string16_value));
405 ASSERT_EQ(std::string("hello"), copy_string_value);
406 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
408 Value* copy_string16 = NULL;
409 ASSERT_TRUE(copy_dict->Get("string16", &copy_string16));
410 ASSERT_TRUE(copy_string16);
411 ASSERT_NE(copy_string16, original_string16);
412 ASSERT_TRUE(copy_string16->IsType(Value::TYPE_STRING));
413 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value));
414 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value));
415 ASSERT_EQ(std::string("hello16"), copy_string_value);
416 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
418 Value* copy_binary = NULL;
419 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
420 ASSERT_TRUE(copy_binary);
421 ASSERT_NE(copy_binary, original_binary);
422 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
423 ASSERT_NE(original_binary->GetBuffer(),
424 static_cast<BinaryValue*>(copy_binary)->GetBuffer());
425 ASSERT_EQ(original_binary->GetSize(),
426 static_cast<BinaryValue*>(copy_binary)->GetSize());
427 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
428 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
429 original_binary->GetSize()));
431 Value* copy_value = NULL;
432 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
433 ASSERT_TRUE(copy_value);
434 ASSERT_NE(copy_value, original_list);
435 ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
436 ListValue* copy_list = NULL;
437 ASSERT_TRUE(copy_value->GetAsList(&copy_list));
438 ASSERT_TRUE(copy_list);
439 ASSERT_EQ(2U, copy_list->GetSize());
441 Value* copy_list_element_0;
442 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
443 ASSERT_TRUE(copy_list_element_0);
444 ASSERT_NE(copy_list_element_0, original_list_element_0);
445 int copy_list_element_0_value;
446 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
447 ASSERT_EQ(0, copy_list_element_0_value);
449 Value* copy_list_element_1;
450 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
451 ASSERT_TRUE(copy_list_element_1);
452 ASSERT_NE(copy_list_element_1, original_list_element_1);
453 int copy_list_element_1_value;
454 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
455 ASSERT_EQ(1, copy_list_element_1_value);
457 copy_value = NULL;
458 ASSERT_TRUE(copy_dict->Get("dictionary", &copy_value));
459 ASSERT_TRUE(copy_value);
460 ASSERT_NE(copy_value, original_nested_dictionary);
461 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY));
462 DictionaryValue* copy_nested_dictionary = NULL;
463 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
464 ASSERT_TRUE(copy_nested_dictionary);
465 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
468 TEST(ValuesTest, Equals) {
469 Value* null1 = Value::CreateNullValue();
470 Value* null2 = Value::CreateNullValue();
471 EXPECT_NE(null1, null2);
472 EXPECT_TRUE(null1->Equals(null2));
474 Value* boolean = new FundamentalValue(false);
475 EXPECT_FALSE(null1->Equals(boolean));
476 delete null1;
477 delete null2;
478 delete boolean;
480 DictionaryValue dv;
481 dv.SetBoolean("a", false);
482 dv.SetInteger("b", 2);
483 dv.SetDouble("c", 2.5);
484 dv.SetString("d1", "string");
485 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
486 dv.Set("e", Value::CreateNullValue());
488 scoped_ptr<DictionaryValue> copy;
489 copy.reset(dv.DeepCopy());
490 EXPECT_TRUE(dv.Equals(copy.get()));
492 ListValue* list = new ListValue;
493 list->Append(Value::CreateNullValue());
494 list->Append(new DictionaryValue);
495 dv.Set("f", list);
497 EXPECT_FALSE(dv.Equals(copy.get()));
498 copy->Set("f", list->DeepCopy());
499 EXPECT_TRUE(dv.Equals(copy.get()));
501 list->Append(new FundamentalValue(true));
502 EXPECT_FALSE(dv.Equals(copy.get()));
504 // Check if Equals detects differences in only the keys.
505 copy.reset(dv.DeepCopy());
506 EXPECT_TRUE(dv.Equals(copy.get()));
507 copy->Remove("a", NULL);
508 copy->SetBoolean("aa", false);
509 EXPECT_FALSE(dv.Equals(copy.get()));
512 TEST(ValuesTest, StaticEquals) {
513 scoped_ptr<Value> null1(Value::CreateNullValue());
514 scoped_ptr<Value> null2(Value::CreateNullValue());
515 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
516 EXPECT_TRUE(Value::Equals(NULL, NULL));
518 scoped_ptr<Value> i42(new FundamentalValue(42));
519 scoped_ptr<Value> j42(new FundamentalValue(42));
520 scoped_ptr<Value> i17(new FundamentalValue(17));
521 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
522 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
523 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
524 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
525 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
526 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
528 // NULL and Value::CreateNullValue() are intentionally different: We need
529 // support for NULL as a return value for "undefined" without caring for
530 // ownership of the pointer.
531 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
532 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
535 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
536 DictionaryValue original_dict;
537 Value* original_null = Value::CreateNullValue();
538 original_dict.Set("null", original_null);
539 FundamentalValue* original_bool = new FundamentalValue(true);
540 original_dict.Set("bool", original_bool);
541 FundamentalValue* original_int = new FundamentalValue(42);
542 original_dict.Set("int", original_int);
543 FundamentalValue* original_double = new FundamentalValue(3.14);
544 original_dict.Set("double", original_double);
545 StringValue* original_string = new StringValue("hello");
546 original_dict.Set("string", original_string);
547 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16"));
548 original_dict.Set("string16", original_string16);
550 scoped_ptr<char[]> original_buffer(new char[42]);
551 memset(original_buffer.get(), '!', 42);
552 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42);
553 original_dict.Set("binary", original_binary);
555 ListValue* original_list = new ListValue();
556 FundamentalValue* original_list_element_0 = new FundamentalValue(0);
557 original_list->Append(original_list_element_0);
558 FundamentalValue* original_list_element_1 = new FundamentalValue(1);
559 original_list->Append(original_list_element_1);
560 original_dict.Set("list", original_list);
562 Value* original_dict_value = &original_dict;
563 Value* original_bool_value = original_bool;
564 Value* original_int_value = original_int;
565 Value* original_double_value = original_double;
566 Value* original_string_value = original_string;
567 Value* original_string16_value = original_string16;
568 Value* original_binary_value = original_binary;
569 Value* original_list_value = original_list;
571 scoped_ptr<Value> copy_dict_value(original_dict_value->DeepCopy());
572 scoped_ptr<Value> copy_bool_value(original_bool_value->DeepCopy());
573 scoped_ptr<Value> copy_int_value(original_int_value->DeepCopy());
574 scoped_ptr<Value> copy_double_value(original_double_value->DeepCopy());
575 scoped_ptr<Value> copy_string_value(original_string_value->DeepCopy());
576 scoped_ptr<Value> copy_string16_value(original_string16_value->DeepCopy());
577 scoped_ptr<Value> copy_binary_value(original_binary_value->DeepCopy());
578 scoped_ptr<Value> copy_list_value(original_list_value->DeepCopy());
580 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get()));
581 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get()));
582 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get()));
583 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get()));
584 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get()));
585 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get()));
586 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get()));
587 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get()));
590 TEST(ValuesTest, RemoveEmptyChildren) {
591 scoped_ptr<DictionaryValue> root(new DictionaryValue);
592 // Remove empty lists and dictionaries.
593 root->Set("empty_dict", new DictionaryValue);
594 root->Set("empty_list", new ListValue);
595 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
596 root.reset(root->DeepCopyWithoutEmptyChildren());
597 EXPECT_TRUE(root->empty());
599 // Make sure we don't prune too much.
600 root->SetBoolean("bool", true);
601 root->Set("empty_dict", new DictionaryValue);
602 root->SetString("empty_string", "");
603 root.reset(root->DeepCopyWithoutEmptyChildren());
604 EXPECT_EQ(2U, root->size());
606 // Should do nothing.
607 root.reset(root->DeepCopyWithoutEmptyChildren());
608 EXPECT_EQ(2U, root->size());
610 // Nested test cases. These should all reduce back to the bool and string
611 // set above.
613 root->Set("a.b.c.d.e", new DictionaryValue);
614 root.reset(root->DeepCopyWithoutEmptyChildren());
615 EXPECT_EQ(2U, root->size());
618 DictionaryValue* inner = new DictionaryValue;
619 root->Set("dict_with_emtpy_children", inner);
620 inner->Set("empty_dict", new DictionaryValue);
621 inner->Set("empty_list", new ListValue);
622 root.reset(root->DeepCopyWithoutEmptyChildren());
623 EXPECT_EQ(2U, root->size());
626 ListValue* inner = new ListValue;
627 root->Set("list_with_empty_children", inner);
628 inner->Append(new DictionaryValue);
629 inner->Append(new ListValue);
630 root.reset(root->DeepCopyWithoutEmptyChildren());
631 EXPECT_EQ(2U, root->size());
634 // Nested with siblings.
636 ListValue* inner = new ListValue;
637 root->Set("list_with_empty_children", inner);
638 inner->Append(new DictionaryValue);
639 inner->Append(new ListValue);
640 DictionaryValue* inner2 = new DictionaryValue;
641 root->Set("dict_with_empty_children", inner2);
642 inner2->Set("empty_dict", new DictionaryValue);
643 inner2->Set("empty_list", new ListValue);
644 root.reset(root->DeepCopyWithoutEmptyChildren());
645 EXPECT_EQ(2U, root->size());
648 // Make sure nested values don't get pruned.
650 ListValue* inner = new ListValue;
651 root->Set("list_with_empty_children", inner);
652 ListValue* inner2 = new ListValue;
653 inner->Append(new DictionaryValue);
654 inner->Append(inner2);
655 inner2->Append(new StringValue("hello"));
656 root.reset(root->DeepCopyWithoutEmptyChildren());
657 EXPECT_EQ(3U, root->size());
658 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner));
659 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
660 EXPECT_TRUE(inner->GetList(0, &inner2));
661 EXPECT_EQ(1U, inner2->GetSize());
665 TEST(ValuesTest, MergeDictionary) {
666 scoped_ptr<DictionaryValue> base(new DictionaryValue);
667 base->SetString("base_key", "base_key_value_base");
668 base->SetString("collide_key", "collide_key_value_base");
669 DictionaryValue* base_sub_dict = new DictionaryValue;
670 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
671 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
672 base->Set("sub_dict_key", base_sub_dict);
674 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
675 merge->SetString("merge_key", "merge_key_value_merge");
676 merge->SetString("collide_key", "collide_key_value_merge");
677 DictionaryValue* merge_sub_dict = new DictionaryValue;
678 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
679 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
680 merge->Set("sub_dict_key", merge_sub_dict);
682 base->MergeDictionary(merge.get());
684 EXPECT_EQ(4U, base->size());
685 std::string base_key_value;
686 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
687 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
688 std::string collide_key_value;
689 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
690 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
691 std::string merge_key_value;
692 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
693 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
695 DictionaryValue* res_sub_dict;
696 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
697 EXPECT_EQ(3U, res_sub_dict->size());
698 std::string sub_base_key_value;
699 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
700 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
701 std::string sub_collide_key_value;
702 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
703 &sub_collide_key_value));
704 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
705 std::string sub_merge_key_value;
706 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
707 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
710 TEST(ValuesTest, MergeDictionaryDeepCopy) {
711 DictionaryValue* child = new DictionaryValue;
712 child->SetString("test", "value");
713 EXPECT_EQ(1U, child->size());
715 std::string value;
716 EXPECT_TRUE(child->GetString("test", &value));
717 EXPECT_EQ("value", value);
719 scoped_ptr<DictionaryValue> base(new DictionaryValue);
720 base->Set("dict", child);
721 EXPECT_EQ(1U, base->size());
723 DictionaryValue* ptr;
724 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
725 EXPECT_EQ(child, ptr);
727 scoped_ptr<DictionaryValue> merged(new DictionaryValue);
728 merged->MergeDictionary(base.get());
729 EXPECT_EQ(1U, merged->size());
730 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
731 EXPECT_NE(child, ptr);
732 EXPECT_TRUE(ptr->GetString("test", &value));
733 EXPECT_EQ("value", value);
735 child->SetString("test", "overwrite");
736 base.reset();
737 EXPECT_TRUE(ptr->GetString("test", &value));
738 EXPECT_EQ("value", value);
741 TEST(ValuesTest, DictionaryIterator) {
742 DictionaryValue dict;
743 for (DictionaryValue::Iterator it(dict); it.HasNext(); it.Advance()) {
744 ADD_FAILURE();
747 StringValue value1("value1");
748 dict.Set("key1", value1.DeepCopy());
749 bool seen1 = false;
750 for (DictionaryValue::Iterator it(dict); it.HasNext(); it.Advance()) {
751 EXPECT_FALSE(seen1);
752 EXPECT_EQ("key1", it.key());
753 EXPECT_TRUE(value1.Equals(&it.value()));
754 seen1 = true;
756 EXPECT_TRUE(seen1);
758 StringValue value2("value2");
759 dict.Set("key2", value2.DeepCopy());
760 bool seen2 = seen1 = false;
761 for (DictionaryValue::Iterator it(dict); it.HasNext(); it.Advance()) {
762 if (it.key() == "key1") {
763 EXPECT_FALSE(seen1);
764 EXPECT_TRUE(value1.Equals(&it.value()));
765 seen1 = true;
766 } else if (it.key() == "key2") {
767 EXPECT_FALSE(seen2);
768 EXPECT_TRUE(value2.Equals(&it.value()));
769 seen2 = true;
770 } else {
771 ADD_FAILURE();
774 EXPECT_TRUE(seen1);
775 EXPECT_TRUE(seen2);
778 } // namespace base