Fix Qt 5 todo issues for QSizePolicy.
[profile/ivi/qtbase.git] / tests / auto / widgets / kernel / qsizepolicy / tst_qsizepolicy.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44 #include <qsizepolicy.h>
45
46 class tst_QSizePolicy : public QObject
47 {
48 Q_OBJECT
49
50 public:
51     tst_QSizePolicy();
52     virtual ~tst_QSizePolicy();
53
54 private slots:
55     void getSetCheck();
56     void dataStream();
57     void horizontalStretch();
58     void verticalStretch();
59 };
60
61 tst_QSizePolicy::tst_QSizePolicy()
62 {
63 }
64
65 tst_QSizePolicy::~tst_QSizePolicy()
66 {
67 }
68
69
70 // Testing get/set functions
71 void tst_QSizePolicy::getSetCheck()
72 {
73     {
74         // check values of a default constructed QSizePolicy
75         QSizePolicy sp;
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);
84     }
85
86     {
87         static const QSizePolicy::Policy policies[3] = {
88             QSizePolicy::Fixed,
89             QSizePolicy::Minimum,
90             QSizePolicy::Ignored
91         };
92         static const QSizePolicy::ControlType controlTypes[4] = {
93             QSizePolicy::DefaultType,
94             QSizePolicy::ButtonBox,
95             QSizePolicy::CheckBox,
96             QSizePolicy::ToolButton
97         };
98
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);
105 #endif
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)
109
110             */
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) {
121                                     bool hfw = j & 1;
122                                     bool wfh = j & 2;   // cannot set hfw and wfh at the same time
123                                     oldsp = sp;
124                                     for (int i = 0; i < 5; ++i) {
125                                         switch (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;
132                                         default: break;
133                                         }
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()));
141
142                                         Qt::Orientations orients;
143                                         if (sp.horizontalPolicy() & QSizePolicy::ExpandFlag)
144                                             orients |= Qt::Horizontal;
145                                         if (sp.verticalPolicy() & QSizePolicy::ExpandFlag)
146                                             orients |= Qt::Vertical;
147
148                                         QCOMPARE(sp.expandingDirections(), orients);
149 #ifdef GENERATE_BASELINE
150                                         stream << sp;
151 #endif
152                                     }
153                                 }
154                             }
155                         }
156                     }
157                 }
158             }
159 #ifdef GENERATE_BASELINE
160             out.close();
161         }
162 #endif
163     }
164 }
165
166 void tst_QSizePolicy::dataStream()
167 {
168     QByteArray data;
169     QSizePolicy sp(QSizePolicy::Minimum, QSizePolicy::Expanding);
170     {
171         QDataStream stream(&data, QIODevice::ReadWrite);
172         sp.setHorizontalStretch(42);
173         sp.setVerticalStretch(10);
174         sp.setControlType(QSizePolicy::CheckBox);
175         sp.setHeightForWidth(true);
176
177         stream << sp;   // big endian
178 /*
179 |                     BYTE 0                    |                    BYTE 1                     |
180 |  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  | 10  | 11  | 12  | 13  | 14  | 15  |
181 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
182 |               Horizontal stretch              |               Vertical stretch                |
183 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
184
185 |                     BYTE 2                    |                    BYTE 3                     |
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 +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
190 */
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)));
195     }
196
197     {
198         QSizePolicy readSP;
199         QDataStream stream(data);
200         stream >> readSP;
201         QCOMPARE(sp, readSP);
202     }
203 }
204
205
206 void tst_QSizePolicy::horizontalStretch()
207 {
208     QSizePolicy sp;
209     sp.setHorizontalStretch(257);
210     QCOMPARE(sp.horizontalStretch(), 255);
211     sp.setHorizontalStretch(-2);
212     QCOMPARE(sp.horizontalStretch(), 0);
213 }
214
215 void tst_QSizePolicy::verticalStretch()
216 {
217     QSizePolicy sp;
218     sp.setVerticalStretch(-2);
219     QCOMPARE(sp.verticalStretch(), 0);
220     sp.setVerticalStretch(257);
221     QCOMPARE(sp.verticalStretch(), 255);
222 }
223 QTEST_MAIN(tst_QSizePolicy)
224 #include "tst_qsizepolicy.moc"