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.
4
5#include "tools/json_schema_compiler/test/simple_api.h"
6#include "tools/json_schema_compiler/test/crossref.h"
7
8#include "testing/gtest/include/gtest/gtest.h"
9
10using namespace test::api::crossref;
11
12namespace {
13
14static scoped_ptr<base::DictionaryValue> CreateTestTypeDictionary() {
15  base::DictionaryValue* value(new base::DictionaryValue());
16  value->SetWithoutPathExpansion("number", new base::FundamentalValue(1.1));
17  value->SetWithoutPathExpansion("integer", new base::FundamentalValue(4));
18  value->SetWithoutPathExpansion("string", new base::StringValue("bling"));
19  value->SetWithoutPathExpansion("boolean", new base::FundamentalValue(true));
20  return scoped_ptr<base::DictionaryValue>(value);
21}
22
23}  // namespace
24
25TEST(JsonSchemaCompilerCrossrefTest, CrossrefTypePopulate) {
26  scoped_ptr<CrossrefType> crossref_type(new CrossrefType());
27  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
28  value->Set("testType", CreateTestTypeDictionary().release());
29  EXPECT_TRUE(CrossrefType::Populate(*value, crossref_type.get()));
30  EXPECT_TRUE(crossref_type->test_type.get());
31  EXPECT_TRUE(CreateTestTypeDictionary()->Equals(
32        crossref_type->test_type->ToValue().get()));
33  EXPECT_TRUE(value->Equals(crossref_type->ToValue().get()));
34}
35
36TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamCreate) {
37  scoped_ptr<base::ListValue> params_value(new base::ListValue());
38  params_value->Append(CreateTestTypeDictionary().release());
39  scoped_ptr<TestTypeOptionalParam::Params> params(
40      TestTypeOptionalParam::Params::Create(*params_value));
41  EXPECT_TRUE(params.get());
42  EXPECT_TRUE(params->test_type.get());
43  EXPECT_TRUE(
44      CreateTestTypeDictionary()->Equals(params->test_type->ToValue().get()));
45}
46
47TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamFail) {
48  scoped_ptr<base::ListValue> params_value(new base::ListValue());
49  scoped_ptr<base::DictionaryValue> test_type_value =
50      CreateTestTypeDictionary();
51  test_type_value->RemoveWithoutPathExpansion("number", NULL);
52  params_value->Append(test_type_value.release());
53  scoped_ptr<TestTypeOptionalParam::Params> params(
54      TestTypeOptionalParam::Params::Create(*params_value));
55  EXPECT_FALSE(params.get());
56}
57
58TEST(JsonSchemaCompilerCrossrefTest, GetTestType) {
59  scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
60  scoped_ptr<test::api::simple_api::TestType> test_type(
61      new test::api::simple_api::TestType());
62  EXPECT_TRUE(
63      test::api::simple_api::TestType::Populate(*value, test_type.get()));
64
65  scoped_ptr<base::ListValue> results =
66      GetTestType::Results::Create(*test_type);
67  base::DictionaryValue* result_dict = NULL;
68  results->GetDictionary(0, &result_dict);
69  EXPECT_TRUE(value->Equals(result_dict));
70}
71
72TEST(JsonSchemaCompilerCrossrefTest, TestTypeInObjectParamsCreate) {
73  {
74    scoped_ptr<base::ListValue> params_value(new base::ListValue());
75    scoped_ptr<base::DictionaryValue> param_object_value(
76        new base::DictionaryValue());
77    param_object_value->Set("testType", CreateTestTypeDictionary().release());
78    param_object_value->Set("boolean", new base::FundamentalValue(true));
79    params_value->Append(param_object_value.release());
80    scoped_ptr<TestTypeInObject::Params> params(
81        TestTypeInObject::Params::Create(*params_value));
82    EXPECT_TRUE(params.get());
83    EXPECT_TRUE(params->param_object.test_type.get());
84    EXPECT_TRUE(params->param_object.boolean);
85    EXPECT_TRUE(CreateTestTypeDictionary()->Equals(
86          params->param_object.test_type->ToValue().get()));
87  }
88  {
89    scoped_ptr<base::ListValue> params_value(new base::ListValue());
90    scoped_ptr<base::DictionaryValue> param_object_value(
91        new base::DictionaryValue());
92    param_object_value->Set("boolean", new base::FundamentalValue(true));
93    params_value->Append(param_object_value.release());
94    scoped_ptr<TestTypeInObject::Params> params(
95        TestTypeInObject::Params::Create(*params_value));
96    EXPECT_TRUE(params.get());
97    EXPECT_FALSE(params->param_object.test_type.get());
98    EXPECT_TRUE(params->param_object.boolean);
99  }
100  {
101    scoped_ptr<base::ListValue> params_value(new base::ListValue());
102    scoped_ptr<base::DictionaryValue> param_object_value(
103        new base::DictionaryValue());
104    param_object_value->Set("testType", new base::StringValue("invalid"));
105    param_object_value->Set("boolean", new base::FundamentalValue(true));
106    params_value->Append(param_object_value.release());
107    scoped_ptr<TestTypeInObject::Params> params(
108        TestTypeInObject::Params::Create(*params_value));
109    EXPECT_FALSE(params.get());
110  }
111  {
112    scoped_ptr<base::ListValue> params_value(new base::ListValue());
113    scoped_ptr<base::DictionaryValue> param_object_value(
114        new base::DictionaryValue());
115    param_object_value->Set("testType", CreateTestTypeDictionary().release());
116    params_value->Append(param_object_value.release());
117    scoped_ptr<TestTypeInObject::Params> params(
118        TestTypeInObject::Params::Create(*params_value));
119    EXPECT_FALSE(params.get());
120  }
121}
122