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 // This file contains the unittests for Model, Location, Orientation, Scale,
27 // ResourceMap and Alias.
28 // TODO: Parse,Serialize tests
29 
30 #include "gtest/gtest.h"
31 #include "kml/dom/kml22.h"
32 #include "kml/dom/kml_factory.h"
33 #include "kml/dom/kml_funcs.h"
34 #include "kml/dom/kml_ptr.h"
35 #include "kml/dom/model.h"
36 
37 namespace kmldom {
38 
39 // This tests the Location class:
40 class LocationTest : public testing::Test {
41  protected:
42   // Called before all tests.
SetUp()43   virtual void SetUp() {
44     location_ = KmlFactory::GetFactory()->CreateLocation();
45   }
46 
47   LocationPtr location_;
48 };
49 
TEST_F(LocationTest,TestType)50 TEST_F(LocationTest, TestType) {
51   ASSERT_EQ(Type_Location, location_->Type());
52   ASSERT_TRUE(location_->IsA(Type_Location));
53   ASSERT_TRUE(location_->IsA(Type_Object));
54 }
55 
56 // Verify proper defaults:
TEST_F(LocationTest,TestDefaults)57 TEST_F(LocationTest, TestDefaults) {
58   ASSERT_FALSE(location_->has_id());
59   ASSERT_FALSE(location_->has_targetid());
60   ASSERT_FALSE(location_->has_longitude());
61   ASSERT_DOUBLE_EQ(0.0, location_->get_longitude());
62   ASSERT_FALSE(location_->has_latitude());
63   ASSERT_DOUBLE_EQ(0.0, location_->get_latitude());
64   ASSERT_FALSE(location_->has_altitude());
65   ASSERT_EQ(ALTITUDEMODE_CLAMPTOGROUND, location_->get_altitude());
66 }
67 
68 // Verify setting default makes has_xxx() true:
TEST_F(LocationTest,TestSetToDefaultValues)69 TEST_F(LocationTest, TestSetToDefaultValues) {
70   // Verify that location_ is in default state:
71   ASSERT_FALSE(location_->has_longitude());
72   ASSERT_FALSE(location_->has_latitude());
73   ASSERT_FALSE(location_->has_altitude());
74   location_->set_longitude(location_->get_longitude());
75   location_->set_latitude(location_->get_latitude());
76   location_->set_altitude(location_->get_altitude());
77   ASSERT_TRUE(location_->has_longitude());
78   ASSERT_TRUE(location_->has_latitude());
79   ASSERT_TRUE(location_->has_altitude());
80 }
81 
82 // Verify set, get, has, clear:
TEST_F(LocationTest,TestSetGetHasClear)83 TEST_F(LocationTest, TestSetGetHasClear) {
84   // Non-default values:
85   const double longitude = 22.33;
86   const double latitude = -54.321;
87   const double altitude = 6543.21;
88 
89   // Set all fields:
90   location_->set_longitude(longitude);
91   location_->set_latitude(latitude);
92   location_->set_altitude(altitude);
93 
94   // Verify getter and has_xxx():
95   ASSERT_TRUE(location_->has_longitude());
96   ASSERT_DOUBLE_EQ(longitude, location_->get_longitude());
97   ASSERT_TRUE(location_->has_latitude());
98   ASSERT_DOUBLE_EQ(latitude, location_->get_latitude());
99   ASSERT_TRUE(location_->has_altitude());
100   ASSERT_DOUBLE_EQ(altitude, location_->get_altitude());
101 
102   // Clear all fields:
103   location_->clear_longitude();
104   location_->clear_latitude();
105   location_->clear_altitude();
106 }
107 
108 // This tests the Orientation class:
109 class OrientationTest : public testing::Test {
110  protected:
SetUp()111   virtual void SetUp() {
112     orientation_ = KmlFactory::GetFactory()->CreateOrientation();
113   }
114 
115   OrientationPtr orientation_;
116 };
117 
TEST_F(OrientationTest,TestType)118 TEST_F(OrientationTest, TestType) {
119   ASSERT_EQ(Type_Orientation, orientation_->Type());
120   ASSERT_TRUE(orientation_->IsA(Type_Orientation));
121   ASSERT_TRUE(orientation_->IsA(Type_Object));
122 }
123 
124 // Verify proper defaults:
TEST_F(OrientationTest,TestDefaults)125 TEST_F(OrientationTest, TestDefaults) {
126   ASSERT_FALSE(orientation_->has_id());
127   ASSERT_FALSE(orientation_->has_targetid());
128   ASSERT_FALSE(orientation_->has_heading());
129   ASSERT_DOUBLE_EQ(0.0, orientation_->get_heading());
130   ASSERT_FALSE(orientation_->has_tilt());
131   ASSERT_DOUBLE_EQ(0.0, orientation_->get_tilt());
132   ASSERT_FALSE(orientation_->has_roll());
133   ASSERT_EQ(ALTITUDEMODE_CLAMPTOGROUND, orientation_->get_roll());
134 }
135 
136 // Verify setting default makes has_xxx() true:
TEST_F(OrientationTest,TestSetToDefaultValues)137 TEST_F(OrientationTest, TestSetToDefaultValues) {
138   // Verify that orientation_ is in default state:
139   ASSERT_FALSE(orientation_->has_heading());
140   ASSERT_FALSE(orientation_->has_tilt());
141   ASSERT_FALSE(orientation_->has_roll());
142   orientation_->set_heading(orientation_->get_heading());
143   orientation_->set_tilt(orientation_->get_tilt());
144   orientation_->set_roll(orientation_->get_roll());
145   ASSERT_TRUE(orientation_->has_heading());
146   ASSERT_TRUE(orientation_->has_tilt());
147   ASSERT_TRUE(orientation_->has_roll());
148 }
149 
150 // Verify set, get, has, clear:
TEST_F(OrientationTest,TestSetGetHasClear)151 TEST_F(OrientationTest, TestSetGetHasClear) {
152   // Non-default values:
153   const double heading = -5.46;
154   const double tilt = 12.45;
155   const double roll = -45.6789;
156 
157   // Set all fields:
158   orientation_->set_heading(heading);
159   orientation_->set_tilt(tilt);
160   orientation_->set_roll(roll);
161 
162   // Verify getter and has_xxx():
163   ASSERT_TRUE(orientation_->has_heading());
164   ASSERT_DOUBLE_EQ(heading, orientation_->get_heading());
165   ASSERT_TRUE(orientation_->has_tilt());
166   ASSERT_DOUBLE_EQ(tilt, orientation_->get_tilt());
167   ASSERT_TRUE(orientation_->has_roll());
168   ASSERT_DOUBLE_EQ(roll, orientation_->get_roll());
169 
170   // Clear all fields:
171   orientation_->clear_heading();
172   orientation_->clear_tilt();
173   orientation_->clear_roll();
174 }
175 
176 // This tests the Scale class:
177 class ScaleTest : public testing::Test {
178  protected:
SetUp()179   virtual void SetUp() {
180     scale_ = KmlFactory::GetFactory()->CreateScale();
181   }
182 
183   ScalePtr scale_;
184 };
185 
TEST_F(ScaleTest,TestType)186 TEST_F(ScaleTest, TestType) {
187   ASSERT_EQ(Type_Scale, scale_->Type());
188   ASSERT_TRUE(scale_->IsA(Type_Scale));
189   ASSERT_TRUE(scale_->IsA(Type_Object));
190 }
191 
192 // Verify proper defaults:
TEST_F(ScaleTest,TestDefaults)193 TEST_F(ScaleTest, TestDefaults) {
194   ASSERT_FALSE(scale_->has_id());
195   ASSERT_FALSE(scale_->has_targetid());
196   ASSERT_FALSE(scale_->has_x());
197   ASSERT_DOUBLE_EQ(1.0, scale_->get_x());
198   ASSERT_FALSE(scale_->has_y());
199   ASSERT_DOUBLE_EQ(1.0, scale_->get_y());
200   ASSERT_FALSE(scale_->has_z());
201   ASSERT_DOUBLE_EQ(1.0, scale_->get_z());
202 }
203 
204 // Verify setting default makes has_xxx() true:
TEST_F(ScaleTest,TestSetToDefaultValues)205 TEST_F(ScaleTest, TestSetToDefaultValues) {
206   // Verify that scale_ in default state:
207   ASSERT_FALSE(scale_->has_x());
208   ASSERT_FALSE(scale_->has_y());
209   ASSERT_FALSE(scale_->has_z());
210   scale_->set_x(scale_->get_x());
211   scale_->set_y(scale_->get_y());
212   scale_->set_z(scale_->get_z());
213   ASSERT_TRUE(scale_->has_x());
214   ASSERT_TRUE(scale_->has_y());
215   ASSERT_TRUE(scale_->has_z());
216 }
217 
218 // Verify set, get, has, clear:
TEST_F(ScaleTest,TestSetGetHasClear)219 TEST_F(ScaleTest, TestSetGetHasClear) {
220   // Non-default values:
221   const double x = 1.23;
222   const double y = -2.41;
223   const double z = 0.4;
224 
225   // Set all fields:
226   scale_->set_x(x);
227   scale_->set_y(y);
228   scale_->set_z(z);
229 
230   // Verify getter and has_xxx():
231   ASSERT_TRUE(scale_->has_x());
232   ASSERT_DOUBLE_EQ(x, scale_->get_x());
233   ASSERT_TRUE(scale_->has_y());
234   ASSERT_DOUBLE_EQ(y, scale_->get_y());
235   ASSERT_TRUE(scale_->has_z());
236   ASSERT_DOUBLE_EQ(z, scale_->get_z());
237 
238   // Clear all fields:
239   scale_->clear_x();
240   scale_->clear_y();
241   scale_->clear_z();
242 }
243 
244 // This tests the Alias class:
245 class AliasTest : public testing::Test {
246  protected:
SetUp()247   virtual void SetUp() {
248     alias_ = KmlFactory::GetFactory()->CreateAlias();
249   }
250 
251   AliasPtr alias_;
252 };
253 
TEST_F(AliasTest,TestType)254 TEST_F(AliasTest, TestType) {
255   ASSERT_EQ(Type_Alias, alias_->Type());
256   ASSERT_TRUE(alias_->IsA(Type_Alias));
257   ASSERT_TRUE(alias_->IsA(Type_Object));
258 }
259 
260 // Verify proper defaults:
TEST_F(AliasTest,TestDefaults)261 TEST_F(AliasTest, TestDefaults) {
262   ASSERT_FALSE(alias_->has_id());
263   ASSERT_FALSE(alias_->has_targetid());
264   ASSERT_FALSE(alias_->has_targethref());
265   ASSERT_EQ(string(""), alias_->get_targethref());
266   ASSERT_FALSE(alias_->has_sourcehref());
267   ASSERT_EQ(string(""), alias_->get_sourcehref());
268 }
269 
270 // Verify setting default makes has_xxx() true:
TEST_F(AliasTest,TestSetToDefaultValues)271 TEST_F(AliasTest, TestSetToDefaultValues) {
272   // Verify that alias_ is in default state:
273   ASSERT_FALSE(alias_->has_targethref());
274   ASSERT_FALSE(alias_->has_sourcehref());
275   alias_->set_targethref(alias_->get_targethref());
276   alias_->set_sourcehref(alias_->get_sourcehref());
277   ASSERT_TRUE(alias_->has_targethref());
278   ASSERT_TRUE(alias_->has_sourcehref());
279 }
280 
281 // Verify set, get, has, clear:
TEST_F(AliasTest,TestSetGetHasClear)282 TEST_F(AliasTest, TestSetGetHasClear) {
283   // Non-default values:
284   const string targethref(
285       "../textures/CU-Macky-Center-StairsnoCulling.jpg");
286   const string sourcehref(
287       "../files/CU-Macky---Center-StairsnoCulling.jpg");
288 
289   // Set all fields:
290   alias_->set_targethref(targethref);
291   alias_->set_sourcehref(sourcehref);
292 
293   // Verify getter and has_xxx():
294   ASSERT_TRUE(alias_->has_targethref());
295   ASSERT_EQ(targethref, alias_->get_targethref());
296   ASSERT_TRUE(alias_->has_sourcehref());
297   ASSERT_EQ(sourcehref, alias_->get_sourcehref());
298 
299   // Clear all fields:
300   alias_->clear_targethref();
301   alias_->clear_sourcehref();
302 }
303 
304 // Verify Serialize.
TEST_F(AliasTest,TestSerialize)305 TEST_F(AliasTest, TestSerialize) {
306   ASSERT_EQ(string("<Alias/>"), SerializeRaw(alias_));
307 }
308 
309 // This tests the ResourceMap class:
310 class ResourceMapTest : public testing::Test {
311  protected:
SetUp()312   virtual void SetUp() {
313     resourcemap_ = KmlFactory::GetFactory()->CreateResourceMap();
314   }
315 
316   ResourceMapPtr resourcemap_;
317 };
318 
TEST_F(ResourceMapTest,TestType)319 TEST_F(ResourceMapTest, TestType) {
320   ASSERT_EQ(Type_ResourceMap, resourcemap_->Type());
321   ASSERT_TRUE(resourcemap_->IsA(Type_ResourceMap));
322   ASSERT_TRUE(resourcemap_->IsA(Type_Object));
323 }
324 
TEST_F(ResourceMapTest,TestDefaults)325 TEST_F(ResourceMapTest, TestDefaults) {
326   ASSERT_FALSE(resourcemap_->has_id());
327   ASSERT_FALSE(resourcemap_->has_targetid());
328   ASSERT_EQ(static_cast<size_t>(0), resourcemap_->get_alias_array_size());
329 }
330 
TEST_F(ResourceMapTest,TestAddAliases)331 TEST_F(ResourceMapTest, TestAddAliases) {
332   // Verify proper initial conditions for this test:
333   ASSERT_EQ(static_cast<size_t>(0), resourcemap_->get_alias_array_size());
334 
335   // Create an Alias:
336   AliasPtr alias = KmlFactory::GetFactory()->CreateAlias();
337   const string targethref0("target0.jpg");
338   const string sourcehref0("source0.jpg");
339   alias->set_targethref(targethref0);
340   alias->set_sourcehref(sourcehref0);
341 
342   // Add it to ResourceMap:
343   resourcemap_->add_alias(alias);
344 
345   // Verify this is the one and only item on the array:
346   ASSERT_EQ(static_cast<size_t>(1), resourcemap_->get_alias_array_size());
347 
348   // Verify the alias is the one we set:
349   ASSERT_EQ(targethref0,
350             resourcemap_->get_alias_array_at(0)->get_targethref());
351   ASSERT_EQ(sourcehref0,
352             resourcemap_->get_alias_array_at(0)->get_sourcehref());
353 
354   // Create another Alias and add to ResourceMap
355   alias = KmlFactory::GetFactory()->CreateAlias();
356   const string targethref1("target1.jpg");
357   const string sourcehref1("source1.jpg");
358   alias->set_targethref(targethref1);
359   alias->set_sourcehref(sourcehref1);
360   resourcemap_->add_alias(alias);
361 
362   // Verify the overall state of the alais array:
363   ASSERT_EQ(static_cast<size_t>(2), resourcemap_->get_alias_array_size());
364   ASSERT_EQ(targethref0,
365             resourcemap_->get_alias_array_at(0)->get_targethref());
366   ASSERT_EQ(sourcehref0,
367             resourcemap_->get_alias_array_at(0)->get_sourcehref());
368   ASSERT_EQ(targethref1,
369             resourcemap_->get_alias_array_at(1)->get_targethref());
370   ASSERT_EQ(sourcehref1,
371             resourcemap_->get_alias_array_at(1)->get_sourcehref());
372 
373   // aliases will be deleted when resourcemap_ is deleted
374 }
375 
376 // This tests the Model class:
377 class ModelTest : public testing::Test {
378  protected:
SetUp()379   virtual void SetUp() {
380     model_ = KmlFactory::GetFactory()->CreateModel();
381   }
382 
383   ModelPtr model_;
384 };
385 
386 // Verify type is correct:
TEST_F(ModelTest,TestType)387 TEST_F(ModelTest, TestType) {
388   ASSERT_EQ(Type_Model, model_->Type());
389   ASSERT_TRUE(model_->IsA(Type_Model));
390   ASSERT_TRUE(model_->IsA(Type_Geometry));
391   ASSERT_TRUE(model_->IsA(Type_Object));
392 }
393 
394 // Verify all child elements are in default state:
TEST_F(ModelTest,TestDefaults)395 TEST_F(ModelTest, TestDefaults) {
396   ASSERT_FALSE(model_->has_id());
397   ASSERT_FALSE(model_->has_targetid());
398   ASSERT_EQ(ALTITUDEMODE_CLAMPTOGROUND, model_->get_altitudemode());
399   ASSERT_EQ(GX_ALTITUDEMODE_CLAMPTOSEAFLOOR, model_->get_gx_altitudemode());
400   ASSERT_FALSE(model_->has_location());
401   ASSERT_FALSE(model_->has_orientation());
402   ASSERT_FALSE(model_->has_scale());
403   ASSERT_FALSE(model_->has_link());
404   ASSERT_FALSE(model_->has_resourcemap());
405 }
406 
407 // Verify set, get, has, clear methods:
TEST_F(ModelTest,TestSetGetHasClear)408 TEST_F(ModelTest, TestSetGetHasClear) {
409   // Set the only simple child to a non-default value:
410   model_->set_altitudemode(ALTITUDEMODE_ABSOLUTE);
411   ASSERT_TRUE(model_->has_altitudemode());
412   ASSERT_EQ(ALTITUDEMODE_ABSOLUTE, model_->get_altitudemode());
413   model_->set_gx_altitudemode(GX_ALTITUDEMODE_RELATIVETOSEAFLOOR);
414   ASSERT_TRUE(model_->has_gx_altitudemode());
415   ASSERT_EQ(GX_ALTITUDEMODE_RELATIVETOSEAFLOOR, model_->get_gx_altitudemode());
416 
417   // Create all possible complex children and give to the Model:
418   model_->set_location(KmlFactory::GetFactory()->CreateLocation());
419   model_->set_orientation(KmlFactory::GetFactory()->CreateOrientation());
420   model_->set_scale(KmlFactory::GetFactory()->CreateScale());
421   model_->set_link(KmlFactory::GetFactory()->CreateLink());
422   model_->set_resourcemap(KmlFactory::GetFactory()->CreateResourceMap());
423   ASSERT_TRUE(model_->has_location());
424   ASSERT_TRUE(model_->has_orientation());
425   ASSERT_TRUE(model_->has_scale());
426   ASSERT_TRUE(model_->has_link());
427   ASSERT_TRUE(model_->has_resourcemap());
428 
429   // Clear all children:
430   model_->clear_altitudemode();
431   model_->clear_gx_altitudemode();
432   model_->clear_location();
433   model_->clear_orientation();
434   model_->clear_scale();
435   model_->clear_link();
436   model_->clear_resourcemap();
437 
438   // Verify Model is now in default state:
439 }
440 
441 // Verify that 2 Models can't take the same Location, Orientation, Scale,
442 // Link or ResourceMap.
443 // (This tests the internal set_parent() method.)
TEST_F(ModelTest,TestSetParent)444 TEST_F(ModelTest, TestSetParent) {
445   KmlFactory* factory = KmlFactory::GetFactory();
446   LocationPtr location = factory->CreateLocation();
447   OrientationPtr orientation = factory->CreateOrientation();
448   ScalePtr scale = factory->CreateScale();
449   LinkPtr link = factory->CreateLink();
450   ResourceMapPtr resourcemap = factory->CreateResourceMap();
451 
452   // Give these all to model_.
453   model_->set_location(location);
454   model_->set_orientation(orientation);
455   model_->set_scale(scale);
456   model_->set_link(link);
457   model_->set_resourcemap(resourcemap);
458 
459   // Try to give these all to another model.
460   ModelPtr model2 = factory->CreateModel();
461   model2->set_location(location);
462   model2->set_orientation(orientation);
463   model2->set_scale(scale);
464   model2->set_link(link);
465   model2->set_resourcemap(resourcemap);
466 
467   // Verify that model_ has each child.
468   ASSERT_TRUE(model_->has_location());
469   ASSERT_TRUE(model_->has_orientation());
470   ASSERT_TRUE(model_->has_scale());
471   ASSERT_TRUE(model_->has_link());
472   ASSERT_TRUE(model_->has_resourcemap());
473 
474   // Verify that model2 has no children.
475   ASSERT_FALSE(model2->has_location());
476   ASSERT_FALSE(model2->has_orientation());
477   ASSERT_FALSE(model2->has_scale());
478   ASSERT_FALSE(model2->has_link());
479   ASSERT_FALSE(model2->has_resourcemap());
480 
481   // smart pointer deletes model_ and the children created here.
482 }
483 
484 }  // end namespace kmldom
485