Skip to content

Commit

Permalink
Add unit test for message bar max item count
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 26, 2020
1 parent 66b260d commit c03ac22
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/qgsmessagebar.h
Expand Up @@ -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
35 changes: 35 additions & 0 deletions tests/src/gui/testqgsmessagebar.cpp
Expand Up @@ -30,6 +30,7 @@ class TestQgsMessageBar: public QObject
void cleanup(); // will be called after every testfunction.
void dismiss();
void pushPop();
void autoDelete();

};

Expand Down Expand Up @@ -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.