OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "base/values.h" | 5 #include "base/values.h" |
6 #include "tools/json_schema_compiler/test/idl_basics.h" | 6 #include "tools/json_schema_compiler/test/idl_basics.h" |
7 #include "tools/json_schema_compiler/test/idl_object_types.h" | 7 #include "tools/json_schema_compiler/test/idl_object_types.h" |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace Function9 = test::api::idl_basics::Function9; | 22 namespace Function9 = test::api::idl_basics::Function9; |
23 namespace Function10 = test::api::idl_basics::Function10; | 23 namespace Function10 = test::api::idl_basics::Function10; |
24 namespace Function11 = test::api::idl_basics::Function11; | 24 namespace Function11 = test::api::idl_basics::Function11; |
25 namespace ObjectFunction1 = test::api::idl_object_types::ObjectFunction1; | 25 namespace ObjectFunction1 = test::api::idl_object_types::ObjectFunction1; |
26 | 26 |
27 TEST(IdlCompiler, Basics) { | 27 TEST(IdlCompiler, Basics) { |
28 // Test MyType1. | 28 // Test MyType1. |
29 MyType1 a; | 29 MyType1 a; |
30 a.x = 5; | 30 a.x = 5; |
31 a.y = std::string("foo"); | 31 a.y = std::string("foo"); |
32 scoped_ptr<base::DictionaryValue> serialized = a.ToValue(); | 32 std::unique_ptr<base::DictionaryValue> serialized = a.ToValue(); |
33 MyType1 b; | 33 MyType1 b; |
34 EXPECT_TRUE(MyType1::Populate(*serialized.get(), &b)); | 34 EXPECT_TRUE(MyType1::Populate(*serialized.get(), &b)); |
35 EXPECT_EQ(a.x, b.x); | 35 EXPECT_EQ(a.x, b.x); |
36 EXPECT_EQ(a.y, b.y); | 36 EXPECT_EQ(a.y, b.y); |
37 | 37 |
38 // Test Function2, which takes an integer parameter. | 38 // Test Function2, which takes an integer parameter. |
39 base::ListValue list; | 39 base::ListValue list; |
40 list.Append(new base::FundamentalValue(5)); | 40 list.Append(new base::FundamentalValue(5)); |
41 scoped_ptr<Function2::Params> f2_params = Function2::Params::Create(list); | 41 std::unique_ptr<Function2::Params> f2_params = |
| 42 Function2::Params::Create(list); |
42 EXPECT_EQ(5, f2_params->x); | 43 EXPECT_EQ(5, f2_params->x); |
43 | 44 |
44 // Test Function3, which takes a MyType1 parameter. | 45 // Test Function3, which takes a MyType1 parameter. |
45 list.Clear(); | 46 list.Clear(); |
46 base::DictionaryValue* tmp = new base::DictionaryValue(); | 47 base::DictionaryValue* tmp = new base::DictionaryValue(); |
47 tmp->SetInteger("x", 17); | 48 tmp->SetInteger("x", 17); |
48 tmp->SetString("y", "hello"); | 49 tmp->SetString("y", "hello"); |
49 tmp->SetString("z", "zstring"); | 50 tmp->SetString("z", "zstring"); |
50 tmp->SetString("a", "astring"); | 51 tmp->SetString("a", "astring"); |
51 tmp->SetString("b", "bstring"); | 52 tmp->SetString("b", "bstring"); |
52 tmp->SetString("c", "cstring"); | 53 tmp->SetString("c", "cstring"); |
53 list.Append(tmp); | 54 list.Append(tmp); |
54 scoped_ptr<Function3::Params> f3_params = Function3::Params::Create(list); | 55 std::unique_ptr<Function3::Params> f3_params = |
| 56 Function3::Params::Create(list); |
55 EXPECT_EQ(17, f3_params->arg.x); | 57 EXPECT_EQ(17, f3_params->arg.x); |
56 EXPECT_EQ("hello", f3_params->arg.y); | 58 EXPECT_EQ("hello", f3_params->arg.y); |
57 | 59 |
58 // Test functions that take a callback function as a parameter, with varying | 60 // Test functions that take a callback function as a parameter, with varying |
59 // callback signatures. | 61 // callback signatures. |
60 scoped_ptr<base::ListValue> f4_results = Function4::Results::Create(); | 62 std::unique_ptr<base::ListValue> f4_results = Function4::Results::Create(); |
61 base::ListValue expected; | 63 base::ListValue expected; |
62 EXPECT_TRUE(f4_results->Equals(&expected)); | 64 EXPECT_TRUE(f4_results->Equals(&expected)); |
63 | 65 |
64 scoped_ptr<base::ListValue> f5_results(Function5::Results::Create(13)); | 66 std::unique_ptr<base::ListValue> f5_results(Function5::Results::Create(13)); |
65 base::Value* f5_result_int = NULL; | 67 base::Value* f5_result_int = NULL; |
66 ASSERT_TRUE(f5_results->Get(0, &f5_result_int)); | 68 ASSERT_TRUE(f5_results->Get(0, &f5_result_int)); |
67 EXPECT_TRUE(f5_result_int->IsType(base::Value::TYPE_INTEGER)); | 69 EXPECT_TRUE(f5_result_int->IsType(base::Value::TYPE_INTEGER)); |
68 | 70 |
69 scoped_ptr<base::ListValue> f6_results(Function6::Results::Create(a)); | 71 std::unique_ptr<base::ListValue> f6_results(Function6::Results::Create(a)); |
70 base::Value* f6_result_dict = NULL; | 72 base::Value* f6_result_dict = NULL; |
71 ASSERT_TRUE(f6_results->Get(0, &f6_result_dict)); | 73 ASSERT_TRUE(f6_results->Get(0, &f6_result_dict)); |
72 MyType1 c; | 74 MyType1 c; |
73 EXPECT_TRUE(MyType1::Populate(*f6_result_dict, &c)); | 75 EXPECT_TRUE(MyType1::Populate(*f6_result_dict, &c)); |
74 EXPECT_EQ(a.x, c.x); | 76 EXPECT_EQ(a.x, c.x); |
75 EXPECT_EQ(a.y, c.y); | 77 EXPECT_EQ(a.y, c.y); |
76 } | 78 } |
77 | 79 |
78 TEST(IdlCompiler, OptionalArguments) { | 80 TEST(IdlCompiler, OptionalArguments) { |
79 // Test a function that takes one optional argument, both without and with | 81 // Test a function that takes one optional argument, both without and with |
80 // that argument. | 82 // that argument. |
81 base::ListValue list; | 83 base::ListValue list; |
82 scoped_ptr<Function7::Params> f7_params = Function7::Params::Create(list); | 84 std::unique_ptr<Function7::Params> f7_params = |
| 85 Function7::Params::Create(list); |
83 EXPECT_EQ(NULL, f7_params->arg.get()); | 86 EXPECT_EQ(NULL, f7_params->arg.get()); |
84 list.Append(new base::FundamentalValue(7)); | 87 list.Append(new base::FundamentalValue(7)); |
85 f7_params = Function7::Params::Create(list); | 88 f7_params = Function7::Params::Create(list); |
86 EXPECT_EQ(7, *(f7_params->arg)); | 89 EXPECT_EQ(7, *(f7_params->arg)); |
87 | 90 |
88 // Similar to above, but a function with one required and one optional | 91 // Similar to above, but a function with one required and one optional |
89 // argument. | 92 // argument. |
90 list.Clear(); | 93 list.Clear(); |
91 list.Append(new base::FundamentalValue(8)); | 94 list.Append(new base::FundamentalValue(8)); |
92 scoped_ptr<Function8::Params> f8_params = Function8::Params::Create(list); | 95 std::unique_ptr<Function8::Params> f8_params = |
| 96 Function8::Params::Create(list); |
93 EXPECT_EQ(8, f8_params->arg1); | 97 EXPECT_EQ(8, f8_params->arg1); |
94 EXPECT_EQ(NULL, f8_params->arg2.get()); | 98 EXPECT_EQ(NULL, f8_params->arg2.get()); |
95 list.Append(new base::StringValue("foo")); | 99 list.Append(new base::StringValue("foo")); |
96 f8_params = Function8::Params::Create(list); | 100 f8_params = Function8::Params::Create(list); |
97 EXPECT_EQ(8, f8_params->arg1); | 101 EXPECT_EQ(8, f8_params->arg1); |
98 EXPECT_EQ("foo", *(f8_params->arg2)); | 102 EXPECT_EQ("foo", *(f8_params->arg2)); |
99 | 103 |
100 // Test a function with an optional argument of custom type. | 104 // Test a function with an optional argument of custom type. |
101 list.Clear(); | 105 list.Clear(); |
102 scoped_ptr<Function9::Params> f9_params = Function9::Params::Create(list); | 106 std::unique_ptr<Function9::Params> f9_params = |
| 107 Function9::Params::Create(list); |
103 EXPECT_EQ(NULL, f9_params->arg.get()); | 108 EXPECT_EQ(NULL, f9_params->arg.get()); |
104 list.Clear(); | 109 list.Clear(); |
105 base::DictionaryValue* tmp = new base::DictionaryValue(); | 110 base::DictionaryValue* tmp = new base::DictionaryValue(); |
106 tmp->SetInteger("x", 17); | 111 tmp->SetInteger("x", 17); |
107 tmp->SetString("y", "hello"); | 112 tmp->SetString("y", "hello"); |
108 tmp->SetString("z", "zstring"); | 113 tmp->SetString("z", "zstring"); |
109 tmp->SetString("a", "astring"); | 114 tmp->SetString("a", "astring"); |
110 tmp->SetString("b", "bstring"); | 115 tmp->SetString("b", "bstring"); |
111 tmp->SetString("c", "cstring"); | 116 tmp->SetString("c", "cstring"); |
112 list.Append(tmp); | 117 list.Append(tmp); |
113 f9_params = Function9::Params::Create(list); | 118 f9_params = Function9::Params::Create(list); |
114 ASSERT_TRUE(f9_params->arg.get() != NULL); | 119 ASSERT_TRUE(f9_params->arg.get() != NULL); |
115 MyType1* t1 = f9_params->arg.get(); | 120 MyType1* t1 = f9_params->arg.get(); |
116 EXPECT_EQ(17, t1->x); | 121 EXPECT_EQ(17, t1->x); |
117 EXPECT_EQ("hello", t1->y); | 122 EXPECT_EQ("hello", t1->y); |
118 } | 123 } |
119 | 124 |
120 TEST(IdlCompiler, ArrayTypes) { | 125 TEST(IdlCompiler, ArrayTypes) { |
121 // Tests of a function that takes an integer and an array of integers. First | 126 // Tests of a function that takes an integer and an array of integers. First |
122 // use an empty array. | 127 // use an empty array. |
123 base::ListValue list; | 128 base::ListValue list; |
124 list.Append(new base::FundamentalValue(33)); | 129 list.Append(new base::FundamentalValue(33)); |
125 list.Append(new base::ListValue); | 130 list.Append(new base::ListValue); |
126 scoped_ptr<Function10::Params> f10_params = Function10::Params::Create(list); | 131 std::unique_ptr<Function10::Params> f10_params = |
| 132 Function10::Params::Create(list); |
127 ASSERT_TRUE(f10_params != NULL); | 133 ASSERT_TRUE(f10_params != NULL); |
128 EXPECT_EQ(33, f10_params->x); | 134 EXPECT_EQ(33, f10_params->x); |
129 EXPECT_TRUE(f10_params->y.empty()); | 135 EXPECT_TRUE(f10_params->y.empty()); |
130 | 136 |
131 // Same function, but this time with 2 values in the array. | 137 // Same function, but this time with 2 values in the array. |
132 list.Clear(); | 138 list.Clear(); |
133 list.Append(new base::FundamentalValue(33)); | 139 list.Append(new base::FundamentalValue(33)); |
134 base::ListValue* sublist = new base::ListValue; | 140 base::ListValue* sublist = new base::ListValue; |
135 sublist->Append(new base::FundamentalValue(34)); | 141 sublist->Append(new base::FundamentalValue(34)); |
136 sublist->Append(new base::FundamentalValue(35)); | 142 sublist->Append(new base::FundamentalValue(35)); |
(...skipping 10 matching lines...) Expand all Loading... |
147 MyType1 a; | 153 MyType1 a; |
148 MyType1 b; | 154 MyType1 b; |
149 a.x = 5; | 155 a.x = 5; |
150 b.x = 6; | 156 b.x = 6; |
151 a.y = std::string("foo"); | 157 a.y = std::string("foo"); |
152 b.y = std::string("bar"); | 158 b.y = std::string("bar"); |
153 base::ListValue* sublist2 = new base::ListValue; | 159 base::ListValue* sublist2 = new base::ListValue; |
154 sublist2->Append(a.ToValue().release()); | 160 sublist2->Append(a.ToValue().release()); |
155 sublist2->Append(b.ToValue().release()); | 161 sublist2->Append(b.ToValue().release()); |
156 list.Append(sublist2); | 162 list.Append(sublist2); |
157 scoped_ptr<Function11::Params> f11_params = Function11::Params::Create(list); | 163 std::unique_ptr<Function11::Params> f11_params = |
| 164 Function11::Params::Create(list); |
158 ASSERT_TRUE(f11_params != NULL); | 165 ASSERT_TRUE(f11_params != NULL); |
159 ASSERT_EQ(2u, f11_params->arg.size()); | 166 ASSERT_EQ(2u, f11_params->arg.size()); |
160 EXPECT_EQ(5, f11_params->arg[0].x); | 167 EXPECT_EQ(5, f11_params->arg[0].x); |
161 EXPECT_EQ("foo", f11_params->arg[0].y); | 168 EXPECT_EQ("foo", f11_params->arg[0].y); |
162 EXPECT_EQ(6, f11_params->arg[1].x); | 169 EXPECT_EQ(6, f11_params->arg[1].x); |
163 EXPECT_EQ("bar", f11_params->arg[1].y); | 170 EXPECT_EQ("bar", f11_params->arg[1].y); |
164 } | 171 } |
165 | 172 |
166 TEST(IdlCompiler, ObjectTypes) { | 173 TEST(IdlCompiler, ObjectTypes) { |
167 // Test the FooType type. | 174 // Test the FooType type. |
168 FooType f1; | 175 FooType f1; |
169 f1.x = 3; | 176 f1.x = 3; |
170 scoped_ptr<base::DictionaryValue> serialized_foo = f1.ToValue(); | 177 std::unique_ptr<base::DictionaryValue> serialized_foo = f1.ToValue(); |
171 FooType f2; | 178 FooType f2; |
172 EXPECT_TRUE(FooType::Populate(*serialized_foo.get(), &f2)); | 179 EXPECT_TRUE(FooType::Populate(*serialized_foo.get(), &f2)); |
173 EXPECT_EQ(f1.x, f2.x); | 180 EXPECT_EQ(f1.x, f2.x); |
174 | 181 |
175 // Test the BarType type. | 182 // Test the BarType type. |
176 BarType b1; | 183 BarType b1; |
177 b1.x.reset(new base::FundamentalValue(7)); | 184 b1.x.reset(new base::FundamentalValue(7)); |
178 scoped_ptr<base::DictionaryValue> serialized_bar = b1.ToValue(); | 185 std::unique_ptr<base::DictionaryValue> serialized_bar = b1.ToValue(); |
179 BarType b2; | 186 BarType b2; |
180 EXPECT_TRUE(BarType::Populate(*serialized_bar.get(), &b2)); | 187 EXPECT_TRUE(BarType::Populate(*serialized_bar.get(), &b2)); |
181 int tmp_int = 0; | 188 int tmp_int = 0; |
182 EXPECT_TRUE(b2.x->GetAsInteger(&tmp_int)); | 189 EXPECT_TRUE(b2.x->GetAsInteger(&tmp_int)); |
183 EXPECT_EQ(7, tmp_int); | 190 EXPECT_EQ(7, tmp_int); |
184 | 191 |
185 // Test the params to the ObjectFunction1 function. | 192 // Test the params to the ObjectFunction1 function. |
186 scoped_ptr<base::DictionaryValue> icon_props(new base::DictionaryValue()); | 193 std::unique_ptr<base::DictionaryValue> icon_props( |
| 194 new base::DictionaryValue()); |
187 icon_props->SetString("hello", "world"); | 195 icon_props->SetString("hello", "world"); |
188 ObjectFunction1::Params::Icon icon; | 196 ObjectFunction1::Params::Icon icon; |
189 EXPECT_TRUE(ObjectFunction1::Params::Icon::Populate(*(icon_props.get()), | 197 EXPECT_TRUE(ObjectFunction1::Params::Icon::Populate(*(icon_props.get()), |
190 &icon)); | 198 &icon)); |
191 base::ListValue list; | 199 base::ListValue list; |
192 list.Append(icon_props.release()); | 200 list.Append(icon_props.release()); |
193 scoped_ptr<ObjectFunction1::Params> params = | 201 std::unique_ptr<ObjectFunction1::Params> params = |
194 ObjectFunction1::Params::Create(list); | 202 ObjectFunction1::Params::Create(list); |
195 ASSERT_TRUE(params.get() != NULL); | 203 ASSERT_TRUE(params.get() != NULL); |
196 std::string tmp; | 204 std::string tmp; |
197 EXPECT_TRUE(params->icon.additional_properties.GetString("hello", &tmp)); | 205 EXPECT_TRUE(params->icon.additional_properties.GetString("hello", &tmp)); |
198 EXPECT_EQ("world", tmp); | 206 EXPECT_EQ("world", tmp); |
199 } | 207 } |
OLD | NEW |