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 <qgraphicsscene.h> |
33 | #include <qgraphicssceneevent.h> |
34 | #include <qgraphicsview.h> |
35 | #include <qstyleoption.h> |
36 | #include <private/qobject_p.h> |
37 | |
38 | class tst_QGraphicsObject : public QObject { |
39 | Q_OBJECT |
40 | |
41 | private slots: |
42 | void pos(); |
43 | void x(); |
44 | void y(); |
45 | void z(); |
46 | void opacity(); |
47 | void enabled(); |
48 | void visible(); |
49 | void deleted(); |
50 | }; |
51 | |
52 | class MyGraphicsObject : public QGraphicsObject |
53 | { |
54 | public: |
55 | MyGraphicsObject() : QGraphicsObject() {} |
56 | virtual QRectF boundingRect() const { return QRectF(); } |
57 | virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {} |
58 | }; |
59 | |
60 | void tst_QGraphicsObject::pos() |
61 | { |
62 | MyGraphicsObject object; |
63 | QSignalSpy xSpy(&object, SIGNAL(xChanged())); |
64 | QSignalSpy ySpy(&object, SIGNAL(yChanged())); |
65 | QVERIFY(object.pos() == QPointF(0, 0)); |
66 | object.setPos(ax: 10, ay: 10); |
67 | QCOMPARE(xSpy.count(), 1); |
68 | QCOMPARE(ySpy.count(), 1); |
69 | |
70 | QCOMPARE(object.pos(), QPointF(10,10)); |
71 | |
72 | object.setPos(ax: 10, ay: 10); |
73 | QCOMPARE(xSpy.count(), 1); |
74 | QCOMPARE(ySpy.count(), 1); |
75 | |
76 | object.setProperty(name: "pos" , value: QPointF(0, 0)); |
77 | QCOMPARE(xSpy.count(), 2); |
78 | QCOMPARE(ySpy.count(), 2); |
79 | QCOMPARE(object.property("pos" ).toPointF(), QPointF(0,0)); |
80 | |
81 | object.setProperty(name: "pos" , value: QPointF(10, 0)); |
82 | QCOMPARE(xSpy.count(), 3); |
83 | QCOMPARE(ySpy.count(), 2); |
84 | QCOMPARE(object.property("pos" ).toPointF(), QPointF(10,0)); |
85 | |
86 | object.setProperty(name: "pos" , value: QPointF(10, 10)); |
87 | QCOMPARE(xSpy.count(), 3); |
88 | QCOMPARE(ySpy.count(), 3); |
89 | QVERIFY(object.property("pos" ) == QPointF(10, 10)); |
90 | } |
91 | |
92 | void tst_QGraphicsObject::x() |
93 | { |
94 | MyGraphicsObject object; |
95 | QSignalSpy xSpy(&object, SIGNAL(xChanged())); |
96 | QSignalSpy ySpy(&object, SIGNAL(yChanged())); |
97 | QVERIFY(object.pos() == QPointF(0, 0)); |
98 | object.setX(10); |
99 | QCOMPARE(xSpy.count(), 1); |
100 | QCOMPARE(ySpy.count(), 0); |
101 | |
102 | QVERIFY(object.pos() == QPointF(10, 0)); |
103 | QCOMPARE(object.x(), qreal(10)); |
104 | |
105 | object.setX(10); |
106 | QCOMPARE(xSpy.count(), 1); |
107 | QCOMPARE(ySpy.count(), 0); |
108 | |
109 | object.setProperty(name: "x" , value: 0); |
110 | QCOMPARE(xSpy.count(), 2); |
111 | QCOMPARE(ySpy.count(), 0); |
112 | QCOMPARE(object.property("x" ).toDouble(), double(0)); |
113 | } |
114 | |
115 | void tst_QGraphicsObject::y() |
116 | { |
117 | MyGraphicsObject object; |
118 | QSignalSpy xSpy(&object, SIGNAL(xChanged())); |
119 | QSignalSpy ySpy(&object, SIGNAL(yChanged())); |
120 | QVERIFY(object.pos() == QPointF(0, 0)); |
121 | object.setY(10); |
122 | QCOMPARE(xSpy.count(), 0); |
123 | QCOMPARE(ySpy.count(), 1); |
124 | |
125 | QVERIFY(object.pos() == QPointF(0, 10)); |
126 | QCOMPARE(object.y(), qreal(10)); |
127 | |
128 | object.setY(10); |
129 | QCOMPARE(xSpy.count(), 0); |
130 | QCOMPARE(ySpy.count(), 1); |
131 | |
132 | object.setProperty(name: "y" , value: 0); |
133 | QCOMPARE(xSpy.count(), 0); |
134 | QCOMPARE(ySpy.count(), 2); |
135 | QCOMPARE(object.property("y" ).toDouble(), qreal(0)); |
136 | } |
137 | |
138 | void tst_QGraphicsObject::z() |
139 | { |
140 | MyGraphicsObject object; |
141 | QSignalSpy zSpy(&object, SIGNAL(zChanged())); |
142 | QCOMPARE(object.zValue(), qreal(0)); |
143 | object.setZValue(10); |
144 | QCOMPARE(zSpy.count(), 1); |
145 | |
146 | QCOMPARE(object.zValue(), qreal(10)); |
147 | |
148 | object.setZValue(10); |
149 | QCOMPARE(zSpy.count(), 1); |
150 | |
151 | object.setProperty(name: "z" , value: 0); |
152 | QCOMPARE(zSpy.count(), 2); |
153 | QCOMPARE(object.property("z" ).toDouble(), double(0)); |
154 | } |
155 | |
156 | void tst_QGraphicsObject::opacity() |
157 | { |
158 | MyGraphicsObject object; |
159 | QSignalSpy spy(&object, SIGNAL(opacityChanged())); |
160 | QCOMPARE(object.opacity(), 1.); |
161 | object.setOpacity(0); |
162 | QCOMPARE(spy.count(), 1); |
163 | |
164 | QCOMPARE(object.opacity(), 0.); |
165 | |
166 | object.setOpacity(0); |
167 | QCOMPARE(spy.count(), 1); |
168 | |
169 | object.setProperty(name: "opacity" , value: .5); |
170 | QCOMPARE(spy.count(), 2); |
171 | QCOMPARE(object.property("opacity" ).toDouble(), .5); |
172 | } |
173 | |
174 | void tst_QGraphicsObject::enabled() |
175 | { |
176 | MyGraphicsObject object; |
177 | QSignalSpy spy(&object, SIGNAL(enabledChanged())); |
178 | QVERIFY(object.isEnabled()); |
179 | object.setEnabled(false); |
180 | QCOMPARE(spy.count(), 1); |
181 | |
182 | QVERIFY(!object.isEnabled()); |
183 | |
184 | object.setEnabled(false); |
185 | QCOMPARE(spy.count(), 1); |
186 | |
187 | object.setProperty(name: "enabled" , value: true); |
188 | QCOMPARE(spy.count(), 2); |
189 | QVERIFY(object.property("enabled" ).toBool()); |
190 | } |
191 | |
192 | void tst_QGraphicsObject::visible() |
193 | { |
194 | MyGraphicsObject object; |
195 | QSignalSpy spy(&object, SIGNAL(visibleChanged())); |
196 | QVERIFY(object.isVisible()); |
197 | object.setVisible(false); |
198 | QCOMPARE(spy.count(), 1); |
199 | |
200 | QVERIFY(!object.isVisible()); |
201 | |
202 | object.setVisible(false); |
203 | QCOMPARE(spy.count(), 1); |
204 | |
205 | object.setProperty(name: "visible" , value: true); |
206 | QCOMPARE(spy.count(), 2); |
207 | QVERIFY(object.property("visible" ).toBool()); |
208 | } |
209 | |
210 | class DeleteTester : public QGraphicsObject |
211 | { |
212 | public: |
213 | DeleteTester(bool *w, bool *pw, QGraphicsItem *parent = 0) |
214 | : QGraphicsObject(parent), wasDeleted(w), parentWasDeleted(pw) |
215 | { } |
216 | |
217 | ~DeleteTester() |
218 | { |
219 | *wasDeleted = QObjectPrivate::get(o: this)->wasDeleted; |
220 | if (QGraphicsItem *p = parentItem()) { |
221 | if (QGraphicsObject *o = p->toGraphicsObject()) |
222 | *parentWasDeleted = QObjectPrivate::get(o)->wasDeleted; |
223 | } |
224 | } |
225 | |
226 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = 0) |
227 | { } |
228 | QRectF boundingRect() const |
229 | { return QRectF(); } |
230 | |
231 | bool *wasDeleted; |
232 | bool *parentWasDeleted; |
233 | }; |
234 | |
235 | void tst_QGraphicsObject::deleted() |
236 | { |
237 | bool item1_parentWasDeleted = false; |
238 | bool item1_wasDeleted = false; |
239 | bool item2_parentWasDeleted = false; |
240 | bool item2_wasDeleted = false; |
241 | DeleteTester *item1 = new DeleteTester(&item1_wasDeleted, &item1_parentWasDeleted); |
242 | DeleteTester *item2 = new DeleteTester(&item2_wasDeleted, &item2_parentWasDeleted, item1); |
243 | Q_UNUSED(item2); |
244 | delete item1; |
245 | |
246 | QVERIFY(!item1_wasDeleted); // destructor not called yet |
247 | QVERIFY(!item1_parentWasDeleted); // no parent |
248 | QVERIFY(!item2_wasDeleted); // destructor not called yet |
249 | QVERIFY(item2_parentWasDeleted); |
250 | } |
251 | |
252 | QTEST_MAIN(tst_QGraphicsObject) |
253 | #include "tst_qgraphicsobject.moc" |
254 | |
255 | |