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/kml_factory.h"
27 #include "kml/dom/kml_cast.h"
28 #include "kml/dom/kmldom.h"
29 #include "gtest/gtest.h"
30 
31 namespace kmldom {
32 
TEST(KmlFactoryTest,TestFactory)33 TEST(KmlFactoryTest, TestFactory) {
34   KmlFactory* factory(KmlFactory::GetFactory());
35 
36   // CreateElementById returns an ElementPtr which automatically
37   // releases the underlying Element storage when it goes
38   // out of scope.
39 
40   // Verify functions used by parser to create any KML element.
41   KmlDomType id = Type_kml;
42 
43   // CreateElementById() new's a Element and returns an ElementPtr
44   // dereferenced here to access Type().  When this expression
45   // goes out of scope ElementPtr's destructor delete's the Element.
46   ASSERT_EQ(Type_kml, factory->CreateElementById(id)->Type());
47 
48   ElementPtr e;
49   e = factory->CreateFieldById(id);
50   ASSERT_EQ(Type_kml, e->Type());
51 
52   // Verify all factory methods return correct type.
53   e = factory->CreateAlias();
54   ASSERT_EQ(Type_Alias, e->Type());
55 
56   e = factory->CreateAtomAuthor();
57   ASSERT_EQ(Type_AtomAuthor, e->Type());
58 
59   e = factory->CreateAtomCategory();
60   ASSERT_EQ(Type_AtomCategory, e->Type());
61 
62   e = factory->CreateAtomContent();
63   ASSERT_EQ(Type_AtomContent, e->Type());
64 
65   e = factory->CreateAtomEntry();
66   ASSERT_EQ(Type_AtomEntry, e->Type());
67 
68   e = factory->CreateAtomFeed();
69   ASSERT_EQ(Type_AtomFeed, e->Type());
70 
71   e = factory->CreateAtomLink();
72   ASSERT_EQ(Type_AtomLink, e->Type());
73 
74   e = factory->CreateBalloonStyle();
75   ASSERT_EQ(Type_BalloonStyle, e->Type());
76 
77   e = factory->CreateCamera();
78   ASSERT_EQ(Type_Camera, e->Type());
79 
80   e = factory->CreateChange();
81   ASSERT_EQ(Type_Change, e->Type());
82 
83   e = factory->CreateCreate();
84   ASSERT_EQ(Type_Create, e->Type());
85 
86   e = factory->CreateCoordinates();
87   ASSERT_EQ(Type_coordinates, e->Type());
88 
89   e = factory->CreateData();
90   ASSERT_EQ(Type_Data, e->Type());
91 
92   e = factory->CreateDelete();
93   ASSERT_EQ(Type_Delete, e->Type());
94 
95   e = factory->CreateDocument();
96   ASSERT_EQ(Type_Document, e->Type());
97 
98   e = factory->CreateExtendedData();
99   ASSERT_EQ(Type_ExtendedData, e->Type());
100 
101   e = factory->CreateFolder();
102   ASSERT_EQ(Type_Folder, e->Type());
103 
104   e = factory->CreateGroundOverlay();
105   ASSERT_EQ(Type_GroundOverlay, e->Type());
106 
107   e = factory->CreateHotSpot();
108   ASSERT_EQ(Type_hotSpot, e->Type());
109 
110   e = factory->CreateIcon();
111   ASSERT_EQ(Type_Icon, e->Type());
112 
113   e = factory->CreateIconStyle();
114   ASSERT_EQ(Type_IconStyle, e->Type());
115 
116   e = factory->CreateIconStyleIcon();
117   ASSERT_EQ(Type_IconStyleIcon, e->Type());
118 
119   e = factory->CreateImagePyramid();
120   ASSERT_EQ(Type_ImagePyramid, e->Type());
121 
122   e = factory->CreateItemIcon();
123   ASSERT_EQ(Type_ItemIcon, e->Type());
124 
125   e = factory->CreateKml();
126   ASSERT_EQ(Type_kml, e->Type());
127 
128   e = factory->CreateLabelStyle();
129   ASSERT_EQ(Type_LabelStyle, e->Type());
130 
131   e = factory->CreateLatLonBox();
132   ASSERT_EQ(Type_LatLonBox, e->Type());
133 
134   e = factory->CreateLatLonAltBox();
135   ASSERT_EQ(Type_LatLonAltBox, e->Type());
136 
137   e = factory->CreateLinearRing();
138   ASSERT_EQ(Type_LinearRing, e->Type());
139 
140   e = factory->CreateLineString();
141   ASSERT_EQ(Type_LineString, e->Type());
142 
143   e = factory->CreateLineStyle();
144   ASSERT_EQ(Type_LineStyle, e->Type());
145 
146   e = factory->CreateLink();
147   ASSERT_EQ(Type_Link, e->Type());
148 
149   e = factory->CreateLinkSnippet();
150   ASSERT_EQ(Type_linkSnippet, e->Type());
151 
152   e = factory->CreateListStyle();
153   ASSERT_EQ(Type_ListStyle, e->Type());
154 
155   e = factory->CreateLocation();
156   ASSERT_EQ(Type_Location, e->Type());
157 
158   e = factory->CreateLod();
159   ASSERT_EQ(Type_Lod, e->Type());
160 
161   e = factory->CreateLookAt();
162   ASSERT_EQ(Type_LookAt, e->Type());
163 
164   e = factory->CreateMetadata();
165   ASSERT_EQ(Type_Metadata, e->Type());
166 
167   e = factory->CreateModel();
168   ASSERT_EQ(Type_Model, e->Type());
169 
170   e = factory->CreateMultiGeometry();
171   ASSERT_EQ(Type_MultiGeometry, e->Type());
172 
173   e = factory->CreateNetworkLink();
174   ASSERT_EQ(Type_NetworkLink, e->Type());
175 
176   e = factory->CreateNetworkLinkControl();
177   ASSERT_EQ(Type_NetworkLinkControl, e->Type());
178 
179   e = factory->CreateOrientation();
180   ASSERT_EQ(Type_Orientation, e->Type());
181 
182   e = factory->CreateOuterBoundaryIs();
183   ASSERT_EQ(Type_outerBoundaryIs, e->Type());
184 
185   e = factory->CreateOverlayXY();
186   ASSERT_EQ(Type_overlayXY, e->Type());
187 
188   e = factory->CreatePair();
189   ASSERT_EQ(Type_Pair, e->Type());
190 
191   e = factory->CreatePhotoOverlay();
192   ASSERT_EQ(Type_PhotoOverlay, e->Type());
193 
194   e = factory->CreatePlacemark();
195   ASSERT_EQ(Type_Placemark, e->Type());
196 
197   e = factory->CreatePolygon();
198   ASSERT_EQ(Type_Polygon, e->Type());
199 
200   e = factory->CreatePoint();
201   ASSERT_EQ(Type_Point, e->Type());
202 
203   e = factory->CreatePolyStyle();
204   ASSERT_EQ(Type_PolyStyle, e->Type());
205 
206   e = factory->CreateRegion();
207   ASSERT_EQ(Type_Region, e->Type());
208 
209   e = factory->CreateResourceMap();
210   ASSERT_EQ(Type_ResourceMap, e->Type());
211 
212   e = factory->CreateRotationXY();
213   ASSERT_EQ(Type_rotationXY, e->Type());
214 
215   e = factory->CreateScale();
216   ASSERT_EQ(Type_Scale, e->Type());
217 
218   e = factory->CreateSchema();
219   ASSERT_EQ(Type_Schema, e->Type());
220 
221   e = factory->CreateSchemaData();
222   ASSERT_EQ(Type_SchemaData, e->Type());
223 
224   e = factory->CreateScreenOverlay();
225   ASSERT_EQ(Type_ScreenOverlay, e->Type());
226 
227   e = factory->CreateScreenXY();
228   ASSERT_EQ(Type_screenXY, e->Type());
229 
230   e = factory->CreateSimpleData();
231   ASSERT_EQ(Type_SimpleData, e->Type());
232 
233   e = factory->CreateSimpleField();
234   ASSERT_EQ(Type_SimpleField, e->Type());
235 
236   e = factory->CreateSize();
237   ASSERT_EQ(Type_size, e->Type());
238 
239   e = factory->CreateSnippet();
240   ASSERT_EQ(Type_Snippet, e->Type());
241 
242   e = factory->CreateStyle();
243   ASSERT_EQ(Type_Style, e->Type());
244 
245   e = factory->CreateStyleMap();
246   ASSERT_EQ(Type_StyleMap, e->Type());
247 
248   e = factory->CreateTimeSpan();
249   ASSERT_EQ(Type_TimeSpan, e->Type());
250 
251   e = factory->CreateTimeStamp();
252   ASSERT_EQ(Type_TimeStamp, e->Type());
253 
254   e = factory->CreateViewVolume();
255   ASSERT_EQ(Type_ViewVolume, e->Type());
256 
257   e = factory->CreateUpdate();
258   ASSERT_EQ(Type_Update, e->Type());
259 
260   e = factory->CreateUrl();
261   ASSERT_EQ(Type_Url, e->Type());
262 
263   e = factory->CreateXalAddressDetails();
264   ASSERT_EQ(Type_XalAddressDetails, e->Type());
265 
266   e = factory->CreateXalAdministrativeArea();
267   ASSERT_EQ(Type_XalAdministrativeArea, e->Type());
268 
269   e = factory->CreateXalCountry();
270   ASSERT_EQ(Type_XalCountry, e->Type());
271 
272   e = factory->CreateXalSubAdministrativeArea();
273   ASSERT_EQ(Type_XalSubAdministrativeArea, e->Type());
274 
275   e = factory->CreateGxAnimatedUpdate();
276   ASSERT_EQ(Type_GxAnimatedUpdate, e->Type());
277 
278   e = factory->CreateGxFlyTo();
279   ASSERT_EQ(Type_GxFlyTo, e->Type());
280 
281   e = factory->CreateGxLatLonQuad();
282   ASSERT_EQ(Type_GxLatLonQuad, e->Type());
283 
284   e = factory->CreateGxMultiTrack();
285   ASSERT_EQ(Type_GxMultiTrack, e->Type());
286 
287   e = factory->CreateGxPlaylist();
288   ASSERT_EQ(Type_GxPlaylist, e->Type());
289 
290   e = factory->CreateGxSimpleArrayData();
291   ASSERT_EQ(Type_GxSimpleArrayData, e->Type());
292 
293   e = factory->CreateGxSimpleArrayField();
294   ASSERT_EQ(Type_GxSimpleArrayField, e->Type());
295 
296   e = factory->CreateGxSoundCue();
297   ASSERT_EQ(Type_GxSoundCue, e->Type());
298 
299   e = factory->CreateGxTimeSpan();
300   ASSERT_EQ(Type_GxTimeSpan, e->Type());
301 
302   e = factory->CreateGxTimeStamp();
303   ASSERT_EQ(Type_GxTimeStamp, e->Type());
304 
305   e = factory->CreateGxTour();
306   ASSERT_EQ(Type_GxTour, e->Type());
307 
308   e = factory->CreateGxTrack();
309   ASSERT_EQ(Type_GxTrack, e->Type());
310 
311   e = factory->CreateGxTourControl();
312   ASSERT_EQ(Type_GxTourControl, e->Type());
313 
314   e = factory->CreateGxWait();
315   ASSERT_EQ(Type_GxWait, e->Type());
316 }
317 
TEST(KmlFactoryTest,TestCreateElementFromName)318 TEST(KmlFactoryTest, TestCreateElementFromName) {
319   KmlFactory* kf = KmlFactory::GetFactory();
320   ASSERT_FALSE(kf->CreateElementFromName(""));
321   ASSERT_FALSE(kf->CreateElementFromName("complete junk"));
322   ASSERT_FALSE(kf->CreateElementFromName("<Placemark"));
323 
324   ASSERT_TRUE(kmldom::AsPlacemark(kf->CreateElementFromName("Placemark")));
325   ASSERT_TRUE(kmldom::AsAtomAuthor(kf->CreateElementFromName("atom:author")));
326   ASSERT_TRUE(kmldom::AsGxTour(kf->CreateElementFromName("gx:Tour")));
327 }
328 
329 }  // end namespace kmldom
330