Cleanup QPushButton auto test code

Fix coding style, replace QVERIFY(a == b) with QCOMPARE, use nullptr,
use a lambda for a local slot, and wait for the test widget to go away
so that it doesn't break following tests.

Change-Id: I1a4f790084b92301444a96a1449c84bf3317b88a
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-03-07 14:42:18 +01:00
parent d115aa90a2
commit 5bd93821e5

View File

@ -58,16 +58,15 @@ private slots:
protected slots:
void resetCounters();
void onClicked();
void onToggled( bool on );
void onToggled(bool on);
void onPressed();
void onReleased();
void helperSlotDelete();
private:
uint click_count;
uint toggle_count;
uint press_count;
uint release_count;
int click_count;
int toggle_count;
int press_count;
int release_count;
QPushButton *testWidget;
QPointingDevice *m_touchScreen = QTest::createTouchDevice();
@ -82,40 +81,40 @@ void tst_QPushButton::getSetCheck()
QMenu *var1 = new QMenu;
obj1.setMenu(var1);
QCOMPARE(var1, obj1.menu());
obj1.setMenu((QMenu *)0);
QCOMPARE((QMenu *)0, obj1.menu());
obj1.setMenu(nullptr);
QCOMPARE(obj1.menu(), nullptr);
delete var1;
}
void tst_QPushButton::initTestCase()
{
// Create the test class
testWidget = new QPushButton( "&Start", 0 );
testWidget = new QPushButton("&Start", 0);
testWidget->setObjectName("testWidget");
testWidget->resize( 200, 200 );
testWidget->resize(200, 200);
testWidget->show();
connect( testWidget, SIGNAL(clicked()), this, SLOT(onClicked()) );
connect( testWidget, SIGNAL(pressed()), this, SLOT(onPressed()) );
connect( testWidget, SIGNAL(released()), this, SLOT(onReleased()) );
connect( testWidget, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)) );
connect(testWidget, SIGNAL(clicked()), this, SLOT(onClicked()));
connect(testWidget, SIGNAL(pressed()), this, SLOT(onPressed()));
connect(testWidget, SIGNAL(released()), this, SLOT(onReleased()));
connect(testWidget, SIGNAL(toggled(bool)), this, SLOT(onToggled(bool)));
}
void tst_QPushButton::cleanupTestCase()
{
delete testWidget;
testWidget = 0;
testWidget = nullptr;
}
void tst_QPushButton::init()
{
testWidget->setAutoRepeat( false );
testWidget->setDown( false );
testWidget->setAutoRepeat(false);
testWidget->setDown(false);
testWidget->setText("Test");
testWidget->setEnabled( true );
testWidget->setEnabled(true);
#if QT_CONFIG(shortcut)
QKeySequence seq;
testWidget->setShortcut( seq );
testWidget->setShortcut(seq);
#endif
resetCounters();
@ -134,7 +133,7 @@ void tst_QPushButton::onClicked()
click_count++;
}
void tst_QPushButton::onToggled( bool /*on*/ )
void tst_QPushButton::onToggled(bool /*on*/)
{
toggle_count++;
}
@ -152,46 +151,46 @@ void tst_QPushButton::onReleased()
void tst_QPushButton::autoRepeat()
{
// If this changes, this test must be completely revised.
QVERIFY( !testWidget->isCheckable() );
QVERIFY(!testWidget->isCheckable());
// verify autorepeat is off by default.
QPushButton tmp( 0 );
QPushButton tmp;
tmp.setObjectName("tmp");
QVERIFY( !tmp.autoRepeat() );
QVERIFY(!tmp.autoRepeat());
// check if we can toggle the mode
testWidget->setAutoRepeat( true );
QVERIFY( testWidget->autoRepeat() );
testWidget->setAutoRepeat(true);
QVERIFY(testWidget->autoRepeat());
testWidget->setAutoRepeat( false );
QVERIFY( !testWidget->autoRepeat() );
testWidget->setAutoRepeat(false);
QVERIFY(!testWidget->autoRepeat());
resetCounters();
// check that the button is down if we press space and not in autorepeat
testWidget->setDown( false );
testWidget->setAutoRepeat( false );
QTest::keyPress( testWidget, Qt::Key_Space );
testWidget->setDown(false);
testWidget->setAutoRepeat(false);
QTest::keyPress(testWidget, Qt::Key_Space);
QTRY_VERIFY( testWidget->isDown() );
QVERIFY( toggle_count == 0 );
QVERIFY( press_count == 1 );
QVERIFY( release_count == 0 );
QVERIFY( click_count == 0 );
QTRY_VERIFY(testWidget->isDown());
QCOMPARE(toggle_count, 0);
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 0);
QCOMPARE(click_count, 0);
QTest::keyRelease( testWidget, Qt::Key_Space );
QTest::keyRelease(testWidget, Qt::Key_Space);
resetCounters();
// check that the button is down if we press space while in autorepeat
// we can't actually confirm how many times it is fired, more than 1 is enough.
testWidget->setDown( false );
testWidget->setAutoRepeat( true );
QTest::keyPress( testWidget, Qt::Key_Space );
testWidget->setDown(false);
testWidget->setAutoRepeat(true);
QTest::keyPress(testWidget, Qt::Key_Space);
QTRY_VERIFY(press_count > 3);
QVERIFY( testWidget->isDown() );
QVERIFY( toggle_count == 0 );
QTest::keyRelease( testWidget, Qt::Key_Space );
QVERIFY(testWidget->isDown());
QCOMPARE(toggle_count, 0);
QTest::keyRelease(testWidget, Qt::Key_Space);
QCOMPARE(press_count, release_count);
QCOMPARE(release_count, click_count);
@ -199,7 +198,7 @@ void tst_QPushButton::autoRepeat()
// check that pressing ENTER has no effect
resetCounters();
testWidget->setDown( false );
testWidget->setDown(false);
// Skip after reset if ButtonPressKeys has Key_Enter
const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
->themeHint(QPlatformTheme::ButtonPressKeys)
@ -207,40 +206,40 @@ void tst_QPushButton::autoRepeat()
if (buttonPressKeys.contains(Qt::Key_Enter)) {
return;
}
testWidget->setAutoRepeat( false );
QTest::keyPress( testWidget, Qt::Key_Enter );
testWidget->setAutoRepeat(false);
QTest::keyPress(testWidget, Qt::Key_Enter);
QTest::qWait( 300 );
QTest::qWait(300);
QVERIFY( !testWidget->isDown() );
QVERIFY( toggle_count == 0 );
QVERIFY( press_count == 0 );
QVERIFY( release_count == 0 );
QVERIFY( click_count == 0 );
QTest::keyRelease( testWidget, Qt::Key_Enter );
QVERIFY(!testWidget->isDown());
QCOMPARE(toggle_count, 0);
QCOMPARE(press_count, 0);
QCOMPARE(release_count, 0);
QCOMPARE(click_count, 0);
QTest::keyRelease(testWidget, Qt::Key_Enter);
// check that pressing ENTER has no effect
resetCounters();
testWidget->setDown( false );
testWidget->setAutoRepeat( true );
QTest::keyClick( testWidget, Qt::Key_Enter );
QTest::qWait( 300 );
QVERIFY( !testWidget->isDown() );
QVERIFY( toggle_count == 0 );
QVERIFY( press_count == 0 );
QVERIFY( release_count == 0 );
QVERIFY( click_count == 0 );
testWidget->setDown(false);
testWidget->setAutoRepeat(true);
QTest::keyClick(testWidget, Qt::Key_Enter);
QTest::qWait(300);
QVERIFY(!testWidget->isDown());
QCOMPARE(toggle_count, 0);
QCOMPARE(press_count, 0);
QCOMPARE(release_count, 0);
QCOMPARE(click_count, 0);
}
void tst_QPushButton::pressed()
{
QTest::keyPress( testWidget, ' ' );
QCOMPARE( press_count, (uint)1 );
QCOMPARE( release_count, (uint)0 );
QTest::keyPress(testWidget, ' ');
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 0);
QTest::keyRelease( testWidget, ' ' );
QCOMPARE( press_count, (uint)1 );
QCOMPARE( release_count, (uint)1 );
QTest::keyRelease(testWidget, ' ');
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 1);
// Skip if ButtonPressKeys has Key_Enter
const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()
@ -250,77 +249,77 @@ void tst_QPushButton::pressed()
return;
}
QTest::keyPress( testWidget,Qt::Key_Enter );
QCOMPARE( press_count, (uint)1 );
QCOMPARE( release_count, (uint)1 );
QTest::keyPress(testWidget,Qt::Key_Enter);
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 1);
testWidget->setAutoDefault(true);
QTest::keyPress( testWidget,Qt::Key_Enter );
QCOMPARE( press_count, (uint)2 );
QCOMPARE( release_count, (uint)2 );
QTest::keyPress(testWidget,Qt::Key_Enter);
QCOMPARE(press_count, 2);
QCOMPARE(release_count, 2);
testWidget->setAutoDefault(false);
}
void tst_QPushButton::isCheckable()
{
QVERIFY( !testWidget->isCheckable() );
QVERIFY(!testWidget->isCheckable());
}
void tst_QPushButton::setDown()
{
testWidget->setDown( false );
QVERIFY( !testWidget->isDown() );
testWidget->setDown(false);
QVERIFY(!testWidget->isDown());
testWidget->setDown( true );
QVERIFY( testWidget->isDown() );
testWidget->setDown(true);
QVERIFY(testWidget->isDown());
testWidget->setDown( true );
QTest::keyClick( testWidget, Qt::Key_Escape );
QVERIFY( !testWidget->isDown() );
testWidget->setDown(true);
QTest::keyClick(testWidget, Qt::Key_Escape);
QVERIFY(!testWidget->isDown());
}
void tst_QPushButton::isChecked()
{
testWidget->setDown( false );
QVERIFY( !testWidget->isChecked() );
testWidget->setDown(false);
QVERIFY(!testWidget->isChecked());
testWidget->setDown( true );
QVERIFY( !testWidget->isChecked() );
testWidget->setDown(true);
QVERIFY(!testWidget->isChecked());
testWidget->setDown( false );
testWidget->setDown(false);
testWidget->toggle();
QVERIFY( testWidget->isChecked() == testWidget->isCheckable() );
QCOMPARE(testWidget->isChecked(), testWidget->isCheckable());
}
void tst_QPushButton::toggle()
{
// the pushbutton shouldn't toggle the button.
testWidget->toggle();
QVERIFY( testWidget->isChecked() == false );
QCOMPARE(testWidget->isChecked(), false);
}
void tst_QPushButton::toggled()
{
// the pushbutton shouldn't send a toggled signal when we call the toggle slot.
QVERIFY( !testWidget->isCheckable() );
QVERIFY(!testWidget->isCheckable());
testWidget->toggle();
QVERIFY( toggle_count == 0 );
QCOMPARE(toggle_count, 0);
// do it again, just to be sure
resetCounters();
testWidget->toggle();
QVERIFY( toggle_count == 0 );
QCOMPARE(toggle_count, 0);
// finally check that we can toggle using the mouse
resetCounters();
QTest::mousePress( testWidget, Qt::LeftButton );
QVERIFY( toggle_count == 0 );
QVERIFY( click_count == 0 );
QTest::mousePress(testWidget, Qt::LeftButton);
QCOMPARE(toggle_count, 0);
QCOMPARE(click_count, 0);
QTest::mouseRelease( testWidget, Qt::LeftButton );
QVERIFY( click_count == 1 );
QTest::mouseRelease(testWidget, Qt::LeftButton);
QCOMPARE(click_count, 1);
}
#if QT_CONFIG(shortcut)
@ -333,56 +332,56 @@ void tst_QPushButton::toggled()
void tst_QPushButton::setAccel()
{
testWidget->setText("&AccelTest");
QKeySequence seq( Qt::ALT | Qt::Key_A );
testWidget->setShortcut( seq );
QKeySequence seq(Qt::ALT | Qt::Key_A);
testWidget->setShortcut(seq);
// The shortcut will not be activated unless the button is in a active
// window and has focus
QApplicationPrivate::setActiveWindow(testWidget);
testWidget->setFocus();
QVERIFY(QTest::qWaitForWindowActive(testWidget));
QTest::keyClick( testWidget, 'A', Qt::AltModifier );
QTRY_VERIFY( click_count == 1 );
QVERIFY( press_count == 1 );
QVERIFY( release_count == 1 );
QVERIFY( toggle_count == 0 );
QTest::keyClick(testWidget, 'A', Qt::AltModifier);
QTRY_VERIFY(click_count == 1);
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 1);
QCOMPARE(toggle_count, 0);
// wait 200 ms because setAccel uses animateClick.
// if we don't wait this may screw up a next test.
QTest::qWait(200);
QTRY_VERIFY( !testWidget->isDown() );
QTRY_VERIFY(!testWidget->isDown());
}
#endif // QT_CONFIG(shortcut)
void tst_QPushButton::clicked()
{
QTest::mousePress( testWidget, Qt::LeftButton );
QVERIFY( press_count == 1 );
QVERIFY( release_count == 0 );
QTest::mousePress(testWidget, Qt::LeftButton);
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 0);
QTest::mouseRelease( testWidget, Qt::LeftButton );
QCOMPARE( press_count, (uint)1 );
QCOMPARE( release_count, (uint)1 );
QTest::mouseRelease(testWidget, Qt::LeftButton);
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 1);
press_count = 0;
release_count = 0;
testWidget->setDown(false);
for (uint i=0; i<10; i++)
QTest::mouseClick( testWidget, Qt::LeftButton );
QCOMPARE( press_count, (uint)10 );
QCOMPARE( release_count, (uint)10 );
QTest::mouseClick(testWidget, Qt::LeftButton);
QCOMPARE(press_count, 10);
QCOMPARE(release_count, 10);
}
void tst_QPushButton::touchTap()
{
QTest::touchEvent(testWidget, m_touchScreen).press(0, QPoint(10, 10));
QVERIFY( press_count == 1 );
QVERIFY( release_count == 0 );
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 0);
QTest::touchEvent(testWidget, m_touchScreen).release(0, QPoint(10, 10));
QCOMPARE( press_count, (uint)1 );
QCOMPARE( release_count, (uint)1 );
QCOMPARE( click_count, (uint)1 );
QCOMPARE(press_count, 1);
QCOMPARE(release_count, 1);
QCOMPARE(click_count, 1);
press_count = 0;
release_count = 0;
@ -392,26 +391,23 @@ void tst_QPushButton::touchTap()
QTest::touchEvent(testWidget, m_touchScreen).press(0, QPoint(10, 10));
QTest::touchEvent(testWidget, m_touchScreen).release(0, QPoint(10, 10));
}
QCOMPARE( press_count, (uint)10 );
QCOMPARE( release_count, (uint)10 );
QCOMPARE( click_count, (uint)10 );
}
QPushButton *pb = 0;
void tst_QPushButton::helperSlotDelete()
{
delete pb;
pb = 0;
QCOMPARE(press_count, 10);
QCOMPARE(release_count, 10);
QCOMPARE(click_count, 10);
}
void tst_QPushButton::popupCrash()
{
pb = new QPushButton("foo");
QPushButton *pb = new QPushButton("foo");
QMenu *menu = new QMenu("bar", pb);
pb->setMenu(menu);
QTimer::singleShot(1000, this, SLOT(helperSlotDelete()));
QTimer::singleShot(1000, this, [&pb]{
delete pb;
pb = nullptr;
});
pb->show();
pb->click();
QTRY_COMPARE(pb, nullptr);
}
void tst_QPushButton::defaultAndAutoDefault()
@ -501,14 +497,14 @@ void tst_QPushButton::defaultAndAutoDefault()
// Reparenting
QVERIFY(button2.autoDefault());
button2.setParent(0);
button2.setParent(nullptr);
QVERIFY(!button2.autoDefault());
button2.setAutoDefault(false);
button2.setParent(&dialog);
QVERIFY(!button2.autoDefault());
button1.setAutoDefault(true);
button1.setParent(0);
button1.setParent(nullptr);
QVERIFY(button1.autoDefault());
}
}
@ -549,7 +545,7 @@ void tst_QPushButton::sizeHint()
button->setParent(widget);
button->sizeHint();
widget->setParent(0);
widget->setParent(nullptr);
delete dialog;
button->setDefault(false);
QCOMPARE(button->sizeHint(), initSizeHint);