epub parsing spec
This commit is contained in:
2 files changed
+1051
-2
No files matched your search
@@ -1,7 +1,86 @@
|
||||
#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:
|
||||
@@ -10,14 +89,67 @@ public:
|
||||
return m_openResult;
|
||||
}
|
||||
QStringList listEntries() const override { return m_entries.keys(); }
|
||||
QByteArray readEntry(const QString &name) const override { return m_entries.value(name); }
|
||||
QByteArray contentHash() const override { return QByteArray("testhash123"); }
|
||||
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
|
||||
|
||||
@@ -351,6 +483,440 @@ private slots:
|
||||
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)
|
||||
|
||||
Reference in new issue
Block a user