Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -0,0 +1,418 @@
|
||||
#include <QtTest/QtTest>
|
||||
#include <QBuffer>
|
||||
#include <QDir>
|
||||
#include <QImage>
|
||||
#include <QSignalSpy>
|
||||
#include <QSize>
|
||||
#include <QTemporaryFile>
|
||||
#include <QThreadPool>
|
||||
#include <QUrl>
|
||||
|
||||
#include <minizip/zip.h>
|
||||
|
||||
#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,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
ZipByteSource source;
|
||||
if (!source.open(epubPath))
|
||||
return {};
|
||||
return EntryImageProvider::loadImageFromSource(source, entry, displayWidth,
|
||||
requestedSize);
|
||||
}
|
||||
|
||||
static EntryImageProvider makeEpubProvider()
|
||||
{
|
||||
return EntryImageProvider([] { return std::make_unique<ZipByteSource>(); },
|
||||
QStringLiteral("epub"));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
static bool addZipEntry(zipFile zf, const char *name, const QByteArray &data)
|
||||
{
|
||||
zip_fileinfo fi{};
|
||||
if (zipOpenNewFileInZip(zf, name, &fi,
|
||||
nullptr, 0, nullptr, 0, nullptr,
|
||||
Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK)
|
||||
return false;
|
||||
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
||||
zipCloseFileInZip(zf);
|
||||
return false;
|
||||
}
|
||||
return zipCloseFileInZip(zf) == ZIP_OK;
|
||||
}
|
||||
|
||||
// Create a QByteArray containing a valid PNG for an image of given size and color.
|
||||
static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QImage img(w, h, QImage::Format_RGB32);
|
||||
img.fill(color);
|
||||
QByteArray data;
|
||||
QBuffer buf(&data);
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
img.save(&buf, "PNG");
|
||||
return data;
|
||||
}
|
||||
|
||||
// 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,
|
||||
QRgb color = qRgb(255, 0, 0))
|
||||
{
|
||||
QTemporaryFile tmp;
|
||||
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_XXXXXX.epub");
|
||||
tmp.setAutoRemove(false);
|
||||
if (!tmp.open()) return {};
|
||||
const QString path = tmp.fileName();
|
||||
tmp.close();
|
||||
|
||||
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
|
||||
if (!zf) return {};
|
||||
|
||||
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH, color));
|
||||
zipClose(zf, nullptr);
|
||||
return ok ? path : QString{};
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Test class
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class EntryImageProviderTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void requestImageReturnsValidImage()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 4);
|
||||
QCOMPARE(img.height(), 4);
|
||||
QCOMPARE(img.size(), QSize(4, 4));
|
||||
}
|
||||
|
||||
void requestImageReturnsNullForMissingEntry()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/nonexistent.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void requestImageReturnsNullWhenNoPath()
|
||||
{
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void clearResetsState()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
// After clear the path is empty, so loadImageFromEpub with empty path returns null
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
|
||||
void largeImageScaledToDisplayWidth()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 600, 400);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 300);
|
||||
QCOMPARE(img.height(), 200);
|
||||
QCOMPARE(img.size(), QSize(300, 200));
|
||||
}
|
||||
|
||||
void smallImageNotEnlarged()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 100, 100);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 100);
|
||||
QCOMPARE(img.height(), 100);
|
||||
}
|
||||
|
||||
void requestedSizeHonored()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 400, 300);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 600, QSize(200, -1));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.width(), 200);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Async response tests
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void asyncResponseEmitsFinished()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QCOMPARE(spy.count(), 1);
|
||||
QVERIFY(response.textureFactory() != nullptr);
|
||||
}
|
||||
|
||||
void cancelledResponseDoesNotEmitFinished()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.cancel();
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QCOMPARE(spy.count(), 0);
|
||||
}
|
||||
|
||||
void cancelledTextureFactoryIsNull()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
response.cancel();
|
||||
response.run();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
QVERIFY(response.textureFactory() == nullptr);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Embedded-path URL format tests
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
void coverImageUrlEmbedsEpubPath()
|
||||
{
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubPath);
|
||||
|
||||
const QString url = provider.coverImageUrl(QStringLiteral("OEBPS/cover.jpg"));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
// New format: image://epub/<internal-path>?src=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/cover.jpg?src=")),
|
||||
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());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
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?src=") + 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());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
// 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?src=") + 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());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
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);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubPath);
|
||||
provider.clear();
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(
|
||||
QStringLiteral("img.png"), QSize());
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
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"), 0, 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)
|
||||
#include "EntryImageProviderTest.moc"
|
||||
Reference in new issue
Block a user