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 <qgraphicsitem.h>
32#include <qpainterpath.h>
33#include <qpen.h>
34
35
36class tst_QGraphicsPolygonItem : public QObject
37{
38 Q_OBJECT
39
40private slots:
41 void qgraphicspolygonitem_data();
42 void qgraphicspolygonitem();
43 void boundingRect_data();
44 void boundingRect();
45 void contains_data();
46 void contains();
47 void fillRule_data();
48 void fillRule();
49 void isObscuredBy_data();
50 void isObscuredBy();
51 void opaqueArea_data();
52 void opaqueArea();
53 void polygon_data();
54 void polygon();
55 void shape_data();
56 void shape();
57 void extension_data();
58 void extension();
59 void setExtension_data();
60 void setExtension();
61 void supportsExtension_data();
62 void supportsExtension();
63};
64
65// Subclass that exposes the protected functions.
66class SubQGraphicsPolygonItem : public QGraphicsPolygonItem
67{
68public:
69 enum Extension {
70 UserExtension = QGraphicsItem::UserExtension
71 };
72
73 SubQGraphicsPolygonItem(QGraphicsItem *parent = 0) : QGraphicsPolygonItem(parent)
74 {
75 }
76
77 SubQGraphicsPolygonItem(const QPolygonF &polygon, QGraphicsItem *parent = 0) : QGraphicsPolygonItem(polygon, parent)
78 {
79 }
80
81 QVariant call_extension(QVariant const& variant) const
82 { return SubQGraphicsPolygonItem::extension(variant); }
83
84 void call_setExtension(SubQGraphicsPolygonItem::Extension extension, QVariant const& variant)
85 { return SubQGraphicsPolygonItem::setExtension(extension: (QGraphicsItem::Extension)extension, variant); }
86
87 bool call_supportsExtension(SubQGraphicsPolygonItem::Extension extension) const
88 { return SubQGraphicsPolygonItem::supportsExtension(extension: (QGraphicsItem::Extension)extension); }
89};
90
91void tst_QGraphicsPolygonItem::qgraphicspolygonitem_data()
92{
93}
94
95void tst_QGraphicsPolygonItem::qgraphicspolygonitem()
96{
97 SubQGraphicsPolygonItem item;
98
99 item.boundingRect();
100 item.contains(point: QPoint());
101 item.isObscuredBy(item: 0);
102 item.opaqueArea();
103 //item.paint();
104 item.shape();
105 item.type();
106 item.call_extension(variant: QVariant());
107 item.call_setExtension(extension: SubQGraphicsPolygonItem::UserExtension, variant: QVariant());
108 item.call_supportsExtension(extension: SubQGraphicsPolygonItem::UserExtension);
109 item.fillRule();
110 item.polygon();
111 item.setFillRule(Qt::OddEvenFill);
112 item.setPolygon(QPolygonF());
113}
114
115void tst_QGraphicsPolygonItem::boundingRect_data()
116{
117 QTest::addColumn<QPolygonF>(name: "polygon");
118 QTest::addColumn<QRectF>(name: "boundingRect");
119 QTest::newRow(dataTag: "null") << QPolygonF() << QRectF();
120 QPolygonF example;
121 example << QPointF(10.4, 20.5) << QPointF(20.2, 30.2);
122 QTest::newRow(dataTag: "example") << example << example.boundingRect();
123 // ### set pen width?
124}
125
126// public QRectF boundingRect() const
127void tst_QGraphicsPolygonItem::boundingRect()
128{
129 QFETCH(QPolygonF, polygon);
130 QFETCH(QRectF, boundingRect);
131
132 SubQGraphicsPolygonItem item(polygon);
133 item.setPen(QPen(Qt::black, 0));
134 QCOMPARE(item.boundingRect(), boundingRect);
135}
136
137void tst_QGraphicsPolygonItem::contains_data()
138{
139 QTest::addColumn<QPolygonF>(name: "polygon");
140 QTest::addColumn<QPointF>(name: "point");
141 QTest::addColumn<bool>(name: "contains");
142 QTest::newRow(dataTag: "null") << QPolygonF() << QPointF() << false;
143}
144
145// public bool contains(QPointF const& point) const
146void tst_QGraphicsPolygonItem::contains()
147{
148 QFETCH(QPolygonF, polygon);
149 QFETCH(QPointF, point);
150 QFETCH(bool, contains);
151
152 SubQGraphicsPolygonItem item(polygon);
153
154 QCOMPARE(item.contains(point), contains);
155}
156
157Q_DECLARE_METATYPE(Qt::FillRule)
158void tst_QGraphicsPolygonItem::fillRule_data()
159{
160 QTest::addColumn<QPolygonF>(name: "polygon");
161 QTest::addColumn<Qt::FillRule>(name: "fillRule");
162 QTest::newRow(dataTag: "OddEvenFill") << QPolygonF() << Qt::OddEvenFill;
163 QTest::newRow(dataTag: "WindingFill") << QPolygonF() << Qt::WindingFill;
164}
165
166// public Qt::FillRule fillRule() const
167void tst_QGraphicsPolygonItem::fillRule()
168{
169 QFETCH(QPolygonF, polygon);
170 QFETCH(Qt::FillRule, fillRule);
171
172 SubQGraphicsPolygonItem item(polygon);
173
174 item.setFillRule(fillRule);
175 QCOMPARE(item.fillRule(), fillRule);
176 // ### Check that the painting is different?
177}
178
179void tst_QGraphicsPolygonItem::isObscuredBy_data()
180{
181 QTest::addColumn<QPolygonF>(name: "polygon");
182 QTest::addColumn<QPolygonF>(name: "otherPolygon");
183 QTest::addColumn<bool>(name: "isObscuredBy");
184 QTest::newRow(dataTag: "null") << QPolygonF() << QPolygonF() << false;
185 //QTest::newRow("ontop-inside") << QPixmap(10, 10) << QPixmap(5, 5) << false;
186 //QTest::newRow("ontop-larger") << QPixmap(10, 10) << QPixmap(11, 11) << true;
187}
188
189// public bool isObscuredBy(QGraphicsItem const* item) const
190void tst_QGraphicsPolygonItem::isObscuredBy()
191{
192 QFETCH(QPolygonF, polygon);
193 QFETCH(QPolygonF, otherPolygon);
194 QFETCH(bool, isObscuredBy);
195 SubQGraphicsPolygonItem item(polygon);
196 SubQGraphicsPolygonItem otherItem(otherPolygon);
197 QCOMPARE(item.isObscuredBy(&otherItem), isObscuredBy);
198}
199
200Q_DECLARE_METATYPE(QPainterPath)
201void tst_QGraphicsPolygonItem::opaqueArea_data()
202{
203 QTest::addColumn<QPolygonF>(name: "polygon");
204 QTest::addColumn<QPainterPath>(name: "opaqueArea");
205 QTest::newRow(dataTag: "null") << QPolygonF() << QPainterPath();
206 // Currently QGraphicsPolygonItem just calls QGraphicsItem test there
207}
208
209// public QPainterPath opaqueArea() const
210void tst_QGraphicsPolygonItem::opaqueArea()
211{
212 QFETCH(QPolygonF, polygon);
213 QFETCH(QPainterPath, opaqueArea);
214
215 SubQGraphicsPolygonItem item(polygon);
216 QCOMPARE(item.opaqueArea(), opaqueArea);
217}
218
219void tst_QGraphicsPolygonItem::polygon_data()
220{
221 QTest::addColumn<QPolygonF>(name: "polygon");
222 QTest::newRow(dataTag: "null") << QPolygonF();
223 QPolygonF example;
224 example << QPointF(10.4, 20.5) << QPointF(20.2, 30.2);
225 QTest::newRow(dataTag: "example") << example;
226}
227
228// public QPolygonF polygon() const
229void tst_QGraphicsPolygonItem::polygon()
230{
231 QFETCH(QPolygonF, polygon);
232
233 SubQGraphicsPolygonItem item;
234 item.setPolygon(polygon);
235 QCOMPARE(item.polygon(), polygon);
236}
237
238void tst_QGraphicsPolygonItem::shape_data()
239{
240 QTest::addColumn<QPainterPath>(name: "shape");
241 QTest::newRow(dataTag: "null") << QPainterPath();
242 // ### what should a normal shape look like?
243}
244
245// public QPainterPath shape() const
246void tst_QGraphicsPolygonItem::shape()
247{
248 QFETCH(QPainterPath, shape);
249
250 SubQGraphicsPolygonItem item;
251 QCOMPARE(item.shape(), shape);
252}
253
254void tst_QGraphicsPolygonItem::extension_data()
255{
256 QTest::addColumn<QVariant>(name: "variant");
257 QTest::addColumn<QVariant>(name: "extension");
258 QTest::newRow(dataTag: "null") << QVariant() << QVariant();
259}
260
261// protected QVariant extension(QVariant const& variant) const
262void tst_QGraphicsPolygonItem::extension()
263{
264 QFETCH(QVariant, variant);
265 QFETCH(QVariant, extension);
266
267 SubQGraphicsPolygonItem item;
268
269 QCOMPARE(item.call_extension(variant), extension);
270}
271
272Q_DECLARE_METATYPE(SubQGraphicsPolygonItem::Extension)
273void tst_QGraphicsPolygonItem::setExtension_data()
274{
275 QTest::addColumn<SubQGraphicsPolygonItem::Extension>(name: "extension");
276 QTest::addColumn<QVariant>(name: "variant");
277 QTest::newRow(dataTag: "null") << SubQGraphicsPolygonItem::Extension() << QVariant();
278}
279
280// protected void setExtension(SubQGraphicsPolygonItem::Extension extension, QVariant const& variant)
281void tst_QGraphicsPolygonItem::setExtension()
282{
283 QFETCH(SubQGraphicsPolygonItem::Extension, extension);
284 QFETCH(QVariant, variant);
285
286 SubQGraphicsPolygonItem item;
287 item.call_setExtension(extension, variant);
288}
289
290void tst_QGraphicsPolygonItem::supportsExtension_data()
291{
292 QTest::addColumn<SubQGraphicsPolygonItem::Extension>(name: "extension");
293 QTest::addColumn<bool>(name: "supportsExtension");
294 QTest::newRow(dataTag: "null") << SubQGraphicsPolygonItem::Extension() << false;
295}
296
297// protected bool supportsExtension(SubQGraphicsPolygonItem::Extension extension) const
298void tst_QGraphicsPolygonItem::supportsExtension()
299{
300 QFETCH(SubQGraphicsPolygonItem::Extension, extension);
301 QFETCH(bool, supportsExtension);
302
303 SubQGraphicsPolygonItem item;
304 QCOMPARE(item.call_supportsExtension(extension), supportsExtension);
305}
306
307QTEST_MAIN(tst_QGraphicsPolygonItem)
308#include "tst_qgraphicspolygonitem.moc"
309
310

source code of qtbase/tests/auto/widgets/graphicsview/qgraphicspolygonitem/tst_qgraphicspolygonitem.cpp