Skip to content

Commit 250df51

Browse files
committed
copy/paste as test unit
1 parent fa66803 commit 250df51

File tree

8 files changed

+261
-19
lines changed

8 files changed

+261
-19
lines changed

src/app/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ ELSE (ANDROID)
456456
ADD_EXECUTABLE(${QGIS_APP_NAME} MACOSX_BUNDLE WIN32 ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${IMAGE_RCC_SRCS} ${TEST_RCC_SRCS})
457457
ENDIF (ANDROID)
458458

459+
# shared library used by tests - TODO: use it also for qgis executable?
460+
ADD_LIBRARY(qgis_app SHARED ${QGIS_APP_SRCS} ${QGIS_APP_MOC_SRCS} ${QGIS_APP_HDRS} ${QGIS_APP_MOC_HDRS} ${IMAGE_RCC_SRCS})
461+
459462
TARGET_LINK_LIBRARIES(${QGIS_APP_NAME}
460463
${QWT_LIBRARY}
461464
${QT_QTSQL_LIBRARY}

src/app/qgisapp.cpp

+53-16
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,24 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, QWidget * parent,
738738

739739
} // QgisApp ctor
740740

741-
741+
QgisApp::QgisApp( )
742+
: QMainWindow( 0, 0 )
743+
, mOverviewMapCursor( 0 )
744+
, mQgisInterface( 0 )
745+
, mInternalClipboard( 0 )
746+
, mpMaptip( 0 )
747+
, mPythonUtils( 0 )
748+
, mpGpsWidget( 0 )
749+
{
750+
smInstance = this;
751+
setupUi( this );
752+
mInternalClipboard = new QgsClipboard;
753+
mMapCanvas = new QgsMapCanvas();
754+
mMapCanvas->freeze();
755+
mMapLegend = new QgsLegend( mMapCanvas );
756+
mUndoWidget = new QgsUndoWidget( NULL, mMapCanvas );
757+
// More tests may need more members to be initialized
758+
}
742759

743760
QgisApp::~QgisApp()
744761
{
@@ -5564,24 +5581,29 @@ void QgisApp::pasteAsNewVector()
55645581
delete layer;
55655582
}
55665583

5567-
void QgisApp::pasteAsNewMemoryVector()
5584+
QgsVectorLayer * QgisApp::pasteAsNewMemoryVector( const QString & theLayerName )
55685585
{
5569-
if ( mMapCanvas && mMapCanvas->isDrawing() ) return;
5586+
if ( mMapCanvas && mMapCanvas->isDrawing() ) return 0;
55705587

5571-
bool ok;
5572-
QString defaultName = tr( "Pasted" );
5573-
QString layerName = QInputDialog::getText( this, tr( "New memory layer name" ),
5574-
tr( "Layer name" ), QLineEdit::Normal,
5575-
defaultName, &ok );
5576-
if ( !ok ) return;
5588+
QString layerName = theLayerName;
55775589

55785590
if ( layerName.isEmpty() )
55795591
{
5580-
layerName = defaultName;
5592+
bool ok;
5593+
QString defaultName = tr( "Pasted" );
5594+
layerName = QInputDialog::getText( this, tr( "New memory layer name" ),
5595+
tr( "Layer name" ), QLineEdit::Normal,
5596+
defaultName, &ok );
5597+
if ( !ok ) return 0;
5598+
5599+
if ( layerName.isEmpty() )
5600+
{
5601+
layerName = defaultName;
5602+
}
55815603
}
55825604

55835605
QgsVectorLayer * layer = pasteToNewMemoryVector();
5584-
if ( !layer ) return;
5606+
if ( !layer ) return 0;
55855607

55865608
layer->setLayerName( layerName );
55875609

@@ -5593,6 +5615,8 @@ void QgisApp::pasteAsNewMemoryVector()
55935615
mMapCanvas->refresh();
55945616

55955617
qApp->processEvents();
5618+
5619+
return layer;
55965620
}
55975621

55985622
QgsVectorLayer * QgisApp::pasteToNewMemoryVector()
@@ -5634,6 +5658,8 @@ QgsVectorLayer * QgisApp::pasteToNewMemoryVector()
56345658

56355659
QString typeName = QString( QGis::featureType( wkbType ) ).replace( "WKB", "" );
56365660

5661+
QgsDebugMsg( QString( "output wkbType = %1 typeName = %2" ).arg( wkbType ).arg( typeName ) );
5662+
56375663
QString message;
56385664

56395665
if ( features.size() == 0 )
@@ -5668,7 +5694,14 @@ QgsVectorLayer * QgisApp::pasteToNewMemoryVector()
56685694

56695695
foreach ( QgsField f, clipboard()->fields().toList() )
56705696
{
5671-
layer->addAttribute( f );
5697+
if ( !layer->addAttribute( f ) )
5698+
{
5699+
QMessageBox::warning( this, tr( "Warning" ),
5700+
tr( "Cannot create field %1 (%2,%3)" ).arg( f.name() ).arg( f.typeName() ).arg( QVariant::typeToName( f.type() ) ),
5701+
QMessageBox::Ok );
5702+
delete layer;
5703+
return 0;
5704+
}
56725705
}
56735706

56745707
// Convert to multi if necessary
@@ -5679,8 +5712,6 @@ QgsVectorLayer * QgisApp::pasteToNewMemoryVector()
56795712
QGis::WkbType type = QGis::flatType( feature.geometry()->wkbType() );
56805713
if ( type == QGis::WKBUnknown || type == QGis::WKBNoGeometry ) continue;
56815714

5682-
QgsDebugMsg( QString( "type = %1" ).arg( type ) );
5683-
56845715
if ( QGis::singleType( wkbType ) != QGis::singleType( type ) )
56855716
{
56865717
feature.setGeometry( 0 );
@@ -5691,9 +5722,15 @@ QgsVectorLayer * QgisApp::pasteToNewMemoryVector()
56915722
feature.geometry()->convertToMultiType();
56925723
}
56935724
}
5694-
layer->addFeatures( features );
5695-
layer->commitChanges();
5725+
if ( ! layer->addFeatures( features ) || ! layer->commitChanges() )
5726+
{
5727+
QgsDebugMsg( "Cannot add features or commit changes" );
5728+
delete layer;
5729+
return 0;
5730+
}
5731+
layer->removeSelection();
56965732

5733+
QgsDebugMsg( QString( "%1 features pasted to memory layer" ).arg( layer->featureCount() ) );
56975734
return layer;
56985735
}
56995736

src/app/qgisapp.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
109109
public:
110110
//! Constructor
111111
QgisApp( QSplashScreen *splash, bool restorePlugins = true, QWidget * parent = 0, Qt::WFlags fl = Qt::Window );
112+
//! Constructor for unit tests
113+
QgisApp( );
112114
//! Destructor
113115
~QgisApp();
114116
/**
@@ -538,7 +540,7 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
538540
//! copies features on the clipboard to a new vector layer
539541
void pasteAsNewVector();
540542
//! copies features on the clipboard to a new memory vector layer
541-
void pasteAsNewMemoryVector();
543+
QgsVectorLayer * pasteAsNewMemoryVector( const QString & theLayerName = QString() );
542544
//! copies style of the active layer to the clipboard
543545
/**
544546
\param sourceLayer The layer where the style will be taken from

src/core/qgsvectordataprovider.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,20 @@ const QList< QgsVectorDataProvider::NativeType > &QgsVectorDataProvider::nativeT
245245
bool QgsVectorDataProvider::supportedType( const QgsField &field ) const
246246
{
247247
int i;
248+
QgsDebugMsgLevel( QString( "field name = %1 type = %2 length = %3 precision = %4" ).arg( field.name() ).arg( QVariant::typeToName( field.type() ) ).arg( field.length() ).arg( field.precision() ), 2 );
248249
for ( i = 0; i < mNativeTypes.size(); i++ )
249250
{
251+
QgsDebugMsgLevel( QString( "native field type = %1 min length = %2 max length = %3 min precision = %4 max precision = %5" ).arg( QVariant::typeToName( mNativeTypes[i].mType ) ).arg( mNativeTypes[i].mMinLen ).arg( mNativeTypes[i].mMaxLen ).arg( mNativeTypes[i].mMinPrec ).arg( mNativeTypes[i].mMaxPrec ), 2 );
250252
if ( field.type() == mNativeTypes[i].mType &&
251253
field.length() >= mNativeTypes[i].mMinLen && field.length() <= mNativeTypes[i].mMaxLen &&
252254
field.precision() >= mNativeTypes[i].mMinPrec && field.precision() <= mNativeTypes[i].mMaxPrec )
253255
{
256+
QgsDebugMsg( "native type matches" );
254257
return true;
255258
}
256259
}
257260

261+
QgsDebugMsg( "no sufficient native type found" );
258262
return false;
259263
}
260264

src/providers/memory/qgsmemoryprovider.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,13 @@ QgsMemoryProvider::QgsMemoryProvider( QString uri )
7373

7474
mNativeTypes
7575
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), "integer", QVariant::Int, 0, 10 )
76-
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), "double", QVariant::Double, 0, 20, 0, 5 )
76+
// Decimal number from OGR/Shapefile/dbf may come with length up to 32 and
77+
// precision up to length-2 = 30 (default, if width is not specified in dbf is length = 24 precision = 15)
78+
// We know that double (QVariant::Double) has only 15-16 significant numbers,
79+
// but setting that correct limits would disable the use of memory provider with
80+
// data from Shapefiles. In any case, the data are handled as doubles.
81+
// So the limits set here are not correct but enable use of data from Shapefiles.
82+
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), "double", QVariant::Double, 0, 32, 0, 30 )
7783
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), "string", QVariant::String, 0, 255 )
7884
;
7985

@@ -309,6 +315,7 @@ bool QgsMemoryProvider::addAttributes( const QList<QgsField> &attributes )
309315
{
310316
for ( QList<QgsField>::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
311317
{
318+
// Why are attributes restricted to int,double and string only?
312319
switch ( it->type() )
313320
{
314321
case QVariant::Int:
@@ -319,7 +326,6 @@ bool QgsMemoryProvider::addAttributes( const QList<QgsField> &attributes )
319326
QgsDebugMsg( "Field type not supported: " + it->typeName() );
320327
continue;
321328
}
322-
323329
// add new field as a last one
324330
mFields.append( *it );
325331

tests/src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ IF (ENABLE_TESTS)
33
ADD_SUBDIRECTORY(gui)
44
ADD_SUBDIRECTORY(analysis)
55
ADD_SUBDIRECTORY(providers)
6+
ADD_SUBDIRECTORY(app)
67
IF (WITH_BINDINGS)
78
ADD_SUBDIRECTORY(python)
89
ENDIF (WITH_BINDINGS)

tests/src/app/CMakeLists.txt

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#####################################################
2+
# Don't forget to include output directory, otherwise
3+
# the UI file won't be wrapped!
4+
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
5+
${CMAKE_CURRENT_BINARY_DIR}
6+
${CMAKE_SOURCE_DIR}/src/core
7+
${CMAKE_SOURCE_DIR}/src/core/composer
8+
${CMAKE_SOURCE_DIR}/src/core/raster
9+
${CMAKE_SOURCE_DIR}/src/core/symbology-ng
10+
${CMAKE_BINARY_DIR}/src/ui
11+
${CMAKE_SOURCE_DIR}/src/gui
12+
${CMAKE_SOURCE_DIR}/src/python
13+
${CMAKE_SOURCE_DIR}/src/app
14+
${CMAKE_SOURCE_DIR}/src/app/pluginmanager
15+
${QT_INCLUDE_DIR}
16+
${GDAL_INCLUDE_DIR}
17+
${PROJ_INCLUDE_DIR}
18+
${GEOS_INCLUDE_DIR}
19+
)
20+
21+
#############################################################
22+
# Compiler defines
23+
24+
# This define is used for tests that need to locate the test
25+
# data under tests/testdata in the qgis source tree.
26+
# the TEST_DATA_DIR variable is set in the top level CMakeLists.txt
27+
ADD_DEFINITIONS(-DTEST_DATA_DIR="\\"${TEST_DATA_DIR}\\"")
28+
29+
ADD_DEFINITIONS(-DINSTALL_PREFIX="\\"${CMAKE_INSTALL_PREFIX}\\"")
30+
#############################################################
31+
# libraries
32+
33+
# because of htonl
34+
IF (WIN32)
35+
SET(PLATFORM_LIBRARIES wsock32)
36+
ENDIF (WIN32)
37+
38+
# Since the tests are not actually installed, but rather
39+
# run directly from the build/src/tests dir we need to
40+
# ensure the qgis libs can be found.
41+
IF (APPLE)
42+
# For Mac OS X, the executable must be at the root of the bundle's executable folder
43+
# SET (CMAKE_INSTALL_NAME_DIR @executable_path/../../../src/core)
44+
ENDIF (APPLE)
45+
46+
#note for tests we should not include the moc of our
47+
#qtests in the executable file list as the moc is
48+
#directly included in the sources
49+
#and should not be compiled twice. Trying to include
50+
#them in will cause an error at build time
51+
52+
#No relinking and full RPATH for the install tree
53+
#See: http://www.cmake.org/Wiki/CMake_RPATH_handling#No_relinking_and_full_RPATH_for_the_install_tree
54+
55+
MACRO (ADD_QGIS_TEST testname testsrc)
56+
SET(qgis_${testname}_SRCS ${testsrc} ${util_SRCS})
57+
SET(qgis_${testname}_MOC_CPPS ${testsrc})
58+
QT4_WRAP_CPP(qgis_${testname}_MOC_SRCS ${qgis_${testname}_MOC_CPPS})
59+
ADD_CUSTOM_TARGET(qgis_${testname}moc ALL DEPENDS ${qgis_${testname}_MOC_SRCS})
60+
ADD_EXECUTABLE(qgis_${testname} ${qgis_${testname}_SRCS})
61+
ADD_DEPENDENCIES(qgis_${testname} qgis_${testname}moc)
62+
TARGET_LINK_LIBRARIES(qgis_${testname}
63+
${QT_QTXML_LIBRARY}
64+
${QT_QTCORE_LIBRARY}
65+
${QT_QTSQL_LIBRARY}
66+
${QT_QTSVG_LIBRARY}
67+
${QT_QTTEST_LIBRARY}
68+
${PROJ_LIBRARY}
69+
${GEOS_LIBRARY}
70+
${GDAL_LIBRARY}
71+
${QWT_LIBRARY}
72+
${QWTPOLAR_LIBRARY}
73+
qgis_core
74+
qgis_gui
75+
qgis_analysis
76+
qgis_app)
77+
ADD_TEST(qgis_${testname} ${CMAKE_CURRENT_BINARY_DIR}/../../../output/bin/qgis_${testname})
78+
#SET_TARGET_PROPERTIES(qgis_${testname} PROPERTIES
79+
# INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/${QGIS_LIB_DIR}
80+
# INSTALL_RPATH_USE_LINK_PATH true )
81+
ENDMACRO (ADD_QGIS_TEST)
82+
83+
#############################################################
84+
# Tests:
85+
86+
ADD_QGIS_TEST(qgisappclippboard testqgisappclipboard.cpp)

0 commit comments

Comments
 (0)