Files
asteria-qt/tests/formats/Fb2ReaderTest.cpp
T
2026-04-17 16:59:36 +02:00

385 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <QtTest/QtTest>
#include <QDir>
#include <QFile>
#include <QTemporaryFile>
#include "backend/formats/Fb2Reader.h"
// ---------------------------------------------------------------------------
// Minimal FB2 fixture builder
// ---------------------------------------------------------------------------
static const char kFb2Header[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\""
" xmlns:l=\"http://www.w3.org/1999/xlink\">";
static const char kFb2Footer[] = "</FictionBook>";
static QString writeTempFb2(const QByteArray &content)
{
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/fb2_test_XXXXXX.fb2");
tmp.setAutoRemove(false);
if (!tmp.open()) return {};
tmp.write(content);
tmp.flush();
return tmp.fileName();
}
static QString buildSampleFb2()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Test Book</book-title>"
" <author>"
" <first-name>Test</first-name>"
" <last-name>Author</last-name>"
" </author>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Chapter One</p></title>"
" <p>First paragraph of chapter one.</p>"
" <p>Second paragraph of chapter one.</p>"
" </section>"
" <section>"
" <title><p>Chapter Two</p></title>"
" <p>This is chapter two content.</p>"
" </section>"
"</body>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildFb2WithCover()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Cover Book</book-title>"
" <author>"
" <first-name>Cover</first-name>"
" <last-name>Tester</last-name>"
" </author>"
" <coverpage>"
" <image l:href=\"#cover_img\"/>"
" </coverpage>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Only Chapter</p></title>"
" <p>Some content.</p>"
" </section>"
"</body>"
"<binary id=\"cover_img\" content-type=\"image/png\">AAAA</binary>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildLargeFb2()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Large Book</book-title>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Big Chapter</p></title>";
for (int i = 0; i < 60; ++i) {
xml += QString("<p>Paragraph %1: The quick brown fox jumped over the lazy dog repeatedly.</p>")
.arg(i + 1).toUtf8();
}
xml += " </section></body>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
static QString buildFb2Windows1251()
{
// Minimal FB2 with windows-1251 encoding declaration and Cyrillic content.
// The bytes below are the windows-1251 encoding of "Тестовая книга" (Test book)
// and "Иван Иванов" (Ivan Ivanov), hand-encoded.
//
// We build the ASCII-safe portions as normal strings, embed raw Cyrillic bytes,
// and declare encoding="windows-1251" so the reader must transcode it.
QByteArray xml =
"<?xml version=\"1.0\" encoding=\"windows-1251\"?>"
"<FictionBook xmlns=\"http://www.gribuser.ru/xml/fictionbook/2.0\""
" xmlns:l=\"http://www.w3.org/1999/xlink\">"
"<description><title-info>"
"<book-title>";
// "Книга" in windows-1251: К=CA, н=ED, и=E8, г=E3, а=E0
xml += QByteArray("\xCA\xED\xE8\xE3\xE0");
xml +=
"</book-title>"
"</title-info></description>"
"<body><section>"
"<title><p>Глава</p></title>"
"<p>";
// "Привет" in windows-1251: П=CF, р=F0, и=E8, в=E2, е=E5, т=F2
xml += QByteArray("\xCF\xF0\xE8\xE2\xE5\xF2");
xml += "</p></section></body></FictionBook>";
return writeTempFb2(xml);
}
static QString buildFb2WithImage()
{
QByteArray xml = kFb2Header;
xml +=
"<description>"
" <title-info>"
" <book-title>Image Book</book-title>"
" </title-info>"
"</description>"
"<body>"
" <section>"
" <title><p>Chapter With Image</p></title>"
" <p>Text before image.</p>"
" <image l:href=\"#inline_img\"/>"
" <p>Text after image.</p>"
" </section>"
"</body>"
"<binary id=\"inline_img\" content-type=\"image/png\">AAAA</binary>";
xml += kFb2Footer;
return writeTempFb2(xml);
}
// ---------------------------------------------------------------------------
// Test class
// ---------------------------------------------------------------------------
class Fb2ReaderTest : public QObject {
Q_OBJECT
private:
QString m_samplePath;
QString m_coverPath;
QString m_largePath;
QString m_imagePath;
QString m_cp1251Path;
private slots:
void initTestCase()
{
m_samplePath = buildSampleFb2();
m_coverPath = buildFb2WithCover();
m_largePath = buildLargeFb2();
m_imagePath = buildFb2WithImage();
m_cp1251Path = buildFb2Windows1251();
QVERIFY2(!m_samplePath.isEmpty(), "Failed to build sample FB2");
QVERIFY2(!m_coverPath.isEmpty(), "Failed to build cover FB2");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large FB2");
QVERIFY2(!m_imagePath.isEmpty(), "Failed to build image FB2");
QVERIFY2(!m_cp1251Path.isEmpty(), "Failed to build windows-1251 FB2");
}
void cleanupTestCase()
{
QFile::remove(m_samplePath);
QFile::remove(m_coverPath);
QFile::remove(m_largePath);
QFile::remove(m_imagePath);
QFile::remove(m_cp1251Path);
}
// --- Error handling ---
void openNonexistentFile()
{
Fb2Reader reader;
QVERIFY(!reader.open(QStringLiteral("/tmp/does_not_exist_xyzzy.fb2")));
QVERIFY(!reader.lastError().isEmpty());
}
void openInvalidXml()
{
const QString path = writeTempFb2("<not valid xml <<<");
Fb2Reader reader;
QVERIFY(!reader.open(path));
QVERIFY(!reader.lastError().isEmpty());
QFile::remove(path);
}
// --- Metadata ---
void titleExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.title(), QStringLiteral("Test Book"));
}
void authorExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
}
void fileTypeIsFb2()
{
Fb2Reader reader;
QCOMPARE(reader.fileType(), QStringLiteral("fb2"));
}
// --- Chapters ---
void chapterCountMatchesBodySections()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QCOMPARE(reader.chapterCount(), 2);
}
void loadChapterContent()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(0);
QCOMPARE(ch.index, 0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains(QStringLiteral("First paragraph")));
}
void loadChapterTwo()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(1);
QCOMPARE(ch.index, 1);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY(combined.contains(QStringLiteral("chapter two")));
}
void loadChapterOutOfRange()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const Chapter ch = reader.loadChapter(99);
QVERIFY(ch.contentChunks.isEmpty());
}
// --- TOC ---
void tableOfContentsBuilt()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
const QList<TocEntry> toc = reader.tableOfContents();
QCOMPARE(toc.size(), 2);
QCOMPARE(toc[0].title, QStringLiteral("Chapter One"));
QCOMPARE(toc[0].chapterIndex, 0);
QCOMPARE(toc[0].depth, 0);
QCOMPARE(toc[1].title, QStringLiteral("Chapter Two"));
QCOMPARE(toc[1].chapterIndex, 1);
}
// --- Chunking ---
void largeChapterProducesMultipleChunks()
{
Fb2Reader reader;
QVERIFY(reader.open(m_largePath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(ch.contentChunks.size() > 1);
}
// --- Cover image ---
void coverImagePathExtracted()
{
Fb2Reader reader;
QVERIFY(reader.open(m_coverPath));
QCOMPARE(reader.coverImagePath(), QStringLiteral("cover_img"));
}
void coverImagePathEmptyWhenNoCover()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QVERIFY(reader.coverImagePath().isEmpty());
}
// --- Image tag rewriting ---
void imageTagsRewrittenToProviderUrls()
{
Fb2Reader reader;
QVERIFY(reader.open(m_imagePath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://fb2/inline_img")),
qPrintable(QStringLiteral("Expected image://fb2/inline_img in: ") + combined));
}
// --- loadMetadata ---
void loadMetadataOnlyParsesDescription()
{
Fb2Reader reader;
QVERIFY(reader.loadMetadata(m_samplePath));
QCOMPARE(reader.title(), QStringLiteral("Test Book"));
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
QCOMPARE(reader.chapterCount(), 0); // chapters not parsed
}
// --- Content hash ---
void contentHashNonEmptyAfterOpen()
{
Fb2Reader reader;
QVERIFY(reader.open(m_samplePath));
QVERIFY(!reader.contentHash().isEmpty());
}
void contentHashEmptyAfterLoadMetadataOnly()
{
Fb2Reader reader;
QVERIFY(reader.loadMetadata(m_samplePath));
QVERIFY(reader.contentHash().isEmpty());
}
// --- Encoding ---
void windows1251FileOpensSuccessfully()
{
Fb2Reader reader;
QVERIFY2(reader.open(m_cp1251Path),
qPrintable(QStringLiteral("open() failed: ") + reader.lastError()));
}
void windows1251TitleDecodedCorrectly()
{
Fb2Reader reader;
QVERIFY(reader.open(m_cp1251Path));
// "Книга" in Unicode
QCOMPARE(reader.title(), QStringLiteral("\u041A\u043D\u0438\u0433\u0430"));
}
void windows1251ContentDecodedCorrectly()
{
Fb2Reader reader;
QVERIFY(reader.open(m_cp1251Path));
QCOMPARE(reader.chapterCount(), 1);
const Chapter ch = reader.loadChapter(0);
const QString combined = ch.contentChunks.join('\n');
// "Привет" in Unicode
QVERIFY(combined.contains(QStringLiteral("\u041F\u0440\u0438\u0432\u0435\u0442")));
}
};
QTEST_MAIN(Fb2ReaderTest)
#include "Fb2ReaderTest.moc"