Skip to content

Commit 1a9f68e

Browse files
committed
Unit tests for QgsProcessingToolboxTreeView
1 parent f640c48 commit 1a9f68e

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

tests/src/gui/testqgsprocessingmodel.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsprocessingregistry.h"
1919
#include "qgsprocessingtoolboxmodel.h"
2020
#include "qgsprocessingrecentalgorithmlog.h"
21+
#include "qgsprocessingtoolboxtreeview.h"
2122
#include "qgssettings.h"
2223

2324
#include <QtTest/QSignalSpy>
@@ -113,6 +114,7 @@ class TestQgsProcessingModel: public QObject
113114
void cleanup() {} // will be called after every testfunction.
114115
void testModel();
115116
void testProxyModel();
117+
void testView();
116118
};
117119

118120

@@ -549,5 +551,90 @@ void TestQgsProcessingModel::testProxyModel()
549551
QCOMPARE( model.rowCount(), 5 );
550552
}
551553

554+
void TestQgsProcessingModel::testView()
555+
{
556+
QgsSettings().clear();
557+
QgsProcessingRegistry registry;
558+
QgsProcessingRecentAlgorithmLog recentLog;
559+
QgsProcessingToolboxTreeView view( nullptr, &registry, &recentLog );
560+
561+
QVERIFY( !view.algorithmForIndex( QModelIndex() ) );
562+
563+
// add a provider
564+
DummyAlgorithm *a1 = new DummyAlgorithm( "a1", "group2", QgsProcessingAlgorithm::FlagHideFromToolbox );
565+
DummyProvider *p1 = new DummyProvider( "p2", "provider2", QList< QgsProcessingAlgorithm * >() << a1 );
566+
registry.addProvider( p1 );
567+
// second provider
568+
DummyAlgorithm *a2 = new DummyAlgorithm( "a2", "group2", QgsProcessingAlgorithm::FlagHideFromModeler,
569+
QStringLiteral( "buffer,vector" ), QStringLiteral( "short desc" ), QStringLiteral( "algorithm2" ) );
570+
DummyProvider *p2 = new DummyProvider( "p1", "provider1", QList< QgsProcessingAlgorithm * >() << a2 );
571+
registry.addProvider( p2 );
572+
573+
QModelIndex provider1Index = view.model()->index( 0, 0, QModelIndex() );
574+
QVERIFY( !view.algorithmForIndex( provider1Index ) );
575+
QModelIndex group2Index = view.model()->index( 0, 0, provider1Index );
576+
QCOMPARE( view.model()->data( group2Index, Qt::DisplayRole ).toString(), QStringLiteral( "group2" ) );
577+
QVERIFY( !view.algorithmForIndex( group2Index ) );
578+
QModelIndex alg2Index = view.model()->index( 0, 0, group2Index );
579+
QCOMPARE( view.algorithmForIndex( alg2Index )->id(), QStringLiteral( "p1:a2" ) );
580+
QModelIndex provider2Index = view.model()->index( 1, 0, QModelIndex() );
581+
QVERIFY( !view.algorithmForIndex( provider2Index ) );
582+
QCOMPARE( view.model()->data( provider2Index, Qt::DisplayRole ).toString(), QStringLiteral( "provider2" ) );
583+
group2Index = view.model()->index( 0, 0, provider2Index );
584+
QCOMPARE( view.model()->data( group2Index, Qt::DisplayRole ).toString(), QStringLiteral( "group2" ) );
585+
QModelIndex alg1Index = view.model()->index( 0, 0, group2Index );
586+
QCOMPARE( view.algorithmForIndex( alg1Index )->id(), QStringLiteral( "p2:a1" ) );
587+
588+
view.setFilters( QgsProcessingToolboxProxyModel::FilterModeler );
589+
provider2Index = view.model()->index( 0, 0, QModelIndex() );
590+
QCOMPARE( view.model()->data( provider2Index, Qt::DisplayRole ).toString(), QStringLiteral( "provider2" ) );
591+
QCOMPARE( view.model()->rowCount(), 1 );
592+
QCOMPARE( view.model()->rowCount( provider2Index ), 1 );
593+
group2Index = view.model()->index( 0, 0, provider2Index );
594+
QCOMPARE( view.model()->rowCount( group2Index ), 1 );
595+
QCOMPARE( view.algorithmForIndex( view.model()->index( 0, 0, group2Index ) )->id(), QStringLiteral( "p2:a1" ) );
596+
view.setFilters( QgsProcessingToolboxProxyModel::FilterToolbox );
597+
provider1Index = view.model()->index( 0, 0, QModelIndex() );
598+
QCOMPARE( view.model()->data( provider1Index, Qt::DisplayRole ).toString(), QStringLiteral( "provider1" ) );
599+
QCOMPARE( view.model()->rowCount(), 1 );
600+
QCOMPARE( view.model()->rowCount( provider1Index ), 1 );
601+
group2Index = view.model()->index( 0, 0, provider1Index );
602+
QCOMPARE( view.model()->rowCount( group2Index ), 1 );
603+
QCOMPARE( view.algorithmForIndex( view.model()->index( 0, 0, group2Index ) )->id(), QStringLiteral( "p1:a2" ) );
604+
605+
view.setFilters( nullptr );
606+
// test filter strings
607+
view.setFilterString( "a1" );
608+
provider2Index = view.model()->index( 0, 0, QModelIndex() );
609+
QCOMPARE( view.model()->data( provider2Index, Qt::DisplayRole ).toString(), QStringLiteral( "provider2" ) );
610+
QCOMPARE( view.model()->rowCount(), 1 );
611+
QCOMPARE( view.model()->rowCount( provider2Index ), 1 );
612+
group2Index = view.model()->index( 0, 0, provider2Index );
613+
QCOMPARE( view.model()->rowCount( group2Index ), 1 );
614+
QCOMPARE( view.algorithmForIndex( view.model()->index( 0, 0, group2Index ) )->id(), QStringLiteral( "p2:a1" ) );
615+
616+
view.setFilterString( QString() );
617+
618+
// selected algorithm
619+
provider1Index = view.model()->index( 0, 0, QModelIndex() );
620+
view.selectionModel()->clear();
621+
QVERIFY( !view.selectedAlgorithm() );
622+
view.selectionModel()->select( provider1Index, QItemSelectionModel::ClearAndSelect );
623+
QVERIFY( !view.selectedAlgorithm() );
624+
group2Index = view.model()->index( 0, 0, provider1Index );
625+
view.selectionModel()->select( group2Index, QItemSelectionModel::ClearAndSelect );
626+
QVERIFY( !view.selectedAlgorithm() );
627+
alg2Index = view.model()->index( 0, 0, group2Index );
628+
view.selectionModel()->select( alg2Index, QItemSelectionModel::ClearAndSelect );
629+
QCOMPARE( view.selectedAlgorithm()->id(), QStringLiteral( "p1:a2" ) );
630+
631+
// when a filter string removes the selected algorithm, the next matching algorithm should be auto-selected
632+
view.setFilterString( QStringLiteral( "p2:a1" ) );
633+
QCOMPARE( view.selectedAlgorithm()->id(), QStringLiteral( "p2:a1" ) );
634+
// but if it doesn't remove the selected one, that algorithm should not be deselected
635+
view.setFilterString( QStringLiteral( "a" ) );
636+
QCOMPARE( view.selectedAlgorithm()->id(), QStringLiteral( "p2:a1" ) );
637+
}
638+
552639
QGSTEST_MAIN( TestQgsProcessingModel )
553640
#include "testqgsprocessingmodel.moc"

0 commit comments

Comments
 (0)