From 4209fba25612d93070976a597d2fa01325631e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Arve=20S=C3=A6ther?= Date: Fri, 26 Jun 2009 11:08:16 +0200 Subject: Autotest for checking if QWidget::setStyle() forces a relayout. --- tests/auto/qboxlayout/tst_qboxlayout.cpp | 79 +++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/tests/auto/qboxlayout/tst_qboxlayout.cpp b/tests/auto/qboxlayout/tst_qboxlayout.cpp index f34fc00..be6f3dd 100644 --- a/tests/auto/qboxlayout/tst_qboxlayout.cpp +++ b/tests/auto/qboxlayout/tst_qboxlayout.cpp @@ -65,8 +65,53 @@ private slots: void sizeHint(); void sizeConstraints(); void setGeometry(); + void setStyleShouldChangeSpacing(); }; +class CustomLayoutStyle : public QWindowsStyle +{ + Q_OBJECT +public: + CustomLayoutStyle() : QWindowsStyle() + { + hspacing = 5; + vspacing = 10; + } + + virtual int pixelMetric(PixelMetric metric, const QStyleOption * option = 0, + const QWidget * widget = 0 ) const; + + int hspacing; + int vspacing; +}; + +int CustomLayoutStyle::pixelMetric(PixelMetric metric, const QStyleOption * option /*= 0*/, + const QWidget * widget /*= 0*/ ) const +{ + switch (metric) { + case PM_LayoutLeftMargin: + return 0; + break; + case PM_LayoutTopMargin: + return 3; + break; + case PM_LayoutRightMargin: + return 6; + break; + case PM_LayoutBottomMargin: + return 9; + break; + case PM_LayoutHorizontalSpacing: + return hspacing; + case PM_LayoutVerticalSpacing: + return vspacing; + break; + default: + break; + } + return QWindowsStyle::pixelMetric(metric, option, widget); +} + tst_QBoxLayout::tst_QBoxLayout() { @@ -163,12 +208,44 @@ void tst_QBoxLayout::setGeometry() lay->addLayout(lay2); w.setLayout(lay); w.show(); - + QRect newGeom(0, 0, 70, 70); lay2->setGeometry(newGeom); QApplication::processEvents(); QVERIFY2(newGeom.contains(dial->geometry()), "dial->geometry() should be smaller and within newGeom"); } +void tst_QBoxLayout::setStyleShouldChangeSpacing() +{ + + QWidget *window = new QWidget; + QHBoxLayout *hbox = new QHBoxLayout(window); + 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"));; + hbox->addWidget(pb1); + hbox->addWidget(pb2); + CustomLayoutStyle *style1 = new CustomLayoutStyle; + style1->hspacing = 6; + window->setStyle(style1); + window->show(); + + QTest::qWait(100); + int spacing = pb2->geometry().left() - pb1->geometry().right() - 1; + QCOMPARE(spacing, 6); + + CustomLayoutStyle *style2 = new CustomLayoutStyle(); + style2->hspacing = 10; + window->setStyle(style2); + QTest::qWait(100); + spacing = pb2->geometry().left() - pb1->geometry().right() - 1; + QEXPECT_FAIL("", "Fix for next minor release", Continue); + QCOMPARE(spacing, 10); + + delete window; + delete style1; + delete style2; +} + + QTEST_MAIN(tst_QBoxLayout) #include "tst_qboxlayout.moc" -- cgit v0.12 From 937e92fe83436c0d02e1a3c1783fc896ffae67ad Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 25 Jun 2009 17:06:51 +0200 Subject: Clean up QSharedPointer cast implementations. There's no need to have separate cast-checker functions, plus the cast- and centralise the function to create the object. Reviewed-by: TrustMe --- src/corelib/tools/qsharedpointer_impl.h | 76 +++++++++------------------------ 1 file changed, 19 insertions(+), 57 deletions(-) diff --git a/src/corelib/tools/qsharedpointer_impl.h b/src/corelib/tools/qsharedpointer_impl.h index 0471a1b..2797622 100644 --- a/src/corelib/tools/qsharedpointer_impl.h +++ b/src/corelib/tools/qsharedpointer_impl.h @@ -87,9 +87,8 @@ namespace QtSharedPointer { template class InternalRefCount; template class ExternalRefCount; - template QSharedPointer qStrongRefFromWeakHelper(const QWeakPointer &, X*); - template QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *); - template QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *); + template QSharedPointer strongRefFromWeakHelper(const QWeakPointer &, X*); + template QSharedPointer copyAndSetPointer(X * ptr, const QSharedPointer &src); // used in debug mode to verify the reuse of pointers Q_CORE_EXPORT void internalSafetyCheckAdd(const volatile void *); @@ -239,10 +238,8 @@ namespace QtSharedPointer { #else template friend class ExternalRefCount; template friend class QWeakPointer; - template friend QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer qSharedPointerDynamicCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *); - template friend QSharedPointer QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer &src, X *); + template friend QSharedPointer copyAndSetPointer(X * ptr, const QSharedPointer &src); + template friend QSharedPointer QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -292,9 +289,9 @@ public: } inline QSharedPointer(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); } inline QSharedPointer &operator=(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); return *this; } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); return *this; } template inline QSharedPointer(const QSharedPointer &other) { *this = other; } @@ -309,11 +306,11 @@ public: template inline QSharedPointer(const QWeakPointer &other) - { *this = QtSharedPointer::qStrongRefFromWeakHelper(other, static_cast(0)); } + { *this = QtSharedPointer::strongRefFromWeakHelper(other, static_cast(0)); } template inline QSharedPointer &operator=(const QWeakPointer &other) - { *this = qStrongRefFromWeakHelper(other, static_cast(0)); return *this; } + { *this = strongRefFromWeakHelper(other, static_cast(0)); return *this; } template QSharedPointer staticCast() const @@ -417,7 +414,7 @@ private: #if defined(Q_NO_TEMPLATE_FRIENDS) public: #else - template friend QSharedPointer QtSharedPointer::qStrongRefFromWeakHelper(const QWeakPointer &src, X *); + template friend QSharedPointer QtSharedPointer::strongRefFromWeakHelper(const QWeakPointer &src, X *); #endif inline void internalSet(Data *o, T *actual) @@ -488,47 +485,14 @@ Q_INLINE_TEMPLATE QWeakPointer QSharedPointer::toWeakRef() const namespace QtSharedPointer { // helper functions: template - Q_INLINE_TEMPLATE X *qVerifyStaticCast(T *src, X *) - { - return static_cast(src); // if you get an error in this line, the cast is invalid - } - template - Q_INLINE_TEMPLATE X *qVerifyDynamicCast(T *src, X *) - { - return dynamic_cast(src); // if you get an error in this line, the cast is invalid - } - template - Q_INLINE_TEMPLATE X *qVerifyConstCast(T *src, X *) - { - return const_cast(src); // if you get an error in this line, the cast is invalid - } - - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCastHelper(const QSharedPointer &src, X *) - { - QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, static_cast(ptr)); - return result; - } - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCastHelper(const QSharedPointer &src, X *) - { - QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, dynamic_cast(ptr)); - return result; - } - template - Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCastHelper(const QSharedPointer &src, X *) + Q_INLINE_TEMPLATE QSharedPointer copyAndSetPointer(X *ptr, const QSharedPointer &src) { QSharedPointer result; - register T *ptr = src.data(); - result.internalSet(src.d, const_cast(ptr)); + result.internalSet(src.d, ptr); return result; } template - Q_INLINE_TEMPLATE QSharedPointer qStrongRefFromWeakHelper + Q_INLINE_TEMPLATE QSharedPointer strongRefFromWeakHelper (const QT_PREPEND_NAMESPACE(QWeakPointer) &src, X *) { QSharedPointer result; @@ -541,9 +505,8 @@ namespace QtSharedPointer { template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QSharedPointer &src) { - X *x = 0; - QtSharedPointer::qVerifyStaticCast(src.data(), x); - return QtSharedPointer::qSharedPointerCastHelper(src, x); + register X *ptr = static_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QWeakPointer &src) @@ -554,8 +517,8 @@ Q_INLINE_TEMPLATE QSharedPointer qSharedPointerCast(const QWeakPointer &sr template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QSharedPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerDynamicCastHelper(src, x); + register X *ptr = dynamic_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QWeakPointer &src) @@ -566,14 +529,13 @@ Q_INLINE_TEMPLATE QSharedPointer qSharedPointerDynamicCast(const QWeakPointer template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCast(const QSharedPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + register X *ptr = const_cast(src.data()); // if you get an error in this line, the cast is invalid + return QtSharedPointer::copyAndSetPointer(ptr, src); } template Q_INLINE_TEMPLATE QSharedPointer qSharedPointerConstCast(const QWeakPointer &src) { - X *x = 0; - return QtSharedPointer::qSharedPointerConstCastHelper(src, x); + return qSharedPointerConstCast(src.toStrongRef()); } template -- cgit v0.12 From 206fc25d734f01d2812653d78eaa625cd20b34e3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 25 Jun 2009 18:13:58 +0200 Subject: Autotest: Fix a few test names and allow me to see the compilation errors Reviewed-by: TrustMe --- tests/auto/qsharedpointer/tst_qsharedpointer.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp index a11164f..db93fc9 100644 --- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp +++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp @@ -908,7 +908,7 @@ void tst_QSharedPointer::invalidConstructs_data() QTest::newRow("implicit-initialization3") << &QTest::QExternalTest::tryCompileFail << "QWeakPointer ptr = new Data;"; - QTest::newRow("implicit-initialization1") + QTest::newRow("implicit-initialization4") << &QTest::QExternalTest::tryCompileFail << "QWeakPointer ptr;" "ptr = new Data;"; @@ -972,11 +972,13 @@ void tst_QSharedPointer::invalidConstructs_data() << &QTest::QExternalTest::tryCompileFail << "QSharedPointer ptr1;\n" "QSharedPointer ptr2 = qSharedPointerCast(ptr1);"; +#ifndef QTEST_NO_RTTI QTest::newRow("invalid-cast2") << &QTest::QExternalTest::tryCompileFail << "QSharedPointer ptr1;\n" "QSharedPointer ptr2 = qSharedPointerDynamicCast(ptr1);"; - QTest::newRow("implicit-initialization1") +#endif + QTest::newRow("invalid-cast3") << &QTest::QExternalTest::tryCompileFail << "QSharedPointer ptr1;\n" "QSharedPointer ptr2 = qSharedPointerConstCast(ptr1);"; @@ -1024,7 +1026,12 @@ void tst_QSharedPointer::invalidConstructs() QByteArray body = code.toLatin1(); - if (!(test.*testFunction)(body)) { + bool result = (test.*testFunction)(body); + if (qgetenv("QTEST_EXTERNAL_DEBUG").toInt() > 0) { + qDebug("External test output:"); + printf("%s\n", test.standardError().constData()); + } + if (!result) { qWarning("External code testing failed\nCode:\n%s\n", body.constData()); QFAIL("Fail"); } -- cgit v0.12