1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #include "kml/dom/schema.h"
27 #include "kml/dom/kml22.h"
28 #include "kml/dom/kml_factory.h"
29 #include "kml/dom/kml_funcs.h"
30 #include "gtest/gtest.h"
31
32 namespace kmldom {
33
34 // <SimpleField>
35 class SimpleFieldTest : public testing::Test {
36 protected:
SetUp()37 virtual void SetUp() {
38 simplefield_ = KmlFactory::GetFactory()->CreateSimpleField();
39 }
40
41 SimpleFieldPtr simplefield_;
42 };
43
TEST_F(SimpleFieldTest,TestType)44 TEST_F(SimpleFieldTest, TestType) {
45 ASSERT_EQ(Type_SimpleField, simplefield_->Type());
46 ASSERT_TRUE(simplefield_->IsA(Type_SimpleField));
47 }
48
TEST_F(SimpleFieldTest,TestDefaults)49 TEST_F(SimpleFieldTest, TestDefaults) {
50 ASSERT_EQ(string(""), simplefield_->get_type());
51 ASSERT_FALSE(simplefield_->has_type());
52 ASSERT_EQ(string(""), simplefield_->get_name());
53 ASSERT_FALSE(simplefield_->has_name());
54 ASSERT_EQ(string(""), simplefield_->get_displayname());
55 ASSERT_FALSE(simplefield_->has_displayname());
56 }
57
TEST_F(SimpleFieldTest,TestSetToDefaultValues)58 TEST_F(SimpleFieldTest, TestSetToDefaultValues) {
59 simplefield_->set_type(simplefield_->get_type());
60 ASSERT_TRUE(simplefield_->has_type());
61 simplefield_->set_name(simplefield_->get_name());
62 ASSERT_TRUE(simplefield_->has_name());
63 simplefield_->set_displayname(simplefield_->get_displayname());
64 ASSERT_TRUE(simplefield_->has_displayname());
65 }
66
TEST_F(SimpleFieldTest,TestSetGetHasClear)67 TEST_F(SimpleFieldTest, TestSetGetHasClear) {
68 string type("tom");
69 simplefield_->set_type(type);
70 ASSERT_TRUE(simplefield_->has_type());
71 ASSERT_TRUE(type == simplefield_->get_type());
72 simplefield_->clear_type();
73
74 string name("tom");
75 simplefield_->set_name(name);
76 ASSERT_TRUE(simplefield_->has_name());
77 ASSERT_TRUE(name == simplefield_->get_name());
78 simplefield_->clear_name();
79
80 string displayname("dick");
81 simplefield_->set_displayname(displayname);
82 ASSERT_TRUE(simplefield_->has_displayname());
83 ASSERT_TRUE(displayname == simplefield_->get_displayname());
84 simplefield_->clear_displayname();
85
86 }
87
88 // <GxSimpleArrayField>
89 class GxSimpleArrayFieldTest : public testing::Test {
90 protected:
SetUp()91 virtual void SetUp() {
92 gx_simplearrayfield_ = KmlFactory::GetFactory()->CreateGxSimpleArrayField();
93 }
94
95 GxSimpleArrayFieldPtr gx_simplearrayfield_;
96 };
97
TEST_F(GxSimpleArrayFieldTest,TestType)98 TEST_F(GxSimpleArrayFieldTest, TestType) {
99 ASSERT_EQ(Type_GxSimpleArrayField, gx_simplearrayfield_->Type());
100 ASSERT_TRUE(gx_simplearrayfield_->IsA(Type_GxSimpleArrayField));
101 }
102
TEST_F(GxSimpleArrayFieldTest,TestDefaults)103 TEST_F(GxSimpleArrayFieldTest, TestDefaults) {
104 ASSERT_EQ(string(""), gx_simplearrayfield_->get_type());
105 ASSERT_FALSE(gx_simplearrayfield_->has_type());
106 ASSERT_EQ(string(""), gx_simplearrayfield_->get_name());
107 ASSERT_FALSE(gx_simplearrayfield_->has_name());
108 ASSERT_EQ(string(""), gx_simplearrayfield_->get_displayname());
109 ASSERT_FALSE(gx_simplearrayfield_->has_displayname());
110 }
111
TEST_F(GxSimpleArrayFieldTest,TestSetToDefaultValues)112 TEST_F(GxSimpleArrayFieldTest, TestSetToDefaultValues) {
113 gx_simplearrayfield_->set_type(gx_simplearrayfield_->get_type());
114 ASSERT_TRUE(gx_simplearrayfield_->has_type());
115 gx_simplearrayfield_->set_name(gx_simplearrayfield_->get_name());
116 ASSERT_TRUE(gx_simplearrayfield_->has_name());
117 gx_simplearrayfield_->set_displayname(gx_simplearrayfield_->get_displayname());
118 ASSERT_TRUE(gx_simplearrayfield_->has_displayname());
119 }
120
TEST_F(GxSimpleArrayFieldTest,TestSetGetHasClear)121 TEST_F(GxSimpleArrayFieldTest, TestSetGetHasClear) {
122 string type("tom");
123 gx_simplearrayfield_->set_type(type);
124 ASSERT_TRUE(gx_simplearrayfield_->has_type());
125 ASSERT_TRUE(type == gx_simplearrayfield_->get_type());
126 gx_simplearrayfield_->clear_type();
127
128 string name("tom");
129 gx_simplearrayfield_->set_name(name);
130 ASSERT_TRUE(gx_simplearrayfield_->has_name());
131 ASSERT_TRUE(name == gx_simplearrayfield_->get_name());
132 gx_simplearrayfield_->clear_name();
133
134 string displayname("dick");
135 gx_simplearrayfield_->set_displayname(displayname);
136 ASSERT_TRUE(gx_simplearrayfield_->has_displayname());
137 ASSERT_TRUE(displayname == gx_simplearrayfield_->get_displayname());
138 gx_simplearrayfield_->clear_displayname();
139
140 }
141
142 // <Schema>
143 class SchemaTest : public testing::Test {
144 protected:
SetUp()145 virtual void SetUp() {
146 schema_ = KmlFactory::GetFactory()->CreateSchema();
147 }
148
149 SchemaPtr schema_;
150 };
151
TEST_F(SchemaTest,TestType)152 TEST_F(SchemaTest, TestType) {
153 ASSERT_EQ(Type_Schema, schema_->Type());
154 ASSERT_TRUE(schema_->IsA(Type_Schema));
155 ASSERT_TRUE(schema_->IsA(Type_Object));
156 }
157
TEST_F(SchemaTest,TestDefaults)158 TEST_F(SchemaTest, TestDefaults) {
159 ASSERT_EQ(string(""), schema_->get_name());
160 ASSERT_FALSE(schema_->has_name());
161 ASSERT_EQ(string(""), schema_->get_id());
162 ASSERT_FALSE(schema_->has_id());
163 }
164
TEST_F(SchemaTest,TestSetToDefaultValues)165 TEST_F(SchemaTest, TestSetToDefaultValues) {
166 schema_->set_name(schema_->get_name());
167 ASSERT_TRUE(schema_->has_name());
168 schema_->set_id(schema_->get_id());
169 ASSERT_TRUE(schema_->has_id());
170 }
171
TEST_F(SchemaTest,TestSetGetHasClear)172 TEST_F(SchemaTest, TestSetGetHasClear) {
173 string name("tom");
174 schema_->set_name(name);
175 ASSERT_TRUE(schema_->has_name());
176 ASSERT_TRUE(name == schema_->get_name());
177 schema_->clear_name();
178
179 string id("dick");
180 schema_->set_id(id);
181 ASSERT_TRUE(schema_->has_id());
182 ASSERT_TRUE(id == schema_->get_id());
183 schema_->clear_id();
184 }
185
TEST_F(SchemaTest,TestArrays)186 TEST_F(SchemaTest, TestArrays) {
187 ASSERT_EQ(static_cast<size_t>(0), schema_->get_simplefield_array_size());
188 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
189 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
190 ASSERT_EQ(static_cast<size_t>(2), schema_->get_simplefield_array_size());
191
192 ASSERT_EQ(static_cast<size_t>(0),
193 schema_->get_gx_simplearrayfield_array_size());
194 schema_->add_gx_simplearrayfield(
195 KmlFactory::GetFactory()->CreateGxSimpleArrayField());
196 schema_->add_gx_simplearrayfield(
197 KmlFactory::GetFactory()->CreateGxSimpleArrayField());
198 ASSERT_EQ(static_cast<size_t>(2),
199 schema_->get_gx_simplearrayfield_array_size());
200 }
201
TEST_F(SchemaTest,TestLists)202 TEST_F(SchemaTest, TestLists) {
203 // Vector is empty.
204 ASSERT_EQ(static_cast<size_t>(0), schema_->get_simplefield_array_size());
205 // Add three <SimpleField> elements:
206 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
207 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
208 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
209 // We have three items in the array:
210 ASSERT_EQ(static_cast<size_t>(3), schema_->get_simplefield_array_size());
211 for (size_t i = 0; i < schema_->get_simplefield_array_size(); ++i) {
212 ASSERT_EQ(Type_SimpleField, schema_->get_simplefield_array_at(i)->Type());
213 }
214 }
215
TEST_F(SchemaTest,TestSerialize)216 TEST_F(SchemaTest, TestSerialize) {
217 schema_->set_id("schema-id");
218 schema_->set_name("schema-name");
219 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
220 schema_->add_simplefield(KmlFactory::GetFactory()->CreateSimpleField());
221
222 string expected(
223 "<Schema id=\"schema-id\" name=\"schema-name\">"
224 "<SimpleField/>"
225 "<SimpleField/>"
226 "</Schema>"
227 );
228 ASSERT_EQ(expected, SerializeRaw(schema_));
229 }
230
231 } // end namespace kmldom
232