| 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/qlottieellipse_p.h" |
| 13 | |
| 14 | using namespace Qt::StringLiterals; |
| 15 | |
| 16 | class tst_QLottieEllipse: public QObject |
| 17 | { |
| 18 | Q_OBJECT |
| 19 | |
| 20 | public: |
| 21 | tst_QLottieEllipse(); |
| 22 | ~tst_QLottieEllipse(); |
| 23 | |
| 24 | private: |
| 25 | |
| 26 | private slots: |
| 27 | void initTestCase(); |
| 28 | void cleanupTestCase(); |
| 29 | |
| 30 | void testStaticInitialX(); |
| 31 | void testStaticInitialY(); |
| 32 | void testStaticInitialWidth(); |
| 33 | void testStaticInitialHeight(); |
| 34 | void testStaticUpdatedX(); |
| 35 | void testStaticUpdatedY(); |
| 36 | void testStaticUpdatedWidth(); |
| 37 | void testStaticUpdatedHeight(); |
| 38 | |
| 39 | void testAnimatedInitialX(); |
| 40 | void testAnimatedInitialY(); |
| 41 | void testAnimatedInitialWidth(); |
| 42 | void testAnimatedInitialHeight(); |
| 43 | void testAnimatedUpdatedX(); |
| 44 | void testAnimatedUpdatedY(); |
| 45 | void testAnimatedUpdatedWidth(); |
| 46 | void testAnimatedUpdatedHeight(); |
| 47 | |
| 48 | void testName(); |
| 49 | void testType(); |
| 50 | void testActive(); |
| 51 | void testHidden(); |
| 52 | void testDirection(); |
| 53 | |
| 54 | private: |
| 55 | void loadTestData(const QString &filename); |
| 56 | void updateProperty(int frame); |
| 57 | |
| 58 | QLottieEllipse *m_ellipse = nullptr; |
| 59 | }; |
| 60 | |
| 61 | tst_QLottieEllipse::tst_QLottieEllipse() |
| 62 | { |
| 63 | |
| 64 | } |
| 65 | |
| 66 | tst_QLottieEllipse::~tst_QLottieEllipse() |
| 67 | { |
| 68 | |
| 69 | } |
| 70 | |
| 71 | void tst_QLottieEllipse::initTestCase() |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void tst_QLottieEllipse::cleanupTestCase() |
| 76 | { |
| 77 | if (m_ellipse) |
| 78 | delete m_ellipse; |
| 79 | } |
| 80 | |
| 81 | void tst_QLottieEllipse::testStaticInitialX() |
| 82 | { |
| 83 | loadTestData("ellipse_static_100x80.json"); |
| 84 | |
| 85 | QVERIFY(qFuzzyCompare(m_ellipse->position().x(), 0.0)); |
| 86 | } |
| 87 | |
| 88 | void tst_QLottieEllipse:: testStaticInitialY() |
| 89 | { |
| 90 | loadTestData("ellipse_static_100x80.json"); |
| 91 | |
| 92 | QVERIFY(qFuzzyCompare(m_ellipse->position().y(), 0.0)); |
| 93 | } |
| 94 | |
| 95 | void tst_QLottieEllipse:: testStaticInitialWidth() |
| 96 | { |
| 97 | loadTestData("ellipse_static_100x80.json"); |
| 98 | |
| 99 | QVERIFY(qFuzzyCompare(m_ellipse->size().width(), 100)); |
| 100 | } |
| 101 | |
| 102 | void tst_QLottieEllipse:: testStaticInitialHeight() |
| 103 | { |
| 104 | loadTestData("ellipse_static_100x80.json"); |
| 105 | |
| 106 | QVERIFY(qFuzzyCompare(m_ellipse->size().height(), 80)); |
| 107 | } |
| 108 | |
| 109 | void tst_QLottieEllipse::testStaticUpdatedX() |
| 110 | { |
| 111 | loadTestData("ellipse_static_100x80.json"); |
| 112 | updateProperty(179); |
| 113 | |
| 114 | QVERIFY(qFuzzyCompare(m_ellipse->position().x(), 0.0)); |
| 115 | } |
| 116 | |
| 117 | void tst_QLottieEllipse:: testStaticUpdatedY() |
| 118 | { |
| 119 | loadTestData("ellipse_static_100x80.json"); |
| 120 | updateProperty(179); |
| 121 | |
| 122 | QVERIFY(qFuzzyCompare(m_ellipse->position().y(), 0.0)); |
| 123 | } |
| 124 | |
| 125 | void tst_QLottieEllipse:: testStaticUpdatedWidth() |
| 126 | { |
| 127 | loadTestData("ellipse_static_100x80.json"); |
| 128 | updateProperty(179); |
| 129 | |
| 130 | QVERIFY(qFuzzyCompare(m_ellipse->size().width(), 100)); |
| 131 | } |
| 132 | |
| 133 | void tst_QLottieEllipse:: testStaticUpdatedHeight() |
| 134 | { |
| 135 | loadTestData("ellipse_static_100x80.json"); |
| 136 | updateProperty(179); |
| 137 | |
| 138 | QVERIFY(qFuzzyCompare(m_ellipse->size().height(), 80)); |
| 139 | } |
| 140 | |
| 141 | void tst_QLottieEllipse::testAnimatedInitialX() |
| 142 | { |
| 143 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 144 | updateProperty(0); |
| 145 | |
| 146 | QVERIFY(qFuzzyCompare(m_ellipse->position().x(), 0.0)); |
| 147 | } |
| 148 | |
| 149 | void tst_QLottieEllipse::testAnimatedInitialY() |
| 150 | { |
| 151 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 152 | updateProperty(0); |
| 153 | |
| 154 | QVERIFY(qFuzzyCompare(m_ellipse->position().y(), 0.0)); |
| 155 | } |
| 156 | |
| 157 | void tst_QLottieEllipse::testAnimatedInitialWidth() |
| 158 | { |
| 159 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 160 | updateProperty(0); |
| 161 | |
| 162 | QVERIFY(qFuzzyCompare(m_ellipse->size().width(), 100.0)); |
| 163 | } |
| 164 | |
| 165 | void tst_QLottieEllipse::testAnimatedInitialHeight() |
| 166 | { |
| 167 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 168 | updateProperty(0); |
| 169 | |
| 170 | QVERIFY(qFuzzyCompare(m_ellipse->size().height(), 80.0)); |
| 171 | } |
| 172 | |
| 173 | void tst_QLottieEllipse::testAnimatedUpdatedX() |
| 174 | { |
| 175 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 176 | updateProperty(179); |
| 177 | |
| 178 | QVERIFY(qFuzzyCompare(m_ellipse->position().x(), 50.0)); |
| 179 | } |
| 180 | |
| 181 | void tst_QLottieEllipse::testAnimatedUpdatedY() |
| 182 | { |
| 183 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 184 | updateProperty(179); |
| 185 | |
| 186 | QVERIFY(qFuzzyCompare(m_ellipse->position().y(), 100.0)); |
| 187 | } |
| 188 | |
| 189 | void tst_QLottieEllipse::testAnimatedUpdatedWidth() |
| 190 | { |
| 191 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 192 | updateProperty(179); |
| 193 | |
| 194 | QVERIFY(qFuzzyCompare(m_ellipse->size().width(), 200.0)); |
| 195 | } |
| 196 | |
| 197 | void tst_QLottieEllipse::testAnimatedUpdatedHeight() |
| 198 | { |
| 199 | loadTestData("ellipse_animated_100x80at00to200x40at50100.json"); |
| 200 | updateProperty(179); |
| 201 | |
| 202 | QVERIFY(qFuzzyCompare(m_ellipse->size().height(), 40.0)); |
| 203 | } |
| 204 | |
| 205 | void tst_QLottieEllipse::testName() |
| 206 | { |
| 207 | loadTestData("ellipse_static_100x80.json"); |
| 208 | QVERIFY(m_ellipse->name() == QString("Ellipse Path 1")); |
| 209 | } |
| 210 | |
| 211 | void tst_QLottieEllipse::testType() |
| 212 | { |
| 213 | loadTestData("ellipse_static_100x80.json"); |
| 214 | QVERIFY(m_ellipse->type() == LOTTIE_SHAPE_ELLIPSE_IX); |
| 215 | } |
| 216 | |
| 217 | void tst_QLottieEllipse::testActive() |
| 218 | { |
| 219 | loadTestData("ellipse_static_100x80.json"); |
| 220 | QVERIFY(m_ellipse->active(100) == true); |
| 221 | |
| 222 | loadTestData("ellipse_hidden.json"); |
| 223 | QVERIFY(m_ellipse->active(100) == false); |
| 224 | } |
| 225 | |
| 226 | void tst_QLottieEllipse::testHidden() |
| 227 | { |
| 228 | loadTestData("ellipse_hidden.json"); |
| 229 | QVERIFY(m_ellipse->hidden() == true); |
| 230 | } |
| 231 | |
| 232 | void tst_QLottieEllipse::testDirection() |
| 233 | { |
| 234 | loadTestData("ellipse_hidden.json"); |
| 235 | QVERIFY(m_ellipse->direction() == 0); |
| 236 | loadTestData("ellipse_direction.json"); |
| 237 | QVERIFY(m_ellipse->direction() == 0); |
| 238 | } |
| 239 | |
| 240 | void tst_QLottieEllipse::loadTestData(const QString &filename) |
| 241 | { |
| 242 | if (m_ellipse) { |
| 243 | delete m_ellipse; |
| 244 | m_ellipse = nullptr; |
| 245 | } |
| 246 | |
| 247 | QFile sourceFile(QFINDTESTDATA(QLatin1String("data/") + filename)); |
| 248 | if (!sourceFile.exists()) |
| 249 | QFAIL("File does not exist"); |
| 250 | if (!sourceFile.open(QIODevice::ReadOnly)) |
| 251 | QFAIL("Cannot read test file"); |
| 252 | |
| 253 | QByteArray json = sourceFile.readAll(); |
| 254 | |
| 255 | sourceFile.close(); |
| 256 | |
| 257 | QJsonDocument doc = QJsonDocument::fromJson(json); |
| 258 | QJsonObject rootObj = doc.object(); |
| 259 | if (rootObj.empty()) |
| 260 | QFAIL("Cannot parse test file"); |
| 261 | |
| 262 | QJsonArray layers = rootObj.value(QLatin1String("layers")).toArray(); |
| 263 | QJsonObject layerObj = layers[0].toObject(); |
| 264 | int type = layerObj.value(QLatin1String("ty")).toInt(); |
| 265 | if (type != 4) |
| 266 | QFAIL("It's not shape layer"); |
| 267 | |
| 268 | QJsonArray shapes = layerObj.value(QLatin1String("shapes")).toArray(); |
| 269 | QJsonArray::const_iterator shapesIt = shapes.constBegin(); |
| 270 | QLottieShape* shape = nullptr; |
| 271 | while (shapesIt != shapes.end()) { |
| 272 | QJsonObject childObj = (*shapesIt).toObject(); |
| 273 | shape = QLottieShape::construct(childObj); |
| 274 | QVERIFY(shape != nullptr); |
| 275 | if (shape->type() == LOTTIE_SHAPE_ELLIPSE_IX) |
| 276 | break; |
| 277 | shapesIt++; |
| 278 | } |
| 279 | |
| 280 | m_ellipse = static_cast<QLottieEllipse*>(shape); |
| 281 | |
| 282 | QVERIFY(m_ellipse != nullptr); |
| 283 | } |
| 284 | |
| 285 | void tst_QLottieEllipse::updateProperty(int frame) |
| 286 | { |
| 287 | m_ellipse->updateProperties(frame); |
| 288 | } |
| 289 | |
| 290 | QTEST_MAIN(tst_QLottieEllipse) |
| 291 | #include "tst_lottieellipse.moc" |