diff options
author | Jan-Arve Saether <jan-arve.saether@nokia.com> | 2012-05-23 14:58:55 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-05-30 01:54:45 +0200 |
commit | 7cdcc267220db301080e7ade8e2f21e530e77b82 (patch) | |
tree | c4d4db9978c7d5985a75b8942eb37c3b7952e9b5 /tests | |
parent | ef204659b9c5d21c77a40c8bc11cd98039785303 (diff) |
Fix Qt 5 todo issues for QSizePolicy.
* Merge the two public ctors.
* Use bitflags instead of shifting bits (more readable).
* Add autotest
* Use int datatype for the "stretch setters". (values out of bounds are clamped)
Streaming to QDataStream will still use the Qt 4 format.
Task-number: QTBUG-25100
Change-Id: Iecb1e78cb12717e4d84448484c3ad8ca469d571a
Reviewed-by: Paul Olav Tvete <paul.tvete@nokia.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/widgets/kernel/kernel.pro | 1 | ||||
-rw-r--r-- | tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro | 6 | ||||
-rw-r--r-- | tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp | 224 |
3 files changed, 231 insertions, 0 deletions
diff --git a/tests/auto/widgets/kernel/kernel.pro b/tests/auto/widgets/kernel/kernel.pro index c2540ec229..14ebda21f9 100644 --- a/tests/auto/widgets/kernel/kernel.pro +++ b/tests/auto/widgets/kernel/kernel.pro @@ -14,5 +14,6 @@ SUBDIRS=\ qwidget_window \ qwidgetaction \ qshortcut \ + qsizepolicy SUBDIRS -= qsound diff --git a/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro b/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro new file mode 100644 index 0000000000..84629c7c0a --- /dev/null +++ b/tests/auto/widgets/kernel/qsizepolicy/qsizepolicy.pro @@ -0,0 +1,6 @@ +CONFIG += testcase +TARGET = tst_qsizepolicy + +QT += widgets widgets-private testlib + +SOURCES += tst_qsizepolicy.cpp diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp new file mode 100644 index 0000000000..7108c9decf --- /dev/null +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -0,0 +1,224 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> +#include <qsizepolicy.h> + +class tst_QSizePolicy : public QObject +{ +Q_OBJECT + +public: + tst_QSizePolicy(); + virtual ~tst_QSizePolicy(); + +private slots: + void getSetCheck(); + void dataStream(); + void horizontalStretch(); + void verticalStretch(); +}; + +tst_QSizePolicy::tst_QSizePolicy() +{ +} + +tst_QSizePolicy::~tst_QSizePolicy() +{ +} + + +// Testing get/set functions +void tst_QSizePolicy::getSetCheck() +{ + { + // check values of a default constructed QSizePolicy + QSizePolicy sp; + QCOMPARE(sp.horizontalPolicy(), QSizePolicy::Fixed); + QCOMPARE(sp.verticalPolicy(), QSizePolicy::Fixed); + QCOMPARE(sp.horizontalStretch(), 0); + QCOMPARE(sp.verticalStretch(), 0); + QCOMPARE(sp.verticalStretch(), 0); + QCOMPARE(sp.controlType(), QSizePolicy::DefaultType); + QCOMPARE(sp.hasHeightForWidth(), false); + QCOMPARE(sp.hasWidthForHeight(), false); + } + + { + static const QSizePolicy::Policy policies[3] = { + QSizePolicy::Fixed, + QSizePolicy::Minimum, + QSizePolicy::Ignored + }; + static const QSizePolicy::ControlType controlTypes[4] = { + QSizePolicy::DefaultType, + QSizePolicy::ButtonBox, + QSizePolicy::CheckBox, + QSizePolicy::ToolButton + }; + +#define ITEMCOUNT(arr) sizeof(arr)/sizeof(arr[0]) + QSizePolicy sp, oldsp; +#ifdef GENERATE_BASELINE + QFile out(QString::fromAscii("qsizepolicy-Qt%1%2.txt").arg((QT_VERSION >> 16) & 0xff).arg((QT_VERSION) >> 8 & 0xff)); + if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) { + QDataStream stream(&out); +#endif + /* Loop for permutating over the values most likely to trigger a bug: + - mininumum, maximum values + - Some values with LSB set, others with MSB unset. (check if shifts are ok) + + */ + // Look specifically for + for (int ihp = 0; ihp < ITEMCOUNT(policies); ++ihp) { + QSizePolicy::Policy hp = policies[ihp]; + for (int ivp = 0; ivp < ITEMCOUNT(policies); ++ivp) { + QSizePolicy::Policy vp = policies[ivp]; + for (int ict = 0; ict < ITEMCOUNT(controlTypes); ++ict) { + QSizePolicy::ControlType ct = controlTypes[ict]; + for (int hst= 0; hst <= 255; hst+=85) { //[0,85,170,255] + for (int vst = 0; vst <= 255; vst+=85) { + for (int j = 0; j < 3; ++j) { + bool hfw = j & 1; + bool wfh = j & 2; // cannot set hfw and wfh at the same time + oldsp = sp; + for (int i = 0; i < 5; ++i) { + switch (i) { + case 0: sp.setHorizontalPolicy(hp); break; + case 1: sp.setVerticalPolicy(vp); break; + case 2: sp.setHorizontalStretch(hst); break; + case 3: sp.setVerticalStretch(vst); break; + case 4: sp.setControlType(ct); break; + case 5: sp.setHeightForWidth(hfw); sp.setWidthForHeight(wfh); break; + default: break; + } + QCOMPARE(sp.horizontalPolicy(), (i >= 0 ? hp : oldsp.horizontalPolicy())); + QCOMPARE(sp.verticalPolicy(), (i >= 1 ? vp : oldsp.verticalPolicy())); + QCOMPARE(sp.horizontalStretch(), (i >= 2 ? hst : oldsp.horizontalStretch())); + QCOMPARE(sp.verticalStretch(), (i >= 3 ? vst : oldsp.verticalStretch())); + QCOMPARE(sp.controlType(), (i >= 4 ? ct : oldsp.controlType())); + QCOMPARE(sp.hasHeightForWidth(), (i >= 5 ? hfw : oldsp.hasHeightForWidth())); + QCOMPARE(sp.hasWidthForHeight(), (i >= 5 ? wfh : oldsp.hasWidthForHeight())); + + Qt::Orientations orients; + if (sp.horizontalPolicy() & QSizePolicy::ExpandFlag) + orients |= Qt::Horizontal; + if (sp.verticalPolicy() & QSizePolicy::ExpandFlag) + orients |= Qt::Vertical; + + QCOMPARE(sp.expandingDirections(), orients); +#ifdef GENERATE_BASELINE + stream << sp; +#endif + } + } + } + } + } + } + } +#ifdef GENERATE_BASELINE + out.close(); + } +#endif + } +} + +void tst_QSizePolicy::dataStream() +{ + QByteArray data; + QSizePolicy sp(QSizePolicy::Minimum, QSizePolicy::Expanding); + { + QDataStream stream(&data, QIODevice::ReadWrite); + sp.setHorizontalStretch(42); + sp.setVerticalStretch(10); + sp.setControlType(QSizePolicy::CheckBox); + sp.setHeightForWidth(true); + + stream << sp; // big endian +/* +| BYTE 0 | BYTE 1 | +| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | ++-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ +| Horizontal stretch | Vertical stretch | ++-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ + +| BYTE 2 | BYTE 3 | +| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | ++-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ +| pad | wfh | Control Type | hfw | Vertical policy | Horizontal policy | ++-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ +*/ + QCOMPARE((char)data[0], char(42)); // h stretch + QCOMPARE((char)data[1], char(10)); // v stretch + QCOMPARE((char)data[2], char(1 | (2 << 1))); // (hfw + CheckBox) + QCOMPARE((char)data[3], char(QSizePolicy::Minimum | (QSizePolicy::Expanding << 4))); + } + + { + QSizePolicy readSP; + QDataStream stream(data); + stream >> readSP; + QCOMPARE(sp, readSP); + } +} + + +void tst_QSizePolicy::horizontalStretch() +{ + QSizePolicy sp; + sp.setHorizontalStretch(257); + QCOMPARE(sp.horizontalStretch(), 255); + sp.setHorizontalStretch(-2); + QCOMPARE(sp.horizontalStretch(), 0); +} + +void tst_QSizePolicy::verticalStretch() +{ + QSizePolicy sp; + sp.setVerticalStretch(-2); + QCOMPARE(sp.verticalStretch(), 0); + sp.setVerticalStretch(257); + QCOMPARE(sp.verticalStretch(), 255); +} +QTEST_MAIN(tst_QSizePolicy) +#include "tst_qsizepolicy.moc" |