Debian Codegraph @

tests/auto/lottie/shape/fill/tst_lottiefill.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/qlottiefill_p.h"
13
14 using namespace Qt::StringLiterals;
15
16 class tst_QLottieFill: public QObject
17 {
18 Q_OBJECT
19
20 public:
21 tst_QLottieFill();
22 ~tst_QLottieFill();
23
24 private:
25
26 private slots:
27 void initTestCase();
28 void cleanupTestCase();
29
30 void testStaticInitialColor();
31 void testStaticeInitialOpacity();
32 void testStaticUpdatedColor();
33 void testStaticeUpdatedOpacity();
34
35 void testAnimatedInitialColor();
36 void testAnimatedInitialOpacity();
37 void testAnimatedUpdatedColor();
38 void testAnimatedUpdatedOpacity();
39
40 void testName();
41 void testType();
42 void testActive();
43 void testHidden();
44
45 private:
46 void loadTestData(const QString &filename);
47 void updateProperty(int frame);
48
49 QLottieFill *m_fill = nullptr;
50 };
51
52 tst_QLottieFill::tst_QLottieFill()
53 {
54
55 }
56
57 tst_QLottieFill::~tst_QLottieFill()
58 {
59
60 }
61
62 void tst_QLottieFill::initTestCase()
63 {
64 }
65
66 void tst_QLottieFill::cleanupTestCase()
67 {
68 if (m_fill)
69 delete m_fill;
70 }
71
72 void tst_QLottieFill::testStaticInitialColor()
73 {
74 loadTestData("fill_static_red_100.json");
75
76 QVERIFY(m_fill->color() == QColor(Qt::red));
77 }
78
79 void tst_QLottieFill:: testStaticeInitialOpacity()
80 {
81 loadTestData("fill_static_red_100.json");
82
83 QVERIFY(qFuzzyCompare(m_fill->opacity(), 100.0));
84 }
85
86 void tst_QLottieFill::testStaticUpdatedColor()
87 {
88 loadTestData("fill_static_red_100.json");
89 updateProperty(179);
90
91 QVERIFY(m_fill->color() == QColor(Qt::red));
92 }
93
94 void tst_QLottieFill:: testStaticeUpdatedOpacity()
95 {
96 loadTestData("fill_static_red_100.json");
97 updateProperty(179);
98
99 QVERIFY(qFuzzyCompare(m_fill->opacity(), 100.0));
100 }
101
102 void tst_QLottieFill::testAnimatedInitialColor()
103 {
104 loadTestData("fill_animated_red100_green0.json");
105 updateProperty(0);
106
107 QVERIFY(m_fill->color() == QColor(Qt::red));
108 }
109
110 void tst_QLottieFill::testAnimatedInitialOpacity()
111 {
112 loadTestData("fill_animated_red100_green0.json");
113 updateProperty(0);
114
115 QVERIFY(qFuzzyCompare(m_fill->opacity(), 100.0));
116 }
117
118 void tst_QLottieFill::testAnimatedUpdatedColor()
119 {
120 loadTestData("fill_animated_red100_green0.json");
121 updateProperty(179);
122
123 QVERIFY(m_fill->color() == QColor(Qt::green));
124 }
125
126 void tst_QLottieFill::testAnimatedUpdatedOpacity()
127 {
128 loadTestData("fill_animated_red100_green0.json");
129 updateProperty(179);
130
131 QVERIFY(qFuzzyCompare(m_fill->opacity(), 0.0));
132 }
133
134 void tst_QLottieFill::testName()
135 {
136 loadTestData("fill_static_red_100.json");
137 QVERIFY(m_fill->name() == QString("Fill 1"));
138 }
139
140 void tst_QLottieFill::testType()
141 {
142 loadTestData("fill_static_red_100.json");
143 QVERIFY(m_fill->type() == LOTTIE_SHAPE_FILL_IX);
144 }
145
146 void tst_QLottieFill::testActive()
147 {
148 loadTestData("fill_static_red_100.json");
149 QVERIFY(m_fill->active(100) == true);
150
151 loadTestData("fill_hidden.json");
152 QVERIFY(m_fill->active(100) == false);
153 }
154
155 void tst_QLottieFill::testHidden()
156 {
157 loadTestData("fill_hidden.json");
158 QVERIFY(m_fill->hidden() == true);
159 }
160
161 void tst_QLottieFill::loadTestData(const QString &filename)
162 {
163 if (m_fill) {
164 delete m_fill;
165 m_fill = nullptr;
166 }
167
168 QFile sourceFile(QFINDTESTDATA(QLatin1String("data/") + filename));
169 if (!sourceFile.exists())
170 QFAIL("File does not exist");
171 if (!sourceFile.open(QIODevice::ReadOnly))
172 QFAIL("Cannot read test file");
173
174 QByteArray json = sourceFile.readAll();
175
176 sourceFile.close();
177
178 QJsonDocument doc = QJsonDocument::fromJson(json);
179 QJsonObject rootObj = doc.object();
180 if (rootObj.empty())
181 QFAIL("Cannot parse test file");
182
183 QJsonArray layers = rootObj.value(QLatin1String("layers")).toArray();
184 QJsonObject layerObj = layers[0].toObject();
185 int type = layerObj.value(QLatin1String("ty")).toInt();
186 if (type != 4)
187 QFAIL("It's not shape layer");
188
189 QJsonArray shapes = layerObj.value(QLatin1String("shapes")).toArray();
190 QJsonArray::const_iterator shapesIt = shapes.constBegin();
191 QLottieShape* shape = nullptr;
192 while (shapesIt != shapes.end()) {
193 QJsonObject childObj = (*shapesIt).toObject();
194 shape = QLottieShape::construct(childObj);
195 QVERIFY(shape != nullptr);
196 if (shape->type() == LOTTIE_SHAPE_FILL_IX)
197 break;
198 shapesIt++;
199 }
200
201 m_fill = static_cast<QLottieFill*>(shape);
202
203 QVERIFY(m_fill != nullptr);
204 }
205
206 void tst_QLottieFill::updateProperty(int frame)
207 {
208 m_fill->updateProperties(frame);
209 }
210
211 QTEST_MAIN(tst_QLottieFill)
212 #include "tst_lottiefill.moc"