Files
asteria-qt/tests/formats/EpubReaderTest.cpp
T

358 lines
15 KiB
C++

#include <QtTest>
#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<QString, QByteArray> m_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);
}
};
QTEST_MAIN(EpubReaderTest)
#include "EpubReaderTest.moc"