Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / 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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtTest/QSignalSpy>
43 #include <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
110 void tst_QDeclarativeParticles::motionGravity()
111 {
112     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
113     QVERIFY(canvas->rootObject());
114
115     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
116     QVERIFY(particles);
117
118     QObject* motionGravity = canvas->rootObject()->findChild<QObject*>("motionGravity");
119     //QCOMPARE(qvariant_cast<QObject*>(particles->property("motion")), motionGravity);
120
121     QSignalSpy xattractorSpy(motionGravity, SIGNAL(xattractorChanged()));
122     QSignalSpy yattractorSpy(motionGravity, SIGNAL(yattractorChanged()));
123     QSignalSpy accelerationSpy(motionGravity, SIGNAL(accelerationChanged()));
124
125     QCOMPARE(motionGravity->property("xattractor").toDouble(), 0.0);
126     QCOMPARE(motionGravity->property("yattractor").toDouble(), 1000.0);
127     QCOMPARE(motionGravity->property("acceleration").toDouble(), 25.0);
128
129     motionGravity->setProperty("xattractor", 20.0);
130     motionGravity->setProperty("yattractor", 10.0);
131     motionGravity->setProperty("acceleration", 10.0);
132
133     QCOMPARE(motionGravity->property("xattractor").toDouble(), 20.0);
134     QCOMPARE(motionGravity->property("yattractor").toDouble(), 10.0);
135     QCOMPARE(motionGravity->property("acceleration").toDouble(), 10.0);
136
137     QCOMPARE(xattractorSpy.count(), 1);
138     QCOMPARE(yattractorSpy.count(), 1);
139     QCOMPARE(accelerationSpy.count(), 1);
140
141     motionGravity->setProperty("xattractor", 20.0);
142     motionGravity->setProperty("yattractor", 10.0);
143     motionGravity->setProperty("acceleration", 10.0);
144
145     QCOMPARE(xattractorSpy.count(), 1);
146     QCOMPARE(yattractorSpy.count(), 1);
147     QCOMPARE(accelerationSpy.count(), 1);
148 }
149
150 void tst_QDeclarativeParticles::motionWander()
151 {
152     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
153     QVERIFY(canvas->rootObject());
154
155     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
156     QVERIFY(particles);
157
158     QSignalSpy motionSpy(particles, SIGNAL(motionChanged()));
159     QObject* motionWander = canvas->rootObject()->findChild<QObject*>("motionWander");
160
161     QCOMPARE(motionSpy.count(), 0);
162     particles->setProperty("motion", QVariant::fromValue(motionWander));
163     //QCOMPARE(particles->property("motion"), QVariant::fromValue(motionWander));
164     //QCOMPARE(motionSpy.count(), 1);
165
166     particles->setProperty("motion", QVariant::fromValue(motionWander));
167     //QCOMPARE(motionSpy.count(), 1);
168
169     QSignalSpy xvarianceSpy(motionWander, SIGNAL(xvarianceChanged()));
170     QSignalSpy yvarianceSpy(motionWander, SIGNAL(yvarianceChanged()));
171     QSignalSpy paceSpy(motionWander, SIGNAL(paceChanged()));
172
173     QCOMPARE(motionWander->property("xvariance").toDouble(), 30.0);
174     QCOMPARE(motionWander->property("yvariance").toDouble(), 30.0);
175     QCOMPARE(motionWander->property("pace").toDouble(), 100.0);
176
177     motionWander->setProperty("xvariance", 20.0);
178     motionWander->setProperty("yvariance", 10.0);
179     motionWander->setProperty("pace", 10.0);
180
181     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
182     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
183     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
184
185     QCOMPARE(xvarianceSpy.count(), 1);
186     QCOMPARE(yvarianceSpy.count(), 1);
187     QCOMPARE(paceSpy.count(), 1);
188
189     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
190     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
191     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
192
193     QCOMPARE(xvarianceSpy.count(), 1);
194     QCOMPARE(yvarianceSpy.count(), 1);
195     QCOMPARE(paceSpy.count(), 1);
196 }
197
198 void tst_QDeclarativeParticles::runs()
199 {
200     QDeclarativeView *canvas = createView(SRCDIR "/data/particlestest.qml");
201     QVERIFY(canvas->rootObject());
202
203     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
204     QVERIFY(particles);
205     QTest::qWait(1000);//Run for one second. Test passes if it doesn't crash.
206 }
207
208 QDeclarativeView *tst_QDeclarativeParticles::createView(const QString &filename)
209 {
210     QDeclarativeView *canvas = new QDeclarativeView(0);
211     canvas->setFixedSize(240,320);
212
213     canvas->setSource(QUrl::fromLocalFile(filename));
214
215     return canvas;
216 }
217 QTEST_MAIN(tst_QDeclarativeParticles)
218
219 #include "tst_qdeclarativeparticles.moc"