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 <qgraphicsscene.h>
32#include <qgraphicsitem.h>
33#include <qpainterpath.h>
34
35class tst_QGraphicsPixmapItem : public QObject
36{
37 Q_OBJECT
38
39private slots:
40 void qgraphicspixmapitem_data();
41 void qgraphicspixmapitem();
42 void boundingRect_data();
43 void boundingRect();
44 void contains_data();
45 void contains();
46 void isObscuredBy_data();
47 void isObscuredBy();
48 void offset_data();
49 void offset();
50 void opaqueArea_data();
51 void opaqueArea();
52 void pixmap_data();
53 void pixmap();
54 void setPixmap_data();
55 void setPixmap();
56 void setShapeMode_data();
57 void setShapeMode();
58 void setTransformationMode_data();
59 void setTransformationMode();
60 void shape_data();
61 void shape();
62 void extension_data();
63 void extension();
64 void setExtension_data();
65 void setExtension();
66 void supportsExtension_data();
67 void supportsExtension();
68};
69
70// Subclass that exposes the protected functions.
71class SubQGraphicsPixmapItem : public QGraphicsPixmapItem
72{
73public:
74 enum Extension {
75 UserExtension = QGraphicsItem::UserExtension
76 };
77 SubQGraphicsPixmapItem(QGraphicsItem *parent = 0) : QGraphicsPixmapItem(parent)
78 {
79 }
80
81 SubQGraphicsPixmapItem(const QPixmap &pixmap, QGraphicsItem *parent = 0) : QGraphicsPixmapItem(pixmap, parent)
82 {
83 }
84
85 QVariant call_extension(QVariant const& variant) const
86 { return SubQGraphicsPixmapItem::extension(variant); }
87
88 void call_setExtension(Extension extension, QVariant const& variant)
89 { return SubQGraphicsPixmapItem::setExtension(extension: (QGraphicsItem::Extension)extension, variant); }
90
91 bool call_supportsExtension(Extension extension) const
92 { return SubQGraphicsPixmapItem::supportsExtension(extension: (QGraphicsItem::Extension)extension); }
93};
94
95void tst_QGraphicsPixmapItem::qgraphicspixmapitem_data()
96{
97}
98
99void tst_QGraphicsPixmapItem::qgraphicspixmapitem()
100{
101 SubQGraphicsPixmapItem item;
102 item.boundingRect();
103 item.contains(point: QPoint());
104 item.isObscuredBy(item: 0);
105 item.opaqueArea();
106 //item.paint();
107 QCOMPARE(item.offset(), QPointF());
108 QCOMPARE(item.pixmap(), QPixmap());
109 QCOMPARE(item.shapeMode(), QGraphicsPixmapItem::MaskShape);
110 QCOMPARE(item.transformationMode(), Qt::FastTransformation);
111 item.setOffset(ax: 0, ay: 0);
112 item.setOffset(QPointF(0, 0));
113 item.setPixmap(QPixmap());
114 item.setShapeMode(QGraphicsPixmapItem::MaskShape);
115 item.setTransformationMode(Qt::FastTransformation);
116 item.shape();
117 item.type();
118 item.call_extension(variant: QVariant());
119 item.call_setExtension(extension: SubQGraphicsPixmapItem::UserExtension, variant: QVariant());
120 item.call_supportsExtension(extension: SubQGraphicsPixmapItem::UserExtension);
121}
122
123void tst_QGraphicsPixmapItem::boundingRect_data()
124{
125 QTest::addColumn<QPixmap>(name: "pixmap");
126 QTest::addColumn<QRectF>(name: "boundingRect");
127 QTest::newRow(dataTag: "null") << QPixmap() << QRectF();
128 QTest::newRow(dataTag: "10x10") << QPixmap(10, 10) << QRectF(0, 0, 10, 10);
129}
130
131// public QRectF boundingRect() const
132void tst_QGraphicsPixmapItem::boundingRect()
133{
134 QFETCH(QPixmap, pixmap);
135 QFETCH(QRectF, boundingRect);
136
137 SubQGraphicsPixmapItem item(pixmap);
138 QCOMPARE(item.boundingRect(), boundingRect);
139}
140
141void tst_QGraphicsPixmapItem::contains_data()
142{
143 QTest::addColumn<QPixmap>(name: "pixmap");
144 QTest::addColumn<QPointF>(name: "point");
145 QTest::addColumn<bool>(name: "contains");
146 QTest::newRow(dataTag: "null") << QPixmap() << QPointF() << false;
147 QTest::newRow(dataTag: "10x10, 100x100") << QPixmap(10, 10) << QPointF(100, 100) << false;
148 QTest::newRow(dataTag: "10x10, 5x5") << QPixmap(10, 10) << QPointF(5, 5) << true;
149 QTest::newRow(dataTag: "border-1") << QPixmap(10, 10) << QPointF(10.5, 10.5) << false;
150 QTest::newRow(dataTag: "border-2") << QPixmap(10, 10) << QPointF(-0.5, -0.5) << false;
151}
152
153// public bool contains(QPointF const& point) const
154void tst_QGraphicsPixmapItem::contains()
155{
156 if (QGuiApplication::platformName().startsWith(s: QLatin1String("wayland"), cs: Qt::CaseInsensitive))
157 QSKIP("Wayland: This fails. Figure out why.");
158
159 QFETCH(QPixmap, pixmap);
160 QFETCH(QPointF, point);
161 QFETCH(bool, contains);
162
163 // At the time of writing, by default pixmaps will have:
164 // - The same pixel format of the primary screen (which is platform dependent and may contain alpha)
165 // - Uninitialized pixels, potentially including an alpha channel
166 // - A ShapeMode of Mask (which mean it will use the alpha channel as a mask for contains())
167 // This means that in order to prevent undefined behavior in this test, we either need to set
168 // the shapeMode to something else, or set the pixels of the pixmap.
169 pixmap.fill(); // Filling the pixmap to be on the safe side.
170
171 SubQGraphicsPixmapItem item(pixmap);
172 QCOMPARE(item.contains(point), contains);
173}
174
175void tst_QGraphicsPixmapItem::isObscuredBy_data()
176{
177 QTest::addColumn<QPixmap>(name: "pixmap");
178 QTest::addColumn<QPixmap>(name: "otherPixmap");
179 QTest::addColumn<bool>(name: "isObscuredBy");
180 QTest::newRow(dataTag: "null") << QPixmap() << QPixmap() << false;
181 QTest::newRow(dataTag: "(10, 10) vs. (5, 5)") << QPixmap(10, 10) << QPixmap(5, 5) << false;
182 QTest::newRow(dataTag: "(5, 5) vs. (10, 10)") << QPixmap(5, 5) << QPixmap(10, 10) << true;
183 QTest::newRow(dataTag: "(10, 10) vs. (10, 10)") << QPixmap(10, 10) << QPixmap(10, 10) << false;
184 QTest::newRow(dataTag: "(9, 9) vs. (10, 10)") << QPixmap(8, 8) << QPixmap(10, 10) << true;
185 QTest::newRow(dataTag: "(10, 10) vs. (9, 9)") << QPixmap(10, 10) << QPixmap(8, 8) << false;
186}
187
188// public bool isObscuredBy(QGraphicsItem const* item) const
189void tst_QGraphicsPixmapItem::isObscuredBy()
190{
191 QFETCH(QPixmap, pixmap);
192 QFETCH(QPixmap, otherPixmap);
193 QFETCH(bool, isObscuredBy);
194 pixmap.fill();
195 otherPixmap.fill();
196
197 SubQGraphicsPixmapItem *item = new SubQGraphicsPixmapItem(pixmap);
198 SubQGraphicsPixmapItem *otherItem = new SubQGraphicsPixmapItem(otherPixmap);
199
200 item->setOffset(ax: -pixmap.width() / 2.0, ay: -pixmap.height() / 2.0);
201 otherItem->setOffset(ax: -otherPixmap.width() / 2.0, ay: -otherPixmap.height() / 2.0);
202
203 QGraphicsScene scene;
204 scene.addItem(item);
205 scene.addItem(item: otherItem);
206 otherItem->setZValue(1);
207
208 QCOMPARE(item->isObscuredBy(otherItem), isObscuredBy);
209}
210
211void tst_QGraphicsPixmapItem::offset_data()
212{
213 QTest::addColumn<QPixmap>(name: "pixmap");
214 QTest::addColumn<QPointF>(name: "offset");
215 QTest::newRow(dataTag: "null") << QPixmap() << QPointF();
216 QTest::newRow(dataTag: "10x10, 1x1") << QPixmap(10, 10) << QPointF(1, 1);
217}
218
219// public QPointF offset() const
220void tst_QGraphicsPixmapItem::offset()
221{
222 QFETCH(QPixmap, pixmap);
223 QFETCH(QPointF, offset);
224
225 SubQGraphicsPixmapItem item(pixmap);
226 item.setOffset(offset);
227 QCOMPARE(item.offset(), offset);
228
229 // ### test actual painting and compare pixmap with offseted one?
230}
231
232Q_DECLARE_METATYPE(QPainterPath)
233void tst_QGraphicsPixmapItem::opaqueArea_data()
234{
235 QTest::addColumn<QPixmap>(name: "pixmap");
236 QTest::addColumn<QPainterPath>(name: "opaqueArea");
237 QTest::newRow(dataTag: "null") << QPixmap() << QPainterPath();
238 // Currently QGraphicsPixmapItem just calls QGraphicsItem test there
239}
240
241// public QPainterPath opaqueArea() const
242void tst_QGraphicsPixmapItem::opaqueArea()
243{
244 QFETCH(QPixmap, pixmap);
245 QFETCH(QPainterPath, opaqueArea);
246
247 SubQGraphicsPixmapItem item;
248 QCOMPARE(item.opaqueArea(), opaqueArea);
249}
250
251void tst_QGraphicsPixmapItem::pixmap_data()
252{
253 QTest::addColumn<QPixmap>(name: "pixmap");
254 QTest::newRow(dataTag: "null") << QPixmap();
255 QTest::newRow(dataTag: "10x10") << QPixmap(10, 10);
256}
257
258// public QPixmap pixmap() const
259void tst_QGraphicsPixmapItem::pixmap()
260{
261 QFETCH(QPixmap, pixmap);
262
263 SubQGraphicsPixmapItem item(pixmap);
264 QCOMPARE(item.pixmap(), pixmap);
265}
266
267void tst_QGraphicsPixmapItem::setPixmap_data()
268{
269 QTest::addColumn<QPixmap>(name: "pixmap");
270 QTest::newRow(dataTag: "null") << QPixmap();
271 QTest::newRow(dataTag: "10x10") << QPixmap(10, 10);
272}
273
274// public void setPixmap(QPixmap const& pixmap)
275void tst_QGraphicsPixmapItem::setPixmap()
276{
277 QFETCH(QPixmap, pixmap);
278
279 SubQGraphicsPixmapItem item;
280 item.setPixmap(pixmap);
281 QCOMPARE(item.pixmap(), pixmap);
282}
283
284Q_DECLARE_METATYPE(QGraphicsPixmapItem::ShapeMode)
285void tst_QGraphicsPixmapItem::setShapeMode_data()
286{
287 QTest::addColumn<QPixmap>(name: "pixmap");
288 QTest::addColumn<QGraphicsPixmapItem::ShapeMode>(name: "mode");
289 QTest::newRow(dataTag: "MaskShape") << QPixmap() << QGraphicsPixmapItem::MaskShape;
290 QTest::newRow(dataTag: "BoundingRectShape") << QPixmap() << QGraphicsPixmapItem::BoundingRectShape;
291 QTest::newRow(dataTag: "HeuristicMaskShape") << QPixmap() << QGraphicsPixmapItem::HeuristicMaskShape;
292}
293
294// public void setShapeMode(QGraphicsPixmapItem::ShapeMode mode)
295void tst_QGraphicsPixmapItem::setShapeMode()
296{
297 QFETCH(QPixmap, pixmap);
298 QFETCH(QGraphicsPixmapItem::ShapeMode, mode);
299
300 SubQGraphicsPixmapItem item(pixmap);
301 item.setShapeMode(mode);
302 QCOMPARE(item.shapeMode(), mode);
303}
304
305Q_DECLARE_METATYPE(Qt::TransformationMode)
306void tst_QGraphicsPixmapItem::setTransformationMode_data()
307{
308 QTest::addColumn<QPixmap>(name: "pixmap");
309 QTest::addColumn<Qt::TransformationMode>(name: "mode");
310 QTest::newRow(dataTag: "FastTransformation") << QPixmap() << Qt::FastTransformation;
311 QTest::newRow(dataTag: "SmoothTransformation") << QPixmap() << Qt::SmoothTransformation;
312}
313
314// public void setTransformationMode(Qt::TransformationMode mode)
315void tst_QGraphicsPixmapItem::setTransformationMode()
316{
317 QFETCH(QPixmap, pixmap);
318 QFETCH(Qt::TransformationMode, mode);
319
320 SubQGraphicsPixmapItem item(pixmap);
321 item.setTransformationMode(mode);
322 QCOMPARE(item.transformationMode(), mode);
323}
324
325void tst_QGraphicsPixmapItem::shape_data()
326{
327 QTest::addColumn<QPixmap>(name: "pixmap");
328 QTest::addColumn<QPainterPath>(name: "shape");
329 QTest::newRow(dataTag: "null") << QPixmap() << QPainterPath();
330 // ### what does a normal shape look like?
331}
332
333// public QPainterPath shape() const
334void tst_QGraphicsPixmapItem::shape()
335{
336 QFETCH(QPixmap, pixmap);
337 QFETCH(QPainterPath, shape);
338
339 SubQGraphicsPixmapItem item(pixmap);
340 QCOMPARE(item.shape(), shape);
341}
342
343Q_DECLARE_METATYPE(SubQGraphicsPixmapItem::Extension)
344void tst_QGraphicsPixmapItem::extension_data()
345{
346 QTest::addColumn<QVariant>(name: "variant");
347 QTest::addColumn<QVariant>(name: "extension");
348 QTest::newRow(dataTag: "null") << QVariant() << QVariant();
349}
350
351// protected QVariant extension(QVariant const& variant) const
352void tst_QGraphicsPixmapItem::extension()
353{
354 QFETCH(QVariant, variant);
355 QFETCH(QVariant, extension);
356
357 SubQGraphicsPixmapItem item;
358 QCOMPARE(item.call_extension(variant), extension);
359}
360
361void tst_QGraphicsPixmapItem::setExtension_data()
362{
363 QTest::addColumn<SubQGraphicsPixmapItem::Extension>(name: "extension");
364 QTest::addColumn<QVariant>(name: "variant");
365 QTest::newRow(dataTag: "null") << SubQGraphicsPixmapItem::UserExtension << QVariant();
366}
367
368// protected void setExtension(QGraphicsItem::Extension extension, QVariant const& variant)
369void tst_QGraphicsPixmapItem::setExtension()
370{
371 QFETCH(SubQGraphicsPixmapItem::Extension, extension);
372 QFETCH(QVariant, variant);
373
374 SubQGraphicsPixmapItem item;
375 item.call_setExtension(extension, variant);
376}
377
378void tst_QGraphicsPixmapItem::supportsExtension_data()
379{
380 QTest::addColumn<SubQGraphicsPixmapItem::Extension>(name: "extension");
381 QTest::addColumn<bool>(name: "supportsExtension");
382 QTest::newRow(dataTag: "null") << SubQGraphicsPixmapItem::UserExtension << false;
383}
384
385// protected bool supportsExtension(QGraphicsItem::Extension extension) const
386void tst_QGraphicsPixmapItem::supportsExtension()
387{
388 QFETCH(SubQGraphicsPixmapItem::Extension, extension);
389 QFETCH(bool, supportsExtension);
390
391 SubQGraphicsPixmapItem item;
392 QCOMPARE(item.call_supportsExtension(extension), supportsExtension);
393}
394
395QTEST_MAIN(tst_QGraphicsPixmapItem)
396#include "tst_qgraphicspixmapitem.moc"
397
398

source code of qtbase/tests/auto/widgets/graphicsview/qgraphicspixmapitem/tst_qgraphicspixmapitem.cpp