3209a8490b21bad71f630977fa695b1852c6fc18
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick1 / qdeclarativeanimations / tst_qdeclarativeanimations.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 <QtQuick1/qdeclarativeview.h>
45 #include <QtQuick1/private/qdeclarativerectangle_p.h>
46 #include <QtQuick1/private/qdeclarativeanimation_p.h>
47 #include <QtQuick1/private/qdeclarativeitem_p.h>
48 #include <QVariantAnimation>
49 #include <QEasingCurve>
50
51 class tst_qdeclarativeanimations : public QObject
52 {
53     Q_OBJECT
54 public:
55     tst_qdeclarativeanimations() {}
56
57 private slots:
58     void initTestCase() { QDeclarativeEngine engine; } // ensure types are registered
59
60     void simpleProperty();
61     void simpleNumber();
62     void simpleColor();
63     void simpleRotation();
64     void alwaysRunToEnd();
65     void complete();
66     void resume();
67     void dotProperty();
68     void badTypes();
69     void badProperties();
70     void mixedTypes();
71     void properties();
72     void propertiesTransition();
73     void invalidDuration();
74     void attached();
75     void propertyValueSourceDefaultStart();
76     void dontStart();
77     void easingProperties();
78     void rotation();
79     void runningTrueBug();
80     void nonTransitionBug();
81     void registrationBug();
82     void doubleRegistrationBug();
83     void alwaysRunToEndRestartBug();
84     void transitionAssignmentBug();
85     void pauseBug();
86 };
87
88 #define QTIMED_COMPARE(lhs, rhs) do { \
89     for (int ii = 0; ii < 5; ++ii) { \
90         if (lhs == rhs)  \
91             break; \
92         QTest::qWait(50); \
93     } \
94     QCOMPARE(lhs, rhs); \
95 } while (false)
96
97 void tst_qdeclarativeanimations::simpleProperty()
98 {
99     QDeclarative1Rectangle rect;
100     QDeclarative1PropertyAnimation animation;
101     animation.setTarget(&rect);
102     animation.setProperty("pos");
103     animation.setTo(QPointF(200,200));
104     QVERIFY(animation.target() == &rect);
105     QVERIFY(animation.property() == "pos");
106     QVERIFY(animation.to().toPointF() == QPointF(200,200));
107     animation.start();
108     QVERIFY(animation.isRunning());
109     QTest::qWait(animation.duration());
110     QTIMED_COMPARE(rect.pos(), QPointF(200,200));
111
112     rect.setPos(0,0);
113     animation.start();
114     animation.pause();
115     QVERIFY(animation.isRunning());
116     QVERIFY(animation.isPaused());
117     animation.setCurrentTime(125);
118     QVERIFY(animation.currentTime() == 125);
119     QCOMPARE(rect.pos(), QPointF(100,100));
120 }
121
122 void tst_qdeclarativeanimations::simpleNumber()
123 {
124     QDeclarative1Rectangle rect;
125     QDeclarative1NumberAnimation animation;
126     animation.setTarget(&rect);
127     animation.setProperty("x");
128     animation.setTo(200);
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     QTIMED_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_qdeclarativeanimations::simpleColor()
148 {
149     QDeclarative1Rectangle rect;
150     QDeclarative1ColorAnimation animation;
151     animation.setTarget(&rect);
152     animation.setProperty("color");
153     animation.setTo(QColor("red"));
154     QVERIFY(animation.target() == &rect);
155     QVERIFY(animation.property() == "color");
156     QVERIFY(animation.to() == QColor("red"));
157     animation.start();
158     QVERIFY(animation.isRunning());
159     QTest::qWait(animation.duration());
160     QTIMED_COMPARE(rect.color(), QColor("red"));
161
162     rect.setColor(QColor("blue"));
163     animation.start();
164     animation.pause();
165     QVERIFY(animation.isRunning());
166     QVERIFY(animation.isPaused());
167     animation.setCurrentTime(125);
168     QVERIFY(animation.currentTime() == 125);
169     QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
170
171     rect.setColor(QColor("green"));
172     animation.setFrom(QColor("blue"));
173     QVERIFY(animation.from() == QColor("blue"));
174     animation.restart();
175     QCOMPARE(rect.color(), QColor("blue"));
176     QVERIFY(animation.isRunning());
177     animation.setCurrentTime(125);
178     QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
179 }
180
181 void tst_qdeclarativeanimations::simpleRotation()
182 {
183     QDeclarative1Rectangle rect;
184     QDeclarative1RotationAnimation animation;
185     animation.setTarget(&rect);
186     animation.setProperty("rotation");
187     animation.setTo(270);
188     QVERIFY(animation.target() == &rect);
189     QVERIFY(animation.property() == "rotation");
190     QVERIFY(animation.to() == 270);
191     QVERIFY(animation.direction() == QDeclarative1RotationAnimation::Numerical);
192     animation.start();
193     QVERIFY(animation.isRunning());
194     QTest::qWait(animation.duration());
195     QTIMED_COMPARE(rect.rotation(), qreal(270));
196
197     rect.setRotation(0);
198     animation.start();
199     animation.pause();
200     QVERIFY(animation.isRunning());
201     QVERIFY(animation.isPaused());
202     animation.setCurrentTime(125);
203     QVERIFY(animation.currentTime() == 125);
204     QCOMPARE(rect.rotation(), qreal(135));
205 }
206
207 void tst_qdeclarativeanimations::alwaysRunToEnd()
208 {
209     QDeclarative1Rectangle rect;
210     QDeclarative1PropertyAnimation animation;
211     animation.setTarget(&rect);
212     animation.setProperty("x");
213     animation.setTo(200);
214     animation.setDuration(1000);
215     animation.setLoops(-1);
216     animation.setAlwaysRunToEnd(true);
217     QVERIFY(animation.loops() == -1);
218     QVERIFY(animation.alwaysRunToEnd() == true);
219     animation.start();
220     QTest::qWait(1500);
221     animation.stop();
222     QVERIFY(rect.x() != qreal(200));
223     QTest::qWait(500);
224     QTIMED_COMPARE(rect.x(), qreal(200));
225 }
226
227 void tst_qdeclarativeanimations::complete()
228 {
229     QDeclarative1Rectangle rect;
230     QDeclarative1PropertyAnimation animation;
231     animation.setTarget(&rect);
232     animation.setProperty("x");
233     animation.setFrom(1);
234     animation.setTo(200);
235     animation.setDuration(500);
236     QVERIFY(animation.from() == 1);
237     animation.start();
238     QTest::qWait(50);
239     animation.stop();
240     QVERIFY(rect.x() != qreal(200));
241     animation.start();
242     QTest::qWait(50);
243     QVERIFY(animation.isRunning());
244     animation.complete();
245     QCOMPARE(rect.x(), qreal(200));
246 }
247
248 void tst_qdeclarativeanimations::resume()
249 {
250     QDeclarative1Rectangle rect;
251     QDeclarative1PropertyAnimation animation;
252     animation.setTarget(&rect);
253     animation.setProperty("x");
254     animation.setFrom(10);
255     animation.setTo(200);
256     animation.setDuration(1000);
257     QVERIFY(animation.from() == 10);
258
259     animation.start();
260     QTest::qWait(400);
261     animation.pause();
262     qreal x = rect.x();
263     QVERIFY(x != qreal(200) && x != qreal(10));
264     QVERIFY(animation.isRunning());
265     QVERIFY(animation.isPaused());
266
267     animation.resume();
268     QVERIFY(animation.isRunning());
269     QVERIFY(!animation.isPaused());
270     QTest::qWait(400);
271     animation.stop();
272     QVERIFY(rect.x() > x);
273 }
274
275 void tst_qdeclarativeanimations::dotProperty()
276 {
277     QDeclarativeEngine engine;
278     QString componentStr = "import QtQuick 1.0\nRectangle{ id: rect; NumberAnimation{ objectName: \"anim\"; target: rect; property: \"border.width\"; to: 10 }}";
279     QDeclarativeComponent animationComponent(&engine);
280     animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
281     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(animationComponent.create());
282     QVERIFY(rect);
283     QDeclarative1NumberAnimation *animation = rect->findChild<QDeclarative1NumberAnimation*>("anim");
284     QVERIFY(animation);
285
286     animation->start();
287     QTest::qWait(animation->duration()+50);
288     QTIMED_COMPARE(rect->border()->width(), 10);
289
290     rect->border()->setWidth(0);
291     animation->start();
292     animation->pause();
293     animation->setCurrentTime(125);
294     QVERIFY(animation->currentTime() == 125);
295     QCOMPARE(rect->border()->width(), 5);
296
297     delete rect;
298 }
299
300 void tst_qdeclarativeanimations::badTypes()
301 {
302     //don't crash
303     {
304         QDeclarativeView *view = new QDeclarativeView;
305         view->setSource(QUrl::fromLocalFile(SRCDIR "/data/badtype1.qml"));
306
307         qApp->processEvents();
308
309         delete view;
310     }
311
312     //make sure we get a compiler error
313     {
314         QDeclarativeEngine engine;
315         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype2.qml"));
316         QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
317         c.create();
318
319         QVERIFY(c.errors().count() == 1);
320         QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: number expected"));
321     }
322
323     //make sure we get a compiler error
324     {
325         QDeclarativeEngine engine;
326         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype3.qml"));
327         QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
328         c.create();
329
330         QVERIFY(c.errors().count() == 1);
331         QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected"));
332     }
333
334     //don't crash
335     {
336         QDeclarativeEngine engine;
337         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype4.qml"));
338         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
339         QVERIFY(rect);
340
341         QDeclarativeItemPrivate::get(rect)->setState("state1");
342         QTest::qWait(1000 + 50);
343         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("MyRect");
344         QVERIFY(myRect);
345         QCOMPARE(myRect->x(),qreal(200));
346     }
347 }
348
349 void tst_qdeclarativeanimations::badProperties()
350 {
351     //make sure we get a runtime error
352     {
353         QDeclarativeEngine engine;
354
355         QDeclarativeComponent c1(&engine, QUrl::fromLocalFile(SRCDIR "/data/badproperty1.qml"));
356         QByteArray message = QUrl::fromLocalFile(SRCDIR "/data/badproperty1.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate non-existent property \"border.colr\"";
357         QTest::ignoreMessage(QtWarningMsg, message);
358         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c1.create());
359         QVERIFY(rect);
360
361         QDeclarativeComponent c2(&engine, QUrl::fromLocalFile(SRCDIR "/data/badproperty2.qml"));
362         message = QUrl::fromLocalFile(SRCDIR "/data/badproperty2.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate read-only property \"border\"";
363         QTest::ignoreMessage(QtWarningMsg, message);
364         rect = qobject_cast<QDeclarative1Rectangle*>(c2.create());
365         QVERIFY(rect);
366
367         //### should we warn here are well?
368         //rect->setState("state1");
369     }
370 }
371
372 //test animating mixed types with property animation in a transition
373 //for example, int + real; color + real; etc
374 void tst_qdeclarativeanimations::mixedTypes()
375 {
376     //assumes border.width stays a real -- not real robust
377     {
378         QDeclarativeEngine engine;
379         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/mixedtype1.qml"));
380         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
381         QVERIFY(rect);
382
383         QDeclarativeItemPrivate::get(rect)->setState("state1");
384         QTest::qWait(500);
385         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("MyRect");
386         QVERIFY(myRect);
387
388         //rather inexact -- is there a better way?
389         QVERIFY(myRect->x() > 100 && myRect->x() < 200);
390         QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10);
391     }
392
393     {
394         QDeclarativeEngine engine;
395         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/mixedtype2.qml"));
396         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
397         QVERIFY(rect);
398
399         QDeclarativeItemPrivate::get(rect)->setState("state1");
400         QTest::qWait(500);
401         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("MyRect");
402         QVERIFY(myRect);
403
404         //rather inexact -- is there a better way?
405         QVERIFY(myRect->x() > 100 && myRect->x() < 200);
406         QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue"));
407     }
408 }
409
410 void tst_qdeclarativeanimations::properties()
411 {
412     const int waitDuration = 300;
413     {
414         QDeclarativeEngine engine;
415         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties.qml"));
416         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
417         QVERIFY(rect);
418
419         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
420         QVERIFY(myRect);
421         QTest::qWait(waitDuration);
422         QTIMED_COMPARE(myRect->x(),qreal(200));
423     }
424
425     {
426         QDeclarativeEngine engine;
427         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties2.qml"));
428         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
429         QVERIFY(rect);
430
431         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
432         QVERIFY(myRect);
433         QTest::qWait(waitDuration);
434         QTIMED_COMPARE(myRect->x(),qreal(200));
435     }
436
437     {
438         QDeclarativeEngine engine;
439         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties3.qml"));
440         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
441         QVERIFY(rect);
442
443         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
444         QVERIFY(myRect);
445         QTest::qWait(waitDuration);
446         QTIMED_COMPARE(myRect->x(),qreal(300));
447     }
448
449     {
450         QDeclarativeEngine engine;
451         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties4.qml"));
452         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
453         QVERIFY(rect);
454
455         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
456         QVERIFY(myRect);
457         QTest::qWait(waitDuration);
458         QTIMED_COMPARE(myRect->y(),qreal(200));
459         QTIMED_COMPARE(myRect->x(),qreal(100));
460     }
461
462     {
463         QDeclarativeEngine engine;
464         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties5.qml"));
465         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
466         QVERIFY(rect);
467
468         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
469         QVERIFY(myRect);
470         QTest::qWait(waitDuration);
471         QTIMED_COMPARE(myRect->x(),qreal(100));
472         QTIMED_COMPARE(myRect->y(),qreal(200));
473     }
474 }
475
476 void tst_qdeclarativeanimations::propertiesTransition()
477 {
478     const int waitDuration = 300;
479     {
480         QDeclarativeEngine engine;
481         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition.qml"));
482         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
483         QVERIFY(rect);
484
485         QDeclarativeItemPrivate::get(rect)->setState("moved");
486         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
487         QVERIFY(myRect);
488         QTest::qWait(waitDuration);
489         QTIMED_COMPARE(myRect->x(),qreal(200));
490     }
491
492     {
493         QDeclarativeEngine engine;
494         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition2.qml"));
495         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
496         QVERIFY(rect);
497
498         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
499         QVERIFY(myRect);
500         QDeclarativeItemPrivate::get(rect)->setState("moved");
501         QCOMPARE(myRect->x(),qreal(200));
502         QCOMPARE(myRect->y(),qreal(100));
503         QTest::qWait(waitDuration);
504         QTIMED_COMPARE(myRect->y(),qreal(200));
505     }
506
507     {
508         QDeclarativeEngine engine;
509         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition3.qml"));
510         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
511         QVERIFY(rect);
512
513         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
514         QVERIFY(myRect);
515         QDeclarativeItemPrivate::get(rect)->setState("moved");
516         QCOMPARE(myRect->x(),qreal(200));
517         QCOMPARE(myRect->y(),qreal(100));
518     }
519
520     {
521         QDeclarativeEngine engine;
522         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition4.qml"));
523         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
524         QVERIFY(rect);
525
526         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
527         QVERIFY(myRect);
528         QDeclarativeItemPrivate::get(rect)->setState("moved");
529         QCOMPARE(myRect->x(),qreal(100));
530         QTest::qWait(waitDuration);
531         QTIMED_COMPARE(myRect->x(),qreal(200));
532     }
533
534     {
535         QDeclarativeEngine engine;
536         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition5.qml"));
537         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
538         QVERIFY(rect);
539
540         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
541         QVERIFY(myRect);
542         QDeclarativeItemPrivate::get(rect)->setState("moved");
543         QCOMPARE(myRect->x(),qreal(100));
544         QTest::qWait(waitDuration);
545         QTIMED_COMPARE(myRect->x(),qreal(200));
546     }
547
548     /*{
549         QDeclarativeEngine engine;
550         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition6.qml"));
551         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
552         QVERIFY(rect);
553
554         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
555         QVERIFY(myRect);
556         QDeclarativeItemPrivate::get(rect)->setState("moved");
557         QCOMPARE(myRect->x(),qreal(100));
558         QTest::qWait(waitDuration);
559         QTIMED_COMPARE(myRect->x(),qreal(100));
560     }*/
561
562     {
563         QDeclarativeEngine engine;
564         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition7.qml"));
565         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
566         QVERIFY(rect);
567
568         QDeclarativeItemPrivate::get(rect)->setState("moved");
569         QDeclarative1Rectangle *myRect = rect->findChild<QDeclarative1Rectangle*>("TheRect");
570         QVERIFY(myRect);
571         QTest::qWait(waitDuration);
572         QTIMED_COMPARE(myRect->x(),qreal(200));
573     }
574
575 }
576
577 void tst_qdeclarativeanimations::invalidDuration()
578 {
579     QDeclarative1PropertyAnimation *animation = new QDeclarative1PropertyAnimation;
580     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PropertyAnimation: Cannot set a duration of < 0");
581     animation->setDuration(-1);
582     QCOMPARE(animation->duration(), 250);
583
584     QDeclarative1PauseAnimation *pauseAnimation = new QDeclarative1PauseAnimation;
585     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PauseAnimation: Cannot set a duration of < 0");
586     pauseAnimation->setDuration(-1);
587     QCOMPARE(pauseAnimation->duration(), 250);
588 }
589
590 void tst_qdeclarativeanimations::attached()
591 {
592     QDeclarativeEngine engine;
593
594     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/attached.qml"));
595     QTest::ignoreMessage(QtDebugMsg, "off");
596     QTest::ignoreMessage(QtDebugMsg, "on");
597     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
598     QVERIFY(rect);
599 }
600
601 void tst_qdeclarativeanimations::propertyValueSourceDefaultStart()
602 {
603     {
604         QDeclarativeEngine engine;
605
606         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource.qml"));
607
608         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
609         QVERIFY(rect);
610
611         QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
612         QVERIFY(myAnim);
613         QVERIFY(myAnim->isRunning());
614     }
615
616     {
617         QDeclarativeEngine engine;
618
619         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource2.qml"));
620
621         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
622         QVERIFY(rect);
623
624         QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
625         QVERIFY(myAnim);
626         QVERIFY(myAnim->isRunning() == false);
627     }
628
629     {
630         QDeclarativeEngine engine;
631
632         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontAutoStart.qml"));
633
634         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
635         QVERIFY(rect);
636
637         QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
638         QVERIFY(myAnim && myAnim->qtAnimation());
639         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
640     }
641 }
642
643
644 void tst_qdeclarativeanimations::dontStart()
645 {
646     {
647         QDeclarativeEngine engine;
648
649         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart.qml"));
650
651         QString warning = c.url().toString() + ":14:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
652         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
653         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
654         QVERIFY(rect);
655
656         QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
657         QVERIFY(myAnim && myAnim->qtAnimation());
658         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
659     }
660
661     {
662         QDeclarativeEngine engine;
663
664         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart2.qml"));
665
666         QString warning = c.url().toString() + ":15:17: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
667         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
668         QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
669         QVERIFY(rect);
670
671         QDeclarative1AbstractAnimation *myAnim = rect->findChild<QDeclarative1AbstractAnimation*>("MyAnim");
672         QVERIFY(myAnim && myAnim->qtAnimation());
673         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
674     }
675 }
676
677 void tst_qdeclarativeanimations::easingProperties()
678 {
679     {
680         QDeclarativeEngine engine;
681         QString componentStr = "import QtQuick 1.0\nNumberAnimation { easing.type: \"InOutQuad\" }";
682         QDeclarativeComponent animationComponent(&engine);
683         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
684         QDeclarative1PropertyAnimation *animObject = qobject_cast<QDeclarative1PropertyAnimation*>(animationComponent.create());
685
686         QVERIFY(animObject != 0);
687         QCOMPARE(animObject->easing().type(), QEasingCurve::InOutQuad);
688     }
689
690     {
691         QDeclarativeEngine engine;
692         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"OutBounce\"; easing.amplitude: 5.0 }";
693         QDeclarativeComponent animationComponent(&engine);
694         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
695         QDeclarative1PropertyAnimation *animObject = qobject_cast<QDeclarative1PropertyAnimation*>(animationComponent.create());
696
697         QVERIFY(animObject != 0);
698         QCOMPARE(animObject->easing().type(), QEasingCurve::OutBounce);
699         QCOMPARE(animObject->easing().amplitude(), 5.0);
700     }
701
702     {
703         QDeclarativeEngine engine;
704         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"OutElastic\"; easing.amplitude: 5.0; easing.period: 3.0}";
705         QDeclarativeComponent animationComponent(&engine);
706         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
707         QDeclarative1PropertyAnimation *animObject = qobject_cast<QDeclarative1PropertyAnimation*>(animationComponent.create());
708
709         QVERIFY(animObject != 0);
710         QCOMPARE(animObject->easing().type(), QEasingCurve::OutElastic);
711         QCOMPARE(animObject->easing().amplitude(), 5.0);
712         QCOMPARE(animObject->easing().period(), 3.0);
713     }
714
715     {
716         QDeclarativeEngine engine;
717         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"InOutBack\"; easing.overshoot: 2 }";
718         QDeclarativeComponent animationComponent(&engine);
719         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
720         QDeclarative1PropertyAnimation *animObject = qobject_cast<QDeclarative1PropertyAnimation*>(animationComponent.create());
721
722         QVERIFY(animObject != 0);
723         QCOMPARE(animObject->easing().type(), QEasingCurve::InOutBack);
724         QCOMPARE(animObject->easing().overshoot(), 2.0);
725     }
726 }
727
728 void tst_qdeclarativeanimations::rotation()
729 {
730     QDeclarativeEngine engine;
731     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/rotation.qml"));
732     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
733     QVERIFY(rect);
734
735     QDeclarative1Rectangle *rr = rect->findChild<QDeclarative1Rectangle*>("rr");
736     QDeclarative1Rectangle *rr2 = rect->findChild<QDeclarative1Rectangle*>("rr2");
737     QDeclarative1Rectangle *rr3 = rect->findChild<QDeclarative1Rectangle*>("rr3");
738     QDeclarative1Rectangle *rr4 = rect->findChild<QDeclarative1Rectangle*>("rr4");
739
740     QDeclarativeItemPrivate::get(rect)->setState("state1");
741     QTest::qWait(800);
742     qreal r1 = rr->rotation();
743     qreal r2 = rr2->rotation();
744     qreal r3 = rr3->rotation();
745     qreal r4 = rr4->rotation();
746
747     QVERIFY(r1 > qreal(0) && r1 < qreal(370));
748     QVERIFY(r2 > qreal(0) && r2 < qreal(370));
749     QVERIFY(r3 < qreal(0) && r3 > qreal(-350));
750     QVERIFY(r4 > qreal(0) && r4 < qreal(10));
751     QCOMPARE(r1,r2);
752     QVERIFY(r4 < r2);
753
754     QTest::qWait(800);
755     QTIMED_COMPARE(rr->rotation() + rr2->rotation() + rr3->rotation() + rr4->rotation(), qreal(370*4));
756 }
757
758 void tst_qdeclarativeanimations::runningTrueBug()
759 {
760     //ensure we start correctly when "running: true" is explicitly set
761     QDeclarativeEngine engine;
762     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/runningTrueBug.qml"));
763     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
764     QVERIFY(rect);
765
766     QDeclarative1Rectangle *cloud = rect->findChild<QDeclarative1Rectangle*>("cloud");
767     QVERIFY(cloud);
768     QTest::qWait(1000);
769     QVERIFY(cloud->x() > qreal(0));
770 }
771
772 //QTBUG-12805
773 void tst_qdeclarativeanimations::nonTransitionBug()
774 {
775     //tests that the animation values from the previous transition are properly cleared
776     //in the case where an animation in the transition doesn't match anything (but previously did)
777     QDeclarativeEngine engine;
778
779     QDeclarativeComponent c(&engine, SRCDIR "/data/nonTransitionBug.qml");
780     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
781     QVERIFY(rect != 0);
782     QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
783     QDeclarative1Rectangle *mover = rect->findChild<QDeclarative1Rectangle*>("mover");
784
785     mover->setX(100);
786     QCOMPARE(mover->x(), qreal(100));
787
788     rectPrivate->setState("left");
789     QTRY_COMPARE(mover->x(), qreal(0));
790
791     mover->setX(100);
792     QCOMPARE(mover->x(), qreal(100));
793
794     //make sure we don't try to animate back to 0
795     rectPrivate->setState("free");
796     QTest::qWait(300);
797     QCOMPARE(mover->x(), qreal(100));
798 }
799
800 //QTBUG-14042
801 void tst_qdeclarativeanimations::registrationBug()
802 {
803     QDeclarativeEngine engine;
804
805     QDeclarativeComponent c(&engine, SRCDIR "/data/registrationBug.qml");
806     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
807     QVERIFY(rect != 0);
808     QTRY_COMPARE(rect->property("value"), QVariant(int(100)));
809 }
810
811 void tst_qdeclarativeanimations::doubleRegistrationBug()
812 {
813     QDeclarativeEngine engine;
814
815     QDeclarativeComponent c(&engine, SRCDIR "/data/doubleRegistrationBug.qml");
816     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
817     QVERIFY(rect != 0);
818
819     QDeclarative1AbstractAnimation *anim = rect->findChild<QDeclarative1AbstractAnimation*>("animation");
820     QVERIFY(anim != 0);
821     QTRY_COMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Stopped);
822 }
823
824 //QTBUG-16736
825 void tst_qdeclarativeanimations::alwaysRunToEndRestartBug()
826 {
827     QDeclarative1Rectangle rect;
828     QDeclarative1PropertyAnimation animation;
829     animation.setTarget(&rect);
830     animation.setProperty("x");
831     animation.setTo(200);
832     animation.setDuration(1000);
833     animation.setLoops(-1);
834     animation.setAlwaysRunToEnd(true);
835     QVERIFY(animation.loops() == -1);
836     QVERIFY(animation.alwaysRunToEnd() == true);
837     animation.start();
838     animation.stop();
839     animation.start();
840     animation.stop();
841     QTest::qWait(500);
842     QVERIFY(rect.x() != qreal(200));
843     QTest::qWait(800);
844     QTIMED_COMPARE(rect.x(), qreal(200));
845     QCOMPARE(static_cast<QDeclarative1AbstractAnimation*>(&animation)->qtAnimation()->state(), QAbstractAnimation::Stopped);
846 }
847
848 //QTBUG-20227
849 void tst_qdeclarativeanimations::transitionAssignmentBug()
850 {
851     QDeclarativeEngine engine;
852
853     QDeclarativeComponent c(&engine, SRCDIR "/data/transitionAssignmentBug.qml");
854     QDeclarative1Rectangle *rect = qobject_cast<QDeclarative1Rectangle*>(c.create());
855     QVERIFY(rect != 0);
856
857     QCOMPARE(rect->property("nullObject").toBool(), false);
858 }
859
860 //QTBUG-13598
861 void tst_qdeclarativeanimations::pauseBug()
862 {
863     QDeclarativeEngine engine;
864
865     QDeclarativeComponent c(&engine, SRCDIR "/data/pauseBug.qml");
866     QDeclarative1AbstractAnimation *anim = qobject_cast<QDeclarative1AbstractAnimation*>(c.create());
867     QVERIFY(anim != 0);
868     QCOMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Paused);
869     QCOMPARE(anim->isPaused(), true);
870     QCOMPARE(anim->isRunning(), true);
871
872     delete anim;
873 }
874
875 QTEST_MAIN(tst_qdeclarativeanimations)
876
877 #include "tst_qdeclarativeanimations.moc"