Add back history view cover photos
This commit is contained in:
11 files changed
+476
-28
No files matched your search
@@ -6,6 +6,7 @@
|
||||
#include <QSize>
|
||||
#include <QTemporaryFile>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/zip.h>
|
||||
|
||||
@@ -43,7 +44,8 @@ static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0))
|
||||
|
||||
// Build a minimal EPUB-like ZIP containing a single image entry.
|
||||
// Returns the path of the temp file (caller removes).
|
||||
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH)
|
||||
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH,
|
||||
QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.epub");
|
||||
@@ -55,7 +57,7 @@ static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH)
|
||||
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
|
||||
if (!zf) return {};
|
||||
|
||||
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH));
|
||||
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH, color));
|
||||
zipClose(zf, nullptr);
|
||||
return ok ? path : QString{};
|
||||
}
|
||||
@@ -211,6 +213,106 @@ private slots:
|
||||
QVERIFY(response.textureFactory() == nullptr);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Embedded-path URL format tests
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void coverImageUrlEmbedsEpubPath()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubPath);
|
||||
|
||||
const QString url = provider.coverImageUrl(QStringLiteral("OEBPS/cover.jpg"));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
// New format: image://epub/<internal-path>?epub=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/cover.jpg?epub=")),
|
||||
qPrintable("Expected query-param URL, got: " + url));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
QVERIFY2(url.contains(encodedPath),
|
||||
qPrintable("URL should contain encoded epub 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());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubA); // configured for A
|
||||
|
||||
// Build an embedded-format id pointing to B via query parameter
|
||||
const QString encodedB = QString::fromUtf8(QUrl::toPercentEncoding(epubB));
|
||||
const QString id = QStringLiteral("img.png?epub=") + encodedB;
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(id, QSize());
|
||||
QSignalSpy spy(resp, &QQuickImageResponse::finished);
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
if (spy.isEmpty()) spy.wait(5000);
|
||||
|
||||
QFile::remove(epubA);
|
||||
QFile::remove(epubB);
|
||||
|
||||
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));
|
||||
|
||||
delete resp;
|
||||
}
|
||||
|
||||
void loadMethodHandlesEmbeddedUrlFormat()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
// m_epubPath is intentionally NOT set — embedded URL must supply it via query
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
const QUrl url(QStringLiteral("image://epub/img.png?epub=") + encodedPath);
|
||||
|
||||
const QImage img = provider.load(url, 0);
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(255, 0, 0));
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
EpubImageProvider provider;
|
||||
provider.setFilePath(epubA);
|
||||
|
||||
// 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);
|
||||
|
||||
QVERIFY(resp->textureFactory() != nullptr);
|
||||
const QImage img = resp->textureFactory()->image();
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.pixel(0, 0), qRgb(255, 0, 0));
|
||||
|
||||
delete resp;
|
||||
}
|
||||
|
||||
void responseAfterClearIsNull()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
|
||||
Reference in new issue
Block a user