From c03ac224204ad6b586082a2926147a7230269aeb Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Tue, 26 May 2020 10:01:56 +1000 Subject: [PATCH] Add unit test for message bar max item count --- src/gui/qgsmessagebar.h | 2 ++ tests/src/gui/testqgsmessagebar.cpp | 35 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/gui/qgsmessagebar.h b/src/gui/qgsmessagebar.h index 61de6da7410b..21d1aa1dfe0d 100644 --- a/src/gui/qgsmessagebar.h +++ b/src/gui/qgsmessagebar.h @@ -268,6 +268,8 @@ class GUI_EXPORT QgsMessageBar: public QFrame //! updates the countdown for widgets that have a timeout duration void updateCountdown(); void resetCountdown(); + + friend class TestQgsMessageBar; }; #endif diff --git a/tests/src/gui/testqgsmessagebar.cpp b/tests/src/gui/testqgsmessagebar.cpp index 39f1bc9c966b..4dec119fecca 100644 --- a/tests/src/gui/testqgsmessagebar.cpp +++ b/tests/src/gui/testqgsmessagebar.cpp @@ -30,6 +30,7 @@ class TestQgsMessageBar: public QObject void cleanup(); // will be called after every testfunction. void dismiss(); void pushPop(); + void autoDelete(); }; @@ -144,5 +145,39 @@ void TestQgsMessageBar::pushPop() QVERIFY( !bar.currentItem() ); } +void TestQgsMessageBar::autoDelete() +{ + // ensure that items are automatically deleted when queue grows too large + QgsMessageBar bar; + for ( int i = 0; i < bar.MAX_ITEMS; ++i ) + { + bar.pushMessage( QString::number( i ), Qgis::Warning ); + } + QCOMPARE( bar.items().size(), 100 ); + QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "99" ) ); + QCOMPARE( bar.items().at( 99 )->text(), QStringLiteral( "0" ) ); + QPointer< QgsMessageBarItem > oldest = bar.items().at( 99 ); + + // push one more item, oldest one should be auto-removed + bar.pushMessage( QStringLiteral( "100" ), Qgis::Warning ); + QCOMPARE( bar.items().size(), 100 ); + QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "100" ) ); + QCOMPARE( bar.items().at( 99 )->text(), QStringLiteral( "1" ) ); + QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete ); + QVERIFY( !oldest ); + + // but if we have a lower priority message we can pop, then do that instead + bar.pushMessage( QStringLiteral( "101" ), Qgis::Info ); + QCOMPARE( bar.items().size(), 100 ); + QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "101" ) ); + QCOMPARE( bar.items().at( 1 )->text(), QStringLiteral( "100" ) ); + QCOMPARE( bar.items().at( 99 )->text(), QStringLiteral( "2" ) ); + bar.pushMessage( QStringLiteral( "102" ), Qgis::Info ); + QCOMPARE( bar.items().size(), 100 ); + QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "102" ) ); + QCOMPARE( bar.items().at( 1 )->text(), QStringLiteral( "100" ) ); + QCOMPARE( bar.items().at( 99 )->text(), QStringLiteral( "2" ) ); +} + QGSTEST_MAIN( TestQgsMessageBar ) #include "testqgsmessagebar.moc"