Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: base/values_unittest.cc

Issue 3117017: Remove deprecated wstring Get(As)String() methods from Value, etc. (Closed)
Patch Set: fix win Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/values.cc ('k') | chrome/browser/autocomplete/search_provider.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits> 5 #include <limits>
6 6
7 #include "base/scoped_ptr.h" 7 #include "base/scoped_ptr.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/string16.h" 9 #include "base/string16.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
(...skipping 16 matching lines...) Expand all
27 ASSERT_EQ(expected_paths_count, differing_paths.size()); 27 ASSERT_EQ(expected_paths_count, differing_paths.size());
28 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(), 28 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(),
29 expected_paths_vector.begin())); 29 expected_paths_vector.begin()));
30 dict2->GetDifferingPaths(dict1, &differing_paths); 30 dict2->GetDifferingPaths(dict1, &differing_paths);
31 ASSERT_EQ(expected_paths_count, differing_paths.size()); 31 ASSERT_EQ(expected_paths_count, differing_paths.size());
32 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(), 32 EXPECT_TRUE(equal(differing_paths.begin(), differing_paths.end(),
33 expected_paths_vector.begin())); 33 expected_paths_vector.begin()));
34 } 34 }
35 }; 35 };
36 36
37 // TODO(viettrungluu): I changed the keys for DictionaryValue from std::wstring
38 // to std::string. I've temporarily kept the old methods taking std::wstring for
39 // compatibility. The ...Deprecated tests are the old tests which use these
40 // methods, and remain to test compatibility. They will be removed once the old
41 // methods are removed.
42
43 TEST_F(ValuesTest, Basic) { 37 TEST_F(ValuesTest, Basic) {
44 // Test basic dictionary getting/setting 38 // Test basic dictionary getting/setting
45 DictionaryValue settings; 39 DictionaryValue settings;
46 std::string homepage = "http://google.com"; 40 std::string homepage = "http://google.com";
47 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); 41 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
48 ASSERT_EQ(std::string("http://google.com"), homepage); 42 ASSERT_EQ(std::string("http://google.com"), homepage);
49 43
50 ASSERT_FALSE(settings.Get("global", NULL)); 44 ASSERT_FALSE(settings.Get("global", NULL));
51 settings.Set("global", Value::CreateBooleanValue(true)); 45 settings.Set("global", Value::CreateBooleanValue(true));
52 ASSERT_TRUE(settings.Get("global", NULL)); 46 ASSERT_TRUE(settings.Get("global", NULL));
(...skipping 23 matching lines...) Expand all
76 ASSERT_EQ(1U, bookmark_list->GetSize()); 70 ASSERT_EQ(1U, bookmark_list->GetSize());
77 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark)); 71 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
78 std::string bookmark_name = "Unnamed"; 72 std::string bookmark_name = "Unnamed";
79 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 73 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
80 ASSERT_EQ(std::string("Froogle"), bookmark_name); 74 ASSERT_EQ(std::string("Froogle"), bookmark_name);
81 std::string bookmark_url; 75 std::string bookmark_url;
82 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); 76 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
83 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); 77 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
84 } 78 }
85 79
86 // TODO(viettrungluu): deprecate:
87 TEST_F(ValuesTest, BasicDeprecated) {
88 // Test basic dictionary getting/setting
89 DictionaryValue settings;
90 std::wstring homepage = L"http://google.com";
91 ASSERT_FALSE(
92 settings.GetString(L"global.homepage", &homepage));
93 ASSERT_EQ(std::wstring(L"http://google.com"), homepage);
94
95 ASSERT_FALSE(settings.Get(L"global", NULL));
96 settings.Set(L"global", Value::CreateBooleanValue(true));
97 ASSERT_TRUE(settings.Get(L"global", NULL));
98 settings.SetString(L"global.homepage", L"http://scurvy.com");
99 ASSERT_TRUE(settings.Get(L"global", NULL));
100 homepage = L"http://google.com";
101 ASSERT_TRUE(settings.GetString(L"global.homepage", &homepage));
102 ASSERT_EQ(std::wstring(L"http://scurvy.com"), homepage);
103
104 // Test storing a dictionary in a list.
105 ListValue* toolbar_bookmarks;
106 ASSERT_FALSE(
107 settings.GetList(L"global.toolbar.bookmarks", &toolbar_bookmarks));
108
109 toolbar_bookmarks = new ListValue;
110 settings.Set(L"global.toolbar.bookmarks", toolbar_bookmarks);
111 ASSERT_TRUE(
112 settings.GetList(L"global.toolbar.bookmarks", &toolbar_bookmarks));
113
114 DictionaryValue* new_bookmark = new DictionaryValue;
115 new_bookmark->SetString(L"name", L"Froogle");
116 new_bookmark->SetString(L"url", L"http://froogle.com");
117 toolbar_bookmarks->Append(new_bookmark);
118
119 ListValue* bookmark_list;
120 ASSERT_TRUE(settings.GetList(L"global.toolbar.bookmarks", &bookmark_list));
121 DictionaryValue* bookmark;
122 ASSERT_EQ(1U, bookmark_list->GetSize());
123 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
124 std::wstring bookmark_name = L"Unnamed";
125 ASSERT_TRUE(bookmark->GetString(L"name", &bookmark_name));
126 ASSERT_EQ(std::wstring(L"Froogle"), bookmark_name);
127 std::wstring bookmark_url;
128 ASSERT_TRUE(bookmark->GetString(L"url", &bookmark_url));
129 ASSERT_EQ(std::wstring(L"http://froogle.com"), bookmark_url);
130 }
131
132 TEST_F(ValuesTest, List) { 80 TEST_F(ValuesTest, List) {
133 scoped_ptr<ListValue> mixed_list(new ListValue()); 81 scoped_ptr<ListValue> mixed_list(new ListValue());
134 mixed_list->Set(0, Value::CreateBooleanValue(true)); 82 mixed_list->Set(0, Value::CreateBooleanValue(true));
135 mixed_list->Set(1, Value::CreateIntegerValue(42)); 83 mixed_list->Set(1, Value::CreateIntegerValue(42));
136 mixed_list->Set(2, Value::CreateRealValue(88.8)); 84 mixed_list->Set(2, Value::CreateRealValue(88.8));
137 mixed_list->Set(3, Value::CreateStringValue("foo")); 85 mixed_list->Set(3, Value::CreateStringValue("foo"));
138 ASSERT_EQ(4u, mixed_list->GetSize()); 86 ASSERT_EQ(4u, mixed_list->GetSize());
139 87
140 Value *value = NULL; 88 Value *value = NULL;
141 bool bool_value = false; 89 bool bool_value = false;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 ASSERT_TRUE(narrow_value->GetAsString(&utf16)); 162 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
215 ASSERT_EQ(std::string("narrow"), narrow); 163 ASSERT_EQ(std::string("narrow"), narrow);
216 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16); 164 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
217 165
218 ASSERT_TRUE(utf16_value->GetAsString(&narrow)); 166 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
219 ASSERT_TRUE(utf16_value->GetAsString(&utf16)); 167 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
220 ASSERT_EQ(std::string("utf16"), narrow); 168 ASSERT_EQ(std::string("utf16"), narrow);
221 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16); 169 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
222 } 170 }
223 171
224 // TODO(viettrungluu): deprecate:
225 TEST_F(ValuesTest, StringValueDeprecated) {
226 // Test overloaded CreateStringValue.
227 scoped_ptr<Value> narrow_value(Value::CreateStringValue("narrow"));
228 ASSERT_TRUE(narrow_value.get());
229 ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING));
230 scoped_ptr<Value> utf16_value(
231 Value::CreateStringValue(ASCIIToUTF16("utf16")));
232 ASSERT_TRUE(utf16_value.get());
233 ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING));
234
235 // Test overloaded GetString.
236 std::string narrow = "http://google.com";
237 std::wstring wide = L"http://google.com";
238 string16 utf16 = ASCIIToUTF16("http://google.com");
239 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
240 ASSERT_TRUE(narrow_value->GetAsString(&wide));
241 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
242 ASSERT_EQ(std::string("narrow"), narrow);
243 ASSERT_EQ(std::wstring(L"narrow"), wide);
244 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
245
246 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
247 ASSERT_TRUE(utf16_value->GetAsString(&wide));
248 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
249 ASSERT_EQ(std::string("utf16"), narrow);
250 ASSERT_EQ(std::wstring(L"utf16"), wide);
251 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
252 }
253
254 // This is a Value object that allows us to tell if it's been 172 // This is a Value object that allows us to tell if it's been
255 // properly deleted by modifying the value of external flag on destruction. 173 // properly deleted by modifying the value of external flag on destruction.
256 class DeletionTestValue : public Value { 174 class DeletionTestValue : public Value {
257 public: 175 public:
258 explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) { 176 explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) {
259 Init(deletion_flag); // Separate function so that we can use ASSERT_* 177 Init(deletion_flag); // Separate function so that we can use ASSERT_*
260 } 178 }
261 179
262 void Init(bool* deletion_flag) { 180 void Init(bool* deletion_flag) {
263 ASSERT_TRUE(deletion_flag); 181 ASSERT_TRUE(deletion_flag);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 280
363 { 281 {
364 DictionaryValue dict; 282 DictionaryValue dict;
365 dict.Set(key, new DeletionTestValue(&deletion_flag)); 283 dict.Set(key, new DeletionTestValue(&deletion_flag));
366 EXPECT_FALSE(deletion_flag); 284 EXPECT_FALSE(deletion_flag);
367 dict.Set(key, Value::CreateNullValue()); 285 dict.Set(key, Value::CreateNullValue());
368 EXPECT_TRUE(deletion_flag); 286 EXPECT_TRUE(deletion_flag);
369 } 287 }
370 } 288 }
371 289
372 // TODO(viettrungluu): deprecate:
373 TEST_F(ValuesTest, DictionaryDeletionDeprecated) {
374 std::wstring key = L"test";
375 bool deletion_flag = true;
376
377 {
378 DictionaryValue dict;
379 dict.Set(key, new DeletionTestValue(&deletion_flag));
380 EXPECT_FALSE(deletion_flag);
381 }
382 EXPECT_TRUE(deletion_flag);
383
384 {
385 DictionaryValue dict;
386 dict.Set(key, new DeletionTestValue(&deletion_flag));
387 EXPECT_FALSE(deletion_flag);
388 dict.Clear();
389 EXPECT_TRUE(deletion_flag);
390 }
391
392 {
393 DictionaryValue dict;
394 dict.Set(key, new DeletionTestValue(&deletion_flag));
395 EXPECT_FALSE(deletion_flag);
396 dict.Set(key, Value::CreateNullValue());
397 EXPECT_TRUE(deletion_flag);
398 }
399 }
400
401 TEST_F(ValuesTest, DictionaryRemoval) { 290 TEST_F(ValuesTest, DictionaryRemoval) {
402 std::string key = "test"; 291 std::string key = "test";
403 bool deletion_flag = true; 292 bool deletion_flag = true;
404 Value* removed_item = NULL; 293 Value* removed_item = NULL;
405 294
406 { 295 {
407 DictionaryValue dict; 296 DictionaryValue dict;
408 dict.Set(key, new DeletionTestValue(&deletion_flag)); 297 dict.Set(key, new DeletionTestValue(&deletion_flag));
409 EXPECT_FALSE(deletion_flag); 298 EXPECT_FALSE(deletion_flag);
410 EXPECT_TRUE(dict.HasKey(key)); 299 EXPECT_TRUE(dict.HasKey(key));
(...skipping 11 matching lines...) Expand all
422 DictionaryValue dict; 311 DictionaryValue dict;
423 dict.Set(key, new DeletionTestValue(&deletion_flag)); 312 dict.Set(key, new DeletionTestValue(&deletion_flag));
424 EXPECT_FALSE(deletion_flag); 313 EXPECT_FALSE(deletion_flag);
425 EXPECT_TRUE(dict.HasKey(key)); 314 EXPECT_TRUE(dict.HasKey(key));
426 EXPECT_TRUE(dict.Remove(key, NULL)); 315 EXPECT_TRUE(dict.Remove(key, NULL));
427 EXPECT_TRUE(deletion_flag); 316 EXPECT_TRUE(deletion_flag);
428 EXPECT_FALSE(dict.HasKey(key)); 317 EXPECT_FALSE(dict.HasKey(key));
429 } 318 }
430 } 319 }
431 320
432 // TODO(viettrungluu): deprecate:
433 TEST_F(ValuesTest, DictionaryRemovalDeprecated) {
434 std::wstring key = L"test";
435 bool deletion_flag = true;
436 Value* removed_item = NULL;
437
438 {
439 DictionaryValue dict;
440 dict.Set(key, new DeletionTestValue(&deletion_flag));
441 EXPECT_FALSE(deletion_flag);
442 EXPECT_TRUE(dict.HasKey(key));
443 EXPECT_FALSE(dict.Remove(L"absent key", &removed_item));
444 EXPECT_TRUE(dict.Remove(key, &removed_item));
445 EXPECT_FALSE(dict.HasKey(key));
446 ASSERT_TRUE(removed_item);
447 }
448 EXPECT_FALSE(deletion_flag);
449 delete removed_item;
450 removed_item = NULL;
451 EXPECT_TRUE(deletion_flag);
452
453 {
454 DictionaryValue dict;
455 dict.Set(key, new DeletionTestValue(&deletion_flag));
456 EXPECT_FALSE(deletion_flag);
457 EXPECT_TRUE(dict.HasKey(key));
458 EXPECT_TRUE(dict.Remove(key, NULL));
459 EXPECT_TRUE(deletion_flag);
460 EXPECT_FALSE(dict.HasKey(key));
461 }
462 }
463
464 TEST_F(ValuesTest, DictionaryWithoutPathExpansion) { 321 TEST_F(ValuesTest, DictionaryWithoutPathExpansion) {
465 DictionaryValue dict; 322 DictionaryValue dict;
466 dict.Set("this.is.expanded", Value::CreateNullValue()); 323 dict.Set("this.is.expanded", Value::CreateNullValue());
467 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); 324 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
468 325
469 EXPECT_FALSE(dict.HasKey("this.is.expanded")); 326 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
470 EXPECT_TRUE(dict.HasKey("this")); 327 EXPECT_TRUE(dict.HasKey("this"));
471 Value* value1; 328 Value* value1;
472 EXPECT_TRUE(dict.Get("this", &value1)); 329 EXPECT_TRUE(dict.Get("this", &value1));
473 DictionaryValue* value2; 330 DictionaryValue* value2;
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 458
602 Value* copy_list_element_1; 459 Value* copy_list_element_1;
603 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1)); 460 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
604 ASSERT_TRUE(copy_list_element_1); 461 ASSERT_TRUE(copy_list_element_1);
605 ASSERT_NE(copy_list_element_1, original_list_element_1); 462 ASSERT_NE(copy_list_element_1, original_list_element_1);
606 int copy_list_element_1_value; 463 int copy_list_element_1_value;
607 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value)); 464 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
608 ASSERT_EQ(1, copy_list_element_1_value); 465 ASSERT_EQ(1, copy_list_element_1_value);
609 } 466 }
610 467
611 // TODO(viettrungluu): deprecate:
612 TEST_F(ValuesTest, DeepCopyDeprecated) {
613 DictionaryValue original_dict;
614 Value* original_null = Value::CreateNullValue();
615 original_dict.Set(L"null", original_null);
616 Value* original_bool = Value::CreateBooleanValue(true);
617 original_dict.Set(L"bool", original_bool);
618 Value* original_int = Value::CreateIntegerValue(42);
619 original_dict.Set(L"int", original_int);
620 Value* original_real = Value::CreateRealValue(3.14);
621 original_dict.Set(L"real", original_real);
622 Value* original_string = Value::CreateStringValue("hello");
623 original_dict.Set(L"string", original_string);
624 Value* original_utf16 = Value::CreateStringValue(ASCIIToUTF16("hello16"));
625 original_dict.Set(L"utf16", original_utf16);
626
627 char* original_buffer = new char[42];
628 memset(original_buffer, '!', 42);
629 BinaryValue* original_binary = Value::CreateBinaryValue(original_buffer, 42);
630 original_dict.Set(L"binary", original_binary);
631
632 ListValue* original_list = new ListValue();
633 Value* original_list_element_0 = Value::CreateIntegerValue(0);
634 original_list->Append(original_list_element_0);
635 Value* original_list_element_1 = Value::CreateIntegerValue(1);
636 original_list->Append(original_list_element_1);
637 original_dict.Set(L"list", original_list);
638
639 scoped_ptr<DictionaryValue> copy_dict(
640 static_cast<DictionaryValue*>(original_dict.DeepCopy()));
641 ASSERT_TRUE(copy_dict.get());
642 ASSERT_NE(copy_dict.get(), &original_dict);
643
644 Value* copy_null = NULL;
645 ASSERT_TRUE(copy_dict->Get(L"null", &copy_null));
646 ASSERT_TRUE(copy_null);
647 ASSERT_NE(copy_null, original_null);
648 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
649
650 Value* copy_bool = NULL;
651 ASSERT_TRUE(copy_dict->Get(L"bool", &copy_bool));
652 ASSERT_TRUE(copy_bool);
653 ASSERT_NE(copy_bool, original_bool);
654 ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
655 bool copy_bool_value = false;
656 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
657 ASSERT_TRUE(copy_bool_value);
658
659 Value* copy_int = NULL;
660 ASSERT_TRUE(copy_dict->Get(L"int", &copy_int));
661 ASSERT_TRUE(copy_int);
662 ASSERT_NE(copy_int, original_int);
663 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
664 int copy_int_value = 0;
665 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
666 ASSERT_EQ(42, copy_int_value);
667
668 Value* copy_real = NULL;
669 ASSERT_TRUE(copy_dict->Get(L"real", &copy_real));
670 ASSERT_TRUE(copy_real);
671 ASSERT_NE(copy_real, original_real);
672 ASSERT_TRUE(copy_real->IsType(Value::TYPE_REAL));
673 double copy_real_value = 0;
674 ASSERT_TRUE(copy_real->GetAsReal(&copy_real_value));
675 ASSERT_EQ(3.14, copy_real_value);
676
677 Value* copy_string = NULL;
678 ASSERT_TRUE(copy_dict->Get(L"string", &copy_string));
679 ASSERT_TRUE(copy_string);
680 ASSERT_NE(copy_string, original_string);
681 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
682 std::string copy_string_value;
683 std::wstring copy_wstring_value;
684 string16 copy_utf16_value;
685 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
686 ASSERT_TRUE(copy_string->GetAsString(&copy_wstring_value));
687 ASSERT_TRUE(copy_string->GetAsString(&copy_utf16_value));
688 ASSERT_EQ(std::string("hello"), copy_string_value);
689 ASSERT_EQ(std::wstring(L"hello"), copy_wstring_value);
690 ASSERT_EQ(ASCIIToUTF16("hello"), copy_utf16_value);
691
692 Value* copy_utf16 = NULL;
693 ASSERT_TRUE(copy_dict->Get(L"utf16", &copy_utf16));
694 ASSERT_TRUE(copy_utf16);
695 ASSERT_NE(copy_utf16, original_utf16);
696 ASSERT_TRUE(copy_utf16->IsType(Value::TYPE_STRING));
697 ASSERT_TRUE(copy_utf16->GetAsString(&copy_string_value));
698 ASSERT_TRUE(copy_utf16->GetAsString(&copy_wstring_value));
699 ASSERT_TRUE(copy_utf16->GetAsString(&copy_utf16_value));
700 ASSERT_EQ(std::string("hello16"), copy_string_value);
701 ASSERT_EQ(std::wstring(L"hello16"), copy_wstring_value);
702 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_utf16_value);
703
704 Value* copy_binary = NULL;
705 ASSERT_TRUE(copy_dict->Get(L"binary", &copy_binary));
706 ASSERT_TRUE(copy_binary);
707 ASSERT_NE(copy_binary, original_binary);
708 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
709 ASSERT_NE(original_binary->GetBuffer(),
710 static_cast<BinaryValue*>(copy_binary)->GetBuffer());
711 ASSERT_EQ(original_binary->GetSize(),
712 static_cast<BinaryValue*>(copy_binary)->GetSize());
713 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
714 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
715 original_binary->GetSize()));
716
717 Value* copy_value = NULL;
718 ASSERT_TRUE(copy_dict->Get(L"list", &copy_value));
719 ASSERT_TRUE(copy_value);
720 ASSERT_NE(copy_value, original_list);
721 ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
722 ListValue* copy_list = static_cast<ListValue*>(copy_value);
723 ASSERT_EQ(2U, copy_list->GetSize());
724
725 Value* copy_list_element_0;
726 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
727 ASSERT_TRUE(copy_list_element_0);
728 ASSERT_NE(copy_list_element_0, original_list_element_0);
729 int copy_list_element_0_value;
730 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
731 ASSERT_EQ(0, copy_list_element_0_value);
732
733 Value* copy_list_element_1;
734 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
735 ASSERT_TRUE(copy_list_element_1);
736 ASSERT_NE(copy_list_element_1, original_list_element_1);
737 int copy_list_element_1_value;
738 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
739 ASSERT_EQ(1, copy_list_element_1_value);
740 }
741
742 TEST_F(ValuesTest, Equals) { 468 TEST_F(ValuesTest, Equals) {
743 Value* null1 = Value::CreateNullValue(); 469 Value* null1 = Value::CreateNullValue();
744 Value* null2 = Value::CreateNullValue(); 470 Value* null2 = Value::CreateNullValue();
745 EXPECT_NE(null1, null2); 471 EXPECT_NE(null1, null2);
746 EXPECT_TRUE(null1->Equals(null2)); 472 EXPECT_TRUE(null1->Equals(null2));
747 473
748 Value* boolean = Value::CreateBooleanValue(false); 474 Value* boolean = Value::CreateBooleanValue(false);
749 EXPECT_FALSE(null1->Equals(boolean)); 475 EXPECT_FALSE(null1->Equals(boolean));
750 delete null1; 476 delete null1;
751 delete null2; 477 delete null2;
(...skipping 17 matching lines...) Expand all
769 495
770 EXPECT_FALSE(dv.Equals(copy)); 496 EXPECT_FALSE(dv.Equals(copy));
771 copy->Set("f", list->DeepCopy()); 497 copy->Set("f", list->DeepCopy());
772 EXPECT_TRUE(dv.Equals(copy)); 498 EXPECT_TRUE(dv.Equals(copy));
773 499
774 list->Append(Value::CreateBooleanValue(true)); 500 list->Append(Value::CreateBooleanValue(true));
775 EXPECT_FALSE(dv.Equals(copy)); 501 EXPECT_FALSE(dv.Equals(copy));
776 delete copy; 502 delete copy;
777 } 503 }
778 504
779 // TODO(viettrungluu): deprecate:
780 TEST_F(ValuesTest, EqualsDeprecated) {
781 Value* null1 = Value::CreateNullValue();
782 Value* null2 = Value::CreateNullValue();
783 EXPECT_NE(null1, null2);
784 EXPECT_TRUE(null1->Equals(null2));
785
786 Value* boolean = Value::CreateBooleanValue(false);
787 EXPECT_FALSE(null1->Equals(boolean));
788 delete null1;
789 delete null2;
790 delete boolean;
791
792 DictionaryValue dv;
793 dv.SetBoolean(L"a", false);
794 dv.SetInteger(L"b", 2);
795 dv.SetReal(L"c", 2.5);
796 dv.SetString(L"d1", "string");
797 dv.SetString(L"d2", L"string");
798 dv.Set(L"e", Value::CreateNullValue());
799
800 DictionaryValue* copy = static_cast<DictionaryValue*>(dv.DeepCopy());
801 EXPECT_TRUE(dv.Equals(copy));
802
803 ListValue* list = new ListValue;
804 list->Append(Value::CreateNullValue());
805 list->Append(new DictionaryValue);
806 dv.Set(L"f", list);
807
808 EXPECT_FALSE(dv.Equals(copy));
809 copy->Set(L"f", list->DeepCopy());
810 EXPECT_TRUE(dv.Equals(copy));
811
812 list->Append(Value::CreateBooleanValue(true));
813 EXPECT_FALSE(dv.Equals(copy));
814 delete copy;
815 }
816
817 TEST_F(ValuesTest, RemoveEmptyChildren) { 505 TEST_F(ValuesTest, RemoveEmptyChildren) {
818 scoped_ptr<DictionaryValue> root(new DictionaryValue); 506 scoped_ptr<DictionaryValue> root(new DictionaryValue);
819 // Remove empty lists and dictionaries. 507 // Remove empty lists and dictionaries.
820 root->Set("empty_dict", new DictionaryValue); 508 root->Set("empty_dict", new DictionaryValue);
821 root->Set("empty_list", new ListValue); 509 root->Set("empty_list", new ListValue);
822 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue); 510 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
823 root.reset(root->DeepCopyWithoutEmptyChildren()); 511 root.reset(root->DeepCopyWithoutEmptyChildren());
824 EXPECT_TRUE(root->empty()); 512 EXPECT_TRUE(root->empty());
825 513
826 // Make sure we don't prune too much. 514 // Make sure we don't prune too much.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 inner2->Append(Value::CreateStringValue("hello")); 570 inner2->Append(Value::CreateStringValue("hello"));
883 root.reset(root->DeepCopyWithoutEmptyChildren()); 571 root.reset(root->DeepCopyWithoutEmptyChildren());
884 EXPECT_EQ(3U, root->size()); 572 EXPECT_EQ(3U, root->size());
885 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner)); 573 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner));
886 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned. 574 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
887 EXPECT_TRUE(inner->GetList(0, &inner2)); 575 EXPECT_TRUE(inner->GetList(0, &inner2));
888 EXPECT_EQ(1U, inner2->GetSize()); 576 EXPECT_EQ(1U, inner2->GetSize());
889 } 577 }
890 } 578 }
891 579
892 // TODO(viettrungluu): deprecate:
893 TEST_F(ValuesTest, RemoveEmptyChildrenDeprecated) {
894 scoped_ptr<DictionaryValue> root(new DictionaryValue);
895 // Remove empty lists and dictionaries.
896 root->Set(L"empty_dict", new DictionaryValue);
897 root->Set(L"empty_list", new ListValue);
898 root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue);
899 root.reset(root->DeepCopyWithoutEmptyChildren());
900 EXPECT_TRUE(root->empty());
901
902 // Make sure we don't prune too much.
903 root->SetBoolean(L"bool", true);
904 root->Set(L"empty_dict", new DictionaryValue);
905 root->SetString(L"empty_string", "");
906 root.reset(root->DeepCopyWithoutEmptyChildren());
907 EXPECT_EQ(2U, root->size());
908
909 // Should do nothing.
910 root.reset(root->DeepCopyWithoutEmptyChildren());
911 EXPECT_EQ(2U, root->size());
912
913 // Nested test cases. These should all reduce back to the bool and string
914 // set above.
915 {
916 root->Set(L"a.b.c.d.e", new DictionaryValue);
917 root.reset(root->DeepCopyWithoutEmptyChildren());
918 EXPECT_EQ(2U, root->size());
919 }
920 {
921 DictionaryValue* inner = new DictionaryValue;
922 root->Set(L"dict_with_emtpy_children", inner);
923 inner->Set(L"empty_dict", new DictionaryValue);
924 inner->Set(L"empty_list", new ListValue);
925 root.reset(root->DeepCopyWithoutEmptyChildren());
926 EXPECT_EQ(2U, root->size());
927 }
928 {
929 ListValue* inner = new ListValue;
930 root->Set(L"list_with_empty_children", inner);
931 inner->Append(new DictionaryValue);
932 inner->Append(new ListValue);
933 root.reset(root->DeepCopyWithoutEmptyChildren());
934 EXPECT_EQ(2U, root->size());
935 }
936
937 // Nested with siblings.
938 {
939 ListValue* inner = new ListValue;
940 root->Set(L"list_with_empty_children", inner);
941 inner->Append(new DictionaryValue);
942 inner->Append(new ListValue);
943 DictionaryValue* inner2 = new DictionaryValue;
944 root->Set(L"dict_with_empty_children", inner2);
945 inner2->Set(L"empty_dict", new DictionaryValue);
946 inner2->Set(L"empty_list", new ListValue);
947 root.reset(root->DeepCopyWithoutEmptyChildren());
948 EXPECT_EQ(2U, root->size());
949 }
950
951 // Make sure nested values don't get pruned.
952 {
953 ListValue* inner = new ListValue;
954 root->Set(L"list_with_empty_children", inner);
955 ListValue* inner2 = new ListValue;
956 inner->Append(new DictionaryValue);
957 inner->Append(inner2);
958 inner2->Append(Value::CreateStringValue("hello"));
959 root.reset(root->DeepCopyWithoutEmptyChildren());
960 EXPECT_EQ(3U, root->size());
961 EXPECT_TRUE(root->GetList(L"list_with_empty_children", &inner));
962 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned.
963 EXPECT_TRUE(inner->GetList(0, &inner2));
964 EXPECT_EQ(1U, inner2->GetSize());
965 }
966 }
967
968 TEST_F(ValuesTest, MergeDictionary) { 580 TEST_F(ValuesTest, MergeDictionary) {
969 scoped_ptr<DictionaryValue> base(new DictionaryValue); 581 scoped_ptr<DictionaryValue> base(new DictionaryValue);
970 base->SetString("base_key", "base_key_value_base"); 582 base->SetString("base_key", "base_key_value_base");
971 base->SetString("collide_key", "collide_key_value_base"); 583 base->SetString("collide_key", "collide_key_value_base");
972 DictionaryValue* base_sub_dict = new DictionaryValue; 584 DictionaryValue* base_sub_dict = new DictionaryValue;
973 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base"); 585 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
974 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base"); 586 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
975 base->Set("sub_dict_key", base_sub_dict); 587 base->Set("sub_dict_key", base_sub_dict);
976 588
977 scoped_ptr<DictionaryValue> merge(new DictionaryValue); 589 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
(...skipping 25 matching lines...) Expand all
1003 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved. 615 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1004 std::string sub_collide_key_value; 616 std::string sub_collide_key_value;
1005 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", 617 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
1006 &sub_collide_key_value)); 618 &sub_collide_key_value));
1007 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. 619 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1008 std::string sub_merge_key_value; 620 std::string sub_merge_key_value;
1009 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); 621 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
1010 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. 622 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1011 } 623 }
1012 624
1013 // TODO(viettrungluu): deprecate:
1014 TEST_F(ValuesTest, MergeDictionaryDeprecated) {
1015 scoped_ptr<DictionaryValue> base(new DictionaryValue);
1016 base->SetString(L"base_key", "base_key_value_base");
1017 base->SetString(L"collide_key", "collide_key_value_base");
1018 DictionaryValue* base_sub_dict = new DictionaryValue;
1019 base_sub_dict->SetString(L"sub_base_key", "sub_base_key_value_base");
1020 base_sub_dict->SetString(L"sub_collide_key", "sub_collide_key_value_base");
1021 base->Set(L"sub_dict_key", base_sub_dict);
1022
1023 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
1024 merge->SetString(L"merge_key", "merge_key_value_merge");
1025 merge->SetString(L"collide_key", "collide_key_value_merge");
1026 DictionaryValue* merge_sub_dict = new DictionaryValue;
1027 merge_sub_dict->SetString(L"sub_merge_key", "sub_merge_key_value_merge");
1028 merge_sub_dict->SetString(L"sub_collide_key", "sub_collide_key_value_merge");
1029 merge->Set(L"sub_dict_key", merge_sub_dict);
1030
1031 base->MergeDictionary(merge.get());
1032
1033 EXPECT_EQ(4U, base->size());
1034 std::string base_key_value;
1035 EXPECT_TRUE(base->GetString(L"base_key", &base_key_value));
1036 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
1037 std::string collide_key_value;
1038 EXPECT_TRUE(base->GetString(L"collide_key", &collide_key_value));
1039 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
1040 std::string merge_key_value;
1041 EXPECT_TRUE(base->GetString(L"merge_key", &merge_key_value));
1042 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
1043
1044 DictionaryValue* res_sub_dict;
1045 EXPECT_TRUE(base->GetDictionary(L"sub_dict_key", &res_sub_dict));
1046 EXPECT_EQ(3U, res_sub_dict->size());
1047 std::string sub_base_key_value;
1048 EXPECT_TRUE(res_sub_dict->GetString(L"sub_base_key", &sub_base_key_value));
1049 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1050 std::string sub_collide_key_value;
1051 EXPECT_TRUE(res_sub_dict->GetString(L"sub_collide_key",
1052 &sub_collide_key_value));
1053 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1054 std::string sub_merge_key_value;
1055 EXPECT_TRUE(res_sub_dict->GetString(L"sub_merge_key", &sub_merge_key_value));
1056 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1057 }
1058
1059 TEST_F(ValuesTest, GetDifferingPaths) { 625 TEST_F(ValuesTest, GetDifferingPaths) {
1060 scoped_ptr<DictionaryValue> dict1(new DictionaryValue()); 626 scoped_ptr<DictionaryValue> dict1(new DictionaryValue());
1061 scoped_ptr<DictionaryValue> dict2(new DictionaryValue()); 627 scoped_ptr<DictionaryValue> dict2(new DictionaryValue());
1062 std::vector<std::string> differing_paths; 628 std::vector<std::string> differing_paths;
1063 629
1064 // Test comparing empty dictionaries. 630 // Test comparing empty dictionaries.
1065 dict1->GetDifferingPaths(dict2.get(), &differing_paths); 631 dict1->GetDifferingPaths(dict2.get(), &differing_paths);
1066 EXPECT_EQ(differing_paths.size(), 0UL); 632 EXPECT_EQ(differing_paths.size(), 0UL);
1067 633
1068 // Compare an empty dictionary with various non-empty dictionaries. 634 // Compare an empty dictionary with various non-empty dictionaries.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1159 "s1", 725 "s1",
1160 "s1.s2" 726 "s1.s2"
1161 }; 727 };
1162 dict1.reset(new DictionaryValue()); 728 dict1.reset(new DictionaryValue());
1163 dict1->Set("s1.s2", new DictionaryValue()); 729 dict1->Set("s1.s2", new DictionaryValue());
1164 dict2.reset(new DictionaryValue()); 730 dict2.reset(new DictionaryValue());
1165 dict2->SetInteger("s1", 1); 731 dict2->SetInteger("s1", 1);
1166 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths8, 732 CompareDictionariesAndCheckResult(dict1.get(), dict2.get(), expected_paths8,
1167 arraysize(expected_paths8)); 733 arraysize(expected_paths8));
1168 } 734 }
OLDNEW
« no previous file with comments | « base/values.cc ('k') | chrome/browser/autocomplete/search_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698