c4230bb4925dceef17cea1bd09094e4ca3d2973f
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeparticles / tst_qdeclarativeparticles.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 class tst_QDeclarativeParticles : public QObject
47 {
48     Q_OBJECT
49 public:
50     tst_QDeclarativeParticles();
51
52 private slots:
53     void properties();
54     void motionGravity();
55     void motionWander();
56     void runs();
57 private:
58     QDeclarativeView *createView(const QString &filename);
59
60 };
61
62 tst_QDeclarativeParticles::tst_QDeclarativeParticles()
63 {
64 }
65
66 void tst_QDeclarativeParticles::properties()
67 {
68     QDeclarativeView *canvas = createView(SRCDIR "/data/particlestest.qml");
69     QVERIFY(canvas->rootObject());
70
71     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
72     QVERIFY(particles);
73
74     particles->setProperty("source", QUrl::fromLocalFile(SRCDIR "/data/particle.png"));
75     QCOMPARE(particles->property("source").toUrl(), QUrl::fromLocalFile(SRCDIR "/data/particle.png"));
76
77     particles->setProperty("lifeSpanDeviation", (1000));
78     QCOMPARE(particles->property("lifeSpanDeviation").toInt(), 1000);
79
80     particles->setProperty("fadeInDuration", 1000);
81     QCOMPARE(particles->property("fadeInDuration").toInt(), 1000);
82
83     particles->setProperty("fadeOutDuration", 1000);
84     QCOMPARE(particles->property("fadeOutDuration").toInt(), 1000);
85
86     particles->setProperty("angle", 100.0);
87     QCOMPARE(particles->property("angle").toDouble(), 100.0);
88
89     particles->setProperty("angleDeviation", 100.0);
90     QCOMPARE(particles->property("angleDeviation").toDouble(), 100.0);
91
92     particles->setProperty("velocity", 100.0);
93     QCOMPARE(particles->property("velocity").toDouble(), 100.0);
94
95     particles->setProperty("velocityDeviation", 100.0);
96     QCOMPARE(particles->property("velocityDeviation").toDouble(), 100.0);
97
98     particles->setProperty("emissionVariance", 0.5);
99     QCOMPARE(particles->property("emissionVariance").toDouble(),0.5);
100
101     particles->setProperty("emissionRate", 12);
102     QCOMPARE(particles->property("emissionRate").toInt(), 12);
103
104     delete canvas;
105 }
106
107 void tst_QDeclarativeParticles::motionGravity()
108 {
109     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
110     QVERIFY(canvas->rootObject());
111
112     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
113     QVERIFY(particles);
114
115     QObject* motionGravity = canvas->rootObject()->findChild<QObject*>("motionGravity");
116     //QCOMPARE(qvariant_cast<QObject*>(particles->property("motion")), motionGravity);
117
118     QSignalSpy xattractorSpy(motionGravity, SIGNAL(xattractorChanged()));
119     QSignalSpy yattractorSpy(motionGravity, SIGNAL(yattractorChanged()));
120     QSignalSpy accelerationSpy(motionGravity, SIGNAL(accelerationChanged()));
121
122     QCOMPARE(motionGravity->property("xattractor").toDouble(), 0.0);
123     QCOMPARE(motionGravity->property("yattractor").toDouble(), 1000.0);
124     QCOMPARE(motionGravity->property("acceleration").toDouble(), 25.0);
125
126     motionGravity->setProperty("xattractor", 20.0);
127     motionGravity->setProperty("yattractor", 10.0);
128     motionGravity->setProperty("acceleration", 10.0);
129
130     QCOMPARE(motionGravity->property("xattractor").toDouble(), 20.0);
131     QCOMPARE(motionGravity->property("yattractor").toDouble(), 10.0);
132     QCOMPARE(motionGravity->property("acceleration").toDouble(), 10.0);
133
134     QCOMPARE(xattractorSpy.count(), 1);
135     QCOMPARE(yattractorSpy.count(), 1);
136     QCOMPARE(accelerationSpy.count(), 1);
137
138     motionGravity->setProperty("xattractor", 20.0);
139     motionGravity->setProperty("yattractor", 10.0);
140     motionGravity->setProperty("acceleration", 10.0);
141
142     QCOMPARE(xattractorSpy.count(), 1);
143     QCOMPARE(yattractorSpy.count(), 1);
144     QCOMPARE(accelerationSpy.count(), 1);
145
146     delete canvas;
147 }
148
149 void tst_QDeclarativeParticles::motionWander()
150 {
151     QDeclarativeView *canvas = createView(SRCDIR "/data/particlemotiontest.qml");
152     QVERIFY(canvas->rootObject());
153
154     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
155     QVERIFY(particles);
156
157     QSignalSpy motionSpy(particles, SIGNAL(motionChanged()));
158     QObject* motionWander = canvas->rootObject()->findChild<QObject*>("motionWander");
159
160     QCOMPARE(motionSpy.count(), 0);
161     particles->setProperty("motion", QVariant::fromValue(motionWander));
162     //QCOMPARE(particles->property("motion"), QVariant::fromValue(motionWander));
163     //QCOMPARE(motionSpy.count(), 1);
164
165     particles->setProperty("motion", QVariant::fromValue(motionWander));
166     //QCOMPARE(motionSpy.count(), 1);
167
168     QSignalSpy xvarianceSpy(motionWander, SIGNAL(xvarianceChanged()));
169     QSignalSpy yvarianceSpy(motionWander, SIGNAL(yvarianceChanged()));
170     QSignalSpy paceSpy(motionWander, SIGNAL(paceChanged()));
171
172     QCOMPARE(motionWander->property("xvariance").toDouble(), 30.0);
173     QCOMPARE(motionWander->property("yvariance").toDouble(), 30.0);
174     QCOMPARE(motionWander->property("pace").toDouble(), 100.0);
175
176     motionWander->setProperty("xvariance", 20.0);
177     motionWander->setProperty("yvariance", 10.0);
178     motionWander->setProperty("pace", 10.0);
179
180     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
181     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
182     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
183
184     QCOMPARE(xvarianceSpy.count(), 1);
185     QCOMPARE(yvarianceSpy.count(), 1);
186     QCOMPARE(paceSpy.count(), 1);
187
188     QCOMPARE(motionWander->property("xvariance").toDouble(), 20.0);
189     QCOMPARE(motionWander->property("yvariance").toDouble(), 10.0);
190     QCOMPARE(motionWander->property("pace").toDouble(), 10.0);
191
192     QCOMPARE(xvarianceSpy.count(), 1);
193     QCOMPARE(yvarianceSpy.count(), 1);
194     QCOMPARE(paceSpy.count(), 1);
195
196     delete canvas;
197 }
198
199 void tst_QDeclarativeParticles::runs()
200 {
201     QDeclarativeView *canvas = createView(SRCDIR "/data/particlestest.qml");
202     QVERIFY(canvas->rootObject());
203
204     QObject* particles = canvas->rootObject()->findChild<QObject*>("particles");
205     QVERIFY(particles);
206     QTest::qWait(1000);//Run for one second. Test passes if it doesn't crash.
207
208     delete canvas;
209 }
210
211 QDeclarativeView *tst_QDeclarativeParticles::createView(const QString &filename)
212 {
213     QDeclarativeView *canvas = new QDeclarativeView(0);
214     canvas->setFixedSize(240,320);
215
216     canvas->setSource(QUrl::fromLocalFile(filename));
217
218     return canvas;
219 }
220 QTEST_MAIN(tst_QDeclarativeParticles)
221
222 #include "tst_qdeclarativeparticles.moc"