From 31d01e174c3a3a1bba2ec9bf40cdc22d4053d8ec Mon Sep 17 00:00:00 2001
From: Michael Brasser <michael.brasser@nokia.com>
Date: Tue, 17 Nov 2009 11:51:40 +1000
Subject: More animation autotests.

---
 src/declarative/util/qmlanimation.cpp              |  50 +++++-----
 src/declarative/util/qmlanimation_p.h              |   2 +-
 src/declarative/util/qmlanimation_p_p.h            |   2 +-
 .../auto/declarative/animations/tst_animations.cpp | 105 +++++++++++++++++++++
 4 files changed, 130 insertions(+), 29 deletions(-)

diff --git a/src/declarative/util/qmlanimation.cpp b/src/declarative/util/qmlanimation.cpp
index d78f0a1..780bc82 100644
--- a/src/declarative/util/qmlanimation.cpp
+++ b/src/declarative/util/qmlanimation.cpp
@@ -62,7 +62,7 @@
 
 QT_BEGIN_NAMESPACE
 
-QEasingCurve stringToCurve(const QString &curve)
+static QEasingCurve stringToCurve(const QString &curve, QObject *obj)
 {
     QEasingCurve easingCurve;
 
@@ -73,8 +73,7 @@ QEasingCurve stringToCurve(const QString &curve)
     if (hasParams) {
         QString easeName = curve.trimmed();
         if (!easeName.endsWith(QLatin1Char(')'))) {
-            qWarning("QEasingCurve: Unmatched perenthesis in easing function '%s'",
-                     qPrintable(curve));
+            qmlInfo(obj) << obj->tr("Unmatched parenthesis in easing function \"%1\"").arg(curve);
             return easingCurve;
         }
 
@@ -83,8 +82,8 @@ QEasingCurve stringToCurve(const QString &curve)
             easeName.mid(idx + 1, easeName.length() - 1 - idx - 1);
         normalizedCurve = easeName.left(idx);
         if (!normalizedCurve.startsWith(QLatin1String("ease"))) {
-            qWarning("QEasingCurve: Easing function '%s' must start with 'ease'",
-                     qPrintable(curve));
+            qmlInfo(obj) << obj->tr("Easing function \"%1\" must start with \"ease\"").arg(curve);
+            return easingCurve;
         }
 
         props = prop_str.split(QLatin1Char(','));
@@ -98,9 +97,8 @@ QEasingCurve stringToCurve(const QString &curve)
 
     int value = me.keyToValue(normalizedCurve.toUtf8().constData());
     if (value < 0) {
-        qWarning("QEasingCurve: Unknown easing curve '%s'",
-                 qPrintable(curve));
-        value = 0;
+        qmlInfo(obj) << obj->tr("Unknown easing curve \"%1\"").arg(curve);
+        return easingCurve;
     }
     easingCurve.setType((QEasingCurve::Type)value);
 
@@ -109,9 +107,8 @@ QEasingCurve stringToCurve(const QString &curve)
             int sep = str.indexOf(QLatin1Char(':'));
 
             if (sep == -1) {
-                qWarning("QEasingCurve: Improperly specified property in easing function '%s'",
-                         qPrintable(curve));
-                return easingCurve;
+                qmlInfo(obj) << obj->tr("Improperly specified parameter in easing function \"%1\"").arg(curve);
+                continue;
             }
 
             QString propName = str.left(sep).trimmed();
@@ -119,9 +116,8 @@ QEasingCurve stringToCurve(const QString &curve)
             qreal propValue = str.mid(sep + 1).trimmed().toDouble(&isOk);
 
             if (propName.isEmpty() || !isOk) {
-                qWarning("QEasingCurve: Improperly specified property in easing function '%s'",
-                         qPrintable(curve));
-                return easingCurve;
+                qmlInfo(obj) << obj->tr("Improperly specified parameter in easing function \"%1\"").arg(curve);
+                continue;
             }
 
             if (propName == QLatin1String("amplitude")) {
@@ -130,10 +126,12 @@ QEasingCurve stringToCurve(const QString &curve)
                 easingCurve.setPeriod(propValue);
             } else if (propName == QLatin1String("overshoot")) {
                 easingCurve.setOvershoot(propValue);
+            } else {
+                qmlInfo(obj) << obj->tr("Unknown easing parameter \"%1\"").arg(propName);
+                continue;
             }
         }
     }
-
     return easingCurve;
 }
 
@@ -219,16 +217,14 @@ void QmlAbstractAnimationPrivate::commence()
     }
 }
 
-//### make static?
-QmlMetaProperty QmlAbstractAnimationPrivate::createProperty(QObject *obj, const QString &str)
+QmlMetaProperty QmlAbstractAnimationPrivate::createProperty(QObject *obj, const QString &str, QObject *infoObj)
 {
-    Q_Q(QmlAbstractAnimation);
     QmlMetaProperty prop = QmlMetaProperty::createProperty(obj, str);
     if (!prop.isValid()) {
-        qmlInfo(q) << QmlAbstractAnimation::tr("Cannot animate non-existant property \"%1\"").arg(str);
+        qmlInfo(infoObj) << QmlAbstractAnimation::tr("Cannot animate non-existant property \"%1\"").arg(str);
         return QmlMetaProperty();
     } else if (!prop.isWritable()) {
-        qmlInfo(q) << QmlAbstractAnimation::tr("Cannot animate read-only property \"%1\"").arg(str);
+        qmlInfo(infoObj) << QmlAbstractAnimation::tr("Cannot animate read-only property \"%1\"").arg(str);
         return QmlMetaProperty();
     }
     return prop;
@@ -436,7 +432,7 @@ void QmlAbstractAnimation::setTarget(QObject *o)
 
     d->target = o;
     if (d->target && !d->propertyName.isEmpty()) {
-        d->userProperty = d->createProperty(d->target, d->propertyName);
+        d->userProperty = d->createProperty(d->target, d->propertyName, this);
     } else {
         d->userProperty.invalidate();
     }
@@ -458,7 +454,7 @@ void QmlAbstractAnimation::setProperty(const QString &n)
 
     d->propertyName = n;
     if (d->target && !d->propertyName.isEmpty()) {
-        d->userProperty = d->createProperty(d->target, d->propertyName);
+        d->userProperty = d->createProperty(d->target, d->propertyName, this);
     } else {
         d->userProperty.invalidate();
     }
@@ -652,7 +648,7 @@ int QmlPauseAnimation::duration() const
 void QmlPauseAnimation::setDuration(int duration)
 {
     if (duration < 0) {
-        qWarning("QmlPauseAnimation: Cannot set a duration of < 0");
+        qmlInfo(this) << tr("Cannot set a duration of < 0");
         return;
     }
 
@@ -1029,7 +1025,7 @@ void QmlPropertyAction::transition(QmlStateActions &actions,
 
     if (hasTarget && d->value.isValid()) {
         Action myAction;
-        myAction.property = d->createProperty(target(), d->propertyName);
+        myAction.property = d->createProperty(target(), d->propertyName, this);
         if (myAction.property.isValid()) {
             myAction.toValue = d->value;
             data->actions << myAction;
@@ -1630,7 +1626,7 @@ int QmlPropertyAnimation::duration() const
 void QmlPropertyAnimation::setDuration(int duration)
 {
     if (duration < 0) {
-        qWarning("QmlPropertyAnimation: Cannot set a duration of < 0");
+        qmlInfo(this) << tr("Cannot set a duration of < 0");
         return;
     }
 
@@ -1872,7 +1868,7 @@ void QmlPropertyAnimation::setEasing(const QString &e)
         return;
 
     d->easing = e;
-    d->va->setEasingCurve(stringToCurve(d->easing));
+    d->va->setEasingCurve(stringToCurve(d->easing, this));
     emit easingChanged(e);
 }
 
@@ -2114,7 +2110,7 @@ void QmlPropertyAnimation::transition(QmlStateActions &actions,
     //an explicit animation has been specified
     if (hasTarget && d->toIsDefined) {
         Action myAction;
-        myAction.property = d->createProperty(target(), d->propertyName);
+        myAction.property = d->createProperty(target(), d->propertyName, this);
         if (myAction.property.isValid()) {
             if (d->fromIsDefined) {
                 d->convertVariant(d->from, d->interpolatorType ? d->interpolatorType : myAction.property.propertyType());
diff --git a/src/declarative/util/qmlanimation_p.h b/src/declarative/util/qmlanimation_p.h
index f126dee..87b6703 100644
--- a/src/declarative/util/qmlanimation_p.h
+++ b/src/declarative/util/qmlanimation_p.h
@@ -136,7 +136,7 @@ private Q_SLOTS:
 };
 
 class QmlPauseAnimationPrivate;
-class QmlPauseAnimation : public QmlAbstractAnimation
+class Q_AUTOTEST_EXPORT QmlPauseAnimation : public QmlAbstractAnimation
 {
     Q_OBJECT
     Q_DECLARE_PRIVATE(QmlPauseAnimation)
diff --git a/src/declarative/util/qmlanimation_p_p.h b/src/declarative/util/qmlanimation_p_p.h
index e50415f..27c0cd7 100644
--- a/src/declarative/util/qmlanimation_p_p.h
+++ b/src/declarative/util/qmlanimation_p_p.h
@@ -208,7 +208,7 @@ public:
     QmlMetaProperty property;
     QmlAnimationGroup *group;
 
-    QmlMetaProperty createProperty(QObject *obj, const QString &str);
+    static QmlMetaProperty createProperty(QObject *obj, const QString &str, QObject *infoObj);
 };
 
 class QmlPauseAnimationPrivate : public QmlAbstractAnimationPrivate
diff --git a/tests/auto/declarative/animations/tst_animations.cpp b/tests/auto/declarative/animations/tst_animations.cpp
index 2692cb6..2506337 100644
--- a/tests/auto/declarative/animations/tst_animations.cpp
+++ b/tests/auto/declarative/animations/tst_animations.cpp
@@ -57,6 +57,8 @@ private slots:
     void simpleNumber();
     void simpleColor();
     void alwaysRunToEnd();
+    void complete();
+    void resume();
     void dotProperty();
     void badTypes();
     void badProperties();
@@ -64,6 +66,7 @@ private slots:
     void properties();
     void propertiesTransition();
     void easingStringConversion();
+    void invalidDuration();
 };
 
 #define QTIMED_COMPARE(lhs, rhs) do { \
@@ -96,6 +99,7 @@ void tst_animations::simpleProperty()
     QVERIFY(animation.isRunning());
     QVERIFY(animation.isPaused());
     animation.setCurrentTime(125);
+    QVERIFY(animation.currentTime() == 125);
     QCOMPARE(rect.pos(), QPointF(100,100));
 }
 
@@ -120,6 +124,7 @@ void tst_animations::simpleNumber()
     QVERIFY(animation.isRunning());
     QVERIFY(animation.isPaused());
     animation.setCurrentTime(125);
+    QVERIFY(animation.currentTime() == 125);
     QCOMPARE(rect.x(), qreal(100));
 }
 
@@ -144,6 +149,7 @@ void tst_animations::simpleColor()
     QVERIFY(animation.isRunning());
     QVERIFY(animation.isPaused());
     animation.setCurrentTime(125);
+    QVERIFY(animation.currentTime() == 125);
     QCOMPARE(rect.color(), QColor::fromRgbF(0.498039, 0, 0.498039, 1));
 }
 
@@ -157,6 +163,8 @@ void tst_animations::alwaysRunToEnd()
     animation.setDuration(1000);
     animation.setRepeat(true);
     animation.setAlwaysRunToEnd(true);
+    QVERIFY(animation.repeat() == true);
+    QVERIFY(animation.alwaysRunToEnd() == true);
     animation.start();
     QTest::qWait(1500);
     animation.stop();
@@ -165,6 +173,54 @@ void tst_animations::alwaysRunToEnd()
     QTIMED_COMPARE(rect.x(), qreal(200));
 }
 
+void tst_animations::complete()
+{
+    QmlGraphicsRectangle rect;
+    QmlPropertyAnimation animation;
+    animation.setTarget(&rect);
+    animation.setProperty("x");
+    animation.setFrom(1);
+    animation.setTo(200);
+    animation.setDuration(500);
+    QVERIFY(animation.from() == 1);
+    animation.start();
+    QTest::qWait(50);
+    animation.stop();
+    QVERIFY(rect.x() != qreal(200));
+    animation.start();
+    QTest::qWait(50);
+    QVERIFY(animation.isRunning());
+    animation.complete();
+    QCOMPARE(rect.x(), qreal(200));
+}
+
+void tst_animations::resume()
+{
+    QmlGraphicsRectangle rect;
+    QmlPropertyAnimation animation;
+    animation.setTarget(&rect);
+    animation.setProperty("x");
+    animation.setFrom(10);
+    animation.setTo(200);
+    animation.setDuration(500);
+    QVERIFY(animation.from() == 10);
+
+    animation.start();
+    QTest::qWait(50);
+    animation.pause();
+    qreal x = rect.x();
+    QVERIFY(x != qreal(200));
+    QVERIFY(animation.isRunning());
+    QVERIFY(animation.isPaused());
+
+    animation.resume();
+    QVERIFY(animation.isRunning());
+    QVERIFY(!animation.isPaused());
+    QTest::qWait(50);
+    animation.stop();
+    QVERIFY(rect.x() > x);
+}
+
 void tst_animations::dotProperty()
 {
     QmlGraphicsRectangle rect;
@@ -180,6 +236,7 @@ void tst_animations::dotProperty()
     animation.start();
     animation.pause();
     animation.setCurrentTime(125);
+    QVERIFY(animation.currentTime() == 125);
     QCOMPARE(rect.border()->width(), 5);
 }
 
@@ -427,6 +484,7 @@ void tst_animations::easingStringConversion()
 {
     QmlNumberAnimation *animation = new QmlNumberAnimation;
     animation->setEasing("easeInOutQuad");
+    QCOMPARE(animation->easing(),QLatin1String("easeInOutQuad"));
     QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve(), QEasingCurve(QEasingCurve::InOutQuad));
 
     animation->setEasing("OutQuad");
@@ -436,9 +494,56 @@ void tst_animations::easingStringConversion()
     QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::OutBounce);
     QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().amplitude(), qreal(5));
 
+    animation->setEasing("easeOutElastic(amplitude: 5, period: 3)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::OutElastic);
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().amplitude(), qreal(5));
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().period(), qreal(3));
+
+    animation->setEasing("easeInOutBack(overshoot: 2)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::InOutBack);
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().overshoot(), qreal(2));
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Unmatched parenthesis in easing function \"easeInOutBack(overshoot: 2\"");
+    animation->setEasing("easeInOutBack(overshoot: 2");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::Linear);
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Easing function \"InOutBack(overshoot: 2)\" must start with \"ease\"");
+    animation->setEasing("InOutBack(overshoot: 2)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::Linear);
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Unknown easing curve \"NonExistantEase\"");
+    animation->setEasing("NonExistantEase");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::Linear);
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Improperly specified parameter in easing function \"easeInOutElastic(amplitude 5)\"");
+    animation->setEasing("easeInOutElastic(amplitude 5)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::InOutElastic);
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Improperly specified parameter in easing function \"easeInOutElastic(amplitude: yes)\"");
+    animation->setEasing("easeInOutElastic(amplitude: yes)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::InOutElastic);
+    QVERIFY(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().amplitude() != qreal(5));
+
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlNumberAnimation (unknown location) Unknown easing parameter \"nonexistantproperty\"");
+    animation->setEasing("easeOutQuad(nonexistantproperty: 12)");
+    QCOMPARE(static_cast<QVariantAnimation*>(((QmlAbstractAnimation*)animation)->qtAnimation())->easingCurve().type(), QEasingCurve::OutQuad);
+
     delete animation;
 }
 
+void tst_animations::invalidDuration()
+{
+    QmlPropertyAnimation *animation = new QmlPropertyAnimation;
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlPropertyAnimation (unknown location) Cannot set a duration of < 0");
+    animation->setDuration(-1);
+    QCOMPARE(animation->duration(), 250);
+
+    QmlPauseAnimation *pauseAnimation = new QmlPauseAnimation;
+    QTest::ignoreMessage(QtWarningMsg, "QML QmlPauseAnimation (unknown location) Cannot set a duration of < 0");
+    pauseAnimation->setDuration(-1);
+    QCOMPARE(pauseAnimation->duration(), 250);
+}
+
 QTEST_MAIN(tst_animations)
 
 #include "tst_animations.moc"
-- 
cgit v0.12