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