1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | #include <qtest.h> |
29 | #include <QtQml/qqmlengine.h> |
30 | #include <QtQml/qqmlcomponent.h> |
31 | #include <QtQuick/private/qquicksmoothedanimation_p.h> |
32 | #include <QtQuick/private/qquickrectangle_p.h> |
33 | #include <private/qqmlvaluetype_p.h> |
34 | #include "../../shared/util.h" |
35 | |
36 | class tst_qquicksmoothedanimation : public QQmlDataTest |
37 | { |
38 | Q_OBJECT |
39 | public: |
40 | tst_qquicksmoothedanimation(); |
41 | |
42 | private slots: |
43 | void defaultValues(); |
44 | void values(); |
45 | void disabled(); |
46 | void simpleAnimation(); |
47 | void valueSource(); |
48 | void behavior(); |
49 | void deleteOnUpdate(); |
50 | void zeroDuration(); |
51 | void noStart(); |
52 | |
53 | private: |
54 | QQmlEngine engine; |
55 | }; |
56 | |
57 | tst_qquicksmoothedanimation::tst_qquicksmoothedanimation() |
58 | { |
59 | } |
60 | |
61 | void tst_qquicksmoothedanimation::defaultValues() |
62 | { |
63 | QQmlEngine engine; |
64 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimation1.qml" )); |
65 | QQuickSmoothedAnimation *obj = qobject_cast<QQuickSmoothedAnimation*>(object: c.create()); |
66 | |
67 | QVERIFY(obj != nullptr); |
68 | |
69 | QCOMPARE(obj->to(), 0.); |
70 | QCOMPARE(obj->velocity(), 200.); |
71 | QCOMPARE(obj->duration(), -1); |
72 | QCOMPARE(obj->maximumEasingTime(), -1); |
73 | QCOMPARE(obj->reversingMode(), QQuickSmoothedAnimation::Eased); |
74 | |
75 | delete obj; |
76 | } |
77 | |
78 | void tst_qquicksmoothedanimation::values() |
79 | { |
80 | QQmlEngine engine; |
81 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimation2.qml" )); |
82 | QQuickSmoothedAnimation *obj = qobject_cast<QQuickSmoothedAnimation*>(object: c.create()); |
83 | |
84 | QVERIFY(obj != nullptr); |
85 | |
86 | QCOMPARE(obj->to(), 10.); |
87 | QCOMPARE(obj->velocity(), 200.); |
88 | QCOMPARE(obj->duration(), 300); |
89 | QCOMPARE(obj->maximumEasingTime(), -1); |
90 | QCOMPARE(obj->reversingMode(), QQuickSmoothedAnimation::Immediate); |
91 | |
92 | delete obj; |
93 | } |
94 | |
95 | void tst_qquicksmoothedanimation::disabled() |
96 | { |
97 | QQmlEngine engine; |
98 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimation3.qml" )); |
99 | QQuickSmoothedAnimation *obj = qobject_cast<QQuickSmoothedAnimation*>(object: c.create()); |
100 | |
101 | QVERIFY(obj != nullptr); |
102 | |
103 | QCOMPARE(obj->to(), 10.); |
104 | QCOMPARE(obj->velocity(), 250.); |
105 | QCOMPARE(obj->maximumEasingTime(), 150); |
106 | QCOMPARE(obj->reversingMode(), QQuickSmoothedAnimation::Sync); |
107 | |
108 | delete obj; |
109 | } |
110 | |
111 | void tst_qquicksmoothedanimation::simpleAnimation() |
112 | { |
113 | QQmlEngine engine; |
114 | QQmlComponent c(&engine, testFileUrl(fileName: "simpleanimation.qml" )); |
115 | QObject *obj = c.create(); |
116 | QVERIFY(obj); |
117 | |
118 | QQuickRectangle *rect = obj->findChild<QQuickRectangle*>(aName: "rect" ); |
119 | QVERIFY(rect); |
120 | |
121 | QQuickSmoothedAnimation *animation = obj->findChild<QQuickSmoothedAnimation*>(aName: "anim" ); |
122 | QVERIFY(animation); |
123 | |
124 | animation->setTargetObject(rect); |
125 | animation->setProperty("x" ); |
126 | animation->setTo(200); |
127 | animation->setDuration(250); |
128 | QCOMPARE(animation->target(), rect); |
129 | QCOMPARE(animation->property(), QLatin1String("x" )); |
130 | QCOMPARE(animation->to(), qreal(200)); |
131 | animation->start(); |
132 | QVERIFY(animation->isRunning()); |
133 | QTest::qWait(ms: animation->duration()); |
134 | QTRY_COMPARE(rect->x(), qreal(200)); |
135 | QTest::qWait(ms: 100); //smoothed animation doesn't report stopped until delayed timer fires |
136 | |
137 | QVERIFY(!animation->isRunning()); |
138 | rect->setX(0); |
139 | animation->start(); |
140 | QVERIFY(animation->isRunning()); |
141 | animation->pause(); |
142 | QVERIFY(animation->isRunning()); |
143 | QVERIFY(animation->isPaused()); |
144 | animation->setCurrentTime(125); |
145 | QCOMPARE(animation->currentTime(), 125); |
146 | QCOMPARE(rect->x(), qreal(100)); |
147 | } |
148 | |
149 | void tst_qquicksmoothedanimation::valueSource() |
150 | { |
151 | QQmlEngine engine; |
152 | |
153 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimationValueSource.qml" )); |
154 | |
155 | QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(object: c.create()); |
156 | QVERIFY(rect); |
157 | |
158 | QQuickRectangle *theRect = rect->findChild<QQuickRectangle*>(aName: "theRect" ); |
159 | QVERIFY(theRect); |
160 | |
161 | QQuickSmoothedAnimation *easeX = rect->findChild<QQuickSmoothedAnimation*>(aName: "easeX" ); |
162 | QVERIFY(easeX); |
163 | QVERIFY(easeX->isRunning()); |
164 | |
165 | QQuickSmoothedAnimation *easeY = rect->findChild<QQuickSmoothedAnimation*>(aName: "easeY" ); |
166 | QVERIFY(easeY); |
167 | QVERIFY(easeY->isRunning()); |
168 | |
169 | // XXX get the proper duration |
170 | QTest::qWait(ms: 100); |
171 | |
172 | QTRY_VERIFY(!easeX->isRunning()); |
173 | QTRY_VERIFY(!easeY->isRunning()); |
174 | |
175 | QTRY_COMPARE(theRect->x(), qreal(200)); |
176 | QTRY_COMPARE(theRect->y(), qreal(200)); |
177 | |
178 | delete rect; |
179 | } |
180 | |
181 | void tst_qquicksmoothedanimation::behavior() |
182 | { |
183 | #ifdef Q_CC_MINGW |
184 | QSKIP("QTBUG-36290 - MinGW Animation tests are flaky." ); |
185 | #endif |
186 | QQmlEngine engine; |
187 | |
188 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimationBehavior.qml" )); |
189 | |
190 | QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(object: c.create()); |
191 | QVERIFY(rect); |
192 | |
193 | QQuickRectangle *theRect = rect->findChild<QQuickRectangle*>(aName: "theRect" ); |
194 | QVERIFY(theRect); |
195 | |
196 | QQuickSmoothedAnimation *easeX = rect->findChild<QQuickSmoothedAnimation*>(aName: "easeX" ); |
197 | QVERIFY(easeX); |
198 | |
199 | QQuickSmoothedAnimation *easeY = rect->findChild<QQuickSmoothedAnimation*>(aName: "easeY" ); |
200 | QVERIFY(easeY); |
201 | |
202 | // XXX get the proper duration |
203 | QTest::qWait(ms: 400); |
204 | |
205 | QTRY_VERIFY(!easeX->isRunning()); |
206 | QTRY_VERIFY(!easeY->isRunning()); |
207 | |
208 | QTRY_COMPARE(theRect->x(), qreal(200)); |
209 | QTRY_COMPARE(theRect->y(), qreal(200)); |
210 | |
211 | delete rect; |
212 | } |
213 | |
214 | void tst_qquicksmoothedanimation::deleteOnUpdate() |
215 | { |
216 | QQmlEngine engine; |
217 | |
218 | QQmlComponent c(&engine, testFileUrl(fileName: "deleteOnUpdate.qml" )); |
219 | |
220 | QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(object: c.create()); |
221 | QVERIFY(rect); |
222 | |
223 | QQuickSmoothedAnimation *anim = rect->findChild<QQuickSmoothedAnimation*>(aName: "anim" ); |
224 | QVERIFY(anim); |
225 | |
226 | //don't crash |
227 | QTest::qWait(ms: 500); |
228 | |
229 | delete rect; |
230 | } |
231 | |
232 | void tst_qquicksmoothedanimation::zeroDuration() |
233 | { |
234 | QQmlEngine engine; |
235 | |
236 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimationZeroDuration.qml" )); |
237 | |
238 | QQuickRectangle *rect = qobject_cast<QQuickRectangle*>(object: c.create()); |
239 | QVERIFY(rect); |
240 | |
241 | QQuickRectangle *theRect = rect->findChild<QQuickRectangle*>(aName: "theRect" ); |
242 | QVERIFY(theRect); |
243 | |
244 | QQuickSmoothedAnimation *easeX = rect->findChild<QQuickSmoothedAnimation*>(aName: "easeX" ); |
245 | QVERIFY(easeX); |
246 | QVERIFY(easeX->isRunning()); |
247 | |
248 | QTRY_VERIFY(!easeX->isRunning()); |
249 | QTRY_COMPARE(theRect->x(), qreal(200)); |
250 | |
251 | delete rect; |
252 | } |
253 | |
254 | //verify that an empty SmoothedAnimation does not fire up the animation system |
255 | //and keep it running forever |
256 | void tst_qquicksmoothedanimation::noStart() |
257 | { |
258 | QQmlEngine engine; |
259 | QQmlComponent c(&engine, testFileUrl(fileName: "smoothedanimation1.qml" )); |
260 | QQuickSmoothedAnimation *obj = qobject_cast<QQuickSmoothedAnimation*>(object: c.create()); |
261 | |
262 | QVERIFY(obj != nullptr); |
263 | |
264 | obj->start(); |
265 | QCOMPARE(obj->isRunning(), false); |
266 | QTest::qWait(ms: 100); |
267 | QCOMPARE(obj->isRunning(), false); |
268 | //this could fail if the test is being run in parallel with other tests |
269 | //or if an earlier test failed and didn't clean up (left an animation running) |
270 | //QCOMPARE(QUnifiedTimer::instance()->runningAnimationCount(), 0); |
271 | |
272 | delete obj; |
273 | } |
274 | |
275 | QTEST_MAIN(tst_qquicksmoothedanimation) |
276 | |
277 | #include "tst_qquicksmoothedanimation.moc" |
278 | |