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/simple_api.h"
6 #include "tools/json_schema_compiler/test/crossref.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using namespace test::api
;
14 scoped_ptr
<base::DictionaryValue
> CreateTestTypeValue() {
15 base::DictionaryValue
* value(new base::DictionaryValue());
16 value
->Set("number", new base::FundamentalValue(1.1));
17 value
->Set("integer", new base::FundamentalValue(4));
18 value
->Set("string", new base::StringValue("bling"));
19 value
->Set("boolean", new base::FundamentalValue(true));
20 return scoped_ptr
<base::DictionaryValue
>(value
);
25 TEST(JsonSchemaCompilerCrossrefTest
, CrossrefTypePopulateAndToValue
) {
26 base::DictionaryValue crossref_orig
;
27 crossref_orig
.Set("testType", CreateTestTypeValue().release());
28 crossref_orig
.Set("testEnumRequired", new base::StringValue("one"));
29 crossref_orig
.Set("testEnumOptional", new base::StringValue("two"));
31 // Test Populate of the value --> compiled type.
32 crossref::CrossrefType crossref_type
;
33 ASSERT_TRUE(crossref::CrossrefType::Populate(crossref_orig
, &crossref_type
));
34 EXPECT_EQ(1.1, crossref_type
.test_type
.number
);
35 EXPECT_EQ(4, crossref_type
.test_type
.integer
);
36 EXPECT_EQ("bling", crossref_type
.test_type
.string
);
37 EXPECT_EQ(true, crossref_type
.test_type
.boolean
);
38 EXPECT_EQ(simple_api::TEST_ENUM_ONE
, crossref_type
.test_enum_required
);
39 EXPECT_EQ(simple_api::TEST_ENUM_TWO
, crossref_type
.test_enum_optional
);
40 EXPECT_EQ(simple_api::TEST_ENUM_NONE
, crossref_type
.test_enum_optional_extra
);
42 // Test ToValue of the compiled type --> value.
43 scoped_ptr
<base::DictionaryValue
> crossref_value
= crossref_type
.ToValue();
44 ASSERT_TRUE(crossref_value
);
45 EXPECT_TRUE(crossref_orig
.Equals(crossref_value
.get()));
48 TEST(JsonSchemaCompilerCrossrefTest
, TestTypeOptionalParamCreate
) {
49 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
50 params_value
->Append(CreateTestTypeValue().release());
51 scoped_ptr
<crossref::TestTypeOptionalParam::Params
> params(
52 crossref::TestTypeOptionalParam::Params::Create(*params_value
));
53 EXPECT_TRUE(params
.get());
54 EXPECT_TRUE(params
->test_type
.get());
56 CreateTestTypeValue()->Equals(params
->test_type
->ToValue().get()));
59 TEST(JsonSchemaCompilerCrossrefTest
, TestTypeOptionalParamFail
) {
60 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
61 scoped_ptr
<base::DictionaryValue
> test_type_value
= CreateTestTypeValue();
62 test_type_value
->RemoveWithoutPathExpansion("number", NULL
);
63 params_value
->Append(test_type_value
.release());
64 scoped_ptr
<crossref::TestTypeOptionalParam::Params
> params(
65 crossref::TestTypeOptionalParam::Params::Create(*params_value
));
66 EXPECT_FALSE(params
.get());
69 TEST(JsonSchemaCompilerCrossrefTest
, GetTestType
) {
70 scoped_ptr
<base::DictionaryValue
> value
= CreateTestTypeValue();
71 scoped_ptr
<simple_api::TestType
> test_type(new simple_api::TestType());
72 EXPECT_TRUE(simple_api::TestType::Populate(*value
, test_type
.get()));
74 scoped_ptr
<base::ListValue
> results
=
75 crossref::GetTestType::Results::Create(*test_type
);
76 base::DictionaryValue
* result_dict
= NULL
;
77 results
->GetDictionary(0, &result_dict
);
78 EXPECT_TRUE(value
->Equals(result_dict
));
81 TEST(JsonSchemaCompilerCrossrefTest
, TestTypeInObjectParamsCreate
) {
83 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
84 scoped_ptr
<base::DictionaryValue
> param_object_value(
85 new base::DictionaryValue());
86 param_object_value
->Set("testType", CreateTestTypeValue().release());
87 param_object_value
->Set("boolean", new base::FundamentalValue(true));
88 params_value
->Append(param_object_value
.release());
89 scoped_ptr
<crossref::TestTypeInObject::Params
> params(
90 crossref::TestTypeInObject::Params::Create(*params_value
));
91 EXPECT_TRUE(params
.get());
92 EXPECT_TRUE(params
->param_object
.test_type
.get());
93 EXPECT_TRUE(params
->param_object
.boolean
);
94 EXPECT_TRUE(CreateTestTypeValue()->Equals(
95 params
->param_object
.test_type
->ToValue().get()));
98 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
99 scoped_ptr
<base::DictionaryValue
> param_object_value(
100 new base::DictionaryValue());
101 param_object_value
->Set("boolean", new base::FundamentalValue(true));
102 params_value
->Append(param_object_value
.release());
103 scoped_ptr
<crossref::TestTypeInObject::Params
> params(
104 crossref::TestTypeInObject::Params::Create(*params_value
));
105 EXPECT_TRUE(params
.get());
106 EXPECT_FALSE(params
->param_object
.test_type
.get());
107 EXPECT_TRUE(params
->param_object
.boolean
);
110 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
111 scoped_ptr
<base::DictionaryValue
> param_object_value(
112 new base::DictionaryValue());
113 param_object_value
->Set("testType", new base::StringValue("invalid"));
114 param_object_value
->Set("boolean", new base::FundamentalValue(true));
115 params_value
->Append(param_object_value
.release());
116 scoped_ptr
<crossref::TestTypeInObject::Params
> params(
117 crossref::TestTypeInObject::Params::Create(*params_value
));
118 EXPECT_FALSE(params
.get());
121 scoped_ptr
<base::ListValue
> params_value(new base::ListValue());
122 scoped_ptr
<base::DictionaryValue
> param_object_value(
123 new base::DictionaryValue());
124 param_object_value
->Set("testType", CreateTestTypeValue().release());
125 params_value
->Append(param_object_value
.release());
126 scoped_ptr
<crossref::TestTypeInObject::Params
> params(
127 crossref::TestTypeInObject::Params::Create(*params_value
));
128 EXPECT_FALSE(params
.get());