1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #include <qsizepolicy.h>
46 class tst_QSizePolicy : public QObject
52 virtual ~tst_QSizePolicy();
57 void horizontalStretch();
58 void verticalStretch();
61 tst_QSizePolicy::tst_QSizePolicy()
65 tst_QSizePolicy::~tst_QSizePolicy()
70 // Testing get/set functions
71 void tst_QSizePolicy::getSetCheck()
74 // check values of a default constructed QSizePolicy
76 QCOMPARE(sp.horizontalPolicy(), QSizePolicy::Fixed);
77 QCOMPARE(sp.verticalPolicy(), QSizePolicy::Fixed);
78 QCOMPARE(sp.horizontalStretch(), 0);
79 QCOMPARE(sp.verticalStretch(), 0);
80 QCOMPARE(sp.verticalStretch(), 0);
81 QCOMPARE(sp.controlType(), QSizePolicy::DefaultType);
82 QCOMPARE(sp.hasHeightForWidth(), false);
83 QCOMPARE(sp.hasWidthForHeight(), false);
87 static const QSizePolicy::Policy policies[3] = {
92 static const QSizePolicy::ControlType controlTypes[4] = {
93 QSizePolicy::DefaultType,
94 QSizePolicy::ButtonBox,
95 QSizePolicy::CheckBox,
96 QSizePolicy::ToolButton
99 #define ITEMCOUNT(arr) sizeof(arr)/sizeof(arr[0])
100 QSizePolicy sp, oldsp;
101 #ifdef GENERATE_BASELINE
102 QFile out(QString::fromAscii("qsizepolicy-Qt%1%2.txt").arg((QT_VERSION >> 16) & 0xff).arg((QT_VERSION) >> 8 & 0xff));
103 if (out.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
104 QDataStream stream(&out);
106 /* Loop for permutating over the values most likely to trigger a bug:
107 - mininumum, maximum values
108 - Some values with LSB set, others with MSB unset. (check if shifts are ok)
111 // Look specifically for
112 for (int ihp = 0; ihp < ITEMCOUNT(policies); ++ihp) {
113 QSizePolicy::Policy hp = policies[ihp];
114 for (int ivp = 0; ivp < ITEMCOUNT(policies); ++ivp) {
115 QSizePolicy::Policy vp = policies[ivp];
116 for (int ict = 0; ict < ITEMCOUNT(controlTypes); ++ict) {
117 QSizePolicy::ControlType ct = controlTypes[ict];
118 for (int hst= 0; hst <= 255; hst+=85) { //[0,85,170,255]
119 for (int vst = 0; vst <= 255; vst+=85) {
120 for (int j = 0; j < 3; ++j) {
122 bool wfh = j & 2; // cannot set hfw and wfh at the same time
124 for (int i = 0; i < 5; ++i) {
126 case 0: sp.setHorizontalPolicy(hp); break;
127 case 1: sp.setVerticalPolicy(vp); break;
128 case 2: sp.setHorizontalStretch(hst); break;
129 case 3: sp.setVerticalStretch(vst); break;
130 case 4: sp.setControlType(ct); break;
131 case 5: sp.setHeightForWidth(hfw); sp.setWidthForHeight(wfh); break;
134 QCOMPARE(sp.horizontalPolicy(), (i >= 0 ? hp : oldsp.horizontalPolicy()));
135 QCOMPARE(sp.verticalPolicy(), (i >= 1 ? vp : oldsp.verticalPolicy()));
136 QCOMPARE(sp.horizontalStretch(), (i >= 2 ? hst : oldsp.horizontalStretch()));
137 QCOMPARE(sp.verticalStretch(), (i >= 3 ? vst : oldsp.verticalStretch()));
138 QCOMPARE(sp.controlType(), (i >= 4 ? ct : oldsp.controlType()));
139 QCOMPARE(sp.hasHeightForWidth(), (i >= 5 ? hfw : oldsp.hasHeightForWidth()));
140 QCOMPARE(sp.hasWidthForHeight(), (i >= 5 ? wfh : oldsp.hasWidthForHeight()));
142 Qt::Orientations orients;
143 if (sp.horizontalPolicy() & QSizePolicy::ExpandFlag)
144 orients |= Qt::Horizontal;
145 if (sp.verticalPolicy() & QSizePolicy::ExpandFlag)
146 orients |= Qt::Vertical;
148 QCOMPARE(sp.expandingDirections(), orients);
149 #ifdef GENERATE_BASELINE
159 #ifdef GENERATE_BASELINE
166 void tst_QSizePolicy::dataStream()
169 QSizePolicy sp(QSizePolicy::Minimum, QSizePolicy::Expanding);
171 QDataStream stream(&data, QIODevice::ReadWrite);
172 sp.setHorizontalStretch(42);
173 sp.setVerticalStretch(10);
174 sp.setControlType(QSizePolicy::CheckBox);
175 sp.setHeightForWidth(true);
177 stream << sp; // big endian
180 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
181 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
182 | Horizontal stretch | Vertical stretch |
183 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
186 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
187 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
188 | pad | wfh | Control Type | hfw | Vertical policy | Horizontal policy |
189 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
191 QCOMPARE((char)data[0], char(42)); // h stretch
192 QCOMPARE((char)data[1], char(10)); // v stretch
193 QCOMPARE((char)data[2], char(1 | (2 << 1))); // (hfw + CheckBox)
194 QCOMPARE((char)data[3], char(QSizePolicy::Minimum | (QSizePolicy::Expanding << 4)));
199 QDataStream stream(data);
201 QCOMPARE(sp, readSP);
206 void tst_QSizePolicy::horizontalStretch()
209 sp.setHorizontalStretch(257);
210 QCOMPARE(sp.horizontalStretch(), 255);
211 sp.setHorizontalStretch(-2);
212 QCOMPARE(sp.horizontalStretch(), 0);
215 void tst_QSizePolicy::verticalStretch()
218 sp.setVerticalStretch(-2);
219 QCOMPARE(sp.verticalStretch(), 0);
220 sp.setVerticalStretch(257);
221 QCOMPARE(sp.verticalStretch(), 255);
223 QTEST_MAIN(tst_QSizePolicy)
224 #include "tst_qsizepolicy.moc"