1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QtTest/QtTest>
44 #include <QtQml/private/qanimationgroupjob_p.h>
45 #include <QtQml/private/qsequentialanimationgroupjob_p.h>
46 #include <QtQml/private/qparallelanimationgroupjob_p.h>
48 Q_DECLARE_METATYPE(QAbstractAnimationJob::State)
50 class tst_QAnimationGroupJob : public QObject
59 void setCurrentTime();
63 void tst_QAnimationGroupJob::initTestCase()
65 qRegisterMetaType<QAbstractAnimationJob::State>("QAbstractAnimationJob::State");
68 void tst_QAnimationGroupJob::construction()
70 QSequentialAnimationGroupJob animationgroup;
73 class TestableGenericAnimation : public QAbstractAnimationJob
76 TestableGenericAnimation(int duration = 250) : m_duration(duration) {}
77 int duration() const { return m_duration; }
83 class UncontrolledAnimation : public QObject, public QAbstractAnimationJob
87 UncontrolledAnimation()
92 int duration() const { return -1; /* not time driven */ }
95 void timerEvent(QTimerEvent *event)
97 if (event->timerId() == id)
101 void updateRunning(bool running)
104 id = startTimer(500);
115 class StateChangeListener: public QAnimationJobChangeListener
118 virtual void animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State newState, QAbstractAnimationJob::State)
125 return states.count();
128 QList<QAbstractAnimationJob::State> states;
131 void tst_QAnimationGroupJob::emptyGroup()
133 QSequentialAnimationGroupJob group;
134 StateChangeListener groupStateChangedSpy;
135 group.addAnimationChangeListener(&groupStateChangedSpy, QAbstractAnimationJob::StateChange);
137 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
140 QCOMPARE(groupStateChangedSpy.count(), 2);
142 QCOMPARE(groupStateChangedSpy.states.at(0), QAnimationGroupJob::Running);
143 QCOMPARE(groupStateChangedSpy.states.at(1), QAnimationGroupJob::Stopped);
145 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
147 QTest::ignoreMessage(QtWarningMsg, "QAbstractAnimationJob::pause: Cannot pause a stopped animation");
150 QCOMPARE(groupStateChangedSpy.count(), 2);
151 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
155 QCOMPARE(groupStateChangedSpy.states.at(2),
156 QAnimationGroupJob::Running);
157 QCOMPARE(groupStateChangedSpy.states.at(3),
158 QAnimationGroupJob::Stopped);
160 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
164 QCOMPARE(groupStateChangedSpy.count(), 4);
165 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
168 void tst_QAnimationGroupJob::setCurrentTime()
170 // was originally sequence operating on same object/property
171 QSequentialAnimationGroupJob *sequence = new QSequentialAnimationGroupJob();
172 QAbstractAnimationJob *a1_s_o1 = new TestableGenericAnimation;
173 QAbstractAnimationJob *a2_s_o1 = new TestableGenericAnimation;
174 QAbstractAnimationJob *a3_s_o1 = new TestableGenericAnimation;
175 a2_s_o1->setLoopCount(3);
176 sequence->appendAnimation(a1_s_o1);
177 sequence->appendAnimation(a2_s_o1);
178 sequence->appendAnimation(a3_s_o1);
180 // was originally sequence operating on different object/properties
181 QAnimationGroupJob *sequence2 = new QSequentialAnimationGroupJob();
182 QAbstractAnimationJob *a1_s_o2 = new TestableGenericAnimation;
183 QAbstractAnimationJob *a1_s_o3 = new TestableGenericAnimation;
184 sequence2->appendAnimation(a1_s_o2);
185 sequence2->appendAnimation(a1_s_o3);
187 // was originally parallel operating on different object/properties
188 QAnimationGroupJob *parallel = new QParallelAnimationGroupJob();
189 QAbstractAnimationJob *a1_p_o1 = new TestableGenericAnimation;
190 QAbstractAnimationJob *a1_p_o2 = new TestableGenericAnimation;
191 QAbstractAnimationJob *a1_p_o3 = new TestableGenericAnimation;
192 a1_p_o2->setLoopCount(3);
193 parallel->appendAnimation(a1_p_o1);
194 parallel->appendAnimation(a1_p_o2);
195 parallel->appendAnimation(a1_p_o3);
197 QAbstractAnimationJob *notTimeDriven = new UncontrolledAnimation;
198 QCOMPARE(notTimeDriven->totalDuration(), -1);
200 QAbstractAnimationJob *loopsForever = new TestableGenericAnimation;
201 loopsForever->setLoopCount(-1);
202 QCOMPARE(loopsForever->totalDuration(), -1);
204 QParallelAnimationGroupJob group;
205 group.appendAnimation(sequence);
206 group.appendAnimation(sequence2);
207 group.appendAnimation(parallel);
208 group.appendAnimation(notTimeDriven);
209 group.appendAnimation(loopsForever);
212 group.setCurrentTime(1);
213 QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
214 QCOMPARE(sequence->state(), QAnimationGroupJob::Stopped);
215 QCOMPARE(a1_s_o1->state(), QAnimationGroupJob::Stopped);
216 QCOMPARE(sequence2->state(), QAnimationGroupJob::Stopped);
217 QCOMPARE(a1_s_o2->state(), QAnimationGroupJob::Stopped);
218 QCOMPARE(parallel->state(), QAnimationGroupJob::Stopped);
219 QCOMPARE(a1_p_o1->state(), QAnimationGroupJob::Stopped);
220 QCOMPARE(a1_p_o2->state(), QAnimationGroupJob::Stopped);
221 QCOMPARE(a1_p_o3->state(), QAnimationGroupJob::Stopped);
222 QCOMPARE(notTimeDriven->state(), QAnimationGroupJob::Stopped);
223 QCOMPARE(loopsForever->state(), QAnimationGroupJob::Stopped);
225 QCOMPARE(group.currentLoopTime(), 1);
226 QCOMPARE(sequence->currentLoopTime(), 1);
227 QCOMPARE(a1_s_o1->currentLoopTime(), 1);
228 QCOMPARE(a2_s_o1->currentLoopTime(), 0);
229 QCOMPARE(a3_s_o1->currentLoopTime(), 0);
230 QCOMPARE(a1_s_o2->currentLoopTime(), 1);
231 QCOMPARE(a1_s_o3->currentLoopTime(), 0);
232 QCOMPARE(a1_p_o1->currentLoopTime(), 1);
233 QCOMPARE(a1_p_o2->currentLoopTime(), 1);
234 QCOMPARE(a1_p_o3->currentLoopTime(), 1);
235 QCOMPARE(notTimeDriven->currentLoopTime(), 1);
236 QCOMPARE(loopsForever->currentLoopTime(), 1);
238 // Current time = 250
239 group.setCurrentTime(250);
240 QCOMPARE(group.currentLoopTime(), 250);
241 QCOMPARE(sequence->currentLoopTime(), 250);
242 QCOMPARE(a1_s_o1->currentLoopTime(), 250);
243 QCOMPARE(a2_s_o1->currentLoopTime(), 0);
244 QCOMPARE(a3_s_o1->currentLoopTime(), 0);
245 QCOMPARE(a1_s_o2->currentLoopTime(), 250);
246 QCOMPARE(a1_s_o3->currentLoopTime(), 0);
247 QCOMPARE(a1_p_o1->currentLoopTime(), 250);
248 QCOMPARE(a1_p_o2->currentLoopTime(), 0);
249 QCOMPARE(a1_p_o2->currentLoop(), 1);
250 QCOMPARE(a1_p_o3->currentLoopTime(), 250);
251 QCOMPARE(notTimeDriven->currentLoopTime(), 250);
252 QCOMPARE(loopsForever->currentLoopTime(), 0);
253 QCOMPARE(loopsForever->currentLoop(), 1);
254 QCOMPARE(sequence->currentAnimation(), a2_s_o1);
256 // Current time = 251
257 group.setCurrentTime(251);
258 QCOMPARE(group.currentLoopTime(), 251);
259 QCOMPARE(sequence->currentLoopTime(), 251);
260 QCOMPARE(a1_s_o1->currentLoopTime(), 250);
261 QCOMPARE(a2_s_o1->currentLoopTime(), 1);
262 QCOMPARE(a2_s_o1->currentLoop(), 0);
263 QCOMPARE(a3_s_o1->currentLoopTime(), 0);
264 QCOMPARE(sequence2->currentLoopTime(), 251);
265 QCOMPARE(a1_s_o2->currentLoopTime(), 250);
266 QCOMPARE(a1_s_o3->currentLoopTime(), 1);
267 QCOMPARE(a1_p_o1->currentLoopTime(), 250);
268 QCOMPARE(a1_p_o2->currentLoopTime(), 1);
269 QCOMPARE(a1_p_o2->currentLoop(), 1);
270 QCOMPARE(a1_p_o3->currentLoopTime(), 250);
271 QCOMPARE(notTimeDriven->currentLoopTime(), 251);
272 QCOMPARE(loopsForever->currentLoopTime(), 1);
273 QCOMPARE(sequence->currentAnimation(), a2_s_o1);
276 void tst_QAnimationGroupJob::addChildTwice()
278 QAbstractAnimationJob *subGroup;
279 QAbstractAnimationJob *subGroup2;
280 QAnimationGroupJob *parent = new QSequentialAnimationGroupJob();
282 subGroup = new QAbstractAnimationJob;
283 parent->appendAnimation(subGroup);
284 parent->appendAnimation(subGroup);
285 QVERIFY(parent->firstChild() && !parent->firstChild()->nextSibling());
289 QVERIFY(!parent->firstChild());
291 // adding the same item twice to a group will remove the item from its current position
292 // and append it to the end
293 subGroup = new QAbstractAnimationJob;
294 parent->appendAnimation(subGroup);
295 subGroup2 = new QAbstractAnimationJob;
296 parent->appendAnimation(subGroup2);
298 QCOMPARE(parent->firstChild(), subGroup);
299 QCOMPARE(parent->lastChild(), subGroup2);
301 parent->appendAnimation(subGroup);
303 QCOMPARE(parent->firstChild(), subGroup2);
304 QCOMPARE(parent->lastChild(), subGroup);
309 QTEST_MAIN(tst_QAnimationGroupJob)
310 #include "tst_qanimationgroupjob.moc"