author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include <QtTest/QtTest> |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
43 |
#include "../../shared/util.h" |
0 | 44 |
|
45 |
#include <QtCore/qpropertyanimation.h> |
|
46 |
#include <QtCore/qvariantanimation.h> |
|
47 |
#include <QtGui/qwidget.h> |
|
48 |
||
49 |
//TESTED_CLASS=QPropertyAnimation |
|
50 |
//TESTED_FILES= |
|
51 |
||
52 |
class UncontrolledAnimation : public QPropertyAnimation |
|
53 |
{ |
|
54 |
Q_OBJECT |
|
55 |
public: |
|
56 |
int duration() const { return -1; /* not time driven */ } |
|
57 |
||
58 |
protected: |
|
59 |
void updateCurrentTime(int currentTime) |
|
60 |
{ |
|
61 |
QPropertyAnimation::updateCurrentTime(currentTime); |
|
62 |
if (currentTime >= QPropertyAnimation::duration() || currentLoop() >= 1) |
|
63 |
stop(); |
|
64 |
} |
|
65 |
}; |
|
66 |
||
67 |
class MyObject : public QObject |
|
68 |
{ |
|
69 |
Q_OBJECT |
|
70 |
Q_PROPERTY(qreal x READ x WRITE setX) |
|
71 |
public: |
|
72 |
MyObject() : m_x(0) { } |
|
73 |
qreal x() const { return m_x; } |
|
74 |
void setX(qreal x) { m_x = x; } |
|
75 |
private: |
|
76 |
qreal m_x; |
|
77 |
}; |
|
78 |
||
79 |
class DummyPropertyAnimation : public QPropertyAnimation |
|
80 |
{ |
|
81 |
public: |
|
82 |
DummyPropertyAnimation(QObject *parent = 0) : QPropertyAnimation(parent) |
|
83 |
{ |
|
84 |
setTargetObject(&o); |
|
85 |
this->setPropertyName("x"); |
|
86 |
setEndValue(100); |
|
87 |
} |
|
88 |
||
89 |
MyObject o; |
|
90 |
}; |
|
91 |
||
92 |
||
93 |
class tst_QPropertyAnimation : public QObject |
|
94 |
{ |
|
95 |
Q_OBJECT |
|
96 |
public: |
|
97 |
tst_QPropertyAnimation(); |
|
98 |
virtual ~tst_QPropertyAnimation(); |
|
99 |
||
100 |
public Q_SLOTS: |
|
101 |
void init(); |
|
102 |
void cleanup(); |
|
103 |
||
104 |
private slots: |
|
105 |
void construction(); |
|
106 |
void setCurrentTime_data(); |
|
107 |
void setCurrentTime(); |
|
108 |
void statesAndSignals_data(); |
|
109 |
void statesAndSignals(); |
|
110 |
void deletion1(); |
|
111 |
void deletion2(); |
|
112 |
void deletion3(); |
|
113 |
void duration0(); |
|
114 |
void noStartValue(); |
|
115 |
void noStartValueWithLoop(); |
|
116 |
void startWhenAnotherIsRunning(); |
|
117 |
void easingcurve_data(); |
|
118 |
void easingcurve(); |
|
119 |
void startWithoutStartValue(); |
|
120 |
void startBackwardWithoutEndValue(); |
|
121 |
void playForwardBackward(); |
|
122 |
void interpolated(); |
|
123 |
void setStartEndValues_data(); |
|
124 |
void setStartEndValues(); |
|
125 |
void zeroDurationStart(); |
|
126 |
void operationsInStates_data(); |
|
127 |
void operationsInStates(); |
|
128 |
void oneKeyValue(); |
|
129 |
void updateOnSetKeyValues(); |
|
130 |
void restart(); |
|
131 |
void valueChanged(); |
|
132 |
void twoAnimations(); |
|
133 |
void deletedInUpdateCurrentTime(); |
|
134 |
void totalDuration(); |
|
135 |
}; |
|
136 |
||
137 |
tst_QPropertyAnimation::tst_QPropertyAnimation() |
|
138 |
{ |
|
139 |
} |
|
140 |
||
141 |
tst_QPropertyAnimation::~tst_QPropertyAnimation() |
|
142 |
{ |
|
143 |
} |
|
144 |
||
145 |
void tst_QPropertyAnimation::init() |
|
146 |
{ |
|
147 |
qRegisterMetaType<QAbstractAnimation::State>("QAbstractAnimation::State"); |
|
148 |
qRegisterMetaType<QAbstractAnimation::DeletionPolicy>("QAbstractAnimation::DeletionPolicy"); |
|
149 |
} |
|
150 |
||
151 |
void tst_QPropertyAnimation::cleanup() |
|
152 |
{ |
|
153 |
} |
|
154 |
||
155 |
class AnimationObject : public QObject |
|
156 |
{ |
|
157 |
Q_OBJECT |
|
158 |
Q_PROPERTY(int value READ value WRITE setValue) |
|
159 |
Q_PROPERTY(qreal realValue READ realValue WRITE setRealValue) |
|
160 |
public: |
|
161 |
AnimationObject(int startValue = 0) |
|
162 |
: v(startValue), rv(startValue) |
|
163 |
{ } |
|
164 |
||
165 |
int value() const { return v; } |
|
166 |
void setValue(int value) { v = value; } |
|
167 |
||
168 |
qreal realValue() const { return rv; } |
|
169 |
void setRealValue(qreal value) { rv = value; } |
|
170 |
||
171 |
int v; |
|
172 |
qreal rv; |
|
173 |
}; |
|
174 |
||
175 |
||
176 |
void tst_QPropertyAnimation::construction() |
|
177 |
{ |
|
178 |
QPropertyAnimation panimation; |
|
179 |
} |
|
180 |
||
181 |
void tst_QPropertyAnimation::setCurrentTime_data() |
|
182 |
{ |
|
183 |
QTest::addColumn<int>("duration"); |
|
184 |
QTest::addColumn<int>("loopCount"); |
|
185 |
QTest::addColumn<int>("currentTime"); |
|
186 |
QTest::addColumn<int>("testCurrentTime"); |
|
187 |
QTest::addColumn<int>("testCurrentLoop"); |
|
188 |
||
189 |
QTest::newRow("-1") << -1 << 1 << 0 << 0 << 0; |
|
190 |
QTest::newRow("0") << 0 << 1 << 0 << 0 << 0; |
|
191 |
QTest::newRow("1") << 0 << 1 << 1 << 0 << 0; |
|
192 |
QTest::newRow("2") << 0 << 2 << 1 << 0 << 0; |
|
193 |
QTest::newRow("3") << 1 << 1 << 0 << 0 << 0; |
|
194 |
QTest::newRow("4") << 1 << 1 << 1 << 1 << 0; |
|
195 |
QTest::newRow("5") << 1 << 2 << 1 << 0 << 1; |
|
196 |
QTest::newRow("6") << 1 << 2 << 2 << 1 << 1; |
|
197 |
QTest::newRow("7") << 1 << 2 << 3 << 1 << 1; |
|
198 |
QTest::newRow("8") << 1 << 3 << 2 << 0 << 2; |
|
199 |
QTest::newRow("9") << 1 << 3 << 3 << 1 << 2; |
|
200 |
QTest::newRow("a") << 10 << 1 << 0 << 0 << 0; |
|
201 |
QTest::newRow("b") << 10 << 1 << 1 << 1 << 0; |
|
202 |
QTest::newRow("c") << 10 << 1 << 10 << 10 << 0; |
|
203 |
QTest::newRow("d") << 10 << 2 << 10 << 0 << 1; |
|
204 |
QTest::newRow("e") << 10 << 2 << 11 << 1 << 1; |
|
205 |
QTest::newRow("f") << 10 << 2 << 20 << 10 << 1; |
|
206 |
QTest::newRow("g") << 10 << 2 << 21 << 10 << 1; |
|
207 |
QTest::newRow("negloop 0") << 10 << -1 << 0 << 0 << 0; |
|
208 |
QTest::newRow("negloop 1") << 10 << -1 << 10 << 0 << 1; |
|
209 |
QTest::newRow("negloop 2") << 10 << -1 << 15 << 5 << 1; |
|
210 |
QTest::newRow("negloop 3") << 10 << -1 << 20 << 0 << 2; |
|
211 |
QTest::newRow("negloop 4") << 10 << -1 << 30 << 0 << 3; |
|
212 |
} |
|
213 |
||
214 |
void tst_QPropertyAnimation::setCurrentTime() |
|
215 |
{ |
|
216 |
QFETCH(int, duration); |
|
217 |
QFETCH(int, loopCount); |
|
218 |
QFETCH(int, currentTime); |
|
219 |
QFETCH(int, testCurrentTime); |
|
220 |
QFETCH(int, testCurrentLoop); |
|
221 |
||
222 |
QPropertyAnimation animation; |
|
223 |
if (duration < 0) |
|
224 |
QTest::ignoreMessage(QtWarningMsg, "QVariantAnimation::setDuration: cannot set a negative duration"); |
|
225 |
animation.setDuration(duration); |
|
226 |
animation.setLoopCount(loopCount); |
|
227 |
animation.setCurrentTime(currentTime); |
|
228 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
QCOMPARE(animation.currentLoopTime(), testCurrentTime); |
0 | 230 |
QCOMPARE(animation.currentLoop(), testCurrentLoop); |
231 |
} |
|
232 |
||
233 |
void tst_QPropertyAnimation::statesAndSignals_data() |
|
234 |
{ |
|
235 |
QTest::addColumn<bool>("uncontrolled"); |
|
236 |
QTest::newRow("normal animation") << false; |
|
237 |
QTest::newRow("animation with undefined duration") << true; |
|
238 |
} |
|
239 |
||
240 |
void tst_QPropertyAnimation::statesAndSignals() |
|
241 |
{ |
|
242 |
QFETCH(bool, uncontrolled); |
|
243 |
QPropertyAnimation *anim; |
|
244 |
if (uncontrolled) |
|
245 |
anim = new UncontrolledAnimation; |
|
246 |
else |
|
247 |
anim = new DummyPropertyAnimation; |
|
248 |
anim->setDuration(100); |
|
249 |
||
250 |
QSignalSpy finishedSpy(anim, SIGNAL(finished())); |
|
251 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
252 |
QSignalSpy currentLoopSpy(anim, SIGNAL(currentLoopChanged(int))); |
|
253 |
||
254 |
anim->setCurrentTime(1); |
|
255 |
anim->setCurrentTime(100); |
|
256 |
QCOMPARE(finishedSpy.count(), 0); |
|
257 |
QCOMPARE(runningSpy.count(), 0); |
|
258 |
QCOMPARE(currentLoopSpy.count(), 0); |
|
259 |
QCOMPARE(anim->state(), QAnimationGroup::Stopped); |
|
260 |
||
261 |
anim->setLoopCount(3); |
|
262 |
anim->setCurrentTime(101); |
|
263 |
||
264 |
if (uncontrolled) |
|
265 |
QSKIP("Uncontrolled animations don't handle looping", SkipSingle); |
|
266 |
||
267 |
QCOMPARE(currentLoopSpy.count(), 1); |
|
268 |
QCOMPARE(anim->currentLoop(), 1); |
|
269 |
||
270 |
anim->setCurrentTime(0); |
|
271 |
QCOMPARE(currentLoopSpy.count(), 2); |
|
272 |
QCOMPARE(anim->currentLoop(), 0); |
|
273 |
||
274 |
anim->start(); |
|
275 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
276 |
QCOMPARE(runningSpy.count(), 1); //anim must have started |
|
277 |
QCOMPARE(anim->currentLoop(), 0); |
|
278 |
runningSpy.clear(); |
|
279 |
||
280 |
anim->stop(); |
|
281 |
QCOMPARE(anim->state(), QAnimationGroup::Stopped); |
|
282 |
QCOMPARE(runningSpy.count(), 1); //anim must have stopped |
|
283 |
QCOMPARE(finishedSpy.count(), 0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
284 |
QCOMPARE(anim->currentLoopTime(), 0); |
0 | 285 |
QCOMPARE(anim->currentLoop(), 0); |
286 |
QCOMPARE(currentLoopSpy.count(), 2); |
|
287 |
runningSpy.clear(); |
|
288 |
||
289 |
anim->start(); |
|
290 |
QTest::qWait(1000); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
291 |
QTRY_COMPARE(anim->state(), QAnimationGroup::Stopped); |
0 | 292 |
QCOMPARE(runningSpy.count(), 2); //started and stopped again |
293 |
runningSpy.clear(); |
|
294 |
QCOMPARE(finishedSpy.count(), 1); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
QCOMPARE(anim->currentLoopTime(), 100); |
0 | 296 |
QCOMPARE(anim->currentLoop(), 2); |
297 |
QCOMPARE(currentLoopSpy.count(), 4); |
|
298 |
||
299 |
anim->start(); // auto-rewinds |
|
300 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
301 |
QCOMPARE(anim->currentTime(), 0); |
|
302 |
QCOMPARE(anim->currentLoop(), 0); |
|
303 |
QCOMPARE(currentLoopSpy.count(), 5); |
|
304 |
QCOMPARE(runningSpy.count(), 1); // anim has started |
|
305 |
QCOMPARE(finishedSpy.count(), 1); |
|
306 |
QCOMPARE(anim->currentLoop(), 0); |
|
307 |
runningSpy.clear(); |
|
308 |
||
309 |
QTest::qWait(1000); |
|
310 |
||
311 |
QCOMPARE(currentLoopSpy.count(), 7); |
|
312 |
QCOMPARE(anim->state(), QAnimationGroup::Stopped); |
|
313 |
QCOMPARE(anim->currentLoop(), 2); |
|
314 |
QCOMPARE(runningSpy.count(), 1); // anim has stopped |
|
315 |
QCOMPARE(finishedSpy.count(), 2); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
316 |
QCOMPARE(anim->currentLoopTime(), 100); |
0 | 317 |
|
318 |
delete anim; |
|
319 |
} |
|
320 |
||
321 |
void tst_QPropertyAnimation::deletion1() |
|
322 |
{ |
|
323 |
QObject *object = new QWidget; |
|
324 |
QPointer<QPropertyAnimation> anim = new QPropertyAnimation(object, "minimumWidth"); |
|
325 |
||
326 |
//test that the animation is deleted correctly depending of the deletion flag passed in start() |
|
327 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
328 |
QSignalSpy finishedSpy(anim, SIGNAL(finished())); |
|
329 |
anim->setStartValue(10); |
|
330 |
anim->setEndValue(20); |
|
331 |
anim->setDuration(200); |
|
332 |
anim->start(); |
|
333 |
QCOMPARE(runningSpy.count(), 1); |
|
334 |
QCOMPARE(finishedSpy.count(), 0); |
|
335 |
||
336 |
QVERIFY(anim); |
|
337 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
338 |
QTest::qWait(100); |
|
339 |
QVERIFY(anim); |
|
340 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
341 |
QTest::qWait(150); |
|
342 |
QVERIFY(anim); //The animation should not have been deleted |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
343 |
QTRY_COMPARE(anim->state(), QAnimationGroup::Stopped); |
0 | 344 |
QCOMPARE(runningSpy.count(), 2); |
345 |
QCOMPARE(finishedSpy.count(), 1); |
|
346 |
||
347 |
anim->start(QVariantAnimation::DeleteWhenStopped); |
|
348 |
QVERIFY(anim); |
|
349 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
350 |
QTest::qWait(100); |
|
351 |
QVERIFY(anim); |
|
352 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
353 |
QTest::qWait(150); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
354 |
QTRY_COMPARE(runningSpy.count(), 4); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
355 |
QCOMPARE(finishedSpy.count(), 2); |
0 | 356 |
QVERIFY(!anim); //The animation must have been deleted |
357 |
delete object; |
|
358 |
} |
|
359 |
||
360 |
void tst_QPropertyAnimation::deletion2() |
|
361 |
{ |
|
362 |
//test that the animation get deleted if the object is deleted |
|
363 |
QObject *object = new QWidget; |
|
364 |
QPointer<QPropertyAnimation> anim = new QPropertyAnimation(object,"minimumWidth"); |
|
365 |
anim->setStartValue(10); |
|
366 |
anim->setEndValue(20); |
|
367 |
anim->setDuration(200); |
|
368 |
||
369 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
370 |
QSignalSpy finishedSpy(anim, SIGNAL(finished())); |
|
371 |
||
372 |
anim->setStartValue(10); |
|
373 |
anim->setEndValue(20); |
|
374 |
anim->setDuration(200); |
|
375 |
anim->start(); |
|
376 |
||
377 |
QTest::qWait(50); |
|
378 |
QVERIFY(anim); |
|
379 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
380 |
||
381 |
QCOMPARE(runningSpy.count(), 1); |
|
382 |
QCOMPARE(finishedSpy.count(), 0); |
|
383 |
||
384 |
//we can't call deletaLater directly because the delete would only happen in the next loop of _this_ event loop |
|
385 |
QTimer::singleShot(0, object, SLOT(deleteLater())); |
|
386 |
QTest::qWait(50); |
|
387 |
||
388 |
QVERIFY(anim->targetObject() == 0); |
|
389 |
} |
|
390 |
||
391 |
void tst_QPropertyAnimation::deletion3() |
|
392 |
{ |
|
393 |
//test that the stopped signal is emit when the animation is destroyed |
|
394 |
QObject *object = new QWidget; |
|
395 |
QPropertyAnimation *anim = new QPropertyAnimation(object,"minimumWidth"); |
|
396 |
anim->setStartValue(10); |
|
397 |
anim->setEndValue(20); |
|
398 |
anim->setDuration(200); |
|
399 |
||
400 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
401 |
QSignalSpy finishedSpy(anim, SIGNAL(finished())); |
|
402 |
anim->start(); |
|
403 |
||
404 |
QTest::qWait(50); |
|
405 |
QCOMPARE(anim->state(), QAnimationGroup::Running); |
|
406 |
QCOMPARE(runningSpy.count(), 1); |
|
407 |
QCOMPARE(finishedSpy.count(), 0); |
|
408 |
delete anim; |
|
409 |
QCOMPARE(runningSpy.count(), 2); |
|
410 |
QCOMPARE(finishedSpy.count(), 0); |
|
411 |
} |
|
412 |
||
413 |
void tst_QPropertyAnimation::duration0() |
|
414 |
{ |
|
415 |
QObject o; |
|
416 |
o.setProperty("ole", 42); |
|
417 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
418 |
||
419 |
QPropertyAnimation animation(&o, "ole"); |
|
420 |
animation.setEndValue(43); |
|
421 |
QVERIFY(!animation.currentValue().isValid()); |
|
422 |
QCOMPARE(animation.currentValue().toInt(), 0); |
|
423 |
animation.setStartValue(42); |
|
424 |
QVERIFY(animation.currentValue().isValid()); |
|
425 |
QCOMPARE(animation.currentValue().toInt(), 42); |
|
426 |
||
427 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
428 |
animation.setDuration(0); |
|
429 |
QCOMPARE(animation.currentValue().toInt(), 43); //it is at the end |
|
430 |
animation.start(); |
|
431 |
QCOMPARE(animation.state(), QAnimationGroup::Stopped); |
|
432 |
QCOMPARE(animation.currentTime(), 0); |
|
433 |
QCOMPARE(o.property("ole").toInt(), 43); |
|
434 |
} |
|
435 |
||
436 |
class StartValueTester : public QObject |
|
437 |
{ |
|
438 |
Q_OBJECT |
|
439 |
Q_PROPERTY(int ole READ ole WRITE setOle) |
|
440 |
public: |
|
441 |
StartValueTester() : o(0) { } |
|
442 |
int ole() const { return o; } |
|
443 |
void setOle(int v) { o = v; values << v; } |
|
444 |
||
445 |
int o; |
|
446 |
QList<int> values; |
|
447 |
}; |
|
448 |
||
449 |
void tst_QPropertyAnimation::noStartValue() |
|
450 |
{ |
|
451 |
StartValueTester o; |
|
452 |
o.setProperty("ole", 42); |
|
453 |
o.values.clear(); |
|
454 |
||
455 |
QPropertyAnimation a(&o, "ole"); |
|
456 |
a.setEndValue(420); |
|
457 |
a.setDuration(250); |
|
458 |
a.start(); |
|
459 |
||
460 |
QTest::qWait(300); |
|
461 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
462 |
QTRY_COMPARE(o.values.first(), 42); |
0 | 463 |
QCOMPARE(o.values.last(), 420); |
464 |
} |
|
465 |
||
466 |
void tst_QPropertyAnimation::noStartValueWithLoop() |
|
467 |
{ |
|
468 |
StartValueTester o; |
|
469 |
o.setProperty("ole", 42); |
|
470 |
o.values.clear(); |
|
471 |
||
472 |
QPropertyAnimation a(&o, "ole"); |
|
473 |
a.setEndValue(420); |
|
474 |
a.setDuration(250); |
|
475 |
a.setLoopCount(2); |
|
476 |
a.start(); |
|
477 |
||
478 |
a.setCurrentTime(250); |
|
479 |
QCOMPARE(o.values.first(), 42); |
|
480 |
QCOMPARE(a.currentValue().toInt(), 42); |
|
481 |
QCOMPARE(o.values.last(), 42); |
|
482 |
||
483 |
a.setCurrentTime(500); |
|
484 |
QCOMPARE(a.currentValue().toInt(), 420); |
|
485 |
} |
|
486 |
||
487 |
void tst_QPropertyAnimation::startWhenAnotherIsRunning() |
|
488 |
{ |
|
489 |
StartValueTester o; |
|
490 |
o.setProperty("ole", 42); |
|
491 |
o.values.clear(); |
|
492 |
||
493 |
{ |
|
494 |
//normal case: the animation finishes and is deleted |
|
495 |
QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole"); |
|
496 |
anim->setEndValue(100); |
|
497 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
498 |
anim->start(QVariantAnimation::DeleteWhenStopped); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
499 |
QTest::qWait(anim->duration() + 100); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
500 |
QTRY_COMPARE(runningSpy.count(), 2); //started and then stopped |
0 | 501 |
QVERIFY(!anim); |
502 |
} |
|
503 |
||
504 |
{ |
|
505 |
QPointer<QVariantAnimation> anim = new QPropertyAnimation(&o, "ole"); |
|
506 |
anim->setEndValue(100); |
|
507 |
QSignalSpy runningSpy(anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
508 |
anim->start(QVariantAnimation::DeleteWhenStopped); |
|
509 |
QTest::qWait(anim->duration()/2); |
|
510 |
QPointer<QVariantAnimation> anim2 = new QPropertyAnimation(&o, "ole"); |
|
511 |
anim2->setEndValue(100); |
|
512 |
QCOMPARE(runningSpy.count(), 1); |
|
513 |
QCOMPARE(anim->state(), QVariantAnimation::Running); |
|
514 |
||
515 |
//anim2 will interrupt anim1 |
|
516 |
QMetaObject::invokeMethod(anim2, "start", Qt::QueuedConnection, Q_ARG(QAbstractAnimation::DeletionPolicy, QVariantAnimation::DeleteWhenStopped)); |
|
517 |
QTest::qWait(50); |
|
518 |
QVERIFY(!anim); //anim should have been deleted |
|
519 |
QVERIFY(anim2); |
|
520 |
QTest::qWait(anim2->duration()); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
521 |
QTRY_VERIFY(!anim2); //anim2 is finished: it should have been deleted by now |
0 | 522 |
QVERIFY(!anim); |
523 |
} |
|
524 |
||
525 |
} |
|
526 |
||
527 |
// copy from easing.cpp in case that function changes definition |
|
528 |
static qreal easeInOutBack(qreal t) |
|
529 |
{ |
|
530 |
qreal s = 1.70158; |
|
531 |
qreal t_adj = 2.0f * (qreal)t; |
|
532 |
if (t_adj < 1) { |
|
533 |
s *= 1.525f; |
|
534 |
return 1.0/2*(t_adj*t_adj*((s+1)*t_adj - s)); |
|
535 |
} else { |
|
536 |
t_adj -= 2; |
|
537 |
s *= 1.525f; |
|
538 |
return 1.0/2*(t_adj*t_adj*((s+1)*t_adj + s) + 2); |
|
539 |
} |
|
540 |
} |
|
541 |
||
542 |
void tst_QPropertyAnimation::easingcurve_data() |
|
543 |
{ |
|
544 |
QTest::addColumn<int>("currentTime"); |
|
545 |
QTest::addColumn<int>("expectedvalue"); |
|
546 |
||
547 |
QTest::newRow("interpolation1") << 0 << 0; |
|
548 |
QTest::newRow("interpolation2") << 1000 << 1000; |
|
549 |
QTest::newRow("extrapolationbelow") << 250 << -99; |
|
550 |
QTest::newRow("extrapolationabove") << 750 << 1099; |
|
551 |
} |
|
552 |
||
553 |
void tst_QPropertyAnimation::easingcurve() |
|
554 |
{ |
|
555 |
QFETCH(int, currentTime); |
|
556 |
QFETCH(int, expectedvalue); |
|
557 |
QObject o; |
|
558 |
o.setProperty("ole", 42); |
|
559 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
560 |
||
561 |
QPropertyAnimation pAnimation(&o, "ole"); |
|
562 |
pAnimation.setStartValue(0); |
|
563 |
pAnimation.setEndValue(1000); |
|
564 |
pAnimation.setDuration(1000); |
|
565 |
||
566 |
// this easingcurve assumes that we extrapolate before startValue and after endValue |
|
567 |
QEasingCurve easingCurve; |
|
568 |
easingCurve.setCustomType(easeInOutBack); |
|
569 |
pAnimation.setEasingCurve(easingCurve); |
|
570 |
pAnimation.start(); |
|
571 |
pAnimation.pause(); |
|
572 |
pAnimation.setCurrentTime(currentTime); |
|
573 |
QCOMPARE(o.property("ole").toInt(), expectedvalue); |
|
574 |
} |
|
575 |
||
576 |
void tst_QPropertyAnimation::startWithoutStartValue() |
|
577 |
{ |
|
578 |
QObject o; |
|
579 |
o.setProperty("ole", 42); |
|
580 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
581 |
||
582 |
QPropertyAnimation anim(&o, "ole"); |
|
583 |
anim.setEndValue(100); |
|
584 |
||
585 |
anim.start(); |
|
586 |
||
587 |
QTest::qWait(100); |
|
588 |
int current = anim.currentValue().toInt(); |
|
589 |
//it is somewhere in the animation |
|
590 |
QVERIFY(current > 42); |
|
591 |
QVERIFY(current < 100); |
|
592 |
||
593 |
QTest::qWait(200); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
594 |
QTRY_COMPARE(anim.state(), QVariantAnimation::Stopped); |
0 | 595 |
current = anim.currentValue().toInt(); |
596 |
QCOMPARE(current, 100); |
|
597 |
QCOMPARE(o.property("ole").toInt(), current); |
|
598 |
||
599 |
anim.setEndValue(110); |
|
600 |
anim.start(); |
|
601 |
current = anim.currentValue().toInt(); |
|
602 |
// the default start value will reevaluate the current property |
|
603 |
// and set it to the end value of the last iteration |
|
604 |
QCOMPARE(current, 100); |
|
605 |
QTest::qWait(100); |
|
606 |
current = anim.currentValue().toInt(); |
|
607 |
//it is somewhere in the animation |
|
608 |
QVERIFY(current >= 100); |
|
609 |
QVERIFY(current <= 110); |
|
610 |
} |
|
611 |
||
612 |
void tst_QPropertyAnimation::startBackwardWithoutEndValue() |
|
613 |
{ |
|
614 |
QObject o; |
|
615 |
o.setProperty("ole", 42); |
|
616 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
617 |
||
618 |
QPropertyAnimation anim(&o, "ole"); |
|
619 |
anim.setStartValue(100); |
|
620 |
anim.setDirection(QAbstractAnimation::Backward); |
|
621 |
||
622 |
//we start without an end value |
|
623 |
anim.start(); |
|
624 |
QCOMPARE(anim.state(), QAbstractAnimation::Running); |
|
625 |
QCOMPARE(o.property("ole").toInt(), 42); //the initial value |
|
626 |
||
627 |
QTest::qWait(100); |
|
628 |
int current = anim.currentValue().toInt(); |
|
629 |
//it is somewhere in the animation |
|
630 |
QVERIFY(current > 42); |
|
631 |
QVERIFY(current < 100); |
|
632 |
||
633 |
QTest::qWait(200); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
634 |
QTRY_COMPARE(anim.state(), QVariantAnimation::Stopped); |
0 | 635 |
current = anim.currentValue().toInt(); |
636 |
QCOMPARE(current, 100); |
|
637 |
QCOMPARE(o.property("ole").toInt(), current); |
|
638 |
||
639 |
anim.setStartValue(110); |
|
640 |
anim.start(); |
|
641 |
current = anim.currentValue().toInt(); |
|
642 |
// the default start value will reevaluate the current property |
|
643 |
// and set it to the end value of the last iteration |
|
644 |
QCOMPARE(current, 100); |
|
645 |
QTest::qWait(100); |
|
646 |
current = anim.currentValue().toInt(); |
|
647 |
//it is somewhere in the animation |
|
648 |
QVERIFY(current >= 100); |
|
649 |
QVERIFY(current <= 110); |
|
650 |
} |
|
651 |
||
652 |
||
653 |
void tst_QPropertyAnimation::playForwardBackward() |
|
654 |
{ |
|
655 |
QObject o; |
|
656 |
o.setProperty("ole", 0); |
|
657 |
QCOMPARE(o.property("ole").toInt(), 0); |
|
658 |
||
659 |
QPropertyAnimation anim(&o, "ole"); |
|
660 |
anim.setStartValue(0); |
|
661 |
anim.setEndValue(100); |
|
662 |
anim.start(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
663 |
QTest::qWait(anim.duration() + 100); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
664 |
QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped); |
0 | 665 |
QCOMPARE(anim.currentTime(), anim.duration()); |
666 |
||
667 |
//the animation is at the end |
|
668 |
anim.setDirection(QVariantAnimation::Backward); |
|
669 |
anim.start(); |
|
670 |
QCOMPARE(anim.state(), QAbstractAnimation::Running); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
671 |
QTest::qWait(anim.duration() + 100); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
672 |
QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped); |
0 | 673 |
QCOMPARE(anim.currentTime(), 0); |
674 |
||
675 |
//the direction is backward |
|
676 |
//restarting should jump to the end |
|
677 |
anim.start(); |
|
678 |
QCOMPARE(anim.state(), QAbstractAnimation::Running); |
|
679 |
QCOMPARE(anim.currentTime(), anim.duration()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
680 |
QTest::qWait(anim.duration() + 100); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
681 |
QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped); |
0 | 682 |
QCOMPARE(anim.currentTime(), 0); |
683 |
} |
|
684 |
||
685 |
struct Number |
|
686 |
{ |
|
687 |
Number() {} |
|
688 |
Number(int n) |
|
689 |
: n(n) {} |
|
690 |
||
691 |
Number(const Number &other) |
|
692 |
: n(other.n){} |
|
693 |
||
694 |
Number &operator=(const Number &other) { |
|
695 |
n = other.n; |
|
696 |
return *this; |
|
697 |
} |
|
698 |
bool operator==(const Number &other) const { |
|
699 |
return n == other.n; |
|
700 |
} |
|
701 |
||
702 |
int n; |
|
703 |
}; |
|
704 |
||
705 |
Q_DECLARE_METATYPE(Number) |
|
706 |
Q_DECLARE_METATYPE(QAbstractAnimation::State) |
|
707 |
||
708 |
QVariant numberInterpolator(const Number &f, const Number &t, qreal progress) |
|
709 |
{ |
|
710 |
return qVariantFromValue<Number>(Number(f.n + (t.n - f.n)*progress)); |
|
711 |
} |
|
712 |
||
713 |
QVariant xaxisQPointInterpolator(const QPointF &f, const QPointF &t, qreal progress) |
|
714 |
{ |
|
715 |
return QPointF(f.x() + (t.x() - f.x())*progress, f.y()); |
|
716 |
} |
|
717 |
||
718 |
void tst_QPropertyAnimation::interpolated() |
|
719 |
{ |
|
720 |
QObject o; |
|
721 |
o.setProperty("point", QPointF()); //this will avoid warnings |
|
722 |
o.setProperty("number", qVariantFromValue<Number>(Number(42))); |
|
723 |
QCOMPARE(qVariantValue<Number>(o.property("number")), Number(42)); |
|
724 |
{ |
|
725 |
qRegisterAnimationInterpolator<Number>(numberInterpolator); |
|
726 |
QPropertyAnimation anim(&o, "number"); |
|
727 |
anim.setStartValue(qVariantFromValue<Number>(Number(0))); |
|
728 |
anim.setEndValue(qVariantFromValue<Number>(Number(100))); |
|
729 |
anim.setDuration(1000); |
|
730 |
anim.start(); |
|
731 |
anim.pause(); |
|
732 |
anim.setCurrentTime(100); |
|
733 |
Number t(qVariantValue<Number>(o.property("number"))); |
|
734 |
QCOMPARE(t, Number(10)); |
|
735 |
anim.setCurrentTime(500); |
|
736 |
QCOMPARE(qVariantValue<Number>(o.property("number")), Number(50)); |
|
737 |
} |
|
738 |
{ |
|
739 |
qRegisterAnimationInterpolator<QPointF>(xaxisQPointInterpolator); |
|
740 |
QPropertyAnimation anim(&o, "point"); |
|
741 |
anim.setStartValue(QPointF(0,0)); |
|
742 |
anim.setEndValue(QPointF(100, 100)); |
|
743 |
anim.setDuration(1000); |
|
744 |
anim.start(); |
|
745 |
anim.pause(); |
|
746 |
anim.setCurrentTime(100); |
|
747 |
QCOMPARE(o.property("point"), QVariant(QPointF(10, 0))); |
|
748 |
anim.setCurrentTime(500); |
|
749 |
QCOMPARE(o.property("point"), QVariant(QPointF(50, 0))); |
|
750 |
} |
|
751 |
{ |
|
752 |
// unregister it and see if we get back the default behaviour |
|
753 |
qRegisterAnimationInterpolator<QPointF>(0); |
|
754 |
QPropertyAnimation anim(&o, "point"); |
|
755 |
anim.setStartValue(QPointF(0,0)); |
|
756 |
anim.setEndValue(QPointF(100, 100)); |
|
757 |
anim.setDuration(1000); |
|
758 |
anim.start(); |
|
759 |
anim.pause(); |
|
760 |
anim.setCurrentTime(100); |
|
761 |
QCOMPARE(o.property("point").toPointF(), QPointF(10, 10)); |
|
762 |
anim.setCurrentTime(500); |
|
763 |
QCOMPARE(o.property("point").toPointF(), QPointF(50, 50)); |
|
764 |
} |
|
765 |
||
766 |
{ |
|
767 |
// Interpolate a qreal property with a int interpolator |
|
768 |
AnimationObject o1; |
|
769 |
o1.setRealValue(42.42); |
|
770 |
QPropertyAnimation anim(&o1, "realValue"); |
|
771 |
anim.setStartValue(0); |
|
772 |
anim.setEndValue(100); |
|
773 |
anim.start(); |
|
774 |
QCOMPARE(o1.realValue(), qreal(0)); |
|
775 |
anim.setCurrentTime(250); |
|
776 |
QCOMPARE(o1.realValue(), qreal(100)); |
|
777 |
} |
|
778 |
} |
|
779 |
||
780 |
Q_DECLARE_METATYPE(QVariant) |
|
781 |
||
782 |
void tst_QPropertyAnimation::setStartEndValues_data() |
|
783 |
{ |
|
784 |
QTest::addColumn<QByteArray>("propertyName"); |
|
785 |
QTest::addColumn<QVariant>("initialValue"); |
|
786 |
QTest::addColumn<QVariant>("startValue"); |
|
787 |
QTest::addColumn<QVariant>("endValue"); |
|
788 |
||
789 |
QTest::newRow("dynamic property") << QByteArray("ole") << QVariant(42) << QVariant(0) << QVariant(10); |
|
790 |
QTest::newRow("real property, with unmatching types") << QByteArray("x") << QVariant(42.) << QVariant(0) << QVariant(10.); |
|
791 |
} |
|
792 |
||
793 |
void tst_QPropertyAnimation::setStartEndValues() |
|
794 |
{ |
|
795 |
MyObject object; |
|
796 |
QFETCH(QByteArray, propertyName); |
|
797 |
QFETCH(QVariant, initialValue); |
|
798 |
QFETCH(QVariant, startValue); |
|
799 |
QFETCH(QVariant, endValue); |
|
800 |
||
801 |
//this tests the start value, end value and default start value |
|
802 |
object.setProperty(propertyName, initialValue); |
|
803 |
QPropertyAnimation anim(&object, propertyName); |
|
804 |
QVariantAnimation::KeyValues values; |
|
805 |
QCOMPARE(anim.keyValues(), values); |
|
806 |
||
807 |
//let's add a start value |
|
808 |
anim.setStartValue(startValue); |
|
809 |
values << QVariantAnimation::KeyValue(0, startValue); |
|
810 |
QCOMPARE(anim.keyValues(), values); |
|
811 |
||
812 |
anim.setEndValue(endValue); |
|
813 |
values << QVariantAnimation::KeyValue(1, endValue); |
|
814 |
QCOMPARE(anim.keyValues(), values); |
|
815 |
||
816 |
//now we can play with objects |
|
817 |
QCOMPARE(object.property(propertyName).toDouble(), initialValue.toDouble()); |
|
818 |
anim.start(); |
|
819 |
QVERIFY(anim.startValue().isValid()); |
|
820 |
QCOMPARE(object.property(propertyName), anim.startValue()); |
|
821 |
anim.setCurrentTime(anim.duration()/2); |
|
822 |
QCOMPARE(object.property(propertyName).toDouble(), (startValue.toDouble() + endValue.toDouble())/2 ); //just in the middle of the animation |
|
823 |
anim.setCurrentTime(anim.duration()); //we go to the end of the animation |
|
824 |
QCOMPARE(anim.state(), QAnimationGroup::Stopped); //it should have stopped |
|
825 |
QVERIFY(anim.endValue().isValid()); |
|
826 |
QCOMPARE(object.property(propertyName), anim.endValue()); //end of the animations |
|
827 |
||
828 |
//now we remove the explicit start value and test the implicit one |
|
829 |
anim.stop(); |
|
830 |
object.setProperty(propertyName, initialValue); |
|
831 |
||
832 |
//let's reset the start value |
|
833 |
values.remove(0); |
|
834 |
anim.setStartValue(QVariant()); |
|
835 |
QCOMPARE(anim.keyValues(), values); |
|
836 |
QVERIFY(!anim.startValue().isValid()); |
|
837 |
||
838 |
anim.start(); |
|
839 |
QCOMPARE(object.property(propertyName), initialValue); |
|
840 |
anim.setCurrentTime(anim.duration()/2); |
|
841 |
QCOMPARE(object.property(propertyName).toDouble(), (initialValue.toDouble() + endValue.toDouble())/2 ); //just in the middle of the animation |
|
842 |
anim.setCurrentTime(anim.duration()); //we go to the end of the animation |
|
843 |
QCOMPARE(anim.state(), QAnimationGroup::Stopped); //it should have stopped |
|
844 |
QVERIFY(anim.endValue().isValid()); |
|
845 |
QCOMPARE(object.property(propertyName), anim.endValue()); //end of the animations |
|
846 |
||
847 |
//now we set back the startValue |
|
848 |
anim.setStartValue(startValue); |
|
849 |
QVERIFY(anim.startValue().isValid()); |
|
850 |
anim.start(); |
|
851 |
QCOMPARE(object.property(propertyName), startValue); |
|
852 |
} |
|
853 |
||
854 |
void tst_QPropertyAnimation::zeroDurationStart() |
|
855 |
{ |
|
856 |
DummyPropertyAnimation anim; |
|
857 |
QSignalSpy spy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
858 |
anim.setDuration(0); |
|
859 |
QCOMPARE(anim.state(), QAbstractAnimation::Stopped); |
|
860 |
anim.start(); |
|
861 |
//the animation stops immediately |
|
862 |
QCOMPARE(anim.state(), QAbstractAnimation::Stopped); |
|
863 |
QCOMPARE(spy.count(), 2); |
|
864 |
||
865 |
//let's check the first state change |
|
866 |
const QVariantList firstChange = spy.first(); |
|
867 |
//old state |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
868 |
QCOMPARE(qVariantValue<QAbstractAnimation::State>(firstChange.last()), QAbstractAnimation::Stopped); |
0 | 869 |
//new state |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
870 |
QCOMPARE(qVariantValue<QAbstractAnimation::State>(firstChange.first()), QAbstractAnimation::Running); |
0 | 871 |
|
872 |
//let's check the first state change |
|
873 |
const QVariantList secondChange = spy.last(); |
|
874 |
//old state |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
875 |
QCOMPARE(qVariantValue<QAbstractAnimation::State>(secondChange.last()), QAbstractAnimation::Running); |
0 | 876 |
//new state |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
877 |
QCOMPARE(qVariantValue<QAbstractAnimation::State>(secondChange.first()), QAbstractAnimation::Stopped); |
0 | 878 |
} |
879 |
||
880 |
#define Pause 1 |
|
881 |
#define Start 2 |
|
882 |
#define Resume 3 |
|
883 |
#define Stop 4 |
|
884 |
||
885 |
void tst_QPropertyAnimation::operationsInStates_data() |
|
886 |
{ |
|
887 |
QTest::addColumn<QAbstractAnimation::State>("originState"); |
|
888 |
QTest::addColumn<int>("operation"); |
|
889 |
QTest::addColumn<QString>("expectedWarning"); |
|
890 |
QTest::addColumn<QAbstractAnimation::State>("expectedState"); |
|
891 |
||
892 |
QString pauseWarn(QLatin1String("QAbstractAnimation::pause: Cannot pause a stopped animation")); |
|
893 |
QString resumeWarn(QLatin1String("QAbstractAnimation::resume: Cannot resume an animation that is not paused")); |
|
894 |
||
895 |
QTest::newRow("S-pause") << QAbstractAnimation::Stopped << Pause << pauseWarn << QAbstractAnimation::Stopped; |
|
896 |
QTest::newRow("S-start") << QAbstractAnimation::Stopped << Start << QString() << QAbstractAnimation::Running; |
|
897 |
QTest::newRow("S-resume") << QAbstractAnimation::Stopped << Resume << resumeWarn << QAbstractAnimation::Stopped; |
|
898 |
QTest::newRow("S-stop") << QAbstractAnimation::Stopped << Stop << QString() << QAbstractAnimation::Stopped; |
|
899 |
||
900 |
QTest::newRow("P-pause") << QAbstractAnimation::Paused << Pause << QString() << QAbstractAnimation::Paused; |
|
901 |
QTest::newRow("P-start") << QAbstractAnimation::Paused << Start << QString() << QAbstractAnimation::Running; |
|
902 |
QTest::newRow("P-resume") << QAbstractAnimation::Paused << Resume << QString() << QAbstractAnimation::Running; |
|
903 |
QTest::newRow("P-stop") << QAbstractAnimation::Paused << Stop << QString() << QAbstractAnimation::Stopped; |
|
904 |
||
905 |
QTest::newRow("R-pause") << QAbstractAnimation::Running << Pause << QString() << QAbstractAnimation::Paused; |
|
906 |
QTest::newRow("R-start") << QAbstractAnimation::Running << Start << QString() << QAbstractAnimation::Running; |
|
907 |
QTest::newRow("R-resume") << QAbstractAnimation::Running << Resume << resumeWarn << QAbstractAnimation::Running; |
|
908 |
QTest::newRow("R-stop") << QAbstractAnimation::Running << Stop << QString() << QAbstractAnimation::Stopped; |
|
909 |
} |
|
910 |
||
911 |
void tst_QPropertyAnimation::operationsInStates() |
|
912 |
{ |
|
913 |
/** |
|
914 |
* | pause() |start() |resume() |stop() |
|
915 |
* ----------+------------+-----------+-----------+-------------------+ |
|
916 |
* Stopped | Stopped |Running |Stopped |Stopped | |
|
917 |
* _| qWarning |restart |qWarning | | |
|
918 |
* Paused | Paused |Running |Running |Stopped | |
|
919 |
* _| | | | | |
|
920 |
* Running | Paused |Running |Running |Stopped | |
|
921 |
* | |restart |qWarning | | |
|
922 |
* ----------+------------+-----------+-----------+-------------------+ |
|
923 |
**/ |
|
924 |
||
925 |
QFETCH(QAbstractAnimation::State, originState); |
|
926 |
QFETCH(int, operation); |
|
927 |
QFETCH(QString, expectedWarning); |
|
928 |
QFETCH(QAbstractAnimation::State, expectedState); |
|
929 |
||
930 |
QObject o; |
|
931 |
o.setProperty("ole", 42); |
|
932 |
QPropertyAnimation anim(&o, "ole"); |
|
933 |
anim.setEndValue(100); |
|
934 |
QSignalSpy spy(&anim, SIGNAL(stateChanged(QAbstractAnimation::State, QAbstractAnimation::State))); |
|
935 |
||
936 |
anim.stop(); |
|
937 |
switch (originState) { |
|
938 |
case QAbstractAnimation::Stopped: |
|
939 |
break; |
|
940 |
case QAbstractAnimation::Paused: |
|
941 |
anim.start(); |
|
942 |
anim.pause(); |
|
943 |
break; |
|
944 |
case QAbstractAnimation::Running: |
|
945 |
anim.start(); |
|
946 |
break; |
|
947 |
} |
|
948 |
if (!expectedWarning.isEmpty()) { |
|
949 |
QTest::ignoreMessage(QtWarningMsg, qPrintable(expectedWarning)); |
|
950 |
} |
|
951 |
QCOMPARE(anim.state(), originState); |
|
952 |
switch (operation) { |
|
953 |
case Pause: |
|
954 |
anim.pause(); |
|
955 |
break; |
|
956 |
case Start: |
|
957 |
anim.start(); |
|
958 |
break; |
|
959 |
case Resume: |
|
960 |
anim.resume(); |
|
961 |
break; |
|
962 |
case Stop: |
|
963 |
anim.stop(); |
|
964 |
break; |
|
965 |
} |
|
966 |
||
967 |
QCOMPARE(anim.state(), expectedState); |
|
968 |
} |
|
969 |
#undef Pause |
|
970 |
#undef Start |
|
971 |
#undef Resume |
|
972 |
#undef Stop |
|
973 |
||
974 |
void tst_QPropertyAnimation::oneKeyValue() |
|
975 |
{ |
|
976 |
QObject o; |
|
977 |
o.setProperty("ole", 42); |
|
978 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
979 |
||
980 |
QPropertyAnimation animation(&o, "ole"); |
|
981 |
animation.setStartValue(43); |
|
982 |
animation.setEndValue(44); |
|
983 |
animation.setDuration(100); |
|
984 |
||
985 |
animation.setCurrentTime(0); |
|
986 |
||
987 |
QVERIFY(animation.currentValue().isValid()); |
|
988 |
QCOMPARE(animation.currentValue().toInt(), 43); |
|
989 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
990 |
||
991 |
// remove the last key value |
|
992 |
animation.setKeyValueAt(1.0, QVariant()); |
|
993 |
||
994 |
// we will neither interpolate, nor update the current value |
|
995 |
// since there is only one 1 key value defined |
|
996 |
animation.setCurrentTime(100); |
|
997 |
||
998 |
// the animation should not have been modified |
|
999 |
QVERIFY(animation.currentValue().isValid()); |
|
1000 |
QCOMPARE(animation.currentValue().toInt(), 43); |
|
1001 |
QCOMPARE(o.property("ole").toInt(), 42); |
|
1002 |
} |
|
1003 |
||
1004 |
void tst_QPropertyAnimation::updateOnSetKeyValues() |
|
1005 |
{ |
|
1006 |
QObject o; |
|
1007 |
o.setProperty("ole", 100); |
|
1008 |
QCOMPARE(o.property("ole").toInt(), 100); |
|
1009 |
||
1010 |
QPropertyAnimation animation(&o, "ole"); |
|
1011 |
animation.setStartValue(100); |
|
1012 |
animation.setEndValue(200); |
|
1013 |
animation.setDuration(100); |
|
1014 |
||
1015 |
animation.setCurrentTime(50); |
|
1016 |
QCOMPARE(animation.currentValue().toInt(), 150); |
|
1017 |
animation.setKeyValueAt(0.0, 300); |
|
1018 |
QCOMPARE(animation.currentValue().toInt(), 250); |
|
1019 |
||
1020 |
o.setProperty("ole", 100); |
|
1021 |
QPropertyAnimation animation2(&o, "ole"); |
|
1022 |
QVariantAnimation::KeyValues kValues; |
|
1023 |
kValues << QVariantAnimation::KeyValue(0.0, 100) << QVariantAnimation::KeyValue(1.0, 200); |
|
1024 |
animation2.setKeyValues(kValues); |
|
1025 |
animation2.setDuration(100); |
|
1026 |
animation2.setCurrentTime(50); |
|
1027 |
QCOMPARE(animation2.currentValue().toInt(), 150); |
|
1028 |
||
1029 |
kValues.clear(); |
|
1030 |
kValues << QVariantAnimation::KeyValue(0.0, 300) << QVariantAnimation::KeyValue(1.0, 200); |
|
1031 |
animation2.setKeyValues(kValues); |
|
1032 |
||
1033 |
QCOMPARE(animation2.currentValue().toInt(), animation.currentValue().toInt()); |
|
1034 |
} |
|
1035 |
||
1036 |
||
1037 |
//this class will 'throw' an error in the test lib |
|
1038 |
// if the property ole is set to ErrorValue |
|
1039 |
class MyErrorObject : public QObject |
|
1040 |
{ |
|
1041 |
Q_OBJECT |
|
1042 |
Q_PROPERTY(int ole READ ole WRITE setOle) |
|
1043 |
public: |
|
1044 |
||
1045 |
static const int ErrorValue = 10000; |
|
1046 |
||
1047 |
MyErrorObject() : m_ole(0) { } |
|
1048 |
int ole() const { return m_ole; } |
|
1049 |
void setOle(int o) |
|
1050 |
{ |
|
1051 |
QVERIFY(o != ErrorValue); |
|
1052 |
m_ole = o; |
|
1053 |
} |
|
1054 |
||
1055 |
private: |
|
1056 |
int m_ole; |
|
1057 |
||
1058 |
||
1059 |
}; |
|
1060 |
||
1061 |
void tst_QPropertyAnimation::restart() |
|
1062 |
{ |
|
1063 |
//here we check that be restarting an animation |
|
1064 |
//it doesn't get an bogus intermediate value (end value) |
|
1065 |
//because the time is not yet reset to 0 |
|
1066 |
MyErrorObject o; |
|
1067 |
o.setOle(100); |
|
1068 |
QCOMPARE(o.property("ole").toInt(), 100); |
|
1069 |
||
1070 |
QPropertyAnimation anim(&o, "ole"); |
|
1071 |
anim.setEndValue(200); |
|
1072 |
anim.start(); |
|
1073 |
anim.setCurrentTime(anim.duration()); |
|
1074 |
QCOMPARE(anim.state(), QAbstractAnimation::Stopped); |
|
1075 |
QCOMPARE(o.property("ole").toInt(), 200); |
|
1076 |
||
1077 |
//we'll check that the animation never gets a wrong value when starting it |
|
1078 |
//after having changed the end value |
|
1079 |
anim.setEndValue(MyErrorObject::ErrorValue); |
|
1080 |
anim.start(); |
|
1081 |
} |
|
1082 |
||
1083 |
void tst_QPropertyAnimation::valueChanged() |
|
1084 |
{ |
|
1085 |
qRegisterMetaType<QVariant>("QVariant"); |
|
1086 |
||
1087 |
//we check that we receive the valueChanged signal |
|
1088 |
MyErrorObject o; |
|
1089 |
o.setOle(0); |
|
1090 |
QCOMPARE(o.property("ole").toInt(), 0); |
|
1091 |
QPropertyAnimation anim(&o, "ole"); |
|
1092 |
anim.setEndValue(5); |
|
1093 |
anim.setDuration(1000); |
|
1094 |
QSignalSpy spy(&anim, SIGNAL(valueChanged(QVariant))); |
|
1095 |
anim.start(); |
|
1096 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1097 |
QTest::qWait(anim.duration() + 100); |
0 | 1098 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1099 |
QTRY_COMPARE(anim.state(), QAbstractAnimation::Stopped); |
0 | 1100 |
QCOMPARE(anim.currentTime(), anim.duration()); |
1101 |
||
1102 |
//let's check that the values go forward |
|
1103 |
QCOMPARE(spy.count(), 6); //we should have got everything from 0 to 5 |
|
1104 |
for (int i = 0; i < spy.count(); ++i) { |
|
1105 |
QCOMPARE(qvariant_cast<QVariant>(spy.at(i).first()).toInt(), i); |
|
1106 |
} |
|
1107 |
} |
|
1108 |
||
1109 |
//this class will help us make sure that 2 animations started |
|
1110 |
//at the same time also end at the same time |
|
1111 |
class MySyncObject : public MyErrorObject |
|
1112 |
{ |
|
1113 |
Q_OBJECT |
|
1114 |
public: |
|
1115 |
MySyncObject() : anim(this, "ole") |
|
1116 |
{ |
|
1117 |
anim.setEndValue(1000); |
|
1118 |
} |
|
1119 |
public slots: |
|
1120 |
void checkAnimationFinished() |
|
1121 |
{ |
|
1122 |
QCOMPARE(anim.state(), QAbstractAnimation::Stopped); |
|
1123 |
QCOMPARE(ole(), 1000); |
|
1124 |
} |
|
1125 |
||
1126 |
public: |
|
1127 |
QPropertyAnimation anim; |
|
1128 |
}; |
|
1129 |
||
1130 |
void tst_QPropertyAnimation::twoAnimations() |
|
1131 |
{ |
|
1132 |
MySyncObject o1, o2; |
|
1133 |
o1.setOle(0); |
|
1134 |
o2.setOle(0); |
|
1135 |
||
1136 |
//when the animation in o1 is finished |
|
1137 |
//the animation in o2 should stop around the same time |
|
1138 |
//We use a queued connection to check just after the tick from the common timer |
|
1139 |
//the other way is true too |
|
1140 |
QObject::connect(&o1.anim, SIGNAL(finished()), |
|
1141 |
&o2, SLOT(checkAnimationFinished()), Qt::QueuedConnection); |
|
1142 |
QObject::connect(&o2.anim, SIGNAL(finished()), |
|
1143 |
&o1, SLOT(checkAnimationFinished()), Qt::QueuedConnection); |
|
1144 |
||
1145 |
o1.anim.start(); |
|
1146 |
o2.anim.start(); |
|
1147 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1148 |
QTest::qWait(o1.anim.duration() + 100); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1149 |
QTRY_COMPARE(o1.anim.state(), QAbstractAnimation::Stopped); |
0 | 1150 |
QCOMPARE(o2.anim.state(), QAbstractAnimation::Stopped); |
1151 |
||
1152 |
QCOMPARE(o1.ole(), 1000); |
|
1153 |
QCOMPARE(o2.ole(), 1000); |
|
1154 |
} |
|
1155 |
||
1156 |
class MyComposedAnimation : public QPropertyAnimation |
|
1157 |
{ |
|
1158 |
Q_OBJECT |
|
1159 |
public: |
|
1160 |
MyComposedAnimation(QObject *target, const QByteArray &propertyName, const QByteArray &innerPropertyName) |
|
1161 |
: QPropertyAnimation(target, propertyName) |
|
1162 |
{ |
|
1163 |
innerAnim = new QPropertyAnimation(target, innerPropertyName); |
|
1164 |
this->setEndValue(1000); |
|
1165 |
innerAnim->setEndValue(1000); |
|
1166 |
innerAnim->setDuration(duration() + 100); |
|
1167 |
} |
|
1168 |
||
1169 |
void start() |
|
1170 |
{ |
|
1171 |
QPropertyAnimation::start(); |
|
1172 |
innerAnim->start(); |
|
1173 |
} |
|
1174 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1175 |
void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) |
0 | 1176 |
{ |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1177 |
QPropertyAnimation::updateState(newState, oldState); |
0 | 1178 |
if (newState == QAbstractAnimation::Stopped) |
1179 |
delete innerAnim; |
|
1180 |
} |
|
1181 |
||
1182 |
public: |
|
1183 |
QPropertyAnimation *innerAnim; |
|
1184 |
}; |
|
1185 |
||
1186 |
void tst_QPropertyAnimation::deletedInUpdateCurrentTime() |
|
1187 |
{ |
|
1188 |
// this test case reproduces an animation being deleted in the updateCurrentTime of |
|
1189 |
// another animation(was causing segfault). |
|
1190 |
// the deleted animation must have been started after the animation that is deleting. |
|
1191 |
AnimationObject o; |
|
1192 |
o.setValue(0); |
|
1193 |
o.setRealValue(0.0); |
|
1194 |
||
1195 |
MyComposedAnimation composedAnimation(&o, "value", "realValue"); |
|
1196 |
composedAnimation.start(); |
|
1197 |
QCOMPARE(composedAnimation.state(), QAbstractAnimation::Running); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1198 |
QTest::qWait(composedAnimation.duration() + 100); |
0 | 1199 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1200 |
QTRY_COMPARE(composedAnimation.state(), QAbstractAnimation::Stopped); |
0 | 1201 |
QCOMPARE(o.value(), 1000); |
1202 |
} |
|
1203 |
||
1204 |
void tst_QPropertyAnimation::totalDuration() |
|
1205 |
{ |
|
1206 |
QPropertyAnimation anim; |
|
1207 |
QCOMPARE(anim.totalDuration(), 250); |
|
1208 |
anim.setLoopCount(2); |
|
1209 |
QCOMPARE(anim.totalDuration(), 2*250); |
|
1210 |
anim.setLoopCount(-1); |
|
1211 |
QCOMPARE(anim.totalDuration(), -1); |
|
1212 |
anim.setDuration(0); |
|
1213 |
QCOMPARE(anim.totalDuration(), 0); |
|
1214 |
} |
|
1215 |
||
1216 |
||
1217 |
QTEST_MAIN(tst_QPropertyAnimation) |
|
1218 |
#include "tst_qpropertyanimation.moc" |