File indexing completed on 2024-04-14 14:16:35

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Patrick Spendrin <ps_ml@gmx.de>
0004 //
0005 
0006 #include "MarbleDirs.h"
0007 #include "GeoDataParser.h"
0008 #include "GeoDataGeometry.h"
0009 #include "GeoDataDocument.h"
0010 #include "GeoDataPlacemark.h"
0011 #include "GeoDataTypes.h"
0012 #include "GeoWriter.h"
0013 
0014 #include <QCoreApplication>
0015 #include <QString>
0016 #include <QBuffer>
0017 #include <QByteArray>
0018 #include <QDebug>
0019 #include <QTest>
0020 
0021 namespace Marble
0022 {
0023 
0024 class TestGeoDataPack : public QObject
0025 {
0026     Q_OBJECT
0027     private Q_SLOTS:
0028         void initTestCase();
0029         void saveKMLToCache();
0030         void loadKMLFromCache();
0031         void saveCitiesToCache();
0032         void loadCitiesFromCache();
0033 
0034     private:
0035         QString content;
0036         QElapsedTimer timer;
0037 };
0038 
0039 void TestGeoDataPack::initTestCase()
0040 {
0041     MarbleDirs::setMarbleDataPath( DATA_PATH );
0042     MarbleDirs::setMarblePluginPath( PLUGIN_PATH );
0043     timer.start();
0044 
0045     content = QString( 
0046 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
0047 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
0048 "  <Document>\n"
0049 "    <Placemark>\n"
0050 "      <name>Empty</name>\n"
0051 "    </Placemark>\n"
0052 "    <Placemark>\n"
0053 "      <name>LinearRingTest.kml</name>\n"
0054 "      <Polygon>\n"
0055 "        <outerBoundaryIs>\n"
0056 "          <LinearRing>\n"
0057 "            <coordinates>\n"
0058 "              -122.365662,37.826988,0\n"
0059 "              -122.365202,37.826302,0\n"
0060 "              -122.364581,37.82655,0\n"
0061 "              -122.365038,37.827237,0\n"
0062 "              -122.365662,37.826988,0\n"
0063 "            </coordinates>\n"
0064 "          </LinearRing>\n"
0065 "        </outerBoundaryIs>\n"
0066 "      </Polygon>\n"
0067 "    </Placemark>\n"
0068 "  </Document>\n"
0069 "</kml>" );
0070 }
0071 
0072 void TestGeoDataPack::saveKMLToCache()
0073 {
0074     GeoDataParser parser( GeoData_KML );
0075     
0076     QByteArray array( content.toUtf8() );
0077     QBuffer buffer( &array );
0078     buffer.open( QIODevice::ReadOnly );
0079     if ( !parser.read( &buffer ) ) {
0080         qWarning( "Could not parse data!" );
0081         QFAIL( "Could not parse data!" );
0082         return;
0083     }
0084     GeoDocument* document = parser.releaseDocument();
0085     QVERIFY( document );
0086     qDebug() << " parse Timer " << timer.elapsed();
0087     GeoDataDocument *dataDocument = static_cast<GeoDataDocument*>( document );
0088     QString path = QString( "%1/%2.cache" );
0089     path = path.arg( QCoreApplication::applicationDirPath() );
0090     path = path.arg( QString( "KMLTest" ) );
0091 
0092     QFile cacheFile( path );
0093     if ( cacheFile.open( QIODevice::WriteOnly ) ) {
0094         QDataStream stream ( &cacheFile );
0095         dataDocument->pack( stream );
0096         cacheFile.close();
0097         qDebug( "Saved kml document to cache: %s", path.toLatin1().data() );
0098     }
0099     qDebug() << "write Timer " << timer.elapsed();
0100     delete document;
0101 }
0102 
0103 void TestGeoDataPack::loadKMLFromCache()
0104 {
0105     GeoDataDocument *cacheDocument = new GeoDataDocument();
0106     QString path = QString( "%1/%2.cache" );
0107     path = path.arg( QCoreApplication::applicationDirPath() );
0108     path = path.arg( QString( "KMLTest" ) );
0109 
0110     QFile cacheFile( path );
0111     if ( cacheFile.open( QIODevice::ReadOnly ) ) {
0112         QDataStream stream ( &cacheFile );
0113         cacheDocument->unpack( stream );
0114         cacheFile.close();
0115         qDebug( "Loaded kml document from cache: %s", path.toLatin1().data() );
0116     }
0117     QVERIFY( cacheDocument );
0118     qDebug() << "read Timer " << timer.elapsed();
0119 
0120     GeoDataParser parser( GeoData_KML );
0121     QByteArray array( content.toUtf8() );
0122     QBuffer buffer( &array );
0123     buffer.open( QIODevice::ReadOnly );
0124     if ( !parser.read( &buffer ) ) {
0125         qWarning( "Could not parse data!" );
0126         QFAIL( "Could not parse data!" );
0127         return;
0128     }
0129     GeoDocument* document = parser.releaseDocument();
0130     QVERIFY( document );
0131     qDebug() << "parse Timer " << timer.elapsed();
0132     GeoDataDocument *dataDocument = static_cast<GeoDataDocument*>( document );
0133     QCOMPARE(*cacheDocument, *dataDocument);
0134     qDebug() << "compare Timer " << timer.elapsed();
0135 
0136     delete document;
0137     delete cacheDocument;
0138 }
0139 
0140 void TestGeoDataPack::saveCitiesToCache()
0141 {
0142     GeoDataParser parser( GeoData_KML );
0143     
0144     QFile citiesFile( CITIES_PATH );
0145     citiesFile.open( QIODevice::ReadOnly );
0146     if ( !parser.read( &citiesFile ) ) {
0147         qWarning( "Could not parse data!" );
0148         QFAIL( "Could not parse data!" );
0149         return;
0150     }
0151     GeoDocument* document = parser.releaseDocument();
0152     QVERIFY( document );
0153     qDebug() << "read Timer " << timer.elapsed();
0154     GeoDataDocument *dataDocument = static_cast<GeoDataDocument*>( document );
0155     QString path = QString( "%1/%2.cache" );
0156     path = path.arg( QCoreApplication::applicationDirPath() );
0157     path = path.arg( QString( "CitiesTest" ) );
0158 
0159     QFile cacheFile( path );
0160     if ( cacheFile.open( QIODevice::WriteOnly ) ) {
0161         QDataStream stream ( &cacheFile );
0162         dataDocument->pack( stream );
0163         cacheFile.close();
0164         qDebug( "Saved kml document to cache: %s", path.toLatin1().data() );
0165     }
0166     QVERIFY( cacheFile.size() > 0 );
0167     qDebug() << "write Timer " << timer.elapsed();
0168     delete document;
0169 }
0170 
0171 void TestGeoDataPack::loadCitiesFromCache()
0172 {
0173     GeoDataDocument *cacheDocument = new GeoDataDocument();
0174     QString path = QString( "%1/%2.cache" );
0175     path = path.arg( QCoreApplication::applicationDirPath() );
0176     path = path.arg( QString( "CitiesTest" ) );
0177 
0178     QFile cacheFile( path );
0179     if ( cacheFile.open( QIODevice::ReadOnly ) ) {
0180         QDataStream stream ( &cacheFile );
0181         cacheDocument->unpack( stream );
0182         cacheFile.close();
0183         qDebug( "Loaded kml document from cache: %s", path.toLatin1().data() );
0184     }
0185     QVERIFY( cacheDocument );
0186     qDebug() << "read Timer " << timer.elapsed();
0187 
0188     GeoDataParser parser( GeoData_KML );
0189     QFile citiesFile( CITIES_PATH );
0190     citiesFile.open( QIODevice::ReadOnly );
0191     if ( !parser.read( &citiesFile ) ) {
0192         qWarning( "Could not parse data!" );
0193         QFAIL( "Could not parse data!" );
0194         return;
0195     }
0196     GeoDocument* document = parser.releaseDocument();
0197     QVERIFY( document );
0198     qDebug() << "parse Timer " << timer.elapsed();
0199 
0200 // commented out as it timeouts the test on build.kde.org
0201 //    GeoDataDocument *dataDocument = static_cast<GeoDataDocument*>( document );
0202 //    QVERIFY( compareDocuments( cacheDocument, dataDocument ) );
0203 //    qDebug() << "compare Timer " << timer.elapsed();
0204 
0205     delete cacheDocument;
0206 //    delete dataDocument;
0207 }
0208 
0209 }
0210 
0211 QTEST_MAIN( Marble::TestGeoDataPack )
0212 
0213 #include "TestGeoDataPack.moc"