Skip to content
Permalink
Browse files

Add test for clipboard logic

  • Loading branch information
nyalldawson committed Mar 1, 2016
1 parent 0d2df80 commit 11cebff60a2e86fc6f4feb5d1db5e58581a4bfef
Showing with 61 additions and 2 deletions.
  1. +2 −2 src/app/qgsclipboard.cpp
  2. +59 −0 tests/src/app/testqgisappclipboard.cpp
@@ -142,7 +142,7 @@ void QgsClipboard::setSystemClipboard()
#endif
cb->setText( textCopy, QClipboard::Clipboard );

QgsDebugMsg( QString( "replaced system clipboard with: %1." ).arg( textCopy ) );
QgsDebugMsgLevel( QString( "replaced system clipboard with: %1." ).arg( textCopy ), 4 );
}

QgsFeatureList QgsClipboard::stringToFeatureList( const QString& string, const QgsFields& fields ) const
@@ -218,7 +218,7 @@ void QgsClipboard::insert( const QgsFeature& feature )
{
mFeatureClipboard.push_back( feature );

QgsDebugMsg( "inserted " + feature.constGeometry()->exportToWkt() );
QgsDebugMsgLevel( "inserted " + feature.constGeometry()->exportToWkt(), 4 );
mUseSystemClipboard = false;
emit changed();
}
@@ -49,6 +49,7 @@ class TestQgisAppClipboard : public QObject
void pasteWkt();
void pasteGeoJson();
void retrieveFields();
void clipboardLogic(); //test clipboard logic

private:
QgisApp * mQgisApp;
@@ -169,5 +170,63 @@ void TestQgisAppClipboard::retrieveFields()
QCOMPARE( fields.at( 1 ).type(), QVariant::Double );
}

void TestQgisAppClipboard::clipboardLogic()
{
//start by setting clipboard contents as text
mQgisApp->clipboard()->setText( "{\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [125, 10]},\"properties\": {\"name\": \"Dinagat Islands\"}}" );
QgsFields fields = mQgisApp->clipboard()->fields();
QCOMPARE( fields.count(), 1 );
QCOMPARE( fields.at( 0 ).name(), QString( "name" ) );
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
QgsFeatureList features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
QCOMPARE( features.length(), 1 );
QCOMPARE( features.at( 0 ).attribute( "name" ).toString(), QString( "Dinagat Islands" ) );

//set clipboard to some QgsFeatures
fields = QgsFields();
fields.append( QgsField( "int_field", QVariant::Int ) );
fields.append( QgsField( "date_field", QVariant::Date ) );
QgsFeature feat( fields, 5 );
feat.setAttribute( "int_field", 9 );
feat.setAttribute( "date_field", QVariant( QDate( 2010, 9, 5 ) ) );
QgsFeature feat2( fields, 6 );
feat2.setAttribute( "int_field", 19 );
feat2.setAttribute( "date_field", QVariant( QDate( 2011, 9, 5 ) ) );
QgsFeatureStore feats;
feats.addFeature( feat );
feats.addFeature( feat2 );
feats.setFields( fields );
QgsCoordinateReferenceSystem crs;
crs.createFromSrsId( 3452 );
feats.setCrs( crs );
mQgisApp->clipboard()->replaceWithCopyOf( feats );

//test result
fields = mQgisApp->clipboard()->fields();
QCOMPARE( fields.count(), 2 );
QCOMPARE( fields.at( 0 ).name(), QString( "int_field" ) );
QCOMPARE( fields.at( 0 ).type(), QVariant::Int );
QCOMPARE( fields.at( 1 ).name(), QString( "date_field" ) );
QCOMPARE( fields.at( 1 ).type(), QVariant::Date );
features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
QCOMPARE( features.length(), 2 );
QCOMPARE( features.at( 0 ).id(), 5LL );
QCOMPARE( features.at( 0 ).attribute( "int_field" ).toInt(), 9 );
QCOMPARE( features.at( 0 ).attribute( "date_field" ).toDate(), QDate( 2010, 9, 5 ) );
QCOMPARE( features.at( 1 ).id(), 6LL );
QCOMPARE( features.at( 1 ).attribute( "int_field" ).toInt(), 19 );
QCOMPARE( features.at( 1 ).attribute( "date_field" ).toDate(), QDate( 2011, 9, 5 ) );

//replace with text again, make sure system clipboard is used rather than internal clipboard
mQgisApp->clipboard()->setText( "{\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [125, 10]},\"properties\": {\"name\": \"Dinagat Islands\"}}" );
fields = mQgisApp->clipboard()->fields();
QCOMPARE( fields.count(), 1 );
QCOMPARE( fields.at( 0 ).name(), QString( "name" ) );
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
QCOMPARE( features.length(), 1 );
QCOMPARE( features.at( 0 ).attribute( "name" ).toString(), QString( "Dinagat Islands" ) );
}

QTEST_MAIN( TestQgisAppClipboard )
#include "testqgisappclipboard.moc"

0 comments on commit 11cebff

Please sign in to comment.
You can’t perform that action at this time.