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