Extract all QtQuick 1 elements into a separate library/plugin.
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeparticles / tst_qdeclarativeparticles.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/QSignalSpy>
43 #include <QtQuick1/qdeclarativeview.h>
44 #include <QGraphicsObject>
45
46 #ifdef Q_OS_SYMBIAN
47 // In Symbian OS test data is located in applications private dir
48 #define SRCDIR "."
49 #endif
50
51 class tst_QDeclarativeParticles : public QObject
52 {
53     Q_OBJECT
54 public:
55     tst_QDeclarativeParticles();
56
57 private slots:
58     void properties();
59     void motionGravity();
60     void motionWander();
61     void runs();
62 private:
63     QDeclarativeView *createView(const QString &filename);
64
65 };
66
67 tst_QDeclarativeParticles::tst_QDeclarativeParticles()
68 {
69 }
70
71 void tst_QDeclarativeParticles::properties()
72 {
73     QDeclarativeView *canvas = createView(SRCDIR "/data/particlestest.qml");
74     QVERIFY(canvas->rootObject());
75
76     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
77     QVERIFY(particles);
78
79     particles->setProperty("source", QUrl::fromLocalFile(SRCDIR "/data/particle.png"));
80     QCOMPARE(particles->property("source").toUrl(), QUrl::fromLocalFile(SRCDIR "/data/particle.png"));
81
82     particles->setProperty("lifeSpanDeviation", (1000));
83     QCOMPARE(particles->property("lifeSpanDeviation").toInt(), 1000);
84
85     particles->setProperty("fadeInDuration", 1000);
86     QCOMPARE(particles->property("fadeInDuration").toInt(), 1000);
87
88     particles->setProperty("fadeOutDuration", 1000);
89     QCOMPARE(particles->property("fadeOutDuration").toInt(), 1000);
90
91     particles->setProperty("angle", 100.0);
92     QCOMPARE(particles->property("angle").toDouble(), 100.0);
93
94     particles->setProperty("angleDeviation", 100.0);
95     QCOMPARE(particles->property("angleDeviation").toDouble(), 100.0);
96
97     particles->setProperty("velocity", 100.0);
98     QCOMPARE(particles->property("velocity").toDouble(), 100.0);
99
100     particles->setProperty("velocityDeviation", 100.0);
101     QCOMPARE(particles->property("velocityDeviation").toDouble(), 100.0);
102
103     particles->setProperty("emissionVariance", 0.5);
104     QCOMPARE(particles->property("emissionVariance").toDouble(),0.5);
105
106     particles->setProperty("emissionRate", 12);
107     QCOMPARE(particles->property("emissionRate").toInt(), 12);
108
109     delete canvas;
110 }
111
112 void tst_QDeclarativeParticles::motionGravity()
113 {
114     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
115     QVERIFY(canvas->rootObject());
116
117     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
118     QVERIFY(particles);
119
120     QObject* motionGravity = canvas->rootObject()->findChild<QObject*>("motionGravity");
121     //QCOMPARE(qvariant_cast<QObject*>(particles->property("motion")), motionGravity);
122
123     QSignalSpy xattractorSpy(motionGravity, SIGNAL(xattractorChanged()));
124     QSignalSpy yattractorSpy(motionGravity, SIGNAL(yattractorChanged()));
125     QSignalSpy accelerationSpy(motionGravity, SIGNAL(accelerationChanged()));
126
127     QCOMPARE(motionGravity->property("xattractor").toDouble(), 0.0);
128     QCOMPARE(motionGravity->property("yattractor").toDouble(), 1000.0);
129     QCOMPARE(motionGravity->property("acceleration").toDouble(), 25.0);
130
131     motionGravity->setProperty("xattractor", 20.0);
132     motionGravity->setProperty("yattractor", 10.0);
133     motionGravity->setProperty("acceleration", 10.0);
134
135     QCOMPARE(motionGravity->property("xattractor").toDouble(), 20.0);
136     QCOMPARE(motionGravity->property("yattractor").toDouble(), 10.0);
137     QCOMPARE(motionGravity->property("acceleration").toDouble(), 10.0);
138
139     QCOMPARE(xattractorSpy.count(), 1);
140     QCOMPARE(yattractorSpy.count(), 1);
141     QCOMPARE(accelerationSpy.count(), 1);
142
143     motionGravity->setProperty("xattractor", 20.0);
144     motionGravity->setProperty("yattractor", 10.0);
145     motionGravity->setProperty("acceleration", 10.0);
146
147     QCOMPARE(xattractorSpy.count(), 1);
148     QCOMPARE(yattractorSpy.count(), 1);
149     QCOMPARE(accelerationSpy.count(), 1);
150
151     delete canvas;
152 }
153
154 void tst_QDeclarativeParticles::motionWander()
155 {
156     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
157     QVERIFY(canvas->rootObject());
158
159     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
160     QVERIFY(particles);
161
162     QSignalSpy motionSpy(particles, SIGNAL(motionChanged()));
163     QObject* motionWander = canvas->rootObject()->findChild<QObject*>("motionWander");
164
165     QCOMPARE(motionSpy.count(), 0);
166     particles->setProperty("motion", QVariant::fromValue(motionWander));
167     //QCOMPARE(particles->property("motion"), QVariant::fromValue(motionWander));
168     //QCOMPARE(motionSpy.count(), 1);
169
170     particles->setProperty("motion", QVariant::fromValue(motionWander));
171     //QCOMPARE(motionSpy.count(), 1);
172
173     QSignalSpy xvarianceSpy(motionWander, SIGNAL(xvarianceChanged()));
174     QSignalSpy yvarianceSpy(motionWander, SIGNAL(yvarianceChanged()));
175     QSignalSpy paceSpy(motionWander, SIGNAL(paceChanged()));
176
177     QCOMPARE(motionWander->property("xvariance").toDouble(), 30.0);
178     QCOMPARE(motionWander->property("yvariance").toDouble(), 30.0);
179     QCOMPARE(motionWander->property("pace").toDouble(), 100.0);
180
181     motionWander->setProperty("xvariance", 20.0);
182     motionWander->setProperty("yvariance", 10.0);
183     motionWander->setProperty("pace", 10.0);
184
185     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
186     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
187     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
188
189     QCOMPARE(xvarianceSpy.count(), 1);
190     QCOMPARE(yvarianceSpy.count(), 1);
191     QCOMPARE(paceSpy.count(), 1);
192
193     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
194     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
195     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
196
197     QCOMPARE(xvarianceSpy.count(), 1);
198     QCOMPARE(yvarianceSpy.count(), 1);
199     QCOMPARE(paceSpy.count(), 1);
200
201     delete canvas;
202 }
203
204 void tst_QDeclarativeParticles::runs()
205 {
206     QDeclarativeView *canvas = createView(SRCDIR "/data/particlestest.qml");
207     QVERIFY(canvas->rootObject());
208
209     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
210     QVERIFY(particles);
211     QTest::qWait(1000);//Run for one second. Test passes if it doesn't crash.
212
213     delete canvas;
214 }
215
216 QDeclarativeView *tst_QDeclarativeParticles::createView(const QString &filename)
217 {
218     QDeclarativeView *canvas = new QDeclarativeView(0);
219     canvas->setFixedSize(240,320);
220
221     canvas->setSource(QUrl::fromLocalFile(filename));
222
223     return canvas;
224 }
225 QTEST_MAIN(tst_QDeclarativeParticles)
226
227 #include "tst_qdeclarativeparticles.moc"