Files
asteria-qt/tests/formats/CbzReaderTest.cpp
T
2026-10-25 15:10:38 +01:00

379 lines
10 KiB
C++

#include <QtTest/QtTest>
#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QImage>
#include <QTemporaryFile>
#include <minizip/zip.h>
#include "backend/formats/comic/CbzReader.h"
// ---------------------------------------------------------------------------
// Fixture helpers
// ---------------------------------------------------------------------------
static QByteArray makePng(int w = 8, int h = 8, QColor color = Qt::blue)
{
QImage img(w, h, QImage::Format_RGB888);
img.fill(color);
QByteArray data;
QBuffer buf(&data);
buf.open(QIODevice::WriteOnly);
img.save(&buf, "PNG");
return data;
}
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;
}
// Creates a CBZ at a temp path with the given entries (name -> bytes).
// Caller must delete the returned path when done.
static QString buildCbz(const QList<QPair<QString, QByteArray>> &entries)
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/cbz_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) {
QFile::remove(path);
return {};
}
for (const auto &[name, data] : entries) {
if (!addEntry(zf, name.toUtf8().constData(), data)) {
zipClose(zf, nullptr);
QFile::remove(path);
return {};
}
}
zipClose(zf, nullptr);
return path;
}
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
class CbzReaderTest : public QObject {
Q_OBJECT
QStringList m_tempFiles;
QString makeCbz(const QList<QPair<QString, QByteArray>> &entries)
{
const QString p = buildCbz(entries);
if (!p.isEmpty())
m_tempFiles.append(p);
return p;
}
private slots:
void cleanupTestCase()
{
for (const QString &p : m_tempFiles)
QFile::remove(p);
}
// --- error cases ---
void openNonexistentFile()
{
CbzReader reader;
QVERIFY(!reader.open("/does/not/exist.cbz"));
QVERIFY(!reader.lastError().isEmpty());
}
void openNonZipFile()
{
QTemporaryFile tmp;
tmp.setAutoRemove(false);
QVERIFY(tmp.open());
tmp.write("this is not a zip file");
tmp.close();
m_tempFiles.append(tmp.fileName());
CbzReader reader;
QVERIFY(!reader.open(tmp.fileName()));
QVERIFY(!reader.lastError().isEmpty());
}
void openZipWithNoImages()
{
const QString path = makeCbz({{"readme.txt", "hello"}});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(!reader.open(path));
QVERIFY(!reader.lastError().isEmpty());
}
// --- page count and chapter structure ---
void pageCount()
{
const QString path = makeCbz({
{"page001.jpg", makePng()},
{"page002.jpg", makePng(8, 8, Qt::red)},
{"page003.jpg", makePng(8, 8, Qt::green)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.pages().size(), 3);
}
void pagesAreSorted()
{
// Add images in reverse order — they must come out sorted
const QString path = makeCbz({
{"page003.png", makePng(8, 8, Qt::red)},
{"page001.png", makePng(8, 8, Qt::blue)},
{"page002.png", makePng(8, 8, Qt::green)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
const auto pages = reader.pages();
QCOMPARE(pages.size(), 3);
QVERIFY(pages.at(0).imageUrl.contains(QLatin1String("page001.png")));
QVERIFY(pages.at(1).imageUrl.contains(QLatin1String("page002.png")));
QVERIFY(pages.at(2).imageUrl.contains(QLatin1String("page003.png")));
}
void pagesHaveValidImageUrls()
{
const QString path = makeCbz({
{"p01.jpg", makePng()},
{"p02.jpg", makePng()},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
const auto pages = reader.pages();
QCOMPARE(pages.size(), 2);
for (const PageInfo &page : pages) {
QVERIFY(page.imageUrl.startsWith(QLatin1String("image://cbz/")));
}
}
void emptyPagesBeforeOpen()
{
CbzReader reader;
QVERIFY(reader.pages().isEmpty());
}
// --- metadata: ComicInfo.xml present ---
void metadataTitle()
{
const QByteArray comicInfo =
"<?xml version=\"1.0\"?>"
"<ComicInfo><Title>My Comic</Title><Writer>Art Spiegelman</Writer></ComicInfo>";
const QString path = makeCbz({
{"ComicInfo.xml", comicInfo},
{"page001.jpg", makePng()},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.title(), QStringLiteral("My Comic"));
QCOMPARE(reader.author(), QStringLiteral("Art Spiegelman"));
}
void metadataSeriesAndNumber()
{
const QByteArray comicInfo =
"<?xml version=\"1.0\"?>"
"<ComicInfo><Series>Batman</Series><Number>42</Number></ComicInfo>";
const QString path = makeCbz({
{"ComicInfo.xml", comicInfo},
{"page001.jpg", makePng()},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.title(), QStringLiteral("Batman #42"));
}
void metadataTitleTakesPriorityOverSeries()
{
const QByteArray comicInfo =
"<?xml version=\"1.0\"?>"
"<ComicInfo>"
" <Title>Special Edition</Title>"
" <Series>Batman</Series><Number>42</Number>"
"</ComicInfo>";
const QString path = makeCbz({
{"ComicInfo.xml", comicInfo},
{"page001.jpg", makePng()},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.title(), QStringLiteral("Special Edition"));
}
// --- metadata: no ComicInfo.xml ---
void metadataFallbackToFilename()
{
const QString path = makeCbz({{"page001.png", makePng()}});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QVERIFY(!reader.title().isEmpty());
// Title must equal the file's base name (no extension)
const QString expected = QFileInfo(path).completeBaseName();
QCOMPARE(reader.title(), expected);
QVERIFY(reader.author().isEmpty());
}
// --- cover ---
void coverFallbackToFirstSortedPage()
{
const QString path = makeCbz({
{"page002.jpg", makePng(8, 8, Qt::red)},
{"page001.jpg", makePng(8, 8, Qt::blue)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.coverImagePath(), QStringLiteral("page001.jpg"));
}
void coverFromComicInfoFrontCover()
{
// Page Image="1" is the front cover → second sorted image
const QByteArray comicInfo =
"<?xml version=\"1.0\"?>"
"<ComicInfo>"
" <Title>Test</Title>"
" <Pages>"
" <Page Image=\"0\" />"
" <Page Image=\"1\" Type=\"FrontCover\" />"
" </Pages>"
"</ComicInfo>";
const QString path = makeCbz({
{"ComicInfo.xml", comicInfo},
{"page001.jpg", makePng(8, 8, Qt::blue)},
{"page002.jpg", makePng(8, 8, Qt::red)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.coverImagePath(), QStringLiteral("page002.jpg"));
}
// --- content hash ---
void contentHashIsStable()
{
const QString path = makeCbz({{"page001.png", makePng()}});
QVERIFY(!path.isEmpty());
CbzReader r1, r2;
QVERIFY(r1.open(path));
QVERIFY(r2.open(path));
QVERIFY(!r1.contentHash().isEmpty());
QCOMPARE(r1.contentHash(), r2.contentHash());
}
void contentHashEmptyBeforeOpen()
{
CbzReader reader;
QVERIFY(reader.contentHash().isEmpty());
}
// --- loadMetadata lightweight path ---
void loadMetadataSetsTitle()
{
const QByteArray comicInfo =
"<?xml version=\"1.0\"?>"
"<ComicInfo><Title>Light Meta</Title></ComicInfo>";
const QString path = makeCbz({
{"ComicInfo.xml", comicInfo},
{"page001.jpg", makePng()},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.loadMetadata(path));
QCOMPARE(reader.title(), QStringLiteral("Light Meta"));
// contentHash must be empty (loadMetadata is lightweight)
QVERIFY(reader.contentHash().isEmpty());
}
void loadMetadataSetsCover()
{
const QString path = makeCbz({
{"page001.jpg", makePng(8, 8, Qt::blue)},
{"page002.jpg", makePng(8, 8, Qt::red)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.loadMetadata(path));
QCOMPARE(reader.coverImagePath(), QStringLiteral("page001.jpg"));
}
// --- file type ---
void fileType()
{
CbzReader reader;
QCOMPARE(reader.fileType(), QStringLiteral("cbz"));
}
// --- skips __MACOSX entries ---
void macosxEntriesSkipped()
{
const QString path = makeCbz({
{"__MACOSX/._page001.jpg", makePng(8, 8, Qt::red)},
{"page001.jpg", makePng(8, 8, Qt::blue)},
});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.pages().size(), 1);
}
};
QTEST_MAIN(CbzReaderTest)
#include "CbzReaderTest.moc"