Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / 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 ** 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 <QtDeclarative/qdeclarativeview.h>
45 #include <private/qdeclarativerectangle_p.h>
46 #include <private/qdeclarativeanimation_p.h>
47 #include <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     QDeclarativeRectangle rect;
105     QDeclarativePropertyAnimation 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     QDeclarativeRectangle rect;
130     QDeclarativeNumberAnimation 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     QDeclarativeRectangle rect;
155     QDeclarativeColorAnimation 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     QDeclarativeRectangle rect;
189     QDeclarativeRotationAnimation 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() == QDeclarativeRotationAnimation::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     QDeclarativeRectangle rect;
215     QDeclarativePropertyAnimation 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     QDeclarativeRectangle rect;
235     QDeclarativePropertyAnimation 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     QDeclarativeRectangle rect;
256     QDeclarativePropertyAnimation 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     QDeclarativeRectangle rect;
283     QDeclarativeNumberAnimation animation;
284     animation.setTarget(&rect);
285     animation.setProperty("border.width");
286     animation.setTo(10);
287     animation.start();
288     QTest::qWait(animation.duration()+50);
289     QTIMED_COMPARE(rect.border()->width(), 10);
290
291     rect.border()->setWidth(0);
292     animation.start();
293     animation.pause();
294     animation.setCurrentTime(125);
295     QVERIFY(animation.currentTime() == 125);
296     QCOMPARE(rect.border()->width(), 5);
297 }
298
299 void tst_qdeclarativeanimations::badTypes()
300 {
301     //don't crash
302     {
303         QDeclarativeView *view = new QDeclarativeView;
304         view->setSource(QUrl::fromLocalFile(SRCDIR "/data/badtype1.qml"));
305
306         qApp->processEvents();
307
308         delete view;
309     }
310
311     //make sure we get a compiler error
312     {
313         QDeclarativeEngine engine;
314         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype2.qml"));
315         QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
316         c.create();
317
318         QVERIFY(c.errors().count() == 1);
319         QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: number expected"));
320     }
321
322     //make sure we get a compiler error
323     {
324         QDeclarativeEngine engine;
325         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype3.qml"));
326         QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
327         c.create();
328
329         QVERIFY(c.errors().count() == 1);
330         QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected"));
331     }
332
333     //don't crash
334     {
335         QDeclarativeEngine engine;
336         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/badtype4.qml"));
337         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
338         QVERIFY(rect);
339
340         QDeclarativeItemPrivate::get(rect)->setState("state1");
341         QTest::qWait(1000 + 50);
342         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("MyRect");
343         QVERIFY(myRect);
344         QCOMPARE(myRect->x(),qreal(200));
345     }
346 }
347
348 void tst_qdeclarativeanimations::badProperties()
349 {
350     //make sure we get a runtime error
351     {
352         QDeclarativeEngine engine;
353
354         QDeclarativeComponent c1(&engine, QUrl::fromLocalFile(SRCDIR "/data/badproperty1.qml"));
355         QByteArray message = QUrl::fromLocalFile(SRCDIR "/data/badproperty1.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate non-existent property \"border.colr\"";
356         QTest::ignoreMessage(QtWarningMsg, message);
357         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c1.create());
358         QVERIFY(rect);
359
360         QDeclarativeComponent c2(&engine, QUrl::fromLocalFile(SRCDIR "/data/badproperty2.qml"));
361         message = QUrl::fromLocalFile(SRCDIR "/data/badproperty2.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate read-only property \"border\"";
362         QTest::ignoreMessage(QtWarningMsg, message);
363         rect = qobject_cast<QDeclarativeRectangle*>(c2.create());
364         QVERIFY(rect);
365
366         //### should we warn here are well?
367         //rect->setState("state1");
368     }
369 }
370
371 //test animating mixed types with property animation in a transition
372 //for example, int + real; color + real; etc
373 void tst_qdeclarativeanimations::mixedTypes()
374 {
375     //assumes border.width stays a real -- not real robust
376     {
377         QDeclarativeEngine engine;
378         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/mixedtype1.qml"));
379         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
380         QVERIFY(rect);
381
382         QDeclarativeItemPrivate::get(rect)->setState("state1");
383         QTest::qWait(500);
384         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("MyRect");
385         QVERIFY(myRect);
386
387         //rather inexact -- is there a better way?
388         QVERIFY(myRect->x() > 100 && myRect->x() < 200);
389         QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10);
390     }
391
392     {
393         QDeclarativeEngine engine;
394         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/mixedtype2.qml"));
395         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
396         QVERIFY(rect);
397
398         QDeclarativeItemPrivate::get(rect)->setState("state1");
399         QTest::qWait(500);
400         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("MyRect");
401         QVERIFY(myRect);
402
403         //rather inexact -- is there a better way?
404         QVERIFY(myRect->x() > 100 && myRect->x() < 200);
405         QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue"));
406     }
407 }
408
409 void tst_qdeclarativeanimations::properties()
410 {
411     const int waitDuration = 300;
412     {
413         QDeclarativeEngine engine;
414         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties.qml"));
415         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
416         QVERIFY(rect);
417
418         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
419         QVERIFY(myRect);
420         QTest::qWait(waitDuration);
421         QTIMED_COMPARE(myRect->x(),qreal(200));
422     }
423
424     {
425         QDeclarativeEngine engine;
426         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties2.qml"));
427         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
428         QVERIFY(rect);
429
430         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
431         QVERIFY(myRect);
432         QTest::qWait(waitDuration);
433         QTIMED_COMPARE(myRect->x(),qreal(200));
434     }
435
436     {
437         QDeclarativeEngine engine;
438         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties3.qml"));
439         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
440         QVERIFY(rect);
441
442         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
443         QVERIFY(myRect);
444         QTest::qWait(waitDuration);
445         QTIMED_COMPARE(myRect->x(),qreal(300));
446     }
447
448     {
449         QDeclarativeEngine engine;
450         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties4.qml"));
451         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
452         QVERIFY(rect);
453
454         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
455         QVERIFY(myRect);
456         QTest::qWait(waitDuration);
457         QTIMED_COMPARE(myRect->y(),qreal(200));
458         QTIMED_COMPARE(myRect->x(),qreal(100));
459     }
460
461     {
462         QDeclarativeEngine engine;
463         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/properties5.qml"));
464         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
465         QVERIFY(rect);
466
467         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
468         QVERIFY(myRect);
469         QTest::qWait(waitDuration);
470         QTIMED_COMPARE(myRect->x(),qreal(100));
471         QTIMED_COMPARE(myRect->y(),qreal(200));
472     }
473 }
474
475 void tst_qdeclarativeanimations::propertiesTransition()
476 {
477     const int waitDuration = 300;
478     {
479         QDeclarativeEngine engine;
480         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition.qml"));
481         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
482         QVERIFY(rect);
483
484         QDeclarativeItemPrivate::get(rect)->setState("moved");
485         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
486         QVERIFY(myRect);
487         QTest::qWait(waitDuration);
488         QTIMED_COMPARE(myRect->x(),qreal(200));
489     }
490
491     {
492         QDeclarativeEngine engine;
493         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition2.qml"));
494         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
495         QVERIFY(rect);
496
497         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
498         QVERIFY(myRect);
499         QDeclarativeItemPrivate::get(rect)->setState("moved");
500         QCOMPARE(myRect->x(),qreal(200));
501         QCOMPARE(myRect->y(),qreal(100));
502         QTest::qWait(waitDuration);
503         QTIMED_COMPARE(myRect->y(),qreal(200));
504     }
505
506     {
507         QDeclarativeEngine engine;
508         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition3.qml"));
509         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
510         QVERIFY(rect);
511
512         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
513         QVERIFY(myRect);
514         QDeclarativeItemPrivate::get(rect)->setState("moved");
515         QCOMPARE(myRect->x(),qreal(200));
516         QCOMPARE(myRect->y(),qreal(100));
517     }
518
519     {
520         QDeclarativeEngine engine;
521         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition4.qml"));
522         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
523         QVERIFY(rect);
524
525         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
526         QVERIFY(myRect);
527         QDeclarativeItemPrivate::get(rect)->setState("moved");
528         QCOMPARE(myRect->x(),qreal(100));
529         QTest::qWait(waitDuration);
530         QTIMED_COMPARE(myRect->x(),qreal(200));
531     }
532
533     {
534         QDeclarativeEngine engine;
535         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition5.qml"));
536         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
537         QVERIFY(rect);
538
539         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
540         QVERIFY(myRect);
541         QDeclarativeItemPrivate::get(rect)->setState("moved");
542         QCOMPARE(myRect->x(),qreal(100));
543         QTest::qWait(waitDuration);
544         QTIMED_COMPARE(myRect->x(),qreal(200));
545     }
546
547     /*{
548         QDeclarativeEngine engine;
549         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition6.qml"));
550         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
551         QVERIFY(rect);
552
553         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
554         QVERIFY(myRect);
555         QDeclarativeItemPrivate::get(rect)->setState("moved");
556         QCOMPARE(myRect->x(),qreal(100));
557         QTest::qWait(waitDuration);
558         QTIMED_COMPARE(myRect->x(),qreal(100));
559     }*/
560
561     {
562         QDeclarativeEngine engine;
563         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/propertiesTransition7.qml"));
564         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
565         QVERIFY(rect);
566
567         QDeclarativeItemPrivate::get(rect)->setState("moved");
568         QDeclarativeRectangle *myRect = rect->findChild<QDeclarativeRectangle*>("TheRect");
569         QVERIFY(myRect);
570         QTest::qWait(waitDuration);
571         QTIMED_COMPARE(myRect->x(),qreal(200));
572     }
573
574 }
575
576 void tst_qdeclarativeanimations::invalidDuration()
577 {
578     QDeclarativePropertyAnimation *animation = new QDeclarativePropertyAnimation;
579     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PropertyAnimation: Cannot set a duration of < 0");
580     animation->setDuration(-1);
581     QCOMPARE(animation->duration(), 250);
582
583     QDeclarativePauseAnimation *pauseAnimation = new QDeclarativePauseAnimation;
584     QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PauseAnimation: Cannot set a duration of < 0");
585     pauseAnimation->setDuration(-1);
586     QCOMPARE(pauseAnimation->duration(), 250);
587 }
588
589 void tst_qdeclarativeanimations::attached()
590 {
591     QDeclarativeEngine engine;
592
593     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/attached.qml"));
594     QTest::ignoreMessage(QtDebugMsg, "off");
595     QTest::ignoreMessage(QtDebugMsg, "on");
596     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
597     QVERIFY(rect);
598 }
599
600 void tst_qdeclarativeanimations::propertyValueSourceDefaultStart()
601 {
602     {
603         QDeclarativeEngine engine;
604
605         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource.qml"));
606
607         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
608         QVERIFY(rect);
609
610         QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
611         QVERIFY(myAnim);
612         QVERIFY(myAnim->isRunning());
613     }
614
615     {
616         QDeclarativeEngine engine;
617
618         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/valuesource2.qml"));
619
620         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
621         QVERIFY(rect);
622
623         QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
624         QVERIFY(myAnim);
625         QVERIFY(myAnim->isRunning() == false);
626     }
627
628     {
629         QDeclarativeEngine engine;
630
631         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontAutoStart.qml"));
632
633         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
634         QVERIFY(rect);
635
636         QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
637         QVERIFY(myAnim && myAnim->qtAnimation());
638         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
639     }
640 }
641
642
643 void tst_qdeclarativeanimations::dontStart()
644 {
645     {
646         QDeclarativeEngine engine;
647
648         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart.qml"));
649
650         QString warning = c.url().toString() + ":14:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
651         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
652         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
653         QVERIFY(rect);
654
655         QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
656         QVERIFY(myAnim && myAnim->qtAnimation());
657         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
658     }
659
660     {
661         QDeclarativeEngine engine;
662
663         QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/dontStart2.qml"));
664
665         QString warning = c.url().toString() + ":15:17: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
666         QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
667         QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
668         QVERIFY(rect);
669
670         QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
671         QVERIFY(myAnim && myAnim->qtAnimation());
672         QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
673     }
674 }
675
676 void tst_qdeclarativeanimations::easingProperties()
677 {
678     {
679         QDeclarativeEngine engine;
680         QString componentStr = "import QtQuick 1.0\nNumberAnimation { easing.type: \"InOutQuad\" }";
681         QDeclarativeComponent animationComponent(&engine);
682         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
683         QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
684
685         QVERIFY(animObject != 0);
686         QCOMPARE(animObject->easing().type(), QEasingCurve::InOutQuad);
687     }
688
689     {
690         QDeclarativeEngine engine;
691         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"OutBounce\"; easing.amplitude: 5.0 }";
692         QDeclarativeComponent animationComponent(&engine);
693         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
694         QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
695
696         QVERIFY(animObject != 0);
697         QCOMPARE(animObject->easing().type(), QEasingCurve::OutBounce);
698         QCOMPARE(animObject->easing().amplitude(), 5.0);
699     }
700
701     {
702         QDeclarativeEngine engine;
703         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"OutElastic\"; easing.amplitude: 5.0; easing.period: 3.0}";
704         QDeclarativeComponent animationComponent(&engine);
705         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
706         QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
707
708         QVERIFY(animObject != 0);
709         QCOMPARE(animObject->easing().type(), QEasingCurve::OutElastic);
710         QCOMPARE(animObject->easing().amplitude(), 5.0);
711         QCOMPARE(animObject->easing().period(), 3.0);
712     }
713
714     {
715         QDeclarativeEngine engine;
716         QString componentStr = "import QtQuick 1.0\nPropertyAnimation { easing.type: \"InOutBack\"; easing.overshoot: 2 }";
717         QDeclarativeComponent animationComponent(&engine);
718         animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
719         QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
720
721         QVERIFY(animObject != 0);
722         QCOMPARE(animObject->easing().type(), QEasingCurve::InOutBack);
723         QCOMPARE(animObject->easing().overshoot(), 2.0);
724     }
725 }
726
727 void tst_qdeclarativeanimations::rotation()
728 {
729     QDeclarativeEngine engine;
730     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/rotation.qml"));
731     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
732     QVERIFY(rect);
733
734     QDeclarativeRectangle *rr = rect->findChild<QDeclarativeRectangle*>("rr");
735     QDeclarativeRectangle *rr2 = rect->findChild<QDeclarativeRectangle*>("rr2");
736     QDeclarativeRectangle *rr3 = rect->findChild<QDeclarativeRectangle*>("rr3");
737     QDeclarativeRectangle *rr4 = rect->findChild<QDeclarativeRectangle*>("rr4");
738
739     QDeclarativeItemPrivate::get(rect)->setState("state1");
740     QTest::qWait(800);
741     qreal r1 = rr->rotation();
742     qreal r2 = rr2->rotation();
743     qreal r3 = rr3->rotation();
744     qreal r4 = rr4->rotation();
745
746     QVERIFY(r1 > qreal(0) && r1 < qreal(370));
747     QVERIFY(r2 > qreal(0) && r2 < qreal(370));
748     QVERIFY(r3 < qreal(0) && r3 > qreal(-350));
749     QVERIFY(r4 > qreal(0) && r4 < qreal(10));
750     QCOMPARE(r1,r2);
751     QVERIFY(r4 < r2);
752
753     QTest::qWait(800);
754     QTIMED_COMPARE(rr->rotation() + rr2->rotation() + rr3->rotation() + rr4->rotation(), qreal(370*4));
755 }
756
757 void tst_qdeclarativeanimations::runningTrueBug()
758 {
759     //ensure we start correctly when "running: true" is explicitly set
760     QDeclarativeEngine engine;
761     QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/runningTrueBug.qml"));
762     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
763     QVERIFY(rect);
764
765     QDeclarativeRectangle *cloud = rect->findChild<QDeclarativeRectangle*>("cloud");
766     QVERIFY(cloud);
767     QTest::qWait(1000);
768     QVERIFY(cloud->x() > qreal(0));
769 }
770
771 //QTBUG-12805
772 void tst_qdeclarativeanimations::nonTransitionBug()
773 {
774     //tests that the animation values from the previous transition are properly cleared
775     //in the case where an animation in the transition doesn't match anything (but previously did)
776     QDeclarativeEngine engine;
777
778     QDeclarativeComponent c(&engine, SRCDIR "/data/nonTransitionBug.qml");
779     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
780     QVERIFY(rect != 0);
781     QDeclarativeItemPrivate *rectPrivate = QDeclarativeItemPrivate::get(rect);
782     QDeclarativeRectangle *mover = rect->findChild<QDeclarativeRectangle*>("mover");
783
784     mover->setX(100);
785     QCOMPARE(mover->x(), qreal(100));
786
787     rectPrivate->setState("left");
788     QTRY_COMPARE(mover->x(), qreal(0));
789
790     mover->setX(100);
791     QCOMPARE(mover->x(), qreal(100));
792
793     //make sure we don't try to animate back to 0
794     rectPrivate->setState("free");
795     QTest::qWait(300);
796     QCOMPARE(mover->x(), qreal(100));
797 }
798
799 //QTBUG-14042
800 void tst_qdeclarativeanimations::registrationBug()
801 {
802     QDeclarativeEngine engine;
803
804     QDeclarativeComponent c(&engine, SRCDIR "/data/registrationBug.qml");
805     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
806     QVERIFY(rect != 0);
807     QTRY_COMPARE(rect->property("value"), QVariant(int(100)));
808 }
809
810 void tst_qdeclarativeanimations::doubleRegistrationBug()
811 {
812     QDeclarativeEngine engine;
813
814     QDeclarativeComponent c(&engine, SRCDIR "/data/doubleRegistrationBug.qml");
815     QDeclarativeRectangle *rect = qobject_cast<QDeclarativeRectangle*>(c.create());
816     QVERIFY(rect != 0);
817
818     QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
819     QVERIFY(anim != 0);
820     QTRY_COMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Stopped);
821 }
822
823 //QTBUG-16736
824 void tst_qdeclarativeanimations::alwaysRunToEndRestartBug()
825 {
826     QDeclarativeRectangle rect;
827     QDeclarativePropertyAnimation animation;
828     animation.setTarget(&rect);
829     animation.setProperty("x");
830     animation.setTo(200);
831     animation.setDuration(1000);
832     animation.setLoops(-1);
833     animation.setAlwaysRunToEnd(true);
834     QVERIFY(animation.loops() == -1);
835     QVERIFY(animation.alwaysRunToEnd() == true);
836     animation.start();
837     animation.stop();
838     animation.start();
839     animation.stop();
840     QTest::qWait(500);
841     QVERIFY(rect.x() != qreal(200));
842     QTest::qWait(800);
843     QTIMED_COMPARE(rect.x(), qreal(200));
844     QCOMPARE(static_cast<QDeclarativeAbstractAnimation*>(&animation)->qtAnimation()->state(), QAbstractAnimation::Stopped);
845 }
846
847 QTEST_MAIN(tst_qdeclarativeanimations)
848
849 #include "tst_qdeclarativeanimations.moc"