#include #include #include #include #include "backend/formats/Fb2Reader.h" // --------------------------------------------------------------------------- // Minimal FB2 fixture builder // --------------------------------------------------------------------------- static const char kFb2Header[] = "" ""; static const char kFb2Footer[] = ""; 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 += "" " " " Test Book" " " " Test" " Author" " " " " "" "" "
" " <p>Chapter One</p>" "

First paragraph of chapter one.

" "

Second paragraph of chapter one.

" "
" "
" " <p>Chapter Two</p>" "

This is chapter two content.

" "
" ""; xml += kFb2Footer; return writeTempFb2(xml); } static QString buildFb2WithCover() { QByteArray xml = kFb2Header; xml += "" " " " Cover Book" " " " Cover" " Tester" " " " " " " " " " " "" "" "
" " <p>Only Chapter</p>" "

Some content.

" "
" "" "AAAA"; xml += kFb2Footer; return writeTempFb2(xml); } static QString buildLargeFb2() { QByteArray xml = kFb2Header; xml += "" " " " Large Book" " " "" "" "
" " <p>Big Chapter</p>"; for (int i = 0; i < 60; ++i) { xml += QString("

Paragraph %1: The quick brown fox jumped over the lazy dog repeatedly.

") .arg(i + 1).toUtf8(); } xml += "
"; 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 = "" "" "" ""; // "Книга" in windows-1251: К=CA, н=ED, и=E8, г=E3, а=E0 xml += QByteArray("\xCA\xED\xE8\xE3\xE0"); xml += "" "" "
" "<p>Глава</p>" "

"; // "Привет" in windows-1251: П=CF, р=F0, и=E8, в=E2, е=E5, т=F2 xml += QByteArray("\xCF\xF0\xE8\xE2\xE5\xF2"); xml += "

"; return writeTempFb2(xml); } static QString buildFb2WithImage() { QByteArray xml = kFb2Header; xml += "" " " " Image Book" " " "" "" "
" " <p>Chapter With Image</p>" "

Text before image.

" " " "

Text after image.

" "
" "" "AAAA"; 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(" 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"