1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
41 #include <QtTest/QtTest>
42 #include <QtDeclarative/qdeclarativeengine.h>
43 #include <QtDeclarative/qdeclarativecomponent.h>
44 #include <QtQuick/qquickview.h>
45 #include <QtQuick/private/qquickrectangle_p.h>
46 #include <QtQuick/private/qdeclarativeanimation_p.h>
47 #include <QtQuick/private/qdeclarativetransition_p.h>
48 #include <QtQuick/private/qquickanimation_p.h>
49 #include <QtQuick/private/qdeclarativepathinterpolator_p.h>
50 #include <QtQuick/private/qquickitem_p.h>
51 #include <QVariantAnimation>
52 #include <QEasingCurve>
57 #include "../../shared/util.h"
59 class tst_qdeclarativeanimations : public QObject
63 tst_qdeclarativeanimations() {}
66 void initTestCase() { QDeclarativeEngine engine; } // ensure types are registered
68 void simpleProperty();
71 void simpleRotation();
73 void pathInterpolator();
74 void pathInterpolatorBackwardJump();
75 void pathWithNoStart();
76 void alwaysRunToEnd();
84 void propertiesTransition();
85 void pathTransition();
86 void disabledTransition();
87 void invalidDuration();
89 void propertyValueSourceDefaultStart();
91 void easingProperties();
93 void runningTrueBug();
94 void nonTransitionBug();
95 void registrationBug();
96 void doubleRegistrationBug();
97 void alwaysRunToEndRestartBug();
98 void transitionAssignmentBug();
99 void pauseBindingBug();
103 #define QTIMED_COMPARE(lhs, rhs) do { \
104 for (int ii = 0; ii < 5; ++ii) { \
109 QCOMPARE(lhs, rhs); \
112 void tst_qdeclarativeanimations::simpleProperty()
114 QQuickRectangle rect;
115 QDeclarativePropertyAnimation animation;
116 animation.setTarget(&rect);
117 animation.setProperty("x");
118 animation.setTo(200);
119 QVERIFY(animation.target() == &rect);
120 QVERIFY(animation.property() == "x");
121 QVERIFY(animation.to().toReal() == 200.0);
123 QVERIFY(animation.isRunning());
124 QTest::qWait(animation.duration());
125 QTIMED_COMPARE(rect.x(), 200.0);
127 rect.setPos(QPointF(0,0));
130 QVERIFY(animation.isRunning());
131 QVERIFY(animation.isPaused());
132 animation.setCurrentTime(125);
133 QVERIFY(animation.currentTime() == 125);
134 QCOMPARE(rect.x(),100.0);
137 void tst_qdeclarativeanimations::simpleNumber()
139 QQuickRectangle rect;
140 QDeclarativeNumberAnimation animation;
141 animation.setTarget(&rect);
142 animation.setProperty("x");
143 animation.setTo(200);
144 QVERIFY(animation.target() == &rect);
145 QVERIFY(animation.property() == "x");
146 QVERIFY(animation.to() == 200);
148 QVERIFY(animation.isRunning());
149 QTest::qWait(animation.duration());
150 QTIMED_COMPARE(rect.x(), qreal(200));
155 QVERIFY(animation.isRunning());
156 QVERIFY(animation.isPaused());
157 animation.setCurrentTime(125);
158 QVERIFY(animation.currentTime() == 125);
159 QCOMPARE(rect.x(), qreal(100));
162 void tst_qdeclarativeanimations::simpleColor()
164 QQuickRectangle rect;
165 QDeclarativeColorAnimation animation;
166 animation.setTarget(&rect);
167 animation.setProperty("color");
168 animation.setTo(QColor("red"));
169 QVERIFY(animation.target() == &rect);
170 QVERIFY(animation.property() == "color");
171 QVERIFY(animation.to() == QColor("red"));
173 QVERIFY(animation.isRunning());
174 QTest::qWait(animation.duration());
175 QTIMED_COMPARE(rect.color(), QColor("red"));
177 rect.setColor(QColor("blue"));
180 QVERIFY(animation.isRunning());
181 QVERIFY(animation.isPaused());
182 animation.setCurrentTime(125);
183 QVERIFY(animation.currentTime() == 125);
184 QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
186 rect.setColor(QColor("green"));
187 animation.setFrom(QColor("blue"));
188 QVERIFY(animation.from() == QColor("blue"));
190 QCOMPARE(rect.color(), QColor("blue"));
191 QVERIFY(animation.isRunning());
192 animation.setCurrentTime(125);
193 QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
196 void tst_qdeclarativeanimations::simpleRotation()
198 QQuickRectangle rect;
199 QDeclarativeRotationAnimation animation;
200 animation.setTarget(&rect);
201 animation.setProperty("rotation");
202 animation.setTo(270);
203 QVERIFY(animation.target() == &rect);
204 QVERIFY(animation.property() == "rotation");
205 QVERIFY(animation.to() == 270);
206 QVERIFY(animation.direction() == QDeclarativeRotationAnimation::Numerical);
208 QVERIFY(animation.isRunning());
209 QTest::qWait(animation.duration());
210 QTIMED_COMPARE(rect.rotation(), qreal(270));
215 QVERIFY(animation.isRunning());
216 QVERIFY(animation.isPaused());
217 animation.setCurrentTime(125);
218 QVERIFY(animation.currentTime() == 125);
219 QCOMPARE(rect.rotation(), qreal(135));
222 void tst_qdeclarativeanimations::simplePath()
225 QDeclarativeEngine engine;
226 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathAnimation.qml")));
227 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
230 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
232 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
238 pathAnim->setCurrentTime(30);
239 QCOMPARE(redRect->x(), qreal(167));
240 QCOMPARE(redRect->y(), qreal(104));
242 pathAnim->setCurrentTime(100);
243 QCOMPARE(redRect->x(), qreal(300));
244 QCOMPARE(redRect->y(), qreal(300));
246 //verify animation runs to end
248 QCOMPARE(redRect->x(), qreal(50));
249 QCOMPARE(redRect->y(), qreal(50));
250 QTRY_COMPARE(redRect->x(), qreal(300));
251 QCOMPARE(redRect->y(), qreal(300));
253 pathAnim->setOrientation(QQuickPathAnimation::RightFirst);
254 QCOMPARE(pathAnim->orientation(), QQuickPathAnimation::RightFirst);
256 QTRY_VERIFY(redRect->rotation() != 0);
261 QDeclarativeEngine engine;
262 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathAnimation2.qml")));
263 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
266 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
268 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
271 QCOMPARE(pathAnim->orientation(), QQuickPathAnimation::RightFirst);
275 QCOMPARE(redRect->x(), qreal(50));
276 QCOMPARE(redRect->y(), qreal(50));
277 QCOMPARE(redRect->rotation(), qreal(-360));
279 pathAnim->setCurrentTime(50);
280 QCOMPARE(redRect->x(), qreal(175));
281 QCOMPARE(redRect->y(), qreal(175));
282 QCOMPARE(redRect->rotation(), qreal(-315));
284 pathAnim->setCurrentTime(100);
285 QCOMPARE(redRect->x(), qreal(300));
286 QCOMPARE(redRect->y(), qreal(300));
287 QCOMPARE(redRect->rotation(), qreal(0));
291 void tst_qdeclarativeanimations::pathInterpolator()
293 QDeclarativeEngine engine;
294 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathInterpolator.qml")));
295 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
296 QVERIFY(interpolator);
298 QCOMPARE(interpolator->progress(), qreal(0));
299 QCOMPARE(interpolator->x(), qreal(50));
300 QCOMPARE(interpolator->y(), qreal(50));
301 QCOMPARE(interpolator->angle(), qreal(0));
303 interpolator->setProgress(.5);
304 QCOMPARE(interpolator->progress(), qreal(.5));
305 QCOMPARE(interpolator->x(), qreal(175));
306 QCOMPARE(interpolator->y(), qreal(175));
307 QCOMPARE(interpolator->angle(), qreal(270));
309 interpolator->setProgress(1);
310 QCOMPARE(interpolator->progress(), qreal(1));
311 QCOMPARE(interpolator->x(), qreal(300));
312 QCOMPARE(interpolator->y(), qreal(300));
313 QCOMPARE(interpolator->angle(), qreal(0));
316 void tst_qdeclarativeanimations::pathInterpolatorBackwardJump()
318 QDeclarativeEngine engine;
319 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathInterpolatorBack.qml")));
320 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
321 QVERIFY(interpolator);
323 QCOMPARE(interpolator->progress(), qreal(0));
324 QCOMPARE(interpolator->x(), qreal(50));
325 QCOMPARE(interpolator->y(), qreal(50));
326 QCOMPARE(interpolator->angle(), qreal(270));
328 interpolator->setProgress(.5);
329 QCOMPARE(interpolator->progress(), qreal(.5));
330 QCOMPARE(interpolator->x(), qreal(100));
331 QCOMPARE(interpolator->y(), qreal(75));
332 QCOMPARE(interpolator->angle(), qreal(90));
334 interpolator->setProgress(1);
335 QCOMPARE(interpolator->progress(), qreal(1));
336 QCOMPARE(interpolator->x(), qreal(200));
337 QCOMPARE(interpolator->y(), qreal(50));
338 QCOMPARE(interpolator->angle(), qreal(0));
340 //make sure we don't get caught in infinite loop here
341 interpolator->setProgress(0);
342 QCOMPARE(interpolator->progress(), qreal(0));
343 QCOMPARE(interpolator->x(), qreal(50));
344 QCOMPARE(interpolator->y(), qreal(50));
345 QCOMPARE(interpolator->angle(), qreal(270));
348 void tst_qdeclarativeanimations::pathWithNoStart()
350 QDeclarativeEngine engine;
351 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathAnimationNoStart.qml")));
352 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
355 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
357 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
362 QCOMPARE(redRect->x(), qreal(50));
363 QCOMPARE(redRect->y(), qreal(50));
365 pathAnim->setCurrentTime(50);
366 QCOMPARE(redRect->x(), qreal(175));
367 QCOMPARE(redRect->y(), qreal(175));
369 pathAnim->setCurrentTime(100);
370 QCOMPARE(redRect->x(), qreal(300));
371 QCOMPARE(redRect->y(), qreal(300));
376 QCOMPARE(redRect->x(), qreal(100));
377 QCOMPARE(redRect->y(), qreal(100));
378 QTRY_COMPARE(redRect->x(), qreal(300));
379 QCOMPARE(redRect->y(), qreal(300));
382 void tst_qdeclarativeanimations::alwaysRunToEnd()
384 QQuickRectangle rect;
385 QDeclarativePropertyAnimation animation;
386 animation.setTarget(&rect);
387 animation.setProperty("x");
388 animation.setTo(200);
389 animation.setDuration(1000);
390 animation.setLoops(-1);
391 animation.setAlwaysRunToEnd(true);
392 QVERIFY(animation.loops() == -1);
393 QVERIFY(animation.alwaysRunToEnd() == true);
397 QVERIFY(rect.x() != qreal(200));
399 QTIMED_COMPARE(rect.x(), qreal(200));
402 void tst_qdeclarativeanimations::complete()
404 QQuickRectangle rect;
405 QDeclarativePropertyAnimation animation;
406 animation.setTarget(&rect);
407 animation.setProperty("x");
408 animation.setFrom(1);
409 animation.setTo(200);
410 animation.setDuration(500);
411 QVERIFY(animation.from() == 1);
415 QVERIFY(rect.x() != qreal(200));
418 QVERIFY(animation.isRunning());
419 animation.complete();
420 QCOMPARE(rect.x(), qreal(200));
423 void tst_qdeclarativeanimations::resume()
425 QQuickRectangle rect;
426 QDeclarativePropertyAnimation animation;
427 animation.setTarget(&rect);
428 animation.setProperty("x");
429 animation.setFrom(10);
430 animation.setTo(200);
431 animation.setDuration(1000);
432 QVERIFY(animation.from() == 10);
438 QVERIFY(x != qreal(200) && x != qreal(10));
439 QVERIFY(animation.isRunning());
440 QVERIFY(animation.isPaused());
443 QVERIFY(animation.isRunning());
444 QVERIFY(!animation.isPaused());
447 QVERIFY(rect.x() > x);
450 void tst_qdeclarativeanimations::dotProperty()
452 QQuickRectangle rect;
453 QDeclarativeNumberAnimation animation;
454 animation.setTarget(&rect);
455 animation.setProperty("border.width");
458 QTest::qWait(animation.duration()+50);
459 QTIMED_COMPARE(rect.border()->width(), 10.0);
461 rect.border()->setWidth(0);
464 animation.setCurrentTime(125);
465 QVERIFY(animation.currentTime() == 125);
466 QCOMPARE(rect.border()->width(), 5.0);
469 void tst_qdeclarativeanimations::badTypes()
473 QQuickView *view = new QQuickView;
474 view->setSource(QUrl::fromLocalFile(TESTDATA("badtype1.qml")));
476 qApp->processEvents();
481 //make sure we get a compiler error
483 QDeclarativeEngine engine;
484 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("badtype2.qml")));
485 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
488 QVERIFY(c.errors().count() == 1);
489 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: number expected"));
492 //make sure we get a compiler error
494 QDeclarativeEngine engine;
495 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("badtype3.qml")));
496 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
499 QVERIFY(c.errors().count() == 1);
500 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected"));
505 QDeclarativeEngine engine;
506 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("badtype4.qml")));
507 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
510 QQuickItemPrivate::get(rect)->setState("state1");
511 QTest::qWait(1000 + 50);
512 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
514 QCOMPARE(myRect->x(),qreal(200));
518 void tst_qdeclarativeanimations::badProperties()
520 //make sure we get a runtime error
522 QDeclarativeEngine engine;
524 QDeclarativeComponent c1(&engine, QUrl::fromLocalFile(TESTDATA("badproperty1.qml")));
525 QByteArray message = QUrl::fromLocalFile(TESTDATA("badproperty1.qml")).toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate non-existent property \"border.colr\"";
526 QTest::ignoreMessage(QtWarningMsg, message);
527 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c1.create());
530 QDeclarativeComponent c2(&engine, QUrl::fromLocalFile(TESTDATA("badproperty2.qml")));
531 message = QUrl::fromLocalFile(TESTDATA("badproperty2.qml")).toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate read-only property \"border\"";
532 QTest::ignoreMessage(QtWarningMsg, message);
533 rect = qobject_cast<QQuickRectangle*>(c2.create());
536 //### should we warn here are well?
537 //rect->setState("state1");
541 //test animating mixed types with property animation in a transition
542 //for example, int + real; color + real; etc
543 void tst_qdeclarativeanimations::mixedTypes()
545 //assumes border.width stays a real -- not real robust
547 QDeclarativeEngine engine;
548 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("mixedtype1.qml")));
549 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
552 QQuickItemPrivate::get(rect)->setState("state1");
554 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
557 //rather inexact -- is there a better way?
558 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
559 QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10);
563 QDeclarativeEngine engine;
564 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("mixedtype2.qml")));
565 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
568 QQuickItemPrivate::get(rect)->setState("state1");
570 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
573 //rather inexact -- is there a better way?
574 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
575 QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue"));
579 void tst_qdeclarativeanimations::properties()
581 const int waitDuration = 300;
583 QDeclarativeEngine engine;
584 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("properties.qml")));
585 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
588 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
590 QTest::qWait(waitDuration);
591 QTIMED_COMPARE(myRect->x(),qreal(200));
595 QDeclarativeEngine engine;
596 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("properties2.qml")));
597 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
600 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
602 QTest::qWait(waitDuration);
603 QTIMED_COMPARE(myRect->x(),qreal(200));
607 QDeclarativeEngine engine;
608 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("properties3.qml")));
609 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
612 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
614 QTest::qWait(waitDuration);
615 QTIMED_COMPARE(myRect->x(),qreal(300));
619 QDeclarativeEngine engine;
620 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("properties4.qml")));
621 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
624 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
626 QTest::qWait(waitDuration);
627 QTIMED_COMPARE(myRect->y(),qreal(200));
628 QTIMED_COMPARE(myRect->x(),qreal(100));
632 QDeclarativeEngine engine;
633 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("properties5.qml")));
634 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
637 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
639 QTest::qWait(waitDuration);
640 QTIMED_COMPARE(myRect->x(),qreal(100));
641 QTIMED_COMPARE(myRect->y(),qreal(200));
645 void tst_qdeclarativeanimations::propertiesTransition()
647 const int waitDuration = 300;
649 QDeclarativeEngine engine;
650 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition.qml")));
651 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
654 QQuickItemPrivate::get(rect)->setState("moved");
655 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
657 QTest::qWait(waitDuration);
658 QTIMED_COMPARE(myRect->x(),qreal(200));
662 QDeclarativeEngine engine;
663 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition2.qml")));
664 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
667 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
669 QQuickItemPrivate::get(rect)->setState("moved");
670 QCOMPARE(myRect->x(),qreal(200));
671 QCOMPARE(myRect->y(),qreal(100));
672 QTest::qWait(waitDuration);
673 QTIMED_COMPARE(myRect->y(),qreal(200));
677 QDeclarativeEngine engine;
678 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition3.qml")));
679 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
682 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
684 QQuickItemPrivate::get(rect)->setState("moved");
685 QCOMPARE(myRect->x(),qreal(200));
686 QCOMPARE(myRect->y(),qreal(100));
690 QDeclarativeEngine engine;
691 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition4.qml")));
692 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
695 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
697 QQuickItemPrivate::get(rect)->setState("moved");
698 QCOMPARE(myRect->x(),qreal(100));
699 QTest::qWait(waitDuration);
700 QTIMED_COMPARE(myRect->x(),qreal(200));
704 QDeclarativeEngine engine;
705 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition5.qml")));
706 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
709 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
711 QQuickItemPrivate::get(rect)->setState("moved");
712 QCOMPARE(myRect->x(),qreal(100));
713 QTest::qWait(waitDuration);
714 QTIMED_COMPARE(myRect->x(),qreal(200));
718 QDeclarativeEngine engine;
719 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition6.qml")));
720 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
723 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
725 QQuickItemPrivate::get(rect)->setState("moved");
726 QCOMPARE(myRect->x(),qreal(100));
727 QTest::qWait(waitDuration);
728 QTIMED_COMPARE(myRect->x(),qreal(100));
732 QDeclarativeEngine engine;
733 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("propertiesTransition7.qml")));
734 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
737 QQuickItemPrivate::get(rect)->setState("moved");
738 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
740 QTest::qWait(waitDuration);
741 QTIMED_COMPARE(myRect->x(),qreal(200));
746 void tst_qdeclarativeanimations::pathTransition()
748 QDeclarativeEngine engine;
749 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pathTransition.qml")));
750 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
753 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("redRect");
756 QQuickItemPrivate::get(rect)->setState("moved");
757 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
758 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(100)) && qFuzzyCompare(myRect->y(), qreal(700)));
761 QQuickItemPrivate::get(rect)->setState("");
762 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
763 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(500)) && qFuzzyCompare(myRect->y(), qreal(50)));
766 void tst_qdeclarativeanimations::disabledTransition()
768 QDeclarativeEngine engine;
769 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("disabledTransition.qml")));
770 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
773 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
776 QDeclarativeTransition *trans = rect->findChild<QDeclarativeTransition*>();
779 QCOMPARE(trans->enabled(), false);
781 QQuickItemPrivate::get(rect)->setState("moved");
782 QCOMPARE(myRect->x(),qreal(200));
784 trans->setEnabled(true);
786 QQuickItemPrivate::get(rect)->setState("");
787 QCOMPARE(myRect->x(),qreal(200));
789 QTIMED_COMPARE(myRect->x(),qreal(100));
792 void tst_qdeclarativeanimations::invalidDuration()
794 QDeclarativePropertyAnimation *animation = new QDeclarativePropertyAnimation;
795 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PropertyAnimation: Cannot set a duration of < 0");
796 animation->setDuration(-1);
797 QCOMPARE(animation->duration(), 250);
799 QDeclarativePauseAnimation *pauseAnimation = new QDeclarativePauseAnimation;
800 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PauseAnimation: Cannot set a duration of < 0");
801 pauseAnimation->setDuration(-1);
802 QCOMPARE(pauseAnimation->duration(), 250);
805 void tst_qdeclarativeanimations::attached()
807 QDeclarativeEngine engine;
809 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("attached.qml")));
810 QTest::ignoreMessage(QtDebugMsg, "off");
811 QTest::ignoreMessage(QtDebugMsg, "on");
812 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
816 void tst_qdeclarativeanimations::propertyValueSourceDefaultStart()
819 QDeclarativeEngine engine;
821 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("valuesource.qml")));
823 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
826 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
828 QVERIFY(myAnim->isRunning());
832 QDeclarativeEngine engine;
834 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("valuesource2.qml")));
836 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
839 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
841 QVERIFY(myAnim->isRunning() == false);
845 QDeclarativeEngine engine;
847 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("dontAutoStart.qml")));
849 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
852 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
853 QVERIFY(myAnim && myAnim->qtAnimation());
854 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
859 void tst_qdeclarativeanimations::dontStart()
862 QDeclarativeEngine engine;
864 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("dontStart.qml")));
866 QString warning = c.url().toString() + ":14:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
867 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
868 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
871 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
872 QVERIFY(myAnim && myAnim->qtAnimation());
873 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
877 QDeclarativeEngine engine;
879 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("dontStart2.qml")));
881 QString warning = c.url().toString() + ":15:17: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
882 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
883 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
886 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
887 QVERIFY(myAnim && myAnim->qtAnimation());
888 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
892 void tst_qdeclarativeanimations::easingProperties()
895 QDeclarativeEngine engine;
896 QString componentStr = "import QtQuick 2.0\nNumberAnimation { easing.type: \"InOutQuad\" }";
897 QDeclarativeComponent animationComponent(&engine);
898 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
899 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
901 QVERIFY(animObject != 0);
902 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutQuad);
906 QDeclarativeEngine engine;
907 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutBounce\"; easing.amplitude: 5.0 }";
908 QDeclarativeComponent animationComponent(&engine);
909 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
910 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
912 QVERIFY(animObject != 0);
913 QCOMPARE(animObject->easing().type(), QEasingCurve::OutBounce);
914 QCOMPARE(animObject->easing().amplitude(), 5.0);
918 QDeclarativeEngine engine;
919 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutElastic\"; easing.amplitude: 5.0; easing.period: 3.0}";
920 QDeclarativeComponent animationComponent(&engine);
921 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
922 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
924 QVERIFY(animObject != 0);
925 QCOMPARE(animObject->easing().type(), QEasingCurve::OutElastic);
926 QCOMPARE(animObject->easing().amplitude(), 5.0);
927 QCOMPARE(animObject->easing().period(), 3.0);
931 QDeclarativeEngine engine;
932 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"InOutBack\"; easing.overshoot: 2 }";
933 QDeclarativeComponent animationComponent(&engine);
934 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
935 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
937 QVERIFY(animObject != 0);
938 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutBack);
939 QCOMPARE(animObject->easing().overshoot(), 2.0);
943 QDeclarativeEngine engine;
944 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"Bezier\"; easing.bezierCurve: [0.5, 0.2, 0.13, 0.65, 1.0, 1.0] }";
945 QDeclarativeComponent animationComponent(&engine);
946 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
947 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
949 QVERIFY(animObject != 0);
950 QCOMPARE(animObject->easing().type(), QEasingCurve::BezierSpline);
951 QList<QPointF> points = animObject->easing().cubicBezierSpline();
952 QCOMPARE(points.count(), 3);
953 QCOMPARE(points.at(0), QPointF(0.5, 0.2));
954 QCOMPARE(points.at(1), QPointF(0.13, 0.65));
955 QCOMPARE(points.at(2), QPointF(1.0, 1.0));
959 void tst_qdeclarativeanimations::rotation()
961 QDeclarativeEngine engine;
962 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("rotation.qml")));
963 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
966 QQuickRectangle *rr = rect->findChild<QQuickRectangle*>("rr");
967 QQuickRectangle *rr2 = rect->findChild<QQuickRectangle*>("rr2");
968 QQuickRectangle *rr3 = rect->findChild<QQuickRectangle*>("rr3");
969 QQuickRectangle *rr4 = rect->findChild<QQuickRectangle*>("rr4");
971 QQuickItemPrivate::get(rect)->setState("state1");
973 qreal r1 = rr->rotation();
974 qreal r2 = rr2->rotation();
975 qreal r3 = rr3->rotation();
976 qreal r4 = rr4->rotation();
978 QVERIFY(r1 > qreal(0) && r1 < qreal(370));
979 QVERIFY(r2 > qreal(0) && r2 < qreal(370));
980 QVERIFY(r3 < qreal(0) && r3 > qreal(-350));
981 QVERIFY(r4 > qreal(0) && r4 < qreal(10));
986 QTIMED_COMPARE(rr->rotation() + rr2->rotation() + rr3->rotation() + rr4->rotation(), qreal(370*4));
989 void tst_qdeclarativeanimations::runningTrueBug()
991 //ensure we start correctly when "running: true" is explicitly set
992 QDeclarativeEngine engine;
993 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("runningTrueBug.qml")));
994 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
997 QQuickRectangle *cloud = rect->findChild<QQuickRectangle*>("cloud");
1000 QVERIFY(cloud->x() > qreal(0));
1004 void tst_qdeclarativeanimations::nonTransitionBug()
1006 //tests that the animation values from the previous transition are properly cleared
1007 //in the case where an animation in the transition doesn't match anything (but previously did)
1008 QDeclarativeEngine engine;
1010 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("nonTransitionBug.qml")));
1011 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1013 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
1014 QQuickRectangle *mover = rect->findChild<QQuickRectangle*>("mover");
1017 QCOMPARE(mover->x(), qreal(100));
1019 rectPrivate->setState("left");
1020 QTRY_COMPARE(mover->x(), qreal(0));
1023 QCOMPARE(mover->x(), qreal(100));
1025 //make sure we don't try to animate back to 0
1026 rectPrivate->setState("free");
1028 QCOMPARE(mover->x(), qreal(100));
1032 void tst_qdeclarativeanimations::registrationBug()
1034 QDeclarativeEngine engine;
1036 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("registrationBug.qml")));
1037 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1039 QTRY_COMPARE(rect->property("value"), QVariant(int(100)));
1042 void tst_qdeclarativeanimations::doubleRegistrationBug()
1044 QDeclarativeEngine engine;
1046 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("doubleRegistrationBug.qml")));
1047 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1050 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1052 QTRY_COMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Stopped);
1056 void tst_qdeclarativeanimations::alwaysRunToEndRestartBug()
1058 QQuickRectangle rect;
1059 QDeclarativePropertyAnimation animation;
1060 animation.setTarget(&rect);
1061 animation.setProperty("x");
1062 animation.setTo(200);
1063 animation.setDuration(1000);
1064 animation.setLoops(-1);
1065 animation.setAlwaysRunToEnd(true);
1066 QVERIFY(animation.loops() == -1);
1067 QVERIFY(animation.alwaysRunToEnd() == true);
1073 QVERIFY(rect.x() != qreal(200));
1075 QTIMED_COMPARE(rect.x(), qreal(200));
1076 QCOMPARE(static_cast<QDeclarativeAbstractAnimation*>(&animation)->qtAnimation()->state(), QAbstractAnimation::Stopped);
1080 void tst_qdeclarativeanimations::transitionAssignmentBug()
1082 QDeclarativeEngine engine;
1084 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("transitionAssignmentBug.qml")));
1085 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1088 QCOMPARE(rect->property("nullObject").toBool(), false);
1092 void tst_qdeclarativeanimations::pauseBindingBug()
1094 QDeclarativeEngine engine;
1096 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pauseBindingBug.qml")));
1097 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1099 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1100 QVERIFY(anim->qtAnimation()->state() == QAbstractAnimation::Paused);
1106 void tst_qdeclarativeanimations::pauseBug()
1108 QDeclarativeEngine engine;
1110 QDeclarativeComponent c(&engine, QUrl::fromLocalFile(TESTDATA("pauseBug.qml")));
1111 QDeclarativeAbstractAnimation *anim = qobject_cast<QDeclarativeAbstractAnimation*>(c.create());
1113 QCOMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Paused);
1114 QCOMPARE(anim->isPaused(), true);
1115 QCOMPARE(anim->isRunning(), true);
1120 QTEST_MAIN(tst_qdeclarativeanimations)
1122 #include "tst_qdeclarativeanimations.moc"