old render cleanup part 2
This commit is contained in:
40 files changed
+329
-3643
No files matched your search
@@ -12,23 +12,22 @@
|
||||
|
||||
#include "backend/formats/AsyncImageResponse.h"
|
||||
#include "backend/formats/EntryImageProvider.h"
|
||||
#include "backend/formats/sources/Fb2ByteSource.h"
|
||||
#include "backend/formats/sources/ZipByteSource.h"
|
||||
|
||||
// Load one zip member through the generic byte-source pipeline (EPUBs are zips).
|
||||
static QImage loadFromEpub(const QString &epubPath, const QString &entry,
|
||||
const QSize &requestedSize)
|
||||
// Load one zip member through the generic byte-source pipeline (CBZs are zips).
|
||||
static QImage loadFromCbz(const QString &cbzPath, const QString &entry,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
ZipByteSource source;
|
||||
if (!source.open(epubPath))
|
||||
if (!source.open(cbzPath))
|
||||
return {};
|
||||
return EntryImageProvider::loadImageFromSource(source, entry, requestedSize);
|
||||
}
|
||||
|
||||
static EntryImageProvider makeEpubProvider()
|
||||
static EntryImageProvider makeCbzProvider()
|
||||
{
|
||||
return EntryImageProvider([] { return std::make_unique<ZipByteSource>(); },
|
||||
QStringLiteral("epub"));
|
||||
QStringLiteral("cbz"));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -61,13 +60,13 @@ static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0))
|
||||
return data;
|
||||
}
|
||||
|
||||
// Build a minimal EPUB-like ZIP containing a single image entry.
|
||||
// Build a minimal CBZ containing a single image entry.
|
||||
// Returns the path of the temp file (caller removes).
|
||||
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH,
|
||||
QRgb color = qRgb(255, 0, 0))
|
||||
static QString buildCbzWithPng(const char *entryPath, int imgW, int imgH,
|
||||
QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.epub");
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.cbz");
|
||||
tmp.setAutoRemove(false);
|
||||
if (!tmp.open()) return {};
|
||||
const QString path = tmp.fileName();
|
||||
@@ -91,13 +90,13 @@ class EntryImageProviderTest : public QObject {
|
||||
private slots:
|
||||
void requestImageReturnsValidImage()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/test.png"), QSize());
|
||||
const QImage img = loadFromCbz(
|
||||
cbzPath, QStringLiteral("images/test.png"), QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 4);
|
||||
@@ -107,47 +106,46 @@ private slots:
|
||||
|
||||
void requestImageReturnsNullForMissingEntry()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/nonexistent.png"), QSize());
|
||||
const QImage img = loadFromCbz(
|
||||
cbzPath, QStringLiteral("images/nonexistent.png"), QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void requestImageReturnsNullWhenNoPath()
|
||||
{
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), QSize());
|
||||
const QImage img = loadFromCbz(
|
||||
QString{}, QStringLiteral("images/test.png"), QSize());
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void clearResetsState()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("images/test.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
// After clear the path is empty, so loadImageFromEpub with empty path returns null
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), QSize());
|
||||
const QImage img = loadFromCbz(
|
||||
QString{}, QStringLiteral("images/test.png"), QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void requestedSizeHonored()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 400, 300);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 400, 300);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), QSize(200, -1));
|
||||
const QImage img = loadFromCbz(
|
||||
cbzPath, QStringLiteral("img.png"), QSize(200, -1));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 200);
|
||||
@@ -159,17 +157,17 @@ private slots:
|
||||
|
||||
void asyncResponseEmitsFinished()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), QSize());
|
||||
AsyncImageResponse response([cbzPath] {
|
||||
return loadFromCbz(
|
||||
cbzPath, QStringLiteral("img.png"), QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QVERIFY(response.textureFactory() != nullptr);
|
||||
@@ -177,35 +175,35 @@ private slots:
|
||||
|
||||
void cancelledResponseDoesNotEmitFinished()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), QSize());
|
||||
AsyncImageResponse response([cbzPath] {
|
||||
return loadFromCbz(
|
||||
cbzPath, QStringLiteral("img.png"), QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.cancel();
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QCOMPARE(spy.count(), 0);
|
||||
}
|
||||
|
||||
void cancelledTextureFactoryIsNull()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), QSize());
|
||||
AsyncImageResponse response([cbzPath] {
|
||||
return loadFromCbz(
|
||||
cbzPath, QStringLiteral("img.png"), QSize());
|
||||
});
|
||||
response.cancel();
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(response.textureFactory() == nullptr);
|
||||
}
|
||||
@@ -214,39 +212,36 @@ private slots:
|
||||
// Embedded-path URL format tests
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void coverImageUrlEmbedsEpubPath()
|
||||
void coverImageUrlEmbedsCbzPath()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubPath);
|
||||
EntryImageProvider provider = makeCbzProvider();
|
||||
provider.setFilePath(cbzPath);
|
||||
|
||||
const QString url = provider.coverImageUrl(QStringLiteral("OEBPS/cover.jpg"));
|
||||
const QString url = provider.coverImageUrl(QStringLiteral("cover.jpg"));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
// New format: image://epub/<internal-path>?src=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/cover.jpg?src=")),
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://cbz/cover.jpg?src=")),
|
||||
qPrintable("Expected query-param URL, 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 requestImageResponseUsesEmbeddedPath()
|
||||
{
|
||||
// Build two EPUBs with different color images; prove the embedded path in the URL
|
||||
// overrides m_epubPath on the provider.
|
||||
const QString epubA = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
const QString epubB = buildEpubWithPng("img.png", 4, 4, qRgb(0, 255, 0));
|
||||
QVERIFY(!epubA.isEmpty() && !epubB.isEmpty());
|
||||
const QString cbzA = buildCbzWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
const QString cbzB = buildCbzWithPng("img.png", 4, 4, qRgb(0, 255, 0));
|
||||
QVERIFY(!cbzA.isEmpty() && !cbzB.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubA); // configured for A
|
||||
EntryImageProvider provider = makeCbzProvider();
|
||||
provider.setFilePath(cbzA); // configured for A
|
||||
|
||||
// Build an embedded-format id pointing to B via query parameter
|
||||
const QString encodedB = QString::fromUtf8(QUrl::toPercentEncoding(epubB));
|
||||
const QString encodedB = QString::fromUtf8(QUrl::toPercentEncoding(cbzB));
|
||||
const QString id = QStringLiteral("img.png?src=") + encodedB;
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(id, QSize());
|
||||
@@ -254,11 +249,10 @@ private slots:
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
if (spy.isEmpty()) spy.wait(5000);
|
||||
|
||||
QFile::remove(epubA);
|
||||
QFile::remove(epubB);
|
||||
QFile::remove(cbzA);
|
||||
QFile::remove(cbzB);
|
||||
|
||||
QVERIFY(resp->textureFactory() != nullptr);
|
||||
// The loaded image should be green (from B), not red (from A)
|
||||
const QImage img = resp->textureFactory()->image();
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(0, 255, 0));
|
||||
@@ -268,21 +262,19 @@ private slots:
|
||||
|
||||
void requestImageResponseFallsBackToProviderPath()
|
||||
{
|
||||
// Without a leading slash in id, the provider should use m_epubPath.
|
||||
const QString epubA = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubA.isEmpty());
|
||||
const QString cbzA = buildCbzWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!cbzA.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubA);
|
||||
EntryImageProvider provider = makeCbzProvider();
|
||||
provider.setFilePath(cbzA);
|
||||
|
||||
// No query param — old format, uses m_epubPath
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(
|
||||
QStringLiteral("img.png"), QSize());
|
||||
QSignalSpy spy(resp, &QQuickImageResponse::finished);
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
if (spy.isEmpty()) spy.wait(5000);
|
||||
|
||||
QFile::remove(epubA);
|
||||
QFile::remove(cbzA);
|
||||
|
||||
QVERIFY(resp->textureFactory() != nullptr);
|
||||
const QImage img = resp->textureFactory()->image();
|
||||
@@ -294,74 +286,22 @@ private slots:
|
||||
|
||||
void responseAfterClearIsNull()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
const QString cbzPath = buildCbzWithPng("img.png", 4, 4);
|
||||
QVERIFY(!cbzPath.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubPath);
|
||||
EntryImageProvider provider = makeCbzProvider();
|
||||
provider.setFilePath(cbzPath);
|
||||
provider.clear();
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(
|
||||
QStringLiteral("img.png"), QSize());
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
QFile::remove(cbzPath);
|
||||
|
||||
QVERIFY(resp->textureFactory() == nullptr);
|
||||
delete resp;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Fb2ByteSource
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void fb2ByteSourceReadsBinaryById()
|
||||
{
|
||||
const QByteArray png = makePng(4, 4, qRgb(0, 0, 255));
|
||||
const QByteArray fb2 = QByteArrayLiteral(
|
||||
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
"<FictionBook><description/><body><p>text</p></body>"
|
||||
"<binary id=\"cover.png\" content-type=\"image/png\">")
|
||||
+ png.toBase64()
|
||||
+ QByteArrayLiteral("</binary></FictionBook>");
|
||||
|
||||
QTemporaryFile tmp(QDir::tempPath() + "/fb2_source_XXXXXX.fb2");
|
||||
tmp.setAutoRemove(false);
|
||||
QVERIFY(tmp.open());
|
||||
tmp.write(fb2);
|
||||
const QString path = tmp.fileName();
|
||||
tmp.close();
|
||||
|
||||
Fb2ByteSource source;
|
||||
QVERIFY(source.open(path));
|
||||
QCOMPARE(source.listEntries(), QStringList{QStringLiteral("cover.png")});
|
||||
|
||||
const QImage img = EntryImageProvider::loadImageFromSource(
|
||||
source, QStringLiteral("cover.png"), QSize());
|
||||
QVERIFY(!source.contentHash().isEmpty());
|
||||
|
||||
QFile::remove(path);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(0, 0, 255));
|
||||
}
|
||||
|
||||
void fb2ByteSourceMissingIdReturnsEmpty()
|
||||
{
|
||||
QTemporaryFile tmp(QDir::tempPath() + "/fb2_source_XXXXXX.fb2");
|
||||
tmp.setAutoRemove(false);
|
||||
QVERIFY(tmp.open());
|
||||
tmp.write(QByteArrayLiteral("<?xml version=\"1.0\"?><FictionBook/>"));
|
||||
const QString path = tmp.fileName();
|
||||
tmp.close();
|
||||
|
||||
Fb2ByteSource source;
|
||||
QVERIFY(source.open(path));
|
||||
const QByteArray data = source.readEntry(QStringLiteral("nope"));
|
||||
|
||||
QFile::remove(path);
|
||||
QVERIFY(data.isEmpty());
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(EntryImageProviderTest)
|
||||
|
||||
Reference in new issue
Block a user