tests/auto/qanimationgroup/tst_qanimationgroup.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtTest/QtTest>
       
    43 
       
    44 #include <QtCore/qanimationgroup.h>
       
    45 #include <QtCore/qsequentialanimationgroup.h>
       
    46 #include <QtCore/qparallelanimationgroup.h>
       
    47 
       
    48 //TESTED_CLASS=QAnimationGroup
       
    49 //TESTED_FILES=
       
    50 
       
    51 Q_DECLARE_METATYPE(QAbstractAnimation::State)
       
    52 
       
    53 class tst_QAnimationGroup : public QObject
       
    54 {
       
    55   Q_OBJECT
       
    56 public:
       
    57     tst_QAnimationGroup();
       
    58     virtual ~tst_QAnimationGroup();
       
    59 
       
    60 public Q_SLOTS:
       
    61     void init();
       
    62     void cleanup();
       
    63 
       
    64 private slots:
       
    65     void construction();
       
    66     void emptyGroup();
       
    67     void setCurrentTime();
       
    68     void statesAndSignals();
       
    69     void setParentAutoAdd();
       
    70     void beginNestedGroup();
       
    71     void addChildTwice();
       
    72     void loopWithoutStartValue();
       
    73 };
       
    74 
       
    75 tst_QAnimationGroup::tst_QAnimationGroup()
       
    76 {
       
    77 }
       
    78 
       
    79 tst_QAnimationGroup::~tst_QAnimationGroup()
       
    80 {
       
    81 }
       
    82 
       
    83 void tst_QAnimationGroup::init()
       
    84 {
       
    85     qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State");
       
    86 }
       
    87 
       
    88 void tst_QAnimationGroup::cleanup()
       
    89 {
       
    90 }
       
    91 
       
    92 void tst_QAnimationGroup::construction()
       
    93 {
       
    94     QSequentialAnimationGroup animationgroup;
       
    95 }
       
    96 
       
    97 class AnimationObject : public QObject
       
    98 {
       
    99     Q_OBJECT
       
   100     Q_PROPERTY(int value READ value WRITE setValue)
       
   101 public:
       
   102     AnimationObject(int startValue = 0)
       
   103         : v(startValue)
       
   104     { }
       
   105 
       
   106     int value() const { return v; }
       
   107     void setValue(int value) { v = value; }
       
   108 
       
   109     int v;
       
   110 };
       
   111 
       
   112 class TestAnimation : public QVariantAnimation
       
   113 {
       
   114     Q_OBJECT
       
   115 public:
       
   116     virtual void updateCurrentValue(const QVariant &value) { Q_UNUSED(value)};
       
   117     virtual void updateState(QAbstractAnimation::State oldState,
       
   118                              QAbstractAnimation::State newState)
       
   119     {
       
   120         Q_UNUSED(oldState)
       
   121         Q_UNUSED(newState)
       
   122     };
       
   123 };
       
   124 
       
   125 class UncontrolledAnimation : public QPropertyAnimation
       
   126 {
       
   127     Q_OBJECT
       
   128 public:
       
   129     UncontrolledAnimation(QObject *target, const QByteArray &propertyName, QObject *parent = 0)
       
   130         : QPropertyAnimation(target, propertyName, parent), id(0)
       
   131     {
       
   132         setDuration(250);
       
   133     }
       
   134 
       
   135     int duration() const { return -1; /* not time driven */ }
       
   136 
       
   137 protected:
       
   138     void timerEvent(QTimerEvent *event)
       
   139     {
       
   140         if (event->timerId() == id)
       
   141             stop();
       
   142     }
       
   143 
       
   144     void updateRunning(bool running)
       
   145     {
       
   146         if (running) {
       
   147             id = startTimer(500);
       
   148         } else {
       
   149             killTimer(id);
       
   150             id = 0;
       
   151         }
       
   152     }
       
   153 
       
   154 private:
       
   155     int id;
       
   156 };
       
   157 
       
   158 void tst_QAnimationGroup::emptyGroup()
       
   159 {
       
   160     QSequentialAnimationGroup group;
       
   161     QSignalSpy groupStateChangedSpy(&group, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State)));
       
   162 
       
   163     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   164     group.start();
       
   165 
       
   166     QCOMPARE(groupStateChangedSpy.count(), 2);
       
   167 
       
   168     QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(0).at(1)),
       
   169              QAnimationGroup::Running);
       
   170     QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(1).at(1)),
       
   171              QAnimationGroup::Stopped);
       
   172 
       
   173     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   174 
       
   175     QTest::ignoreMessage(QtWarningMsg, "QAbstractAnimation::pause: Cannot pause a stopped animation");
       
   176     group.pause();
       
   177 
       
   178     QCOMPARE(groupStateChangedSpy.count(), 2);
       
   179     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   180 
       
   181     group.start();
       
   182 
       
   183     QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(2).at(1)),
       
   184              QAnimationGroup::Running);
       
   185     QCOMPARE(qVariantValue<QAbstractAnimation::State>(groupStateChangedSpy.at(3).at(1)),
       
   186              QAnimationGroup::Stopped);
       
   187 
       
   188     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   189 
       
   190     group.stop();
       
   191 
       
   192     QCOMPARE(groupStateChangedSpy.count(), 4);
       
   193     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   194 }
       
   195 
       
   196 void tst_QAnimationGroup::setCurrentTime()
       
   197 {
       
   198     AnimationObject s_o1;
       
   199     AnimationObject s_o2;
       
   200     AnimationObject s_o3;
       
   201     AnimationObject p_o1;
       
   202     AnimationObject p_o2;
       
   203     AnimationObject p_o3;
       
   204     AnimationObject t_o1;
       
   205     AnimationObject t_o2;
       
   206 
       
   207     // sequence operating on same object/property
       
   208     QSequentialAnimationGroup *sequence = new QSequentialAnimationGroup();
       
   209     QAbstractAnimation *a1_s_o1 = new QPropertyAnimation(&s_o1, "value");
       
   210     QAbstractAnimation *a2_s_o1 = new QPropertyAnimation(&s_o1, "value");
       
   211     QAbstractAnimation *a3_s_o1 = new QPropertyAnimation(&s_o1, "value");
       
   212     a2_s_o1->setLoopCount(3);
       
   213     sequence->addAnimation(a1_s_o1);
       
   214     sequence->addAnimation(a2_s_o1);
       
   215     sequence->addAnimation(a3_s_o1);
       
   216 
       
   217     // sequence operating on different object/properties
       
   218     QAnimationGroup *sequence2 = new QSequentialAnimationGroup();
       
   219     QAbstractAnimation *a1_s_o2 = new QPropertyAnimation(&s_o2, "value");
       
   220     QAbstractAnimation *a1_s_o3 = new QPropertyAnimation(&s_o3, "value");
       
   221     sequence2->addAnimation(a1_s_o2);
       
   222     sequence2->addAnimation(a1_s_o3);
       
   223 
       
   224     // parallel operating on different object/properties
       
   225     QAnimationGroup *parallel = new QParallelAnimationGroup();
       
   226     QAbstractAnimation *a1_p_o1 = new QPropertyAnimation(&p_o1, "value");
       
   227     QAbstractAnimation *a1_p_o2 = new QPropertyAnimation(&p_o2, "value");
       
   228     QAbstractAnimation *a1_p_o3 = new QPropertyAnimation(&p_o3, "value");
       
   229     a1_p_o2->setLoopCount(3);
       
   230     parallel->addAnimation(a1_p_o1);
       
   231     parallel->addAnimation(a1_p_o2);
       
   232     parallel->addAnimation(a1_p_o3);
       
   233 
       
   234     QAbstractAnimation *notTimeDriven = new UncontrolledAnimation(&t_o1, "value");
       
   235     QCOMPARE(notTimeDriven->totalDuration(), -1);
       
   236 
       
   237     QAbstractAnimation *loopsForever = new QPropertyAnimation(&t_o2, "value");
       
   238     loopsForever->setLoopCount(-1);
       
   239     QCOMPARE(loopsForever->totalDuration(), -1);
       
   240 
       
   241     QParallelAnimationGroup group;
       
   242     group.addAnimation(sequence);
       
   243     group.addAnimation(sequence2);
       
   244     group.addAnimation(parallel);
       
   245     group.addAnimation(notTimeDriven);
       
   246     group.addAnimation(loopsForever);
       
   247 
       
   248     // Current time = 1
       
   249     group.setCurrentTime(1);
       
   250     QCOMPARE(group.state(), QAnimationGroup::Stopped);
       
   251     QCOMPARE(sequence->state(), QAnimationGroup::Stopped);
       
   252     QCOMPARE(a1_s_o1->state(), QAnimationGroup::Stopped);
       
   253     QCOMPARE(sequence2->state(), QAnimationGroup::Stopped);
       
   254     QCOMPARE(a1_s_o2->state(), QAnimationGroup::Stopped);
       
   255     QCOMPARE(parallel->state(), QAnimationGroup::Stopped);
       
   256     QCOMPARE(a1_p_o1->state(), QAnimationGroup::Stopped);
       
   257     QCOMPARE(a1_p_o2->state(), QAnimationGroup::Stopped);
       
   258     QCOMPARE(a1_p_o3->state(), QAnimationGroup::Stopped);
       
   259     QCOMPARE(notTimeDriven->state(), QAnimationGroup::Stopped);
       
   260     QCOMPARE(loopsForever->state(), QAnimationGroup::Stopped);
       
   261 
       
   262     QCOMPARE(group.currentTime(), 1);
       
   263     QCOMPARE(sequence->currentTime(), 1);
       
   264     QCOMPARE(a1_s_o1->currentTime(), 1);
       
   265     QCOMPARE(a2_s_o1->currentTime(), 0);
       
   266     QCOMPARE(a3_s_o1->currentTime(), 0);
       
   267     QCOMPARE(a1_s_o2->currentTime(), 1);
       
   268     QCOMPARE(a1_s_o3->currentTime(), 0);
       
   269     QCOMPARE(a1_p_o1->currentTime(), 1);
       
   270     QCOMPARE(a1_p_o2->currentTime(), 1);
       
   271     QCOMPARE(a1_p_o3->currentTime(), 1);
       
   272     QCOMPARE(notTimeDriven->currentTime(), 1);
       
   273     QCOMPARE(loopsForever->currentTime(), 1);
       
   274 
       
   275     // Current time = 250
       
   276     group.setCurrentTime(250);
       
   277     QCOMPARE(group.currentTime(), 250);
       
   278     QCOMPARE(sequence->currentTime(), 250);
       
   279     QCOMPARE(a1_s_o1->currentTime(), 250);
       
   280     QCOMPARE(a2_s_o1->currentTime(), 0);
       
   281     QCOMPARE(a3_s_o1->currentTime(), 0);
       
   282     QCOMPARE(a1_s_o2->currentTime(), 250);
       
   283     QCOMPARE(a1_s_o3->currentTime(), 0);
       
   284     QCOMPARE(a1_p_o1->currentTime(), 250);
       
   285     QCOMPARE(a1_p_o2->currentTime(), 0);
       
   286     QCOMPARE(a1_p_o2->currentLoop(), 1);
       
   287     QCOMPARE(a1_p_o3->currentTime(), 250);
       
   288     QCOMPARE(notTimeDriven->currentTime(), 250);
       
   289     QCOMPARE(loopsForever->currentTime(), 0);
       
   290     QCOMPARE(loopsForever->currentLoop(), 1);
       
   291     QCOMPARE(sequence->currentAnimation(), a2_s_o1);
       
   292 
       
   293     // Current time = 251
       
   294     group.setCurrentTime(251);
       
   295     QCOMPARE(group.currentTime(), 251);
       
   296     QCOMPARE(sequence->currentTime(), 251);
       
   297     QCOMPARE(a1_s_o1->currentTime(), 250);
       
   298     QCOMPARE(a2_s_o1->currentTime(), 1);
       
   299     QCOMPARE(a2_s_o1->currentLoop(), 0);
       
   300     QCOMPARE(a3_s_o1->currentTime(), 0);
       
   301     QCOMPARE(sequence2->currentTime(), 251);
       
   302     QCOMPARE(a1_s_o2->currentTime(), 250);
       
   303     QCOMPARE(a1_s_o3->currentTime(), 1);
       
   304     QCOMPARE(a1_p_o1->currentTime(), 250);
       
   305     QCOMPARE(a1_p_o2->currentTime(), 1);
       
   306     QCOMPARE(a1_p_o2->currentLoop(), 1);
       
   307     QCOMPARE(a1_p_o3->currentTime(), 250);
       
   308     QCOMPARE(notTimeDriven->currentTime(), 251);
       
   309     QCOMPARE(loopsForever->currentTime(), 1);
       
   310     QCOMPARE(sequence->currentAnimation(), a2_s_o1);
       
   311 }
       
   312 
       
   313 void tst_QAnimationGroup::statesAndSignals()
       
   314 {
       
   315 }
       
   316 
       
   317 void tst_QAnimationGroup::setParentAutoAdd()
       
   318 {
       
   319     QParallelAnimationGroup group;
       
   320     QVariantAnimation *animation = new QPropertyAnimation(&group);
       
   321     QCOMPARE(animation->group(), static_cast<QAnimationGroup*>(&group));
       
   322 }
       
   323 
       
   324 void tst_QAnimationGroup::beginNestedGroup()
       
   325 {
       
   326     QAnimationGroup *subGroup;
       
   327     QAnimationGroup *parent = new QParallelAnimationGroup();
       
   328 
       
   329     for (int i = 0; i < 10; ++i) {
       
   330         if (i & 1)
       
   331             subGroup = new QParallelAnimationGroup(parent);
       
   332         else
       
   333             subGroup = new QSequentialAnimationGroup(parent);
       
   334 
       
   335         QCOMPARE(parent->animationCount(), 1);
       
   336         QAnimationGroup *child = static_cast<QAnimationGroup *>(parent->animationAt(0));
       
   337 
       
   338         QCOMPARE(child->parent(), static_cast<QObject *>(parent));
       
   339         if (i & 1)
       
   340             QVERIFY(qobject_cast<QParallelAnimationGroup *> (child));
       
   341         else
       
   342             QVERIFY(qobject_cast<QSequentialAnimationGroup *> (child));
       
   343 
       
   344         parent = child;
       
   345     }
       
   346 }
       
   347 
       
   348 void tst_QAnimationGroup::addChildTwice()
       
   349 {
       
   350     QAbstractAnimation *subGroup;
       
   351     QAbstractAnimation *subGroup2;
       
   352     QAnimationGroup *parent = new QSequentialAnimationGroup();
       
   353 
       
   354     subGroup = new QPropertyAnimation();
       
   355     subGroup->setParent(parent);
       
   356     parent->addAnimation(subGroup);
       
   357     QCOMPARE(parent->animationCount(), 1);
       
   358 
       
   359     parent->clearAnimations();
       
   360 
       
   361     QCOMPARE(parent->animationCount(), 0);
       
   362 
       
   363     // adding the same item twice to a group will remove the item from its current position
       
   364     // and append it to the end
       
   365     subGroup = new QPropertyAnimation(parent);
       
   366     subGroup2 = new QPropertyAnimation(parent);
       
   367 
       
   368     QCOMPARE(parent->animationCount(), 2);
       
   369     QCOMPARE(parent->animationAt(0), subGroup);
       
   370     QCOMPARE(parent->animationAt(1), subGroup2);
       
   371 
       
   372     parent->addAnimation(subGroup);
       
   373 
       
   374     QCOMPARE(parent->animationCount(), 2);
       
   375     QCOMPARE(parent->animationAt(0), subGroup2);
       
   376     QCOMPARE(parent->animationAt(1), subGroup);
       
   377 
       
   378     delete parent;
       
   379 }
       
   380 
       
   381 void tst_QAnimationGroup::loopWithoutStartValue()
       
   382 {
       
   383     QAnimationGroup *parent = new QSequentialAnimationGroup();
       
   384     QObject o;
       
   385     o.setProperty("ole", 0);
       
   386     QCOMPARE(o.property("ole").toInt(), 0);
       
   387 
       
   388     QPropertyAnimation anim1(&o, "ole");
       
   389     anim1.setEndValue(-50);
       
   390     anim1.setDuration(100);
       
   391 
       
   392     QPropertyAnimation anim2(&o, "ole");
       
   393     anim2.setEndValue(50);
       
   394     anim2.setDuration(100);
       
   395 
       
   396     parent->addAnimation(&anim1);
       
   397     parent->addAnimation(&anim2);
       
   398 
       
   399     parent->setLoopCount(-1);
       
   400     parent->start();
       
   401 
       
   402     QVERIFY(anim1.startValue().isNull());
       
   403     QCOMPARE(anim1.currentValue().toInt(), 0);
       
   404     QCOMPARE(parent->currentLoop(), 0);
       
   405 
       
   406     parent->setCurrentTime(200);
       
   407     QCOMPARE(parent->currentLoop(), 1);
       
   408     QCOMPARE(anim1.currentValue().toInt(), 50);
       
   409     parent->stop();
       
   410 }
       
   411 
       
   412 QTEST_MAIN(tst_QAnimationGroup)
       
   413 #include "tst_qanimationgroup.moc"