Files
asteria-qt/tests/EntryImageProviderTest.cpp
T
2026-10-25 15:43:51 +01:00

243 lines
7.3 KiB
C++

#include <QtTest/QtTest>
#include <QBuffer>
#include <QDir>
#include <QImage>
#include <QSignalSpy>
#include <QSize>
#include <QTemporaryFile>
#include <QThreadPool>
#include <QUrl>
#include <minizip/zip.h>
#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<ZipByteSource>(); },
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;
}
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;
}
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 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 (stateless provider)
// -----------------------------------------------------------------------
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();
// 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 requestImageResponseWithoutSourcePathReturnsNull()
{
EntryImageProvider provider = makeCbzProvider();
// Id without ?src= has no container to load from
QQuickImageResponse *resp = provider.requestImageResponse(
QStringLiteral("img.png"), QSize());
QThreadPool::globalInstance()->waitForDone();
QVERIFY(resp->textureFactory() == nullptr);
delete resp;
}
};
QTEST_MAIN(EntryImageProviderTest)
#include "EntryImageProviderTest.moc"