old render cleanup part 2

This commit is contained in:
Agel committed 2026-10-25 15:10:38 +01:00
1 parent acc7e71e7b
commit cef762c3ee
40 files changed
+329 -3643

No files matched your search

+14 -40
View File
@@ -137,10 +137,7 @@ private slots:
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.chapterCount(), 1);
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.contentChunks.size(), 3);
QCOMPARE(reader.pages().size(), 3);
}
void pagesAreSorted()
@@ -155,14 +152,14 @@ private slots:
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")));
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 oneChunkPerPage()
void pagesHaveValidImageUrls()
{
const QString path = makeCbz({
{"p01.jpg", makePng()},
@@ -172,31 +169,18 @@ private slots:
CbzReader reader;
QVERIFY(reader.open(path));
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.contentChunks.size(), 2);
const auto pages = reader.pages();
QCOMPARE(pages.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("<img")), 1);
for (const PageInfo &page : pages) {
QVERIFY(page.imageUrl.startsWith(QLatin1String("image://cbz/")));
}
}
void outOfRangeChapterReturnsEmpty()
{
const QString path = makeCbz({{"page001.png", makePng()}});
QVERIFY(!path.isEmpty());
CbzReader reader;
QVERIFY(reader.open(path));
const Chapter ch = reader.loadChapter(5);
QVERIFY(ch.contentChunks.isEmpty());
}
void emptyChapterCountBeforeOpen()
void emptyPagesBeforeOpen()
{
CbzReader reader;
QCOMPARE(reader.chapterCount(), 0);
QVERIFY(reader.pages().isEmpty());
}
// --- metadata: ComicInfo.xml present ---
@@ -272,16 +256,6 @@ private slots:
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()
@@ -396,7 +370,7 @@ private slots:
CbzReader reader;
QVERIFY(reader.open(path));
QCOMPARE(reader.loadChapter(0).contentChunks.size(), 1);
QCOMPARE(reader.pages().size(), 1);
}
};
-614
View File
@@ -1,614 +0,0 @@
#include <QtTest/QtTest>
#include <QDir>
#include <QFile>
#include <QTemporaryFile>
#include <minizip/zip.h>
#include "backend/formats/EpubReader.h"
// ---------------------------------------------------------------------------
// Minimal EPUB fixture builder (uses minizip directly)
// ---------------------------------------------------------------------------
static const char kMimetype[] = "application/epub+zip";
static const char kContainer[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">"
" <rootfiles>"
" <rootfile full-path=\"OEBPS/content.opf\""
" media-type=\"application/oebps-package+xml\"/>"
" </rootfiles>"
"</container>";
static const char kOpf[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\" unique-identifier=\"BookId\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Test Book</dc:title>"
" <dc:creator>Test Author</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" <item id=\"ch2\" href=\"chapter2.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine toc=\"ncx\">"
" <itemref idref=\"ch1\"/>"
" <itemref idref=\"ch2\"/>"
" </spine>"
"</package>";
static const char kNcx[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">"
" <navMap>"
" <navPoint id=\"np1\" playOrder=\"1\">"
" <navLabel><text>Chapter One</text></navLabel>"
" <content src=\"chapter1.xhtml\"/>"
" </navPoint>"
" <navPoint id=\"np2\" playOrder=\"2\">"
" <navLabel><text>Chapter Two</text></navLabel>"
" <content src=\"chapter2.xhtml\"/>"
" </navPoint>"
" </navMap>"
"</ncx>";
static const char kChapter1[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Chapter One</title></head>"
" <body>"
" <h1>Chapter One</h1>"
" <p>First paragraph of chapter one.</p>"
" <p>Second paragraph of chapter one.</p>"
" </body>"
"</html>";
static const char kChapter2[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Chapter Two</title></head>"
" <body>"
" <p>This is chapter two content.</p>"
" </body>"
"</html>";
// Add one entry to an open zipFile handle.
static bool addEntry(zipFile zf, const char *name, const QByteArray &data, bool store = false)
{
zip_fileinfo fi{};
int method = store ? Z_NO_COMPRESSION : Z_DEFLATED;
int level = store ? 0 : Z_DEFAULT_COMPRESSION;
if (zipOpenNewFileInZip(zf, name, &fi,
nullptr, 0, nullptr, 0, nullptr,
method, level) != ZIP_OK)
return false;
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
zipCloseFileInZip(zf);
return false;
}
return zipCloseFileInZip(zf) == ZIP_OK;
}
// Build a minimal two-chapter EPUB and return its file path (caller deletes).
static QString buildSampleEpub()
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_test_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), /*store=*/true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(kOpf));
ok = ok && addEntry(zf, "OEBPS/toc.ncx", QByteArray(kNcx));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
ok = ok && addEntry(zf, "OEBPS/chapter2.xhtml", QByteArray(kChapter2));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with an <img> tag in the chapter (image at OEBPS/Images/test.png).
static QString buildEpubWithImage()
{
static const char opfImg[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Image Book</dc:title>"
" <dc:creator>Tester</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" <item id=\"img\" href=\"Images/test.png\" media-type=\"image/png\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
static const char chapterImg[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Image Chapter</title></head>"
" <body>"
" <p>Chapter with image</p>"
" <img src=\"Images/test.png\"/>"
" </body>"
"</html>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_img_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfImg));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(chapterImg));
ok = ok && addEntry(zf, "OEBPS/Images/test.png", QByteArray("DUMMY_PNG"));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with a chapter in a subdirectory and a relative image path.
static QString buildEpubWithRelativeImage()
{
static const char opfRel[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Relative Image Book</dc:title>"
" <dc:creator>Tester</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"Text/chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" <item id=\"img\" href=\"Images/test.png\" media-type=\"image/png\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
static const char chapterRel[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Relative Image Chapter</title></head>"
" <body>"
" <p>Chapter with relative image</p>"
" <img src=\"../Images/test.png\"/>"
" </body>"
"</html>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_relimg_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfRel));
ok = ok && addEntry(zf, "OEBPS/Text/chapter1.xhtml", QByteArray(chapterRel));
ok = ok && addEntry(zf, "OEBPS/Images/test.png", QByteArray("DUMMY_PNG"));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB without an NCX.
static QString buildEpubNoNcx()
{
static const char opfNoNcx[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>No TOC Book</dc:title>"
" <dc:creator>Nobody</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_notoc_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfNoNcx));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with one large chapter (>4096 chars of <p> elements).
static QString buildEpubLargeChapter()
{
QString body;
for (int i = 0; i < 60; ++i)
body += QString("<p>Paragraph %1: The quick brown fox jumped over the lazy dog repeatedly.</p>\n").arg(i + 1);
QByteArray chapterXhtml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
"<head><title>Big</title></head>"
"<body>" + body.toUtf8() + "</body>"
"</html>";
static const char opfLarge[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Large</dc:title>"
" <dc:creator>Writer</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_large_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfLarge));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", chapterXhtml);
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with a cover image using both meta tag and properties attribute
static QString buildEpubWithCoverImage()
{
static const char opfCover[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Cover Book</dc:title>"
" <dc:creator>Cover Tester</dc:creator>"
" <meta name=\"cover\" content=\"cover-id\"/>"
" </metadata>"
" <manifest>"
" <item id=\"cover-id\" href=\"cover.jpg\" media-type=\"image/jpeg\" properties=\"cover-image\"/>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_cover_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfCover));
ok = ok && addEntry(zf, "OEBPS/cover.jpg", QByteArray("DUMMY_JPEG"));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with cover image in subdirectory
static QString buildEpubWithCoverImageInSubdir()
{
static const char opfCoverSubdir[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Cover Subdir Book</dc:title>"
" <dc:creator>Subdir Tester</dc:creator>"
" <meta name=\"cover\" content=\"cover-img\"/>"
" </metadata>"
" <manifest>"
" <item id=\"cover-img\" href=\"Images/cover.jpg\" media-type=\"image/jpeg\" properties=\"cover-image\"/>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_coversub_XXXXXX.epub");
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, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfCoverSubdir));
ok = ok && addEntry(zf, "OEBPS/Images/cover.jpg", QByteArray("DUMMY_JPEG"));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter1));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
class EpubReaderTest : public QObject {
Q_OBJECT
private:
QString m_epubPath;
QString m_noNcxPath;
QString m_largePath;
QString m_imgPath;
QString m_relImgPath;
QString m_coverPath;
QString m_coverSubdirPath;
private slots:
void initTestCase()
{
m_epubPath = buildSampleEpub();
m_noNcxPath = buildEpubNoNcx();
m_largePath = buildEpubLargeChapter();
m_imgPath = buildEpubWithImage();
m_relImgPath = buildEpubWithRelativeImage();
m_coverPath = buildEpubWithCoverImage();
m_coverSubdirPath = buildEpubWithCoverImageInSubdir();
QVERIFY2(!m_epubPath.isEmpty(), "Failed to build sample EPUB fixture");
QVERIFY2(!m_noNcxPath.isEmpty(), "Failed to build no-NCX EPUB fixture");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large chapter EPUB fixture");
QVERIFY2(!m_imgPath.isEmpty(), "Failed to build image EPUB fixture");
QVERIFY2(!m_relImgPath.isEmpty(), "Failed to build relative-image EPUB fixture");
QVERIFY2(!m_coverPath.isEmpty(), "Failed to build cover image EPUB fixture");
QVERIFY2(!m_coverSubdirPath.isEmpty(), "Failed to build cover image subdir EPUB fixture");
}
void cleanupTestCase()
{
QFile::remove(m_epubPath);
QFile::remove(m_noNcxPath);
QFile::remove(m_largePath);
QFile::remove(m_imgPath);
QFile::remove(m_relImgPath);
QFile::remove(m_coverPath);
QFile::remove(m_coverSubdirPath);
}
// --- Phase 2: error handling ---
void openNonexistentFile()
{
EpubReader reader;
QVERIFY(!reader.open("/tmp/does_not_exist_xyzzy.epub"));
QVERIFY(!reader.lastError().isEmpty());
}
void openInvalidZip()
{
QTemporaryFile tmp;
QVERIFY(tmp.open());
tmp.write("This is not a zip file.\n");
tmp.flush();
EpubReader reader;
QVERIFY(!reader.open(tmp.fileName()));
QVERIFY(!reader.lastError().isEmpty());
}
// --- Phase 3: metadata ---
void openValidEpub()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
}
void titleExtracted()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
QCOMPARE(reader.title(), QStringLiteral("Test Book"));
}
void authorExtracted()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
}
// --- Phase 4: chapters ---
void chapterCountMatchesSpine()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
QCOMPARE(reader.chapterCount(), 2);
}
void loadChapterContent()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.index, 0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains("First paragraph"));
}
void loadChapterTwo()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
Chapter ch = reader.loadChapter(1);
QCOMPARE(ch.index, 1);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains("chapter two"));
}
void loadChapterOutOfRange()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
Chapter ch = reader.loadChapter(99);
QVERIFY(ch.contentChunks.isEmpty());
}
// --- Phase 5: TOC ---
void tableOfContentsFromNcx()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
const QList<TocEntry> toc = reader.tableOfContents();
QCOMPARE(toc.size(), 2);
QCOMPARE(toc[0].title, QStringLiteral("Chapter One"));
QCOMPARE(toc[0].chapterIndex, 0);
QCOMPARE(toc[0].depth, 0);
QCOMPARE(toc[1].title, QStringLiteral("Chapter Two"));
QCOMPARE(toc[1].chapterIndex, 1);
}
void epubWithNoNcx()
{
EpubReader reader;
QVERIFY(reader.open(m_noNcxPath));
QVERIFY(reader.tableOfContents().isEmpty());
QCOMPARE(reader.chapterCount(), 1);
}
// --- Phase 6: chunking ---
void largeChapterProducesMultipleChunks()
{
EpubReader reader;
QVERIFY(reader.open(m_largePath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
}
void chunksAreStandaloneHtml()
{
EpubReader reader;
QVERIFY(reader.open(m_largePath));
const Chapter ch = reader.loadChapter(0);
for (const QString &chunk : ch.contentChunks) {
const bool hasBlock = chunk.contains("<p") || chunk.contains("<h1") ||
chunk.contains("<h2") || chunk.contains("<div");
QVERIFY2(hasBlock, "Chunk lacks any block element");
}
}
// --- Phase 7: image URL rewriting ---
void imageTagsRewrittenToProviderUrls()
{
EpubReader reader;
QVERIFY(reader.open(m_imgPath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
const QString expected = QStringLiteral("image://epub/OEBPS/Images/test.png");
QVERIFY2(combined.contains(expected),
qPrintable(QStringLiteral("Expected ") + expected + QStringLiteral(" in: ") + combined));
}
void relativeImagePathResolved()
{
EpubReader reader;
QVERIFY(reader.open(m_relImgPath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
const QString expected = QStringLiteral("image://epub/OEBPS/Images/test.png");
QVERIFY2(combined.contains(expected),
qPrintable(QStringLiteral("Expected ") + expected + QStringLiteral(" in: ") + combined));
}
// --- Phase 8: cover image detection ---
void coverImagePathExtracted()
{
EpubReader reader;
QVERIFY(reader.open(m_coverPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("OEBPS/cover.jpg"));
}
void coverImagePathWithSubdirectory()
{
EpubReader reader;
QVERIFY(reader.open(m_coverSubdirPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("OEBPS/Images/cover.jpg"));
}
void coverImagePathEmptyForEpubWithoutCover()
{
EpubReader reader;
QVERIFY(reader.open(m_epubPath));
QVERIFY(reader.coverImagePath().isEmpty());
}
};
QTEST_MAIN(EpubReaderTest)
#include "EpubReaderTest.moc"
-384
View File
@@ -1,384 +0,0 @@
#include <QtTest/QtTest>
#include <QDir>
#include <QFile>
#include <QTemporaryFile>
#include "backend/formats/Fb2Reader.h"
// ---------------------------------------------------------------------------
// Minimal FB2 fixture builder
// ---------------------------------------------------------------------------
static const char kFb2Header[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\""
" xmlns:l=\"http://www.w3.org/1999/xlink\">";
static const char kFb2Footer[] = "</FictionBook>";
static QString writeTempFb2(const QByteArray &content)
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/fb2_test_XXXXXX.fb2");
tmp.setAutoRemove(false);
if (!tmp.open()) return {};
tmp.write(content);
tmp.flush();
return tmp.fileName();
}
static QString buildSampleFb2()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Test Book</book-title>"
" <author>"
" <first-name>Test</first-name>"
" <last-name>Author</last-name>"
" </author>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Chapter One</p></title>"
" <p>First paragraph of chapter one.</p>"
" <p>Second paragraph of chapter one.</p>"
" </section>"
" <section>"
" <title><p>Chapter Two</p></title>"
" <p>This is chapter two content.</p>"
" </section>"
"</body>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildFb2WithCover()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Cover Book</book-title>"
" <author>"
" <first-name>Cover</first-name>"
" <last-name>Tester</last-name>"
" </author>"
" <coverpage>"
" <image l:href=\"#cover_img\"/>"
" </coverpage>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Only Chapter</p></title>"
" <p>Some content.</p>"
" </section>"
"</body>"
"<binary id=\"cover_img\" content-type=\"image/png\">AAAA</binary>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildLargeFb2()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Large Book</book-title>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Big Chapter</p></title>";
for (int i = 0; i < 60; ++i) {
xml += QString("<p>Paragraph %1: The quick brown fox jumped over the lazy dog repeatedly.</p>")
.arg(i + 1).toUtf8();
}
xml += " </section></body>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildFb2Windows1251()
{
// Minimal FB2 with windows-1251 encoding declaration and Cyrillic content.
// The bytes below are the windows-1251 encoding of "Тестовая книга" (Test book)
// and "Иван Иванов" (Ivan Ivanov), hand-encoded.
//
// We build the ASCII-safe portions as normal strings, embed raw Cyrillic bytes,
// and declare encoding="windows-1251" so the reader must transcode it.
QByteArray xml =
"<?xml version=\"1.0\" encoding=\"windows-1251\"?>"
"<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\""
" xmlns:l=\"http://www.w3.org/1999/xlink\">"
"<description><title-info>"
"<book-title>";
// "Книга" in windows-1251: К=CA, н=ED, и=E8, г=E3, а=E0
xml += QByteArray("\xCA\xED\xE8\xE3\xE0");
xml +=
"</book-title>"
"</title-info></description>"
"<body><section>"
"<title><p>Глава</p></title>"
"<p>";
// "Привет" in windows-1251: П=CF, р=F0, и=E8, в=E2, е=E5, т=F2
xml += QByteArray("\xCF\xF0\xE8\xE2\xE5\xF2");
xml += "</p></section></body></FictionBook>";
return writeTempFb2(xml);
}
static QString buildFb2WithImage()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Image Book</book-title>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Chapter With Image</p></title>"
" <p>Text before image.</p>"
" <image l:href=\"#inline_img\"/>"
" <p>Text after image.</p>"
" </section>"
"</body>"
"<binary id=\"inline_img\" content-type=\"image/png\">AAAA</binary>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
class Fb2ReaderTest : public QObject {
Q_OBJECT
private:
QString m_samplePath;
QString m_coverPath;
QString m_largePath;
QString m_imagePath;
QString m_cp1251Path;
private slots:
void initTestCase()
{
m_samplePath = buildSampleFb2();
m_coverPath = buildFb2WithCover();
m_largePath = buildLargeFb2();
m_imagePath = buildFb2WithImage();
m_cp1251Path = buildFb2Windows1251();
QVERIFY2(!m_samplePath.isEmpty(), "Failed to build sample FB2");
QVERIFY2(!m_coverPath.isEmpty(), "Failed to build cover FB2");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large FB2");
QVERIFY2(!m_imagePath.isEmpty(), "Failed to build image FB2");
QVERIFY2(!m_cp1251Path.isEmpty(), "Failed to build windows-1251 FB2");
}
void cleanupTestCase()
{
QFile::remove(m_samplePath);
QFile::remove(m_coverPath);
QFile::remove(m_largePath);
QFile::remove(m_imagePath);
QFile::remove(m_cp1251Path);
}
// --- Error handling ---
void openNonexistentFile()
{
Fb2Reader reader;
QVERIFY(!reader.open(QStringLiteral("/tmp/does_not_exist_xyzzy.fb2")));
QVERIFY(!reader.lastError().isEmpty());
}
void openInvalidXml()
{
const QString path = writeTempFb2("<not valid xml <<<");
Fb2Reader reader;
QVERIFY(!reader.open(path));
QVERIFY(!reader.lastError().isEmpty());
QFile::remove(path);
}
// --- Metadata ---
void titleExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.title(), QStringLiteral("Test Book"));
}
void authorExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
}
void fileTypeIsFb2()
{
Fb2Reader reader;
QCOMPARE(reader.fileType(), QStringLiteral("fb2"));
}
// --- Chapters ---
void chapterCountMatchesBodySections()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.chapterCount(), 2);
}
void loadChapterContent()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.index, 0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains(QStringLiteral("First paragraph")));
}
void loadChapterTwo()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(1);
QCOMPARE(ch.index, 1);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains(QStringLiteral("chapter two")));
}
void loadChapterOutOfRange()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(99);
QVERIFY(ch.contentChunks.isEmpty());
}
// --- TOC ---
void tableOfContentsBuilt()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const QList<TocEntry> toc = reader.tableOfContents();
QCOMPARE(toc.size(), 2);
QCOMPARE(toc[0].title, QStringLiteral("Chapter One"));
QCOMPARE(toc[0].chapterIndex, 0);
QCOMPARE(toc[0].depth, 0);
QCOMPARE(toc[1].title, QStringLiteral("Chapter Two"));
QCOMPARE(toc[1].chapterIndex, 1);
}
// --- Chunking ---
void largeChapterProducesMultipleChunks()
{
Fb2Reader reader;
QVERIFY(reader.open(m_largePath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
}
// --- Cover image ---
void coverImagePathExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_coverPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("cover_img"));
}
void coverImagePathEmptyWhenNoCover()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QVERIFY(reader.coverImagePath().isEmpty());
}
// --- Image tag rewriting ---
void imageTagsRewrittenToProviderUrls()
{
Fb2Reader reader;
QVERIFY(reader.open(m_imagePath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://fb2/inline_img")),
qPrintable(QStringLiteral("Expected image://fb2/inline_img in: ") + combined));
}
// --- loadMetadata ---
void loadMetadataOnlyParsesDescription()
{
Fb2Reader reader;
QVERIFY(reader.loadMetadata(m_samplePath));
QCOMPARE(reader.title(), QStringLiteral("Test Book"));
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
QCOMPARE(reader.chapterCount(), 0); // chapters not parsed
}
// --- Content hash ---
void contentHashNonEmptyAfterOpen()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QVERIFY(!reader.contentHash().isEmpty());
}
void contentHashEmptyAfterLoadMetadataOnly()
{
Fb2Reader reader;
QVERIFY(reader.loadMetadata(m_samplePath));
QVERIFY(reader.contentHash().isEmpty());
}
// --- Encoding ---
void windows1251FileOpensSuccessfully()
{
Fb2Reader reader;
QVERIFY2(reader.open(m_cp1251Path),
qPrintable(QStringLiteral("open() failed: ") + reader.lastError()));
}
void windows1251TitleDecodedCorrectly()
{
Fb2Reader reader;
QVERIFY(reader.open(m_cp1251Path));
// "Книга" in Unicode
QCOMPARE(reader.title(), QStringLiteral("\u041A\u043D\u0438\u0433\u0430"));
}
void windows1251ContentDecodedCorrectly()
{
Fb2Reader reader;
QVERIFY(reader.open(m_cp1251Path));
QCOMPARE(reader.chapterCount(), 1);
const Chapter ch = reader.loadChapter(0);
const QString combined = ch.contentChunks.join('\n');
// "Привет" in Unicode
QVERIFY(combined.contains(QStringLiteral("\u041F\u0440\u0438\u0432\u0435\u0442")));
}
};
QTEST_MAIN(Fb2ReaderTest)
#include "Fb2ReaderTest.moc"
+9 -15
View File
@@ -42,9 +42,7 @@ private slots:
FolderReader reader;
QVERIFY(reader.open(tmp.path()));
QCOMPARE(reader.chapterCount(), 1);
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.contentChunks.size(), 3);
QCOMPARE(reader.pages().size(), 3);
}
void sortOrderAlphabetical()
@@ -57,12 +55,11 @@ private slots:
FolderReader reader;
QVERIFY(reader.open(tmp.path()));
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.contentChunks.size(), 3);
// Chunks contain the entry name — check alphabetical order
QVERIFY(ch.contentChunks.at(0).contains(QStringLiteral("a.png")));
QVERIFY(ch.contentChunks.at(1).contains(QStringLiteral("b.png")));
QVERIFY(ch.contentChunks.at(2).contains(QStringLiteral("c.png")));
const auto pages = reader.pages();
QCOMPARE(pages.size(), 3);
QVERIFY(pages.at(0).imageUrl.contains(QStringLiteral("a.png")));
QVERIFY(pages.at(1).imageUrl.contains(QStringLiteral("b.png")));
QVERIFY(pages.at(2).imageUrl.contains(QStringLiteral("c.png")));
}
void coverIsFirstImage()
@@ -103,12 +100,10 @@ private slots:
QCOMPARE(ra.contentHash(), rb.contentHash());
}
void fileTypeAndRenderingMode()
void fileType()
{
FolderReader reader;
QCOMPARE(reader.fileType(), QStringLiteral("folder"));
QCOMPARE(static_cast<int>(reader.preferredRenderingMode()),
static_cast<int>(FormatReader::RenderingMode::Manga));
}
void nonImageFilesIgnored()
@@ -124,8 +119,7 @@ private slots:
FolderReader reader;
QVERIFY(reader.open(tmp.path()));
QCOMPARE(reader.chapterCount(), 1);
QCOMPARE(reader.loadChapter(0).contentChunks.size(), 1);
QCOMPARE(reader.pages().size(), 1);
}
void dotFilesIgnored()
@@ -137,7 +131,7 @@ private slots:
FolderReader reader;
QVERIFY(reader.open(tmp.path()));
QCOMPARE(reader.loadChapter(0).contentChunks.size(), 1);
QCOMPARE(reader.pages().size(), 1);
}
};
+2 -29
View File
@@ -37,21 +37,10 @@ private slots:
QCOMPARE(parsed.sourcePath, sourcePath);
}
void roundTripThroughImageUrl()
{
const QString sourcePath = QStringLiteral("/books/unicode путь.cbz");
const QString url = ImageUrl::build(QStringLiteral("cbz"),
QStringLiteral("dir/page01.jpg"), sourcePath);
const auto parsed = ImageUrl::parseImageUrl(QUrl(url));
QCOMPARE(parsed.entry, QStringLiteral("dir/page01.jpg"));
QCOMPARE(parsed.sourcePath, sourcePath);
}
void providerIdWithoutQueryHasEmptySourcePath()
{
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("OEBPS/img.png"));
QCOMPARE(parsed.entry, QStringLiteral("OEBPS/img.png"));
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("page01.png"));
QCOMPARE(parsed.entry, QStringLiteral("page01.png"));
QVERIFY(parsed.sourcePath.isEmpty());
}
@@ -61,22 +50,6 @@ private slots:
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("page%2001.jpg"));
QCOMPARE(parsed.entry, QStringLiteral("page 01.jpg"));
}
void entryWithSlashesSurvivesParsing()
{
const auto parsed = ImageUrl::parseImageUrl(
QUrl(QStringLiteral("image://dir/nested/deep/page.png")));
QCOMPARE(parsed.entry, QStringLiteral("nested/deep/page.png"));
QVERIFY(parsed.sourcePath.isEmpty());
}
void querySuffixMatchesBuild()
{
const QString sourcePath = QStringLiteral("/tmp/a b.fb2");
QCOMPARE(ImageUrl::build(QStringLiteral("fb2"), QStringLiteral("cover"), sourcePath),
ImageUrl::build(QStringLiteral("fb2"), QStringLiteral("cover"))
+ ImageUrl::querySuffix(sourcePath));
}
};
QTEST_MAIN(ImageUrlTest)
-168
View File
@@ -1,168 +0,0 @@
#include <QtTest>
#include "backend/formats/TxtReader.h"
class TxtReaderTest : public QObject {
Q_OBJECT
private:
QString fixturePath(const QString &filename) const
{
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
}
private slots:
void openValidFile()
{
TxtReader reader;
QVERIFY(reader.open(fixturePath("sample.txt")));
QCOMPARE(reader.chapterCount(), 1);
QCOMPARE(reader.title(), QStringLiteral("sample"));
}
void loadChapter()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.index, 0);
QVERIFY(!ch.contentChunks.isEmpty());
// Small file should produce a single chunk
QCOMPARE(ch.contentChunks.size(), 1);
QVERIFY(ch.contentChunks.first().contains("Books are a uniquely portable magic"));
// Verify Cyrillic content survived
QVERIFY(ch.contentChunks.first().contains(QString::fromUtf8("Искусство чтения")));
// Verify HTML paragraph structure
QVERIFY(ch.contentChunks.first().contains("<p>"));
}
void tableOfContentsEmpty()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
QVERIFY(reader.tableOfContents().isEmpty());
}
void authorEmpty()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
QVERIFY(reader.author().isEmpty());
}
void openNonexistentFile()
{
TxtReader reader;
QVERIFY(!reader.open("/tmp/does_not_exist_asteria.txt"));
QVERIFY(!reader.lastError().isEmpty());
}
void loadChapterOutOfRange()
{
TxtReader reader;
reader.open(fixturePath("sample.txt"));
Chapter ch = reader.loadChapter(1);
QVERIFY(ch.contentChunks.isEmpty());
}
void openCp1251File()
{
TxtReader reader;
QVERIFY(reader.open(fixturePath("sample_cp1251.txt")));
QCOMPARE(reader.chapterCount(), 1);
Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
QString allContent = ch.contentChunks.join(QString());
QVERIFY(allContent.contains(QString::fromUtf8("Привет, мир!")));
QVERIFY(allContent.contains(QString::fromUtf8("Русская литература")));
}
void largeFileProducesMultipleChunks()
{
// Generate a large file with paragraphs
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
QVERIFY(tmpFile.open());
// Write ~20KB of paragraphed text (well above 4096 chunk size)
QString paragraph = QStringLiteral("This is a paragraph of text that has some content. ");
paragraph = paragraph.repeated(10); // ~500 chars per paragraph
for (int i = 0; i < 50; ++i) {
tmpFile.write(paragraph.toUtf8());
tmpFile.write("\n\n");
}
tmpFile.close();
TxtReader reader;
QVERIFY(reader.open(tmpFile.fileName()));
Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
// Each chunk should have <p> tags
for (const QString &chunk : ch.contentChunks) {
QVERIFY(chunk.contains("<p>"));
}
}
void wallOfTextProducesMultipleChunks()
{
// Generate a large file with no blank lines
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
QVERIFY(tmpFile.open());
// Write ~20KB of text with single newlines only
QString line = QStringLiteral("This is a line of continuous text without blank lines between them. ");
line = line.repeated(5); // ~340 chars per line
for (int i = 0; i < 80; ++i) {
tmpFile.write(line.toUtf8());
tmpFile.write("\n");
}
tmpFile.close();
TxtReader reader;
QVERIFY(reader.open(tmpFile.fileName()));
Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
// Each chunk should be standalone HTML
for (const QString &chunk : ch.contentChunks) {
QVERIFY(chunk.contains("<p>"));
}
}
void eachChunkIsStandaloneHtml()
{
// Generate a large file
QTemporaryFile tmpFile;
tmpFile.setAutoRemove(true);
QVERIFY(tmpFile.open());
QString paragraph = QStringLiteral("Test paragraph content here. ");
paragraph = paragraph.repeated(15);
for (int i = 0; i < 40; ++i) {
tmpFile.write(paragraph.toUtf8());
tmpFile.write("\n\n");
}
tmpFile.close();
TxtReader reader;
QVERIFY(reader.open(tmpFile.fileName()));
Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
for (const QString &chunk : ch.contentChunks) {
// Every chunk must start with <p> and end with </p>
QString trimmed = chunk.trimmed();
QVERIFY2(trimmed.startsWith("<p>"),
qPrintable("Chunk doesn't start with <p>: " + trimmed.left(50)));
QVERIFY2(trimmed.endsWith("</p>"),
qPrintable("Chunk doesn't end with </p>: " + trimmed.right(50)));
}
}
};
QTEST_MAIN(TxtReaderTest)
#include "TxtReaderTest.moc"