Debian Codegraph @

tests/auto/lottie/shape/shapetransform/tst_lottieshapetransform.cpp

raw
1 // Copyright (C) 2018 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/QtTest>
5
6 #include <QFile>
7 #include <QJsonDocument>
8 #include <QJsonObject>
9 #include <QJsonArray>
10
11 #include "private/qlottielayer_p.h"
12 #include "private/qlottiegroup_p.h"
13 #include "private/qlottieshapetransform_p.h"
14
15 using namespace Qt::StringLiterals;
16
17 class tst_QLottieShapeTransform: public QObject
18 {
19 Q_OBJECT
20
21 public:
22 tst_QLottieShapeTransform();
23 ~tst_QLottieShapeTransform();
24
25 private:
26
27 // void testParseStaticRect();
28
29 private slots:
30 void initTestCase();
31 void cleanupTestCase();
32
33 void testStaticInitialAnchorX();
34 void testStaticInitialAnchorY();
35 void testStaticInitialPositionX();
36 void testStaticInitialPositionY();
37 void testStaticInitialScaleX();
38 void testStaticInitialScaleY();
39 void testStaticInitialRotation();
40 void testStaticInitialOpacity();
41 void testStaticInitialSkew();
42 void testStaticInitialSkewAxis();
43 void testStaticUpdatedAnchorX();
44 void testStaticUpdatedAnchorY();
45 void testStaticUpdatedPositionX();
46 void testStaticUpdatedPositionY();
47 void testStaticUpdatedScaleX();
48 void testStaticUpdatedScaleY();
49 void testStaticUpdatedRotation();
50 void testStaticUpdatedOpacity();
51 void testStaticUpdatedSkew();
52 void testStaticUpdatedSkewAxis();
53
54
55 void testAnimatedInitialAnchorX();
56 void testAnimatedInitialAnchorY();
57 void testAnimatedInitialPositionX();
58 void testAnimatedInitialPositionY();
59 void testAnimatedInitialScaleX();
60 void testAnimatedInitialScaleY();
61 void testAnimatedInitialRotation();
62 void testAnimatedInitialOpacity();
63 void testAnimatedInitialSkew();
64 void testAnimatedInitialSkewAxis();
65 void testAnimatedUpdatedAnchorX();
66 void testAnimatedUpdatedAnchorY();
67 void testAnimatedUpdatedPositionX();
68 void testAnimatedUpdatedPositionY();
69 void testAnimatedUpdatedScaleX();
70 void testAnimatedUpdatedScaleY();
71 void testAnimatedUpdatedRotation();
72 void testAnimatedUpdatedOpacity();
73 void testAnimatedUpdatedSkew();
74 void testAnimatedUpdatedSkewAxis();
75
76 void testName();
77 void testType();
78 void testActive();
79
80 private:
81 void loadTestData(const QString &filename);
82 void updateProperty(int frame);
83
84 QLottieShapeTransform *m_transform = nullptr;
85 };
86
87 tst_QLottieShapeTransform::tst_QLottieShapeTransform()
88 {
89
90 }
91
92 tst_QLottieShapeTransform::~tst_QLottieShapeTransform()
93 {
94
95 }
96
97 void tst_QLottieShapeTransform::initTestCase()
98 {
99 }
100
101 void tst_QLottieShapeTransform::cleanupTestCase()
102 {
103 if (m_transform)
104 delete m_transform;
105 }
106
107 void tst_QLottieShapeTransform::testStaticInitialAnchorX()
108 {
109 loadTestData("shapetransform_static.json");
110
111 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().x(), 50.0));
112 }
113
114 void tst_QLottieShapeTransform::testStaticInitialAnchorY()
115 {
116 loadTestData("shapetransform_static.json");
117
118 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().y(), 50.0));
119 }
120
121 void tst_QLottieShapeTransform::testStaticInitialPositionX()
122 {
123 loadTestData("shapetransform_static.json");
124
125 QVERIFY(qFuzzyCompare(m_transform->position().x(), 50.0));
126 }
127
128 void tst_QLottieShapeTransform::testStaticInitialPositionY()
129 {
130 loadTestData("shapetransform_static.json");
131
132 QVERIFY(qFuzzyCompare(m_transform->position().y(), 50.0));
133 }
134
135 void tst_QLottieShapeTransform::testStaticInitialScaleX()
136 {
137 loadTestData("shapetransform_static.json");
138
139 QVERIFY(qFuzzyCompare(m_transform->scale().x(), 0.5));
140 }
141
142 void tst_QLottieShapeTransform::testStaticInitialScaleY()
143 {
144 loadTestData("shapetransform_static.json");
145
146 QVERIFY(qFuzzyCompare(m_transform->scale().y(), 0.5));
147 }
148
149 void tst_QLottieShapeTransform::testStaticInitialRotation()
150 {
151 loadTestData("shapetransform_static.json");
152
153 QVERIFY(qFuzzyCompare(m_transform->rotation(), 0.0));
154 }
155
156 void tst_QLottieShapeTransform::testStaticInitialOpacity()
157 {
158 loadTestData("shapetransform_static.json");
159
160 QVERIFY(qFuzzyCompare(m_transform->opacity(), 1.0));
161 }
162
163 void tst_QLottieShapeTransform::testStaticInitialSkew()
164 {
165 loadTestData("shapetransform_static.json");
166
167 QVERIFY(qFuzzyCompare(m_transform->skew(), 0.0));
168 }
169
170 void tst_QLottieShapeTransform::testStaticInitialSkewAxis()
171 {
172 loadTestData("shapetransform_static.json");
173
174 QVERIFY(qFuzzyCompare(m_transform->skewAxis(), 0.0));
175 }
176
177 void tst_QLottieShapeTransform::testStaticUpdatedAnchorX()
178 {
179 loadTestData("shapetransform_static.json");
180 updateProperty(180);
181
182 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().x(), 50.0));
183 }
184
185 void tst_QLottieShapeTransform::testStaticUpdatedAnchorY()
186 {
187 loadTestData("shapetransform_static.json");
188 updateProperty(180);
189
190 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().y(), 50.0));
191 }
192
193 void tst_QLottieShapeTransform::testStaticUpdatedPositionX()
194 {
195 loadTestData("shapetransform_static.json");
196 updateProperty(180);
197
198 QVERIFY(qFuzzyCompare(m_transform->position().x(), 50.0));
199 }
200
201 void tst_QLottieShapeTransform::testStaticUpdatedPositionY()
202 {
203 loadTestData("shapetransform_static.json");
204 updateProperty(180);
205
206 QVERIFY(qFuzzyCompare(m_transform->position().y(), 50.0));
207 }
208
209 void tst_QLottieShapeTransform::testStaticUpdatedScaleX()
210 {
211 loadTestData("shapetransform_static.json");
212 updateProperty(180);
213
214 QVERIFY(qFuzzyCompare(m_transform->scale().x(), 0.5));
215 }
216
217 void tst_QLottieShapeTransform::testStaticUpdatedScaleY()
218 {
219 loadTestData("shapetransform_static.json");
220 updateProperty(180);
221
222 QVERIFY(qFuzzyCompare(m_transform->scale().y(), 0.5));
223 }
224
225 void tst_QLottieShapeTransform::testStaticUpdatedRotation()
226 {
227 loadTestData("shapetransform_static.json");
228 updateProperty(180);
229
230 QVERIFY(qFuzzyCompare(m_transform->rotation(), 0.0));
231 }
232
233 void tst_QLottieShapeTransform::testStaticUpdatedOpacity()
234 {
235 loadTestData("shapetransform_static.json");
236 updateProperty(180);
237
238 QVERIFY(qFuzzyCompare(m_transform->opacity(), 1.0));
239 }
240
241 void tst_QLottieShapeTransform::testStaticUpdatedSkew()
242 {
243 loadTestData("shapetransform_static.json");
244 updateProperty(180);
245
246 QVERIFY(qFuzzyCompare(m_transform->skew(), 0.0));
247 }
248
249 void tst_QLottieShapeTransform::testStaticUpdatedSkewAxis()
250 {
251 loadTestData("shapetransform_static.json");
252 updateProperty(180);
253
254 QVERIFY(qFuzzyCompare(m_transform->skewAxis(), 0.0));
255 }
256
257 void tst_QLottieShapeTransform::testAnimatedInitialAnchorX()
258 {
259 loadTestData("shapetransform_animated.json");
260 updateProperty(0);
261
262 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().x(), 50.0));
263 }
264
265 void tst_QLottieShapeTransform::testAnimatedInitialAnchorY()
266 {
267 loadTestData("shapetransform_animated.json");
268 updateProperty(0);
269
270 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().y(), 50.0));
271 }
272
273 void tst_QLottieShapeTransform::testAnimatedInitialPositionX()
274 {
275 loadTestData("shapetransform_animated.json");
276 updateProperty(0);
277
278 QVERIFY(qFuzzyCompare(m_transform->position().x(), 50.0));
279 }
280
281 void tst_QLottieShapeTransform::testAnimatedInitialPositionY()
282 {
283 loadTestData("shapetransform_animated.json");
284 updateProperty(0);
285
286 QVERIFY(qFuzzyCompare(m_transform->position().y(), 50.0));
287 }
288
289 void tst_QLottieShapeTransform::testAnimatedInitialScaleX()
290 {
291 loadTestData("shapetransform_animated.json");
292 updateProperty(0);
293
294 QVERIFY(qFuzzyCompare(m_transform->scale().x(), 0.5));
295 }
296
297 void tst_QLottieShapeTransform::testAnimatedInitialScaleY()
298 {
299 loadTestData("shapetransform_animated.json");
300 updateProperty(0);
301
302 QVERIFY(qFuzzyCompare(m_transform->scale().y(), 0.5));
303 }
304
305 void tst_QLottieShapeTransform::testAnimatedInitialRotation()
306 {
307 loadTestData("shapetransform_animated.json");
308 updateProperty(0);
309
310 QVERIFY(qFuzzyCompare(m_transform->rotation(), 0.0));
311 }
312
313 void tst_QLottieShapeTransform::testAnimatedInitialOpacity()
314 {
315 loadTestData("shapetransform_animated.json");
316 updateProperty(0);
317
318 QVERIFY(qFuzzyCompare(m_transform->opacity(), 1.0));
319 }
320
321 void tst_QLottieShapeTransform::testAnimatedInitialSkew()
322 {
323 loadTestData("shapetransform_animated.json");
324 updateProperty(0);
325
326 QVERIFY(qFuzzyCompare(m_transform->skew(), 0.0));
327 }
328
329 void tst_QLottieShapeTransform::testAnimatedInitialSkewAxis()
330 {
331 loadTestData("shapetransform_animated.json");
332 updateProperty(0);
333
334 QVERIFY(qFuzzyCompare(m_transform->skewAxis(), 0.0));
335 }
336
337 void tst_QLottieShapeTransform::testAnimatedUpdatedAnchorX()
338 {
339 loadTestData("shapetransform_animated.json");
340 updateProperty(180);
341
342 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().x(), 25.0));
343 }
344
345 void tst_QLottieShapeTransform::testAnimatedUpdatedAnchorY()
346 {
347 loadTestData("shapetransform_animated.json");
348 updateProperty(180);
349
350 QVERIFY(qFuzzyCompare(m_transform->anchorPoint().y(), 25.0));
351 }
352
353 void tst_QLottieShapeTransform::testAnimatedUpdatedPositionX()
354 {
355 loadTestData("shapetransform_animated.json");
356 updateProperty(180);
357
358 QVERIFY(qFuzzyCompare(m_transform->position().x(), 75.0));
359 }
360
361 void tst_QLottieShapeTransform::testAnimatedUpdatedPositionY()
362 {
363 loadTestData("shapetransform_animated.json");
364 updateProperty(180);
365
366 QVERIFY(qFuzzyCompare(m_transform->position().y(), 75.0));
367 }
368
369 void tst_QLottieShapeTransform::testAnimatedUpdatedScaleX()
370 {
371 loadTestData("shapetransform_animated.json");
372 updateProperty(180);
373
374 QVERIFY(qFuzzyCompare(m_transform->scale().x(), 1.0));
375 }
376
377 void tst_QLottieShapeTransform::testAnimatedUpdatedScaleY()
378 {
379 loadTestData("shapetransform_animated.json");
380 updateProperty(180);
381
382 QVERIFY(qFuzzyCompare(m_transform->scale().y(), 1.0));
383 }
384
385 void tst_QLottieShapeTransform::testAnimatedUpdatedRotation()
386 {
387 loadTestData("shapetransform_animated.json");
388 updateProperty(180);
389
390 QVERIFY(qFuzzyCompare(m_transform->rotation(), (3 * 360 + 30.0)));
391 }
392
393 void tst_QLottieShapeTransform::testAnimatedUpdatedOpacity()
394 {
395 loadTestData("shapetransform_animated.json");
396 updateProperty(180);
397
398 QVERIFY(qFuzzyCompare(m_transform->opacity(), 0.25));
399 }
400
401 void tst_QLottieShapeTransform::testAnimatedUpdatedSkew()
402 {
403 loadTestData("shapetransform_animated.json");
404 updateProperty(180);
405
406 QVERIFY(qFuzzyCompare(m_transform->skew(), 4.0));
407 }
408
409 void tst_QLottieShapeTransform::testAnimatedUpdatedSkewAxis()
410 {
411 loadTestData("shapetransform_animated.json");
412 updateProperty(180);
413
414 QVERIFY(qFuzzyCompare(m_transform->skewAxis(), (2 * 360 + 25.0)));
415 }
416
417 void tst_QLottieShapeTransform::testName()
418 {
419 loadTestData("shapetransform_static.json");
420 QVERIFY(m_transform->name() == QString("Transform"));
421 }
422
423 void tst_QLottieShapeTransform::testType()
424 {
425 loadTestData("shapetransform_static.json");
426 QVERIFY(m_transform->type() == LOTTIE_SHAPE_TRANS_IX);
427 }
428
429 void tst_QLottieShapeTransform::testActive()
430 {
431 loadTestData("shapetransform_static.json");
432 QVERIFY(m_transform->active(100) == true);
433 }
434
435 void tst_QLottieShapeTransform::loadTestData(const QString &filename)
436 {
437 if (m_transform) {
438 delete m_transform;
439 m_transform = nullptr;
440 }
441
442 QFile sourceFile(QFINDTESTDATA(QLatin1String("data/") + filename));
443 if (!sourceFile.exists())
444 QFAIL("File does not exist");
445 if (!sourceFile.open(QIODevice::ReadOnly))
446 QFAIL("Cannot read test file");
447
448 QByteArray json = sourceFile.readAll();
449
450 sourceFile.close();
451
452 QJsonDocument doc = QJsonDocument::fromJson(json);
453 QJsonObject rootObj = doc.object();
454 if (rootObj.empty())
455 QFAIL("Cannot parse test file");
456
457 QJsonArray layers = rootObj.value(QLatin1String("layers")).toArray();
458 QJsonObject layerObj = layers[0].toObject();
459 int type = layerObj.value(QLatin1String("ty")).toInt();
460 if (type != 4)
461 QFAIL("It's not shape layer");
462
463 QJsonArray shapes = layerObj.value(QLatin1String("shapes")).toArray();
464 QJsonArray::const_iterator shapesIt = shapes.constBegin();
465 QLottieGroup* group = nullptr;
466 while (shapesIt != shapes.end()) {
467 QJsonObject childObj = (*shapesIt).toObject();
468 QLottieShape *shape = QLottieShape::construct(childObj);
469 QVERIFY(shape != nullptr);
470 if (shape->type() == LOTTIE_SHAPE_GROUP_IX) {
471 group = static_cast<QLottieGroup *>(shape);
472 break;
473 }
474 shapesIt++;
475 }
476 QVERIFY(group != nullptr);
477
478 m_transform = static_cast<QLottieShapeTransform*>(group->findChild("Transform"));
479
480 QVERIFY(m_transform != nullptr);
481 }
482
483 void tst_QLottieShapeTransform::updateProperty(int frame)
484 {
485 m_transform->updateProperties(frame);
486 }
487
488 QTEST_MAIN(tst_QLottieShapeTransform)
489 #include "tst_lottieshapetransform.moc"