Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativesmoothedanimation / tst_qdeclarativesmoothedanimation.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 <qtest.h>
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <private/qdeclarativesmoothedanimation_p.h>
45 #include <private/qdeclarativerectangle_p.h>
46 #include <private/qdeclarativevaluetype_p.h>
47 #include "../../../shared/util.h"
48
49 #ifdef Q_OS_SYMBIAN
50 // In Symbian OS test data is located in applications private dir
51 #define SRCDIR "."
52 #endif
53
54 class tst_qdeclarativesmoothedanimation : public QObject
55 {
56     Q_OBJECT
57 public:
58     tst_qdeclarativesmoothedanimation();
59
60 private slots:
61     void defaultValues();
62     void values();
63     void disabled();
64     void simpleAnimation();
65     void valueSource();
66     void behavior();
67
68 private:
69     QDeclarativeEngine engine;
70 };
71
72 tst_qdeclarativesmoothedanimation::tst_qdeclarativesmoothedanimation()
73 {
74 }
75
76 void tst_qdeclarativesmoothedanimation::defaultValues()
77 {
78     QDeclarativeEngine engine;
79     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/smoothedanimation1.qml"));
80     QDeclarativeSmoothedAnimation *obj = qobject_cast<QDeclarativeSmoothedAnimation*>(c.create());
81
82     QVERIFY(obj != 0);
83
84     QCOMPARE(obj->to(), 0.);
85     QCOMPARE(obj->velocity(), 200.);
86     QCOMPARE(obj->duration(), -1);
87     QCOMPARE(obj->maximumEasingTime(), -1);
88     QCOMPARE(obj->reversingMode(), QDeclarativeSmoothedAnimation::Eased);
89
90     delete obj;
91 }
92
93 void tst_qdeclarativesmoothedanimation::values()
94 {
95     QDeclarativeEngine engine;
96     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/smoothedanimation2.qml"));
97     QDeclarativeSmoothedAnimation *obj = qobject_cast<QDeclarativeSmoothedAnimation*>(c.create());
98
99     QVERIFY(obj != 0);
100
101     QCOMPARE(obj->to(), 10.);
102     QCOMPARE(obj->velocity(), 200.);
103     QCOMPARE(obj->duration(), 300);
104     QCOMPARE(obj->maximumEasingTime(), -1);
105     QCOMPARE(obj->reversingMode(), QDeclarativeSmoothedAnimation::Immediate);
106
107     delete obj;
108 }
109
110 void tst_qdeclarativesmoothedanimation::disabled()
111 {
112     QDeclarativeEngine engine;
113     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/smoothedanimation3.qml"));
114     QDeclarativeSmoothedAnimation *obj = qobject_cast<QDeclarativeSmoothedAnimation*>(c.create());
115
116     QVERIFY(obj != 0);
117
118     QCOMPARE(obj->to(), 10.);
119     QCOMPARE(obj->velocity(), 250.);
120     QCOMPARE(obj->maximumEasingTime(), 150);
121     QCOMPARE(obj->reversingMode(), QDeclarativeSmoothedAnimation::Sync);
122
123     delete obj;
124 }
125
126 void tst_qdeclarativesmoothedanimation::simpleAnimation()
127 {
128     QDeclarativeRectangle rect;
129     QDeclarativeSmoothedAnimation animation;
130     animation.setTarget(&rect);
131     animation.setProperty("x");
132     animation.setTo(200);
133     animation.setDuration(250);
134     QVERIFY(animation.target() == &rect);
135     QVERIFY(animation.property() == "x");
136     QVERIFY(animation.to() == 200);
137     animation.start();
138     QVERIFY(animation.isRunning());
139     QTest::qWait(animation.duration());
140     QTRY_COMPARE(rect.x(), qreal(200));
141
142     rect.setX(0);
143     animation.start();
144     animation.pause();
145     QVERIFY(animation.isRunning());
146     QVERIFY(animation.isPaused());
147     animation.setCurrentTime(125);
148     QVERIFY(animation.currentTime() == 125);
149     QCOMPARE(rect.x(), qreal(100));
150 }
151
152 void tst_qdeclarativesmoothedanimation::valueSource()
153 {
154     QDeclarativeEngine engine;
155
156     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/smoothedanimationValueSource.qml"));
157
158     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
159     QVERIFY(rect);
160
161     QDeclarativeRectangle *theRect = rect->findChild<QDeclarativeRectangle*>("theRect");
162     QVERIFY(theRect);
163
164     QDeclarativeSmoothedAnimation *easeX = rect->findChild<QDeclarativeSmoothedAnimation*>("easeX");
165     QVERIFY(easeX);
166     QVERIFY(easeX->isRunning());
167
168     QDeclarativeSmoothedAnimation *easeY = rect->findChild<QDeclarativeSmoothedAnimation*>("easeY");
169     QVERIFY(easeY);
170     QVERIFY(easeY->isRunning());
171
172     // XXX get the proper duration
173     QTest::qWait(100);
174
175     QTRY_VERIFY(!easeX->isRunning());
176     QTRY_VERIFY(!easeY->isRunning());
177
178     QTRY_COMPARE(theRect->x(), qreal(200));
179     QTRY_COMPARE(theRect->y(), qreal(200));
180 }
181
182 void tst_qdeclarativesmoothedanimation::behavior()
183 {
184     QDeclarativeEngine engine;
185
186     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/smoothedanimationBehavior.qml"));
187
188     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
189     QVERIFY(rect);
190
191     QDeclarativeRectangle *theRect = rect->findChild<QDeclarativeRectangle*>("theRect");
192     QVERIFY(theRect);
193
194     QDeclarativeSmoothedAnimation *easeX = rect->findChild<QDeclarativeSmoothedAnimation*>("easeX");
195     QVERIFY(easeX);
196
197     QDeclarativeSmoothedAnimation *easeY = rect->findChild<QDeclarativeSmoothedAnimation*>("easeY");
198     QVERIFY(easeY);
199
200     // XXX get the proper duration
201     QTest::qWait(400);
202
203     QTRY_VERIFY(!easeX->isRunning());
204     QTRY_VERIFY(!easeY->isRunning());
205
206     QTRY_COMPARE(theRect->x(), qreal(200));
207     QTRY_COMPARE(theRect->y(), qreal(200));
208 }
209
210 QTEST_MAIN(tst_qdeclarativesmoothedanimation)
211
212 #include "tst_qdeclarativesmoothedanimation.moc"