| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
|
| 30 |
#include <QtQml/private/qabstractanimationjob_p.h> |
| 31 |
#include <QtQml/private/qanimationgroupjob_p.h> |
| 32 |
#include <QtTest> |
| 33 |
|
| 34 |
class tst_QAbstractAnimationJob : public QObject |
| 35 |
{ |
| 36 |
Q_OBJECT |
| 37 |
private slots: |
| 38 |
void construction(); |
| 39 |
void destruction(); |
| 40 |
void currentLoop(); |
| 41 |
void currentLoopTime(); |
| 42 |
void currentTime(); |
| 43 |
void direction(); |
| 44 |
void group(); |
| 45 |
void loopCount(); |
| 46 |
void state(); |
| 47 |
void totalDuration(); |
| 48 |
void avoidJumpAtStart(); |
| 49 |
void avoidJumpAtStartWithStop(); |
| 50 |
void avoidJumpAtStartWithRunning(); |
| 51 |
}; |
| 52 |
|
| 53 |
class TestableQAbstractAnimation : public QAbstractAnimationJob |
| 54 |
{ |
| 55 |
public: |
| 56 |
TestableQAbstractAnimation() {} |
| 57 |
virtual ~TestableQAbstractAnimation() {}; |
| 58 |
|
| 59 |
int duration() const { return m_duration; } |
| 60 |
virtual void updateCurrentTime(int) {} |
| 61 |
|
| 62 |
void setDuration(int duration) { m_duration = duration; } |
| 63 |
private: |
| 64 |
int m_duration = 10; |
| 65 |
}; |
| 66 |
|
| 67 |
class DummyQAnimationGroup : public QAnimationGroupJob |
| 68 |
{ |
| 69 |
public: |
| 70 |
int duration() const { return 10; } |
| 71 |
virtual void updateCurrentTime(int) {} |
| 72 |
}; |
| 73 |
|
| 74 |
void tst_QAbstractAnimationJob::construction() |
| 75 |
{ |
| 76 |
TestableQAbstractAnimation anim; |
| 77 |
} |
| 78 |
|
| 79 |
void tst_QAbstractAnimationJob::destruction() |
| 80 |
{ |
| 81 |
TestableQAbstractAnimation *anim = new TestableQAbstractAnimation; |
| 82 |
delete anim; |
| 83 |
} |
| 84 |
|
| 85 |
void tst_QAbstractAnimationJob::currentLoop() |
| 86 |
{ |
| 87 |
TestableQAbstractAnimation anim; |
| 88 |
QCOMPARE(anim.currentLoop(), 0); |
| 89 |
} |
| 90 |
|
| 91 |
void tst_QAbstractAnimationJob::currentLoopTime() |
| 92 |
{ |
| 93 |
TestableQAbstractAnimation anim; |
| 94 |
QCOMPARE(anim.currentLoopTime(), 0); |
| 95 |
} |
| 96 |
|
| 97 |
void tst_QAbstractAnimationJob::currentTime() |
| 98 |
{ |
| 99 |
TestableQAbstractAnimation anim; |
| 100 |
QCOMPARE(anim.currentTime(), 0); |
| 101 |
anim.setCurrentTime(10); |
| 102 |
QCOMPARE(anim.currentTime(), 10); |
| 103 |
} |
| 104 |
|
| 105 |
void tst_QAbstractAnimationJob::direction() |
| 106 |
{ |
| 107 |
TestableQAbstractAnimation anim; |
| 108 |
QCOMPARE(anim.direction(), QAbstractAnimationJob::Forward); |
| 109 |
anim.setDirection(QAbstractAnimationJob::Backward); |
| 110 |
QCOMPARE(anim.direction(), QAbstractAnimationJob::Backward); |
| 111 |
anim.setDirection(QAbstractAnimationJob::Forward); |
| 112 |
QCOMPARE(anim.direction(), QAbstractAnimationJob::Forward); |
| 113 |
} |
| 114 |
|
| 115 |
void tst_QAbstractAnimationJob::group() |
| 116 |
{ |
| 117 |
TestableQAbstractAnimation *anim = new TestableQAbstractAnimation; |
| 118 |
DummyQAnimationGroup group; |
| 119 |
group.appendAnimation(anim); |
| 120 |
QCOMPARE(anim->group(), &group); |
| 121 |
} |
| 122 |
|
| 123 |
void tst_QAbstractAnimationJob::loopCount() |
| 124 |
{ |
| 125 |
TestableQAbstractAnimation anim; |
| 126 |
QCOMPARE(anim.loopCount(), 1); |
| 127 |
anim.setLoopCount(10); |
| 128 |
QCOMPARE(anim.loopCount(), 10); |
| 129 |
} |
| 130 |
|
| 131 |
void tst_QAbstractAnimationJob::state() |
| 132 |
{ |
| 133 |
TestableQAbstractAnimation anim; |
| 134 |
QCOMPARE(anim.state(), QAbstractAnimationJob::Stopped); |
| 135 |
} |
| 136 |
|
| 137 |
void tst_QAbstractAnimationJob::totalDuration() |
| 138 |
{ |
| 139 |
TestableQAbstractAnimation anim; |
| 140 |
QCOMPARE(anim.duration(), 10); |
| 141 |
anim.setLoopCount(5); |
| 142 |
QCOMPARE(anim.totalDuration(), 50); |
| 143 |
} |
| 144 |
|
| 145 |
void tst_QAbstractAnimationJob::avoidJumpAtStart() |
| 146 |
{ |
| 147 |
TestableQAbstractAnimation anim; |
| 148 |
anim.setDuration(1000); |
| 149 |
|
| 150 |
/* |
| 151 |
the timer shouldn't actually start until we hit the event loop, |
| 152 |
so the sleep should have no effect |
| 153 |
*/ |
| 154 |
anim.start(); |
| 155 |
QTest::qSleep(300); |
| 156 |
QCoreApplication::processEvents(); |
| 157 |
QVERIFY(anim.currentTime() < 50); |
| 158 |
} |
| 159 |
|
| 160 |
void tst_QAbstractAnimationJob::avoidJumpAtStartWithStop() |
| 161 |
{ |
| 162 |
TestableQAbstractAnimation anim; |
| 163 |
anim.setDuration(1000); |
| 164 |
|
| 165 |
TestableQAbstractAnimation anim2; |
| 166 |
anim2.setDuration(1000); |
| 167 |
|
| 168 |
TestableQAbstractAnimation anim3; |
| 169 |
anim3.setDuration(1000); |
| 170 |
|
| 171 |
anim.start(); |
| 172 |
QTest::qWait(300); |
| 173 |
anim.stop(); |
| 174 |
|
| 175 |
/* |
| 176 |
same test as avoidJumpAtStart, but after there is a |
| 177 |
running animation that is stopped |
| 178 |
*/ |
| 179 |
anim2.start(); |
| 180 |
QTest::qSleep(300); |
| 181 |
anim3.start(); |
| 182 |
QCoreApplication::processEvents(); |
| 183 |
QVERIFY(anim2.currentTime() < 50); |
| 184 |
QVERIFY(anim3.currentTime() < 50); |
| 185 |
} |
| 186 |
|
| 187 |
void tst_QAbstractAnimationJob::avoidJumpAtStartWithRunning() |
| 188 |
{ |
| 189 |
TestableQAbstractAnimation anim; |
| 190 |
anim.setDuration(2000); |
| 191 |
|
| 192 |
TestableQAbstractAnimation anim2; |
| 193 |
anim2.setDuration(1000); |
| 194 |
|
| 195 |
TestableQAbstractAnimation anim3; |
| 196 |
anim3.setDuration(1000); |
| 197 |
|
| 198 |
anim.start(); |
| 199 |
QTest::qWait(300); //make sure timer has started |
| 200 |
|
| 201 |
/* |
| 202 |
same test as avoidJumpAtStart, but with an |
| 203 |
existing running animation |
| 204 |
*/ |
| 205 |
anim2.start(); |
| 206 |
QTest::qSleep(300); //force large delta for next tick |
| 207 |
anim3.start(); |
| 208 |
QCoreApplication::processEvents(); |
| 209 |
QVERIFY(anim2.currentTime() < 50); |
| 210 |
QVERIFY(anim3.currentTime() < 50); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
QTEST_MAIN(tst_QAbstractAnimationJob) |
| 215 |
|
| 216 |
#include "tst_qabstractanimationjob.moc" |