1 /*
2 * Copyright (C) 2015-2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/file.h>
18 #include <gtest/gtest.h>
19 #include <fstream>
20 #include "host/libs/location/GpsFix.h"
21 #include "host/libs/location/KmlParser.h"
22 #include "host/libs/location/StringParse.h"
23
24 namespace cuttlefish {
25 namespace {
ParseKmlFile(GpsFixArray * locations,char * text,std::string * error)26 bool ParseKmlFile(GpsFixArray* locations, char* text, std::string* error) {
27 bool result;
28 TemporaryDir myDir;
29 std::string path = std::string(myDir.path) + "/" + "test.kml";
30
31 std::ofstream myfile;
32 myfile.open(path.c_str());
33 myfile << text;
34 myfile.close();
35 result = KmlParser::parseFile(path.c_str(), locations, error);
36 return result;
37 }
38
ParseKmlString(GpsFixArray * locations,char * text,std::string * error)39 bool ParseKmlString(GpsFixArray* locations, char* text, std::string* error) {
40 bool result;
41 result = KmlParser::parseString(text, strlen(text), locations, error);
42 return result;
43 }
44 } // namespace
45
TEST(KmlParser,ParseNonexistentFile)46 TEST(KmlParser, ParseNonexistentFile) {
47 GpsFixArray locations;
48 std::string error;
49 ASSERT_FALSE(KmlParser::parseFile("", &locations, &error));
50 ASSERT_EQ(0U, locations.size());
51 EXPECT_EQ(std::string("KML document not parsed successfully."), error);
52 }
53
54 char kEmptyKmlText[] =
55 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
56 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
57 "</kml>";
TEST(KmlParser,ParseEmptyKmlFile)58 TEST(KmlParser, ParseEmptyKmlFile) {
59 GpsFixArray locations;
60 std::string error;
61 ASSERT_TRUE(ParseKmlFile(&locations, kEmptyKmlText, &error));
62 EXPECT_EQ(0U, locations.size());
63 EXPECT_EQ("", error);
64 }
65
TEST(KmlParser,ParseEmptyKmlString)66 TEST(KmlParser, ParseEmptyKmlString) {
67 GpsFixArray locations;
68 std::string error;
69 ASSERT_TRUE(ParseKmlString(&locations, kEmptyKmlText, &error));
70 EXPECT_EQ(0U, locations.size());
71 EXPECT_EQ("", error);
72 }
73
74 char kValidkmlText[] =
75 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
76 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
77 "<Placemark>"
78 "<name>Simple placemark</name>"
79 "<description>Attached to the ground.</description>"
80 "<Point>"
81 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
82 "</Point>"
83 "</Placemark>"
84 "</kml>";
TEST(KmlParser,ParseValidKmlFile)85 TEST(KmlParser, ParseValidKmlFile) {
86 GpsFixArray locations;
87 std::string error;
88
89 ASSERT_TRUE(ParseKmlFile(&locations, kValidkmlText, &error));
90 EXPECT_EQ(1U, locations.size());
91 EXPECT_EQ("", error);
92 }
93
TEST(KmlParser,ParseValidKmlString)94 TEST(KmlParser, ParseValidKmlString) {
95 GpsFixArray locations;
96 std::string error;
97
98 ASSERT_TRUE(ParseKmlString(&locations, kValidkmlText, &error));
99 EXPECT_EQ(1U, locations.size());
100 EXPECT_EQ("", error);
101 }
102
103 char kValidComplexText[] =
104 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
105 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
106 "<Document>"
107 "<name>KML Samples</name>"
108 "<Style id=\"globeIcon\">"
109 "<IconStyle></IconStyle><LineStyle><width>2</width></LineStyle>"
110 "</Style>"
111 "<Folder>"
112 "<name>Placemarks</name>"
113 "<description>These are just some</description>"
114 "<LookAt>"
115 "<tilt>40.5575073395506</tilt><range>500.6566641072245</range>"
116 "</LookAt>"
117 "<Placemark>"
118 "<name>Tessellated</name>"
119 "<visibility>0</visibility>"
120 "<description>Black line (10 pixels wide), height tracks "
121 "terrain</description>"
122 "<LookAt><longitude>-122.0839597145766</longitude></LookAt>"
123 "<styleUrl>#downArrowIcon</styleUrl>"
124 "<Point>"
125 "<altitudeMode>relativeToGround</altitudeMode>"
126 "<coordinates>-122.084075,37.4220033612141,50</coordinates>"
127 "</Point>"
128 "</Placemark>"
129 "<Placemark>"
130 "<name>Transparent</name>"
131 "<visibility>0</visibility>"
132 "<styleUrl>#transRedPoly</styleUrl>"
133 "<Polygon>"
134 "<extrude>1</extrude>"
135 "<altitudeMode>relativeToGround</altitudeMode>"
136 "<outerBoundaryIs>"
137 "<LinearRing>"
138 "<coordinates> -122.084075,37.4220033612141,50</coordinates>"
139 "</LinearRing>"
140 "</outerBoundaryIs>"
141 "</Polygon>"
142 "</Placemark>"
143 "</Folder>"
144 "<Placemark>"
145 "<name>Fruity</name>"
146 "<visibility>0</visibility>"
147 "<description><![CDATA[If the <tessellate> tag has a value of "
148 "n]]></description>"
149 "<LookAt><longitude>-112.0822680013139</longitude></LookAt>"
150 "<LineString>"
151 "<tessellate>1</tessellate>"
152 "<coordinates> -122.084075,37.4220033612141,50 </coordinates>"
153 "</LineString>"
154 "</Placemark>"
155 "</Document>"
156 "</kml>";
TEST(KmlParser,ParseValidComplexFile)157 TEST(KmlParser, ParseValidComplexFile) {
158 GpsFixArray locations;
159 std::string error;
160 ASSERT_TRUE(ParseKmlFile(&locations, kValidComplexText, &error));
161
162 EXPECT_EQ("", error);
163 EXPECT_EQ(3U, locations.size());
164
165 EXPECT_EQ("Tessellated", locations[0].name);
166 EXPECT_EQ("Black line (10 pixels wide), height tracks terrain",
167 locations[0].description);
168 EXPECT_EQ("Transparent", locations[1].name);
169 EXPECT_EQ("", locations[1].description);
170 EXPECT_EQ("Fruity", locations[2].name);
171 EXPECT_EQ("If the <tessellate> tag has a value of n",
172 locations[2].description);
173
174 for (unsigned i = 0; i < locations.size(); ++i) {
175 EXPECT_FLOAT_EQ(-122.084075, locations[i].longitude);
176 EXPECT_FLOAT_EQ(37.4220033612141, locations[i].latitude);
177 EXPECT_FLOAT_EQ(50, locations[i].elevation);
178 }
179 }
180
TEST(KmlParser,ParseValidComplexString)181 TEST(KmlParser, ParseValidComplexString) {
182 GpsFixArray locations;
183 std::string error;
184 ASSERT_TRUE(ParseKmlString(&locations, kValidComplexText, &error));
185
186 EXPECT_EQ("", error);
187 EXPECT_EQ(3U, locations.size());
188
189 EXPECT_EQ("Tessellated", locations[0].name);
190 EXPECT_EQ("Black line (10 pixels wide), height tracks terrain",
191 locations[0].description);
192 EXPECT_EQ("Transparent", locations[1].name);
193 EXPECT_EQ("", locations[1].description);
194 EXPECT_EQ("Fruity", locations[2].name);
195 EXPECT_EQ("If the <tessellate> tag has a value of n",
196 locations[2].description);
197
198 for (unsigned i = 0; i < locations.size(); ++i) {
199 EXPECT_FLOAT_EQ(-122.084075, locations[i].longitude);
200 EXPECT_FLOAT_EQ(37.4220033612141, locations[i].latitude);
201 EXPECT_FLOAT_EQ(50, locations[i].elevation);
202 }
203 }
204
205 char kOneCoordinateText[] =
206 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
207 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
208 "<Placemark>"
209 "<Point>"
210 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
211 "</Point>"
212 "</Placemark>"
213 "</kml>";
TEST(KmlParser,ParseOneCoordinateFile)214 TEST(KmlParser, ParseOneCoordinateFile) {
215 GpsFixArray locations;
216 std::string error;
217 ASSERT_TRUE(ParseKmlFile(&locations, kOneCoordinateText, &error));
218
219 EXPECT_EQ("", error);
220 EXPECT_EQ(1U, locations.size());
221 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
222 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
223 EXPECT_FLOAT_EQ(0.0, locations[0].elevation);
224 }
225
TEST(KmlParser,ParseOneCoordinateString)226 TEST(KmlParser, ParseOneCoordinateString) {
227 GpsFixArray locations;
228 std::string error;
229 ASSERT_TRUE(ParseKmlString(&locations, kOneCoordinateText, &error));
230
231 EXPECT_EQ("", error);
232 EXPECT_EQ(1U, locations.size());
233 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
234 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
235 EXPECT_FLOAT_EQ(0.0, locations[0].elevation);
236 }
237
238 char kMultipleCoordinatesText[] =
239 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
240 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
241 "<Placemark>"
242 "<LineString>"
243 "<coordinates>-122.0822035425683,37.42228990140251,0 \
244 10.4,39.,20\t\t0,21.4,1"
245 "</coordinates>"
246 "</LineString>"
247 "</Placemark>"
248 "</kml>";
TEST(KmlParser,ParseMultipleCoordinatesFile)249 TEST(KmlParser, ParseMultipleCoordinatesFile) {
250 GpsFixArray locations;
251 std::string error;
252 ASSERT_TRUE(ParseKmlFile(&locations, kMultipleCoordinatesText, &error));
253
254 EXPECT_EQ("", error);
255 ASSERT_EQ(3U, locations.size());
256
257 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
258 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
259 EXPECT_FLOAT_EQ(0, locations[0].elevation);
260 EXPECT_FLOAT_EQ(10.4, locations[1].longitude);
261 EXPECT_FLOAT_EQ(39., locations[1].latitude);
262 EXPECT_FLOAT_EQ(20, locations[1].elevation);
263 EXPECT_FLOAT_EQ(0, locations[2].longitude);
264 EXPECT_FLOAT_EQ(21.4, locations[2].latitude);
265 EXPECT_FLOAT_EQ(1, locations[2].elevation);
266 }
267
TEST(KmlParser,ParseMultipleCoordinatesString)268 TEST(KmlParser, ParseMultipleCoordinatesString) {
269 GpsFixArray locations;
270 std::string error;
271 ASSERT_TRUE(ParseKmlString(&locations, kMultipleCoordinatesText, &error));
272
273 EXPECT_EQ("", error);
274 ASSERT_EQ(3U, locations.size());
275
276 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
277 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
278 EXPECT_FLOAT_EQ(0, locations[0].elevation);
279 EXPECT_FLOAT_EQ(10.4, locations[1].longitude);
280 EXPECT_FLOAT_EQ(39., locations[1].latitude);
281 EXPECT_FLOAT_EQ(20, locations[1].elevation);
282 EXPECT_FLOAT_EQ(0, locations[2].longitude);
283 EXPECT_FLOAT_EQ(21.4, locations[2].latitude);
284 EXPECT_FLOAT_EQ(1, locations[2].elevation);
285 }
286
287 char kBadCoordinatesText[] =
288 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
289 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
290 "<Placemark>"
291 "<LineString>"
292 "<coordinates>-122.0822035425683, 37.42228990140251, 0 \
293 10.4,39.20\t021.41"
294 "</coordinates>"
295 "</LineString>"
296 "</Placemark>"
297 "</kml>";
TEST(KmlParser,ParseBadCoordinatesFile)298 TEST(KmlParser, ParseBadCoordinatesFile) {
299 GpsFixArray locations;
300 std::string error;
301 ASSERT_FALSE(ParseKmlFile(&locations, kBadCoordinatesText, &error));
302 }
303
TEST(KmlParser,ParseBadCoordinatesString)304 TEST(KmlParser, ParseBadCoordinatesString) {
305 GpsFixArray locations;
306 std::string error;
307 ASSERT_FALSE(ParseKmlString(&locations, kBadCoordinatesText, &error));
308 }
309
310 char kLocationNormalText[] =
311 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
312 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
313 "<Placemark>"
314 "<name>Simple placemark</name>"
315 "<description>Attached to the ground.</description>"
316 "<Point>"
317 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
318 "</Point>"
319 "</Placemark>"
320 "</kml>";
TEST(KmlParser,ParseLocationNormalFile)321 TEST(KmlParser, ParseLocationNormalFile) {
322 GpsFixArray locations;
323 std::string error;
324
325 ASSERT_TRUE(ParseKmlFile(&locations, kLocationNormalText, &error));
326
327 for (unsigned i = 0; i < locations.size(); ++i) {
328 EXPECT_EQ("Simple placemark", locations[i].name);
329 EXPECT_EQ("Attached to the ground.", locations[i].description);
330 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
331 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
332 EXPECT_FLOAT_EQ(0, locations[i].elevation);
333 }
334 }
335
TEST(KmlParser,ParseLocationNormalString)336 TEST(KmlParser, ParseLocationNormalString) {
337 GpsFixArray locations;
338 std::string error;
339
340 ASSERT_TRUE(ParseKmlString(&locations, kLocationNormalText, &error));
341
342 for (unsigned i = 0; i < locations.size(); ++i) {
343 EXPECT_EQ("Simple placemark", locations[i].name);
344 EXPECT_EQ("Attached to the ground.", locations[i].description);
345 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
346 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
347 EXPECT_FLOAT_EQ(0, locations[i].elevation);
348 }
349 }
350
351 char kLocationMissingFieldsText[] =
352 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
353 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
354 "<Placemark>"
355 "<Point>"
356 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
357 "</Point>"
358 "</Placemark>"
359 "</kml>";
TEST(KmlParser,ParseLocationNormalMissingOptionalFieldsFile)360 TEST(KmlParser, ParseLocationNormalMissingOptionalFieldsFile) {
361 GpsFixArray locations;
362 std::string error;
363
364 ASSERT_TRUE(ParseKmlFile(&locations, kLocationMissingFieldsText, &error));
365 EXPECT_EQ("", error);
366 ASSERT_EQ(1U, locations.size());
367 for (unsigned i = 0; i < locations.size(); ++i) {
368 EXPECT_EQ("", locations[i].name);
369 EXPECT_EQ("", locations[i].description);
370 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
371 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
372 EXPECT_FLOAT_EQ(0, locations[i].elevation);
373 }
374 }
375
TEST(KmlParser,ParseLocationNormalMissingOptionalFieldsString)376 TEST(KmlParser, ParseLocationNormalMissingOptionalFieldsString) {
377 GpsFixArray locations;
378 std::string error;
379
380 ASSERT_TRUE(ParseKmlString(&locations, kLocationMissingFieldsText, &error));
381 EXPECT_EQ("", error);
382 ASSERT_EQ(1U, locations.size());
383 for (unsigned i = 0; i < locations.size(); ++i) {
384 EXPECT_EQ("", locations[i].name);
385 EXPECT_EQ("", locations[i].description);
386 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
387 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
388 EXPECT_FLOAT_EQ(0, locations[i].elevation);
389 }
390 }
391
392 char kLocationMissingRequiredFieldsText[] =
393 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
394 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
395 "<Placemark>"
396 "<name>Simple placemark</name>"
397 "<description>Attached to the ground.</description>"
398 "</Placemark>"
399 "</kml>";
TEST(KmlParser,ParseLocationMissingRequiredFieldsFile)400 TEST(KmlParser, ParseLocationMissingRequiredFieldsFile) {
401 GpsFixArray locations;
402 std::string error;
403
404 ASSERT_FALSE(
405 ParseKmlFile(&locations, kLocationMissingRequiredFieldsText, &error));
406 EXPECT_EQ("Location found with missing or malformed coordinates", error);
407 }
408
TEST(KmlParser,ParseLocationMissingRequiredFieldsString)409 TEST(KmlParser, ParseLocationMissingRequiredFieldsString) {
410 GpsFixArray locations;
411 std::string error;
412
413 ASSERT_FALSE(
414 ParseKmlString(&locations, kLocationMissingRequiredFieldsText, &error));
415 EXPECT_EQ("Location found with missing or malformed coordinates", error);
416 }
417
418 char kLocationNameOnlyFirstText[] =
419 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
420 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
421 "<Placemark>"
422 "<name>Simple placemark</name>kk0"
423 "<description>Attached to the ground.</description>"
424 "<LineString>"
425 "<coordinates>-122.0822035425683,37.42228990140251,0\
426 -122.0822035425683,37.42228990140251,0</coordinates>"
427 "</LineString>"
428 "</Placemark>"
429 "</kml>";
TEST(KmlParser,ParseLocationNameOnlyFirstFile)430 TEST(KmlParser, ParseLocationNameOnlyFirstFile) {
431 GpsFixArray locations;
432 std::string error;
433 EXPECT_TRUE(ParseKmlFile(&locations, kLocationNameOnlyFirstText, &error));
434 EXPECT_EQ("", error);
435 ASSERT_EQ(2U, locations.size());
436
437 EXPECT_EQ("Simple placemark", locations[0].name);
438 EXPECT_EQ("Attached to the ground.", locations[0].description);
439 EXPECT_EQ("", locations[1].name);
440 EXPECT_EQ("", locations[1].description);
441
442 for (unsigned i = 0; i < locations.size(); ++i) {
443 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
444 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
445 EXPECT_FLOAT_EQ(0, locations[i].elevation);
446 }
447 }
448
TEST(KmlParser,ParseLocationNameOnlyFirstString)449 TEST(KmlParser, ParseLocationNameOnlyFirstString) {
450 GpsFixArray locations;
451 std::string error;
452 EXPECT_TRUE(ParseKmlString(&locations, kLocationNameOnlyFirstText, &error));
453 EXPECT_EQ("", error);
454 ASSERT_EQ(2U, locations.size());
455
456 EXPECT_EQ("Simple placemark", locations[0].name);
457 EXPECT_EQ("Attached to the ground.", locations[0].description);
458 EXPECT_EQ("", locations[1].name);
459 EXPECT_EQ("", locations[1].description);
460
461 for (unsigned i = 0; i < locations.size(); ++i) {
462 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
463 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
464 EXPECT_FLOAT_EQ(0, locations[i].elevation);
465 }
466 }
467
468 char kMultipleLocationsText[] =
469 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
470 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
471 "<Placemark>"
472 "<name>Simple placemark</name>"
473 "<description>Attached to the ground.</description>"
474 "<Point>"
475 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
476 "</Point>"
477 "</Placemark>"
478 "<Placemark>"
479 "<name>Simple placemark</name>"
480 "<description>Attached to the ground.</description>"
481 "<Point>"
482 "<coordinates>-122.0822035425683,37.42228990140251,0\
483 -122.0822035425683,37.42228990140251,0</coordinates>"
484 "</Point>"
485 "</Placemark>"
486 "<Placemark>"
487 "<name>Simple placemark</name>"
488 "<description>Attached to the ground.</description>"
489 "<Point>"
490 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
491 "</Point>"
492 "</Placemark>"
493 "</kml>";
TEST(KmlParser,ParseMultipleLocationsFile)494 TEST(KmlParser, ParseMultipleLocationsFile) {
495 GpsFixArray locations;
496 std::string error;
497 EXPECT_TRUE(ParseKmlFile(&locations, kMultipleLocationsText, &error));
498 EXPECT_EQ("", error);
499 ASSERT_EQ(4U, locations.size());
500
501 for (unsigned i = 0; i < locations.size(); ++i) {
502 if (i != 2) {
503 EXPECT_EQ("Simple placemark", locations[i].name);
504 EXPECT_EQ("Attached to the ground.", locations[i].description);
505 } else {
506 EXPECT_EQ("", locations[i].name);
507 EXPECT_EQ("", locations[i].description);
508 }
509 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
510 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
511 EXPECT_FLOAT_EQ(0, locations[i].elevation);
512 }
513 }
514
TEST(KmlParser,ParseMultipleLocationsString)515 TEST(KmlParser, ParseMultipleLocationsString) {
516 GpsFixArray locations;
517 std::string error;
518 EXPECT_TRUE(ParseKmlString(&locations, kMultipleLocationsText, &error));
519 EXPECT_EQ("", error);
520 ASSERT_EQ(4U, locations.size());
521
522 for (unsigned i = 0; i < locations.size(); ++i) {
523 if (i != 2) {
524 EXPECT_EQ("Simple placemark", locations[i].name);
525 EXPECT_EQ("Attached to the ground.", locations[i].description);
526 } else {
527 EXPECT_EQ("", locations[i].name);
528 EXPECT_EQ("", locations[i].description);
529 }
530 EXPECT_FLOAT_EQ(-122.0822035425683, locations[i].longitude);
531 EXPECT_FLOAT_EQ(37.42228990140251, locations[i].latitude);
532 EXPECT_FLOAT_EQ(0, locations[i].elevation);
533 }
534 }
535
536 char kTraverseEmptyDocText[] =
537 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
538 "<kml xmlns=\"http://earth.google.com/kml/2.x\"></kml>";
TEST(KmlParser,TraverseEmptyDocFile)539 TEST(KmlParser, TraverseEmptyDocFile) {
540 GpsFixArray locations;
541 std::string error;
542 EXPECT_TRUE(ParseKmlFile(&locations, kTraverseEmptyDocText, &error));
543 EXPECT_EQ("", error);
544 EXPECT_EQ(0U, locations.size());
545 }
546
TEST(KmlParser,TraverseEmptyDocString)547 TEST(KmlParser, TraverseEmptyDocString) {
548 GpsFixArray locations;
549 std::string error;
550 EXPECT_TRUE(ParseKmlString(&locations, kTraverseEmptyDocText, &error));
551 EXPECT_EQ("", error);
552 EXPECT_EQ(0U, locations.size());
553 }
554
555 char kNoPlacemarksText[] =
556 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
557 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
558 "<LineString></LineString>"
559 "<name></name>"
560 "</kml>";
TEST(KmlParser,TraverseDocNoPlacemarksFile)561 TEST(KmlParser, TraverseDocNoPlacemarksFile) {
562 GpsFixArray locations;
563 std::string error;
564 EXPECT_TRUE(ParseKmlFile(&locations, kNoPlacemarksText, &error));
565 EXPECT_EQ("", error);
566 EXPECT_EQ(0U, locations.size());
567 }
568
TEST(KmlParser,TraverseDocNoPlacemarksString)569 TEST(KmlParser, TraverseDocNoPlacemarksString) {
570 GpsFixArray locations;
571 std::string error;
572 EXPECT_TRUE(ParseKmlString(&locations, kNoPlacemarksText, &error));
573 EXPECT_EQ("", error);
574 EXPECT_EQ(0U, locations.size());
575 }
576
577 char kNestedDocText[] =
578 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
579 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
580 "<Document>"
581 "<Folder>"
582 "<name>Placemarks</name>"
583 "<description>These are just some of the different kinds of placemarks "
584 "with"
585 "which you can mark your favorite places</description>"
586 "<LookAt>"
587 "<longitude>-122.0839597145766</longitude>"
588 "<latitude>37.42222904525232</latitude>"
589 "<altitude>0</altitude>"
590 "<heading>-148.4122922628044</heading>"
591 "<tilt>40.5575073395506</tilt>"
592 "<range>500.6566641072245</range>"
593 "</LookAt>"
594 "<Placemark>"
595 "<name>Simple placemark</name>"
596 "<description>Attached to the ground.</description>"
597 "<Point>"
598 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
599 "</Point>"
600 "</Placemark>"
601 "</Folder>"
602 "</Document>"
603 "</kml>";
TEST(KmlParser,TraverseNestedDocFile)604 TEST(KmlParser, TraverseNestedDocFile) {
605 GpsFixArray locations;
606 std::string error;
607 EXPECT_TRUE(ParseKmlFile(&locations, kNestedDocText, &error));
608 EXPECT_EQ("", error);
609 ASSERT_EQ(1U, locations.size());
610
611 EXPECT_EQ("Simple placemark", locations[0].name);
612 EXPECT_EQ("Attached to the ground.", locations[0].description);
613 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
614 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
615 EXPECT_FLOAT_EQ(0, locations[0].elevation);
616 }
617
TEST(KmlParser,TraverseNestedDocString)618 TEST(KmlParser, TraverseNestedDocString) {
619 GpsFixArray locations;
620 std::string error;
621 EXPECT_TRUE(ParseKmlString(&locations, kNestedDocText, &error));
622 EXPECT_EQ("", error);
623 ASSERT_EQ(1U, locations.size());
624
625 EXPECT_EQ("Simple placemark", locations[0].name);
626 EXPECT_EQ("Attached to the ground.", locations[0].description);
627 EXPECT_FLOAT_EQ(-122.0822035425683, locations[0].longitude);
628 EXPECT_FLOAT_EQ(37.42228990140251, locations[0].latitude);
629 EXPECT_FLOAT_EQ(0, locations[0].elevation);
630 }
631
632 char kNullNameNoCrashText[] =
633 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
634 "<kml xmlns=\"http://earth.google.com/kml/2.x\">"
635 "<Placemark>"
636 "<name/>"
637 "<description/>"
638 "<Point>"
639 "<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"
640 "</Point>"
641 "</Placemark>"
642 "</kml>";
TEST(KmlParser,ParsePlacemarkNullNameNoCrashFile)643 TEST(KmlParser, ParsePlacemarkNullNameNoCrashFile) {
644 GpsFixArray locations;
645 std::string error;
646 EXPECT_TRUE(ParseKmlFile(&locations, kNullNameNoCrashText, &error));
647 ASSERT_EQ(1U, locations.size());
648 EXPECT_STREQ("", locations.front().name.c_str());
649 EXPECT_STREQ("", locations.front().description.c_str());
650 }
651
TEST(KmlParser,ParsePlacemarkNullNameNoCrashString)652 TEST(KmlParser, ParsePlacemarkNullNameNoCrashString) {
653 GpsFixArray locations;
654 std::string error;
655 EXPECT_TRUE(ParseKmlString(&locations, kNullNameNoCrashText, &error));
656 ASSERT_EQ(1U, locations.size());
657 EXPECT_STREQ("", locations.front().name.c_str());
658 EXPECT_STREQ("", locations.front().description.c_str());
659 }
660
661 } // namespace cuttlefish