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

Side by Side Diff: components/policy/core/common/schema_unittest.cc

Issue 2889163002: Remove raw DictionaryValue::Set in //components (Closed)
Patch Set: Nits Created 3 years, 6 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/policy/core/common/schema.h" 5 #include "components/policy/core/common/schema.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/values.h"
15 #include "components/policy/core/common/schema_internal.h" 16 #include "components/policy/core/common/schema_internal.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace policy { 19 namespace policy {
19 20
20 namespace { 21 namespace {
21 22
22 #define TestSchemaValidation(a, b, c, d) \ 23 #define TestSchemaValidation(a, b, c, d) \
23 TestSchemaValidationHelper( \ 24 TestSchemaValidationHelper( \
24 base::StringPrintf("%s:%i", __FILE__, __LINE__), a, b, c, d) 25 base::StringPrintf("%s:%i", __FILE__, __LINE__), a, b, c, d)
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 613
613 // Wrong type, expected integer. 614 // Wrong type, expected integer.
614 bundle.SetBoolean("Integer", true); 615 bundle.SetBoolean("Integer", true);
615 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 616 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
616 617
617 // Wrong type, expected list of strings. 618 // Wrong type, expected list of strings.
618 { 619 {
619 bundle.Clear(); 620 bundle.Clear();
620 base::ListValue list; 621 base::ListValue list;
621 list.AppendInteger(1); 622 list.AppendInteger(1);
622 bundle.Set("Array", list.DeepCopy()); 623 bundle.Set("Array", base::MakeUnique<base::Value>(list));
623 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 624 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
624 } 625 }
625 626
626 // Wrong type in a sub-object. 627 // Wrong type in a sub-object.
627 { 628 {
628 bundle.Clear(); 629 bundle.Clear();
629 base::DictionaryValue dict; 630 base::DictionaryValue dict;
630 dict.SetString("one", "one"); 631 dict.SetString("one", "one");
631 bundle.Set("Object", dict.DeepCopy()); 632 bundle.Set("Object", base::MakeUnique<base::Value>(dict));
632 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 633 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
633 } 634 }
634 635
635 // Unknown name. 636 // Unknown name.
636 bundle.Clear(); 637 bundle.Clear();
637 bundle.SetBoolean("Unknown", true); 638 bundle.SetBoolean("Unknown", true);
638 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 639 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
639 640
640 // All of these will be valid. 641 // All of these will be valid.
641 bundle.Clear(); 642 bundle.Clear();
642 bundle.SetBoolean("Boolean", true); 643 bundle.SetBoolean("Boolean", true);
643 bundle.SetInteger("Integer", 123); 644 bundle.SetInteger("Integer", 123);
644 bundle.Set("Null", base::MakeUnique<base::Value>()); 645 bundle.Set("Null", base::MakeUnique<base::Value>());
645 bundle.Set("Number", new base::Value(3.14)); 646 bundle.SetDouble("Number", 3.14);
646 bundle.SetString("String", "omg"); 647 bundle.SetString("String", "omg");
647 648
648 { 649 {
649 base::ListValue list; 650 base::ListValue list;
650 list.AppendString("a string"); 651 list.AppendString("a string");
651 list.AppendString("another string"); 652 list.AppendString("another string");
652 bundle.Set("Array", list.DeepCopy()); 653 bundle.Set("Array", base::MakeUnique<base::Value>(list));
653 } 654 }
654 655
655 { 656 {
656 base::DictionaryValue dict; 657 base::DictionaryValue dict;
657 dict.SetString("one", "string"); 658 dict.SetString("one", "string");
658 dict.SetInteger("two", 2); 659 dict.SetInteger("two", 2);
659 base::ListValue list; 660 base::ListValue list;
660 list.Append(dict.CreateDeepCopy()); 661 list.GetList().push_back(dict);
661 list.Append(dict.CreateDeepCopy()); 662 list.GetList().push_back(dict);
662 bundle.Set("ArrayOfObjects", list.DeepCopy()); 663 bundle.Set("ArrayOfObjects", base::MakeUnique<base::Value>(list));
663 } 664 }
664 665
665 { 666 {
666 base::ListValue list; 667 base::ListValue list;
667 list.AppendString("a string"); 668 list.AppendString("a string");
668 list.AppendString("another string"); 669 list.AppendString("another string");
669 base::ListValue listlist; 670 base::ListValue listlist;
670 listlist.Append(list.CreateDeepCopy()); 671 listlist.GetList().push_back(list);
671 listlist.Append(list.CreateDeepCopy()); 672 listlist.GetList().push_back(list);
672 bundle.Set("ArrayOfArray", listlist.DeepCopy()); 673 bundle.Set("ArrayOfArray", base::MakeUnique<base::Value>(listlist));
673 } 674 }
674 675
675 { 676 {
676 base::DictionaryValue dict; 677 base::DictionaryValue dict;
677 dict.SetBoolean("one", true); 678 dict.SetBoolean("one", true);
678 dict.SetInteger("two", 2); 679 dict.SetInteger("two", 2);
679 dict.SetString("additionally", "a string"); 680 dict.SetString("additionally", "a string");
680 dict.SetString("and also", "another string"); 681 dict.SetString("and also", "another string");
681 bundle.Set("Object", dict.DeepCopy()); 682 bundle.Set("Object", base::MakeUnique<base::Value>(dict));
682 } 683 }
683 684
684 bundle.SetInteger("IntegerWithEnums", 1); 685 bundle.SetInteger("IntegerWithEnums", 1);
685 bundle.SetInteger("IntegerWithEnumsGaps", 20); 686 bundle.SetInteger("IntegerWithEnumsGaps", 20);
686 bundle.SetString("StringWithEnums", "two"); 687 bundle.SetString("StringWithEnums", "two");
687 bundle.SetInteger("IntegerWithRange", 3); 688 bundle.SetInteger("IntegerWithRange", 3);
688 689
689 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 690 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
690 691
691 bundle.SetInteger("IntegerWithEnums", 0); 692 bundle.SetInteger("IntegerWithEnums", 0);
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 TestSchemaValidationWithPath(subschema, root, "items[0].two"); 797 TestSchemaValidationWithPath(subschema, root, "items[0].two");
797 root.Remove(root.GetSize() - 1, NULL); 798 root.Remove(root.GetSize() - 1, NULL);
798 } 799 }
799 800
800 // Tests on ObjectOfArray. 801 // Tests on ObjectOfArray.
801 { 802 {
802 Schema subschema = schema.GetProperty("ObjectOfArray"); 803 Schema subschema = schema.GetProperty("ObjectOfArray");
803 ASSERT_TRUE(subschema.valid()); 804 ASSERT_TRUE(subschema.valid());
804 base::DictionaryValue root; 805 base::DictionaryValue root;
805 806
806 base::ListValue* list_value = new base::ListValue(); 807 base::ListValue* list_value =
807 root.Set("List", list_value); // Pass ownership to root. 808 root.SetList("List", base::MakeUnique<base::ListValue>());
808 809
809 // Test that there are not errors here. 810 // Test that there are not errors here.
810 list_value->AppendInteger(12345); 811 list_value->AppendInteger(12345);
811 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); 812 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
812 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); 813 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
813 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 814 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
814 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 815 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
815 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 816 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
816 817
817 // Invalid list item. 818 // Invalid list item.
818 list_value->AppendString("blabla"); 819 list_value->AppendString("blabla");
819 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 820 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
820 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 821 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
821 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); 822 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
822 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 823 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
823 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 824 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
824 TestSchemaValidationWithPath(subschema, root, "List.items[1]"); 825 TestSchemaValidationWithPath(subschema, root, "List.items[1]");
825 } 826 }
826 827
827 // Tests on ArrayOfObjectOfArray. 828 // Tests on ArrayOfObjectOfArray.
828 { 829 {
829 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray"); 830 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray");
830 ASSERT_TRUE(subschema.valid()); 831 ASSERT_TRUE(subschema.valid());
831 base::ListValue root; 832 base::ListValue root;
832 833
833 base::ListValue* list_value = new base::ListValue(); 834 auto dict_value = base::MakeUnique<base::DictionaryValue>();
834 std::unique_ptr<base::DictionaryValue> dict_value( 835 base::ListValue* list_value =
835 new base::DictionaryValue()); 836 dict_value->SetList("List", base::MakeUnique<base::ListValue>());
836 dict_value->Set("List", list_value); // Pass ownership to dict_value.
837 root.Append(std::move(dict_value)); 837 root.Append(std::move(dict_value));
838 838
839 // Test that there are not errors here. 839 // Test that there are not errors here.
840 list_value->AppendString("blabla"); 840 list_value->AppendString("blabla");
841 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); 841 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
842 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); 842 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
843 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 843 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
844 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 844 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
845 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 845 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
846 846
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 1175
1176 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( 1176 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
1177 "{" 1177 "{"
1178 " \"type\": \"integer\"," 1178 " \"type\": \"integer\","
1179 " \"minimum\": 10," 1179 " \"minimum\": 10,"
1180 " \"maximum\": 20" 1180 " \"maximum\": 20"
1181 "}"))); 1181 "}")));
1182 } 1182 }
1183 1183
1184 } // namespace policy 1184 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698