void tst_QPauseAnimationJob::sequentialGroupWithPause()
{
    QSequentialAnimationGroupJob group;

    TestableGenericAnimation animation;
    group.appendAnimation(&animation);

    TestablePauseAnimation pause;
    pause.setDuration(250);
    group.appendAnimation(&pause);

    group.start();

    QVERIFY(group.state() == QAbstractAnimationJob::Running);
    QVERIFY(animation.state() == QAbstractAnimationJob::Running);
    QVERIFY(pause.state() == QAbstractAnimationJob::Stopped);

    group.setCurrentTime(300);

    QVERIFY(group.state() == QAbstractAnimationJob::Running);
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    QCOMPARE((QAbstractAnimationJob*)&pause, group.currentAnimation());
    QVERIFY(pause.state() == QAbstractAnimationJob::Running);

    group.setCurrentTime(600);

    QVERIFY(group.state() == QAbstractAnimationJob::Stopped);
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    QVERIFY(pause.state() == QAbstractAnimationJob::Stopped);

    QCOMPARE(pause.m_updateCurrentTimeCount, 2);
}
void tst_QPauseAnimationJob::pauseAndPropertyAnimations()
{
    EnableConsistentTiming enabled;

    TestablePauseAnimation pause;
    pause.setDuration(200);

    TestableGenericAnimation animation;

    pause.start();

    QTest::qWait(100);
    animation.start();

    QCOMPARE(animation.state(), QAbstractAnimationJob::Running);

    QTRY_COMPARE(animation.state(), QAbstractAnimationJob::Running);
    QVERIFY(pause.state() == QAbstractAnimationJob::Running);
    QVERIFY2(pause.m_updateCurrentTimeCount >= 2,
             QByteArrayLiteral("pause.m_updateCurrentTimeCount=") + QByteArray::number(pause.m_updateCurrentTimeCount));

    QTRY_COMPARE(animation.state(), QAbstractAnimationJob::Stopped);
    QCOMPARE(pause.state(), QAbstractAnimationJob::Stopped);
    QVERIFY2(pause.m_updateCurrentTimeCount > 3,
             QByteArrayLiteral("pause.m_updateCurrentTimeCount=") + QByteArray::number(pause.m_updateCurrentTimeCount));
}
void tst_QPauseAnimationJob::pauseAndPropertyAnimations()
{
    EnableConsistentTiming enabled;

    TestablePauseAnimation pause;
    pause.setDuration(200);

    TestableGenericAnimation animation;

    pause.start();

    QTest::qWait(100);
    animation.start();

    QVERIFY(animation.state() == QAbstractAnimationJob::Running);
    QVERIFY(pause.state() == QAbstractAnimationJob::Running);
    QCOMPARE(pause.m_updateCurrentTimeCount, 2);

    QTest::qWait(animation.totalDuration() + 100);

#ifdef Q_OS_WIN
    if (animation.state() != QAbstractAnimationJob::Stopped)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    QVERIFY(pause.state() == QAbstractAnimationJob::Stopped);
    QVERIFY(pause.m_updateCurrentTimeCount > 3);
}
void tst_QPauseAnimationJob::zeroDuration()
{
    TestablePauseAnimation animation;
    animation.setDuration(0);
    animation.start();
    QTest::qWait(animation.totalDuration() + 100);
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    QCOMPARE(animation.m_updateCurrentTimeCount, 1);
}
void tst_QPauseAnimationJob::noTimerUpdates()
{
    EnableConsistentTiming enabled;

    QFETCH(int, duration);
    QFETCH(int, loopCount);

    TestablePauseAnimation animation;
    animation.setDuration(duration);
    animation.setLoopCount(loopCount);
    animation.start();
    QTest::qWait(animation.totalDuration() + 100);

#ifdef Q_OS_WIN
    if (animation.state() != QAbstractAnimationJob::Stopped)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif

    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    const int expectedLoopCount = 1 + loopCount;

#ifdef Q_OS_WIN
    if (animation.m_updateCurrentTimeCount != expectedLoopCount)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QCOMPARE(animation.m_updateCurrentTimeCount, expectedLoopCount);
}
void tst_QPauseAnimationJob::pauseResume()
{
    TestablePauseAnimation animation;
    animation.setDuration(400);
    animation.start();
    QCOMPARE(animation.state(), QAbstractAnimationJob::Running);
    QTest::qWait(200);
    animation.pause();
    QCOMPARE(animation.state(), QAbstractAnimationJob::Paused);
    animation.start();
    QTest::qWait(300);
    QTRY_VERIFY(animation.state() == QAbstractAnimationJob::Stopped);
    QVERIFY2(animation.m_updateCurrentTimeCount >= 3,
            QByteArrayLiteral("animation.m_updateCurrentTimeCount=") + QByteArray::number(animation.m_updateCurrentTimeCount));
}
void tst_QPauseAnimationJob::changeDirectionWhileRunning()
{
    EnableConsistentTiming enabled;

    TestablePauseAnimation animation;
    animation.setDuration(400);
    animation.start();
    QTest::qWait(100);
    QVERIFY(animation.state() == QAbstractAnimationJob::Running);
    animation.setDirection(QAbstractAnimationJob::Backward);
    QTest::qWait(animation.totalDuration() + 50);
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);
}
void tst_QPauseAnimationJob::pauseResume()
{
    TestablePauseAnimation animation;
    animation.setDuration(400);
    animation.start();
    QVERIFY(animation.state() == QAbstractAnimationJob::Running);
    QTest::qWait(200);
    animation.pause();
    QVERIFY(animation.state() == QAbstractAnimationJob::Paused);
    animation.start();
    QTest::qWait(300);
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);

#ifdef Q_OS_WIN
    if (animation.m_updateCurrentTimeCount != 3)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QCOMPARE(animation.m_updateCurrentTimeCount, 3);
}
void tst_QPauseAnimationJob::multiplePauseAnimations()
{
    EnableConsistentTiming enabled;

    TestablePauseAnimation animation;
    animation.setDuration(200);

    TestablePauseAnimation animation2;
    animation2.setDuration(800);

    animation.start();
    animation2.start();
    QTest::qWait(animation.totalDuration() + 100);

#ifdef Q_OS_WIN
    if (animation.state() != QAbstractAnimationJob::Stopped)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QVERIFY(animation.state() == QAbstractAnimationJob::Stopped);

#ifdef Q_OS_WIN
    if (animation2.state() != QAbstractAnimationJob::Running)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QVERIFY(animation2.state() == QAbstractAnimationJob::Running);

#ifdef Q_OS_WIN
    if (animation.m_updateCurrentTimeCount != 2)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QCOMPARE(animation.m_updateCurrentTimeCount, 2);

#ifdef Q_OS_WIN
    if (animation2.m_updateCurrentTimeCount != 2)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QCOMPARE(animation2.m_updateCurrentTimeCount, 2);

    QTest::qWait(550);

#ifdef Q_OS_WIN
    if (animation2.state() != QAbstractAnimationJob::Stopped)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QVERIFY(animation2.state() == QAbstractAnimationJob::Stopped);

#ifdef Q_OS_WIN
    if (animation2.m_updateCurrentTimeCount != 3)
        QEXPECT_FAIL("", winTimerError, Abort);
#endif
    QCOMPARE(animation2.m_updateCurrentTimeCount, 3);
}