#include #include #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 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 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 m_entries; mutable QHash m_readCount; }; static QHash 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("

Sample chapter text

")) { QHash entries; entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); QString metaBlock; if (!title.isEmpty()) metaBlock += QStringLiteral("%1\n").arg(title); if (!author.isEmpty()) metaBlock += QStringLiteral("%1\n").arg(author); QString manifestItems = QStringLiteral( " \n"); if (!extraManifest.isEmpty()) { manifestItems += extraManifest; } QString spineBlock; if (!spineRef.isEmpty()) { spineBlock = QStringLiteral(" \n").arg(spineRef); } QString opf = QStringLiteral( "\n" " \n" " %1\n" " \n" " \n" "%2" " \n" " \n" "%3" " \n" "").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(); 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(); 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(); // 1. container.xml src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); // 2. content.opf src->m_entries.insert(QStringLiteral("OEBPS/content.opf"), "\n" " \n" " Test EPUB Title\n" " Test Author\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""); // 3. toc.ncx src->m_entries.insert(QStringLiteral("OEBPS/toc.ncx"), "\n" " \n" " \n" " Chapter 1: The Beginning\n" " \n" " \n" " \n" ""); // 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"), "\n" "\n" " \n" "\n" "\n" "

Heading

\n" "

A stylish quote with an embedded image:

\n" "

External image:

\n" "\n" ""); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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(); src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); src->m_entries.insert(QStringLiteral("EPUB/package.opf"), "\n" " \n" " EPUB 3 Sample\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""); src->m_entries.insert(QStringLiteral("EPUB/nav.xhtml"), "\n" "\n" " \n" "\n" ""); src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), "

Hello EPUB 3

"); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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(); src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); src->m_entries.insert(QStringLiteral("EPUB/package.opf"), "\n" " \n" " Unwrap Test\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""); // Generates several long paragraphs inside a single wrapper div QString xhtml = QStringLiteral( "\n" "
\n" ); for (int i = 0; i < 10; ++i) { xhtml += QStringLiteral("

This is paragraph %1 with sufficient text to exceed the target chunk size when combined together with other paragraphs in the chapter.

\n").arg(i); } xhtml += QStringLiteral("
"); src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8()); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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(); src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); src->m_entries.insert(QStringLiteral("EPUB/package.opf"), "\n" " \n" " Inline Div Test\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""); QString xhtml = QStringLiteral( "\n" "
\n" "
«Одноглазый».
\n" "
«Когда я умру, они съедят меня и оставят только кости», — эта мысль странным образом утешала.
\n" "
" ); src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8()); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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>(entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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(); 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(); src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" ""); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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(); src->m_entries.insert(QStringLiteral("META-INF/container.xml"), "\n" " \n" " \n" " \n" ""); auto sharedEntries = std::make_shared>(src->m_entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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>(entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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>(entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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")] = "\n" " \n" " E3 Cover\n" " \n" " \n" " \n" " \n" " \n" " \n" ""; auto sharedEntries = std::make_shared>(entries); EpubReader reader([sharedEntries]() { auto s = std::make_unique(); 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")] = "\n" " \n" " Fallback Cover\n" " \n" " \n" " \n" " \n" " \n" " \n" ""; auto sharedEntries = std::make_shared>(entries); EpubReader reader([sharedEntries]() { auto s = std::make_unique(); 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>(entries); EpubReader reader([sharedEntries]() { auto s = std::make_unique(); 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>(validEntries); bool allowOpen = true; auto factory = [sharedEntries, &allowOpen]() { auto s = std::make_unique(); 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( "\n" "

Paragraph with unclosed tag\n" "

Mismatched tag hierarchy

\n" ""); auto entries = makeValidEpubEntries(QStringLiteral("Malformed"), QStringLiteral("Author"), QString(), QStringLiteral("ch1"), malformedXhtml); auto sharedEntries = std::make_shared>(entries); auto factory = [sharedEntries]() { auto s = std::make_unique(); 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")] = "\n" " \n" " Fonts & Cache\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" ""; 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")] = "" "

Chapter 1

"; entries[QStringLiteral("OEBPS/text/ch2.xhtml")] = "" "

Chapter 2

"; auto sharedEntries = std::make_shared>(entries); auto src = std::make_unique(); src->m_entries = entries; auto rawSource = src.get(); auto factory = [sharedEntries, rawSource]() { auto s = std::make_unique(); 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("

Direct parse

"), 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(); }, defaultScheme); QCOMPARE(reader.mode(), ReadingMode::Text); QVERIFY(reader.pages().isEmpty()); // Text mode has empty pages } }; QTEST_MAIN(EpubReaderTest) #include "EpubReaderTest.moc"