1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2020 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 | #include <QtTest/QtTest> |
30 | |
31 | #include <qtimeline.h> |
32 | |
33 | class tst_QTimeLine : public QObject |
34 | { |
35 | Q_OBJECT |
36 | private slots: |
37 | void range(); |
38 | void currentTime(); |
39 | void duration(); |
40 | void frameRate(); |
41 | void value(); |
42 | void currentFrame(); |
43 | void loopCount(); |
44 | void interpolation(); |
45 | void reverse_data(); |
46 | void reverse(); |
47 | void toggleDirection(); |
48 | void frameChanged(); |
49 | void stopped(); |
50 | void finished(); |
51 | void isRunning(); |
52 | void multipleTimeLines(); |
53 | void sineCurve(); |
54 | void cosineCurve(); |
55 | void outOfRange(); |
56 | void stateInFinishedSignal(); |
57 | void resume(); |
58 | void restart(); |
59 | void setPaused(); |
60 | |
61 | protected slots: |
62 | void finishedSlot(); |
63 | |
64 | protected: |
65 | QTimeLine::State state; |
66 | QTimeLine * view; |
67 | }; |
68 | |
69 | void tst_QTimeLine::range() |
70 | { |
71 | QTimeLine timeLine(200); |
72 | QCOMPARE(timeLine.startFrame(), 0); |
73 | QCOMPARE(timeLine.endFrame(), 0); |
74 | timeLine.setFrameRange(startFrame: 0, endFrame: 1); |
75 | QCOMPARE(timeLine.startFrame(), 0); |
76 | QCOMPARE(timeLine.endFrame(), 1); |
77 | timeLine.setFrameRange(startFrame: 10, endFrame: 20); |
78 | QCOMPARE(timeLine.startFrame(), 10); |
79 | QCOMPARE(timeLine.endFrame(), 20); |
80 | |
81 | timeLine.setStartFrame(6); |
82 | QCOMPARE(timeLine.startFrame(), 6); |
83 | timeLine.setEndFrame(16); |
84 | QCOMPARE(timeLine.endFrame(), 16); |
85 | |
86 | // Verify that you can change the range in the timeLine |
87 | timeLine.setFrameRange(startFrame: 1000, endFrame: 2000); |
88 | QSignalSpy spy(&timeLine, &QTimeLine::frameChanged); |
89 | QVERIFY(spy.isValid()); |
90 | timeLine.start(); // make sure that the logic works for a running timeline |
91 | QTRY_COMPARE(timeLine.state(), QTimeLine::Running); |
92 | timeLine.setCurrentTime(timeLine.duration()/2); |
93 | int oldValue = timeLine.currentFrame(); |
94 | timeLine.setFrameRange(startFrame: 0, endFrame: 500); |
95 | QVERIFY(timeLine.currentFrame() < oldValue); |
96 | timeLine.setEndFrame(10000); |
97 | timeLine.setStartFrame(5000); |
98 | QVERIFY(timeLine.currentFrame() > oldValue); |
99 | timeLine.setFrameRange(startFrame: 0, endFrame: 500); |
100 | QTRY_VERIFY(spy.count() > 1); |
101 | QVERIFY(timeLine.currentFrame() < oldValue); |
102 | } |
103 | |
104 | void tst_QTimeLine::currentTime() |
105 | { |
106 | QTimeLine timeLine(2000); |
107 | timeLine.setUpdateInterval((timeLine.duration()/2) / 33); |
108 | timeLine.setFrameRange(startFrame: 10, endFrame: 20); |
109 | QCOMPARE(timeLine.currentTime(), 0); |
110 | timeLine.start(); |
111 | QTRY_COMPARE(timeLine.state(), QTimeLine::Running); |
112 | QTRY_VERIFY(timeLine.currentTime() > timeLine.duration()/2 - timeLine.duration()/4); |
113 | QVERIFY(timeLine.currentTime() < timeLine.duration()/2 + timeLine.duration()/4); |
114 | QTRY_COMPARE(timeLine.state(), QTimeLine::NotRunning); |
115 | QCOMPARE(timeLine.currentTime(), timeLine.duration()); |
116 | |
117 | QSignalSpy spy(&timeLine, &QTimeLine::valueChanged); |
118 | QVERIFY(spy.isValid()); |
119 | spy.clear(); |
120 | timeLine.setCurrentTime(timeLine.duration()/2); |
121 | timeLine.setCurrentTime(timeLine.duration()/2); |
122 | QCOMPARE(spy.count(), 1); |
123 | spy.clear(); |
124 | QCOMPARE(timeLine.currentTime(), timeLine.duration()/2); |
125 | timeLine.resume(); |
126 | // Let it update on its own |
127 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
128 | QTRY_VERIFY(timeLine.currentTime() > timeLine.duration()/2); |
129 | QVERIFY(timeLine.currentTime() < timeLine.duration()); |
130 | QTRY_COMPARE(timeLine.state(), QTimeLine::NotRunning); |
131 | QCOMPARE(timeLine.currentTime(), timeLine.duration()); |
132 | |
133 | // Reverse should decrease the currentTime |
134 | timeLine.setCurrentTime(timeLine.duration()/2); |
135 | timeLine.start(); |
136 | // Let it update on its own |
137 | int currentTime = timeLine.currentTime(); |
138 | QTRY_VERIFY(timeLine.currentTime() > currentTime); |
139 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
140 | currentTime = timeLine.currentTime(); |
141 | timeLine.setDirection(QTimeLine::Backward); |
142 | QTRY_VERIFY(timeLine.currentTime() < currentTime); |
143 | timeLine.stop(); |
144 | } |
145 | |
146 | void tst_QTimeLine::duration() |
147 | { |
148 | QTimeLine timeLine(200); |
149 | timeLine.setFrameRange(startFrame: 10, endFrame: 20); |
150 | QCOMPARE(timeLine.duration(), 200); |
151 | timeLine.setDuration(1000); |
152 | QCOMPARE(timeLine.duration(), 1000); |
153 | |
154 | timeLine.start(); |
155 | QTRY_COMPARE(timeLine.state(), QTimeLine::Running); |
156 | QTRY_VERIFY(timeLine.currentTime() > 0); |
157 | QTRY_COMPARE(timeLine.state(), QTimeLine::NotRunning); |
158 | QCOMPARE(timeLine.currentTime(), 1000); |
159 | // The duration shouldn't change |
160 | QCOMPARE(timeLine.duration(), 1000); |
161 | } |
162 | |
163 | void tst_QTimeLine::frameRate() |
164 | { |
165 | QTimeLine timeLine; |
166 | timeLine.setFrameRange(startFrame: 100, endFrame: 2000); |
167 | QCOMPARE(timeLine.updateInterval(), 1000 / 25); |
168 | timeLine.setUpdateInterval(1000 / 60); |
169 | QCOMPARE(timeLine.updateInterval(), 1000 / 60); |
170 | |
171 | // Default speed |
172 | timeLine.setUpdateInterval(1000 / 33); |
173 | QSignalSpy spy(&timeLine, &QTimeLine::frameChanged); |
174 | QVERIFY(spy.isValid()); |
175 | timeLine.start(); |
176 | QTest::qWait(ms: timeLine.duration()*2); |
177 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
178 | int slowCount = spy.count(); |
179 | |
180 | // Faster!! |
181 | timeLine.setUpdateInterval(1000 / 100); |
182 | spy.clear(); |
183 | timeLine.setCurrentTime(0); |
184 | timeLine.start(); |
185 | QTest::qWait(ms: timeLine.duration()*2); |
186 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
187 | QVERIFY2(slowCount < spy.count(), QByteArray::number(spy.count())); |
188 | } |
189 | |
190 | void tst_QTimeLine::value() |
191 | { |
192 | QTimeLine timeLine(4500); // Should be at least 5% under 5000ms |
193 | QCOMPARE(timeLine.currentValue(), 0.0); |
194 | |
195 | // Default speed |
196 | QSignalSpy spy(&timeLine, &QTimeLine::valueChanged); |
197 | QVERIFY(spy.isValid()); |
198 | timeLine.start(); |
199 | QTRY_VERIFY(timeLine.currentValue() > 0); |
200 | QTRY_COMPARE(timeLine.state(), QTimeLine::NotRunning); |
201 | QCOMPARE(timeLine.currentValue(), 1.0); |
202 | QVERIFY(spy.count() > 0); |
203 | |
204 | // Reverse should decrease the value |
205 | timeLine.setCurrentTime(100); |
206 | timeLine.start(); |
207 | // Let it update on its own |
208 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
209 | QTRY_VERIFY(timeLine.currentValue()); |
210 | qreal value = timeLine.currentValue(); |
211 | timeLine.setDirection(QTimeLine::Backward); |
212 | QTRY_VERIFY(timeLine.currentValue() < value); |
213 | timeLine.stop(); |
214 | } |
215 | |
216 | void tst_QTimeLine::currentFrame() |
217 | { |
218 | QTimeLine timeLine(2000); |
219 | timeLine.setFrameRange(startFrame: 10, endFrame: 20); |
220 | QCOMPARE(timeLine.currentFrame(), 10); |
221 | |
222 | // Default speed |
223 | QSignalSpy spy(&timeLine, &QTimeLine::frameChanged); |
224 | QVERIFY(spy.isValid()); |
225 | timeLine.start(); |
226 | QTRY_VERIFY(timeLine.currentFrame() > 10); |
227 | QTRY_COMPARE(timeLine.state(), QTimeLine::NotRunning); |
228 | QCOMPARE(timeLine.currentFrame(), 20); |
229 | |
230 | // Reverse should decrease the value |
231 | timeLine.setCurrentTime(timeLine.duration()/2); |
232 | timeLine.start(); |
233 | // Let it update on its own |
234 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
235 | QTRY_VERIFY(timeLine.currentTime() > timeLine.duration()/2); // wait for continuation |
236 | int value = timeLine.currentFrame(); |
237 | timeLine.setDirection(QTimeLine::Backward); |
238 | QTRY_VERIFY(timeLine.currentFrame() < value); |
239 | timeLine.stop(); |
240 | } |
241 | |
242 | void tst_QTimeLine::loopCount() |
243 | { |
244 | QTimeLine timeLine(200); |
245 | QCOMPARE(timeLine.loopCount(), 1); |
246 | timeLine.setFrameRange(startFrame: 10, endFrame: 20); |
247 | QCOMPARE(timeLine.loopCount(), 1); |
248 | timeLine.setLoopCount(0); |
249 | QCOMPARE(timeLine.loopCount(), 0); |
250 | |
251 | // Default speed infiniti looping |
252 | QSignalSpy spy(&timeLine, &QTimeLine::frameChanged); |
253 | QVERIFY(spy.isValid()); |
254 | timeLine.start(); |
255 | QTest::qWait(ms: timeLine.duration()); |
256 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
257 | //QCOMPARE(timeLine.currentFrame(), 20); |
258 | QTest::qWait(ms: timeLine.duration()*6); |
259 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
260 | QVERIFY(timeLine.currentTime() >= 0); |
261 | QVERIFY(timeLine.currentFrame() >= 10); |
262 | QVERIFY(timeLine.currentFrame() <= 20); |
263 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
264 | timeLine.stop(); |
265 | |
266 | timeLine.setDuration(2500); // ### some platforms have a very low resolution timer |
267 | timeLine.setFrameRange(startFrame: 0, endFrame: 2); |
268 | timeLine.setLoopCount(4); |
269 | |
270 | QSignalSpy finishedSpy(&timeLine, &QTimeLine::finished); |
271 | QSignalSpy frameChangedSpy(&timeLine, &QTimeLine::frameChanged); |
272 | QVERIFY(finishedSpy.isValid()); |
273 | QVERIFY(frameChangedSpy.isValid()); |
274 | QEventLoop loop; |
275 | connect(sender: &timeLine, SIGNAL(finished()), receiver: &loop, SLOT(quit())); |
276 | |
277 | |
278 | for(int i=0;i<2;i++) { |
279 | |
280 | timeLine.start(); |
281 | //we clear te list after the start so we don't catch |
282 | //a frameChanged signal for the frame 0 at the beginning |
283 | finishedSpy.clear(); |
284 | frameChangedSpy.clear(); |
285 | |
286 | loop.exec(); |
287 | |
288 | QCOMPARE(finishedSpy.count(), 1); |
289 | QCOMPARE(frameChangedSpy.count(), 11); |
290 | for (int i = 0; i < 11; ++i) { |
291 | QCOMPARE(frameChangedSpy.at(i).at(0).toInt(), (i+1) % 3); |
292 | } |
293 | } |
294 | |
295 | timeLine.setDirection(QTimeLine::Backward); |
296 | timeLine.start(); |
297 | loop.exec(); |
298 | |
299 | QCOMPARE(finishedSpy.count(), 2); |
300 | QCOMPARE(frameChangedSpy.count(), 22); |
301 | for (int i = 11; i < 22; ++i) { |
302 | QCOMPARE(frameChangedSpy.at(i).at(0).toInt(), 2 - (i+2) % 3); |
303 | } |
304 | } |
305 | |
306 | void tst_QTimeLine::interpolation() |
307 | { |
308 | QTimeLine timeLine(400); |
309 | QCOMPARE(timeLine.easingCurve(), QEasingCurve::InOutSine); |
310 | timeLine.setFrameRange(startFrame: 100, endFrame: 200); |
311 | timeLine.setEasingCurve(QEasingCurve::Linear); |
312 | QCOMPARE(timeLine.easingCurve(), QEasingCurve::Linear); |
313 | |
314 | // smooth |
315 | timeLine.setEasingCurve(QEasingCurve::InOutSine); |
316 | timeLine.start(); |
317 | QTest::qWait(ms: 100); |
318 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
319 | int firstValue = timeLine.currentFrame(); |
320 | QTest::qWait(ms: 200); |
321 | int endValue = timeLine.currentFrame(); |
322 | timeLine.stop(); |
323 | timeLine.setCurrentTime(0); |
324 | |
325 | // linear |
326 | timeLine.setEasingCurve(QEasingCurve::Linear); |
327 | timeLine.start(); |
328 | QTest::qWait(ms: 100); |
329 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
330 | |
331 | // Smooth accellerates slowly so in the beginning so it is farther behind |
332 | if (firstValue >= timeLine.currentFrame()) |
333 | QEXPECT_FAIL("" , "QTBUG-24796: QTimeLine exhibits inconsistent timing behaviour" , Abort); |
334 | QVERIFY(firstValue < timeLine.currentFrame()); |
335 | QTest::qWait(ms: 200); |
336 | QVERIFY(endValue > timeLine.currentFrame()); |
337 | timeLine.stop(); |
338 | } |
339 | |
340 | void tst_QTimeLine::reverse_data() |
341 | { |
342 | QTest::addColumn<int>(name: "duration" ); |
343 | QTest::addColumn<int>(name: "start" ); |
344 | QTest::addColumn<int>(name: "end" ); |
345 | QTest::addColumn<int>(name: "direction" ); |
346 | QTest::addColumn<int>(name: "direction2" ); |
347 | QTest::addColumn<int>(name: "direction3" ); |
348 | QTest::addColumn<int>(name: "startTime" ); |
349 | QTest::addColumn<int>(name: "currentFrame" ); |
350 | QTest::addColumn<qreal>(name: "currentValue" ); |
351 | QTest::addColumn<int>(name: "wait" ); |
352 | QTest::addColumn<int>(name: "state" ); |
353 | QTest::addColumn<int>(name: "wait2" ); |
354 | |
355 | QTest::newRow(dataTag: "start at end" ) << 200 << 1000 << 2000 << (int)QTimeLine::Backward << (int)QTimeLine::Forward << (int)QTimeLine::Backward << 200 << 2000 << qreal(1.0) << 40 << (int)QTimeLine::Running << 140; |
356 | QTest::newRow(dataTag: "start at half" ) << 200 << 1000 << 2000 << (int)QTimeLine::Backward << (int)QTimeLine::Forward << (int)QTimeLine::Backward << 100 << 1500 << qreal(0.5) << 40 << (int)QTimeLine::Running << 140; |
357 | QTest::newRow(dataTag: "start at quarter" ) << 200 << 1000 << 2000 << (int)QTimeLine::Backward << (int)QTimeLine::Forward << (int)QTimeLine::Backward << 50 << 1250 << qreal(0.25) << 40 << (int)QTimeLine::Running << 140; |
358 | } |
359 | |
360 | void tst_QTimeLine::reverse() |
361 | { |
362 | QFETCH(int, duration); |
363 | QFETCH(int, start); |
364 | QFETCH(int, end); |
365 | QFETCH(int, direction); |
366 | QFETCH(int, direction2); |
367 | QFETCH(int, direction3); |
368 | QFETCH(int, startTime); |
369 | QFETCH(int, currentFrame); |
370 | QFETCH(qreal, currentValue); |
371 | QFETCH(int, wait); |
372 | QFETCH(int, state); |
373 | QFETCH(int, wait2); |
374 | |
375 | QTimeLine timeLine(duration); |
376 | timeLine.setEasingCurve(QEasingCurve::Linear); |
377 | timeLine.setFrameRange(startFrame: start, endFrame: end); |
378 | |
379 | timeLine.setDirection((QTimeLine::Direction)direction); |
380 | timeLine.setDirection((QTimeLine::Direction)direction2); |
381 | timeLine.setDirection((QTimeLine::Direction)direction3); |
382 | QCOMPARE(timeLine.direction(), ((QTimeLine::Direction)direction)); |
383 | |
384 | timeLine.setCurrentTime(startTime); |
385 | timeLine.setDirection((QTimeLine::Direction)direction); |
386 | timeLine.setDirection((QTimeLine::Direction)direction2); |
387 | timeLine.setDirection((QTimeLine::Direction)direction3); |
388 | |
389 | QCOMPARE(timeLine.currentFrame(), currentFrame); |
390 | QCOMPARE(timeLine.currentValue(), currentValue); |
391 | timeLine.start(); |
392 | |
393 | QTest::qWait(ms: wait); |
394 | QCOMPARE(timeLine.state(), (QTimeLine::State)state); |
395 | int firstValue = timeLine.currentFrame(); |
396 | timeLine.setDirection((QTimeLine::Direction)direction2); |
397 | timeLine.setDirection((QTimeLine::Direction)direction3); |
398 | timeLine.setDirection((QTimeLine::Direction)direction2); |
399 | timeLine.setDirection((QTimeLine::Direction)direction3); |
400 | QTest::qWait(ms: wait2); |
401 | int endValue = timeLine.currentFrame(); |
402 | QVERIFY(endValue < firstValue); |
403 | |
404 | |
405 | } |
406 | |
407 | void tst_QTimeLine::toggleDirection() |
408 | { |
409 | // Note: enum values are cast to int so that QCOMPARE will show |
410 | // the values if they don't match. |
411 | QTimeLine timeLine; |
412 | QCOMPARE(int(timeLine.direction()), int(QTimeLine::Forward)); |
413 | timeLine.toggleDirection(); |
414 | QCOMPARE(int(timeLine.direction()), int(QTimeLine::Backward)); |
415 | timeLine.toggleDirection(); |
416 | QCOMPARE(int(timeLine.direction()), int(QTimeLine::Forward)); |
417 | } |
418 | |
419 | void tst_QTimeLine::frameChanged() |
420 | { |
421 | QTimeLine timeLine; |
422 | timeLine.setEasingCurve(QEasingCurve::Linear); |
423 | timeLine.setFrameRange(startFrame: 0,endFrame: 9); |
424 | timeLine.setUpdateInterval(800); |
425 | QSignalSpy spy(&timeLine, &QTimeLine::frameChanged); |
426 | QVERIFY(spy.isValid()); |
427 | |
428 | // Test what happens when duration expires before all frames are emitted. |
429 | timeLine.start(); |
430 | QTest::qWait(ms: timeLine.duration()/2); |
431 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
432 | QCOMPARE(spy.count(), 0); |
433 | QTest::qWait(ms: timeLine.duration()); |
434 | if (timeLine.state() != QTimeLine::NotRunning) |
435 | QEXPECT_FAIL("" , "QTBUG-24796: QTimeLine runs slower than it should" , Abort); |
436 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
437 | if (spy.count() != 1) |
438 | QEXPECT_FAIL("" , "QTBUG-24796: QTimeLine runs slower than it should" , Abort); |
439 | QCOMPARE(spy.count(), 1); |
440 | |
441 | // Test what happens when the frames are all emitted well before duration expires. |
442 | timeLine.setUpdateInterval(5); |
443 | spy.clear(); |
444 | timeLine.setCurrentTime(0); |
445 | timeLine.start(); |
446 | QTest::qWait(ms: timeLine.duration()*2); |
447 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
448 | QCOMPARE(spy.count(), 10); |
449 | } |
450 | |
451 | void tst_QTimeLine::stopped() |
452 | { |
453 | QTimeLine timeLine; |
454 | timeLine.setFrameRange(startFrame: 0, endFrame: 9); |
455 | qRegisterMetaType<QTimeLine::State>(typeName: "QTimeLine::State" ); |
456 | QSignalSpy spy(&timeLine, &QTimeLine::stateChanged); |
457 | QVERIFY(spy.isValid()); |
458 | timeLine.start(); |
459 | QTest::qWait(ms: timeLine.duration()*2); |
460 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
461 | QCOMPARE(spy.count(), 2); |
462 | spy.clear(); |
463 | timeLine.start(); |
464 | timeLine.stop(); |
465 | QCOMPARE(spy.count(), 2); |
466 | timeLine.setDirection(QTimeLine::Backward); |
467 | QCOMPARE(timeLine.loopCount(), 1); |
468 | } |
469 | |
470 | void tst_QTimeLine::finished() |
471 | { |
472 | QTimeLine timeLine; |
473 | timeLine.setFrameRange(startFrame: 0,endFrame: 9); |
474 | QSignalSpy spy(&timeLine, &QTimeLine::finished); |
475 | QVERIFY(spy.isValid()); |
476 | timeLine.start(); |
477 | QTRY_COMPARE(spy.count(), 1); |
478 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
479 | |
480 | spy.clear(); |
481 | timeLine.start(); |
482 | timeLine.stop(); |
483 | QCOMPARE(spy.count(), 0); |
484 | } |
485 | |
486 | void tst_QTimeLine::isRunning() |
487 | { |
488 | QTimeLine timeLine; |
489 | timeLine.setFrameRange(startFrame: 0,endFrame: 9); |
490 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
491 | timeLine.start(); |
492 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
493 | timeLine.stop(); |
494 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
495 | |
496 | timeLine.start(); |
497 | QTest::qWait(ms: timeLine.duration()*2); |
498 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
499 | } |
500 | |
501 | void tst_QTimeLine::multipleTimeLines() |
502 | { |
503 | // Stopping a timer shouldn't affect the other timers |
504 | QTimeLine timeLine(200); |
505 | timeLine.setFrameRange(startFrame: 0,endFrame: 99); |
506 | QSignalSpy spy(&timeLine, &QTimeLine::finished); |
507 | QVERIFY(spy.isValid()); |
508 | |
509 | QTimeLine timeLineKiller; |
510 | timeLineKiller.setFrameRange(startFrame: 0,endFrame: 99); |
511 | |
512 | timeLineKiller.start(); |
513 | timeLine.start(); |
514 | timeLineKiller.stop(); |
515 | QTest::qWait(ms: timeLine.duration()*2); |
516 | QCOMPARE(spy.count(), 1); |
517 | } |
518 | |
519 | void tst_QTimeLine::sineCurve() |
520 | { |
521 | QTimeLine timeLine(1000); |
522 | timeLine.setEasingCurve(QEasingCurve::SineCurve); |
523 | QCOMPARE(timeLine.valueForTime(0), qreal(0)); |
524 | QCOMPARE(timeLine.valueForTime(250), qreal(0.5)); |
525 | QCOMPARE(timeLine.valueForTime(500), qreal(1)); |
526 | QCOMPARE(timeLine.valueForTime(750), qreal(0.5)); |
527 | QCOMPARE(timeLine.valueForTime(1000), qreal(0)); |
528 | } |
529 | |
530 | void tst_QTimeLine::cosineCurve() |
531 | { |
532 | QTimeLine timeLine(1000); |
533 | timeLine.setEasingCurve(QEasingCurve::CosineCurve); |
534 | QCOMPARE(timeLine.valueForTime(0), qreal(0.5)); |
535 | QCOMPARE(timeLine.valueForTime(250), qreal(1)); |
536 | QCOMPARE(timeLine.valueForTime(500), qreal(0.5)); |
537 | QCOMPARE(timeLine.valueForTime(750), qreal(0)); |
538 | QCOMPARE(timeLine.valueForTime(1000), qreal(0.5)); |
539 | } |
540 | |
541 | void tst_QTimeLine::outOfRange() |
542 | { |
543 | QTimeLine timeLine(1000); |
544 | QCOMPARE(timeLine.valueForTime(-100), qreal(0)); |
545 | QCOMPARE(timeLine.valueForTime(2000), qreal(1)); |
546 | |
547 | timeLine.setEasingCurve(QEasingCurve::SineCurve); |
548 | QCOMPARE(timeLine.valueForTime(2000), qreal(0)); |
549 | } |
550 | |
551 | void tst_QTimeLine::stateInFinishedSignal() |
552 | { |
553 | QTimeLine timeLine(50); |
554 | |
555 | connect(sender: &timeLine, SIGNAL(finished()), receiver: this, SLOT(finishedSlot())); |
556 | state = QTimeLine::State(-1); |
557 | |
558 | timeLine.start(); |
559 | QTest::qWait(ms: 250); |
560 | |
561 | QCOMPARE(state, QTimeLine::NotRunning); |
562 | } |
563 | |
564 | void tst_QTimeLine::finishedSlot() |
565 | { |
566 | QTimeLine *timeLine = qobject_cast<QTimeLine *>(object: sender()); |
567 | if (timeLine) |
568 | state = timeLine->state(); |
569 | } |
570 | |
571 | void tst_QTimeLine::resume() |
572 | { |
573 | QTimeLine timeLine(1000); |
574 | { |
575 | QCOMPARE(timeLine.currentTime(), 0); |
576 | timeLine.start(); |
577 | QTRY_VERIFY(timeLine.currentTime() > 0); |
578 | timeLine.stop(); |
579 | int oldCurrentTime = timeLine.currentTime(); |
580 | QVERIFY(oldCurrentTime > 0); |
581 | QVERIFY(oldCurrentTime < 1000); |
582 | timeLine.resume(); |
583 | QTRY_VERIFY(timeLine.currentTime() > oldCurrentTime); |
584 | timeLine.stop(); |
585 | int currentTime = timeLine.currentTime(); |
586 | QVERIFY(currentTime < 1000); |
587 | } |
588 | timeLine.setDirection(QTimeLine::Backward); |
589 | { |
590 | timeLine.setCurrentTime(1000); |
591 | QCOMPARE(timeLine.currentTime(), 1000); |
592 | timeLine.start(); |
593 | QTRY_VERIFY(timeLine.currentTime() < 1000); |
594 | timeLine.stop(); |
595 | int oldCurrentTime = timeLine.currentTime(); |
596 | QVERIFY(oldCurrentTime < 1000); |
597 | QVERIFY(oldCurrentTime > 0); |
598 | timeLine.resume(); |
599 | QTRY_VERIFY(timeLine.currentTime() < oldCurrentTime); |
600 | timeLine.stop(); |
601 | int currentTime = timeLine.currentTime(); |
602 | QVERIFY(currentTime < oldCurrentTime); |
603 | QVERIFY(currentTime > 0); |
604 | } |
605 | } |
606 | |
607 | void tst_QTimeLine::restart() |
608 | { |
609 | QTimeLine timeLine(100); |
610 | timeLine.setFrameRange(startFrame: 0,endFrame: 9); |
611 | |
612 | timeLine.start(); |
613 | QTRY_COMPARE(timeLine.currentFrame(), timeLine.endFrame()); |
614 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
615 | |
616 | // A restart with the same duration |
617 | timeLine.start(); |
618 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
619 | QCOMPARE(timeLine.currentFrame(), timeLine.startFrame()); |
620 | QCOMPARE(timeLine.currentTime(), 0); |
621 | QTRY_COMPARE(timeLine.currentFrame(), timeLine.endFrame()); |
622 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
623 | |
624 | // Set a smaller duration and restart |
625 | timeLine.setDuration(50); |
626 | timeLine.start(); |
627 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
628 | QCOMPARE(timeLine.currentFrame(), timeLine.startFrame()); |
629 | QCOMPARE(timeLine.currentTime(), 0); |
630 | QTRY_COMPARE(timeLine.currentFrame(), timeLine.endFrame()); |
631 | QCOMPARE(timeLine.state(), QTimeLine::NotRunning); |
632 | |
633 | // Set a longer duration and restart |
634 | timeLine.setDuration(150); |
635 | timeLine.start(); |
636 | QCOMPARE(timeLine.state(), QTimeLine::Running); |
637 | QCOMPARE(timeLine.currentFrame(), timeLine.startFrame()); |
638 | QCOMPARE(timeLine.currentTime(), 0); |
639 | } |
640 | |
641 | void tst_QTimeLine::setPaused() |
642 | { |
643 | const int EndTime = 10000; |
644 | QTimeLine timeLine(EndTime); |
645 | { |
646 | QCOMPARE(timeLine.currentTime(), 0); |
647 | timeLine.start(); |
648 | QTRY_VERIFY(timeLine.currentTime() != 0); // wait for start |
649 | timeLine.setPaused(true); |
650 | int oldCurrentTime = timeLine.currentTime(); |
651 | QVERIFY(oldCurrentTime > 0); |
652 | QVERIFY(oldCurrentTime < EndTime); |
653 | QTest::qWait(ms: 1000); |
654 | timeLine.setPaused(false); |
655 | QTRY_VERIFY(timeLine.currentTime() > oldCurrentTime); |
656 | QVERIFY(timeLine.currentTime() > 0); |
657 | QVERIFY(timeLine.currentTime() < EndTime); |
658 | timeLine.stop(); |
659 | } |
660 | } |
661 | |
662 | QTEST_MAIN(tst_QTimeLine) |
663 | |
664 | #include "tst_qtimeline.moc" |
665 | |