old render cleanup part 2
This commit is contained in:
40 files changed
+329
-3643
No files matched your search
@@ -10,18 +10,15 @@
|
||||
|
||||
#include "backend/HistoryBookModel.h"
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/comic/CbzReader.h"
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/formats/sources/ZipByteSource.h"
|
||||
|
||||
static FormatRegistry makeRegistry()
|
||||
{
|
||||
return FormatRegistry({
|
||||
{QStringLiteral("txt"), false, {},
|
||||
[] { return std::make_unique<TxtReader>(); }, nullptr},
|
||||
{QStringLiteral("epub"), false, QStringLiteral("epub"),
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
{QStringLiteral("cbz"), false, QStringLiteral("cbz"),
|
||||
[] { return std::make_unique<CbzReader>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); }},
|
||||
});
|
||||
}
|
||||
@@ -30,14 +27,12 @@ static FormatRegistry makeRegistry()
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static bool addEntry(zipFile zf, const char *name, const QByteArray &data, bool store = false)
|
||||
static bool addEntry(zipFile zf, const char *name, const QByteArray &data)
|
||||
{
|
||||
zip_fileinfo fi{};
|
||||
int method = store ? Z_NO_COMPRESSION : Z_DEFLATED;
|
||||
int level = store ? 0 : Z_DEFAULT_COMPRESSION;
|
||||
if (zipOpenNewFileInZip(zf, name, &fi,
|
||||
nullptr, 0, nullptr, 0, nullptr,
|
||||
method, level) != ZIP_OK)
|
||||
Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK)
|
||||
return false;
|
||||
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
||||
zipCloseFileInZip(zf);
|
||||
@@ -57,46 +52,17 @@ static QByteArray makePng(int w = 4, int h = 4, QRgb color = qRgb(255, 0, 0))
|
||||
return data;
|
||||
}
|
||||
|
||||
// Build a minimal EPUB with a cover image declared in OPF metadata.
|
||||
static QString buildEpubWithCover()
|
||||
static QString buildCbzWithCover()
|
||||
{
|
||||
static const char kMimetype[] = "application/epub+zip";
|
||||
|
||||
static const char kContainer[] =
|
||||
static const char kComicInfo[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">"
|
||||
" <rootfiles>"
|
||||
" <rootfile full-path=\"OEBPS/content.opf\""
|
||||
" media-type=\"application/oebps-package+xml\"/>"
|
||||
" </rootfiles>"
|
||||
"</container>";
|
||||
|
||||
// OPF with <meta name="cover" content="cover-img"/> referencing the cover item.
|
||||
static const char kOpf[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\" unique-identifier=\"BookId\">"
|
||||
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
|
||||
" <dc:title>Cover Test Book</dc:title>"
|
||||
" <dc:creator>Test Author</dc:creator>"
|
||||
" <meta name=\"cover\" content=\"cover-img\"/>"
|
||||
" </metadata>"
|
||||
" <manifest>"
|
||||
" <item id=\"cover-img\" href=\"Images/cover.png\" media-type=\"image/png\"/>"
|
||||
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
|
||||
" </manifest>"
|
||||
" <spine>"
|
||||
" <itemref idref=\"ch1\"/>"
|
||||
" </spine>"
|
||||
"</package>";
|
||||
|
||||
static const char kChapter[] =
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
|
||||
" <body><p>Content</p></body>"
|
||||
"</html>";
|
||||
"<ComicInfo>"
|
||||
" <Title>Cover Test Book</Title>"
|
||||
" <Writer>Test Author</Writer>"
|
||||
"</ComicInfo>";
|
||||
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_test_XXXXXX.epub");
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/hbm_cover_test_XXXXXX.cbz");
|
||||
tmp.setAutoRemove(false);
|
||||
if (!tmp.open()) return {};
|
||||
const QString path = tmp.fileName();
|
||||
@@ -106,11 +72,8 @@ static QString buildEpubWithCover()
|
||||
if (!zf) return {};
|
||||
|
||||
bool ok = true;
|
||||
ok = ok && addEntry(zf, "mimetype", QByteArray(kMimetype), /*store=*/true);
|
||||
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
|
||||
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(kOpf));
|
||||
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(kChapter));
|
||||
ok = ok && addEntry(zf, "OEBPS/Images/cover.png", makePng());
|
||||
ok = ok && addEntry(zf, "ComicInfo.xml", QByteArray(kComicInfo));
|
||||
ok = ok && addEntry(zf, "001_cover.png", makePng());
|
||||
|
||||
zipClose(zf, nullptr);
|
||||
return ok ? path : QString{};
|
||||
@@ -124,66 +87,59 @@ class HistoryBookModelTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void loadMetadataPopulatesCoverImageUrlForEpub()
|
||||
void loadMetadataPopulatesCoverImageUrlForCbz()
|
||||
{
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithCover();
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), registry);
|
||||
HistoryBookModel model(cbzPath, 0.0, QString(), registry);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
QSignalSpy coverSpy(&model, &HistoryBookModel::coverImageLoaded);
|
||||
model.loadMetadata();
|
||||
|
||||
// Wait up to 5 seconds for the async metadata load to complete
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
// coverImageUrl's NOTIFY signal must fire so QML bindings update
|
||||
QCOMPARE(coverSpy.count(), 1);
|
||||
|
||||
const QString url = model.coverImageUrl();
|
||||
QVERIFY2(!url.isEmpty(), "coverImageUrl should be set after loadMetadata for an EPUB with cover");
|
||||
// Format: image://epub/<internal-path>?src=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/Images/cover.png?src=")),
|
||||
QVERIFY2(!url.isEmpty(), "coverImageUrl should be set after loadMetadata for a CBZ with cover");
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://cbz/001_cover.png?src=")),
|
||||
qPrintable("Expected query-param URL format, got: " + url));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(cbzPath));
|
||||
QVERIFY2(url.contains(encodedPath),
|
||||
qPrintable("URL should contain encoded epub path. URL: " + url));
|
||||
qPrintable("URL should contain encoded cbz path. URL: " + url));
|
||||
}
|
||||
|
||||
void loadMetadataLeavesCoverImageUrlEmptyForTxt()
|
||||
void loadMetadataLeavesCoverImageUrlEmptyForMissingFile()
|
||||
{
|
||||
const QString txtPath = QStringLiteral(FIXTURES_DIR) + "/sample.txt";
|
||||
QVERIFY(QFile::exists(txtPath));
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(txtPath, 0.0, QString(), registry);
|
||||
HistoryBookModel model(QStringLiteral("/nonexistent/file.cbz"), 0.0, QString(), registry);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
// Should finish or not crash, coverImageUrl remains empty
|
||||
QVERIFY2(model.coverImageUrl().isEmpty(),
|
||||
"TXT files have no cover; coverImageUrl should remain empty");
|
||||
"Missing files have no cover; coverImageUrl should remain empty");
|
||||
}
|
||||
|
||||
void loadMetadataPopulatesTitle()
|
||||
{
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithCover();
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), registry);
|
||||
HistoryBookModel model(cbzPath, 0.0, QString(), registry);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
QVERIFY(spy.wait(5000));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QCOMPARE(model.title(), QStringLiteral("Cover Test Book"));
|
||||
QCOMPARE(model.author(), QStringLiteral("Test Author"));
|
||||
|
||||
Reference in new issue
Block a user