1 /****************************************************************************
3 ** Copyright (C) 2012 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 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(270));
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()
325 QDeclarativeEngine engine;
326 QDeclarativeComponent c(&engine, testFileUrl("pathInterpolatorBack.qml"));
327 QDeclarativePathInterpolator *interpolator = qobject_cast<QDeclarativePathInterpolator*>(c.create());
328 QVERIFY(interpolator);
330 QCOMPARE(interpolator->progress(), qreal(0));
331 QCOMPARE(interpolator->x(), qreal(50));
332 QCOMPARE(interpolator->y(), qreal(50));
333 QCOMPARE(interpolator->angle(), qreal(270));
335 interpolator->setProgress(.5);
336 QCOMPARE(interpolator->progress(), qreal(.5));
337 QCOMPARE(interpolator->x(), qreal(100));
338 QCOMPARE(interpolator->y(), qreal(75));
339 QCOMPARE(interpolator->angle(), qreal(90));
341 interpolator->setProgress(1);
342 QCOMPARE(interpolator->progress(), qreal(1));
343 QCOMPARE(interpolator->x(), qreal(200));
344 QCOMPARE(interpolator->y(), qreal(50));
345 QCOMPARE(interpolator->angle(), qreal(0));
347 //make sure we don't get caught in infinite loop here
348 interpolator->setProgress(0);
349 QCOMPARE(interpolator->progress(), qreal(0));
350 QCOMPARE(interpolator->x(), qreal(50));
351 QCOMPARE(interpolator->y(), qreal(50));
352 QCOMPARE(interpolator->angle(), qreal(270));
355 void tst_qdeclarativeanimations::pathWithNoStart()
357 QDeclarativeEngine engine;
358 QDeclarativeComponent c(&engine, testFileUrl("pathAnimationNoStart.qml"));
359 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
362 QQuickRectangle *redRect = rect->findChild<QQuickRectangle*>();
364 QQuickPathAnimation *pathAnim = rect->findChild<QQuickPathAnimation*>();
369 QCOMPARE(redRect->x(), qreal(50));
370 QCOMPARE(redRect->y(), qreal(50));
372 pathAnim->setCurrentTime(50);
373 QCOMPARE(redRect->x(), qreal(175));
374 QCOMPARE(redRect->y(), qreal(175));
376 pathAnim->setCurrentTime(100);
377 QCOMPARE(redRect->x(), qreal(300));
378 QCOMPARE(redRect->y(), qreal(300));
383 QCOMPARE(redRect->x(), qreal(100));
384 QCOMPARE(redRect->y(), qreal(100));
385 QTRY_COMPARE(redRect->x(), qreal(300));
386 QCOMPARE(redRect->y(), qreal(300));
389 void tst_qdeclarativeanimations::alwaysRunToEnd()
391 QQuickRectangle rect;
392 QDeclarativePropertyAnimation animation;
393 animation.setTarget(&rect);
394 animation.setProperty("x");
395 animation.setTo(200);
396 animation.setDuration(1000);
397 animation.setLoops(-1);
398 animation.setAlwaysRunToEnd(true);
399 QVERIFY(animation.loops() == -1);
400 QVERIFY(animation.alwaysRunToEnd() == true);
404 QVERIFY(rect.x() != qreal(200));
406 QTIMED_COMPARE(rect.x(), qreal(200));
409 void tst_qdeclarativeanimations::complete()
411 QQuickRectangle rect;
412 QDeclarativePropertyAnimation animation;
413 animation.setTarget(&rect);
414 animation.setProperty("x");
415 animation.setFrom(1);
416 animation.setTo(200);
417 animation.setDuration(500);
418 QVERIFY(animation.from() == 1);
422 QVERIFY(rect.x() != qreal(200));
425 QVERIFY(animation.isRunning());
426 animation.complete();
427 QCOMPARE(rect.x(), qreal(200));
430 void tst_qdeclarativeanimations::resume()
432 QQuickRectangle rect;
433 QDeclarativePropertyAnimation animation;
434 animation.setTarget(&rect);
435 animation.setProperty("x");
436 animation.setFrom(10);
437 animation.setTo(200);
438 animation.setDuration(1000);
439 QVERIFY(animation.from() == 10);
445 QVERIFY(x != qreal(200) && x != qreal(10));
446 QVERIFY(animation.isRunning());
447 QVERIFY(animation.isPaused());
450 QVERIFY(animation.isRunning());
451 QVERIFY(!animation.isPaused());
454 QVERIFY(rect.x() > x);
457 void tst_qdeclarativeanimations::dotProperty()
459 QQuickRectangle rect;
460 QDeclarativeNumberAnimation animation;
461 animation.setTarget(&rect);
462 animation.setProperty("border.width");
465 QTest::qWait(animation.duration()+50);
466 QTIMED_COMPARE(rect.border()->width(), 10.0);
468 rect.border()->setWidth(0);
471 animation.setCurrentTime(125);
472 QVERIFY(animation.currentTime() == 125);
473 QCOMPARE(rect.border()->width(), 5.0);
476 void tst_qdeclarativeanimations::badTypes()
480 QQuickView *view = new QQuickView;
481 view->setSource(testFileUrl("badtype1.qml"));
483 qApp->processEvents();
488 //make sure we get a compiler error
490 QDeclarativeEngine engine;
491 QDeclarativeComponent c(&engine, testFileUrl("badtype2.qml"));
492 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
495 QVERIFY(c.errors().count() == 1);
496 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: number expected"));
499 //make sure we get a compiler error
501 QDeclarativeEngine engine;
502 QDeclarativeComponent c(&engine, testFileUrl("badtype3.qml"));
503 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
506 QVERIFY(c.errors().count() == 1);
507 QCOMPARE(c.errors().at(0).description(), QLatin1String("Invalid property assignment: color expected"));
512 QDeclarativeEngine engine;
513 QDeclarativeComponent c(&engine, testFileUrl("badtype4.qml"));
514 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
517 QQuickItemPrivate::get(rect)->setState("state1");
518 QTest::qWait(1000 + 50);
519 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
521 QCOMPARE(myRect->x(),qreal(200));
525 void tst_qdeclarativeanimations::badProperties()
527 //make sure we get a runtime error
529 QDeclarativeEngine engine;
531 QDeclarativeComponent c1(&engine, testFileUrl("badproperty1.qml"));
532 QByteArray message = testFileUrl("badproperty1.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate non-existent property \"border.colr\"";
533 QTest::ignoreMessage(QtWarningMsg, message);
534 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c1.create());
537 QDeclarativeComponent c2(&engine, testFileUrl("badproperty2.qml"));
538 message = testFileUrl("badproperty2.qml").toString().toUtf8() + ":18:9: QML ColorAnimation: Cannot animate read-only property \"border\"";
539 QTest::ignoreMessage(QtWarningMsg, message);
540 rect = qobject_cast<QQuickRectangle*>(c2.create());
543 //### should we warn here are well?
544 //rect->setState("state1");
548 //test animating mixed types with property animation in a transition
549 //for example, int + real; color + real; etc
550 void tst_qdeclarativeanimations::mixedTypes()
552 //assumes border.width stays a real -- not real robust
554 QDeclarativeEngine engine;
555 QDeclarativeComponent c(&engine, testFileUrl("mixedtype1.qml"));
556 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
559 QQuickItemPrivate::get(rect)->setState("state1");
561 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
564 //rather inexact -- is there a better way?
565 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
566 QVERIFY(myRect->border()->width() > 1 && myRect->border()->width() < 10);
570 QDeclarativeEngine engine;
571 QDeclarativeComponent c(&engine, testFileUrl("mixedtype2.qml"));
572 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
575 QQuickItemPrivate::get(rect)->setState("state1");
577 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("MyRect");
580 //rather inexact -- is there a better way?
581 QVERIFY(myRect->x() > 100 && myRect->x() < 200);
582 #if defined(UBUNTU_ONEIRIC) && defined(__x86_64__)
583 QEXPECT_FAIL("", "Fails on this platform - QTBUG-23385", Continue);
585 QVERIFY(myRect->color() != QColor("red") && myRect->color() != QColor("blue"));
589 void tst_qdeclarativeanimations::properties()
591 const int waitDuration = 300;
593 QDeclarativeEngine engine;
594 QDeclarativeComponent c(&engine, testFileUrl("properties.qml"));
595 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
598 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
600 QTest::qWait(waitDuration);
601 QTIMED_COMPARE(myRect->x(),qreal(200));
605 QDeclarativeEngine engine;
606 QDeclarativeComponent c(&engine, testFileUrl("properties2.qml"));
607 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
610 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
612 QTest::qWait(waitDuration);
613 QTIMED_COMPARE(myRect->x(),qreal(200));
617 QDeclarativeEngine engine;
618 QDeclarativeComponent c(&engine, testFileUrl("properties3.qml"));
619 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
622 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
624 QTest::qWait(waitDuration);
625 QTIMED_COMPARE(myRect->x(),qreal(300));
629 QDeclarativeEngine engine;
630 QDeclarativeComponent c(&engine, testFileUrl("properties4.qml"));
631 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
634 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
636 QTest::qWait(waitDuration);
637 QTIMED_COMPARE(myRect->y(),qreal(200));
638 QTIMED_COMPARE(myRect->x(),qreal(100));
642 QDeclarativeEngine engine;
643 QDeclarativeComponent c(&engine, testFileUrl("properties5.qml"));
644 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
647 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
649 QTest::qWait(waitDuration);
650 QTIMED_COMPARE(myRect->x(),qreal(100));
651 QTIMED_COMPARE(myRect->y(),qreal(200));
655 void tst_qdeclarativeanimations::propertiesTransition()
657 const int waitDuration = 300;
659 QDeclarativeEngine engine;
660 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition.qml"));
661 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
664 QQuickItemPrivate::get(rect)->setState("moved");
665 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
667 QTest::qWait(waitDuration);
668 QTIMED_COMPARE(myRect->x(),qreal(200));
672 QDeclarativeEngine engine;
673 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition2.qml"));
674 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
677 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
679 QQuickItemPrivate::get(rect)->setState("moved");
680 QCOMPARE(myRect->x(),qreal(200));
681 QCOMPARE(myRect->y(),qreal(100));
682 QTest::qWait(waitDuration);
683 QTIMED_COMPARE(myRect->y(),qreal(200));
687 QDeclarativeEngine engine;
688 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition3.qml"));
689 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
692 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
694 QQuickItemPrivate::get(rect)->setState("moved");
695 QCOMPARE(myRect->x(),qreal(200));
696 QCOMPARE(myRect->y(),qreal(100));
700 QDeclarativeEngine engine;
701 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition4.qml"));
702 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
705 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
707 QQuickItemPrivate::get(rect)->setState("moved");
708 QCOMPARE(myRect->x(),qreal(100));
709 QTest::qWait(waitDuration);
710 QTIMED_COMPARE(myRect->x(),qreal(200));
714 QDeclarativeEngine engine;
715 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition5.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(100));
723 QTest::qWait(waitDuration);
724 QTIMED_COMPARE(myRect->x(),qreal(200));
728 QDeclarativeEngine engine;
729 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition6.qml"));
730 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
733 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
735 QQuickItemPrivate::get(rect)->setState("moved");
736 QCOMPARE(myRect->x(),qreal(100));
737 QTest::qWait(waitDuration);
738 QTIMED_COMPARE(myRect->x(),qreal(100));
742 QDeclarativeEngine engine;
743 QDeclarativeComponent c(&engine, testFileUrl("propertiesTransition7.qml"));
744 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
747 QQuickItemPrivate::get(rect)->setState("moved");
748 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
750 QTest::qWait(waitDuration);
751 QTIMED_COMPARE(myRect->x(),qreal(200));
756 void tst_qdeclarativeanimations::pathTransition()
758 QDeclarativeEngine engine;
759 QDeclarativeComponent c(&engine, testFileUrl("pathTransition.qml"));
760 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
763 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("redRect");
766 QQuickItemPrivate::get(rect)->setState("moved");
767 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
768 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(100)) && qFuzzyCompare(myRect->y(), qreal(700)));
771 QQuickItemPrivate::get(rect)->setState("");
772 QTRY_VERIFY(myRect->x() < 500 && myRect->x() > 100 && myRect->y() > 50 && myRect->y() < 700 ); //animation started
773 QTRY_VERIFY(qFuzzyCompare(myRect->x(), qreal(500)) && qFuzzyCompare(myRect->y(), qreal(50)));
776 void tst_qdeclarativeanimations::disabledTransition()
778 QDeclarativeEngine engine;
779 QDeclarativeComponent c(&engine, testFileUrl("disabledTransition.qml"));
780 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
783 QQuickRectangle *myRect = rect->findChild<QQuickRectangle*>("TheRect");
786 QDeclarativeTransition *trans = rect->findChild<QDeclarativeTransition*>();
789 QCOMPARE(trans->enabled(), false);
791 QQuickItemPrivate::get(rect)->setState("moved");
792 QCOMPARE(myRect->x(),qreal(200));
794 trans->setEnabled(true);
796 QQuickItemPrivate::get(rect)->setState("");
797 QCOMPARE(myRect->x(),qreal(200));
799 QTIMED_COMPARE(myRect->x(),qreal(100));
802 void tst_qdeclarativeanimations::invalidDuration()
804 QDeclarativePropertyAnimation *animation = new QDeclarativePropertyAnimation;
805 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PropertyAnimation: Cannot set a duration of < 0");
806 animation->setDuration(-1);
807 QCOMPARE(animation->duration(), 250);
809 QDeclarativePauseAnimation *pauseAnimation = new QDeclarativePauseAnimation;
810 QTest::ignoreMessage(QtWarningMsg, "<Unknown File>: QML PauseAnimation: Cannot set a duration of < 0");
811 pauseAnimation->setDuration(-1);
812 QCOMPARE(pauseAnimation->duration(), 250);
815 void tst_qdeclarativeanimations::attached()
817 QDeclarativeEngine engine;
819 QDeclarativeComponent c(&engine, testFileUrl("attached.qml"));
820 QTest::ignoreMessage(QtDebugMsg, "off");
821 QTest::ignoreMessage(QtDebugMsg, "on");
822 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
826 void tst_qdeclarativeanimations::propertyValueSourceDefaultStart()
829 QDeclarativeEngine engine;
831 QDeclarativeComponent c(&engine, testFileUrl("valuesource.qml"));
833 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
836 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
838 QVERIFY(myAnim->isRunning());
842 QDeclarativeEngine engine;
844 QDeclarativeComponent c(&engine, testFileUrl("valuesource2.qml"));
846 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
849 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
851 QVERIFY(myAnim->isRunning() == false);
855 QDeclarativeEngine engine;
857 QDeclarativeComponent c(&engine, testFileUrl("dontAutoStart.qml"));
859 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
862 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
863 QVERIFY(myAnim && myAnim->qtAnimation());
864 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
869 void tst_qdeclarativeanimations::dontStart()
872 QDeclarativeEngine engine;
874 QDeclarativeComponent c(&engine, testFileUrl("dontStart.qml"));
876 QString warning = c.url().toString() + ":14:13: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
877 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
878 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
881 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
882 QVERIFY(myAnim && myAnim->qtAnimation());
883 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
887 QDeclarativeEngine engine;
889 QDeclarativeComponent c(&engine, testFileUrl("dontStart2.qml"));
891 QString warning = c.url().toString() + ":15:17: QML NumberAnimation: setRunning() cannot be used on non-root animation nodes.";
892 QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
893 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
896 QDeclarativeAbstractAnimation *myAnim = rect->findChild<QDeclarativeAbstractAnimation*>("MyAnim");
897 QVERIFY(myAnim && myAnim->qtAnimation());
898 QVERIFY(myAnim->qtAnimation()->state() == QAbstractAnimation::Stopped);
902 void tst_qdeclarativeanimations::easingProperties()
905 QDeclarativeEngine engine;
906 QString componentStr = "import QtQuick 2.0\nNumberAnimation { easing.type: \"InOutQuad\" }";
907 QDeclarativeComponent animationComponent(&engine);
908 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
909 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
911 QVERIFY(animObject != 0);
912 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutQuad);
916 QDeclarativeEngine engine;
917 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutBounce\"; easing.amplitude: 5.0 }";
918 QDeclarativeComponent animationComponent(&engine);
919 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
920 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
922 QVERIFY(animObject != 0);
923 QCOMPARE(animObject->easing().type(), QEasingCurve::OutBounce);
924 QCOMPARE(animObject->easing().amplitude(), 5.0);
928 QDeclarativeEngine engine;
929 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"OutElastic\"; easing.amplitude: 5.0; easing.period: 3.0}";
930 QDeclarativeComponent animationComponent(&engine);
931 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
932 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
934 QVERIFY(animObject != 0);
935 QCOMPARE(animObject->easing().type(), QEasingCurve::OutElastic);
936 QCOMPARE(animObject->easing().amplitude(), 5.0);
937 QCOMPARE(animObject->easing().period(), 3.0);
941 QDeclarativeEngine engine;
942 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"InOutBack\"; easing.overshoot: 2 }";
943 QDeclarativeComponent animationComponent(&engine);
944 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
945 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
947 QVERIFY(animObject != 0);
948 QCOMPARE(animObject->easing().type(), QEasingCurve::InOutBack);
949 QCOMPARE(animObject->easing().overshoot(), 2.0);
953 QDeclarativeEngine engine;
954 QString componentStr = "import QtQuick 2.0\nPropertyAnimation { easing.type: \"Bezier\"; easing.bezierCurve: [0.5, 0.2, 0.13, 0.65, 1.0, 1.0] }";
955 QDeclarativeComponent animationComponent(&engine);
956 animationComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
957 QDeclarativePropertyAnimation *animObject = qobject_cast<QDeclarativePropertyAnimation*>(animationComponent.create());
959 QVERIFY(animObject != 0);
960 QCOMPARE(animObject->easing().type(), QEasingCurve::BezierSpline);
961 QList<QPointF> points = animObject->easing().cubicBezierSpline();
962 QCOMPARE(points.count(), 3);
963 QCOMPARE(points.at(0), QPointF(0.5, 0.2));
964 QCOMPARE(points.at(1), QPointF(0.13, 0.65));
965 QCOMPARE(points.at(2), QPointF(1.0, 1.0));
969 void tst_qdeclarativeanimations::rotation()
971 QDeclarativeEngine engine;
972 QDeclarativeComponent c(&engine, testFileUrl("rotation.qml"));
973 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
976 QQuickRectangle *rr = rect->findChild<QQuickRectangle*>("rr");
977 QQuickRectangle *rr2 = rect->findChild<QQuickRectangle*>("rr2");
978 QQuickRectangle *rr3 = rect->findChild<QQuickRectangle*>("rr3");
979 QQuickRectangle *rr4 = rect->findChild<QQuickRectangle*>("rr4");
981 QQuickItemPrivate::get(rect)->setState("state1");
983 qreal r1 = rr->rotation();
984 qreal r2 = rr2->rotation();
985 qreal r3 = rr3->rotation();
986 qreal r4 = rr4->rotation();
988 QVERIFY(r1 > qreal(0) && r1 < qreal(370));
989 QVERIFY(r2 > qreal(0) && r2 < qreal(370));
990 QVERIFY(r3 < qreal(0) && r3 > qreal(-350));
991 QVERIFY(r4 > qreal(0) && r4 < qreal(10));
996 QTIMED_COMPARE(rr->rotation() + rr2->rotation() + rr3->rotation() + rr4->rotation(), qreal(370*4));
999 void tst_qdeclarativeanimations::runningTrueBug()
1001 //ensure we start correctly when "running: true" is explicitly set
1002 QDeclarativeEngine engine;
1003 QDeclarativeComponent c(&engine, testFileUrl("runningTrueBug.qml"));
1004 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1007 QQuickRectangle *cloud = rect->findChild<QQuickRectangle*>("cloud");
1010 QVERIFY(cloud->x() > qreal(0));
1014 void tst_qdeclarativeanimations::nonTransitionBug()
1016 //tests that the animation values from the previous transition are properly cleared
1017 //in the case where an animation in the transition doesn't match anything (but previously did)
1018 QDeclarativeEngine engine;
1020 QDeclarativeComponent c(&engine, testFileUrl("nonTransitionBug.qml"));
1021 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1023 QQuickItemPrivate *rectPrivate = QQuickItemPrivate::get(rect);
1024 QQuickRectangle *mover = rect->findChild<QQuickRectangle*>("mover");
1027 QCOMPARE(mover->x(), qreal(100));
1029 rectPrivate->setState("left");
1030 QTRY_COMPARE(mover->x(), qreal(0));
1033 QCOMPARE(mover->x(), qreal(100));
1035 //make sure we don't try to animate back to 0
1036 rectPrivate->setState("free");
1038 QCOMPARE(mover->x(), qreal(100));
1042 void tst_qdeclarativeanimations::registrationBug()
1044 QDeclarativeEngine engine;
1046 QDeclarativeComponent c(&engine, testFileUrl("registrationBug.qml"));
1047 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1049 QTRY_COMPARE(rect->property("value"), QVariant(int(100)));
1052 void tst_qdeclarativeanimations::doubleRegistrationBug()
1054 QDeclarativeEngine engine;
1056 QDeclarativeComponent c(&engine, testFileUrl("doubleRegistrationBug.qml"));
1057 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1060 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1062 QTRY_COMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Stopped);
1066 void tst_qdeclarativeanimations::alwaysRunToEndRestartBug()
1068 QQuickRectangle rect;
1069 QDeclarativePropertyAnimation animation;
1070 animation.setTarget(&rect);
1071 animation.setProperty("x");
1072 animation.setTo(200);
1073 animation.setDuration(1000);
1074 animation.setLoops(-1);
1075 animation.setAlwaysRunToEnd(true);
1076 QVERIFY(animation.loops() == -1);
1077 QVERIFY(animation.alwaysRunToEnd() == true);
1083 QVERIFY(rect.x() != qreal(200));
1085 QTIMED_COMPARE(rect.x(), qreal(200));
1086 QCOMPARE(static_cast<QDeclarativeAbstractAnimation*>(&animation)->qtAnimation()->state(), QAbstractAnimation::Stopped);
1090 void tst_qdeclarativeanimations::transitionAssignmentBug()
1092 QDeclarativeEngine engine;
1094 QDeclarativeComponent c(&engine, testFileUrl("transitionAssignmentBug.qml"));
1095 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1098 QCOMPARE(rect->property("nullObject").toBool(), false);
1102 void tst_qdeclarativeanimations::pauseBindingBug()
1104 QDeclarativeEngine engine;
1106 QDeclarativeComponent c(&engine, testFileUrl("pauseBindingBug.qml"));
1107 QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(c.create());
1109 QDeclarativeAbstractAnimation *anim = rect->findChild<QDeclarativeAbstractAnimation*>("animation");
1110 QVERIFY(anim->qtAnimation()->state() == QAbstractAnimation::Paused);
1116 void tst_qdeclarativeanimations::pauseBug()
1118 QDeclarativeEngine engine;
1120 QDeclarativeComponent c(&engine, testFileUrl("pauseBug.qml"));
1121 QDeclarativeAbstractAnimation *anim = qobject_cast<QDeclarativeAbstractAnimation*>(c.create());
1123 QCOMPARE(anim->qtAnimation()->state(), QAbstractAnimation::Paused);
1124 QCOMPARE(anim->isPaused(), true);
1125 QCOMPARE(anim->isRunning(), true);
1130 QTEST_MAIN(tst_qdeclarativeanimations)
1132 #include "tst_qdeclarativeanimations.moc"