151 lines
4.7 KiB
C++
151 lines
4.7 KiB
C++
#include <QtTest/QtTest>
|
|
#include <QBuffer>
|
|
#include <QDir>
|
|
#include <QImage>
|
|
#include <QSignalSpy>
|
|
#include <QTemporaryFile>
|
|
#include <QUrl>
|
|
|
|
#include <minizip/zip.h>
|
|
|
|
#include "backend/HistoryBookModel.h"
|
|
#include "backend/FormatReader.h"
|
|
#include "backend/formats/comic/CbzReader.h"
|
|
#include "backend/formats/FormatRegistry.h"
|
|
#include "backend/formats/sources/ZipByteSource.h"
|
|
|
|
static FormatRegistry makeRegistry()
|
|
{
|
|
return FormatRegistry({
|
|
{QStringLiteral("cbz"), false, QStringLiteral("cbz"),
|
|
[] { return std::make_unique<CbzReader>(); },
|
|
[] { return std::make_unique<ZipByteSource>(); }},
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
static bool addEntry(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 = 4, int h = 4, 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 buildCbzWithCover()
|
|
{
|
|
static const char kComicInfo[] =
|
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
|
"<ComicInfo>"
|
|
" <Title>Cover Test Book</Title>"
|
|
" <Writer>Test Author</Writer>"
|
|
"</ComicInfo>";
|
|
|
|
QTemporaryFile tmp;
|
|
tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_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 {};
|
|
|
|
bool ok = true;
|
|
ok = ok && addEntry(zf, "ComicInfo.xml", QByteArray(kComicInfo));
|
|
ok = ok && addEntry(zf, "001_cover.png", makePng());
|
|
|
|
zipClose(zf, nullptr);
|
|
return ok ? path : QString{};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test class
|
|
// ---------------------------------------------------------------------------
|
|
|
|
class HistoryBookModelTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void loadMetadataPopulatesCoverImageUrlForCbz()
|
|
{
|
|
const QString cbzPath = buildCbzWithCover();
|
|
QVERIFY(!cbzPath.isEmpty());
|
|
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryBookModel model(cbzPath, 0.0, QString(), registry);
|
|
|
|
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
|
QSignalSpy coverSpy(&model, &HistoryBookModel::coverImageLoaded);
|
|
model.loadMetadata();
|
|
|
|
QVERIFY(spy.wait(5000));
|
|
|
|
QFile::remove(cbzPath);
|
|
|
|
QCOMPARE(coverSpy.count(), 1);
|
|
|
|
const QString url = model.coverImageUrl();
|
|
QVERIFY2(!url.isEmpty(), "coverImageUrl should be set after loadMetadata for a CBZ with cover");
|
|
QVERIFY2(url.startsWith(QStringLiteral("image://cbz/001_cover.png?src=")),
|
|
qPrintable("Expected query-param URL format, got: " + url));
|
|
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(cbzPath));
|
|
QVERIFY2(url.contains(encodedPath),
|
|
qPrintable("URL should contain encoded cbz path. URL: " + url));
|
|
}
|
|
|
|
void loadMetadataLeavesCoverImageUrlEmptyForMissingFile()
|
|
{
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryBookModel model(QStringLiteral("/nonexistent/file.cbz"), 0.0, QString(), registry);
|
|
|
|
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
|
model.loadMetadata();
|
|
|
|
// Should finish or not crash, coverImageUrl remains empty
|
|
QVERIFY2(model.coverImageUrl().isEmpty(),
|
|
"Missing files have no cover; coverImageUrl should remain empty");
|
|
}
|
|
|
|
void loadMetadataPopulatesTitle()
|
|
{
|
|
const QString cbzPath = buildCbzWithCover();
|
|
QVERIFY(!cbzPath.isEmpty());
|
|
|
|
FormatRegistry registry = makeRegistry();
|
|
HistoryBookModel model(cbzPath, 0.0, QString(), registry);
|
|
|
|
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
|
model.loadMetadata();
|
|
QVERIFY(spy.wait(5000));
|
|
|
|
QFile::remove(cbzPath);
|
|
|
|
QCOMPARE(model.title(), QStringLiteral("Cover Test Book"));
|
|
QCOMPARE(model.author(), QStringLiteral("Test Author"));
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(HistoryBookModelTest)
|
|
#include "HistoryBookModelTest.moc"
|