Skip to content

Commit 6e4cfb4

Browse files
author
timlinux
committed
Added first of gui lib tests (for quickprint)
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8015 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 49e8990 commit 6e4cfb4

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed

tests/src/gui/CMakeLists.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Standard includes and utils to compile into all tests.
2+
SET (util_SRCS ../core/qgsrenderchecker.cpp)
3+
4+
5+
#####################################################
6+
# Don't forget to include output directory, otherwise
7+
# the UI file won't be wrapped!
8+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
9+
${CMAKE_CURRENT_BINARY_DIR}
10+
${CMAKE_CURRENT_SOURCE_DIR}/../core #for render checker class
11+
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/gui
12+
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core
13+
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/core/raster
14+
${QT_INCLUDE_DIR}
15+
${GDAL_INCLUDE_DIR}
16+
${PROJ_INCLUDE_DIR}
17+
${GEOS_INCLUDE_DIR}
18+
)
19+
20+
#############################################################
21+
# Compiler defines
22+
23+
# expect that classes are being IMPORTED for the tests
24+
IF (WIN32)
25+
# expect that classes are being imported
26+
# Note: MSVC doesn't like when the macros are quotes
27+
# and MSYS doesn't like them unqouted (bacause of braces)
28+
IF (MSVC)
29+
ADD_DEFINITIONS("-DCORE_EXPORT=__declspec(dllimport)")
30+
#ADD_DEFINITIONS("-DPLUGIN_EXPORT=__declspec(dllimport)")
31+
#next needed for qgis gui includes
32+
ADD_DEFINITIONS("-DGUI_EXPORT=__declspec(dllexport)")
33+
ELSE (MSVC)
34+
ADD_DEFINITIONS("-UCORE_EXPORT \"-DCORE_EXPORT=__declspec(dllimport)\"")
35+
#ADD_DEFINITIONS("\"-DPLUGIN_EXPORT=__declspec(dllimport)\"")
36+
#next needed for qgis gui includes
37+
ADD_DEFINITIONS("-UGUI_EXPORT \"-DGUI_EXPORT=__declspec(dllexport)\"")
38+
ENDIF (MSVC)
39+
ENDIF (WIN32)
40+
41+
# This define is used for tests that need to locate the test
42+
# data under tests/testdata in the qgis source tree.
43+
# the TEST_DATA_DIR variable is set in the top level CMakeLists.txt
44+
ADD_DEFINITIONS(-DTEST_DATA_DIR="\\"${TEST_DATA_DIR}\\"")
45+
46+
#############################################################
47+
# libraries
48+
49+
# because of htonl
50+
IF (WIN32)
51+
SET(PLATFORM_LIBRARIES wsock32)
52+
ENDIF (WIN32)
53+
54+
# Since the tests are not actually installed, but rather
55+
# run directly from the build/src/tests dir we need to
56+
# ensure the omg libs can be found.
57+
IF (APPLE)
58+
# For Mac OS X, the executable must be at the root of the bundle's executable folder
59+
SET (CMAKE_INSTALL_NAME_DIR @executable_path/../../../src/gui)
60+
ENDIF (APPLE)
61+
62+
#note for tests we should not include the moc of our
63+
#qtests in the executable file list as the moc is
64+
#directly included in the sources
65+
#and should not be compiled twice. Trying to include
66+
#them in will cause an error at build time
67+
#############################################################
68+
# Tests:
69+
70+
#
71+
# QgsQuickPrint test
72+
#
73+
SET(qgis_quickprinttest_SRCS testqgsquickprint.cpp ${util_SRCS})
74+
SET(qgis_quickprinttest_MOC_CPPS testqgsquickprint.cpp)
75+
QT4_WRAP_CPP(qgis_quickprinttest_MOC_SRCS ${qgis_quickprinttest_MOC_CPPS})
76+
ADD_CUSTOM_TARGET(qgis_quickprinttestmoc ALL DEPENDS ${qgis_quickprinttest_MOC_SRCS})
77+
ADD_EXECUTABLE(qgis_quickprinttest ${qgis_quickprinttest_SRCS})
78+
ADD_DEPENDENCIES(qgis_quickprinttest qgis_quickprinttestmoc)
79+
TARGET_LINK_LIBRARIES(qgis_quickprinttest ${QT_LIBRARIES} qgis_core qgis_gui)
80+
SET_TARGET_PROPERTIES(qgis_quickprinttest
81+
PROPERTIES INSTALL_RPATH ${QGIS_LIB_DIR}
82+
INSTALL_RPATH_USE_LINK_PATH true)
83+
IF (APPLE)
84+
# For Mac OS X, the executable must be at the root of the bundle's executable folder
85+
INSTALL(TARGETS qgis_quickprinttest RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX})
86+
ADD_TEST(qgis_quickprinttest ${CMAKE_INSTALL_PREFIX}/qgis_quickprinttest)
87+
ELSE (APPLE)
88+
INSTALL(TARGETS qgis_quickprinttest RUNTIME DESTINATION ${QGIS_BIN_DIR})
89+
ADD_TEST(qgis_quickprinttest ${QGIS_BIN_DIR}/qgis_quickprinttest)
90+
ENDIF (APPLE)
91+
92+
93+

tests/src/gui/testqgsquickprint.cpp

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/***************************************************************************
2+
testqgsquickprint.cpp
3+
--------------------------------------
4+
Date : 20 Jan 2008
5+
Copyright : (C) 2008 by Tim Sutton
6+
Email : tim @ linfiniti.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include <QtTest>
16+
#include <QObject>
17+
#include <QStringList>
18+
#include <QObject>
19+
#include <QApplication>
20+
#include <QFileInfo>
21+
#include <QDir>
22+
#include <QDesktopServices>
23+
24+
#include <iostream>
25+
//qgis includes...
26+
#include <qgsmaprender.h>
27+
#include <qgsmaplayer.h>
28+
#include <qgsvectorlayer.h>
29+
#include <qgsapplication.h>
30+
#include <qgsproviderregistry.h>
31+
#include <qgsmaplayerregistry.h>
32+
#include <qgsquickprint.h>
33+
//qgis test includes
34+
#include <qgsrenderchecker.h>
35+
36+
/** \ingroup UnitTests
37+
* This is a unit test for the different renderers for vector layers.
38+
*/
39+
class TestQgsQuickPrint: public QObject
40+
{
41+
Q_OBJECT;
42+
private slots:
43+
void initTestCase();// will be called before the first testfunction is executed.
44+
void cleanupTestCase();// will be called after the last testfunction was executed.
45+
void init(){};// will be called before each testfunction is executed.
46+
void cleanup(){};// will be called after every testfunction.
47+
48+
void basicMapTest();
49+
QString getQgisPath(); // Gets the path to QGIS installation
50+
private:
51+
bool imageCheck(QString theType); //as above
52+
QgsMapRender * mpMapRenderer;
53+
QgsMapLayer * mpPointsLayer;
54+
QgsMapLayer * mpLinesLayer;
55+
QgsMapLayer * mpPolysLayer;
56+
QString mTestDataDir;
57+
QString mReport;
58+
};
59+
60+
QString TestQgsQuickPrint::getQgisPath()
61+
{
62+
#ifdef Q_OS_LINUX
63+
QString qgisPath = QCoreApplication::applicationDirPath () + "/../";
64+
#else //mac and win
65+
QString qgisPath = QCoreApplication::applicationDirPath () ;
66+
#endif
67+
return qgisPath;
68+
}
69+
70+
void TestQgsQuickPrint::initTestCase()
71+
{
72+
// init QGIS's paths - true means that all path will be inited from prefix
73+
//QString qgisPath = QCoreApplication::applicationDirPath ();
74+
QgsApplication::setPrefixPath(getQgisPath(), TRUE);
75+
#ifdef Q_OS_LINUX
76+
// QgsApplication::setPkgDataPath(qgisPath + "/../share/qgis");
77+
// QgsApplication::setPluginPath(qgisPath + "/../lib/qgis");
78+
#endif
79+
// Instantiate the plugin directory so that providers are loaded
80+
QgsProviderRegistry::instance(QgsApplication::pluginPath());
81+
82+
//create some objects that will be used in all tests...
83+
84+
std::cout << "Prefix PATH: " << QgsApplication::prefixPath().toLocal8Bit().data() << std::endl;
85+
std::cout << "Plugin PATH: " << QgsApplication::pluginPath().toLocal8Bit().data() << std::endl;
86+
std::cout << "PkgData PATH: " << QgsApplication::pkgDataPath().toLocal8Bit().data() << std::endl;
87+
std::cout << "User DB PATH: " << QgsApplication::qgisUserDbFilePath().toLocal8Bit().data() << std::endl;
88+
89+
//
90+
//create a point layer that will be used in all tests...
91+
//
92+
QString myDataDir (TEST_DATA_DIR); //defined in CmakeLists.txt
93+
mTestDataDir = myDataDir + QDir::separator();
94+
QString myPointsFileName = mTestDataDir + "points.shp";
95+
QFileInfo myPointFileInfo ( myPointsFileName );
96+
mpPointsLayer = new QgsVectorLayer ( myPointFileInfo.filePath(),
97+
myPointFileInfo.completeBaseName(), "ogr" );
98+
// Register the layer with the registry
99+
QgsMapLayerRegistry::instance()->addMapLayer(mpPointsLayer);
100+
101+
//
102+
//create a poly layer that will be used in all tests...
103+
//
104+
QString myPolysFileName = mTestDataDir + "polys.shp";
105+
QFileInfo myPolyFileInfo ( myPolysFileName );
106+
mpPolysLayer = new QgsVectorLayer ( myPolyFileInfo.filePath(),
107+
myPolyFileInfo.completeBaseName(), "ogr" );
108+
// Register the layer with the registry
109+
QgsMapLayerRegistry::instance()->addMapLayer(mpPolysLayer);
110+
111+
//
112+
// Create a line layer that will be used in all tests...
113+
//
114+
QString myLinesFileName = mTestDataDir + "lines.shp";
115+
QFileInfo myLineFileInfo ( myLinesFileName );
116+
mpLinesLayer = new QgsVectorLayer ( myLineFileInfo.filePath(),
117+
myLineFileInfo.completeBaseName(), "ogr" );
118+
// Register the layer with the registry
119+
QgsMapLayerRegistry::instance()->addMapLayer(mpLinesLayer);
120+
//
121+
// We only need maprender instead of mapcanvas
122+
// since maprender does not require a qui
123+
// and is more light weight
124+
//
125+
mpMapRenderer = new QgsMapRender();
126+
QStringList myLayers;
127+
myLayers << mpPointsLayer->getLayerID();
128+
myLayers << mpPolysLayer->getLayerID();
129+
myLayers << mpLinesLayer->getLayerID();
130+
mpMapRenderer->setLayerSet(myLayers);
131+
mpMapRenderer->setExtent(mpPointsLayer->extent());
132+
mReport+= "<h1>QuickPrint Tests</h1>\n";
133+
}
134+
void TestQgsQuickPrint::cleanupTestCase()
135+
{
136+
/*
137+
QString myReportFile = QDir::tempPath() + QDir::separator() + "quickprinttest.html";
138+
QFile myFile ( myReportFile);
139+
if ( myFile.open ( QIODevice::WriteOnly ) )
140+
{
141+
QTextStream myQTextStream ( &myFile );
142+
myQTextStream << mReport;
143+
myFile.close();
144+
QDesktopServices::openUrl("file://"+myReportFile);
145+
}
146+
*/
147+
148+
}
149+
150+
void TestQgsQuickPrint::basicMapTest()
151+
{
152+
//make the legends really long so we can test
153+
//word wrapping
154+
mpPointsLayer->setLayerName("This is a very very very long name it should word wrap");
155+
mpPolysLayer->setLayerName("This is a very very very long name it should also word wrap");
156+
mpLinesLayer->setLayerName("This is a very very very very long name it should word wrap");
157+
158+
//now print the map
159+
QgsQuickPrint myQuickPrint;
160+
myQuickPrint.setMapBackgroundColor ( Qt::cyan );
161+
myQuickPrint.setOutputPdf (QDir::tempPath() + QDir::separator() + "quickprinttest.pdf");
162+
myQuickPrint.setMapRender (mpMapRenderer);
163+
myQuickPrint.setTitle ("Map Title");
164+
myQuickPrint.setName ("Map Name");
165+
myQuickPrint.setCopyright ("Copyright Text");
166+
//void setNorthArrow(QString theFileName);
167+
//void setLogo1(QString theFileName);
168+
//void setLogo2(QString theFileName);
169+
myQuickPrint.printMap();
170+
}
171+
172+
173+
//
174+
// Helper functions below
175+
//
176+
177+
bool TestQgsQuickPrint::imageCheck(QString theTestType)
178+
{
179+
//use the QgsRenderChecker test utility class to
180+
//ensure the rendered output matches our control image
181+
mpMapRenderer->setExtent(mpPointsLayer->extent());
182+
QString myExpectedImage = mTestDataDir + "expected_" + theTestType + ".png";
183+
QgsRenderChecker myChecker;
184+
myChecker.setExpectedImage ( myExpectedImage );
185+
myChecker.setMapRenderer ( mpMapRenderer );
186+
bool myResultFlag = myChecker.runTest( theTestType );
187+
mReport += myChecker.report();
188+
return myResultFlag;
189+
}
190+
191+
QTEST_MAIN(TestQgsQuickPrint)
192+
#include "moc_testqgsquickprint.cxx"
193+

0 commit comments

Comments
 (0)