Skip to content

Commit 26626ea

Browse files
committed
Fix test
1 parent e23167e commit 26626ea

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

tests/src/core/testqgsvectorlayerutils.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ class TestQgsVectorLayerUtils : public QObject
2626
public:
2727
TestQgsVectorLayerUtils() = default;
2828

29-
private:
30-
bool mTestHasError = false ;
31-
QgsMapLayer *mpPointsLayer = nullptr;
32-
QgsMapLayer *mpLinesLayer = nullptr;
33-
QgsMapLayer *mpPolysLayer = nullptr;
34-
QgsVectorLayer *mpNonSpatialLayer = nullptr;
35-
QString mTestDataDir;
36-
QString mReport;
37-
3829
private slots:
3930

4031
void initTestCase(); // will be called before the first testfunction is executed.
@@ -71,7 +62,9 @@ class FeatureFetcher : public QThread
7162
void run() override
7263
{
7364
QgsFeature feat;
74-
QgsVectorLayerUtils::getFeatureSource( mLayer ).get()->getFeatures().nextFeature( feat );
65+
auto fs = QgsVectorLayerUtils::getFeatureSource( mLayer );
66+
if ( fs )
67+
fs->getFeatures().nextFeature( feat );
7568
emit resultReady( feat.attribute( QStringLiteral( "col1" ) ) );
7669
}
7770

@@ -85,35 +78,51 @@ class FeatureFetcher : public QThread
8578

8679
void TestQgsVectorLayerUtils::testGetFeatureSource()
8780
{
88-
QgsVectorLayer *vl = new QgsVectorLayer( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
81+
std::unique_ptr<QgsVectorLayer> vl = qgis::make_unique<QgsVectorLayer>( QStringLiteral( "Point?field=col1:integer" ), QStringLiteral( "vl" ), QStringLiteral( "memory" ) );
8982
vl->startEditing();
9083
QgsFeature f1( vl->fields(), 1 );
9184
f1.setAttribute( QStringLiteral( "col1" ), 10 );
9285
vl->addFeature( f1 );
9386

94-
QPointer<QgsVectorLayer> vlPtr( vl );
87+
QPointer<QgsVectorLayer> vlPtr( vl.get() );
9588

9689
QgsFeature feat;
97-
QgsVectorLayerUtils::getFeatureSource( vlPtr ).get()->getFeatures().nextFeature( feat );
90+
QgsVectorLayerUtils::getFeatureSource( vlPtr )->getFeatures().nextFeature( feat );
9891
QCOMPARE( feat.attribute( QStringLiteral( "col1" ) ).toInt(), 10 );
9992

10093
FeatureFetcher *thread = new FeatureFetcher( vlPtr );
10194

10295
bool finished = false;
10396
QVariant result;
104-
connect( thread, &FeatureFetcher::resultReady, this, [finished, result]( const QVariant & res )
97+
98+
auto onResultReady = [&finished, &result]( const QVariant & res )
10599
{
106100
finished = true;
107101
result = res;
108-
} );
102+
};
103+
104+
connect( thread, &FeatureFetcher::resultReady, this, onResultReady );
109105
connect( thread, &QThread::finished, thread, &QThread::deleteLater );
110106

111107
thread->start();
112108
while ( !finished )
113109
QCoreApplication::processEvents();
114110
QCOMPARE( result.toInt(), 10 );
115-
116111
thread->quit();
112+
113+
FeatureFetcher *thread2 = new FeatureFetcher( vlPtr );
114+
115+
finished = false;
116+
result = QVariant();
117+
connect( thread2, &FeatureFetcher::resultReady, this, onResultReady );
118+
connect( thread2, &QThread::finished, thread, &QThread::deleteLater );
119+
120+
vl.reset();
121+
thread2->start();
122+
while ( !finished )
123+
QCoreApplication::processEvents();
124+
QVERIFY( result.isNull() );
125+
thread2->quit();
117126
}
118127

119128
QGSTEST_MAIN( TestQgsVectorLayerUtils )

0 commit comments

Comments
 (0)