#include #include #include #include #include #include #include #include "backend/formats/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> &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> &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.chapterCount(), 1); const Chapter ch = reader.loadChapter(0); QCOMPARE(ch.contentChunks.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 Chapter ch = reader.loadChapter(0); QCOMPARE(ch.contentChunks.size(), 3); QVERIFY(ch.contentChunks.at(0).contains(QLatin1String("page001.png"))); QVERIFY(ch.contentChunks.at(1).contains(QLatin1String("page002.png"))); QVERIFY(ch.contentChunks.at(2).contains(QLatin1String("page003.png"))); } void oneChunkPerPage() { const QString path = makeCbz({ {"p01.jpg", makePng()}, {"p02.jpg", makePng()}, }); QVERIFY(!path.isEmpty()); CbzReader reader; QVERIFY(reader.open(path)); const Chapter ch = reader.loadChapter(0); QCOMPARE(ch.contentChunks.size(), 2); // Each chunk is an HTML fragment with a single image://cbz/ img tag for (const QString &chunk : ch.contentChunks) { QVERIFY(chunk.contains(QLatin1String("image://cbz/"))); QCOMPARE(chunk.count(QLatin1String("" "My ComicArt Spiegelman"; 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 = "" "Batman42"; 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 = "" "" " Special Edition" " Batman42" ""; 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()); } void tableOfContentsIsEmpty() { const QString path = makeCbz({{"page001.png", makePng()}}); QVERIFY(!path.isEmpty()); CbzReader reader; QVERIFY(reader.open(path)); QVERIFY(reader.tableOfContents().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 = "" "" " Test" " " " " " " " " ""; 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 = "" "Light Meta"; 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.loadChapter(0).contentChunks.size(), 1); } }; QTEST_MAIN(CbzReaderTest) #include "CbzReaderTest.moc"