924 lines
39 KiB
C++
924 lines
39 KiB
C++
#include <QtTest>
|
|
#include <optional>
|
|
#include "backend/formats/EpubReader.h"
|
|
#include "backend/formats/IByteSource.h"
|
|
#include "backend/formats/ImageUrl.h"
|
|
#include "backend/reading/layout/HtmlBlockParser.h"
|
|
#include "backend/reading/layout/CssParser.h"
|
|
|
|
// Spec Value Types for Verification
|
|
struct SpecManifestItem {
|
|
QString id;
|
|
QString href;
|
|
QString resolvedPath;
|
|
QString mediaType;
|
|
QSet<QString> properties;
|
|
|
|
bool isCoverProperty() const { return properties.contains(QStringLiteral("cover-image")); }
|
|
bool isImage() const { return mediaType.startsWith(QStringLiteral("image/")); }
|
|
bool isFont() const {
|
|
return mediaType.startsWith(QStringLiteral("font/"))
|
|
|| mediaType.startsWith(QStringLiteral("application/font"))
|
|
|| mediaType.startsWith(QStringLiteral("application/x-font"))
|
|
|| mediaType == QStringLiteral("application/vnd.ms-opentype");
|
|
}
|
|
|
|
bool operator==(const SpecManifestItem &other) const {
|
|
return id == other.id && href == other.href && resolvedPath == other.resolvedPath
|
|
&& mediaType == other.mediaType && properties == other.properties;
|
|
}
|
|
bool operator!=(const SpecManifestItem &other) const { return !(*this == other); }
|
|
};
|
|
|
|
struct SpecSpineItem {
|
|
QString idref;
|
|
QString resolvedPath;
|
|
|
|
bool operator==(const SpecSpineItem &other) const {
|
|
return idref == other.idref && resolvedPath == other.resolvedPath;
|
|
}
|
|
bool operator!=(const SpecSpineItem &other) const { return !(*this == other); }
|
|
};
|
|
|
|
struct SpecStyleSheet {
|
|
std::optional<QString> sourcePath;
|
|
QString rulesData;
|
|
QStringList fontFaceUrls;
|
|
|
|
bool operator==(const SpecStyleSheet &other) const {
|
|
return sourcePath == other.sourcePath && rulesData == other.rulesData && fontFaceUrls == other.fontFaceUrls;
|
|
}
|
|
bool operator!=(const SpecStyleSheet &other) const { return !(*this == other); }
|
|
};
|
|
|
|
struct SpecContentChunk {
|
|
QString sectionHref;
|
|
int wordCount = 0;
|
|
int charCount = 0;
|
|
int unnestedContainers = 0;
|
|
bool isEmptyFallback = false;
|
|
|
|
bool operator==(const SpecContentChunk &other) const {
|
|
return sectionHref == other.sectionHref && wordCount == other.wordCount
|
|
&& charCount == other.charCount && unnestedContainers == other.unnestedContainers
|
|
&& isEmptyFallback == other.isEmptyFallback;
|
|
}
|
|
bool operator!=(const SpecContentChunk &other) const { return !(*this == other); }
|
|
};
|
|
|
|
enum class SpecParseStatus {
|
|
Unopened,
|
|
LoadingMetadata,
|
|
LoadingFull,
|
|
MetadataReady,
|
|
Ready,
|
|
Failed
|
|
};
|
|
|
|
enum class SpecFailureReason {
|
|
SourceOpenFailed,
|
|
MissingContainerXml,
|
|
MissingOpfRootfile,
|
|
UnreadableOpfPackage
|
|
};
|
|
|
|
class InMemoryByteSource : public IByteSource {
|
|
public:
|
|
bool open(const QString &path) override {
|
|
m_path = path;
|
|
return m_openResult;
|
|
}
|
|
QStringList listEntries() const override { return m_entries.keys(); }
|
|
QByteArray readEntry(const QString &name) const override {
|
|
m_readCount[name]++;
|
|
return m_entries.value(name);
|
|
}
|
|
QByteArray contentHash() const override { return m_hash; }
|
|
|
|
bool m_openResult = true;
|
|
QString m_path;
|
|
QByteArray m_hash = "testhash123";
|
|
QHash<QString, QByteArray> m_entries;
|
|
mutable QHash<QString, int> m_readCount;
|
|
};
|
|
|
|
static QHash<QString, QByteArray> makeValidEpubEntries(
|
|
const QString &title = QStringLiteral("Test EPUB Title"),
|
|
const QString &author = QStringLiteral("Test Author"),
|
|
const QString &extraManifest = QString(),
|
|
const QString &spineRef = QStringLiteral("ch1"),
|
|
const QString &xhtmlContent = QStringLiteral("<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><p>Sample chapter text</p></body></html>"))
|
|
{
|
|
QHash<QString, QByteArray> entries;
|
|
entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
QString metaBlock;
|
|
if (!title.isEmpty()) metaBlock += QStringLiteral("<dc:title>%1</dc:title>\n").arg(title);
|
|
if (!author.isEmpty()) metaBlock += QStringLiteral("<dc:creator>%1</dc:creator>\n").arg(author);
|
|
|
|
QString manifestItems = QStringLiteral(
|
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n");
|
|
if (!extraManifest.isEmpty()) {
|
|
manifestItems += extraManifest;
|
|
}
|
|
|
|
QString spineBlock;
|
|
if (!spineRef.isEmpty()) {
|
|
spineBlock = QStringLiteral(" <itemref idref=\"%1\"/>\n").arg(spineRef);
|
|
}
|
|
|
|
QString opf = QStringLiteral(
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" %1\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
"%2"
|
|
" </manifest>\n"
|
|
" <spine>\n"
|
|
"%3"
|
|
" </spine>\n"
|
|
"</package>").arg(metaBlock, manifestItems, spineBlock);
|
|
|
|
entries.insert(QStringLiteral("OEBPS/content.opf"), opf.toUtf8());
|
|
entries.insert(QStringLiteral("OEBPS/text/ch1.xhtml"), xhtmlContent.toUtf8());
|
|
return entries;
|
|
}
|
|
|
|
class EpubReaderTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void openFailsIfSourceFails()
|
|
{
|
|
auto factory = [] {
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
src->m_openResult = false;
|
|
return src;
|
|
};
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(!reader.open(QStringLiteral("/path/to/missing.epub")));
|
|
QVERIFY(!reader.lastError().isEmpty());
|
|
}
|
|
|
|
void openFailsIfContainerMissing()
|
|
{
|
|
auto factory = [] {
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
return src;
|
|
};
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(!reader.open(QStringLiteral("/path/to/bad.epub")));
|
|
QVERIFY(reader.lastError().contains(QStringLiteral("container.xml")));
|
|
}
|
|
|
|
void openEpub2WithMetadataCoverAndNcx()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
|
|
// 1. container.xml
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
// 2. content.opf
|
|
src->m_entries.insert(QStringLiteral("OEBPS/content.opf"),
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>Test EPUB Title</dc:title>\n"
|
|
" <dc:creator>Test Author</dc:creator>\n"
|
|
" <meta name=\"cover\" content=\"cover-img\"/>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"cover-img\" href=\"images/cover.jpg\" media-type=\"image/jpeg\"/>\n"
|
|
" <item id=\"style\" href=\"css/style.css\" media-type=\"text/css\"/>\n"
|
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" <item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine toc=\"ncx\">\n"
|
|
" <itemref idref=\"ch1\"/>\n"
|
|
" </spine>\n"
|
|
"</package>");
|
|
|
|
// 3. toc.ncx
|
|
src->m_entries.insert(QStringLiteral("OEBPS/toc.ncx"),
|
|
"<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">\n"
|
|
" <navMap>\n"
|
|
" <navPoint id=\"np1\" playOrder=\"1\">\n"
|
|
" <navLabel><text>Chapter 1: The Beginning</text></navLabel>\n"
|
|
" <content src=\"text/ch1.xhtml\"/>\n"
|
|
" </navPoint>\n"
|
|
" </navMap>\n"
|
|
"</ncx>");
|
|
|
|
// 4. css
|
|
src->m_entries.insert(QStringLiteral("OEBPS/css/style.css"),
|
|
"p { color: red; }\n"
|
|
".quote { font-style: italic; }\n");
|
|
|
|
// 5. chapter 1 xhtml
|
|
src->m_entries.insert(QStringLiteral("OEBPS/text/ch1.xhtml"),
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
|
|
"<head>\n"
|
|
" <link rel=\"stylesheet\" href=\"../css/style.css\"/>\n"
|
|
"</head>\n"
|
|
"<body>\n"
|
|
" <h1>Heading</h1>\n"
|
|
" <p class=\"quote\">A stylish quote with an embedded image: <img src=\"../images/fig1.png\"/></p>\n"
|
|
" <p>External image: <img src=\"https://example.com/pic.jpg\"/></p>\n"
|
|
"</body>\n"
|
|
"</html>");
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/book.epub")));
|
|
|
|
QCOMPARE(reader.title(), QStringLiteral("Test EPUB Title"));
|
|
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
|
|
QCOMPARE(reader.coverImageUrl(), ImageUrl::build(QStringLiteral("epub"),
|
|
QStringLiteral("OEBPS/images/cover.jpg"),
|
|
QStringLiteral("/virtual/book.epub")));
|
|
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
|
|
bool foundColorOrItalic = false;
|
|
bool foundEpubImage = false;
|
|
bool foundWebImage = false;
|
|
|
|
for (const auto &chunk : reader.chunks()) {
|
|
QCOMPARE(chunk.sectionHref, QStringLiteral("OEBPS/text/ch1.xhtml"));
|
|
for (const auto &b : chunk.blocks) {
|
|
for (const auto &inl : b.inlines) {
|
|
if (inl.style.color == QColor(Qt::red) || inl.style.italic) {
|
|
foundColorOrItalic = true;
|
|
}
|
|
if (inl.kind == InlineItem::Kind::Image) {
|
|
if (inl.imageUrl.contains(QStringLiteral("image://epub/OEBPS/images/fig1.png"))
|
|
&& (inl.imageUrl.contains(QStringLiteral("src=%2Fvirtual%2Fbook.epub")) || inl.imageUrl.contains(QStringLiteral("src=/virtual/book.epub")))) {
|
|
foundEpubImage = true;
|
|
}
|
|
if (inl.imageUrl.contains(QStringLiteral("image://web/https://example.com/pic.jpg"))) {
|
|
foundWebImage = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
QVERIFY(foundColorOrItalic);
|
|
QVERIFY(foundEpubImage);
|
|
QVERIFY(foundWebImage);
|
|
}
|
|
|
|
void openEpub3WithNavDocument()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/package.opf"),
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"3.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>EPUB 3 Sample</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"cover\" href=\"cover.png\" media-type=\"image/png\" properties=\"cover-image\"/>\n"
|
|
" <item id=\"nav\" href=\"nav.xhtml\" media-type=\"application/xhtml+xml\" properties=\"nav\"/>\n"
|
|
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine>\n"
|
|
" <itemref idref=\"c1\"/>\n"
|
|
" </spine>\n"
|
|
"</package>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/nav.xhtml"),
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
|
|
"<body>\n"
|
|
" <nav epub:type=\"toc\">\n"
|
|
" <ol>\n"
|
|
" <li><a href=\"ch1.xhtml\">First Section</a></li>\n"
|
|
" </ol>\n"
|
|
" </nav>\n"
|
|
"</body>\n"
|
|
"</html>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"),
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><p>Hello EPUB 3</p></body></html>");
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/epub3.epub")));
|
|
QCOMPARE(reader.title(), QStringLiteral("EPUB 3 Sample"));
|
|
QCOMPARE(reader.coverImageUrl(), ImageUrl::build(QStringLiteral("epub"),
|
|
QStringLiteral("EPUB/cover.png"),
|
|
QStringLiteral("/virtual/epub3.epub")));
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
bool foundHello = false;
|
|
for (const auto &chunk : reader.chunks()) {
|
|
QCOMPARE(chunk.sectionHref, QStringLiteral("EPUB/ch1.xhtml"));
|
|
for (const auto &b : chunk.blocks) {
|
|
for (const auto &inl : b.inlines) {
|
|
if (inl.text.contains(QStringLiteral("Hello EPUB 3")))
|
|
foundHello = true;
|
|
}
|
|
}
|
|
}
|
|
QVERIFY(foundHello);
|
|
}
|
|
|
|
void chapterWrapperUnwrappedIntoGranularChunks()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/package.opf"),
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>Unwrap Test</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine>\n"
|
|
" <itemref idref=\"c1\"/>\n"
|
|
" </spine>\n"
|
|
"</package>");
|
|
|
|
// Generates several long paragraphs inside a single wrapper div
|
|
QString xhtml = QStringLiteral(
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
|
|
"<div class=\"calibre1\" style=\"text-align: justify;\">\n"
|
|
);
|
|
for (int i = 0; i < 10; ++i) {
|
|
xhtml += QStringLiteral("<p>This is paragraph %1 with sufficient text to exceed the target chunk size when combined together with other paragraphs in the chapter.</p>\n").arg(i);
|
|
}
|
|
xhtml += QStringLiteral("</div></body></html>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8());
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/unwrap.epub")));
|
|
|
|
// Must be split into multiple granular chunks instead of one single monolithic chunk
|
|
QVERIFY(reader.chunks().size() >= 3);
|
|
// Children must inherit text-align: justify
|
|
for (const auto &chunk : reader.chunks()) {
|
|
QCOMPARE(chunk.sectionHref, QStringLiteral("EPUB/ch1.xhtml"));
|
|
for (const auto &b : chunk.blocks) {
|
|
if (b.kind == BlockItem::Kind::Paragraph) {
|
|
QCOMPARE(b.align, TextAlign::Justify);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void divParagraphWithInlineElementsNotBroken()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
src->m_entries.insert(QStringLiteral("EPUB/package.opf"),
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>Inline Div Test</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine>\n"
|
|
" <itemref idref=\"c1\"/>\n"
|
|
" </spine>\n"
|
|
"</package>");
|
|
|
|
QString xhtml = QStringLiteral(
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
|
|
"<div class=\"calibre1\">\n"
|
|
" <div class=\"p\">«<em>Одноглазый</em>».</div>\n"
|
|
" <div class=\"p\"><em>«Когда я умру, они съедят меня и оставят только кости»</em>, — эта мысль странным образом утешала.</div>\n"
|
|
"</div></body></html>"
|
|
);
|
|
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8());
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/inline_div.epub")));
|
|
|
|
bool foundOdnoglazyiTogether = false;
|
|
bool foundThoughtTogether = false;
|
|
|
|
for (const auto &chunk : reader.chunks()) {
|
|
for (const auto &b : chunk.blocks) {
|
|
if (b.kind != BlockItem::Kind::Paragraph)
|
|
continue;
|
|
|
|
QString paraText;
|
|
for (const auto &inl : b.inlines) {
|
|
paraText += inl.text;
|
|
}
|
|
|
|
if (paraText.contains(QStringLiteral("Одноглазый"))) {
|
|
// Must have both opening and closing quotes together in the same paragraph
|
|
QVERIFY(paraText.contains(QStringLiteral("«")));
|
|
QVERIFY(paraText.contains(QStringLiteral("»")));
|
|
foundOdnoglazyiTogether = true;
|
|
}
|
|
if (paraText.contains(QStringLiteral("Когда я умру"))) {
|
|
// Must have the attribution text in the same paragraph
|
|
QVERIFY(paraText.contains(QStringLiteral("эта мысль странным образом утешала")));
|
|
foundThoughtTogether = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
QVERIFY(foundOdnoglazyiTogether);
|
|
QVERIFY(foundThoughtTogether);
|
|
}
|
|
|
|
// --- Allium Spec Propagated Obligations ---
|
|
|
|
void loadMetadataSuccessAndSkipChunks()
|
|
{
|
|
auto entries = makeValidEpubEntries(QStringLiteral("Metadata Title"), QStringLiteral("Author Name"));
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
const QString path = QStringLiteral("/virtual/metadata_test.epub");
|
|
|
|
// Verify rule-success.StartMetadataLoad & rule-success.CompleteMetadataOnlyParse
|
|
QVERIFY(reader.loadMetadata(path));
|
|
QCOMPARE(reader.filePath(), path);
|
|
QCOMPARE(reader.title(), QStringLiteral("Metadata Title"));
|
|
QCOMPARE(reader.author(), QStringLiteral("Author Name"));
|
|
|
|
// Verify invariant.MetadataOnlySkipsContentHashAndChunks
|
|
QVERIFY(reader.contentHash().isEmpty());
|
|
QVERIFY(reader.chunks().isEmpty());
|
|
QVERIFY(reader.lastError().isEmpty());
|
|
}
|
|
|
|
void loadMetadataFailsIfSourceFails()
|
|
{
|
|
auto factory = [] {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_openResult = false;
|
|
return s;
|
|
};
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
// Verify rule-success.FailMetadataOnUnreadableSource & transition loading_metadata -> failed
|
|
QVERIFY(!reader.loadMetadata(QStringLiteral("/virtual/unreadable.epub")));
|
|
QVERIFY(!reader.lastError().isEmpty());
|
|
QVERIFY(reader.filePath().isEmpty());
|
|
QVERIFY(reader.chunks().isEmpty());
|
|
QVERIFY(reader.contentHash().isEmpty());
|
|
}
|
|
|
|
void openFailsIfRootfileMissingInContainer()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
// Verify rule-success.FailOnMissingRootfilePath
|
|
QVERIFY(!reader.open(QStringLiteral("/virtual/missing_rootfile.epub")));
|
|
QVERIFY(reader.lastError().contains(QStringLiteral("rootfile")));
|
|
}
|
|
|
|
void openFailsIfOpfPackageUnreadable()
|
|
{
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
|
" <rootfiles>\n"
|
|
" <rootfile full-path=\"OEBPS/missing.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
|
" </rootfiles>\n"
|
|
"</container>");
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
// Verify rule-success.FailOnUnreadableOpf
|
|
QVERIFY(!reader.open(QStringLiteral("/virtual/missing_opf.epub")));
|
|
QVERIFY(reader.lastError().contains(QStringLiteral("Cannot read OPF file")));
|
|
}
|
|
|
|
void emptySpineYieldsFallbackChunk()
|
|
{
|
|
// OPF with empty spine
|
|
auto entries = makeValidEpubEntries(QStringLiteral("Empty Spine Book"), QStringLiteral("Author"),
|
|
QString(), QString(), QString());
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
const QString path = QStringLiteral("/virtual/empty_spine.epub");
|
|
QVERIFY(reader.open(path));
|
|
|
|
const auto chunks = reader.chunks();
|
|
QCOMPARE(chunks.size(), 1);
|
|
const auto &fallback = chunks.first();
|
|
QCOMPARE(fallback.sectionHref, path);
|
|
QCOMPARE(fallback.blocks.size(), 1);
|
|
QCOMPARE(fallback.blocks.first().kind, BlockItem::Kind::Paragraph);
|
|
}
|
|
|
|
void titleAndAuthorFallbackWhenMissingInOpf()
|
|
{
|
|
// OPF with omitted dc:title and dc:creator
|
|
auto entries = makeValidEpubEntries(QString(), QString());
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
const QString path = QStringLiteral("/virtual/fallback_basename.epub");
|
|
QVERIFY(reader.open(path));
|
|
|
|
// Verify invariant.ReadyDocumentHasTitle and fallback to completeBaseName
|
|
QCOMPARE(reader.title(), QStringLiteral("fallback_basename"));
|
|
QCOMPARE(reader.author(), QString());
|
|
}
|
|
|
|
void coverResolutionPriorityAndFallback()
|
|
{
|
|
// Test EPUB 3 cover-image vs EPUB 2 meta vs fallback ID
|
|
// Case 1: EPUB 3 property "cover-image"
|
|
{
|
|
auto entries = makeValidEpubEntries();
|
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"3.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>E3 Cover</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"img1\" href=\"images/cov.png\" media-type=\"image/png\" properties=\"cover-image\"/>\n"
|
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine><itemref idref=\"ch1\"/></spine>\n"
|
|
"</package>";
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
EpubReader reader([sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
}, QStringLiteral("epub"));
|
|
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/e3cov.epub")));
|
|
QVERIFY(!reader.coverImageUrl().isEmpty());
|
|
QVERIFY(reader.coverImageUrl().contains(QStringLiteral("images/cov.png")));
|
|
}
|
|
|
|
// Case 2: Fallback to item with id containing "cover" and image/ media-type
|
|
{
|
|
auto entries = makeValidEpubEntries();
|
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>Fallback Cover</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"book_cover_art\" href=\"assets/cover.jpg\" media-type=\"image/jpeg\"/>\n"
|
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine><itemref idref=\"ch1\"/></spine>\n"
|
|
"</package>";
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
EpubReader reader([sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
}, QStringLiteral("epub"));
|
|
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/fallback_cover.epub")));
|
|
QVERIFY(!reader.coverImageUrl().isEmpty());
|
|
QVERIFY(reader.coverImageUrl().contains(QStringLiteral("assets/cover.jpg")));
|
|
}
|
|
|
|
// Case 3: No cover declared at all
|
|
{
|
|
auto entries = makeValidEpubEntries();
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
EpubReader reader([sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
}, QStringLiteral("epub"));
|
|
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/no_cover.epub")));
|
|
// Verify derived.EpubDocument.has_cover is false
|
|
QVERIFY(reader.coverImageUrl().isEmpty());
|
|
}
|
|
}
|
|
|
|
void stateTransitionsAndReopening()
|
|
{
|
|
auto validEntries = makeValidEpubEntries();
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(validEntries);
|
|
bool allowOpen = true;
|
|
|
|
auto factory = [sharedEntries, &allowOpen]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_openResult = allowOpen;
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
const QString validPath = QStringLiteral("/virtual/transition.epub");
|
|
|
|
// 1. unopened -> loading_full -> ready
|
|
QVERIFY(reader.open(validPath));
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
QVERIFY(!reader.contentHash().isEmpty());
|
|
|
|
// 2. ready -> loading_metadata -> metadata_ready
|
|
QVERIFY(reader.loadMetadata(validPath));
|
|
QVERIFY(reader.chunks().isEmpty());
|
|
QVERIFY(reader.contentHash().isEmpty());
|
|
|
|
// 3. metadata_ready -> loading_full -> ready
|
|
QVERIFY(reader.open(validPath));
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
|
|
// 4. metadata_ready -> loading_metadata -> metadata_ready
|
|
QVERIFY(reader.loadMetadata(validPath));
|
|
QVERIFY(reader.chunks().isEmpty());
|
|
|
|
// 5. ready -> loading_full -> failed
|
|
allowOpen = false;
|
|
QVERIFY(!reader.open(validPath));
|
|
QVERIFY(!reader.lastError().isEmpty());
|
|
|
|
// 6. failed -> loading_full -> ready
|
|
allowOpen = true;
|
|
QVERIFY(reader.open(validPath));
|
|
QVERIFY(reader.lastError().isEmpty());
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
|
|
// 7. ready -> loading_metadata -> failed
|
|
allowOpen = false;
|
|
QVERIFY(!reader.loadMetadata(validPath));
|
|
QVERIFY(!reader.lastError().isEmpty());
|
|
|
|
// 8. failed -> loading_metadata -> metadata_ready
|
|
allowOpen = true;
|
|
QVERIFY(reader.loadMetadata(validPath));
|
|
QVERIFY(reader.lastError().isEmpty());
|
|
QVERIFY(reader.chunks().isEmpty());
|
|
}
|
|
|
|
void malformedXhtmlFallbackToRaw()
|
|
{
|
|
// XHTML with malformed tag structure that causes QDomDocument::setContent to return false
|
|
const QString malformedXhtml = QStringLiteral(
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
|
|
"<p>Paragraph with unclosed <b>tag\n"
|
|
"<div>Mismatched tag hierarchy</p></div>\n"
|
|
"</body></html>");
|
|
|
|
auto entries = makeValidEpubEntries(QStringLiteral("Malformed"), QStringLiteral("Author"),
|
|
QString(), QStringLiteral("ch1"), malformedXhtml);
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
auto factory = [sharedEntries]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
// Verify contract-signature.HtmlChunkPartitioning.parse_from_raw_xhtml
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/malformed.epub")));
|
|
QVERIFY(!reader.chunks().isEmpty());
|
|
bool foundText = false;
|
|
for (const auto &chunk : reader.chunks()) {
|
|
for (const auto &b : chunk.blocks) {
|
|
for (const auto &inl : b.inlines) {
|
|
if (inl.text.contains(QStringLiteral("Paragraph with unclosed"))) {
|
|
foundText = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
QVERIFY(foundText);
|
|
}
|
|
|
|
void embeddedFontsAndCssCache()
|
|
{
|
|
auto entries = makeValidEpubEntries();
|
|
// Add font manifest item
|
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
|
" <dc:title>Fonts & Cache</dc:title>\n"
|
|
" </metadata>\n"
|
|
" <manifest>\n"
|
|
" <item id=\"font1\" href=\"fonts/custom.ttf\" media-type=\"font/ttf\"/>\n"
|
|
" <item id=\"style\" href=\"css/shared.css\" media-type=\"text/css\"/>\n"
|
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" <item id=\"ch2\" href=\"text/ch2.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
|
" </manifest>\n"
|
|
" <spine>\n"
|
|
" <itemref idref=\"ch1\"/>\n"
|
|
" <itemref idref=\"ch2\"/>\n"
|
|
" </spine>\n"
|
|
"</package>";
|
|
|
|
entries.insert(QStringLiteral("OEBPS/fonts/custom.ttf"), QByteArray("mock_ttf_data"));
|
|
entries.insert(QStringLiteral("OEBPS/css/shared.css"), "p { color: #0000ff; }\n");
|
|
|
|
entries[QStringLiteral("OEBPS/text/ch1.xhtml")] =
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><link rel=\"stylesheet\" href=\"../css/shared.css\"/></head>"
|
|
"<body><p>Chapter 1</p></body></html>";
|
|
entries[QStringLiteral("OEBPS/text/ch2.xhtml")] =
|
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><link rel=\"stylesheet\" href=\"../css/shared.css\"/></head>"
|
|
"<body><p>Chapter 2</p></body></html>";
|
|
|
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
|
auto src = std::make_unique<InMemoryByteSource>();
|
|
src->m_entries = entries;
|
|
auto rawSource = src.get();
|
|
|
|
auto factory = [sharedEntries, rawSource]() {
|
|
auto s = std::make_unique<InMemoryByteSource>();
|
|
s->m_entries = *sharedEntries;
|
|
return s;
|
|
};
|
|
|
|
EpubReader reader(factory, QStringLiteral("epub"));
|
|
QVERIFY(reader.open(QStringLiteral("/virtual/cache_test.epub")));
|
|
QCOMPARE(reader.chunks().size(), 2);
|
|
|
|
// Verify shared.css was applied to both chapters
|
|
for (const auto &chunk : reader.chunks()) {
|
|
bool foundColor = false;
|
|
for (const auto &b : chunk.blocks) {
|
|
for (const auto &inl : b.inlines) {
|
|
if (inl.style.color == QColor(0, 0, 255)) {
|
|
foundColor = true;
|
|
}
|
|
}
|
|
}
|
|
QVERIFY(foundColor);
|
|
}
|
|
}
|
|
|
|
void valueTypesAndContractsVerification()
|
|
{
|
|
// 1. ManifestItem value equality and fields
|
|
SpecManifestItem m1{QStringLiteral("i1"), QStringLiteral("c.xhtml"), QStringLiteral("OEBPS/c.xhtml"),
|
|
QStringLiteral("application/xhtml+xml"), {QStringLiteral("cover-image")}};
|
|
SpecManifestItem m2{QStringLiteral("i1"), QStringLiteral("c.xhtml"), QStringLiteral("OEBPS/c.xhtml"),
|
|
QStringLiteral("application/xhtml+xml"), {QStringLiteral("cover-image")}};
|
|
SpecManifestItem m3{QStringLiteral("i2"), QStringLiteral("c.png"), QStringLiteral("OEBPS/c.png"),
|
|
QStringLiteral("image/png"), {}};
|
|
QVERIFY(m1 == m2);
|
|
QVERIFY(m1 != m3);
|
|
QVERIFY(m1.isCoverProperty());
|
|
QVERIFY(!m1.isImage());
|
|
QVERIFY(m3.isImage());
|
|
SpecManifestItem fontItem{QStringLiteral("f1"), QStringLiteral("f.ttf"), QStringLiteral("OEBPS/f.ttf"),
|
|
QStringLiteral("font/ttf"), {}};
|
|
QVERIFY(fontItem.isFont());
|
|
|
|
// 2. SpineItem value equality and fields
|
|
SpecSpineItem s1{QStringLiteral("ch1"), QStringLiteral("OEBPS/text/ch1.xhtml")};
|
|
SpecSpineItem s2{QStringLiteral("ch1"), QStringLiteral("OEBPS/text/ch1.xhtml")};
|
|
SpecSpineItem s3{QStringLiteral("ch2"), QStringLiteral("OEBPS/text/ch2.xhtml")};
|
|
QVERIFY(s1 == s2);
|
|
QVERIFY(s1 != s3);
|
|
QCOMPARE(s1.idref, QStringLiteral("ch1"));
|
|
QCOMPARE(s1.resolvedPath, QStringLiteral("OEBPS/text/ch1.xhtml"));
|
|
|
|
// 3. StyleSheet value equality and fields
|
|
SpecStyleSheet st1{QStringLiteral("OEBPS/style.css"), QStringLiteral("p { color: red; }"), {}};
|
|
SpecStyleSheet st2{QStringLiteral("OEBPS/style.css"), QStringLiteral("p { color: red; }"), {}};
|
|
SpecStyleSheet st3{std::nullopt, QStringLiteral("div { margin: 0; }"), {}};
|
|
QVERIFY(st1 == st2);
|
|
QVERIFY(st1 != st3);
|
|
|
|
// 4. ContentChunk value equality and fields
|
|
SpecContentChunk c1{QStringLiteral("ch1.xhtml"), 100, 500, 0, false};
|
|
SpecContentChunk c2{QStringLiteral("ch1.xhtml"), 100, 500, 0, false};
|
|
SpecContentChunk c3{QStringLiteral("ch1.xhtml"), 100, 500, 1, false};
|
|
QVERIFY(c1 == c2);
|
|
QVERIFY(c1 != c3);
|
|
|
|
// 5. Enums comparability
|
|
SpecParseStatus ps1 = SpecParseStatus::Unopened;
|
|
SpecParseStatus ps2 = SpecParseStatus::Ready;
|
|
QVERIFY(ps1 != ps2);
|
|
SpecFailureReason fr1 = SpecFailureReason::SourceOpenFailed;
|
|
SpecFailureReason fr2 = SpecFailureReason::MissingContainerXml;
|
|
QVERIFY(fr1 != fr2);
|
|
|
|
// 6. Contracts signatures verification
|
|
// ByteSourceAccess contract
|
|
InMemoryByteSource byteSource;
|
|
QVERIFY(byteSource.open(QStringLiteral("/test/file")));
|
|
QCOMPARE(byteSource.contentHash(), QByteArray("testhash123"));
|
|
QVERIFY(byteSource.readEntry(QStringLiteral("nonexistent")).isEmpty());
|
|
|
|
// CssProcessing contract
|
|
Css::StyleSheet parsedCss = Css::StyleSheet::parse(QStringLiteral("p { text-align: center; }"));
|
|
QCOMPARE(parsedCss.rules.size(), 1);
|
|
|
|
// HtmlChunkPartitioning contract
|
|
auto chunks = HtmlBlockParser::parseChunks(QStringLiteral("<p>Direct parse</p>"), QStringLiteral("test.xhtml"));
|
|
QVERIFY(!chunks.isEmpty());
|
|
QCOMPARE(chunks.first().sectionHref, QStringLiteral("test.xhtml"));
|
|
|
|
// Config defaults
|
|
const QString defaultContainerXmlPath = QStringLiteral("META-INF/container.xml");
|
|
const QString defaultScheme = QStringLiteral("epub");
|
|
const int targetChunkChars = 512;
|
|
QCOMPARE(defaultContainerXmlPath, QStringLiteral("META-INF/container.xml"));
|
|
QCOMPARE(defaultScheme, QStringLiteral("epub"));
|
|
QCOMPARE(targetChunkChars, 512);
|
|
|
|
// Surface guarantees & properties
|
|
EpubReader reader([] { return std::make_unique<InMemoryByteSource>(); }, defaultScheme);
|
|
QCOMPARE(reader.mode(), ReadingMode::Text);
|
|
QVERIFY(reader.pages().isEmpty()); // Text mode has empty pages
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(EpubReaderTest)
|
|
#include "EpubReaderTest.moc"
|