1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
43
#include <QtTest/QtTest>
44
#include <qgraphicsitem.h>
45
#include <qgraphicsscene.h>
46
#include <qgraphicssceneevent.h>
47
#include <qgraphicsview.h>
48
#include <qstyleoption.h>
49
#include <private/qobject_p.h>
51
class tst_QGraphicsObject : public QObject {
56
void cleanupTestCase();
72
// This will be called before the first test function is executed.
73
// It is only called once.
74
void tst_QGraphicsObject::initTestCase()
78
// This will be called after the last test function is executed.
79
// It is only called once.
80
void tst_QGraphicsObject::cleanupTestCase()
84
// This will be called before each test function is executed.
85
void tst_QGraphicsObject::init()
89
// This will be called after every test function.
90
void tst_QGraphicsObject::cleanup()
95
class MyGraphicsObject : public QGraphicsObject
98
MyGraphicsObject() : QGraphicsObject() {}
99
virtual QRectF boundingRect() const { return QRectF(); }
100
virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}
103
void tst_QGraphicsObject::pos()
105
MyGraphicsObject object;
106
QSignalSpy xSpy(&object, SIGNAL(xChanged()));
107
QSignalSpy ySpy(&object, SIGNAL(yChanged()));
108
QVERIFY(object.pos() == QPointF(0, 0));
109
object.setPos(10, 10);
110
QCOMPARE(xSpy.count(), 1);
111
QCOMPARE(ySpy.count(), 1);
113
QVERIFY(object.pos() == QPointF(10,10));
115
object.setPos(10, 10);
116
QCOMPARE(xSpy.count(), 1);
117
QCOMPARE(ySpy.count(), 1);
119
object.setProperty("pos", QPointF(0, 0));
120
QCOMPARE(xSpy.count(), 2);
121
QCOMPARE(ySpy.count(), 2);
122
QVERIFY(object.property("pos") == QPointF(0,0));
124
object.setProperty("pos", QPointF(10, 0));
125
QCOMPARE(xSpy.count(), 3);
126
QCOMPARE(ySpy.count(), 2);
127
QVERIFY(object.property("pos") == QPointF(10,0));
129
object.setProperty("pos", QPointF(10, 10));
130
QCOMPARE(xSpy.count(), 3);
131
QCOMPARE(ySpy.count(), 3);
132
QVERIFY(object.property("pos") == QPointF(10, 10));
135
void tst_QGraphicsObject::x()
137
MyGraphicsObject object;
138
QSignalSpy xSpy(&object, SIGNAL(xChanged()));
139
QSignalSpy ySpy(&object, SIGNAL(yChanged()));
140
QVERIFY(object.pos() == QPointF(0, 0));
142
QCOMPARE(xSpy.count(), 1);
143
QCOMPARE(ySpy.count(), 0);
145
QVERIFY(object.pos() == QPointF(10, 0));
146
QVERIFY(object.x() == 10);
149
QCOMPARE(xSpy.count(), 1);
150
QCOMPARE(ySpy.count(), 0);
152
object.setProperty("x", 0);
153
QCOMPARE(xSpy.count(), 2);
154
QCOMPARE(ySpy.count(), 0);
155
QVERIFY(object.property("x") == 0);
158
void tst_QGraphicsObject::y()
160
MyGraphicsObject object;
161
QSignalSpy xSpy(&object, SIGNAL(xChanged()));
162
QSignalSpy ySpy(&object, SIGNAL(yChanged()));
163
QVERIFY(object.pos() == QPointF(0, 0));
165
QCOMPARE(xSpy.count(), 0);
166
QCOMPARE(ySpy.count(), 1);
168
QVERIFY(object.pos() == QPointF(0, 10));
169
QVERIFY(object.y() == 10);
172
QCOMPARE(xSpy.count(), 0);
173
QCOMPARE(ySpy.count(), 1);
175
object.setProperty("y", 0);
176
QCOMPARE(xSpy.count(), 0);
177
QCOMPARE(ySpy.count(), 2);
178
QVERIFY(object.property("y") == 0);
181
void tst_QGraphicsObject::z()
183
MyGraphicsObject object;
184
QSignalSpy zSpy(&object, SIGNAL(zChanged()));
185
QVERIFY(object.zValue() == 0);
186
object.setZValue(10);
187
QCOMPARE(zSpy.count(), 1);
189
QVERIFY(object.zValue() == 10);
191
object.setZValue(10);
192
QCOMPARE(zSpy.count(), 1);
194
object.setProperty("z", 0);
195
QCOMPARE(zSpy.count(), 2);
196
QVERIFY(object.property("z") == 0);
199
void tst_QGraphicsObject::opacity()
201
MyGraphicsObject object;
202
QSignalSpy spy(&object, SIGNAL(opacityChanged()));
203
QVERIFY(object.opacity() == 1.);
204
object.setOpacity(0);
205
QCOMPARE(spy.count(), 1);
207
QVERIFY(object.opacity() == 0.);
209
object.setOpacity(0);
210
QCOMPARE(spy.count(), 1);
212
object.setProperty("opacity", .5);
213
QCOMPARE(spy.count(), 2);
214
QVERIFY(object.property("opacity") == .5);
217
void tst_QGraphicsObject::enabled()
219
MyGraphicsObject object;
220
QSignalSpy spy(&object, SIGNAL(enabledChanged()));
221
QVERIFY(object.isEnabled() == true);
222
object.setEnabled(false);
223
QCOMPARE(spy.count(), 1);
225
QVERIFY(object.isEnabled() == false);
227
object.setEnabled(false);
228
QCOMPARE(spy.count(), 1);
230
object.setProperty("enabled", true);
231
QCOMPARE(spy.count(), 2);
232
QVERIFY(object.property("enabled") == true);
235
void tst_QGraphicsObject::visible()
237
MyGraphicsObject object;
238
QSignalSpy spy(&object, SIGNAL(visibleChanged()));
239
QVERIFY(object.isVisible() == true);
240
object.setVisible(false);
241
QCOMPARE(spy.count(), 1);
243
QVERIFY(object.isVisible() == false);
245
object.setVisible(false);
246
QCOMPARE(spy.count(), 1);
248
object.setProperty("visible", true);
249
QCOMPARE(spy.count(), 2);
250
QVERIFY(object.property("visible") == true);
253
class DeleteTester : public QGraphicsObject
256
DeleteTester(bool *w, bool *pw, QGraphicsItem *parent = 0)
257
: QGraphicsObject(parent), wasDeleted(w), parentWasDeleted(pw)
262
*wasDeleted = QObjectPrivate::get(this)->wasDeleted;
263
if (QGraphicsItem *p = parentItem()) {
264
if (QGraphicsObject *o = p->toGraphicsObject())
265
*parentWasDeleted = QObjectPrivate::get(o)->wasDeleted;
269
void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = 0)
271
QRectF boundingRect() const
275
bool *parentWasDeleted;
278
void tst_QGraphicsObject::deleted()
280
bool item1_parentWasDeleted = false;
281
bool item1_wasDeleted = false;
282
bool item2_parentWasDeleted = false;
283
bool item2_wasDeleted = false;
284
DeleteTester *item1 = new DeleteTester(&item1_wasDeleted, &item1_parentWasDeleted);
285
DeleteTester *item2 = new DeleteTester(&item2_wasDeleted, &item2_parentWasDeleted, item1);
289
QVERIFY(!item1_wasDeleted); // destructor not called yet
290
QVERIFY(!item1_parentWasDeleted); // no parent
291
QVERIFY(!item2_wasDeleted); // destructor not called yet
292
QVERIFY(item2_parentWasDeleted);
295
QTEST_MAIN(tst_QGraphicsObject)
296
#include "tst_qgraphicsobject.moc"