1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtTest/QtTest> |
31 | #include <qgraphicslayoutitem.h> |
32 | #include <float.h> |
33 | #include <limits.h> |
34 | |
35 | class tst_QGraphicsLayoutItem : public QObject { |
36 | Q_OBJECT |
37 | |
38 | private slots: |
39 | void qgraphicslayoutitem(); |
40 | |
41 | void contentsRect(); |
42 | void effectiveSizeHint_data(); |
43 | void effectiveSizeHint(); |
44 | void getContentsMargins(); |
45 | void isLayout_data(); |
46 | void isLayout(); |
47 | void maximumSize(); |
48 | void minimumSize(); |
49 | void parentLayoutItem_data(); |
50 | void parentLayoutItem(); |
51 | void preferredSize(); |
52 | void setMaximumSize_data(); |
53 | void setMaximumSize(); |
54 | void setMinimumSize_data(); |
55 | void setMinimumSize(); |
56 | void setPreferredSize_data(); |
57 | void setPreferredSize(); |
58 | void setSizePolicy_data(); |
59 | void setPreferredSize2(); |
60 | void setSizePolicy(); |
61 | }; |
62 | |
63 | // Subclass that exposes the protected functions. |
64 | class SubQGraphicsLayoutItem : public QGraphicsLayoutItem { |
65 | public: |
66 | SubQGraphicsLayoutItem(QGraphicsLayoutItem *par = 0, bool layout = false) |
67 | : QGraphicsLayoutItem(par, layout), updateGeometryCalled(0) |
68 | {} |
69 | |
70 | // QGraphicsLayoutItem::geometry is a pure virtual function |
71 | QRectF geometry() const |
72 | { return QRectF(); } |
73 | |
74 | // QGraphicsLayoutItem::setGeometry is a pure virtual function |
75 | void setGeometry(QRectF const& rect) |
76 | { Q_UNUSED(rect); } |
77 | |
78 | // QGraphicsLayoutItem::sizeHint is a pure virtual function |
79 | QSizeF sizeHint(Qt::SizeHint which, QSizeF const& constraint = QSizeF()) const |
80 | { Q_UNUSED(which); Q_UNUSED(constraint); return QSizeF(); } |
81 | |
82 | void updateGeometry() |
83 | { updateGeometryCalled++; QGraphicsLayoutItem::updateGeometry(); } |
84 | int updateGeometryCalled; |
85 | |
86 | }; |
87 | |
88 | void tst_QGraphicsLayoutItem::qgraphicslayoutitem() |
89 | { |
90 | SubQGraphicsLayoutItem layoutItem; |
91 | layoutItem.contentsRect(); |
92 | layoutItem.effectiveSizeHint(which: Qt::MinimumSize); |
93 | layoutItem.geometry(); |
94 | QCOMPARE(layoutItem.isLayout(), false); |
95 | layoutItem.maximumSize(); |
96 | layoutItem.minimumSize(); |
97 | QCOMPARE(layoutItem.parentLayoutItem(), nullptr); |
98 | layoutItem.preferredSize(); |
99 | layoutItem.sizePolicy(); |
100 | layoutItem.sizeHint(which: Qt::MinimumSize); |
101 | } |
102 | |
103 | // QRectF contentsRect() const public |
104 | void tst_QGraphicsLayoutItem::contentsRect() |
105 | { |
106 | SubQGraphicsLayoutItem layoutItem; |
107 | QRectF f = layoutItem.contentsRect(); |
108 | QCOMPARE(f, QRectF(QPoint(), QSizeF(0, 0))); |
109 | } |
110 | Q_DECLARE_METATYPE(Qt::SizeHint) |
111 | void tst_QGraphicsLayoutItem::effectiveSizeHint_data() |
112 | { |
113 | QTest::addColumn<Qt::SizeHint>(name: "sizeHint" ); |
114 | QTest::addColumn<QSizeF>(name: "constraint" ); |
115 | for (int i = 0; i < 15; ++i) { |
116 | QTestData &data = QTest::newRow(dataTag: QByteArray::number(i).constData()); |
117 | switch(i % 5) { |
118 | case 0: data << Qt::MinimumSize; break; |
119 | case 1: data << Qt::PreferredSize; break; |
120 | case 2: data << Qt::MaximumSize; break; |
121 | case 3: data << Qt::MinimumDescent; break; |
122 | case 4: data << Qt::NSizeHints; break; |
123 | } |
124 | switch(i % 3) { |
125 | case 0: data << QSizeF(-1, -1); break; |
126 | case 1: data << QSizeF(0, 0); break; |
127 | case 2: data << QSizeF(10, 10); break; |
128 | } |
129 | } |
130 | } |
131 | |
132 | // QSizeF effectiveSizeHint(Qt::SizeHint which, QSizeF const& constraint = QSize()) const public |
133 | void tst_QGraphicsLayoutItem::effectiveSizeHint() |
134 | { |
135 | QFETCH(Qt::SizeHint, sizeHint); |
136 | QFETCH(QSizeF, constraint); |
137 | SubQGraphicsLayoutItem layoutItem; |
138 | QSizeF r = layoutItem.effectiveSizeHint(which: sizeHint, constraint); |
139 | if (constraint.width() != -1) |
140 | QCOMPARE(r.width(), constraint.width()); |
141 | if (constraint.height() != -1) |
142 | QCOMPARE(r.height(), constraint.height()); |
143 | } |
144 | |
145 | // void getContentsMargins(qreal* left, qreal* top, qreal* right, qreal* bottom) const public |
146 | void tst_QGraphicsLayoutItem::getContentsMargins() |
147 | { |
148 | SubQGraphicsLayoutItem layoutItem; |
149 | qreal left; |
150 | qreal top; |
151 | qreal right; |
152 | qreal bottom; |
153 | layoutItem.getContentsMargins(left: &left, top: &top, right: &right, bottom: &bottom); |
154 | QCOMPARE(left, (qreal)0); |
155 | QCOMPARE(top, (qreal)0); |
156 | QCOMPARE(right, (qreal)0); |
157 | QCOMPARE(bottom, (qreal)0); |
158 | } |
159 | |
160 | void tst_QGraphicsLayoutItem::isLayout_data() |
161 | { |
162 | QTest::addColumn<bool>(name: "isLayout" ); |
163 | QTest::newRow(dataTag: "no" ) << false; |
164 | QTest::newRow(dataTag: "yes" ) << true; |
165 | } |
166 | |
167 | // bool isLayout() const public |
168 | void tst_QGraphicsLayoutItem::isLayout() |
169 | { |
170 | QFETCH(bool, isLayout); |
171 | SubQGraphicsLayoutItem layoutItem(0, isLayout); |
172 | QCOMPARE(layoutItem.isLayout(), isLayout); |
173 | } |
174 | |
175 | // QSizeF maximumSize() const public |
176 | void tst_QGraphicsLayoutItem::maximumSize() |
177 | { |
178 | SubQGraphicsLayoutItem layoutItem; |
179 | QCOMPARE(layoutItem.maximumSize(), QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); |
180 | // layoutItem.effectiveSizeHint(Qt::MaximumSize); |
181 | } |
182 | |
183 | // QSizeF minimumSize() const public |
184 | void tst_QGraphicsLayoutItem::minimumSize() |
185 | { |
186 | SubQGraphicsLayoutItem layoutItem; |
187 | QCOMPARE(layoutItem.minimumSize(), QSizeF(0, 0)); |
188 | // layoutItem.effectiveSizeHint(Qt::MinimumSize); |
189 | } |
190 | |
191 | void tst_QGraphicsLayoutItem::parentLayoutItem_data() |
192 | { |
193 | QTest::addColumn<bool>(name: "parent" ); |
194 | QTest::newRow(dataTag: "no" ) << false; |
195 | QTest::newRow(dataTag: "yes" ) << true; |
196 | } |
197 | |
198 | // QGraphicsLayoutItem* parentLayoutItem() const public |
199 | void tst_QGraphicsLayoutItem::parentLayoutItem() |
200 | { |
201 | QFETCH(bool, parent); |
202 | SubQGraphicsLayoutItem parentLayout; |
203 | SubQGraphicsLayoutItem layoutItem(parent ? &parentLayout : 0); |
204 | QCOMPARE(layoutItem.parentLayoutItem(), static_cast<QGraphicsLayoutItem*>( parent ? &parentLayout : 0)); |
205 | } |
206 | |
207 | // QSizeF preferredSize() const public |
208 | void tst_QGraphicsLayoutItem::preferredSize() |
209 | { |
210 | SubQGraphicsLayoutItem layoutItem; |
211 | QCOMPARE(layoutItem.preferredSize(), QSizeF(0, 0)); |
212 | // layoutItem.effectiveSizeHint(Qt::PreferredSize)); |
213 | } |
214 | |
215 | void tst_QGraphicsLayoutItem::setMaximumSize_data() |
216 | { |
217 | QTest::addColumn<QSizeF>(name: "size" ); |
218 | QTest::addColumn<QSizeF>(name: "outputSize" ); |
219 | QTest::newRow(dataTag: "-1" ) << QSizeF(-1, -1) << QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); |
220 | QTest::newRow(dataTag: "0" ) << QSizeF(0, 0) << QSizeF(0, 0); |
221 | QTest::newRow(dataTag: "10" ) << QSizeF(10, 10) << QSizeF(10, 10); |
222 | } |
223 | |
224 | // void setMaximumSize(QSizeF const& size) public |
225 | void tst_QGraphicsLayoutItem::setMaximumSize() |
226 | { |
227 | QFETCH(QSizeF, size); |
228 | QFETCH(QSizeF, outputSize); |
229 | SubQGraphicsLayoutItem layoutItem; |
230 | QSizeF oldSize = layoutItem.maximumSize(); |
231 | layoutItem.setMaximumSize(size); |
232 | if (size.isValid()) |
233 | QCOMPARE(layoutItem.updateGeometryCalled, (oldSize == size) ? 0 : 1); |
234 | else |
235 | QVERIFY(!layoutItem.updateGeometryCalled); |
236 | layoutItem.setMinimumSize(aw: 1, ah: 1); |
237 | |
238 | QVERIFY(layoutItem.maximumSize().width() <= outputSize.width()); |
239 | QVERIFY(layoutItem.maximumSize().height() <= outputSize.height()); |
240 | QVERIFY(layoutItem.minimumSize().width() <= outputSize.width()); |
241 | QVERIFY(layoutItem.minimumSize().height() <= outputSize.height()); |
242 | QVERIFY(layoutItem.preferredSize().width() <= outputSize.width()); |
243 | QVERIFY(layoutItem.preferredSize().height() <= outputSize.height()); |
244 | } |
245 | |
246 | void tst_QGraphicsLayoutItem::setMinimumSize_data() |
247 | { |
248 | QTest::addColumn<QSizeF>(name: "size" ); |
249 | QTest::newRow(dataTag: "-1" ) << QSizeF(-1, -1); |
250 | QTest::newRow(dataTag: "0" ) << QSizeF(0, 0); |
251 | QTest::newRow(dataTag: "10" ) << QSizeF(10, 10); |
252 | } |
253 | |
254 | // void setMinimumSize(QSizeF const& size) public |
255 | void tst_QGraphicsLayoutItem::setMinimumSize() |
256 | { |
257 | QFETCH(QSizeF, size); |
258 | SubQGraphicsLayoutItem layoutItem; |
259 | QSizeF oldSize = layoutItem.minimumSize(); |
260 | |
261 | layoutItem.setMinimumSize(size); |
262 | if (size.isValid()) { |
263 | QEXPECT_FAIL("0" , "updateGeometry() is called when it doesn't have to be." , Continue); |
264 | QCOMPARE(layoutItem.updateGeometryCalled, (oldSize == size) ? 0 : 1); |
265 | } else { |
266 | QVERIFY(!layoutItem.updateGeometryCalled); |
267 | } |
268 | layoutItem.setMaximumSize(aw: 5, ah: 5); |
269 | QEXPECT_FAIL("10" , "layoutItem.maximumSize().width() < size.width()" , Abort); |
270 | QVERIFY(layoutItem.maximumSize().width() >= size.width()); |
271 | QVERIFY(layoutItem.maximumSize().height() >= size.height()); |
272 | QVERIFY(layoutItem.minimumSize().width() >= size.width()); |
273 | QVERIFY(layoutItem.minimumSize().height() >= size.height()); |
274 | QVERIFY(layoutItem.preferredSize().width() >= size.width()); |
275 | QVERIFY(layoutItem.preferredSize().height() >= size.height()); |
276 | } |
277 | |
278 | void tst_QGraphicsLayoutItem::setPreferredSize_data() |
279 | { |
280 | QTest::addColumn<QSizeF>(name: "size" ); |
281 | QTest::newRow(dataTag: "-1" ) << QSizeF(-1, -1); |
282 | QTest::newRow(dataTag: "0" ) << QSizeF(0, 0); |
283 | QTest::newRow(dataTag: "10" ) << QSizeF(10, 10); |
284 | } |
285 | |
286 | // void setPreferredSize(QSizeF const& size) public |
287 | void tst_QGraphicsLayoutItem::setPreferredSize() |
288 | { |
289 | QFETCH(QSizeF, size); |
290 | SubQGraphicsLayoutItem layoutItem; |
291 | QSizeF oldSize = layoutItem.preferredSize(); |
292 | layoutItem.setPreferredSize(size); |
293 | if (size.isValid()) |
294 | QCOMPARE(layoutItem.preferredSize(), size); |
295 | |
296 | if (size.isValid()) { |
297 | QEXPECT_FAIL("0" , "updateGeometry() is called when it doesn't have to be." , Continue); |
298 | QCOMPARE(layoutItem.updateGeometryCalled, (oldSize == size) ? 0 : 1); |
299 | } else { |
300 | QVERIFY(!layoutItem.updateGeometryCalled); |
301 | } |
302 | } |
303 | |
304 | void tst_QGraphicsLayoutItem::setPreferredSize2() |
305 | { |
306 | SubQGraphicsLayoutItem layoutItem; |
307 | layoutItem.setPreferredSize(QSizeF(30, -1)); |
308 | QCOMPARE(layoutItem.preferredWidth(), qreal(30)); |
309 | } |
310 | |
311 | void tst_QGraphicsLayoutItem::setSizePolicy_data() |
312 | { |
313 | QTest::addColumn<QSizePolicy>(name: "policy" ); |
314 | QTest::newRow(dataTag: "default" ) << QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed, QSizePolicy::DefaultType); |
315 | QTest::newRow(dataTag: "rand" ) << QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred); |
316 | } |
317 | |
318 | // void setSizePolicy(QSizePolicy const& policy) public |
319 | void tst_QGraphicsLayoutItem::setSizePolicy() |
320 | { |
321 | QFETCH(QSizePolicy, policy); |
322 | SubQGraphicsLayoutItem layoutItem; |
323 | QSizePolicy defaultPolicy(QSizePolicy::Preferred, QSizePolicy::Preferred, QSizePolicy::DefaultType); |
324 | QCOMPARE(layoutItem.sizePolicy(), defaultPolicy); |
325 | |
326 | layoutItem.setSizePolicy(policy); |
327 | QCOMPARE(layoutItem.sizePolicy(), policy); |
328 | QCOMPARE(layoutItem.updateGeometryCalled, (defaultPolicy == policy) ? 0 : 1); |
329 | } |
330 | |
331 | QTEST_MAIN(tst_QGraphicsLayoutItem) |
332 | #include "tst_qgraphicslayoutitem.moc" |
333 | |
334 | |