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