Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / tools / json_schema_compiler / test / arrays_unittest.cc
blobf0d3ab111291de042b9cf38c7b001f855a4c8d59
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 "tools/json_schema_compiler/test/arrays.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/json_schema_compiler/test/enums.h"
10 using namespace test::api::arrays;
12 namespace {
14 // TODO(calamity): Change to AppendString etc once kalman's patch goes through
15 static scoped_ptr<base::DictionaryValue> CreateBasicArrayTypeDictionary() {
16 base::DictionaryValue* value = new base::DictionaryValue();
17 base::ListValue* strings_value = new base::ListValue();
18 strings_value->Append(new base::StringValue("a"));
19 strings_value->Append(new base::StringValue("b"));
20 strings_value->Append(new base::StringValue("c"));
21 strings_value->Append(new base::StringValue("it's easy as"));
22 base::ListValue* integers_value = new base::ListValue();
23 integers_value->Append(new base::FundamentalValue(1));
24 integers_value->Append(new base::FundamentalValue(2));
25 integers_value->Append(new base::FundamentalValue(3));
26 base::ListValue* booleans_value = new base::ListValue();
27 booleans_value->Append(new base::FundamentalValue(false));
28 booleans_value->Append(new base::FundamentalValue(true));
29 base::ListValue* numbers_value = new base::ListValue();
30 numbers_value->Append(new base::FundamentalValue(6.1));
31 value->Set("numbers", numbers_value);
32 value->Set("booleans", booleans_value);
33 value->Set("strings", strings_value);
34 value->Set("integers", integers_value);
35 return scoped_ptr<base::DictionaryValue>(value);
38 static base::Value* CreateItemValue(int val) {
39 base::DictionaryValue* value(new base::DictionaryValue());
40 value->Set("val", new base::FundamentalValue(val));
41 return value;
44 } // namespace
46 TEST(JsonSchemaCompilerArrayTest, BasicArrayType) {
48 scoped_ptr<base::DictionaryValue> value = CreateBasicArrayTypeDictionary();
49 scoped_ptr<BasicArrayType> basic_array_type(new BasicArrayType());
50 ASSERT_TRUE(BasicArrayType::Populate(*value, basic_array_type.get()));
51 EXPECT_TRUE(value->Equals(basic_array_type->ToValue().get()));
55 TEST(JsonSchemaCompilerArrayTest, EnumArrayReference) {
56 // { "types": ["one", "two", "three"] }
57 base::ListValue* types = new base::ListValue();
58 types->AppendString("one");
59 types->AppendString("two");
60 types->AppendString("three");
61 base::DictionaryValue value;
62 value.Set("types", types);
64 EnumArrayReference enum_array_reference;
66 // Test Populate.
67 ASSERT_TRUE(EnumArrayReference::Populate(value, &enum_array_reference));
69 Enumeration expected_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
70 ENUMERATION_THREE};
71 EXPECT_EQ(std::vector<Enumeration>(
72 expected_types, expected_types + arraysize(expected_types)),
73 enum_array_reference.types);
75 // Test ToValue.
76 scoped_ptr<base::Value> as_value(enum_array_reference.ToValue());
77 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
80 TEST(JsonSchemaCompilerArrayTest, EnumArrayMixed) {
81 // { "types": ["one", "two", "three"] }
82 base::ListValue* infile_enums = new base::ListValue();
83 infile_enums->AppendString("one");
84 infile_enums->AppendString("two");
85 infile_enums->AppendString("three");
87 base::ListValue* external_enums = new base::ListValue();
88 external_enums->AppendString("one");
89 external_enums->AppendString("two");
90 external_enums->AppendString("three");
92 base::DictionaryValue value;
93 value.Set("infile_enums", infile_enums);
94 value.Set("external_enums", external_enums);
96 EnumArrayMixed enum_array_mixed;
98 // Test Populate.
99 ASSERT_TRUE(EnumArrayMixed::Populate(value, &enum_array_mixed));
101 Enumeration expected_infile_types[] = {ENUMERATION_ONE, ENUMERATION_TWO,
102 ENUMERATION_THREE};
103 EXPECT_EQ(std::vector<Enumeration>(
104 expected_infile_types,
105 expected_infile_types + arraysize(expected_infile_types)),
106 enum_array_mixed.infile_enums);
108 test::api::enums::Enumeration expected_external_types[] = {
109 test::api::enums::ENUMERATION_ONE, test::api::enums::ENUMERATION_TWO,
110 test::api::enums::ENUMERATION_THREE};
111 EXPECT_EQ(std::vector<test::api::enums::Enumeration>(
112 expected_external_types,
113 expected_external_types + arraysize(expected_external_types)),
114 enum_array_mixed.external_enums);
116 // Test ToValue.
117 scoped_ptr<base::Value> as_value(enum_array_mixed.ToValue());
118 EXPECT_TRUE(value.Equals(as_value.get())) << value << " != " << *as_value;
121 TEST(JsonSchemaCompilerArrayTest, OptionalEnumArrayType) {
123 std::vector<Enumeration> enums;
124 enums.push_back(ENUMERATION_ONE);
125 enums.push_back(ENUMERATION_TWO);
126 enums.push_back(ENUMERATION_THREE);
128 scoped_ptr<base::ListValue> types(new base::ListValue());
129 for (size_t i = 0; i < enums.size(); ++i)
130 types->Append(new base::StringValue(ToString(enums[i])));
132 base::DictionaryValue value;
133 value.Set("types", types.release());
135 OptionalEnumArrayType enum_array_type;
136 ASSERT_TRUE(OptionalEnumArrayType::Populate(value, &enum_array_type));
137 EXPECT_EQ(enums, *enum_array_type.types);
140 base::DictionaryValue value;
141 scoped_ptr<base::ListValue> enum_array(new base::ListValue());
142 enum_array->Append(new base::StringValue("invalid"));
144 value.Set("types", enum_array.release());
145 OptionalEnumArrayType enum_array_type;
146 ASSERT_FALSE(OptionalEnumArrayType::Populate(value, &enum_array_type));
147 EXPECT_TRUE(enum_array_type.types->empty());
151 TEST(JsonSchemaCompilerArrayTest, RefArrayType) {
153 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
154 scoped_ptr<base::ListValue> ref_array(new base::ListValue());
155 ref_array->Append(CreateItemValue(1));
156 ref_array->Append(CreateItemValue(2));
157 ref_array->Append(CreateItemValue(3));
158 value->Set("refs", ref_array.release());
159 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType());
160 EXPECT_TRUE(RefArrayType::Populate(*value, ref_array_type.get()));
161 ASSERT_EQ(3u, ref_array_type->refs.size());
162 EXPECT_EQ(1, ref_array_type->refs[0]->val);
163 EXPECT_EQ(2, ref_array_type->refs[1]->val);
164 EXPECT_EQ(3, ref_array_type->refs[2]->val);
167 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
168 scoped_ptr<base::ListValue> not_ref_array(new base::ListValue());
169 not_ref_array->Append(CreateItemValue(1));
170 not_ref_array->Append(new base::FundamentalValue(3));
171 value->Set("refs", not_ref_array.release());
172 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType());
173 EXPECT_FALSE(RefArrayType::Populate(*value, ref_array_type.get()));
177 TEST(JsonSchemaCompilerArrayTest, IntegerArrayParamsCreate) {
178 scoped_ptr<base::ListValue> params_value(new base::ListValue());
179 scoped_ptr<base::ListValue> integer_array(new base::ListValue());
180 integer_array->Append(new base::FundamentalValue(2));
181 integer_array->Append(new base::FundamentalValue(4));
182 integer_array->Append(new base::FundamentalValue(8));
183 params_value->Append(integer_array.release());
184 scoped_ptr<IntegerArray::Params> params(
185 IntegerArray::Params::Create(*params_value));
186 EXPECT_TRUE(params.get());
187 ASSERT_EQ(3u, params->nums.size());
188 EXPECT_EQ(2, params->nums[0]);
189 EXPECT_EQ(4, params->nums[1]);
190 EXPECT_EQ(8, params->nums[2]);
193 TEST(JsonSchemaCompilerArrayTest, AnyArrayParamsCreate) {
194 scoped_ptr<base::ListValue> params_value(new base::ListValue());
195 scoped_ptr<base::ListValue> any_array(new base::ListValue());
196 any_array->Append(new base::FundamentalValue(1));
197 any_array->Append(new base::StringValue("test"));
198 any_array->Append(CreateItemValue(2));
199 params_value->Append(any_array.release());
200 scoped_ptr<AnyArray::Params> params(
201 AnyArray::Params::Create(*params_value));
202 EXPECT_TRUE(params.get());
203 ASSERT_EQ(3u, params->anys.size());
204 int int_temp = 0;
205 EXPECT_TRUE(params->anys[0]->GetAsInteger(&int_temp));
206 EXPECT_EQ(1, int_temp);
209 TEST(JsonSchemaCompilerArrayTest, ObjectArrayParamsCreate) {
210 scoped_ptr<base::ListValue> params_value(new base::ListValue());
211 scoped_ptr<base::ListValue> item_array(new base::ListValue());
212 item_array->Append(CreateItemValue(1));
213 item_array->Append(CreateItemValue(2));
214 params_value->Append(item_array.release());
215 scoped_ptr<ObjectArray::Params> params(
216 ObjectArray::Params::Create(*params_value));
217 EXPECT_TRUE(params.get());
218 ASSERT_EQ(2u, params->objects.size());
219 EXPECT_EQ(1, params->objects[0]->additional_properties["val"]);
220 EXPECT_EQ(2, params->objects[1]->additional_properties["val"]);
223 TEST(JsonSchemaCompilerArrayTest, RefArrayParamsCreate) {
224 scoped_ptr<base::ListValue> params_value(new base::ListValue());
225 scoped_ptr<base::ListValue> item_array(new base::ListValue());
226 item_array->Append(CreateItemValue(1));
227 item_array->Append(CreateItemValue(2));
228 params_value->Append(item_array.release());
229 scoped_ptr<RefArray::Params> params(
230 RefArray::Params::Create(*params_value));
231 EXPECT_TRUE(params.get());
232 ASSERT_EQ(2u, params->refs.size());
233 EXPECT_EQ(1, params->refs[0]->val);
234 EXPECT_EQ(2, params->refs[1]->val);
237 TEST(JsonSchemaCompilerArrayTest, ReturnIntegerArrayResultCreate) {
238 std::vector<int> integers;
239 integers.push_back(1);
240 integers.push_back(2);
241 scoped_ptr<base::ListValue> results =
242 ReturnIntegerArray::Results::Create(integers);
244 base::ListValue expected;
245 base::ListValue* expected_argument = new base::ListValue();
246 expected_argument->Append(new base::FundamentalValue(1));
247 expected_argument->Append(new base::FundamentalValue(2));
248 expected.Append(expected_argument);
249 EXPECT_TRUE(results->Equals(&expected));
252 TEST(JsonSchemaCompilerArrayTest, ReturnRefArrayResultCreate) {
253 std::vector<linked_ptr<Item> > items;
254 items.push_back(linked_ptr<Item>(new Item()));
255 items.push_back(linked_ptr<Item>(new Item()));
256 items[0]->val = 1;
257 items[1]->val = 2;
258 scoped_ptr<base::ListValue> results =
259 ReturnRefArray::Results::Create(items);
261 base::ListValue expected;
262 base::ListValue* expected_argument = new base::ListValue();
263 base::DictionaryValue* first = new base::DictionaryValue();
264 first->SetInteger("val", 1);
265 expected_argument->Append(first);
266 base::DictionaryValue* second = new base::DictionaryValue();
267 second->SetInteger("val", 2);
268 expected_argument->Append(second);
269 expected.Append(expected_argument);
270 EXPECT_TRUE(results->Equals(&expected));