Navigation Menu

Skip to content

Commit

Permalink
Add missing test file
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Aug 5, 2020
1 parent fe5b423 commit c5532d0
Showing 1 changed file with 175 additions and 0 deletions.
175 changes: 175 additions & 0 deletions tests/src/core/testqgsannotationitem.cpp
@@ -0,0 +1,175 @@
/***************************************************************************
testqgsannotationitem.cpp
-----------------------
begin : October 2019
copyright : (C) 2019 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsannotationitem.h"
#include "qgsannotationitemregistry.h"
#include "qgsmultirenderchecker.h"
#include "qgstest.h"
#include "qgsproject.h"
#include "qgsreadwritecontext.h"

#include <QObject>
#include <QPainter>
#include <QImage>
#include <QtTest/QSignalSpy>


//simple item for testing, since some methods in QgsAnnotationItem are pure virtual
class TestItem : public QgsAnnotationItem
{

public:

TestItem() : QgsAnnotationItem( QgsCoordinateReferenceSystem() )
{
}

//implement pure virtual methods
QString type() const override { return QStringLiteral( "test_item" ); }

void render( QgsRenderContext &, QgsFeedback * ) override
{
}

TestItem *clone() override
{
return new TestItem();
}

bool writeXml( QDomElement &, QDomDocument &, const QgsReadWriteContext & ) const override
{
return true;
}
};


class TestQgsAnnotationItem: public QObject
{
Q_OBJECT

private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void registry();

private:

QString mReport;

bool renderCheck( QString testName, QImage &image, int mismatchCount );

};

void TestQgsAnnotationItem::initTestCase()
{
mReport = QStringLiteral( "<h1>Annotation Item Tests</h1>\n" );
}

void TestQgsAnnotationItem::cleanupTestCase()
{
QString myReportFile = QDir::tempPath() + QDir::separator() + "qgistest.html";
QFile myFile( myReportFile );
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
{
QTextStream myQTextStream( &myFile );
myQTextStream << mReport;
myFile.close();
}
}

void TestQgsAnnotationItem::init()
{

}

void TestQgsAnnotationItem::cleanup()
{

}

void TestQgsAnnotationItem::registry()
{
// test QgsAnnotationItemRegistry
QgsAnnotationItemRegistry registry;

// empty registry
QVERIFY( !registry.itemMetadata( QString() ) );
QVERIFY( registry.itemTypes().isEmpty() );
QVERIFY( !registry.createItem( QString() ) );

auto create = []()->QgsAnnotationItem *
{
return new TestItem();
};

auto createFromElement = []( const QDomElement &, const QgsReadWriteContext & )->QgsAnnotationItem *
{
return new TestItem();
};

QSignalSpy spyTypeAdded( &registry, &QgsAnnotationItemRegistry::typeAdded );

QgsAnnotationItemMetadata *metadata = new QgsAnnotationItemMetadata( QStringLiteral( "test_item" ), QStringLiteral( "my type" ), QStringLiteral( "my types" ), create, createFromElement );
QVERIFY( registry.addItemType( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );
QCOMPARE( spyTypeAdded.value( 0 ).at( 0 ).toString(), QStringLiteral( "test_item" ) );
QCOMPARE( spyTypeAdded.value( 0 ).at( 1 ).toString(), QStringLiteral( "my type" ) );
// duplicate type id
QVERIFY( !registry.addItemType( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );

//retrieve metadata
QVERIFY( !registry.itemMetadata( QString() ) );
QCOMPARE( registry.itemMetadata( QStringLiteral( "test_item" ) )->visibleName(), QStringLiteral( "my type" ) );
QCOMPARE( registry.itemMetadata( QStringLiteral( "test_item" ) )->visiblePluralName(), QStringLiteral( "my types" ) );
QCOMPARE( registry.itemTypes().count(), 1 );
QCOMPARE( registry.itemTypes().value( QStringLiteral( "test_item" ) ), QStringLiteral( "my type" ) );
QgsAnnotationItem *item = registry.createItem( QStringLiteral( "test_item" ) );
QVERIFY( item );
QVERIFY( dynamic_cast< TestItem *>( item ) );
delete item;

#if 0
//test populate
QgsAnnotationItemRegistry reg2;
QVERIFY( reg2.itemTypes().isEmpty() );
QVERIFY( reg2.populate() );
QVERIFY( !reg2.itemTypes().isEmpty() );
QVERIFY( !reg2.populate() );
#endif
}

bool TestQgsAnnotationItem::renderCheck( QString testName, QImage &image, int mismatchCount )
{
mReport += "<h2>" + testName + "</h2>\n";
QString myTmpDir = QDir::tempPath() + QDir::separator();
QString myFileName = myTmpDir + testName + ".png";
image.save( myFileName, "PNG" );
QgsRenderChecker myChecker;
myChecker.setControlPathPrefix( QStringLiteral( "annotations" ) );
myChecker.setControlName( "expected_" + testName );
myChecker.setRenderedImage( myFileName );
bool myResultFlag = myChecker.compareImages( testName, mismatchCount );
mReport += myChecker.report();
return myResultFlag;
}


QGSTEST_MAIN( TestQgsAnnotationItem )
#include "testqgsannotationitem.moc"

0 comments on commit c5532d0

Please sign in to comment.