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