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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Utku Aydın <utkuaydin34@gmail.com>
0004 //
0005 
0006 #include <QObject>
0007 #include <QTemporaryFile>
0008 #include <QTest>
0009 #include <QDebug>
0010 #include <QDir>
0011 
0012 #include "GeoSceneDocument.h"
0013 #include "GeoSceneHead.h"
0014 #include "GeoSceneZoom.h"
0015 #include "GeoSceneIcon.h"
0016 #include "GeoSceneMap.h"
0017 #include "GeoSceneLayer.h"
0018 #include "GeoSceneTileDataset.h"
0019 #include "GeoSceneGeodata.h"
0020 #include "GeoSceneSettings.h"
0021 #include "GeoSceneProperty.h"
0022 #include "GeoSceneLegend.h"
0023 #include "GeoSceneSection.h"
0024 #include "GeoSceneItem.h"
0025 #include "GeoSceneVector.h"
0026 #include "DgmlElementDictionary.h"
0027 
0028 #include "GeoWriter.h"
0029 #include "GeoSceneParser.h"
0030 
0031 using namespace Marble;
0032 
0033 class TestGeoSceneWriter : public QObject
0034 {
0035     Q_OBJECT
0036 
0037 private Q_SLOTS:
0038     void initTestCase();
0039     void saveFile_data();
0040     void saveFile();
0041     void saveAndLoad_data();
0042     void saveAndLoad();
0043     void saveAndCompare_data();
0044     void saveAndCompare();
0045 
0046     void writeHeadTag();
0047 private:
0048     QDir dgmlPath;
0049     QMap<QString, QSharedPointer<GeoSceneParser> > parsers;
0050 };
0051 
0052 Q_DECLARE_METATYPE( QSharedPointer<GeoSceneParser> )
0053 
0054 void TestGeoSceneWriter::initTestCase()
0055 {
0056     QStringList dgmlFilters;
0057     dgmlFilters << "*.dgml";
0058 
0059     dgmlPath = QDir( DGML_PATH );
0060     dgmlPath.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot);
0061 
0062     foreach( const QString &dgmlDirname, dgmlPath.entryList() ) {
0063         qDebug() << dgmlDirname;
0064         QDir dataDir(dgmlPath.absoluteFilePath(dgmlDirname));
0065         dataDir.setNameFilters( dgmlFilters );
0066 
0067         //check there are dgml files in the data dir
0068         if( dataDir.count() == 0 ) {
0069             continue;
0070         }
0071 
0072         //test the loading of each file in the data dir
0073         foreach( const QString &filename, dataDir.entryList(dgmlFilters, QDir::Files) ){
0074 
0075             //Add example files
0076             QFile file( dataDir.filePath(filename));
0077 
0078             //Verify file existence
0079             QVERIFY( file.exists() );
0080 
0081             //Make the parsers
0082             GeoSceneParser* parser = new GeoSceneParser( GeoScene_DGML );
0083 
0084             QSharedPointer<GeoSceneParser>parserPointer ( parser );
0085 
0086             //Open the files and verify
0087             QVERIFY( file.open( QIODevice::ReadOnly ) );
0088 
0089             //Parser and verify
0090             QVERIFY2(parser->read(&file), filename.toLatin1().constData());
0091 
0092             parsers.insert( dataDir.filePath(filename), parserPointer );
0093 
0094             //close files
0095             file.close();
0096         }
0097     }
0098 }
0099 
0100 void TestGeoSceneWriter::saveFile_data()
0101 {
0102     QTest::addColumn<QSharedPointer<GeoSceneParser> >( "parser" );
0103 
0104     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator itpoint = parsers.begin();
0105     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator const endpoint = parsers.end();
0106     for (; itpoint != endpoint; ++itpoint ) {
0107         QTest::newRow(itpoint.key().toLatin1().constData()) << itpoint.value();
0108     }
0109 }
0110 
0111 void TestGeoSceneWriter::saveFile()
0112 {
0113     QFETCH( QSharedPointer<GeoSceneParser>, parser );
0114 
0115     //attempt to save a file using the GeoWriter
0116     QTemporaryFile tempFile;
0117 
0118     GeoWriter writer;
0119     writer.setDocumentType( dgml::dgmlTag_nameSpace20 );
0120 
0121     // Open file in right mode
0122     QVERIFY( tempFile.open() );
0123 
0124     QVERIFY( writer.write( &tempFile, (dynamic_cast<GeoSceneDocument*>(parser->activeDocument() ) ) ) );
0125 }
0126 
0127 void TestGeoSceneWriter::saveAndLoad_data()
0128 {
0129     QTest::addColumn<QSharedPointer<GeoSceneParser> >( "parser" );
0130 
0131     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator itpoint = parsers.begin();
0132     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator const endpoint = parsers.end();
0133     for (; itpoint != endpoint; ++itpoint ) {
0134         QTest::newRow(itpoint.key().toLatin1().constData()) << itpoint.value();
0135     }
0136 }
0137 
0138 void TestGeoSceneWriter::saveAndLoad()
0139 {
0140     //Save the file and then verify loading it again
0141     QFETCH( QSharedPointer<GeoSceneParser>, parser );
0142 
0143     QTemporaryFile tempFile;
0144     GeoWriter writer;
0145     writer.setDocumentType( dgml::dgmlTag_nameSpace20 );
0146 
0147     // Open file in right mode
0148     QVERIFY( tempFile.open() );
0149 
0150     QVERIFY( writer.write( &tempFile, (dynamic_cast<GeoSceneDocument*>(parser->activeDocument() ) ) ) );
0151 
0152     GeoSceneParser resultParser( GeoScene_DGML );
0153 
0154     tempFile.reset();
0155     QVERIFY( resultParser.read( &tempFile ) );
0156 }
0157 
0158 void TestGeoSceneWriter::saveAndCompare_data()
0159 {
0160     QTest::addColumn<QSharedPointer<GeoSceneParser> >("parser");
0161     QTest::addColumn<QString>("original");
0162 
0163     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator itpoint = parsers.begin();
0164     QMap<QString, QSharedPointer<GeoSceneParser> >::iterator const endpoint = parsers.end();
0165     for (; itpoint != endpoint; ++itpoint ) {
0166         QTest::newRow(itpoint.key().toLatin1().constData()) << itpoint.value() << itpoint.key();
0167     }
0168 }
0169 
0170 void TestGeoSceneWriter::saveAndCompare()
0171 {
0172     //save the file and compare it to the original
0173     QFETCH( QSharedPointer<GeoSceneParser>, parser );
0174     QFETCH( QString, original );
0175 
0176     //attempt to save a file using the GeoWriter
0177     QTemporaryFile tempFile;
0178 
0179     GeoWriter writer;
0180     //FIXME: a better way to do this?
0181     writer.setDocumentType( dgml::dgmlTag_nameSpace20 );
0182 
0183     // Open file in right mode
0184     QVERIFY( tempFile.open() );
0185 
0186     QVERIFY( writer.write( &tempFile, (dynamic_cast<GeoSceneDocument*>(parser->activeDocument() ) ) ) );
0187 
0188     QFile file( original );
0189     QVERIFY( file.open( QIODevice::ReadOnly ) );
0190     QVERIFY( tempFile.reset() );
0191     QTextStream oldFile( &file );
0192     QTextStream newFile( &tempFile );
0193 
0194     QVERIFY( newFile.readAll().simplified().compare( oldFile.readAll().simplified() ) );
0195 }
0196 
0197 void TestGeoSceneWriter::writeHeadTag()
0198 {
0199     GeoSceneDocument *document = new GeoSceneDocument;
0200     
0201     GeoSceneHead* head = document->head();
0202     head->setName( "Test Map" );
0203     head->setTheme( "testmap" );
0204     head->setTarget( "earth" );
0205     
0206     GeoSceneIcon* icon = document->head()->icon();
0207     icon->setPixmap( "preview.jpg" );
0208     
0209     GeoSceneZoom* zoom = document->head()->zoom();
0210     zoom->setMaximum( 1000 );
0211     zoom->setMaximum( 500 );
0212     zoom->setDiscrete( true );
0213     
0214     GeoSceneTileDataset* texture = new GeoSceneTileDataset( "map" );
0215     texture->setSourceDir( "earth/testmap" );
0216     texture->setFileFormat( "png" );
0217     texture->setTileProjection(GeoSceneAbstractTileProjection::Equirectangular);
0218     texture->addDownloadUrl( QUrl( "http://download.kde.org/marble/map/{x}/{y}/{zoomLevel}" ) );
0219     texture->addDownloadUrl( QUrl( "http://download.google.com/marble/map/{x}/{y}/{zoomLevel}" ) );
0220     texture->addDownloadPolicy( DownloadBrowse, 20 );
0221     texture->addDownloadPolicy( DownloadBulk, 20 );
0222     texture->setMaximumTileLevel( 15 );
0223     texture->setLevelZeroColumns( 2 );
0224     texture->setLevelZeroRows( 2 );
0225     
0226     GeoSceneGeodata* geodata = new GeoSceneGeodata( "cityplacemarks" );
0227     geodata->setSourceFile( "baseplacemarks.kml" );
0228     
0229     GeoSceneLayer* layer = new GeoSceneLayer( "testmap" );
0230     layer->setBackend( "texture" );
0231     layer->addDataset( texture );
0232     
0233     GeoSceneLayer* secondLayer = new GeoSceneLayer( "standardplaces" );
0234     secondLayer->setBackend( "geodata" );
0235     secondLayer->addDataset( geodata );
0236     
0237     GeoSceneLayer* thirdLayer = new GeoSceneLayer( "mwdbii" );
0238     thirdLayer->setBackend( "vector" );
0239     thirdLayer->setRole( "polyline" );
0240     
0241     GeoSceneVector* vector = new GeoSceneVector( "pdiffborder" );
0242     vector->setFeature( "border" );
0243     vector->setFileFormat( "PNT" );
0244     vector->setSourceFile( "earth/mwdbii/PDIFFBORDER.PNT" );
0245     vector->pen().setColor( "#ffe300" );
0246     thirdLayer->addDataset( vector );
0247     
0248     GeoSceneMap* map = document->map();
0249     map->addLayer( layer );
0250     map->addLayer( secondLayer );
0251     map->addLayer( thirdLayer );
0252     
0253     GeoSceneSettings *settings = document->settings();
0254    
0255     GeoSceneProperty *coorGrid = new GeoSceneProperty( "coordinate-grid" );
0256     coorGrid->setValue( true );
0257     coorGrid->setAvailable( true );
0258     settings->addProperty( coorGrid );
0259     
0260     GeoSceneProperty *overviewmap = new GeoSceneProperty( "overviewmap" );
0261     overviewmap->setValue( true );
0262     overviewmap->setAvailable( true );
0263     settings->addProperty( overviewmap );
0264     
0265     GeoSceneProperty *compass = new GeoSceneProperty( "compass" );
0266     compass->setValue( true );
0267     compass->setAvailable( true );
0268     settings->addProperty( compass );
0269     
0270     GeoSceneProperty *scalebar = new GeoSceneProperty( "scalebar" );
0271     scalebar->setValue( true );
0272     scalebar->setAvailable( true );
0273     settings->addProperty( scalebar );
0274     
0275     GeoSceneLegend* legend = document->legend();
0276     GeoSceneSection* section = new GeoSceneSection( "areas" );
0277     section->setHeading( "Areas" );
0278     legend->addSection( section );
0279     
0280     GeoSceneItem* sportsPitch = new GeoSceneItem( "sports_pitch" );
0281     sportsPitch->setText( "Sports pitch" );
0282     GeoSceneIcon* sportsPitchIcon = sportsPitch->icon();
0283     sportsPitchIcon->setPixmap( "maps/earth/testmap/legend/sports_pitch.png" );
0284     section->addItem( sportsPitch );
0285     
0286     GeoSceneItem* sportsCentre = new GeoSceneItem( "sports_centre" );
0287     sportsCentre->setText( "Sports centre" );
0288     GeoSceneIcon* sportsCentreIcon = sportsCentre->icon();
0289     sportsCentreIcon->setColor( "#00FF00" );
0290     section->addItem( sportsCentre );
0291         
0292     QTemporaryFile tempFile;
0293     tempFile.open();
0294     
0295     GeoWriter writer;
0296     writer.setDocumentType( "http://edu.kde.org/marble/dgml/2.0" );
0297     QVERIFY( writer.write( &tempFile, document ) );
0298     
0299     //Parser and verify
0300     GeoSceneParser parser( GeoScene_DGML );
0301     tempFile.reset();
0302     QVERIFY( parser.read( &tempFile ) );
0303 
0304     GeoSceneDocument *document2 = static_cast<GeoSceneDocument*>( parser.activeDocument() );
0305     QTemporaryFile tempFile2;
0306     tempFile2.open();
0307     GeoWriter writer2;
0308     writer2.setDocumentType( "http://edu.kde.org/marble/dgml/2.0" );
0309     QVERIFY( writer2.write( &tempFile2, document2 ) );
0310 
0311     tempFile.reset();
0312     QTextStream file( &tempFile );
0313     tempFile2.reset();
0314     QTextStream file2( &tempFile2 );
0315     QVERIFY( file.readAll().simplified().compare( file2.readAll().simplified() ) );
0316 
0317     delete document;
0318 }
0319 
0320 QTEST_MAIN( TestGeoSceneWriter )
0321 #include "TestGeoSceneWriter.moc"