PDA

View Full Version : Unit Test: Test if QPushButton has a menu



FelixB
6th November 2012, 09:11
Hello,

I have a GUI with lots of buttons. Each button should show a menu when clicked. I want to test this behaviour in the following unit test:



BOOST_AUTO_TEST_CASE(EachButtonHasMenuOnClick)
{
BOOST_FOREACH(QPushButton* elButton, m_perilib.findChildren<QPushButton*>())
{
// click on button
QTest::mouseClick( elButton, Qt::LeftButton );

// check if menu is visible
QMenu* elButtonMenu = elButton->menu();
BOOST_REQUIRE(elButtonMenu);
BOOST_CHECK_EQUAL(true, elButtonMenu->isVisible());

// click again (somewhere outside the menu) to close menu
QTest::mouseClick( elButton, Qt::LeftButton );
BOOST_CHECK_EQUAL(false, elButtonMenu->isVisible());
}
}


But the problem is: the menu is opened modal, so QTest::mouseClick does not return until the user closes the menu by clicking anywhere. That is quite bad for an automated unit test... Is there any simple solution? Or do I have to use a different thread?

thx!
Felix

StrikeByte
7th November 2012, 12:12
Can't you just send a mouse click event to the main window using postevent?