Skip to content
Permalink
Browse files

Add unit test for message bar max item count

  • Loading branch information
nyalldawson committed May 26, 2020
1 parent 66b260d commit c03ac224204ad6b586082a2926147a7230269aeb
Showing with 37 additions and 0 deletions.
  1. +2 −0 src/gui/qgsmessagebar.h
  2. +35 −0 tests/src/gui/testqgsmessagebar.cpp
@@ -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
@@ -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"

0 comments on commit c03ac22

Please sign in to comment.
You can’t perform that action at this time.