File indexing completed on 2024-11-03 10:43:40

0001 // SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
0002 // SPDX-License-Identifier: GPL-3.0-or-later
0003 
0004 #include <QtTest/QtTest>
0005 
0006 #include "utils/blurhash.hpp"
0007 #include "utils/blurhashimageprovider.h"
0008 
0009 class BlurHashTest : public QObject
0010 {
0011     Q_OBJECT
0012 
0013     QImage goodBlurHashImage;
0014     QImage specialBlurHashImage;
0015     BlurhashImageProvider *imageProvider;
0016 
0017 private Q_SLOTS:
0018     void initTestCase()
0019     {
0020         goodBlurHashImage = QImage(QLatin1String(DATA_DIR) + QLatin1String("/blurhash.png"));
0021         specialBlurHashImage = QImage(QLatin1String(DATA_DIR) + QLatin1String("/blurhash2.png"));
0022         imageProvider = new BlurhashImageProvider();
0023     }
0024 
0025     void testImageProviderEncoding()
0026     {
0027         const QString blurHashEncoded = QStringLiteral(
0028             "%7CKO2%3FU%252Tw%3DwR6cErDEhOD%5D%7ERBVZRip0W9ofwxM_%7D;RPxuwH%253s89%5Dt8%24%25tLOtxZ%25gixtQt8IUS%23I.ENa0NZIVt6xFM%7BM%7B%251j%5EM_bcRPX9nht7n%"
0029             "2Bj%5BrrW;ni%25Mt7V%40W;t7t8%251bbxat7WBIUR%2ARjRjRjxuRjs.MxbbV%40WY");
0030 
0031         auto response = dynamic_cast<AsyncImageResponse *>(imageProvider->requestImageResponse(blurHashEncoded, QSize(25, 25)));
0032         QSignalSpy spy(response, &QQuickImageResponse::finished);
0033         spy.wait();
0034 
0035         QCOMPARE(response->m_image.size(), QSize(25, 25));
0036         QCOMPARE(response->m_image, goodBlurHashImage);
0037     }
0038 
0039     void testImageProviderSpecialEncoding()
0040     {
0041         const QString blurHashEncoded = QStringLiteral("URI#cIR+L1%14eoJtAWYXMt5IAob4oRQfiRR");
0042 
0043         auto response = dynamic_cast<AsyncImageResponse *>(imageProvider->requestImageResponse(blurHashEncoded, QSize(25, 25)));
0044         QSignalSpy spy(response, &QQuickImageResponse::finished);
0045         spy.wait();
0046 
0047         QCOMPARE(response->m_image.size(), QSize(25, 25));
0048         QCOMPARE(response->m_image, specialBlurHashImage);
0049     }
0050 
0051     void testBlurHashAlgo()
0052     {
0053         const QByteArray blurHash = QByteArrayLiteral(
0054             "|KO2?U%2Tw=wR6cErDEhOD]~RBVZRip0W9ofwxM_};RPxuwH%3s89]t8$%tLOtxZ%gixtQt8IUS#I.ENa0NZIVt6xFM{M{%1j^M_bcRPX9nht7n+j[rrW;ni%Mt7V@W;t7t8%1bbxat7WBIUR*"
0055             "RjRjRjxuRjs.MxbbV@WY");
0056 
0057         auto data = blurhash::decode(blurHash.constData(), 25, 25);
0058         QImage image(data.image.data(), 25, 25, 25 * 3, QImage::Format_RGB888);
0059         image = image.convertToFormat(QImage::Format_RGB32);
0060 
0061         QCOMPARE(image, goodBlurHashImage);
0062     }
0063 };
0064 
0065 QTEST_MAIN(BlurHashTest)
0066 #include "blurhashtest.moc"