~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/auto/widgets/graphicsview/qgraphicsobject/tst_qgraphicsobject.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
**
 
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.
 
28
**
 
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.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
 
 
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>
 
50
 
 
51
class tst_QGraphicsObject : public QObject {
 
52
    Q_OBJECT
 
53
 
 
54
public slots:
 
55
    void initTestCase();
 
56
    void cleanupTestCase();
 
57
    void init();
 
58
    void cleanup();
 
59
 
 
60
private slots:
 
61
    void pos();
 
62
    void x();
 
63
    void y();
 
64
    void z();
 
65
    void opacity();
 
66
    void enabled();
 
67
    void visible();
 
68
    void deleted();
 
69
};
 
70
 
 
71
 
 
72
// This will be called before the first test function is executed.
 
73
// It is only called once.
 
74
void tst_QGraphicsObject::initTestCase()
 
75
{
 
76
}
 
77
 
 
78
// This will be called after the last test function is executed.
 
79
// It is only called once.
 
80
void tst_QGraphicsObject::cleanupTestCase()
 
81
{
 
82
}
 
83
 
 
84
// This will be called before each test function is executed.
 
85
void tst_QGraphicsObject::init()
 
86
{
 
87
}
 
88
 
 
89
// This will be called after every test function.
 
90
void tst_QGraphicsObject::cleanup()
 
91
{
 
92
}
 
93
 
 
94
 
 
95
class MyGraphicsObject : public QGraphicsObject
 
96
{
 
97
public:
 
98
    MyGraphicsObject() : QGraphicsObject() {}
 
99
    virtual QRectF boundingRect() const { return QRectF(); }
 
100
    virtual void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) {}
 
101
};
 
102
 
 
103
void tst_QGraphicsObject::pos()
 
104
{
 
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);
 
112
 
 
113
    QVERIFY(object.pos() == QPointF(10,10));
 
114
 
 
115
    object.setPos(10, 10);
 
116
    QCOMPARE(xSpy.count(), 1);
 
117
    QCOMPARE(ySpy.count(), 1);
 
118
 
 
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));
 
123
 
 
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));
 
128
 
 
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));
 
133
}
 
134
 
 
135
void tst_QGraphicsObject::x()
 
136
{
 
137
    MyGraphicsObject object;
 
138
    QSignalSpy xSpy(&object, SIGNAL(xChanged()));
 
139
    QSignalSpy ySpy(&object, SIGNAL(yChanged()));
 
140
    QVERIFY(object.pos() == QPointF(0, 0));
 
141
    object.setX(10);
 
142
    QCOMPARE(xSpy.count(), 1);
 
143
    QCOMPARE(ySpy.count(), 0);
 
144
 
 
145
    QVERIFY(object.pos() == QPointF(10, 0));
 
146
    QVERIFY(object.x() == 10);
 
147
 
 
148
    object.setX(10);
 
149
    QCOMPARE(xSpy.count(), 1);
 
150
    QCOMPARE(ySpy.count(), 0);
 
151
 
 
152
    object.setProperty("x", 0);
 
153
    QCOMPARE(xSpy.count(), 2);
 
154
    QCOMPARE(ySpy.count(), 0);
 
155
    QVERIFY(object.property("x") == 0);
 
156
}
 
157
 
 
158
void tst_QGraphicsObject::y()
 
159
{
 
160
    MyGraphicsObject object;
 
161
    QSignalSpy xSpy(&object, SIGNAL(xChanged()));
 
162
    QSignalSpy ySpy(&object, SIGNAL(yChanged()));
 
163
    QVERIFY(object.pos() == QPointF(0, 0));
 
164
    object.setY(10);
 
165
    QCOMPARE(xSpy.count(), 0);
 
166
    QCOMPARE(ySpy.count(), 1);
 
167
 
 
168
    QVERIFY(object.pos() == QPointF(0, 10));
 
169
    QVERIFY(object.y() == 10);
 
170
 
 
171
    object.setY(10);
 
172
    QCOMPARE(xSpy.count(), 0);
 
173
    QCOMPARE(ySpy.count(), 1);
 
174
 
 
175
    object.setProperty("y", 0);
 
176
    QCOMPARE(xSpy.count(), 0);
 
177
    QCOMPARE(ySpy.count(), 2);
 
178
    QVERIFY(object.property("y") == 0);
 
179
}
 
180
 
 
181
void tst_QGraphicsObject::z()
 
182
{
 
183
    MyGraphicsObject object;
 
184
    QSignalSpy zSpy(&object, SIGNAL(zChanged()));
 
185
    QVERIFY(object.zValue() == 0);
 
186
    object.setZValue(10);
 
187
    QCOMPARE(zSpy.count(), 1);
 
188
 
 
189
    QVERIFY(object.zValue() == 10);
 
190
 
 
191
    object.setZValue(10);
 
192
    QCOMPARE(zSpy.count(), 1);
 
193
 
 
194
    object.setProperty("z", 0);
 
195
    QCOMPARE(zSpy.count(), 2);
 
196
    QVERIFY(object.property("z") == 0);
 
197
}
 
198
 
 
199
void tst_QGraphicsObject::opacity()
 
200
{
 
201
    MyGraphicsObject object;
 
202
    QSignalSpy spy(&object, SIGNAL(opacityChanged()));
 
203
    QVERIFY(object.opacity() == 1.);
 
204
    object.setOpacity(0);
 
205
    QCOMPARE(spy.count(), 1);
 
206
 
 
207
    QVERIFY(object.opacity() == 0.);
 
208
 
 
209
    object.setOpacity(0);
 
210
    QCOMPARE(spy.count(), 1);
 
211
 
 
212
    object.setProperty("opacity", .5);
 
213
    QCOMPARE(spy.count(), 2);
 
214
    QVERIFY(object.property("opacity") == .5);
 
215
}
 
216
 
 
217
void tst_QGraphicsObject::enabled()
 
218
{
 
219
    MyGraphicsObject object;
 
220
    QSignalSpy spy(&object, SIGNAL(enabledChanged()));
 
221
    QVERIFY(object.isEnabled() == true);
 
222
    object.setEnabled(false);
 
223
    QCOMPARE(spy.count(), 1);
 
224
 
 
225
    QVERIFY(object.isEnabled() == false);
 
226
 
 
227
    object.setEnabled(false);
 
228
    QCOMPARE(spy.count(), 1);
 
229
 
 
230
    object.setProperty("enabled", true);
 
231
    QCOMPARE(spy.count(), 2);
 
232
    QVERIFY(object.property("enabled") == true);
 
233
}
 
234
 
 
235
void tst_QGraphicsObject::visible()
 
236
{
 
237
    MyGraphicsObject object;
 
238
    QSignalSpy spy(&object, SIGNAL(visibleChanged()));
 
239
    QVERIFY(object.isVisible() == true);
 
240
    object.setVisible(false);
 
241
    QCOMPARE(spy.count(), 1);
 
242
 
 
243
    QVERIFY(object.isVisible() == false);
 
244
 
 
245
    object.setVisible(false);
 
246
    QCOMPARE(spy.count(), 1);
 
247
 
 
248
    object.setProperty("visible", true);
 
249
    QCOMPARE(spy.count(), 2);
 
250
    QVERIFY(object.property("visible") == true);
 
251
}
 
252
 
 
253
class DeleteTester : public QGraphicsObject
 
254
{
 
255
public:
 
256
    DeleteTester(bool *w, bool *pw, QGraphicsItem *parent = 0)
 
257
        : QGraphicsObject(parent), wasDeleted(w), parentWasDeleted(pw)
 
258
    { }
 
259
 
 
260
    ~DeleteTester()
 
261
    {
 
262
        *wasDeleted = QObjectPrivate::get(this)->wasDeleted;
 
263
        if (QGraphicsItem *p = parentItem()) {
 
264
            if (QGraphicsObject *o = p->toGraphicsObject())
 
265
                *parentWasDeleted = QObjectPrivate::get(o)->wasDeleted;
 
266
        }
 
267
    }
 
268
 
 
269
    void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget * = 0)
 
270
    { }
 
271
    QRectF boundingRect() const
 
272
    { return QRectF(); }
 
273
 
 
274
    bool *wasDeleted;
 
275
    bool *parentWasDeleted;
 
276
};
 
277
 
 
278
void tst_QGraphicsObject::deleted()
 
279
{
 
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);
 
286
    Q_UNUSED(item2);
 
287
    delete item1;
 
288
 
 
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);
 
293
}
 
294
 
 
295
QTEST_MAIN(tst_QGraphicsObject)
 
296
#include "tst_qgraphicsobject.moc"
 
297