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 "base/values.h"
6 #include "tools/json_schema_compiler/test/idl_basics.h"
7 #include "tools/json_schema_compiler/test/idl_object_types.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using test::api::idl_basics::MyType1
;
12 using test::api::idl_object_types::BarType
;
13 using test::api::idl_object_types::FooType
;
15 namespace Function2
= test::api::idl_basics::Function2
;
16 namespace Function3
= test::api::idl_basics::Function3
;
17 namespace Function4
= test::api::idl_basics::Function4
;
18 namespace Function5
= test::api::idl_basics::Function5
;
19 namespace Function6
= test::api::idl_basics::Function6
;
20 namespace Function7
= test::api::idl_basics::Function7
;
21 namespace Function8
= test::api::idl_basics::Function8
;
22 namespace Function9
= test::api::idl_basics::Function9
;
23 namespace Function10
= test::api::idl_basics::Function10
;
24 namespace Function11
= test::api::idl_basics::Function11
;
25 namespace ObjectFunction1
= test::api::idl_object_types::ObjectFunction1
;
27 TEST(IdlCompiler
, Basics
) {
31 a
.y
= std::string("foo");
32 scoped_ptr
<base::DictionaryValue
> serialized
= a
.ToValue();
34 EXPECT_TRUE(MyType1::Populate(*serialized
.get(), &b
));
38 // Test Function2, which takes an integer parameter.
40 list
.Append(new base::FundamentalValue(5));
41 scoped_ptr
<Function2::Params
> f2_params
= Function2::Params::Create(list
);
42 EXPECT_EQ(5, f2_params
->x
);
44 // Test Function3, which takes a MyType1 parameter.
46 base::DictionaryValue
* tmp
= new base::DictionaryValue();
47 tmp
->SetInteger("x", 17);
48 tmp
->SetString("y", "hello");
49 tmp
->SetString("z", "zstring");
50 tmp
->SetString("a", "astring");
51 tmp
->SetString("b", "bstring");
52 tmp
->SetString("c", "cstring");
54 scoped_ptr
<Function3::Params
> f3_params
= Function3::Params::Create(list
);
55 EXPECT_EQ(17, f3_params
->arg
.x
);
56 EXPECT_EQ("hello", f3_params
->arg
.y
);
58 // Test functions that take a callback function as a parameter, with varying
59 // callback signatures.
60 scoped_ptr
<base::ListValue
> f4_results
= Function4::Results::Create();
61 base::ListValue expected
;
62 EXPECT_TRUE(f4_results
->Equals(&expected
));
64 scoped_ptr
<base::ListValue
> f5_results(Function5::Results::Create(13));
65 base::Value
* f5_result_int
= NULL
;
66 ASSERT_TRUE(f5_results
->Get(0, &f5_result_int
));
67 EXPECT_TRUE(f5_result_int
->IsType(base::Value::TYPE_INTEGER
));
69 scoped_ptr
<base::ListValue
> f6_results(Function6::Results::Create(a
));
70 base::Value
* f6_result_dict
= NULL
;
71 ASSERT_TRUE(f6_results
->Get(0, &f6_result_dict
));
73 EXPECT_TRUE(MyType1::Populate(*f6_result_dict
, &c
));
78 TEST(IdlCompiler
, OptionalArguments
) {
79 // Test a function that takes one optional argument, both without and with
82 scoped_ptr
<Function7::Params
> f7_params
= Function7::Params::Create(list
);
83 EXPECT_EQ(NULL
, f7_params
->arg
.get());
84 list
.Append(new base::FundamentalValue(7));
85 f7_params
= Function7::Params::Create(list
);
86 EXPECT_EQ(7, *(f7_params
->arg
));
88 // Similar to above, but a function with one required and one optional
91 list
.Append(new base::FundamentalValue(8));
92 scoped_ptr
<Function8::Params
> f8_params
= Function8::Params::Create(list
);
93 EXPECT_EQ(8, f8_params
->arg1
);
94 EXPECT_EQ(NULL
, f8_params
->arg2
.get());
95 list
.Append(new base::StringValue("foo"));
96 f8_params
= Function8::Params::Create(list
);
97 EXPECT_EQ(8, f8_params
->arg1
);
98 EXPECT_EQ("foo", *(f8_params
->arg2
));
100 // Test a function with an optional argument of custom type.
102 scoped_ptr
<Function9::Params
> f9_params
= Function9::Params::Create(list
);
103 EXPECT_EQ(NULL
, f9_params
->arg
.get());
105 base::DictionaryValue
* tmp
= new base::DictionaryValue();
106 tmp
->SetInteger("x", 17);
107 tmp
->SetString("y", "hello");
108 tmp
->SetString("z", "zstring");
109 tmp
->SetString("a", "astring");
110 tmp
->SetString("b", "bstring");
111 tmp
->SetString("c", "cstring");
113 f9_params
= Function9::Params::Create(list
);
114 ASSERT_TRUE(f9_params
->arg
.get() != NULL
);
115 MyType1
* t1
= f9_params
->arg
.get();
116 EXPECT_EQ(17, t1
->x
);
117 EXPECT_EQ("hello", t1
->y
);
120 TEST(IdlCompiler
, ArrayTypes
) {
121 // Tests of a function that takes an integer and an array of integers. First
122 // use an empty array.
123 base::ListValue list
;
124 list
.Append(new base::FundamentalValue(33));
125 list
.Append(new base::ListValue
);
126 scoped_ptr
<Function10::Params
> f10_params
= Function10::Params::Create(list
);
127 ASSERT_TRUE(f10_params
!= NULL
);
128 EXPECT_EQ(33, f10_params
->x
);
129 EXPECT_TRUE(f10_params
->y
.empty());
131 // Same function, but this time with 2 values in the array.
133 list
.Append(new base::FundamentalValue(33));
134 base::ListValue
* sublist
= new base::ListValue
;
135 sublist
->Append(new base::FundamentalValue(34));
136 sublist
->Append(new base::FundamentalValue(35));
137 list
.Append(sublist
);
138 f10_params
= Function10::Params::Create(list
);
139 ASSERT_TRUE(f10_params
!= NULL
);
140 EXPECT_EQ(33, f10_params
->x
);
141 ASSERT_EQ(2u, f10_params
->y
.size());
142 EXPECT_EQ(34, f10_params
->y
[0]);
143 EXPECT_EQ(35, f10_params
->y
[1]);
145 // Now test a function which takes an array of a defined type.
151 a
.y
= std::string("foo");
152 b
.y
= std::string("bar");
153 base::ListValue
* sublist2
= new base::ListValue
;
154 sublist2
->Append(a
.ToValue().release());
155 sublist2
->Append(b
.ToValue().release());
156 list
.Append(sublist2
);
157 scoped_ptr
<Function11::Params
> f11_params
= Function11::Params::Create(list
);
158 ASSERT_TRUE(f11_params
!= NULL
);
159 ASSERT_EQ(2u, f11_params
->arg
.size());
160 EXPECT_EQ(5, f11_params
->arg
[0]->x
);
161 EXPECT_EQ("foo", f11_params
->arg
[0]->y
);
162 EXPECT_EQ(6, f11_params
->arg
[1]->x
);
163 EXPECT_EQ("bar", f11_params
->arg
[1]->y
);
166 TEST(IdlCompiler
, ObjectTypes
) {
167 // Test the FooType type.
170 scoped_ptr
<base::DictionaryValue
> serialized_foo
= f1
.ToValue();
172 EXPECT_TRUE(FooType::Populate(*serialized_foo
.get(), &f2
));
173 EXPECT_EQ(f1
.x
, f2
.x
);
175 // Test the BarType type.
177 b1
.x
.reset(new base::FundamentalValue(7));
178 scoped_ptr
<base::DictionaryValue
> serialized_bar
= b1
.ToValue();
180 EXPECT_TRUE(BarType::Populate(*serialized_bar
.get(), &b2
));
182 EXPECT_TRUE(b2
.x
->GetAsInteger(&tmp_int
));
183 EXPECT_EQ(7, tmp_int
);
185 // Test the params to the ObjectFunction1 function.
186 scoped_ptr
<base::DictionaryValue
> icon_props(new base::DictionaryValue());
187 icon_props
->SetString("hello", "world");
188 ObjectFunction1::Params::Icon icon
;
189 EXPECT_TRUE(ObjectFunction1::Params::Icon::Populate(*(icon_props
.get()),
191 base::ListValue list
;
192 list
.Append(icon_props
.release());
193 scoped_ptr
<ObjectFunction1::Params
> params
=
194 ObjectFunction1::Params::Create(list
);
195 ASSERT_TRUE(params
.get() != NULL
);
197 EXPECT_TRUE(params
->icon
.additional_properties
.GetString("hello", &tmp
));
198 EXPECT_EQ("world", tmp
);