tst_qaction: Fix top level widget leaks.

Replace the pointer member test widge by a widget
instiantiated on the stack where needed.
Remove empty functions.

Change-Id: Ie2da1d88bb23e56e85a2c57cc28220d74c63fae1
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-06-11 12:08:30 +02:00 committed by Friedemann Kleint
parent 9ab11475a0
commit 3eb3007e9c

View File

@ -42,15 +42,12 @@ class tst_QAction : public QObject
public: public:
tst_QAction(); tst_QAction();
virtual ~tst_QAction();
void updateState(QActionEvent *e); void updateState(QActionEvent *e);
public slots:
void initTestCase();
void cleanupTestCase();
private slots: private slots:
void init();
void cleanup();
void getSetCheck(); void getSetCheck();
void setText_data(); void setText_data();
void setText(); void setText();
@ -68,11 +65,26 @@ private slots:
private: private:
int m_lastEventType; int m_lastEventType;
int m_keyboardScheme; const int m_keyboardScheme;
QAction *m_lastAction; QAction *m_lastAction;
QWidget *m_tstWidget;
}; };
tst_QAction::tst_QAction()
: m_keyboardScheme(QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::KeyboardScheme).toInt())
{
}
void tst_QAction::init()
{
m_lastEventType = 0;
m_lastAction = nullptr;
}
void tst_QAction::cleanup()
{
QVERIFY(QApplication::topLevelWidgets().isEmpty());
}
// Testing get/set functions // Testing get/set functions
void tst_QAction::getSetCheck() void tst_QAction::getSetCheck()
{ {
@ -104,46 +116,16 @@ class MyWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
MyWidget(tst_QAction *tst, QWidget *parent = 0) : QWidget(parent) { this->tst = tst; } explicit MyWidget(tst_QAction *tst, QWidget *parent = nullptr) : QWidget(parent), m_test(tst)
{ setWindowTitle(QTest::currentTestFunction()); }
protected: protected:
virtual void actionEvent(QActionEvent *e) { tst->updateState(e); } void actionEvent(QActionEvent *e) override { m_test->updateState(e); }
private: private:
tst_QAction *tst; tst_QAction *m_test;
}; };
tst_QAction::tst_QAction() : m_keyboardScheme(QPlatformTheme::WindowsKeyboardScheme)
{
if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme())
m_keyboardScheme = theme->themeHint(QPlatformTheme::KeyboardScheme).toInt();
}
tst_QAction::~tst_QAction()
{
}
void tst_QAction::initTestCase()
{
m_lastEventType = 0;
m_lastAction = 0;
MyWidget *mw = new MyWidget(this);
m_tstWidget = mw;
mw->show();
qApp->setActiveWindow(mw);
}
void tst_QAction::cleanupTestCase()
{
QWidget *testWidget = m_tstWidget;
if (testWidget) {
testWidget->hide();
delete testWidget;
}
}
void tst_QAction::setText_data() void tst_QAction::setText_data()
{ {
QTest::addColumn<QString>("text"); QTest::addColumn<QString>("text");
@ -208,7 +190,10 @@ void tst_QAction::actionEvent()
a.setText("action text"); a.setText("action text");
// add action // add action
m_tstWidget->addAction(&a); MyWidget testWidget(this);
testWidget.show();
QApplication::setActiveWindow(&testWidget);
testWidget.addAction(&a);
qApp->processEvents(); qApp->processEvents();
QCOMPARE(m_lastEventType, (int)QEvent::ActionAdded); QCOMPARE(m_lastEventType, (int)QEvent::ActionAdded);
@ -222,7 +207,7 @@ void tst_QAction::actionEvent()
QCOMPARE(m_lastAction, &a); QCOMPARE(m_lastAction, &a);
// remove action // remove action
m_tstWidget->removeAction(&a); testWidget.removeAction(&a);
qApp->processEvents(); qApp->processEvents();
QCOMPARE(m_lastEventType, (int)QEvent::ActionRemoved); QCOMPARE(m_lastEventType, (int)QEvent::ActionRemoved);
@ -262,22 +247,24 @@ void tst_QAction::alternateShortcuts()
{ {
//test the alternate shortcuts (by adding more than 1 shortcut) //test the alternate shortcuts (by adding more than 1 shortcut)
QWidget *wid = m_tstWidget; MyWidget testWidget(this);
testWidget.show();
QApplication::setActiveWindow(&testWidget);
{ {
QAction act(wid); QAction act(&testWidget);
wid->addAction(&act); testWidget.addAction(&act);
QList<QKeySequence> shlist = QList<QKeySequence>() << QKeySequence("CTRL+P") << QKeySequence("CTRL+A"); QList<QKeySequence> shlist = QList<QKeySequence>() << QKeySequence("CTRL+P") << QKeySequence("CTRL+A");
act.setShortcuts(shlist); act.setShortcuts(shlist);
QSignalSpy spy(&act, SIGNAL(triggered())); QSignalSpy spy(&act, SIGNAL(triggered()));
act.setAutoRepeat(true); act.setAutoRepeat(true);
QTest::keyClick(wid, Qt::Key_A, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier);
QCOMPARE(spy.count(), 1); //act should have been triggered QCOMPARE(spy.count(), 1); //act should have been triggered
act.setAutoRepeat(false); act.setAutoRepeat(false);
QTest::keyClick(wid, Qt::Key_A, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier);
QCOMPARE(spy.count(), 2); //act should have been triggered a 2nd time QCOMPARE(spy.count(), 2); //act should have been triggered a 2nd time
//end of the scope of the action, it will be destroyed and removed from wid //end of the scope of the action, it will be destroyed and removed from wid
@ -286,11 +273,15 @@ void tst_QAction::alternateShortcuts()
//this tests a crash (if the action did not unregister its alternate shortcuts) //this tests a crash (if the action did not unregister its alternate shortcuts)
QTest::keyClick(wid, Qt::Key_A, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_A, Qt::ControlModifier);
} }
void tst_QAction::enabledVisibleInteraction() void tst_QAction::enabledVisibleInteraction()
{ {
MyWidget testWidget(this);
testWidget.show();
QApplication::setActiveWindow(&testWidget);
QAction act(0); QAction act(0);
// check defaults // check defaults
QVERIFY(act.isEnabled()); QVERIFY(act.isEnabled());
@ -305,20 +296,20 @@ void tst_QAction::enabledVisibleInteraction()
QVERIFY(act.isVisible()); QVERIFY(act.isVisible());
// check if shortcut is disabled if not visible // check if shortcut is disabled if not visible
m_tstWidget->addAction(&act); testWidget.addAction(&act);
act.setShortcut(QKeySequence("Ctrl+T")); act.setShortcut(QKeySequence("Ctrl+T"));
QSignalSpy spy(&act, SIGNAL(triggered())); QSignalSpy spy(&act, SIGNAL(triggered()));
act.setEnabled(true); act.setEnabled(true);
act.setVisible(false); act.setVisible(false);
QTest::keyClick(m_tstWidget, Qt::Key_T, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier);
QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger
act.setVisible(false); act.setVisible(false);
act.setEnabled(true); act.setEnabled(true);
QTest::keyClick(m_tstWidget, Qt::Key_T, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier);
QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger QCOMPARE(spy.count(), 0); //act is not visible, so don't trigger
act.setVisible(true); act.setVisible(true);
act.setEnabled(true); act.setEnabled(true);
QTest::keyClick(m_tstWidget, Qt::Key_T, Qt::ControlModifier); QTest::keyClick(&testWidget, Qt::Key_T, Qt::ControlModifier);
QCOMPARE(spy.count(), 1); //act is visible and enabled, so trigger QCOMPARE(spy.count(), 1); //act is visible and enabled, so trigger
} }
@ -378,38 +369,42 @@ void tst_QAction::task229128TriggeredSignalWhenInActiongroup()
void tst_QAction::repeat() void tst_QAction::repeat()
{ {
QWidget *wid = m_tstWidget; MyWidget testWidget(this);
QAction act(wid); testWidget.show();
wid->addAction(&act); QApplication::setActiveWindow(&testWidget);
QVERIFY(QTest::qWaitForWindowActive(&testWidget));
QAction act(&testWidget);
testWidget.addAction(&act);
act.setShortcut(QKeySequence(Qt::Key_F)); act.setShortcut(QKeySequence(Qt::Key_F));
QSignalSpy spy(&act, SIGNAL(triggered())); QSignalSpy spy(&act, SIGNAL(triggered()));
act.setAutoRepeat(true); act.setAutoRepeat(true);
QTest::keyPress(wid, Qt::Key_F); QTest::keyPress(&testWidget, Qt::Key_F);
QTest::keyRelease(wid, Qt::Key_F); QTest::keyRelease(&testWidget, Qt::Key_F);
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
spy.clear(); spy.clear();
QTest::keyPress(wid, Qt::Key_F); QTest::keyPress(&testWidget, Qt::Key_F);
// repeat event // repeat event
QTest::simulateEvent(wid, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true);
QTest::simulateEvent(wid, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true);
QTest::keyRelease(wid, Qt::Key_F); QTest::keyRelease(&testWidget, Qt::Key_F);
QCOMPARE(spy.count(), 3); QCOMPARE(spy.count(), 3);
spy.clear(); spy.clear();
act.setAutoRepeat(false); act.setAutoRepeat(false);
QTest::keyPress(wid, Qt::Key_F); QTest::keyPress(&testWidget, Qt::Key_F);
QTest::simulateEvent(wid, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true);
QTest::simulateEvent(wid, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true);
QTest::keyRelease(wid, Qt::Key_F); QTest::keyRelease(&testWidget, Qt::Key_F);
QCOMPARE(spy.count(), 1); QCOMPARE(spy.count(), 1);
spy.clear(); spy.clear();
act.setAutoRepeat(true); act.setAutoRepeat(true);
QTest::keyPress(wid, Qt::Key_F); QTest::keyPress(&testWidget, Qt::Key_F);
QTest::simulateEvent(wid, true, Qt::Key_F, Qt::NoModifier, QString("f"), true); QTest::simulateEvent(&testWidget, true, Qt::Key_F, Qt::NoModifier, QString("f"), true);
QTest::keyRelease(wid, Qt::Key_F); QTest::keyRelease(&testWidget, Qt::Key_F);
QCOMPARE(spy.count(), 2); QCOMPARE(spy.count(), 2);
} }