tst_qboxlayout: Fix top level widget leaks.

Instantiate widgets on stack and add cleanup function for the check.

Change-Id: Ia527c228f9173d1b5aeba94ba4e14e1beba60731
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Jan Arve Sæther <jan-arve.saether@theqtcompany.com>
This commit is contained in:
Friedemann Kleint 2015-06-11 12:16:39 +02:00
parent 75dfebae4a
commit e849e2c162

View File

@ -48,17 +48,8 @@ class tst_QBoxLayout : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
tst_QBoxLayout();
virtual ~tst_QBoxLayout();
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
private slots: private slots:
void cleanup();
void insertSpacerItem(); void insertSpacerItem();
void insertLayout(); void insertLayout();
void sizeHint(); void sizeHint();
@ -120,34 +111,15 @@ int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * opti
return QProxyStyle::pixelMetric(metric, option, widget); return QProxyStyle::pixelMetric(metric, option, widget);
} }
tst_QBoxLayout::tst_QBoxLayout()
{
}
tst_QBoxLayout::~tst_QBoxLayout()
{
}
void tst_QBoxLayout::initTestCase()
{
}
void tst_QBoxLayout::cleanupTestCase()
{
}
void tst_QBoxLayout::init()
{
}
void tst_QBoxLayout::cleanup() void tst_QBoxLayout::cleanup()
{ {
QVERIFY(QApplication::topLevelWidgets().isEmpty());
} }
void tst_QBoxLayout::insertSpacerItem() void tst_QBoxLayout::insertSpacerItem()
{ {
QWidget *window = new QWidget; QWidget window;
window.setWindowTitle(QTest::currentTestFunction());
QSpacerItem *spacer1 = new QSpacerItem(20, 10, QSizePolicy::Expanding, QSizePolicy::Expanding); QSpacerItem *spacer1 = new QSpacerItem(20, 10, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *spacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Expanding); QSpacerItem *spacer2 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Expanding);
@ -157,44 +129,43 @@ void tst_QBoxLayout::insertSpacerItem()
layout->addSpacerItem(spacer1); layout->addSpacerItem(spacer1);
layout->addWidget(new QLineEdit("Baaaaaaaaaaaaaaaaaaaaaaaaar")); layout->addWidget(new QLineEdit("Baaaaaaaaaaaaaaaaaaaaaaaaar"));
layout->insertSpacerItem(0, spacer2); layout->insertSpacerItem(0, spacer2);
window->setLayout(layout); window.setLayout(layout);
QVERIFY(layout->itemAt(0) == spacer2); QVERIFY(layout->itemAt(0) == spacer2);
QVERIFY(layout->itemAt(2) == spacer1); QVERIFY(layout->itemAt(2) == spacer1);
window->show(); window.show();
} }
void tst_QBoxLayout::insertLayout() void tst_QBoxLayout::insertLayout()
{ {
QWidget *window = new QWidget; QWidget window;
QVBoxLayout *vbox = new QVBoxLayout(window); QVBoxLayout *vbox = new QVBoxLayout(&window);
QVBoxLayout *dummyParentLayout = new QVBoxLayout; QScopedPointer<QVBoxLayout> dummyParentLayout(new QVBoxLayout);
QHBoxLayout *subLayout = new QHBoxLayout; QHBoxLayout *subLayout = new QHBoxLayout;
dummyParentLayout->addLayout(subLayout); dummyParentLayout->addLayout(subLayout);
QCOMPARE(subLayout->parent(), dummyParentLayout); QCOMPARE(subLayout->parent(), dummyParentLayout.data());
QCOMPARE(dummyParentLayout->count(), 1); QCOMPARE(dummyParentLayout->count(), 1);
// add subLayout to another layout // add subLayout to another layout
QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout \"\" already has a parent"); QTest::ignoreMessage(QtWarningMsg, "QLayout::addChildLayout: layout \"\" already has a parent");
vbox->addLayout(subLayout); vbox->addLayout(subLayout);
QCOMPARE((subLayout->parent() == vbox), (vbox->count() == 1)); QCOMPARE((subLayout->parent() == vbox), (vbox->count() == 1));
delete dummyParentLayout;
delete window;
} }
void tst_QBoxLayout::sizeHint() void tst_QBoxLayout::sizeHint()
{ {
QWidget *window = new QWidget; QWidget window;
window.setWindowTitle(QTest::currentTestFunction());
QHBoxLayout *lay1 = new QHBoxLayout; QHBoxLayout *lay1 = new QHBoxLayout;
QHBoxLayout *lay2 = new QHBoxLayout; QHBoxLayout *lay2 = new QHBoxLayout;
QLabel *label = new QLabel("widget twooooooooooooooooooooooooooooooooooooooooooooooooooooooo"); QLabel *label = new QLabel("widget twooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
lay2->addWidget(label); lay2->addWidget(label);
lay1->addLayout(lay2); lay1->addLayout(lay2);
window->setLayout(lay1); window.setLayout(lay1);
window->show(); window.show();
QTest::qWaitForWindowExposed(&window);
label->setText("foooooooo baaaaaaar"); label->setText("foooooooo baaaaaaar");
QSize sh = lay1->sizeHint(); QSize sh = lay1->sizeHint();
QApplication::processEvents(); QApplication::processEvents();
@ -207,24 +178,26 @@ void tst_QBoxLayout::sizeHint()
void tst_QBoxLayout::sizeConstraints() void tst_QBoxLayout::sizeConstraints()
{ {
QWidget *window = new QWidget; QWidget window;
window.setWindowTitle(QTest::currentTestFunction());
QHBoxLayout *lay = new QHBoxLayout; QHBoxLayout *lay = new QHBoxLayout;
lay->addWidget(new QLabel("foooooooooooooooooooooooooooooooooooo")); lay->addWidget(new QLabel("foooooooooooooooooooooooooooooooooooo"));
lay->addWidget(new QLabel("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar")); lay->addWidget(new QLabel("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar"));
lay->setSizeConstraint(QLayout::SetFixedSize); lay->setSizeConstraint(QLayout::SetFixedSize);
window->setLayout(lay); window.setLayout(lay);
window->show(); window.show();
QApplication::processEvents(); QTest::qWaitForWindowExposed(&window);
QSize sh = window->sizeHint(); QSize sh = window.sizeHint();
lay->takeAt(1); lay->takeAt(1);
QVERIFY(sh.width() >= window->sizeHint().width() && QVERIFY(sh.width() >= window.sizeHint().width() &&
sh.height() >= window->sizeHint().height()); sh.height() >= window.sizeHint().height());
} }
void tst_QBoxLayout::setGeometry() void tst_QBoxLayout::setGeometry()
{ {
QWidget toplevel; QWidget toplevel;
toplevel.setWindowTitle(QTest::currentTestFunction());
setFrameless(&toplevel); setFrameless(&toplevel);
QWidget w(&toplevel); QWidget w(&toplevel);
QVBoxLayout *lay = new QVBoxLayout; QVBoxLayout *lay = new QVBoxLayout;
@ -247,33 +220,30 @@ void tst_QBoxLayout::setGeometry()
void tst_QBoxLayout::setStyleShouldChangeSpacing() void tst_QBoxLayout::setStyleShouldChangeSpacing()
{ {
QWidget *window = new QWidget; QWidget window;
QHBoxLayout *hbox = new QHBoxLayout(window); window.setWindowTitle(QTest::currentTestFunction());
QHBoxLayout *hbox = new QHBoxLayout(&window);
QPushButton *pb1 = new QPushButton(tr("The spacing between this")); QPushButton *pb1 = new QPushButton(tr("The spacing between this"));
QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget"));; QPushButton *pb2 = new QPushButton(tr("and this button should depend on the style of the parent widget"));;
pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect); pb1->setAttribute(Qt::WA_LayoutUsesWidgetRect);
pb2->setAttribute(Qt::WA_LayoutUsesWidgetRect); pb2->setAttribute(Qt::WA_LayoutUsesWidgetRect);
hbox->addWidget(pb1); hbox->addWidget(pb1);
hbox->addWidget(pb2); hbox->addWidget(pb2);
CustomLayoutStyle *style1 = new CustomLayoutStyle; QScopedPointer<CustomLayoutStyle> style1(new CustomLayoutStyle);
style1->hspacing = 6; style1->hspacing = 6;
window->setStyle(style1); window.setStyle(style1.data());
window->show(); window.show();
QTest::qWaitForWindowExposed(&window);
QTest::qWait(100);
int spacing = pb2->geometry().left() - pb1->geometry().right() - 1; int spacing = pb2->geometry().left() - pb1->geometry().right() - 1;
QCOMPARE(spacing, 6); QCOMPARE(spacing, 6);
CustomLayoutStyle *style2 = new CustomLayoutStyle(); QScopedPointer<CustomLayoutStyle> style2(new CustomLayoutStyle());
style2->hspacing = 10; style2->hspacing = 10;
window->setStyle(style2); window.setStyle(style2.data());
QTest::qWait(100); QTest::qWait(100);
spacing = pb2->geometry().left() - pb1->geometry().right() - 1; spacing = pb2->geometry().left() - pb1->geometry().right() - 1;
QCOMPARE(spacing, 10); QCOMPARE(spacing, 10);
delete window;
delete style1;
delete style2;
} }
void tst_QBoxLayout::taskQTBUG_7103_minMaxWidthNotRespected() void tst_QBoxLayout::taskQTBUG_7103_minMaxWidthNotRespected()
@ -287,6 +257,7 @@ void tst_QBoxLayout::taskQTBUG_7103_minMaxWidthNotRespected()
layout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding)); layout->addSpacerItem(new QSpacerItem(1, 1, QSizePolicy::Fixed, QSizePolicy::Expanding));
QWidget widget; QWidget widget;
widget.setWindowTitle(QTest::currentTestFunction());
widget.setLayout(layout); widget.setLayout(layout);
widget.show(); widget.show();
QVERIFY(QTest::qWaitForWindowExposed(&widget)); QVERIFY(QTest::qWaitForWindowExposed(&widget));
@ -325,6 +296,7 @@ void tst_QBoxLayout::taskQTBUG_27420_takeAtShouldUnparentLayout()
void tst_QBoxLayout::taskQTBUG_40609_addingWidgetToItsOwnLayout(){ void tst_QBoxLayout::taskQTBUG_40609_addingWidgetToItsOwnLayout(){
QWidget widget; QWidget widget;
widget.setWindowTitle(QTest::currentTestFunction());
widget.setObjectName("347b469225a24a0ef05150a"); widget.setObjectName("347b469225a24a0ef05150a");
QVBoxLayout layout(&widget); QVBoxLayout layout(&widget);
layout.setObjectName("ef9e2b42298e0e6420105bb"); layout.setObjectName("ef9e2b42298e0e6420105bb");
@ -340,6 +312,7 @@ void tst_QBoxLayout::taskQTBUG_40609_addingWidgetToItsOwnLayout(){
void tst_QBoxLayout::taskQTBUG_40609_addingLayoutToItself(){ void tst_QBoxLayout::taskQTBUG_40609_addingLayoutToItself(){
QWidget widget; QWidget widget;
widget.setWindowTitle(QTest::currentTestFunction());
widget.setObjectName("fe44e5cb6c08006597126a"); widget.setObjectName("fe44e5cb6c08006597126a");
QVBoxLayout layout(&widget); QVBoxLayout layout(&widget);
layout.setObjectName("cc751dd0f50f62b05a62da"); layout.setObjectName("cc751dd0f50f62b05a62da");