Rename QDeclarative symbols to QQuick and QQml
[profile/ivi/qtdeclarative.git] / tests / auto / qml / animation / qanimationgroupjob / tst_qanimationgroupjob.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #include <QtQml/private/qanimationgroupjob_p.h>
45 #include <QtQml/private/qsequentialanimationgroupjob_p.h>
46 #include <QtQml/private/qparallelanimationgroupjob_p.h>
47
48 Q_DECLARE_METATYPE(QAbstractAnimationJob::State)
49
50 class tst_QAnimationGroupJob : public QObject
51 {
52     Q_OBJECT
53 public Q_SLOTS:
54     void initTestCase();
55
56 private slots:
57     void construction();
58     void emptyGroup();
59     void setCurrentTime();
60     void addChildTwice();
61 };
62
63 void tst_QAnimationGroupJob::initTestCase()
64 {
65     qRegisterMetaType<QAbstractAnimationJob::State>("QAbstractAnimationJob::State");
66 }
67
68 void tst_QAnimationGroupJob::construction()
69 {
70     QSequentialAnimationGroupJob animationgroup;
71 }
72
73 class TestableGenericAnimation : public QAbstractAnimationJob
74 {
75 public:
76     TestableGenericAnimation(int duration = 250) : m_duration(duration) {}
77     int duration() const { return m_duration; }
78
79 private:
80     int m_duration;
81 };
82
83 class UncontrolledAnimation : public QObject, public QAbstractAnimationJob
84 {
85     Q_OBJECT
86 public:
87     UncontrolledAnimation()
88         : id(0)
89     {
90     }
91
92     int duration() const { return -1; /* not time driven */ }
93
94 protected:
95     void timerEvent(QTimerEvent *event)
96     {
97         if (event->timerId() == id)
98             stop();
99     }
100
101     void updateRunning(bool running)
102     {
103         if (running) {
104             id = startTimer(500);
105         } else {
106             killTimer(id);
107             id = 0;
108         }
109     }
110
111 private:
112     int id;
113 };
114
115 class StateChangeListener: public QAnimationJobChangeListener
116 {
117 public:
118     virtual void animationStateChanged(QAbstractAnimationJob *, QAbstractAnimationJob::State newState, QAbstractAnimationJob::State)
119     {
120         states << newState;
121     }
122
123     int count()
124     {
125         return states.count();
126     }
127
128     QList<QAbstractAnimationJob::State> states;
129 };
130
131 void tst_QAnimationGroupJob::emptyGroup()
132 {
133     QSequentialAnimationGroupJob group;
134     StateChangeListener groupStateChangedSpy;
135     group.addAnimationChangeListener(&groupStateChangedSpy, QAbstractAnimationJob::StateChange);
136
137     QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
138     group.start();
139
140     QCOMPARE(groupStateChangedSpy.count(), 2);
141
142     QCOMPARE(groupStateChangedSpy.states.at(0), QAnimationGroupJob::Running);
143     QCOMPARE(groupStateChangedSpy.states.at(1), QAnimationGroupJob::Stopped);
144
145     QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
146
147     QTest::ignoreMessage(QtWarningMsg, "QAbstractAnimationJob::pause: Cannot pause a stopped animation");
148     group.pause();
149
150     QCOMPARE(groupStateChangedSpy.count(), 2);
151     QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
152
153     group.start();
154
155     QCOMPARE(groupStateChangedSpy.states.at(2),
156              QAnimationGroupJob::Running);
157     QCOMPARE(groupStateChangedSpy.states.at(3),
158              QAnimationGroupJob::Stopped);
159
160     QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
161
162     group.stop();
163
164     QCOMPARE(groupStateChangedSpy.count(), 4);
165     QCOMPARE(group.state(), QAnimationGroupJob::Stopped);
166 }
167
168 void tst_QAnimationGroupJob::setCurrentTime()
169 {
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);
179
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);
186
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);
196
197     QAbstractAnimationJob *notTimeDriven = new UncontrolledAnimation;
198     QCOMPARE(notTimeDriven->totalDuration(), -1);
199
200     QAbstractAnimationJob *loopsForever = new TestableGenericAnimation;
201     loopsForever->setLoopCount(-1);
202     QCOMPARE(loopsForever->totalDuration(), -1);
203
204     QParallelAnimationGroupJob group;
205     group.appendAnimation(sequence);
206     group.appendAnimation(sequence2);
207     group.appendAnimation(parallel);
208     group.appendAnimation(notTimeDriven);
209     group.appendAnimation(loopsForever);
210
211     // Current time = 1
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);
224
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);
237
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);
255
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);
274 }
275
276 void tst_QAnimationGroupJob::addChildTwice()
277 {
278     QAbstractAnimationJob *subGroup;
279     QAbstractAnimationJob *subGroup2;
280     QAnimationGroupJob *parent = new QSequentialAnimationGroupJob();
281
282     subGroup = new QAbstractAnimationJob;
283     parent->appendAnimation(subGroup);
284     parent->appendAnimation(subGroup);
285     QVERIFY(parent->firstChild() && !parent->firstChild()->nextSibling());
286
287     parent->clear();
288
289     QVERIFY(!parent->firstChild());
290
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);
297
298     QCOMPARE(parent->firstChild(), subGroup);
299     QCOMPARE(parent->lastChild(), subGroup2);
300
301     parent->appendAnimation(subGroup);
302
303     QCOMPARE(parent->firstChild(), subGroup2);
304     QCOMPARE(parent->lastChild(), subGroup);
305
306     delete parent;
307 }
308
309 QTEST_MAIN(tst_QAnimationGroupJob)
310 #include "tst_qanimationgroupjob.moc"