#include #include #include #include #include #include #include #include #include #include #include "backend/formats/AsyncImageResponse.h" #include "backend/formats/EntryImageProvider.h" #include "backend/formats/sources/ZipByteSource.h" // Load one zip member through the generic byte-source pipeline (CBZs are zips). static QImage loadFromCbz(const QString &cbzPath, const QString &entry, const QSize &requestedSize) { ZipByteSource source; if (!source.open(cbzPath)) return {}; return EntryImageProvider::loadImageFromSource(source, entry, requestedSize); } static EntryImageProvider makeCbzProvider() { return EntryImageProvider([] { return std::make_unique(); }, QStringLiteral("cbz")); } // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- static bool addZipEntry(zipFile zf, const char *name, const QByteArray &data) { zip_fileinfo fi{}; if (zipOpenNewFileInZip(zf, name, &fi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK) return false; if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) { zipCloseFileInZip(zf); return false; } return zipCloseFileInZip(zf) == ZIP_OK; } // Create a QByteArray containing a valid PNG for an image of given size and color. static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0)) { QImage img(w, h, QImage::Format_RGB32); img.fill(color); QByteArray data; QBuffer buf(&data); buf.open(QIODevice::WriteOnly); img.save(&buf, "PNG"); return data; } // Build a minimal CBZ containing a single image entry. // Returns the path of the temp file (caller removes). static QString buildCbzWithPng(const char *entryPath, int imgW, int imgH, QRgb color = qRgb(255, 0, 0)) { QTemporaryFile tmp; tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.cbz"); tmp.setAutoRemove(false); if (!tmp.open()) return {}; const QString path = tmp.fileName(); tmp.close(); zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE); if (!zf) return {}; const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH, color)); zipClose(zf, nullptr); return ok ? path : QString{}; } // --------------------------------------------------------------------------- // Test class // --------------------------------------------------------------------------- class EntryImageProviderTest : public QObject { Q_OBJECT private slots: void requestImageReturnsValidImage() { const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); const QImage img = loadFromCbz( cbzPath, QStringLiteral("images/test.png"), QSize()); QFile::remove(cbzPath); QVERIFY(!img.isNull()); QCOMPARE(img.width(), 4); QCOMPARE(img.height(), 4); QCOMPARE(img.size(), QSize(4, 4)); } void requestImageReturnsNullForMissingEntry() { const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); const QImage img = loadFromCbz( cbzPath, QStringLiteral("images/nonexistent.png"), QSize()); QFile::remove(cbzPath); QVERIFY(img.isNull()); } void requestImageReturnsNullWhenNoPath() { const QImage img = loadFromCbz( QString{}, QStringLiteral("images/test.png"), QSize()); QVERIFY(img.isNull()); } void clearResetsState() { const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); const QImage img = loadFromCbz( QString{}, QStringLiteral("images/test.png"), QSize()); QFile::remove(cbzPath); QVERIFY(img.isNull()); } void requestedSizeHonored() { const QString cbzPath = buildCbzWithPng("img.png", 400, 300); QVERIFY(!cbzPath.isEmpty()); const QImage img = loadFromCbz( cbzPath, QStringLiteral("img.png"), QSize(200, -1)); QFile::remove(cbzPath); QVERIFY(!img.isNull()); QCOMPARE(img.width(), 200); } // ----------------------------------------------------------------------- // Async response tests // ----------------------------------------------------------------------- void asyncResponseEmitsFinished() { const QString cbzPath = buildCbzWithPng("img.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); AsyncImageResponse response([cbzPath] { return loadFromCbz( cbzPath, QStringLiteral("img.png"), QSize()); }); QSignalSpy spy(&response, &QQuickImageResponse::finished); response.run(); QFile::remove(cbzPath); QCOMPARE(spy.count(), 1); QVERIFY(response.textureFactory() != nullptr); } void cancelledResponseDoesNotEmitFinished() { const QString cbzPath = buildCbzWithPng("img.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); AsyncImageResponse response([cbzPath] { return loadFromCbz( cbzPath, QStringLiteral("img.png"), QSize()); }); QSignalSpy spy(&response, &QQuickImageResponse::finished); response.cancel(); response.run(); QFile::remove(cbzPath); QCOMPARE(spy.count(), 0); } void cancelledTextureFactoryIsNull() { const QString cbzPath = buildCbzWithPng("img.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); AsyncImageResponse response([cbzPath] { return loadFromCbz( cbzPath, QStringLiteral("img.png"), QSize()); }); response.cancel(); response.run(); QFile::remove(cbzPath); QVERIFY(response.textureFactory() == nullptr); } // ----------------------------------------------------------------------- // Embedded-path URL format tests // ----------------------------------------------------------------------- void coverImageUrlEmbedsCbzPath() { const QString cbzPath = buildCbzWithPng("img.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); EntryImageProvider provider = makeCbzProvider(); provider.setFilePath(cbzPath); const QString url = provider.coverImageUrl(QStringLiteral("cover.jpg")); QFile::remove(cbzPath); QVERIFY2(url.startsWith(QStringLiteral("image://cbz/cover.jpg?src=")), qPrintable("Expected query-param URL, got: " + url)); const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(cbzPath)); QVERIFY2(url.contains(encodedPath), qPrintable("URL should contain encoded cbz path. URL: " + url)); } void requestImageResponseUsesEmbeddedPath() { const QString cbzA = buildCbzWithPng("img.png", 4, 4, qRgb(255, 0, 0)); const QString cbzB = buildCbzWithPng("img.png", 4, 4, qRgb(0, 255, 0)); QVERIFY(!cbzA.isEmpty() && !cbzB.isEmpty()); EntryImageProvider provider = makeCbzProvider(); provider.setFilePath(cbzA); // configured for A // Build an embedded-format id pointing to B via query parameter const QString encodedB = QString::fromUtf8(QUrl::toPercentEncoding(cbzB)); const QString id = QStringLiteral("img.png?src=") + encodedB; QQuickImageResponse *resp = provider.requestImageResponse(id, QSize()); QSignalSpy spy(resp, &QQuickImageResponse::finished); QThreadPool::globalInstance()->waitForDone(); if (spy.isEmpty()) spy.wait(5000); QFile::remove(cbzA); QFile::remove(cbzB); QVERIFY(resp->textureFactory() != nullptr); const QImage img = resp->textureFactory()->image(); QVERIFY(!img.isNull()); QCOMPARE(img.pixel(0, 0), qRgb(0, 255, 0)); delete resp; } void requestImageResponseFallsBackToProviderPath() { const QString cbzA = buildCbzWithPng("img.png", 4, 4, qRgb(255, 0, 0)); QVERIFY(!cbzA.isEmpty()); EntryImageProvider provider = makeCbzProvider(); provider.setFilePath(cbzA); QQuickImageResponse *resp = provider.requestImageResponse( QStringLiteral("img.png"), QSize()); QSignalSpy spy(resp, &QQuickImageResponse::finished); QThreadPool::globalInstance()->waitForDone(); if (spy.isEmpty()) spy.wait(5000); QFile::remove(cbzA); QVERIFY(resp->textureFactory() != nullptr); const QImage img = resp->textureFactory()->image(); QVERIFY(!img.isNull()); QCOMPARE(img.pixel(0, 0), qRgb(255, 0, 0)); delete resp; } void responseAfterClearIsNull() { const QString cbzPath = buildCbzWithPng("img.png", 4, 4); QVERIFY(!cbzPath.isEmpty()); EntryImageProvider provider = makeCbzProvider(); provider.setFilePath(cbzPath); provider.clear(); QQuickImageResponse *resp = provider.requestImageResponse( QStringLiteral("img.png"), QSize()); QThreadPool::globalInstance()->waitForDone(); QFile::remove(cbzPath); QVERIFY(resp->textureFactory() == nullptr); delete resp; } }; QTEST_MAIN(EntryImageProviderTest) #include "EntryImageProviderTest.moc"