@@ -26,15 +26,6 @@ class TestQgsVectorLayerUtils : public QObject
26
26
public:
27
27
TestQgsVectorLayerUtils () = default ;
28
28
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
-
38
29
private slots:
39
30
40
31
void initTestCase (); // will be called before the first testfunction is executed.
@@ -71,7 +62,9 @@ class FeatureFetcher : public QThread
71
62
void run () override
72
63
{
73
64
QgsFeature feat;
74
- QgsVectorLayerUtils::getFeatureSource ( mLayer ).get ()->getFeatures ().nextFeature ( feat );
65
+ auto fs = QgsVectorLayerUtils::getFeatureSource ( mLayer );
66
+ if ( fs )
67
+ fs->getFeatures ().nextFeature ( feat );
75
68
emit resultReady ( feat.attribute ( QStringLiteral ( " col1" ) ) );
76
69
}
77
70
@@ -85,35 +78,51 @@ class FeatureFetcher : public QThread
85
78
86
79
void TestQgsVectorLayerUtils::testGetFeatureSource ()
87
80
{
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" ) );
89
82
vl->startEditing ();
90
83
QgsFeature f1 ( vl->fields (), 1 );
91
84
f1.setAttribute ( QStringLiteral ( " col1" ), 10 );
92
85
vl->addFeature ( f1 );
93
86
94
- QPointer<QgsVectorLayer> vlPtr ( vl );
87
+ QPointer<QgsVectorLayer> vlPtr ( vl. get () );
95
88
96
89
QgsFeature feat;
97
- QgsVectorLayerUtils::getFeatureSource ( vlPtr ). get () ->getFeatures ().nextFeature ( feat );
90
+ QgsVectorLayerUtils::getFeatureSource ( vlPtr )->getFeatures ().nextFeature ( feat );
98
91
QCOMPARE ( feat.attribute ( QStringLiteral ( " col1" ) ).toInt (), 10 );
99
92
100
93
FeatureFetcher *thread = new FeatureFetcher ( vlPtr );
101
94
102
95
bool finished = false ;
103
96
QVariant result;
104
- connect ( thread, &FeatureFetcher::resultReady, this , [finished, result]( const QVariant & res )
97
+
98
+ auto onResultReady = [&finished, &result]( const QVariant & res )
105
99
{
106
100
finished = true ;
107
101
result = res;
108
- } );
102
+ };
103
+
104
+ connect ( thread, &FeatureFetcher::resultReady, this , onResultReady );
109
105
connect ( thread, &QThread::finished, thread, &QThread::deleteLater );
110
106
111
107
thread->start ();
112
108
while ( !finished )
113
109
QCoreApplication::processEvents ();
114
110
QCOMPARE ( result.toInt (), 10 );
115
-
116
111
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 ();
117
126
}
118
127
119
128
QGSTEST_MAIN ( TestQgsVectorLayerUtils )
0 commit comments