Skip to content

Commit 11cebff

Browse files
committed
Add test for clipboard logic
1 parent 0d2df80 commit 11cebff

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/app/qgsclipboard.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void QgsClipboard::setSystemClipboard()
142142
#endif
143143
cb->setText( textCopy, QClipboard::Clipboard );
144144

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

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

221-
QgsDebugMsg( "inserted " + feature.constGeometry()->exportToWkt() );
221+
QgsDebugMsgLevel( "inserted " + feature.constGeometry()->exportToWkt(), 4 );
222222
mUseSystemClipboard = false;
223223
emit changed();
224224
}

tests/src/app/testqgisappclipboard.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class TestQgisAppClipboard : public QObject
4949
void pasteWkt();
5050
void pasteGeoJson();
5151
void retrieveFields();
52+
void clipboardLogic(); //test clipboard logic
5253

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

173+
void TestQgisAppClipboard::clipboardLogic()
174+
{
175+
//start by setting clipboard contents as text
176+
mQgisApp->clipboard()->setText( "{\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [125, 10]},\"properties\": {\"name\": \"Dinagat Islands\"}}" );
177+
QgsFields fields = mQgisApp->clipboard()->fields();
178+
QCOMPARE( fields.count(), 1 );
179+
QCOMPARE( fields.at( 0 ).name(), QString( "name" ) );
180+
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
181+
QgsFeatureList features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
182+
QCOMPARE( features.length(), 1 );
183+
QCOMPARE( features.at( 0 ).attribute( "name" ).toString(), QString( "Dinagat Islands" ) );
184+
185+
//set clipboard to some QgsFeatures
186+
fields = QgsFields();
187+
fields.append( QgsField( "int_field", QVariant::Int ) );
188+
fields.append( QgsField( "date_field", QVariant::Date ) );
189+
QgsFeature feat( fields, 5 );
190+
feat.setAttribute( "int_field", 9 );
191+
feat.setAttribute( "date_field", QVariant( QDate( 2010, 9, 5 ) ) );
192+
QgsFeature feat2( fields, 6 );
193+
feat2.setAttribute( "int_field", 19 );
194+
feat2.setAttribute( "date_field", QVariant( QDate( 2011, 9, 5 ) ) );
195+
QgsFeatureStore feats;
196+
feats.addFeature( feat );
197+
feats.addFeature( feat2 );
198+
feats.setFields( fields );
199+
QgsCoordinateReferenceSystem crs;
200+
crs.createFromSrsId( 3452 );
201+
feats.setCrs( crs );
202+
mQgisApp->clipboard()->replaceWithCopyOf( feats );
203+
204+
//test result
205+
fields = mQgisApp->clipboard()->fields();
206+
QCOMPARE( fields.count(), 2 );
207+
QCOMPARE( fields.at( 0 ).name(), QString( "int_field" ) );
208+
QCOMPARE( fields.at( 0 ).type(), QVariant::Int );
209+
QCOMPARE( fields.at( 1 ).name(), QString( "date_field" ) );
210+
QCOMPARE( fields.at( 1 ).type(), QVariant::Date );
211+
features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
212+
QCOMPARE( features.length(), 2 );
213+
QCOMPARE( features.at( 0 ).id(), 5LL );
214+
QCOMPARE( features.at( 0 ).attribute( "int_field" ).toInt(), 9 );
215+
QCOMPARE( features.at( 0 ).attribute( "date_field" ).toDate(), QDate( 2010, 9, 5 ) );
216+
QCOMPARE( features.at( 1 ).id(), 6LL );
217+
QCOMPARE( features.at( 1 ).attribute( "int_field" ).toInt(), 19 );
218+
QCOMPARE( features.at( 1 ).attribute( "date_field" ).toDate(), QDate( 2011, 9, 5 ) );
219+
220+
//replace with text again, make sure system clipboard is used rather than internal clipboard
221+
mQgisApp->clipboard()->setText( "{\n\"type\": \"Feature\",\"geometry\": {\"type\": \"Point\",\"coordinates\": [125, 10]},\"properties\": {\"name\": \"Dinagat Islands\"}}" );
222+
fields = mQgisApp->clipboard()->fields();
223+
QCOMPARE( fields.count(), 1 );
224+
QCOMPARE( fields.at( 0 ).name(), QString( "name" ) );
225+
QCOMPARE( fields.at( 0 ).type(), QVariant::String );
226+
features = mQgisApp->clipboard()->copyOf( mQgisApp->clipboard()->fields() );
227+
QCOMPARE( features.length(), 1 );
228+
QCOMPARE( features.at( 0 ).attribute( "name" ).toString(), QString( "Dinagat Islands" ) );
229+
}
230+
172231
QTEST_MAIN( TestQgisAppClipboard )
173232
#include "testqgisappclipboard.moc"

0 commit comments

Comments
 (0)