Skip to content
Permalink
Browse files

Add unit test for message bar push/pop logic, also test that items

are correctly deleted when dismissed
  • Loading branch information
nyalldawson committed May 26, 2020
1 parent 32274ed commit 66b260d7c4df552498f475f76e47a20a101e94ba
@@ -144,6 +144,13 @@ is set to 0, then the message must be manually dismissed by the user.
QgsMessageBarItem *currentItem();
%Docstring
Returns the current visible item, or ``None`` if no item is shown.
%End

QList<QgsMessageBarItem *> items();
%Docstring
Returns a list of all items currently visible or queued for the bar.

.. versionadded:: 3.14
%End

signals:
@@ -355,6 +355,11 @@ QgsMessageBarItem *QgsMessageBar::currentItem()
return mItems.value( 0 );
}

QList<QgsMessageBarItem *> QgsMessageBar::items()
{
return mItems;
}

QgsMessageBarItem *QgsMessageBar::createMessage( const QString &text, QWidget *parent )
{
QgsMessageBarItem *item = new QgsMessageBarItem( text, Qgis::Info, 0, parent );
@@ -163,6 +163,13 @@ class GUI_EXPORT QgsMessageBar: public QFrame
*/
QgsMessageBarItem *currentItem();

/**
* Returns a list of all items currently visible or queued for the bar.
*
* \since QGIS 3.14
*/
QList<QgsMessageBarItem *> items();

signals:

/**
@@ -29,6 +29,7 @@ class TestQgsMessageBar: public QObject
void init(); // will be called before each testfunction is executed.
void cleanup(); // will be called after every testfunction.
void dismiss();
void pushPop();

};

@@ -80,7 +81,68 @@ void TestQgsMessageBar::dismiss()
QVERIFY( !pItem.data() );
}

void TestQgsMessageBar::pushPop()
{
// test pushing/popping message logic
QgsMessageBar bar;
QCOMPARE( bar.items().size(), 0 );
QVERIFY( !bar.currentItem() );
bar.pushMessage( QStringLiteral( "1" ) );
QCOMPARE( bar.items().size(), 1 );
QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "1" ) );
QCOMPARE( bar.currentItem()->text(), QStringLiteral( "1" ) );
QPointer< QgsMessageBarItem > item1 = bar.currentItem();
// make sure correct item is the visible one
QCOMPARE( qobject_cast< QgsMessageBarItem * >( dynamic_cast< QGridLayout * >( bar.layout() )->itemAt( 3 )->widget() )->text(), QStringLiteral( "1" ) );

bar.pushMessage( QStringLiteral( "2" ) );
QCOMPARE( bar.items().size(), 2 );
QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "2" ) );
QCOMPARE( bar.items().at( 1 )->text(), QStringLiteral( "1" ) );
QCOMPARE( bar.currentItem()->text(), QStringLiteral( "2" ) );
QPointer< QgsMessageBarItem > item2 = bar.currentItem();
QCOMPARE( qobject_cast< QgsMessageBarItem * >( dynamic_cast< QGridLayout * >( bar.layout() )->itemAt( 3 )->widget() )->text(), QStringLiteral( "2" ) );

bar.pushMessage( QStringLiteral( "3" ) );
QCOMPARE( bar.items().size(), 3 );
QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "3" ) );
QCOMPARE( bar.items().at( 1 )->text(), QStringLiteral( "2" ) );
QCOMPARE( bar.items().at( 2 )->text(), QStringLiteral( "1" ) );
QCOMPARE( bar.currentItem()->text(), QStringLiteral( "3" ) );
QPointer< QgsMessageBarItem > item3 = bar.currentItem();
QCOMPARE( qobject_cast< QgsMessageBarItem * >( dynamic_cast< QGridLayout * >( bar.layout() )->itemAt( 3 )->widget() )->text(), QStringLiteral( "3" ) );

const int childCount = bar.children().count();
QVERIFY( bar.popWidget() );
QCOMPARE( bar.items().size(), 2 );
QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "2" ) );
QCOMPARE( bar.items().at( 1 )->text(), QStringLiteral( "1" ) );
QCOMPARE( bar.currentItem()->text(), QStringLiteral( "2" ) );
QCOMPARE( qobject_cast< QgsMessageBarItem * >( dynamic_cast< QGridLayout * >( bar.layout() )->itemAt( 3 )->widget() )->text(), QStringLiteral( "2" ) );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QCOMPARE( bar.children().count(), childCount - 1 );
QVERIFY( !item3 );

QVERIFY( bar.popWidget() );
QCOMPARE( bar.items().size(), 1 );
QCOMPARE( bar.items().at( 0 )->text(), QStringLiteral( "1" ) );
QCOMPARE( bar.currentItem()->text(), QStringLiteral( "1" ) );
QCOMPARE( qobject_cast< QgsMessageBarItem * >( dynamic_cast< QGridLayout * >( bar.layout() )->itemAt( 3 )->widget() )->text(), QStringLiteral( "1" ) );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QCOMPARE( bar.children().count(), childCount - 2 );
QVERIFY( !item2 );

QVERIFY( bar.popWidget() );
QCOMPARE( bar.items().size(), 0 );
QVERIFY( !bar.currentItem() );
QgsApplication::sendPostedEvents( nullptr, QEvent::DeferredDelete );
QCOMPARE( bar.children().count(), childCount - 3 );
QVERIFY( !item1 );

QVERIFY( !bar.popWidget() );
QCOMPARE( bar.items().size(), 0 );
QVERIFY( !bar.currentItem() );
}

QGSTEST_MAIN( TestQgsMessageBar )
#include "testqgsmessagebar.moc"

0 comments on commit 66b260d

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