500 lines
17 KiB
C++
500 lines
17 KiB
C++
#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{};
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Test class
|
|
// ---------------------------------------------------------------------------
|
|
|
|
class EpubReaderTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private:
|
|
QString m_epubPath;
|
|
QString m_noNcxPath;
|
|
QString m_largePath;
|
|
QString m_imgPath;
|
|
QString m_relImgPath;
|
|
|
|
private slots:
|
|
void initTestCase()
|
|
{
|
|
m_epubPath = buildSampleEpub();
|
|
m_noNcxPath = buildEpubNoNcx();
|
|
m_largePath = buildEpubLargeChapter();
|
|
m_imgPath = buildEpubWithImage();
|
|
m_relImgPath = buildEpubWithRelativeImage();
|
|
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");
|
|
}
|
|
|
|
void cleanupTestCase()
|
|
{
|
|
QFile::remove(m_epubPath);
|
|
QFile::remove(m_noNcxPath);
|
|
QFile::remove(m_largePath);
|
|
QFile::remove(m_imgPath);
|
|
QFile::remove(m_relImgPath);
|
|
}
|
|
|
|
// --- 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');
|
|
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
|
|
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png 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');
|
|
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
|
|
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png in: ") + combined));
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(EpubReaderTest)
|
|
#include "EpubReaderTest.moc"
|