tests/auto/qpropertyanimation/tst_qpropertyanimation.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
    42 #include <QtTest/QtTest>
    42 #include <QtTest/QtTest>
       
    43 #include "../../shared/util.h"
    43 
    44 
    44 #include <QtCore/qpropertyanimation.h>
    45 #include <QtCore/qpropertyanimation.h>
    45 #include <QtCore/qvariantanimation.h>
    46 #include <QtCore/qvariantanimation.h>
    46 #include <QtGui/qwidget.h>
    47 #include <QtGui/qwidget.h>
    47 
    48 
   285     QCOMPARE(currentLoopSpy.count(), 2);
   286     QCOMPARE(currentLoopSpy.count(), 2);
   286     runningSpy.clear();
   287     runningSpy.clear();
   287 
   288 
   288     anim->start();
   289     anim->start();
   289     QTest::qWait(1000);
   290     QTest::qWait(1000);
   290     QCOMPARE(anim->state(), QAnimationGroup::Stopped);
   291     QTRY_COMPARE(anim->state(), QAnimationGroup::Stopped);
   291     QCOMPARE(runningSpy.count(), 2); //started and stopped again
   292     QCOMPARE(runningSpy.count(), 2); //started and stopped again
   292     runningSpy.clear();
   293     runningSpy.clear();
   293     QCOMPARE(finishedSpy.count(), 1);
   294     QCOMPARE(finishedSpy.count(), 1);
   294     QCOMPARE(anim->currentLoopTime(), 100);
   295     QCOMPARE(anim->currentLoopTime(), 100);
   295     QCOMPARE(anim->currentLoop(), 2);
   296     QCOMPARE(anim->currentLoop(), 2);
   337     QTest::qWait(100);
   338     QTest::qWait(100);
   338     QVERIFY(anim);
   339     QVERIFY(anim);
   339     QCOMPARE(anim->state(), QAnimationGroup::Running);
   340     QCOMPARE(anim->state(), QAnimationGroup::Running);
   340     QTest::qWait(150);
   341     QTest::qWait(150);
   341     QVERIFY(anim); //The animation should not have been deleted
   342     QVERIFY(anim); //The animation should not have been deleted
   342     QCOMPARE(anim->state(), QAnimationGroup::Stopped);
   343     QTRY_COMPARE(anim->state(), QAnimationGroup::Stopped);
   343     QCOMPARE(runningSpy.count(), 2);
   344     QCOMPARE(runningSpy.count(), 2);
   344     QCOMPARE(finishedSpy.count(), 1);
   345     QCOMPARE(finishedSpy.count(), 1);
   345 
   346 
   346     anim->start(QVariantAnimation::DeleteWhenStopped);
   347     anim->start(QVariantAnimation::DeleteWhenStopped);
   347     QVERIFY(anim);
   348     QVERIFY(anim);
   348     QCOMPARE(anim->state(), QAnimationGroup::Running);
   349     QCOMPARE(anim->state(), QAnimationGroup::Running);
   349     QTest::qWait(100);
   350     QTest::qWait(100);
   350     QVERIFY(anim);
   351     QVERIFY(anim);
   351     QCOMPARE(anim->state(), QAnimationGroup::Running);
   352     QCOMPARE(anim->state(), QAnimationGroup::Running);
   352     QTest::qWait(150);
   353     QTest::qWait(150);
       
   354     QTRY_COMPARE(runningSpy.count(), 4);
       
   355     QCOMPARE(finishedSpy.count(), 2);
   353     QVERIFY(!anim);  //The animation must have been deleted
   356     QVERIFY(!anim);  //The animation must have been deleted
   354     QCOMPARE(runningSpy.count(), 4);
       
   355     QCOMPARE(finishedSpy.count(), 2);
       
   356     delete object;
   357     delete object;
   357 }
   358 }
   358 
   359 
   359 void tst_QPropertyAnimation::deletion2()
   360 void tst_QPropertyAnimation::deletion2()
   360 {
   361 {
   456     a.setDuration(250);
   457     a.setDuration(250);
   457     a.start();
   458     a.start();
   458 
   459 
   459     QTest::qWait(300);
   460     QTest::qWait(300);
   460 
   461 
   461     QCOMPARE(o.values.first(), 42);
   462     QTRY_COMPARE(o.values.first(), 42);
   462     QCOMPARE(o.values.last(), 420);
   463     QCOMPARE(o.values.last(), 420);
   463 }
   464 }
   464 
   465 
   465 void tst_QPropertyAnimation::noStartValueWithLoop()
   466 void tst_QPropertyAnimation::noStartValueWithLoop()
   466 {
   467 {
   494         QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole");
   495         QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole");
   495         anim->setEndValue(100);
   496         anim->setEndValue(100);
   496         QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)));
   497         QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)));
   497         anim->start(QVariantAnimation::DeleteWhenStopped);
   498         anim->start(QVariantAnimation::DeleteWhenStopped);
   498         QTest::qWait(anim->duration() + 100);
   499         QTest::qWait(anim->duration() + 100);
   499         QCOMPARE(runningSpy.count(), 2); //started and then stopped
   500         QTRY_COMPARE(runningSpy.count(), 2); //started and then stopped
   500         QVERIFY(!anim);
   501         QVERIFY(!anim);
   501     }
   502     }
   502 
   503 
   503     {
   504     {
   504         QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole");
   505         QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole");
   515         QMetaObject::invokeMethod(anim2, "start", Qt::QueuedConnection, Q_ARG(QAbstractAnimation::DeletionPolicy, QVariantAnimation::DeleteWhenStopped));
   516         QMetaObject::invokeMethod(anim2, "start", Qt::QueuedConnection, Q_ARG(QAbstractAnimation::DeletionPolicy, QVariantAnimation::DeleteWhenStopped));
   516         QTest::qWait(50);
   517         QTest::qWait(50);
   517         QVERIFY(!anim); //anim should have been deleted
   518         QVERIFY(!anim); //anim should have been deleted
   518         QVERIFY(anim2);
   519         QVERIFY(anim2);
   519         QTest::qWait(anim2->duration());
   520         QTest::qWait(anim2->duration());
   520         QVERIFY(!anim2); //anim2 is finished: it should have been deleted by now
   521         QTRY_VERIFY(!anim2); //anim2 is finished: it should have been deleted by now
   521         QVERIFY(!anim);
   522         QVERIFY(!anim);
   522     }
   523     }
   523 
   524 
   524 }
   525 }
   525 
   526 
   588     //it is somewhere in the animation
   589     //it is somewhere in the animation
   589     QVERIFY(current > 42);
   590     QVERIFY(current > 42);
   590     QVERIFY(current < 100);
   591     QVERIFY(current < 100);
   591 
   592 
   592     QTest::qWait(200);
   593     QTest::qWait(200);
   593     QCOMPARE(anim.state(), QVariantAnimation::Stopped);
   594     QTRY_COMPARE(anim.state(), QVariantAnimation::Stopped);
   594     current = anim.currentValue().toInt();
   595     current = anim.currentValue().toInt();
   595     QCOMPARE(current, 100);
   596     QCOMPARE(current, 100);
   596     QCOMPARE(o.property("ole").toInt(), current);
   597     QCOMPARE(o.property("ole").toInt(), current);
   597 
   598 
   598     anim.setEndValue(110);
   599     anim.setEndValue(110);
   628     //it is somewhere in the animation
   629     //it is somewhere in the animation
   629     QVERIFY(current > 42);
   630     QVERIFY(current > 42);
   630     QVERIFY(current < 100);
   631     QVERIFY(current < 100);
   631 
   632 
   632     QTest::qWait(200);
   633     QTest::qWait(200);
   633     QCOMPARE(anim.state(), QVariantAnimation::Stopped);
   634     QTRY_COMPARE(anim.state(), QVariantAnimation::Stopped);
   634     current = anim.currentValue().toInt();
   635     current = anim.currentValue().toInt();
   635     QCOMPARE(current, 100);
   636     QCOMPARE(current, 100);
   636     QCOMPARE(o.property("ole").toInt(), current);
   637     QCOMPARE(o.property("ole").toInt(), current);
   637 
   638 
   638     anim.setStartValue(110);
   639     anim.setStartValue(110);
   658     QPropertyAnimation anim(&o, "ole");
   659     QPropertyAnimation anim(&o, "ole");
   659     anim.setStartValue(0);
   660     anim.setStartValue(0);
   660     anim.setEndValue(100);
   661     anim.setEndValue(100);
   661     anim.start();
   662     anim.start();
   662     QTest::qWait(anim.duration() + 100);
   663     QTest::qWait(anim.duration() + 100);
   663     QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
   664     QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped);
   664     QCOMPARE(anim.currentTime(), anim.duration());
   665     QCOMPARE(anim.currentTime(), anim.duration());
   665 
   666 
   666     //the animation is at the end
   667     //the animation is at the end
   667     anim.setDirection(QVariantAnimation::Backward);
   668     anim.setDirection(QVariantAnimation::Backward);
   668     anim.start();
   669     anim.start();
   669     QCOMPARE(anim.state(), QAbstractAnimation::Running);
   670     QCOMPARE(anim.state(), QAbstractAnimation::Running);
   670     QTest::qWait(anim.duration() + 100);
   671     QTest::qWait(anim.duration() + 100);
   671     QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
   672     QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped);
   672     QCOMPARE(anim.currentTime(), 0);
   673     QCOMPARE(anim.currentTime(), 0);
   673 
   674 
   674     //the direction is backward
   675     //the direction is backward
   675     //restarting should jump to the end
   676     //restarting should jump to the end
   676     anim.start();
   677     anim.start();
   677     QCOMPARE(anim.state(), QAbstractAnimation::Running);
   678     QCOMPARE(anim.state(), QAbstractAnimation::Running);
   678     QCOMPARE(anim.currentTime(), anim.duration());
   679     QCOMPARE(anim.currentTime(), anim.duration());
   679     QTest::qWait(anim.duration() + 100);
   680     QTest::qWait(anim.duration() + 100);
   680     QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
   681     QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped);
   681     QCOMPARE(anim.currentTime(), 0);
   682     QCOMPARE(anim.currentTime(), 0);
   682 }
   683 }
   683 
   684 
   684 struct Number
   685 struct Number
   685 {
   686 {
  1093     QSignalSpy spy(&anim, SIGNAL(valueChanged(QVariant)));
  1094     QSignalSpy spy(&anim, SIGNAL(valueChanged(QVariant)));
  1094     anim.start();
  1095     anim.start();
  1095 
  1096 
  1096     QTest::qWait(anim.duration() + 100);
  1097     QTest::qWait(anim.duration() + 100);
  1097 
  1098 
  1098     QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
  1099     QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped);
  1099     QCOMPARE(anim.currentTime(), anim.duration());
  1100     QCOMPARE(anim.currentTime(), anim.duration());
  1100 
  1101 
  1101     //let's check that the values go forward
  1102     //let's check that the values go forward
  1102     QCOMPARE(spy.count(), 6); //we should have got everything from 0 to 5
  1103     QCOMPARE(spy.count(), 6); //we should have got everything from 0 to 5
  1103     for (int i = 0; i < spy.count(); ++i) {
  1104     for (int i = 0; i < spy.count(); ++i) {
  1143 
  1144 
  1144     o1.anim.start();
  1145     o1.anim.start();
  1145     o2.anim.start();
  1146     o2.anim.start();
  1146 
  1147 
  1147     QTest::qWait(o1.anim.duration() + 100);
  1148     QTest::qWait(o1.anim.duration() + 100);
  1148     QCOMPARE(o1.anim.state(), QAbstractAnimation::Stopped);
  1149     QTRY_COMPARE(o1.anim.state(), QAbstractAnimation::Stopped);
  1149     QCOMPARE(o2.anim.state(), QAbstractAnimation::Stopped);
  1150     QCOMPARE(o2.anim.state(), QAbstractAnimation::Stopped);
  1150 
  1151 
  1151     QCOMPARE(o1.ole(), 1000);
  1152     QCOMPARE(o1.ole(), 1000);
  1152     QCOMPARE(o2.ole(), 1000);
  1153     QCOMPARE(o2.ole(), 1000);
  1153 }
  1154 }
  1194     MyComposedAnimation composedAnimation(&o, "value", "realValue");
  1195     MyComposedAnimation composedAnimation(&o, "value", "realValue");
  1195     composedAnimation.start();
  1196     composedAnimation.start();
  1196     QCOMPARE(composedAnimation.state(), QAbstractAnimation::Running);
  1197     QCOMPARE(composedAnimation.state(), QAbstractAnimation::Running);
  1197     QTest::qWait(composedAnimation.duration() + 100);
  1198     QTest::qWait(composedAnimation.duration() + 100);
  1198 
  1199 
  1199     QCOMPARE(composedAnimation.state(), QAbstractAnimation::Stopped);
  1200     QTRY_COMPARE(composedAnimation.state(), QAbstractAnimation::Stopped);
  1200     QCOMPARE(o.value(), 1000);
  1201     QCOMPARE(o.value(), 1000);
  1201 }
  1202 }
  1202 
  1203 
  1203 void tst_QPropertyAnimation::totalDuration()
  1204 void tst_QPropertyAnimation::totalDuration()
  1204 {
  1205 {