File indexing completed on 2022-10-04 14:00:22

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 #include <QTest>
0007 #include <QDebug>
0008 #include "GeoDataDocument.h"
0009 #include "GeoDataFolder.h"
0010 #include "GeoDataLinearRing.h"
0011 #include "GeoDataLineString.h"
0012 #include "GeoDataPlacemark.h"
0013 #include "GeoDataCoordinates.h"
0014 #include "GeoDataLatLonAltBox.h"
0015 #include "GeoDataTypes.h"
0016 #include "GeoDataStyle.h"
0017 #include "GeoDataIconStyle.h"
0018 #include "GeoDataStyleMap.h"
0019 #include "MarbleDebug.h"
0020 
0021 namespace Marble
0022 {
0023 
0024 class TestGeoData : public QObject
0025 {
0026     Q_OBJECT
0027 private Q_SLOTS:
0028     void nodeTypeTest();
0029     void parentingTest();
0030     void testCast();
0031 };
0032 
0033 /// test the nodeType function through various construction tests
0034 void TestGeoData::nodeTypeTest()
0035 {
0036     /// basic testing of nodeType
0037     GeoDataFolder *folder = new GeoDataFolder;
0038     const char* folderType = GeoDataTypes::GeoDataFolderType;
0039     QCOMPARE( folder->nodeType(), folderType );
0040 
0041     /// testing the nodeType of an object appended to a container
0042     GeoDataDocument document;
0043     document.append( folder );
0044     GeoDataFeature &featureRef = document.last();
0045     QVERIFY(geodata_cast<GeoDataFolder>(&featureRef));
0046 }
0047 
0048 void TestGeoData::parentingTest()
0049 {
0050     GeoDataDocument *document = new GeoDataDocument;
0051     GeoDataFolder *folder = new GeoDataFolder;
0052 
0053     /// simple parenting test
0054     GeoDataPlacemark *placemark = new GeoDataPlacemark;
0055     placemark->setParent(document);
0056     QCOMPARE(placemark->parent(), document);
0057 
0058     /// simple append and child count test
0059     document->append(placemark);
0060 
0061     /// appending folder to document before feeding folder
0062     document->append(folder);
0063     QCOMPARE(document->size(), 2);
0064 
0065     GeoDataPlacemark *placemark2 = new GeoDataPlacemark;
0066     folder->append(placemark2);
0067     QCOMPARE(folder->size(), 1);
0068 
0069 
0070     /// retrieve child and check it matches placemark
0071     GeoDataPlacemark *placemarkPtr;
0072     QVERIFY(geodata_cast<GeoDataPlacemark>(document->child(0)));
0073     placemarkPtr = static_cast<GeoDataPlacemark*>(document->child(0));
0074     QCOMPARE(placemarkPtr, placemark);
0075 
0076     /// check retrieved placemark matches intented child
0077     int position = document->childPosition(placemarkPtr);
0078     QCOMPARE(position, 0);
0079 
0080     /// retrieve child two and check it matches folder
0081     GeoDataFolder *folderPtr;
0082     QVERIFY(geodata_cast<GeoDataFolder>(document->child(1)));
0083     folderPtr = static_cast<GeoDataFolder*>(document->child(1));
0084     QCOMPARE(folderPtr, folder);
0085 
0086     /// check retrieved folder matches intended child
0087     position = document->childPosition(folderPtr);
0088     QCOMPARE(position, 1);
0089 
0090     /// retrieve child three and check it matches placemark
0091     QCOMPARE(folderPtr->size(), 1);
0092     placemarkPtr = static_cast<GeoDataPlacemark*>(folderPtr->child(0));
0093     QCOMPARE(placemarkPtr->nodeType(), placemark2->nodeType());
0094     QCOMPARE(placemarkPtr, placemark2);
0095 
0096 
0097     /// check retrieved placemark matches intended child
0098     QCOMPARE(folderPtr->childPosition(placemarkPtr), 0);
0099 
0100     /// Set a style
0101     GeoDataIconStyle iconStyle;
0102     iconStyle.setIconPath( "myicon.png" );
0103     GeoDataStyle::Ptr style(new GeoDataStyle);
0104     style->setId( "mystyle" );
0105     style->setIconStyle( iconStyle );
0106     GeoDataObject* noParent = nullptr;
0107     QCOMPARE( style->parent(), noParent );
0108     QCOMPARE( iconStyle.parent(), noParent );
0109     document->setStyle( style );
0110     QCOMPARE( style->parent(), document ); // Parent should be assigned now
0111     QCOMPARE( style->iconStyle().parent(), style.data() );
0112     QCOMPARE( iconStyle.parent(), noParent ); // setIconStyle copies
0113     QCOMPARE( placemark->style()->parent(), noParent );
0114     placemark->setStyle( style );
0115     QCOMPARE( placemark->style()->parent(), placemark ); // Parent should be assigned now
0116 
0117     /// Set a style map
0118     GeoDataStyleMap* styleMap = new GeoDataStyleMap;
0119     styleMap->setId( "mystylemap" );
0120     styleMap->insert( "normal", "#mystyle" );
0121     styleMap->insert( "highlight", "#mystyle" );
0122     document->addStyle( style );
0123     document->setStyleMap( styleMap );
0124     QCOMPARE( placemark2->style()->parent(), noParent );
0125     placemark2->setStyleUrl( "#mystyle" );
0126     QCOMPARE( placemark2->style()->parent(), document ); // Parent is document, not placemark2
0127     QCOMPARE( iconStyle.iconPath(), QString( "myicon.png" ) );
0128     QCOMPARE( placemark2->style()->iconStyle().iconPath(), QString( "myicon.png" ) );
0129 }
0130 
0131 void TestGeoData::testCast()
0132 {
0133     GeoDataLineString obj;
0134     GeoDataObject *base = &obj;
0135     QCOMPARE(geodata_cast<GeoDataObject>(base), nullptr);
0136     QCOMPARE(geodata_cast<GeoDataGeometry>(base), nullptr);
0137     QCOMPARE(geodata_cast<GeoDataLineString>(base), &obj);
0138     QCOMPARE(geodata_cast<GeoDataLinearRing>(base), nullptr);
0139     QCOMPARE(geodata_cast<GeoDataPlacemark>(base), nullptr);
0140 
0141     const GeoDataObject *cbase = &obj;
0142     QCOMPARE(geodata_cast<GeoDataObject>(cbase), nullptr);
0143     QCOMPARE(geodata_cast<GeoDataGeometry>(cbase), nullptr);
0144     QCOMPARE(geodata_cast<GeoDataLineString>(cbase), &obj);
0145     QCOMPARE(geodata_cast<GeoDataLinearRing>(cbase), nullptr);
0146     QCOMPARE(geodata_cast<GeoDataPlacemark>(cbase), nullptr);
0147 
0148     QCOMPARE(geodata_cast<GeoDataPlacemark>(static_cast<GeoDataObject*>(nullptr)), nullptr);
0149 }
0150 
0151 }
0152 
0153 QTEST_MAIN( Marble::TestGeoData )
0154 
0155 #include "TestGeoData.moc"