Skip to content

Commit

Permalink
Added first of gui lib tests (for quickprint)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@8015 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
timlinux committed Jan 21, 2008
1 parent bdcae62 commit 80ef2de
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tests/src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Standard includes and utils to compile into all tests.
SET (util_SRCS ../core/qgsrenderchecker.cpp)


#####################################################
# Don't forget to include output directory, otherwise
# the UI file won't be wrapped!
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../core #for render checker class
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/gui
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/raster
${QT_INCLUDE_DIR}
${GDAL_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
)

#############################################################
# Compiler defines

# expect that classes are being IMPORTED for the tests
IF (WIN32)
# expect that classes are being imported
# Note: MSVC doesn't like when the macros are quotes
# and MSYS doesn't like them unqouted (bacause of braces)
IF (MSVC)
ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)")
#ADD_DEFINITIONS("-DPLUGIN_EXPORT=__declspec(dllimport)")
#next needed for qgis gui includes
ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllexport)")
ELSE (MSVC)
ADD_DEFINITIONS("-UCORE_EXPORT \"-DCORE_EXPORT=__declspec(dllimport)\"")
#ADD_DEFINITIONS("\"-DPLUGIN_EXPORT=__declspec(dllimport)\"")
#next needed for qgis gui includes
ADD_DEFINITIONS("-UGUI_EXPORT \"-DGUI_EXPORT=__declspec(dllexport)\"")
ENDIF (MSVC)
ENDIF (WIN32)

# This define is used for tests that need to locate the test
# data under tests/testdata in the qgis source tree.
# the TEST_DATA_DIR variable is set in the top level CMakeLists.txt
ADD_DEFINITIONS(-DTEST_DATA_DIR="\\"${TEST_DATA_DIR}\\"")
#############################################################
# libraries
# because of htonl
IF (WIN32)
SET(PLATFORM_LIBRARIES wsock32)
ENDIF (WIN32)
# Since the tests are not actually installed, but rather
# run directly from the build/src/tests dir we need to
# ensure the omg libs can be found.
IF (APPLE)
# For Mac OS X, the executable must be at the root of the bundle's executable folder
SET (CMAKE_INSTALL_NAME_DIR @executable_path/../../../src/gui)
ENDIF (APPLE)
#note for tests we should not include the moc of our
#qtests in the executable file list as the moc is
#directly included in the sources
#and should not be compiled twice. Trying to include
#them in will cause an error at build time
#############################################################
# Tests:
#
# QgsQuickPrint test
#
SET(qgis_quickprinttest_SRCS testqgsquickprint.cpp ${util_SRCS})
SET(qgis_quickprinttest_MOC_CPPS testqgsquickprint.cpp)
QT4_WRAP_CPP(qgis_quickprinttest_MOC_SRCS ${qgis_quickprinttest_MOC_CPPS})
ADD_CUSTOM_TARGET(qgis_quickprinttestmoc ALL DEPENDS ${qgis_quickprinttest_MOC_SRCS})
ADD_EXECUTABLE(qgis_quickprinttest ${qgis_quickprinttest_SRCS})
ADD_DEPENDENCIES(qgis_quickprinttest qgis_quickprinttestmoc)
TARGET_LINK_LIBRARIES(qgis_quickprinttest ${QT_LIBRARIES} qgis_core qgis_gui)
SET_TARGET_PROPERTIES(qgis_quickprinttest
PROPERTIES INSTALL_RPATH ${QGIS_LIB_DIR}
INSTALL_RPATH_USE_LINK_PATH true)
IF (APPLE)
# For Mac OS X, the executable must be at the root of the bundle's executable folder
INSTALL(TARGETS qgis_quickprinttest RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
ADD_TEST(qgis_quickprinttest ${CMAKE_INSTALL_PREFIX}/qgis_quickprinttest)
ELSE (APPLE)
INSTALL(TARGETS qgis_quickprinttest RUNTIME DESTINATION ${QGIS_BIN_DIR})
ADD_TEST(qgis_quickprinttest ${QGIS_BIN_DIR}/qgis_quickprinttest)
ENDIF (APPLE)
193 changes: 193 additions & 0 deletions tests/src/gui/testqgsquickprint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/***************************************************************************
testqgsquickprint.cpp
--------------------------------------
Date : 20 Jan 2008
Copyright : (C) 2008 by Tim Sutton
Email : tim @ linfiniti.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 <QtTest>
#include <QObject>
#include <QStringList>
#include <QObject>
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QDesktopServices>

#include <iostream>
//qgis includes...
#include <qgsmaprender.h>
#include <qgsmaplayer.h>
#include <qgsvectorlayer.h>
#include <qgsapplication.h>
#include <qgsproviderregistry.h>
#include <qgsmaplayerregistry.h>
#include <qgsquickprint.h>
//qgis test includes
#include <qgsrenderchecker.h>

/** \ingroup UnitTests
* This is a unit test for the different renderers for vector layers.
*/
class TestQgsQuickPrint: 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 basicMapTest();
QString getQgisPath(); // Gets the path to QGIS installation
private:
bool imageCheck(QString theType); //as above
QgsMapRender * mpMapRenderer;
QgsMapLayer * mpPointsLayer;
QgsMapLayer * mpLinesLayer;
QgsMapLayer * mpPolysLayer;
QString mTestDataDir;
QString mReport;
};

QString TestQgsQuickPrint::getQgisPath()
{
#ifdef Q_OS_LINUX
QString qgisPath = QCoreApplication::applicationDirPath () + "/../";
#else //mac and win
QString qgisPath = QCoreApplication::applicationDirPath () ;
#endif
return qgisPath;
}

void TestQgsQuickPrint::initTestCase()
{
// init QGIS's paths - true means that all path will be inited from prefix
//QString qgisPath = QCoreApplication::applicationDirPath ();
QgsApplication::setPrefixPath(getQgisPath(), TRUE);
#ifdef Q_OS_LINUX
// QgsApplication::setPkgDataPath(qgisPath + "/../share/qgis");
// QgsApplication::setPluginPath(qgisPath + "/../lib/qgis");
#endif
// Instantiate the plugin directory so that providers are loaded
QgsProviderRegistry::instance(QgsApplication::pluginPath());

//create some objects that will be used in all tests...

std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;

//
//create a point layer that will be used in all tests...
//
QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt
mTestDataDir = myDataDir + QDir::separator();
QString myPointsFileName = mTestDataDir + "points.shp";
QFileInfo myPointFileInfo ( myPointsFileName );
mpPointsLayer = new QgsVectorLayer ( myPointFileInfo.filePath(),
myPointFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayer(mpPointsLayer);

//
//create a poly layer that will be used in all tests...
//
QString myPolysFileName = mTestDataDir + "polys.shp";
QFileInfo myPolyFileInfo ( myPolysFileName );
mpPolysLayer = new QgsVectorLayer ( myPolyFileInfo.filePath(),
myPolyFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayer(mpPolysLayer);

//
// Create a line layer that will be used in all tests...
//
QString myLinesFileName = mTestDataDir + "lines.shp";
QFileInfo myLineFileInfo ( myLinesFileName );
mpLinesLayer = new QgsVectorLayer ( myLineFileInfo.filePath(),
myLineFileInfo.completeBaseName(), "ogr" );
// Register the layer with the registry
QgsMapLayerRegistry::instance()->addMapLayer(mpLinesLayer);
//
// We only need maprender instead of mapcanvas
// since maprender does not require a qui
// and is more light weight
//
mpMapRenderer = new QgsMapRender();
QStringList myLayers;
myLayers << mpPointsLayer->getLayerID();
myLayers << mpPolysLayer->getLayerID();
myLayers << mpLinesLayer->getLayerID();
mpMapRenderer->setLayerSet(myLayers);
mpMapRenderer->setExtent(mpPointsLayer->extent());
mReport+= "<h1>QuickPrint Tests</h1>\n";
}
void TestQgsQuickPrint::cleanupTestCase()
{
/*
QString myReportFile = QDir::tempPath() + QDir::separator() + "quickprinttest.html";
QFile myFile ( myReportFile);
if ( myFile.open ( QIODevice::WriteOnly ) )
{
QTextStream myQTextStream ( &myFile );
myQTextStream << mReport;
myFile.close();
QDesktopServices::openUrl("file://"+myReportFile);
}
*/

}

void TestQgsQuickPrint::basicMapTest()
{
//make the legends really long so we can test
//word wrapping
mpPointsLayer->setLayerName("This is a very very very long name it should word wrap");
mpPolysLayer->setLayerName("This is a very very very long name it should also word wrap");
mpLinesLayer->setLayerName("This is a very very very very long name it should word wrap");

//now print the map
QgsQuickPrint myQuickPrint;
myQuickPrint.setMapBackgroundColor ( Qt::cyan );
myQuickPrint.setOutputPdf (QDir::tempPath() + QDir::separator() + "quickprinttest.pdf");
myQuickPrint.setMapRender (mpMapRenderer);
myQuickPrint.setTitle ("Map Title");
myQuickPrint.setName ("Map Name");
myQuickPrint.setCopyright ("Copyright Text");
//void setNorthArrow(QString theFileName);
//void setLogo1(QString theFileName);
//void setLogo2(QString theFileName);
myQuickPrint.printMap();
}


//
// Helper functions below
//

bool TestQgsQuickPrint::imageCheck(QString theTestType)
{
//use the QgsRenderChecker test utility class to
//ensure the rendered output matches our control image
mpMapRenderer->setExtent(mpPointsLayer->extent());
QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png";
QgsRenderChecker myChecker;
myChecker.setExpectedImage ( myExpectedImage );
myChecker.setMapRenderer ( mpMapRenderer );
bool myResultFlag = myChecker.runTest( theTestType );
mReport += myChecker.report();
return myResultFlag;
}

QTEST_MAIN(TestQgsQuickPrint)
#include "moc_testqgsquickprint.cxx"

0 comments on commit 80ef2de

Please sign in to comment.