Rename speed -> velocity in the particle system
[profile/ivi/qtdeclarative.git] / tests / auto / particles / qquickimageparticle / tst_qquickimageparticle.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43 #include "../shared/particlestestsshared.h"
44 #include <private/qquickparticlesystem_p.h>
45 #include <private/qabstractanimation_p.h>
46
47 #include "../../shared/util.h"
48
49 const double CONV_FACTOR = 0.017453292519943295;//Degrees to radians
50
51 class tst_qquickimageparticle : public QQmlDataTest
52 {
53     Q_OBJECT
54 public:
55     tst_qquickimageparticle() {}
56     ~tst_qquickimageparticle();
57
58 private slots:
59     void initTestCase();
60     void test_basic();
61     void test_colored();
62     void test_colorVariance();
63     void test_deformed();
64     void test_tabled();
65     void test_sprite();
66 };
67
68 void tst_qquickimageparticle::initTestCase()
69 {
70     QQmlDataTest::initTestCase();
71     QUnifiedTimer::instance()->setConsistentTiming(true);
72     //QQuickImageParticle has several debug statements, with possible pointer dereferences
73     qputenv("QML_PARTICLES_DEBUG","please");
74 }
75
76 tst_qquickimageparticle::~tst_qquickimageparticle()
77 {
78     qputenv("QML_PARTICLES_DEBUG","");
79 }
80
81 void tst_qquickimageparticle::test_basic()
82 {
83     QQuickView* view = createView(testFileUrl("basic.qml"), 600);
84     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
85     ensureAnimTime(600, system->m_animation);
86
87     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
88     foreach (QQuickParticleData *d, system->groupData[0]->data) {
89         if (d->t == -1)
90             continue; //Particle data unused
91
92         QCOMPARE(d->x, 0.f);
93         QCOMPARE(d->y, 0.f);
94         QCOMPARE(d->vx, 0.f);
95         QCOMPARE(d->vy, 0.f);
96         QCOMPARE(d->ax, 0.f);
97         QCOMPARE(d->ay, 0.f);
98         QCOMPARE(d->lifeSpan, 0.5f);
99         QCOMPARE(d->size, 32.f);
100         QCOMPARE(d->endSize, 32.f);
101         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
102         QCOMPARE(d->color.r, (uchar)255);
103         QCOMPARE(d->color.g, (uchar)255);
104         QCOMPARE(d->color.b, (uchar)255);
105         QCOMPARE(d->color.a, (uchar)255);
106         QCOMPARE(d->xx, 1.0f);
107         QCOMPARE(d->xy, 0.0f);
108         QCOMPARE(d->yy, 1.0f);
109         QCOMPARE(d->yx, 0.0f);
110         QCOMPARE(d->rotation, 0.0f);
111         QCOMPARE(d->rotationVelocity, 0.0f);
112         QCOMPARE(d->autoRotate, 0.0f);
113         QCOMPARE(d->animX, 0.0f);
114         QCOMPARE(d->animY, 0.0f);
115         QCOMPARE(d->animWidth, 1.0f);
116         QCOMPARE(d->animHeight, 1.0f);
117         QCOMPARE(d->frameDuration, 1.0f);
118         QCOMPARE(d->frameCount, 1.0f);
119         QCOMPARE(d->animT, -1.0f);
120     }
121     delete view;
122 }
123
124
125 void tst_qquickimageparticle::test_colored()
126 {
127     QQuickView* view = createView(testFileUrl("colored.qml"), 600);
128     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
129     ensureAnimTime(600, system->m_animation);
130
131     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
132     foreach (QQuickParticleData *d, system->groupData[0]->data) {
133         if (d->t == -1)
134             continue; //Particle data unused
135
136         QCOMPARE(d->x, 0.f);
137         QCOMPARE(d->y, 0.f);
138         QCOMPARE(d->vx, 0.f);
139         QCOMPARE(d->vy, 0.f);
140         QCOMPARE(d->ax, 0.f);
141         QCOMPARE(d->ay, 0.f);
142         QCOMPARE(d->lifeSpan, 0.5f);
143         QCOMPARE(d->size, 32.f);
144         QCOMPARE(d->endSize, 32.f);
145         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
146         QCOMPARE(d->color.r, (uchar)003);
147         QCOMPARE(d->color.g, (uchar)002);
148         QCOMPARE(d->color.b, (uchar)001);
149         QCOMPARE(d->color.a, (uchar)127);
150         QCOMPARE(d->xx, 1.0f);
151         QCOMPARE(d->xy, 0.0f);
152         QCOMPARE(d->yy, 1.0f);
153         QCOMPARE(d->yx, 0.0f);
154         QCOMPARE(d->rotation, 0.0f);
155         QCOMPARE(d->rotationVelocity, 0.0f);
156         QCOMPARE(d->autoRotate, 0.0f);
157         QCOMPARE(d->animX, 0.0f);
158         QCOMPARE(d->animY, 0.0f);
159         QCOMPARE(d->animWidth, 1.0f);
160         QCOMPARE(d->animHeight, 1.0f);
161         QCOMPARE(d->frameDuration, 1.0f);
162         QCOMPARE(d->frameCount, 1.0f);
163         QCOMPARE(d->animT, -1.0f);
164     }
165     delete view;
166 }
167
168
169 void tst_qquickimageparticle::test_colorVariance()
170 {
171     QQuickView* view = createView(testFileUrl("colorVariance.qml"), 600);
172     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
173     ensureAnimTime(600, system->m_animation);
174
175     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
176     foreach (QQuickParticleData *d, system->groupData[0]->data) {
177         if (d->t == -1)
178             continue; //Particle data unused
179
180         QCOMPARE(d->x, 0.f);
181         QCOMPARE(d->y, 0.f);
182         QCOMPARE(d->vx, 0.f);
183         QCOMPARE(d->vy, 0.f);
184         QCOMPARE(d->ax, 0.f);
185         QCOMPARE(d->ay, 0.f);
186         QCOMPARE(d->lifeSpan, 0.5f);
187         QCOMPARE(d->size, 32.f);
188         QCOMPARE(d->endSize, 32.f);
189         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
190         QVERIFY(d->color.r < 128);
191         QVERIFY(d->color.g < 64);
192         QVERIFY(d->color.b < 32);
193         QVERIFY(d->color.a >= 64);
194         QVERIFY(d->color.a < 192);
195         QCOMPARE(d->xx, 1.0f);
196         QCOMPARE(d->xy, 0.0f);
197         QCOMPARE(d->yy, 1.0f);
198         QCOMPARE(d->yx, 0.0f);
199         QCOMPARE(d->rotation, 0.0f);
200         QCOMPARE(d->rotationVelocity, 0.0f);
201         QCOMPARE(d->autoRotate, 0.0f);
202         QCOMPARE(d->animX, 0.0f);
203         QCOMPARE(d->animY, 0.0f);
204         QCOMPARE(d->animWidth, 1.0f);
205         QCOMPARE(d->animHeight, 1.0f);
206         QCOMPARE(d->frameDuration, 1.0f);
207         QCOMPARE(d->frameCount, 1.0f);
208         QCOMPARE(d->animT, -1.0f);
209     }
210     delete view;
211 }
212
213
214 void tst_qquickimageparticle::test_deformed()
215 {
216     QQuickView* view = createView(testFileUrl("deformed.qml"), 600);
217     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
218     ensureAnimTime(600, system->m_animation);
219
220     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
221     foreach (QQuickParticleData *d, system->groupData[0]->data) {
222         if (d->t == -1)
223             continue; //Particle data unused
224
225         QCOMPARE(d->x, 0.f);
226         QCOMPARE(d->y, 0.f);
227         QCOMPARE(d->vx, 0.f);
228         QCOMPARE(d->vy, 0.f);
229         QCOMPARE(d->ax, 0.f);
230         QCOMPARE(d->ay, 0.f);
231         QCOMPARE(d->lifeSpan, 0.5f);
232         QCOMPARE(d->size, 32.f);
233         QCOMPARE(d->endSize, 32.f);
234         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
235         QCOMPARE(d->color.r, (uchar)255);
236         QCOMPARE(d->color.g, (uchar)255);
237         QCOMPARE(d->color.b, (uchar)255);
238         QCOMPARE(d->color.a, (uchar)255);
239         QCOMPARE(d->xx, 0.5f);
240         QCOMPARE(d->xy, 0.5f);
241         QCOMPARE(d->yy, 0.5f);
242         QCOMPARE(d->yx, 0.5f);
243         QCOMPARE(d->rotation, 90.0f * (float)CONV_FACTOR);
244         QCOMPARE(d->rotationVelocity, 90.0f * (float)CONV_FACTOR);
245         QCOMPARE(d->autoRotate, 1.0f);
246         QCOMPARE(d->animX, 0.0f);
247         QCOMPARE(d->animY, 0.0f);
248         QCOMPARE(d->animWidth, 1.0f);
249         QCOMPARE(d->animHeight, 1.0f);
250         QCOMPARE(d->frameDuration, 1.0f);
251         QCOMPARE(d->frameCount, 1.0f);
252         QCOMPARE(d->animT, -1.0f);
253     }
254     delete view;
255 }
256
257
258 void tst_qquickimageparticle::test_tabled()
259 {
260     QQuickView* view = createView(testFileUrl("tabled.qml"), 600);
261     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
262     ensureAnimTime(600, system->m_animation);
263
264     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
265     foreach (QQuickParticleData *d, system->groupData[0]->data) {
266         if (d->t == -1)
267             continue; //Particle data unused
268
269         QCOMPARE(d->x, 0.f);
270         QCOMPARE(d->y, 0.f);
271         QCOMPARE(d->vx, 0.f);
272         QCOMPARE(d->vy, 0.f);
273         QCOMPARE(d->ax, 0.f);
274         QCOMPARE(d->ay, 0.f);
275         QCOMPARE(d->lifeSpan, 0.5f);
276         QCOMPARE(d->size, 32.f);
277         QCOMPARE(d->endSize, 32.f);
278         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
279         QCOMPARE(d->color.r, (uchar)255);
280         QCOMPARE(d->color.g, (uchar)255);
281         QCOMPARE(d->color.b, (uchar)255);
282         QCOMPARE(d->color.a, (uchar)255);
283         QCOMPARE(d->xx, 1.0f);
284         QCOMPARE(d->xy, 0.0f);
285         QCOMPARE(d->yy, 1.0f);
286         QCOMPARE(d->yx, 0.0f);
287         QCOMPARE(d->rotation, 0.0f);
288         QCOMPARE(d->rotationVelocity, 0.0f);
289         QCOMPARE(d->autoRotate, 0.0f);
290         QCOMPARE(d->animX, 0.0f);
291         QCOMPARE(d->animY, 0.0f);
292         QCOMPARE(d->animWidth, 1.0f);
293         QCOMPARE(d->animHeight, 1.0f);
294         QCOMPARE(d->frameDuration, 1.0f);
295         QCOMPARE(d->frameCount, 1.0f);
296         QCOMPARE(d->animT, -1.0f);
297         //TODO: This performance level doesn't alter particleData, but goes straight to shaders. Find something to test
298     }
299     delete view;
300 }
301
302
303 void tst_qquickimageparticle::test_sprite()
304 {
305     QQuickView* view = createView(testFileUrl("sprite.qml"), 600);
306     QQuickParticleSystem* system = view->rootObject()->findChild<QQuickParticleSystem*>("system");
307     ensureAnimTime(600, system->m_animation);
308
309     QVERIFY(extremelyFuzzyCompare(system->groupData[0]->size(), 500, 10));
310     foreach (QQuickParticleData *d, system->groupData[0]->data) {
311         if (d->t == -1)
312             continue; //Particle data unused
313
314         QCOMPARE(d->x, 0.f);
315         QCOMPARE(d->y, 0.f);
316         QCOMPARE(d->vx, 0.f);
317         QCOMPARE(d->vy, 0.f);
318         QCOMPARE(d->ax, 0.f);
319         QCOMPARE(d->ay, 0.f);
320         QCOMPARE(d->lifeSpan, 0.5f);
321         QCOMPARE(d->size, 32.f);
322         QCOMPARE(d->endSize, 32.f);
323         QVERIFY(myFuzzyLEQ(d->t, ((qreal)system->timeInt/1000.0)));
324         QCOMPARE(d->color.r, (uchar)255);
325         QCOMPARE(d->color.g, (uchar)255);
326         QCOMPARE(d->color.b, (uchar)255);
327         QCOMPARE(d->color.a, (uchar)255);
328         QCOMPARE(d->xx, 1.0f);
329         QCOMPARE(d->xy, 0.0f);
330         QCOMPARE(d->yy, 1.0f);
331         QCOMPARE(d->yx, 0.0f);
332         QCOMPARE(d->rotation, 0.0f);
333         QCOMPARE(d->rotationVelocity, 0.0f);
334         QCOMPARE(d->autoRotate, 0.0f);
335         QVERIFY(myFuzzyCompare(d->frameDuration, 120.f));
336         QCOMPARE(d->frameCount, 6.0f);
337         QVERIFY(d->animT > 0.0f);
338         QCOMPARE(d->animX, 0.0f);
339         QCOMPARE(d->animY, 0.0f);
340         QCOMPARE(d->animWidth, 31.0f);
341         QCOMPARE(d->animHeight, 30.0f);
342     }
343     delete view;
344 }
345
346 QTEST_MAIN(tst_qquickimageparticle);
347
348 #include "tst_qquickimageparticle.moc"