#include #include "backend/formats/EpubReader.h" #include "backend/formats/IByteSource.h" #include "backend/formats/ImageUrl.h" 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 { return m_entries.value(name); } QByteArray contentHash() const override { return QByteArray("testhash123"); } bool m_openResult = true; QString m_path; QHash m_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()); QString fullHtml = reader.chunks().join(QString()); // Verify CSS styling inlined QVERIFY(fullHtml.contains(QStringLiteral("color:red")) || fullHtml.contains(QStringLiteral("italic"))); // Verify embedded image resolved to image://epub/ with source path QVERIFY(fullHtml.contains(QStringLiteral("image://epub/OEBPS/images/fig1.png"))); QVERIFY(fullHtml.contains(QStringLiteral("src=%2Fvirtual%2Fbook.epub")) || fullHtml.contains(QStringLiteral("src=/virtual/book.epub"))); // Verify web image resolved to image://web/ QVERIFY(fullHtml.contains(QStringLiteral("image://web/https://example.com/pic.jpg"))); } 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()); QVERIFY(reader.chunks().first().contains(QStringLiteral("Hello EPUB 3"))); } 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 QString &c : reader.chunks()) { QVERIFY(c.contains(QStringLiteral("text-align:justify")) || c.contains(QStringLiteral("text-align: 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 QString &chunk : reader.chunks()) { if (chunk.contains(QStringLiteral("Одноглазый"))) { // Must have both opening and closing quotes together in the same chunk QVERIFY(chunk.contains(QStringLiteral("«"))); QVERIFY(chunk.contains(QStringLiteral("»"))); foundOdnoglazyiTogether = true; } if (chunk.contains(QStringLiteral("Когда я умру"))) { // Must have the attribution text in the same chunk QVERIFY(chunk.contains(QStringLiteral("эта мысль странным образом утешала"))); foundThoughtTogether = true; } } QVERIFY(foundOdnoglazyiTogether); QVERIFY(foundThoughtTogether); } }; QTEST_MAIN(EpubReaderTest) #include "EpubReaderTest.moc"