| OLD | NEW |
| 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 "components/policy/core/common/schema_internal.h" | 7 #include "components/policy/core/common/schema_internal.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace policy { | 10 namespace policy { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 #define OBJECT_TYPE "\"type\":\"object\"" | 14 #define OBJECT_TYPE "\"type\":\"object\"" |
| 15 | 15 |
| 16 const internal::SchemaNode kTypeBoolean = { base::Value::TYPE_BOOLEAN, NULL, }; | |
| 17 const internal::SchemaNode kTypeInteger = { base::Value::TYPE_INTEGER, NULL, }; | |
| 18 const internal::SchemaNode kTypeNumber = { base::Value::TYPE_DOUBLE, NULL, }; | |
| 19 const internal::SchemaNode kTypeString = { base::Value::TYPE_STRING, NULL, }; | |
| 20 | |
| 21 bool ParseFails(const std::string& content) { | 16 bool ParseFails(const std::string& content) { |
| 22 std::string error; | 17 std::string error; |
| 23 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse(content, &error); | 18 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse(content, &error); |
| 24 if (schema) | 19 if (schema) |
| 25 EXPECT_TRUE(schema->schema().valid()); | 20 EXPECT_TRUE(schema->schema().valid()); |
| 26 else | 21 else |
| 27 EXPECT_FALSE(error.empty()); | 22 EXPECT_FALSE(error.empty()); |
| 28 return !schema; | 23 return !schema; |
| 29 } | 24 } |
| 30 | 25 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 { "abab", base::Value::TYPE_STRING }, | 272 { "abab", base::Value::TYPE_STRING }, |
| 278 { "bb", base::Value::TYPE_NULL }, | 273 { "bb", base::Value::TYPE_NULL }, |
| 279 }; | 274 }; |
| 280 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { | 275 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { |
| 281 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); | 276 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); |
| 282 ASSERT_TRUE(sub.valid()); | 277 ASSERT_TRUE(sub.valid()); |
| 283 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); | 278 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); |
| 284 } | 279 } |
| 285 } | 280 } |
| 286 | 281 |
| 287 TEST(SchemaTest, WrapSimpleNode) { | 282 TEST(SchemaTest, Wrap) { |
| 288 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&kTypeString); | 283 const internal::SchemaNode kSchemas[] = { |
| 289 ASSERT_TRUE(policy_schema); | 284 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node |
| 290 Schema schema = policy_schema->schema(); | 285 { base::Value::TYPE_BOOLEAN, -1 }, // 1 |
| 291 ASSERT_TRUE(schema.valid()); | 286 { base::Value::TYPE_INTEGER, -1 }, // 2 |
| 292 EXPECT_EQ(base::Value::TYPE_STRING, schema.type()); | 287 { base::Value::TYPE_DOUBLE, -1 }, // 3 |
| 293 } | 288 { base::Value::TYPE_STRING, -1 }, // 4 |
| 294 | 289 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. |
| 295 TEST(SchemaTest, WrapDictionary) { | 290 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. |
| 296 const internal::SchemaNode kList = { | |
| 297 base::Value::TYPE_LIST, | |
| 298 &kTypeString, | |
| 299 }; | 291 }; |
| 300 | 292 |
| 301 const internal::PropertyNode kPropertyNodes[] = { | 293 const internal::PropertyNode kPropertyNodes[] = { |
| 302 { "Boolean", &kTypeBoolean }, | 294 { "Boolean", 1 }, |
| 303 { "Integer", &kTypeInteger }, | 295 { "Integer", 2 }, |
| 304 { "List", &kList }, | 296 { "Number", 3 }, |
| 305 { "Number", &kTypeNumber }, | 297 { "String", 4 }, |
| 306 { "String", &kTypeString }, | 298 { "List", 5 }, |
| 307 }; | 299 }; |
| 308 | 300 |
| 309 const internal::PropertiesNode kProperties = { | 301 const internal::PropertiesNode kProperties[] = { |
| 310 kPropertyNodes, | 302 // Properties 0 to 5 (exclusive) are known, from kPropertyNodes. |
| 311 kPropertyNodes + arraysize(kPropertyNodes), | 303 // SchemaNode offset 6 is for additionalProperties (list of lists). |
| 312 NULL, | 304 { 0, 5, 6 }, |
| 313 }; | 305 }; |
| 314 | 306 |
| 315 const internal::SchemaNode root = { | 307 const internal::SchemaData kData = { |
| 316 base::Value::TYPE_DICTIONARY, | 308 kSchemas, |
| 317 &kProperties, | 309 kPropertyNodes, |
| 310 kProperties, |
| 318 }; | 311 }; |
| 319 | 312 |
| 320 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&root); | 313 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&kData); |
| 321 ASSERT_TRUE(policy_schema); | 314 ASSERT_TRUE(policy_schema); |
| 322 Schema schema = policy_schema->schema(); | 315 Schema schema = policy_schema->schema(); |
| 323 ASSERT_TRUE(schema.valid()); | 316 ASSERT_TRUE(schema.valid()); |
| 324 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); | 317 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 325 | 318 |
| 319 struct { |
| 320 const char* key; |
| 321 base::Value::Type type; |
| 322 } kExpectedProperties[] = { |
| 323 { "Boolean", base::Value::TYPE_BOOLEAN }, |
| 324 { "Integer", base::Value::TYPE_INTEGER }, |
| 325 { "Number", base::Value::TYPE_DOUBLE }, |
| 326 { "String", base::Value::TYPE_STRING }, |
| 327 { "List", base::Value::TYPE_LIST }, |
| 328 }; |
| 329 |
| 326 Schema::Iterator it = schema.GetPropertiesIterator(); | 330 Schema::Iterator it = schema.GetPropertiesIterator(); |
| 327 for (size_t i = 0; i < arraysize(kPropertyNodes); ++i) { | 331 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
| 328 ASSERT_FALSE(it.IsAtEnd()); | 332 ASSERT_FALSE(it.IsAtEnd()); |
| 329 EXPECT_STREQ(kPropertyNodes[i].key, it.key()); | 333 EXPECT_STREQ(kExpectedProperties[i].key, it.key()); |
| 330 Schema sub = it.schema(); | 334 Schema sub = it.schema(); |
| 331 ASSERT_TRUE(sub.valid()); | 335 ASSERT_TRUE(sub.valid()); |
| 332 EXPECT_EQ(kPropertyNodes[i].schema->type, sub.type()); | 336 EXPECT_EQ(kExpectedProperties[i].type, sub.type()); |
| 337 |
| 338 if (sub.type() == base::Value::TYPE_LIST) { |
| 339 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 340 Schema items = sub.GetItems(); |
| 341 ASSERT_TRUE(items.valid()); |
| 342 EXPECT_EQ(base::Value::TYPE_STRING, items.type()); |
| 343 } |
| 344 |
| 333 it.Advance(); | 345 it.Advance(); |
| 334 } | 346 } |
| 335 EXPECT_TRUE(it.IsAtEnd()); | 347 EXPECT_TRUE(it.IsAtEnd()); |
| 348 |
| 349 Schema sub = schema.GetAdditionalProperties(); |
| 350 ASSERT_TRUE(sub.valid()); |
| 351 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 352 Schema subsub = sub.GetItems(); |
| 353 ASSERT_TRUE(subsub.valid()); |
| 354 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type()); |
| 355 Schema subsubsub = subsub.GetItems(); |
| 356 ASSERT_TRUE(subsubsub.valid()); |
| 357 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); |
| 336 } | 358 } |
| 337 | 359 |
| 338 } // namespace policy | 360 } // namespace policy |
| OLD | NEW |