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 <QtCore/qabstractanimation.h>
31#include <QtCore/qanimationgroup.h>
32#include <QtTest>
33
34class tst_QAbstractAnimation : public QObject
35{
36 Q_OBJECT
37private 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
53class TestableQAbstractAnimation : public QAbstractAnimation
54{
55 Q_OBJECT
56
57public:
58 TestableQAbstractAnimation() : m_duration(10) {}
59 virtual ~TestableQAbstractAnimation() {};
60
61 int duration() const { return m_duration; }
62 virtual void updateCurrentTime(int) {}
63
64 void setDuration(int duration) { m_duration = duration; }
65private:
66 int m_duration;
67};
68
69class DummyQAnimationGroup : public QAnimationGroup
70{
71 Q_OBJECT
72public:
73 int duration() const { return 10; }
74 virtual void updateCurrentTime(int) {}
75};
76
77void tst_QAbstractAnimation::construction()
78{
79 TestableQAbstractAnimation anim;
80}
81
82void tst_QAbstractAnimation::destruction()
83{
84 TestableQAbstractAnimation *anim = new TestableQAbstractAnimation;
85 delete anim;
86
87 // Animations should stop when deleted
88 auto *stopWhenDeleted = new TestableQAbstractAnimation;
89 QAbstractAnimation::State lastOldState, lastNewState;
90 QObject::connect(sender: stopWhenDeleted, signal: &QAbstractAnimation::stateChanged,
91 slot: [&](QAbstractAnimation::State newState, QAbstractAnimation::State oldState) {
92 lastNewState = newState;
93 lastOldState = oldState;
94 });
95 stopWhenDeleted->start();
96 QCOMPARE(lastOldState, QAbstractAnimation::Stopped);
97 QCOMPARE(lastNewState, QAbstractAnimation::Running);
98 delete stopWhenDeleted;
99 QCOMPARE(lastOldState, QAbstractAnimation::Running);
100 QCOMPARE(lastNewState, QAbstractAnimation::Stopped);
101}
102
103void tst_QAbstractAnimation::currentLoop()
104{
105 TestableQAbstractAnimation anim;
106 QCOMPARE(anim.currentLoop(), 0);
107}
108
109void tst_QAbstractAnimation::currentLoopTime()
110{
111 TestableQAbstractAnimation anim;
112 QCOMPARE(anim.currentLoopTime(), 0);
113}
114
115void tst_QAbstractAnimation::currentTime()
116{
117 TestableQAbstractAnimation anim;
118 QCOMPARE(anim.currentTime(), 0);
119 anim.setCurrentTime(10);
120 QCOMPARE(anim.currentTime(), 10);
121}
122
123void tst_QAbstractAnimation::direction()
124{
125 TestableQAbstractAnimation anim;
126 QCOMPARE(anim.direction(), QAbstractAnimation::Forward);
127 anim.setDirection(QAbstractAnimation::Backward);
128 QCOMPARE(anim.direction(), QAbstractAnimation::Backward);
129 anim.setDirection(QAbstractAnimation::Forward);
130 QCOMPARE(anim.direction(), QAbstractAnimation::Forward);
131}
132
133void tst_QAbstractAnimation::group()
134{
135 TestableQAbstractAnimation *anim = new TestableQAbstractAnimation;
136 DummyQAnimationGroup group;
137 group.addAnimation(animation: anim);
138 QCOMPARE(anim->group(), &group);
139}
140
141void tst_QAbstractAnimation::loopCount()
142{
143 TestableQAbstractAnimation anim;
144 QCOMPARE(anim.loopCount(), 1);
145 anim.setLoopCount(10);
146 QCOMPARE(anim.loopCount(), 10);
147}
148
149void tst_QAbstractAnimation::state()
150{
151 TestableQAbstractAnimation anim;
152 QCOMPARE(anim.state(), QAbstractAnimation::Stopped);
153}
154
155void tst_QAbstractAnimation::totalDuration()
156{
157 TestableQAbstractAnimation anim;
158 QCOMPARE(anim.duration(), 10);
159 anim.setLoopCount(5);
160 QCOMPARE(anim.totalDuration(), 50);
161}
162
163void tst_QAbstractAnimation::avoidJumpAtStart()
164{
165 TestableQAbstractAnimation anim;
166 anim.setDuration(1000);
167
168 /*
169 the timer shouldn't actually start until we hit the event loop,
170 so the sleep should have no effect
171 */
172 anim.start();
173 QTest::qSleep(ms: 300);
174 QCoreApplication::processEvents();
175 QVERIFY(anim.currentTime() < 50);
176}
177
178void tst_QAbstractAnimation::avoidJumpAtStartWithStop()
179{
180 TestableQAbstractAnimation anim;
181 anim.setDuration(1000);
182
183 TestableQAbstractAnimation anim2;
184 anim2.setDuration(1000);
185
186 TestableQAbstractAnimation anim3;
187 anim3.setDuration(1000);
188
189 anim.start();
190 QTest::qWait(ms: 300);
191 anim.stop();
192
193 /*
194 same test as avoidJumpAtStart, but after there is a
195 running animation that is stopped
196 */
197 anim2.start();
198 QTest::qSleep(ms: 300);
199 anim3.start();
200 QCoreApplication::processEvents();
201 QVERIFY(anim2.currentTime() < 50);
202 QVERIFY(anim3.currentTime() < 50);
203}
204
205void tst_QAbstractAnimation::avoidJumpAtStartWithRunning()
206{
207 TestableQAbstractAnimation anim;
208 anim.setDuration(2000);
209
210 TestableQAbstractAnimation anim2;
211 anim2.setDuration(1000);
212
213 TestableQAbstractAnimation anim3;
214 anim3.setDuration(1000);
215
216 anim.start();
217 QTest::qWait(ms: 300); //make sure timer has started
218
219 /*
220 same test as avoidJumpAtStart, but with an
221 existing running animation
222 */
223 anim2.start();
224 QTest::qSleep(ms: 300); //force large delta for next tick
225 anim3.start();
226 QCoreApplication::processEvents();
227 QVERIFY(anim2.currentTime() < 50);
228 QVERIFY(anim3.currentTime() < 50);
229}
230
231
232QTEST_MAIN(tst_QAbstractAnimation)
233
234#include "tst_qabstractanimation.moc"
235

source code of qtbase/tests/auto/corelib/animation/qabstractanimation/tst_qabstractanimation.cpp