1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** 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 QDeclarativeDataTest
63 tst_qdeclarativeanimations() {}
68 QDeclarativeEngine engine; // ensure types are registered
69 QDeclarativeDataTest::initTestCase();
72 void simpleProperty();
75 void simpleRotation();
77 void pathInterpolator();
78 void pathInterpolatorBackwardJump();
79 void pathWithNoStart();
80 void alwaysRunToEnd();
88 void propertiesTransition();
89 void pathTransition();
90 void disabledTransition();
91 void invalidDuration();
93 void propertyValueSourceDefaultStart();
95 void easingProperties();
97 void runningTrueBug();
98 void nonTransitionBug();
99 void registrationBug();
100 void doubleRegistrationBug();
101 void alwaysRunToEndRestartBug();
102 void transitionAssignmentBug();
103 void pauseBindingBug();
107 #define QTIMED_COMPARE(lhs, rhs) do { \
108 for (int ii = 0; ii < 5; ++ii) { \
113 QCOMPARE(lhs, rhs); \
116 void tst_qdeclarativeanimations::simpleProperty()
118 QQuickRectangle rect;
119 QDeclarativePropertyAnimation animation;
120 animation.setTarget(&rect);
121 animation.setProperty("x");
122 animation.setTo(200);
123 QVERIFY(animation.target() == &rect);
124 QVERIFY(animation.property() == "x");
125 QVERIFY(animation.to().toReal() == 200.0);
127 QVERIFY(animation.isRunning());
128 QTest::qWait(animation.duration());
129 QTIMED_COMPARE(rect.x(), 200.0);
131 rect.setPos(QPointF(0,0));
134 QVERIFY(animation.isRunning());
135 QVERIFY(animation.isPaused());
136 animation.setCurrentTime(125);
137 QVERIFY(animation.currentTime() == 125);
138 QCOMPARE(rect.x(),100.0);
141 void tst_qdeclarativeanimations::simpleNumber()
143 QQuickRectangle rect;
144 QDeclarativeNumberAnimation animation;
145 animation.setTarget(&rect);
146 animation.setProperty("x");
147 animation.setTo(200);
148 QVERIFY(animation.target() == &rect);
149 QVERIFY(animation.property() == "x");
150 QVERIFY(animation.to() == 200);
152 QVERIFY(animation.isRunning());
153 QTest::qWait(animation.duration());
154 QTIMED_COMPARE(rect.x(), qreal(200));
159 QVERIFY(animation.isRunning());
160 QVERIFY(animation.isPaused());
161 animation.setCurrentTime(125);
162 QVERIFY(animation.currentTime() == 125);
163 QCOMPARE(rect.x(), qreal(100));
166 void tst_qdeclarativeanimations::simpleColor()
168 QQuickRectangle rect;
169 QDeclarativeColorAnimation animation;
170 animation.setTarget(&rect);
171 animation.setProperty("color");
172 animation.setTo(QColor("red"));
173 QVERIFY(animation.target() == &rect);
174 QVERIFY(animation.property() == "color");
175 QVERIFY(animation.to() == QColor("red"));
177 QVERIFY(animation.isRunning());
178 QTest::qWait(animation.duration());
179 QTIMED_COMPARE(rect.color(), QColor("red"));
181 rect.setColor(QColor("blue"));
184 QVERIFY(animation.isRunning());
185 QVERIFY(animation.isPaused());
186 animation.setCurrentTime(125);
187 QVERIFY(animation.currentTime() == 125);
188 #if defined(UBUNTU_ONEIRIC) && defined(__x86_64__)
189 QEXPECT_FAIL("", "Fails on this platform - QTBUG-23385", Abort);
191 QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
193 rect.setColor(QColor("green"));
194 animation.setFrom(QColor("blue"));
195 QVERIFY(animation.from() == QColor("blue"));
197 QCOMPARE(rect.color(), QColor("blue"));
198 QVERIFY(animation.isRunning());
199 animation.setCurrentTime(125);
200 QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
203 void tst_qdeclarativeanimations::simpleRotation()
205 QQuickRectangle rect;
206 QDeclarativeRotationAnimation animation;
207 animation.setTarget(&rect);
208 animation.setProperty("rotation");
209 animation.setTo(270);
210 QVERIFY(animation.target() == &rect);
211 QVERIFY(animation.property() == "rotation");
212 QVERIFY(animation.to() == 270);
213 QVERIFY(animation.direction() == QDeclarativeRotationAnimation::Numerical);
215 QVERIFY(animation.isRunning());
216 QTest::qWait(animation.duration());
217 QTIMED_COMPARE(rect.rotation(), qreal(270));
222 QVERIFY(animation.isRunning());
223 QVERIFY(animation.isPaused());
224 animation.setCurrentTime(125);
225 QVERIFY(animation.currentTime() == 125);
226 QCOMPARE(rect.rotation(), qreal(135));
229 void tst_qdeclarativeanimations::simplePath()
232 QDeclarativeEngine engine;
233 QDeclarativeComponent c(&engine, testFileUrl("pathAnimation.qml"));
234 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
237 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
239 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
245 pathAnim->setCurrentTime(30);
246 QCOMPARE(redRect->x(), qreal(167));
247 QCOMPARE(redRect->y(), qreal(104));
249 pathAnim->setCurrentTime(100);
250 QCOMPARE(redRect->x(), qreal(300));
251 QCOMPARE(redRect->y(), qreal(300));
253 //verify animation runs to end
255 QCOMPARE(redRect->x(), qreal(50));
256 QCOMPARE(redRect->y(), qreal(50));
257 QTRY_COMPARE(redRect->x(), qreal(300));
258 QCOMPARE(redRect->y(), qreal(300));
260 pathAnim->setOrientation(QQuickPathAnimation::RightFirst);
261 QCOMPARE(pathAnim->orientation(), QQuickPathAnimation::RightFirst);
263 QTRY_VERIFY(redRect->rotation() != 0);
268 QDeclarativeEngine engine;
269 QDeclarativeComponent c(&engine, testFileUrl("pathAnimation2.qml"));
270 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
273 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
275 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
278 QCOMPARE(pathAnim->orientation(), QQuickPathAnimation::RightFirst);
282 QCOMPARE(redRect->x(), qreal(50));
283 QCOMPARE(redRect->y(), qreal(50));
284 QCOMPARE(redRect->rotation(), qreal(-360));
286 pathAnim->setCurrentTime(50);
287 QCOMPARE(redRect->x(), qreal(175));
288 QCOMPARE(redRect->y(), qreal(175));
289 QCOMPARE(redRect->rotation(), qreal(-315));
291 pathAnim->setCurrentTime(100);
292 QCOMPARE(redRect->x(), qreal(300));
293 QCOMPARE(redRect->y(), qreal(300));
294 QCOMPARE(redRect->rotation(), qreal(0));
298 void tst_qdeclarativeanimations::pathInterpolator()
300 QDeclarativeEngine engine;
301 QDeclarativeComponent c(&engine, testFileUrl("pathInterpolator.qml"));
302 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
303 QVERIFY(interpolator);
305 QCOMPARE(interpolator->progress(), qreal(0));
306 QCOMPARE(interpolator->x(), qreal(50));
307 QCOMPARE(interpolator->y(), qreal(50));
308 QCOMPARE(interpolator->angle(), qreal(0));
310 interpolator->setProgress(.5);
311 QCOMPARE(interpolator->progress(), qreal(.5));
312 QCOMPARE(interpolator->x(), qreal(175));
313 QCOMPARE(interpolator->y(), qreal(175));
314 QCOMPARE(interpolator->angle(), qreal(90));
316 interpolator->setProgress(1);
317 QCOMPARE(interpolator->progress(), qreal(1));
318 QCOMPARE(interpolator->x(), qreal(300));
319 QCOMPARE(interpolator->y(), qreal(300));
320 QCOMPARE(interpolator->angle(), qreal(0));
323 void tst_qdeclarativeanimations::pathInterpolatorBackwardJump()
326 QDeclarativeEngine engine;
327 QDeclarativeComponent c(&engine, testFileUrl("pathInterpolatorBack.qml"));
328 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
329 QVERIFY(interpolator);
331 QCOMPARE(interpolator->progress(), qreal(0));
332 QCOMPARE(interpolator->x(), qreal(50));
333 QCOMPARE(interpolator->y(), qreal(50));
334 QCOMPARE(interpolator->angle(), qreal(90));
336 interpolator->setProgress(.5);
337 QCOMPARE(interpolator->progress(), qreal(.5));
338 QCOMPARE(interpolator->x(), qreal(100));
339 QCOMPARE(interpolator->y(), qreal(75));
340 QCOMPARE(interpolator->angle(), qreal(270));
342 interpolator->setProgress(1);
343 QCOMPARE(interpolator->progress(), qreal(1));
344 QCOMPARE(interpolator->x(), qreal(200));
345 QCOMPARE(interpolator->y(), qreal(50));
346 QCOMPARE(interpolator->angle(), qreal(0));
348 //make sure we don't get caught in infinite loop here
349 interpolator->setProgress(0);
350 QCOMPARE(interpolator->progress(), qreal(0));
351 QCOMPARE(interpolator->x(), qreal(50));
352 QCOMPARE(interpolator->y(), qreal(50));
353 QCOMPARE(interpolator->angle(), qreal(90));
357 QDeclarativeEngine engine;
358 QDeclarativeComponent c(&engine, testFileUrl("pathInterpolatorBack2.qml"));
359 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
360 QVERIFY(interpolator);
362 QCOMPARE(interpolator->progress(), qreal(0));
363 QCOMPARE(interpolator->x(), qreal(200));
364 QCOMPARE(interpolator->y(), qreal(280));
365 QCOMPARE(interpolator->angle(), qreal(180));
367 interpolator->setProgress(1);
368 QCOMPARE(interpolator->progress(), qreal(1));
369 QCOMPARE(interpolator->x(), qreal(0));
370 QCOMPARE(interpolator->y(), qreal(80));
371 QCOMPARE(interpolator->angle(), qreal(180));
373 //make sure we don't get caught in infinite loop here
374 interpolator->setProgress(0);
375 QCOMPARE(interpolator->progress(), qreal(0));
376 QCOMPARE(interpolator->x(), qreal(200));
377 QCOMPARE(interpolator->y(), qreal(280));
378 QCOMPARE(interpolator->angle(), qreal(180));
382 void tst_qdeclarativeanimations::pathWithNoStart()
384 QDeclarativeEngine engine;
385 QDeclarativeComponent c(&engine, testFileUrl("pathAnimationNoStart.qml"));
386 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
389 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
391 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
396 QCOMPARE(redRect->x(), qreal(50));
397 QCOMPARE(redRect->y(), qreal(50));
399 pathAnim->setCurrentTime(50);
400 QCOMPARE(redRect->x(), qreal(175));
401 QCOMPARE(redRect->y(), qreal(175));
403 pathAnim->setCurrentTime(100);
404 QCOMPARE(redRect->x(), qreal(300));
405 QCOMPARE(redRect->y(), qreal(300));
410 QCOMPARE(redRect->x(), qreal(100));
411 QCOMPARE(redRect->y(), qreal(100));
412 QTRY_COMPARE(redRect->x(), qreal(300));
413 QCOMPARE(redRect->y(), qreal(300));
416 void tst_qdeclarativeanimations::alwaysRunToEnd()
418 QQuickRectangle rect;
419 QDeclarativePropertyAnimation animation;
420 animation.setTarget(&rect);
421 animation.setProperty("x");
422 animation.setTo(200);
423 animation.setDuration(1000);
424 animation.setLoops(-1);
425 animation.setAlwaysRunToEnd(true);
426 QVERIFY(animation.loops() == -1);
427 QVERIFY(animation.alwaysRunToEnd() == true);
431 QVERIFY(rect.x() != qreal(200));
433 QTIMED_COMPARE(rect.x(), qreal(200));
436 void tst_qdeclarativeanimations::complete()
438 QQuickRectangle rect;
439 QDeclarativePropertyAnimation animation;
440 animation.setTarget(&rect);
441 animation.setProperty("x");
442 animation.setFrom(1);
443 animation.setTo(200);
444 animation.setDuration(500);
445 QVERIFY(animation.from() == 1);
449 QVERIFY(rect.x() != qreal(200));
452 QVERIFY(animation.isRunning());
453 animation.complete();
454 QCOMPARE(rect.x(), qreal(200));
457 void tst_qdeclarativeanimations::resume()
459 QQuickRectangle rect;
460 QDeclarativePropertyAnimation animation;
461 animation.setTarget(&rect);
462 animation.setProperty("x");
463 animation.setFrom(10);
464 animation.setTo(200);
465 animation.setDuration(1000);
466 QVERIFY(animation.from() == 10);
472 QVERIFY(x != qreal(200) && x != qreal(10));
473 QVERIFY(animation.isRunning());
474 QVERIFY(animation.isPaused());
477 QVERIFY(animation.isRunning());
478 QVERIFY(!animation.isPaused());
481 QVERIFY(rect.x() > x);
484 void tst_qdeclarativeanimations::dotProperty()
486 QQuickRectangle rect;
487 QDeclarativeNumberAnimation animation;
488 animation.setTarget(&rect);
489 animation.setProperty("border.width");
492 QTest::qWait(animation.duration()+50);
493 QTIMED_COMPARE(rect.border()->width(), 10.0);
495 rect.border()->setWidth(0);
498 animation.setCurrentTime(125);
499 QVERIFY(animation.currentTime() == 125);
500 QCOMPARE(rect.border()->width(), 5.0);
503 void tst_qdeclarativeanimations::badTypes()
507 QQuickView *view = new QQuickView;
508 view->setSource(testFileUrl("badtype1.qml"));
510 qApp->processEvents();
515 //make sure we get a compiler error
517 QDeclarativeEngine engine;
518 QDeclarativeComponent c(&engine, testFileUrl("badtype2.qml"));
519 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
522 QVERIFY(c.errors().count() == 1);
523 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: number expected"));
526 //make sure we get a compiler error
528 QDeclarativeEngine engine;
529 QDeclarativeComponent c(&engine, testFileUrl("badtype3.qml"));
530 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
533 QVERIFY(c.errors().count() == 1);
534 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected"));
539 QDeclarativeEngine engine;
540 QDeclarativeComponent c(&engine, testFileUrl("badtype4.qml"));
541 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
544 QQuickItemPrivate::get(rect)->setState("state1");
545 QTest::qWait(1000 + 50);
546 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
548 QCOMPARE(myRect->x(),qreal(200));
552 void tst_qdeclarativeanimations::badProperties()
554 //make sure we get a runtime error
556 QDeclarativeEngine engine;
558 QDeclarativeComponent c1(&engine, testFileUrl("badproperty1.qml"));
559 QByteArray message = testFileUrl("badproperty1.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate non-existent property \"border.colr\"";
560 QTest::ignoreMessage(QtWarningMsg, message);
561 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c1.create());
564 QDeclarativeComponent c2(&engine, testFileUrl("badproperty2.qml"));
565 message = testFileUrl("badproperty2.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate read-only property \"border\"";
566 QTest::ignoreMessage(QtWarningMsg, message);
567 rect = qobject_cast<QQuickRectangle*>(c2.create());
570 //### should we warn here are well?
571 //rect->setState("state1");
575 //test animating mixed types with property animation in a transition
576 //for example, int + real; color + real; etc
577 void tst_qdeclarativeanimations::mixedTypes()
579 //assumes border.width stays a real -- not real robust
581 QDeclarativeEngine engine;
582 QDeclarativeComponent c(&engine, testFileUrl("mixedtype1.qml"));
583 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
586 QQuickItemPrivate::get(rect)->setState("state1");
588 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
591 //rather inexact -- is there a better way?
592 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
593 QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10);
597 QDeclarativeEngine engine;
598 QDeclarativeComponent c(&engine, testFileUrl("mixedtype2.qml"));
599 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
602 QQuickItemPrivate::get(rect)->setState("state1");
604 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
607 //rather inexact -- is there a better way?
608 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
609 #if defined(UBUNTU_ONEIRIC) && defined(__x86_64__)
610 QEXPECT_FAIL("", "Fails on this platform - QTBUG-23385", Continue);
612 QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue"));
616 void tst_qdeclarativeanimations::properties()
618 const int waitDuration = 300;
620 QDeclarativeEngine engine;
621 QDeclarativeComponent c(&engine, testFileUrl("properties.qml"));
622 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
625 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
627 QTest::qWait(waitDuration);
628 QTIMED_COMPARE(myRect->x(),qreal(200));
632 QDeclarativeEngine engine;
633 QDeclarativeComponent c(&engine, testFileUrl("properties2.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(200));
644 QDeclarativeEngine engine;
645 QDeclarativeComponent c(&engine, testFileUrl("properties3.qml"));
646 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
649 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
651 QTest::qWait(waitDuration);
652 QTIMED_COMPARE(myRect->x(),qreal(300));
656 QDeclarativeEngine engine;
657 QDeclarativeComponent c(&engine, testFileUrl("properties4.qml"));
658 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
661 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
663 QTest::qWait(waitDuration);
664 QTIMED_COMPARE(myRect->y(),qreal(200));
665 QTIMED_COMPARE(myRect->x(),qreal(100));
669 QDeclarativeEngine engine;
670 QDeclarativeComponent c(&engine, testFileUrl("properties5.qml"));
671 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
674 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
676 QTest::qWait(waitDuration);
677 QTIMED_COMPARE(myRect->x(),qreal(100));
678 QTIMED_COMPARE(myRect->y(),qreal(200));
682 void tst_qdeclarativeanimations::propertiesTransition()
684 const int waitDuration = 300;
686 QDeclarativeEngine engine;
687 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition.qml"));
688 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
691 QQuickItemPrivate::get(rect)->setState("moved");
692 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
694 QTest::qWait(waitDuration);
695 QTIMED_COMPARE(myRect->x(),qreal(200));
699 QDeclarativeEngine engine;
700 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition2.qml"));
701 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
704 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
706 QQuickItemPrivate::get(rect)->setState("moved");
707 QCOMPARE(myRect->x(),qreal(200));
708 QCOMPARE(myRect->y(),qreal(100));
709 QTest::qWait(waitDuration);
710 QTIMED_COMPARE(myRect->y(),qreal(200));
714 QDeclarativeEngine engine;
715 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition3.qml"));
716 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
719 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
721 QQuickItemPrivate::get(rect)->setState("moved");
722 QCOMPARE(myRect->x(),qreal(200));
723 QCOMPARE(myRect->y(),qreal(100));
727 QDeclarativeEngine engine;
728 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition4.qml"));
729 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
732 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
734 QQuickItemPrivate::get(rect)->setState("moved");
735 QCOMPARE(myRect->x(),qreal(100));
736 QTest::qWait(waitDuration);
737 QTIMED_COMPARE(myRect->x(),qreal(200));
741 QDeclarativeEngine engine;
742 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition5.qml"));
743 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
746 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
748 QQuickItemPrivate::get(rect)->setState("moved");
749 QCOMPARE(myRect->x(),qreal(100));
750 QTest::qWait(waitDuration);
751 QTIMED_COMPARE(myRect->x(),qreal(200));
755 QDeclarativeEngine engine;
756 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition6.qml"));
757 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
760 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
762 QQuickItemPrivate::get(rect)->setState("moved");
763 QCOMPARE(myRect->x(),qreal(100));
764 QTest::qWait(waitDuration);
765 QTIMED_COMPARE(myRect->x(),qreal(100));
769 QDeclarativeEngine engine;
770 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition7.qml"));
771 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
774 QQuickItemPrivate::get(rect)->setState("moved");
775 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
777 QTest::qWait(waitDuration);
778 QTIMED_COMPARE(myRect->x(),qreal(200));
783 void tst_qdeclarativeanimations::pathTransition()
785 QDeclarativeEngine engine;
786 QDeclarativeComponent c(&engine, testFileUrl("pathTransition.qml"));
787 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
790 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("redRect");
793 QQuickItemPrivate::get(rect)->setState("moved");
794 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
795 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(100)) && qFuzzyCompare(myRect->y(), qreal(700)));
798 QQuickItemPrivate::get(rect)->setState("");
799 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
800 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(500)) && qFuzzyCompare(myRect->y(), qreal(50)));
803 void tst_qdeclarativeanimations::disabledTransition()
805 QDeclarativeEngine engine;
806 QDeclarativeComponent c(&engine, testFileUrl("disabledTransition.qml"));
807 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
810 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
813 QDeclarativeTransition *trans = rect->findChild<QDeclarativeTransition*>();
816 QCOMPARE(trans->enabled(), false);
818 QQuickItemPrivate::get(rect)->setState("moved");
819 QCOMPARE(myRect->x(),qreal(200));
821 trans->setEnabled(true);
823 QQuickItemPrivate::get(rect)->setState("");
824 QCOMPARE(myRect->x(),qreal(200));
826 QTIMED_COMPARE(myRect->x(),qreal(100));
829 void tst_qdeclarativeanimations::invalidDuration()
831 QDeclarativePropertyAnimation *animation = new QDeclarativePropertyAnimation;
832 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PropertyAnimation: Cannot set a duration of < 0");
833 animation->setDuration(-1);
834 QCOMPARE(animation->duration(), 250);
836 QDeclarativePauseAnimation *pauseAnimation = new QDeclarativePauseAnimation;
837 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PauseAnimation: Cannot set a duration of < 0");
838 pauseAnimation->setDuration(-1);
839 QCOMPARE(pauseAnimation->duration(), 250);
842 void tst_qdeclarativeanimations::attached()
844 QDeclarativeEngine engine;
846 QDeclarativeComponent c(&engine, testFileUrl("attached.qml"));
847 QTest::ignoreMessage(QtDebugMsg, "off");
848 QTest::ignoreMessage(QtDebugMsg, "on");
849 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
853 void tst_qdeclarativeanimations::propertyValueSourceDefaultStart()
856 QDeclarativeEngine engine;
858 QDeclarativeComponent c(&engine, testFileUrl("valuesource.qml"));
860 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
863 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
865 QVERIFY(myAnim->isRunning());
869 QDeclarativeEngine engine;
871 QDeclarativeComponent c(&engine, testFileUrl("valuesource2.qml"));
873 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
876 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
878 QVERIFY(myAnim->isRunning() == false);
882 QDeclarativeEngine engine;
884 QDeclarativeComponent c(&engine, testFileUrl("dontAutoStart.qml"));
886 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
889 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
890 QVERIFY(myAnim && myAnim->qtAnimation());
891 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
896 void tst_qdeclarativeanimations::dontStart()
899 QDeclarativeEngine engine;
901 QDeclarativeComponent c(&engine, testFileUrl("dontStart.qml"));
903 QString warning = c.url().toString() + ":14:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
904 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
905 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
908 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
909 QVERIFY(myAnim && myAnim->qtAnimation());
910 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
914 QDeclarativeEngine engine;
916 QDeclarativeComponent c(&engine, testFileUrl("dontStart2.qml"));
918 QString warning = c.url().toString() + ":15:17: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
919 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
920 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
923 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
924 QVERIFY(myAnim && myAnim->qtAnimation());
925 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
929 void tst_qdeclarativeanimations::easingProperties()
932 QDeclarativeEngine engine;
933 QString componentStr = "import QtQuick 2.0\nNumberAnimation { easing.type: \"InOutQuad\" }";
934 QDeclarativeComponent animationComponent(&engine);
935 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
936 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
938 QVERIFY(animObject != 0);
939 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutQuad);
943 QDeclarativeEngine engine;
944 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutBounce\"; easing.amplitude: 5.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::OutBounce);
951 QCOMPARE(animObject->easing().amplitude(), 5.0);
955 QDeclarativeEngine engine;
956 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutElastic\"; easing.amplitude: 5.0; easing.period: 3.0}";
957 QDeclarativeComponent animationComponent(&engine);
958 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
959 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
961 QVERIFY(animObject != 0);
962 QCOMPARE(animObject->easing().type(), QEasingCurve::OutElastic);
963 QCOMPARE(animObject->easing().amplitude(), 5.0);
964 QCOMPARE(animObject->easing().period(), 3.0);
968 QDeclarativeEngine engine;
969 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"InOutBack\"; easing.overshoot: 2 }";
970 QDeclarativeComponent animationComponent(&engine);
971 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
972 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
974 QVERIFY(animObject != 0);
975 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutBack);
976 QCOMPARE(animObject->easing().overshoot(), 2.0);
980 QDeclarativeEngine engine;
981 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"Bezier\"; easing.bezierCurve: [0.5, 0.2, 0.13, 0.65, 1.0, 1.0] }";
982 QDeclarativeComponent animationComponent(&engine);
983 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
984 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
986 QVERIFY(animObject != 0);
987 QCOMPARE(animObject->easing().type(), QEasingCurve::BezierSpline);
988 QList<QPointF> points = animObject->easing().cubicBezierSpline();
989 QCOMPARE(points.count(), 3);
990 QCOMPARE(points.at(0), QPointF(0.5, 0.2));
991 QCOMPARE(points.at(1), QPointF(0.13, 0.65));
992 QCOMPARE(points.at(2), QPointF(1.0, 1.0));
996 void tst_qdeclarativeanimations::rotation()
998 QDeclarativeEngine engine;
999 QDeclarativeComponent c(&engine, testFileUrl("rotation.qml"));
1000 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1003 QQuickRectangle *rr = rect->findChild<QQuickRectangle*>("rr");
1004 QQuickRectangle *rr2 = rect->findChild<QQuickRectangle*>("rr2");
1005 QQuickRectangle *rr3 = rect->findChild<QQuickRectangle*>("rr3");
1006 QQuickRectangle *rr4 = rect->findChild<QQuickRectangle*>("rr4");
1008 QQuickItemPrivate::get(rect)->setState("state1");
1010 qreal r1 = rr->rotation();
1011 qreal r2 = rr2->rotation();
1012 qreal r3 = rr3->rotation();
1013 qreal r4 = rr4->rotation();
1015 QVERIFY(r1 > qreal(0) && r1 < qreal(370));
1016 QVERIFY(r2 > qreal(0) && r2 < qreal(370));
1017 QVERIFY(r3 < qreal(0) && r3 > qreal(-350));
1018 QVERIFY(r4 > qreal(0) && r4 < qreal(10));
1023 QTIMED_COMPARE(rr->rotation() + rr2->rotation() + rr3->rotation() + rr4->rotation(), qreal(370*4));
1026 void tst_qdeclarativeanimations::runningTrueBug()
1028 //ensure we start correctly when "running: true" is explicitly set
1029 QDeclarativeEngine engine;
1030 QDeclarativeComponent c(&engine, testFileUrl("runningTrueBug.qml"));
1031 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1034 QQuickRectangle *cloud = rect->findChild<QQuickRectangle*>("cloud");
1037 QVERIFY(cloud->x() > qreal(0));
1041 void tst_qdeclarativeanimations::nonTransitionBug()
1043 //tests that the animation values from the previous transition are properly cleared
1044 //in the case where an animation in the transition doesn't match anything (but previously did)
1045 QDeclarativeEngine engine;
1047 QDeclarativeComponent c(&engine, testFileUrl("nonTransitionBug.qml"));
1048 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1050 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
1051 QQuickRectangle *mover = rect->findChild<QQuickRectangle*>("mover");
1054 QCOMPARE(mover->x(), qreal(100));
1056 rectPrivate->setState("left");
1057 QTRY_COMPARE(mover->x(), qreal(0));
1060 QCOMPARE(mover->x(), qreal(100));
1062 //make sure we don't try to animate back to 0
1063 rectPrivate->setState("free");
1065 QCOMPARE(mover->x(), qreal(100));
1069 void tst_qdeclarativeanimations::registrationBug()
1071 QDeclarativeEngine engine;
1073 QDeclarativeComponent c(&engine, testFileUrl("registrationBug.qml"));
1074 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1076 QTRY_COMPARE(rect->property("value"), QVariant(int(100)));
1079 void tst_qdeclarativeanimations::doubleRegistrationBug()
1081 QDeclarativeEngine engine;
1083 QDeclarativeComponent c(&engine, testFileUrl("doubleRegistrationBug.qml"));
1084 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1087 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1089 QTRY_COMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Stopped);
1093 void tst_qdeclarativeanimations::alwaysRunToEndRestartBug()
1095 QQuickRectangle rect;
1096 QDeclarativePropertyAnimation animation;
1097 animation.setTarget(&rect);
1098 animation.setProperty("x");
1099 animation.setTo(200);
1100 animation.setDuration(1000);
1101 animation.setLoops(-1);
1102 animation.setAlwaysRunToEnd(true);
1103 QVERIFY(animation.loops() == -1);
1104 QVERIFY(animation.alwaysRunToEnd() == true);
1110 QVERIFY(rect.x() != qreal(200));
1112 QTIMED_COMPARE(rect.x(), qreal(200));
1113 QCOMPARE(static_cast<QDeclarativeAbstractAnimation*>(&animation)->qtAnimation()->state(), QAbstractAnimation::Stopped);
1117 void tst_qdeclarativeanimations::transitionAssignmentBug()
1119 QDeclarativeEngine engine;
1121 QDeclarativeComponent c(&engine, testFileUrl("transitionAssignmentBug.qml"));
1122 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1125 QCOMPARE(rect->property("nullObject").toBool(), false);
1129 void tst_qdeclarativeanimations::pauseBindingBug()
1131 QDeclarativeEngine engine;
1133 QDeclarativeComponent c(&engine, testFileUrl("pauseBindingBug.qml"));
1134 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1136 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1137 QVERIFY(anim->qtAnimation()->state() == QAbstractAnimation::Paused);
1143 void tst_qdeclarativeanimations::pauseBug()
1145 QDeclarativeEngine engine;
1147 QDeclarativeComponent c(&engine, testFileUrl("pauseBug.qml"));
1148 QDeclarativeAbstractAnimation *anim = qobject_cast<QDeclarativeAbstractAnimation*>(c.create());
1150 QCOMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Paused);
1151 QCOMPARE(anim->isPaused(), true);
1152 QCOMPARE(anim->isRunning(), true);
1157 QTEST_MAIN(tst_qdeclarativeanimations)
1159 #include "tst_qdeclarativeanimations.moc"