Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -10,7 +10,27 @@
|
||||
|
||||
#include <minizip/zip.h>
|
||||
|
||||
#include "backend/formats/EpubImageProvider.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
|
||||
@@ -66,7 +86,7 @@ static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH,
|
||||
// Test class
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class EpubImageProviderTest : public QObject {
|
||||
class EntryImageProviderTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
@@ -75,7 +95,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -91,7 +111,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("OEBPS/Images/nonexistent.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -101,7 +121,7 @@ private slots:
|
||||
|
||||
void requestImageReturnsNullWhenNoPath()
|
||||
{
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
QVERIFY(img.isNull());
|
||||
}
|
||||
@@ -112,7 +132,7 @@ private slots:
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
// After clear the path is empty, so loadImageFromEpub with empty path returns null
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -125,7 +145,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 600, 400);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -141,7 +161,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 100, 100);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -156,7 +176,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 400, 300);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||
const QImage img = loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 600, QSize(200, -1));
|
||||
|
||||
QFile::remove(epubPath);
|
||||
@@ -174,7 +194,10 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.run();
|
||||
|
||||
@@ -189,7 +212,10 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||
response.cancel();
|
||||
response.run();
|
||||
@@ -204,7 +230,10 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
AsyncImageResponse response([epubPath] {
|
||||
return loadFromEpub(
|
||||
epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||
});
|
||||
response.cancel();
|
||||
response.run();
|
||||
|
||||
@@ -222,15 +251,15 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
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=")),
|
||||
// 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),
|
||||
@@ -245,12 +274,12 @@ private slots:
|
||||
const QString epubB = buildEpubWithPng("img.png", 4, 4, qRgb(0, 255, 0));
|
||||
QVERIFY(!epubA.isEmpty() && !epubB.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
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?epub=") + encodedB;
|
||||
const QString id = QStringLiteral("img.png?src=") + encodedB;
|
||||
|
||||
QQuickImageResponse *resp = provider.requestImageResponse(id, QSize());
|
||||
QSignalSpy spy(resp, &QQuickImageResponse::finished);
|
||||
@@ -274,10 +303,10 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
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?epub=") + encodedPath);
|
||||
const QUrl url(QStringLiteral("image://epub/img.png?src=") + encodedPath);
|
||||
|
||||
const QImage img = provider.load(url, 0);
|
||||
|
||||
@@ -293,7 +322,7 @@ private slots:
|
||||
const QString epubA = buildEpubWithPng("img.png", 4, 4, qRgb(255, 0, 0));
|
||||
QVERIFY(!epubA.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubA);
|
||||
|
||||
// No query param — old format, uses m_epubPath
|
||||
@@ -318,7 +347,7 @@ private slots:
|
||||
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
EpubImageProvider provider;
|
||||
EntryImageProvider provider = makeEpubProvider();
|
||||
provider.setFilePath(epubPath);
|
||||
provider.clear();
|
||||
|
||||
@@ -331,7 +360,59 @@ private slots:
|
||||
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(EpubImageProviderTest)
|
||||
#include "EpubImageProviderTest.moc"
|
||||
QTEST_MAIN(EntryImageProviderTest)
|
||||
#include "EntryImageProviderTest.moc"
|
||||
@@ -0,0 +1,141 @@
|
||||
#include <QtTest>
|
||||
#include <QTemporaryDir>
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/IImageProvider.h"
|
||||
#include "backend/IImageLoader.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/comic/CbzReader.h"
|
||||
#include "backend/formats/comic/CbrReader.h"
|
||||
#include "backend/formats/comic/FolderReader.h"
|
||||
#include "backend/formats/sources/DirByteSource.h"
|
||||
#include "backend/formats/sources/RarByteSource.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>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); },
|
||||
/*loaderIncludesWeb=*/true},
|
||||
{QStringLiteral("cbz"), false, QStringLiteral("cbz"),
|
||||
[] { return std::make_unique<CbzReader>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); }},
|
||||
{QStringLiteral("cbr"), false, QStringLiteral("cbr"),
|
||||
[] { return std::make_unique<CbrReader>(); },
|
||||
[] { return std::make_unique<RarByteSource>(); }},
|
||||
{{}, /*matchesDirectory=*/true, QStringLiteral("dir"),
|
||||
[] { return std::make_unique<FolderReader>(); },
|
||||
[] { return std::make_unique<DirByteSource>(); }},
|
||||
});
|
||||
}
|
||||
|
||||
class FormatRegistryTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void createTxtReader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.txt") != nullptr);
|
||||
}
|
||||
|
||||
void createTxtReaderUpperCase()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.TXT") != nullptr);
|
||||
}
|
||||
|
||||
void createUnknownFormat()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.xyz") == nullptr);
|
||||
}
|
||||
|
||||
void createNoExtension()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("noext") == nullptr);
|
||||
}
|
||||
|
||||
void createEmptyPath()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("") == nullptr);
|
||||
}
|
||||
|
||||
void createCbzReader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.cbz") != nullptr);
|
||||
}
|
||||
|
||||
void createCbrReader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.cbr") != nullptr);
|
||||
}
|
||||
|
||||
void createFolderReader()
|
||||
{
|
||||
QTemporaryDir tmp;
|
||||
QVERIFY(tmp.isValid());
|
||||
auto registry = makeRegistry();
|
||||
auto reader = registry.createReader(tmp.path());
|
||||
QVERIFY(reader != nullptr);
|
||||
QCOMPARE(reader->fileType(), QStringLiteral("folder"));
|
||||
}
|
||||
|
||||
void supportedExtensions()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
const QStringList exts = registry.supportedExtensions();
|
||||
QVERIFY(exts.contains(QStringLiteral("txt")));
|
||||
QVERIFY(exts.contains(QStringLiteral("epub")));
|
||||
QVERIFY(exts.contains(QStringLiteral("cbz")));
|
||||
QVERIFY(exts.contains(QStringLiteral("cbr")));
|
||||
// The directory row contributes no extension
|
||||
QCOMPARE(exts.size(), 4);
|
||||
}
|
||||
|
||||
void imageProviderSchemesMatchTable()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
|
||||
std::unique_ptr<IImageProvider> epub(registry.createImageProvider("book.epub"));
|
||||
QVERIFY(epub);
|
||||
QCOMPARE(epub->scheme(), QStringLiteral("epub"));
|
||||
|
||||
std::unique_ptr<IImageProvider> cbz(registry.createImageProvider("comic.cbz"));
|
||||
QVERIFY(cbz);
|
||||
QCOMPARE(cbz->scheme(), QStringLiteral("cbz"));
|
||||
|
||||
QTemporaryDir tmp;
|
||||
std::unique_ptr<IImageProvider> dir(registry.createImageProvider(tmp.path()));
|
||||
QVERIFY(dir);
|
||||
QCOMPARE(dir->scheme(), QStringLiteral("dir"));
|
||||
}
|
||||
|
||||
void formatsWithoutImagesHaveNoProviderOrLoader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createImageProvider("plain.txt") == nullptr);
|
||||
QVERIFY(registry.createImageLoader("plain.txt") == nullptr);
|
||||
QVERIFY(registry.createImageProvider("file.xyz") == nullptr);
|
||||
}
|
||||
|
||||
void createImageLoaderReturnsLoader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createImageLoader("comic.cbz") != nullptr);
|
||||
// epub loader is a composite (epub + web) but still non-null
|
||||
QVERIFY(registry.createImageLoader("book.epub") != nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(FormatRegistryTest)
|
||||
#include "FormatRegistryTest.moc"
|
||||
@@ -12,13 +12,18 @@
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/ReaderFactory.h"
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/formats/sources/ZipByteSource.h"
|
||||
|
||||
static ReaderFactory makeFactory()
|
||||
static FormatRegistry makeRegistry()
|
||||
{
|
||||
return ReaderFactory({
|
||||
[] { return std::make_unique<TxtReader>(); },
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
return FormatRegistry({
|
||||
{QStringLiteral("txt"), false, {},
|
||||
[] { return std::make_unique<TxtReader>(); }, nullptr},
|
||||
{QStringLiteral("epub"), false, QStringLiteral("epub"),
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
[] { return std::make_unique<ZipByteSource>(); },
|
||||
/*loaderIncludesWeb=*/true},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -125,10 +130,11 @@ private slots:
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), factory);
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(epubPath, 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
|
||||
@@ -136,10 +142,13 @@ private slots:
|
||||
|
||||
QFile::remove(epubPath);
|
||||
|
||||
// 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>?epub=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/Images/cover.png?epub=")),
|
||||
// Format: image://epub/<internal-path>?src=<encoded-epub-path>
|
||||
QVERIFY2(url.startsWith(QStringLiteral("image://epub/OEBPS/Images/cover.png?src=")),
|
||||
qPrintable("Expected query-param URL format, got: " + url));
|
||||
const QString encodedPath = QString::fromUtf8(QUrl::toPercentEncoding(epubPath));
|
||||
QVERIFY2(url.contains(encodedPath),
|
||||
@@ -151,8 +160,8 @@ private slots:
|
||||
const QString txtPath = QStringLiteral(FIXTURES_DIR) + "/sample.txt";
|
||||
QVERIFY(QFile::exists(txtPath));
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(txtPath, 0.0, QString(), factory);
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(txtPath, 0.0, QString(), registry);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
@@ -168,8 +177,8 @@ private slots:
|
||||
const QString epubPath = buildEpubWithCover();
|
||||
QVERIFY(!epubPath.isEmpty());
|
||||
|
||||
ReaderFactory factory = makeFactory();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), factory);
|
||||
FormatRegistry registry = makeRegistry();
|
||||
HistoryBookModel model(epubPath, 0.0, QString(), registry);
|
||||
|
||||
QSignalSpy spy(&model, &HistoryBookModel::metadataLoaded);
|
||||
model.loadMetadata();
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
#include <QtTest>
|
||||
#include <QTemporaryDir>
|
||||
#include "backend/formats/ReaderFactory.h"
|
||||
#include "backend/FormatReader.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/comic/CbzReader.h"
|
||||
#include "backend/formats/comic/CbrReader.h"
|
||||
#include "backend/formats/comic/FolderReader.h"
|
||||
|
||||
static ReaderFactory makeFactory()
|
||||
{
|
||||
return ReaderFactory({
|
||||
[] { return std::make_unique<TxtReader>(); },
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
[] { return std::make_unique<CbzReader>(); },
|
||||
[] { return std::make_unique<CbrReader>(); },
|
||||
[] { return std::make_unique<FolderReader>(); },
|
||||
});
|
||||
}
|
||||
|
||||
class ReaderFactoryTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void createTxtReader()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("file.txt") != nullptr);
|
||||
}
|
||||
|
||||
void createTxtReaderUpperCase()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("file.TXT") != nullptr);
|
||||
}
|
||||
|
||||
void createUnknownFormat()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("file.xyz") == nullptr);
|
||||
}
|
||||
|
||||
void createNoExtension()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("noext") == nullptr);
|
||||
}
|
||||
|
||||
void createEmptyPath()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("") == nullptr);
|
||||
}
|
||||
|
||||
void createCbzReader()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("file.cbz") != nullptr);
|
||||
}
|
||||
|
||||
void createCbrReader()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
QVERIFY(factory.create("file.cbr") != nullptr);
|
||||
}
|
||||
|
||||
void createFolderReader()
|
||||
{
|
||||
QTemporaryDir tmp;
|
||||
QVERIFY(tmp.isValid());
|
||||
auto factory = makeFactory();
|
||||
auto reader = factory.create(tmp.path());
|
||||
QVERIFY(reader != nullptr);
|
||||
QCOMPARE(reader->fileType(), QStringLiteral("<dir>"));
|
||||
}
|
||||
|
||||
void supportedExtensions()
|
||||
{
|
||||
auto factory = makeFactory();
|
||||
const QStringList exts = factory.supportedExtensions();
|
||||
QVERIFY(exts.contains(QStringLiteral("txt")));
|
||||
QVERIFY(exts.contains(QStringLiteral("epub")));
|
||||
QVERIFY(exts.contains(QStringLiteral("cbz")));
|
||||
QVERIFY(exts.contains(QStringLiteral("cbr")));
|
||||
// Sentinel for directory must not appear in the extensions list
|
||||
QVERIFY(!exts.contains(QStringLiteral("<dir>")));
|
||||
for (const QString &e : exts)
|
||||
QVERIFY(!e.startsWith(QLatin1Char('<')));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(ReaderFactoryTest)
|
||||
#include "ReaderFactoryTest.moc"
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "backend/AppState.h"
|
||||
#include "backend/IFileDialogService.h"
|
||||
#include "backend/formats/ReaderFactory.h"
|
||||
#include "backend/formats/FormatRegistry.h"
|
||||
#include "backend/formats/TxtReader.h"
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/state/BookPositionStore.h"
|
||||
@@ -49,7 +49,7 @@ private:
|
||||
AppState *appState = nullptr;
|
||||
BookPositionStore *store = nullptr;
|
||||
FakeDialogService *dialog = nullptr;
|
||||
ReaderFactory *factory = nullptr;
|
||||
FormatRegistry *factory = nullptr;
|
||||
Book *book = nullptr;
|
||||
|
||||
Ctx() {
|
||||
@@ -59,9 +59,11 @@ private:
|
||||
appState = new AppState(*s);
|
||||
store = new BookPositionStore(*appState);
|
||||
dialog = new FakeDialogService;
|
||||
factory = new ReaderFactory({
|
||||
[] { return std::make_unique<TxtReader>(); },
|
||||
[] { return std::make_unique<EpubReader>(); },
|
||||
factory = new FormatRegistry({
|
||||
{QStringLiteral("txt"), false, {},
|
||||
[] { return std::make_unique<TxtReader>(); }, nullptr},
|
||||
{QStringLiteral("epub"), false, QStringLiteral("epub"),
|
||||
[] { return std::make_unique<EpubReader>(); }, nullptr},
|
||||
});
|
||||
book = new Book(*dialog, *store, *factory);
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ private slots:
|
||||
void fileTypeAndRenderingMode()
|
||||
{
|
||||
FolderReader reader;
|
||||
QCOMPARE(reader.fileType(), QStringLiteral("<dir>"));
|
||||
QCOMPARE(reader.fileType(), QStringLiteral("folder"));
|
||||
QCOMPARE(static_cast<int>(reader.preferredRenderingMode()),
|
||||
static_cast<int>(FormatReader::RenderingMode::Manga));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <QtTest/QtTest>
|
||||
#include <QUrl>
|
||||
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
class ImageUrlTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void buildWithoutQuery()
|
||||
{
|
||||
QCOMPARE(ImageUrl::build(QStringLiteral("cbz"), QStringLiteral("page01.jpg")),
|
||||
QStringLiteral("image://cbz/page01.jpg"));
|
||||
}
|
||||
|
||||
void buildWithSourcePath()
|
||||
{
|
||||
const QString url = ImageUrl::build(QStringLiteral("cbz"),
|
||||
QStringLiteral("page01.jpg"),
|
||||
QStringLiteral("/books/My Comic #1.cbz"));
|
||||
QVERIFY(url.startsWith(QStringLiteral("image://cbz/page01.jpg?src=")));
|
||||
// Source path must be percent-encoded (space and '#')
|
||||
QVERIFY(!url.contains(QLatin1Char(' ')));
|
||||
QVERIFY(!url.contains(QLatin1Char('#')));
|
||||
}
|
||||
|
||||
void roundTripThroughProviderId()
|
||||
{
|
||||
const QString sourcePath = QStringLiteral("/books/My Comic #1.cbz");
|
||||
const QString url = ImageUrl::build(QStringLiteral("cbz"),
|
||||
QStringLiteral("page01.jpg"), sourcePath);
|
||||
// QML hands the provider everything after "image://<scheme>/"
|
||||
const QString id = url.mid(QStringLiteral("image://cbz/").size());
|
||||
|
||||
const auto parsed = ImageUrl::parseProviderId(id);
|
||||
QCOMPARE(parsed.entry, QStringLiteral("page01.jpg"));
|
||||
QCOMPARE(parsed.sourcePath, sourcePath);
|
||||
}
|
||||
|
||||
void roundTripThroughImageUrl()
|
||||
{
|
||||
const QString sourcePath = QStringLiteral("/books/unicode путь.cbz");
|
||||
const QString url = ImageUrl::build(QStringLiteral("cbz"),
|
||||
QStringLiteral("dir/page01.jpg"), sourcePath);
|
||||
|
||||
const auto parsed = ImageUrl::parseImageUrl(QUrl(url));
|
||||
QCOMPARE(parsed.entry, QStringLiteral("dir/page01.jpg"));
|
||||
QCOMPARE(parsed.sourcePath, sourcePath);
|
||||
}
|
||||
|
||||
void providerIdWithoutQueryHasEmptySourcePath()
|
||||
{
|
||||
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("OEBPS/img.png"));
|
||||
QCOMPARE(parsed.entry, QStringLiteral("OEBPS/img.png"));
|
||||
QVERIFY(parsed.sourcePath.isEmpty());
|
||||
}
|
||||
|
||||
void providerIdDecodesPercentEncodedEntry()
|
||||
{
|
||||
// QML percent-encodes entry names before they reach the provider
|
||||
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("page%2001.jpg"));
|
||||
QCOMPARE(parsed.entry, QStringLiteral("page 01.jpg"));
|
||||
}
|
||||
|
||||
void entryWithSlashesSurvivesParsing()
|
||||
{
|
||||
const auto parsed = ImageUrl::parseImageUrl(
|
||||
QUrl(QStringLiteral("image://dir/nested/deep/page.png")));
|
||||
QCOMPARE(parsed.entry, QStringLiteral("nested/deep/page.png"));
|
||||
QVERIFY(parsed.sourcePath.isEmpty());
|
||||
}
|
||||
|
||||
void querySuffixMatchesBuild()
|
||||
{
|
||||
const QString sourcePath = QStringLiteral("/tmp/a b.fb2");
|
||||
QCOMPARE(ImageUrl::build(QStringLiteral("fb2"), QStringLiteral("cover"), sourcePath),
|
||||
ImageUrl::build(QStringLiteral("fb2"), QStringLiteral("cover"))
|
||||
+ ImageUrl::querySuffix(sourcePath));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(ImageUrlTest)
|
||||
#include "ImageUrlTest.moc"
|
||||
@@ -0,0 +1,124 @@
|
||||
#include <QtTest>
|
||||
|
||||
#include "backend/reading/MangaPagination.h"
|
||||
|
||||
static QSize portrait() { return {800, 1200}; }
|
||||
static QSize landscape() { return {1600, 1200}; }
|
||||
|
||||
class MangaPaginationTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void portraitViewportAllSolo()
|
||||
{
|
||||
MangaPagination p;
|
||||
p.rebuild({portrait(), portrait(), portrait()}, /*landscape=*/false, /*shift=*/true);
|
||||
QCOMPARE(p.count(), 3);
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
QCOMPARE(p.at(i).first, i);
|
||||
QCOMPARE(p.at(i).second, -1);
|
||||
}
|
||||
}
|
||||
|
||||
void landscapeCoverSoloThenPairs()
|
||||
{
|
||||
MangaPagination p;
|
||||
// cover + 4 portrait pages, shift off: cover solo, (1,2), (3,4)
|
||||
p.rebuild({portrait(), portrait(), portrait(), portrait(), portrait()},
|
||||
true, /*shift=*/false);
|
||||
QCOMPARE(p.count(), 3);
|
||||
QCOMPARE(p.at(0).first, 0);
|
||||
QCOMPARE(p.at(0).second, -1);
|
||||
QCOMPARE(p.at(1).first, 1);
|
||||
QCOMPARE(p.at(1).second, 2);
|
||||
QCOMPARE(p.at(2).first, 3);
|
||||
QCOMPARE(p.at(2).second, 4);
|
||||
}
|
||||
|
||||
void shiftMakesFirstPageSolo()
|
||||
{
|
||||
MangaPagination p;
|
||||
// shift on: cover solo, page 1 solo, (2,3), 4 solo
|
||||
p.rebuild({portrait(), portrait(), portrait(), portrait(), portrait()},
|
||||
true, /*shift=*/true);
|
||||
QCOMPARE(p.count(), 4);
|
||||
QCOMPARE(p.at(1).first, 1);
|
||||
QCOMPARE(p.at(1).second, -1);
|
||||
QCOMPARE(p.at(2).first, 2);
|
||||
QCOMPARE(p.at(2).second, 3);
|
||||
QCOMPARE(p.at(3).first, 4);
|
||||
QCOMPARE(p.at(3).second, -1);
|
||||
}
|
||||
|
||||
void widePagesStaySolo()
|
||||
{
|
||||
MangaPagination p;
|
||||
// cover, portrait, WIDE, portrait, portrait — wide page must not pair
|
||||
p.rebuild({portrait(), portrait(), landscape(), portrait(), portrait()},
|
||||
true, /*shift=*/false);
|
||||
QCOMPARE(p.count(), 4);
|
||||
QCOMPARE(p.at(1).first, 1);
|
||||
QCOMPARE(p.at(1).second, -1); // can't pair: next is wide
|
||||
QCOMPARE(p.at(2).first, 2);
|
||||
QCOMPARE(p.at(2).second, -1); // wide page solo
|
||||
QCOMPARE(p.at(3).first, 3);
|
||||
QCOMPARE(p.at(3).second, 4);
|
||||
}
|
||||
|
||||
void invalidSizesTreatedAsPortrait()
|
||||
{
|
||||
MangaPagination p;
|
||||
p.rebuild({QSize(), QSize(), QSize()}, true, /*shift=*/false);
|
||||
QCOMPARE(p.count(), 2);
|
||||
QCOMPARE(p.at(1).first, 1);
|
||||
QCOMPARE(p.at(1).second, 2);
|
||||
}
|
||||
|
||||
void pairIndexForChunkFindsBothSlots()
|
||||
{
|
||||
MangaPagination p;
|
||||
p.rebuild({portrait(), portrait(), portrait(), portrait(), portrait()},
|
||||
true, /*shift=*/false);
|
||||
QCOMPARE(p.pairIndexForChunk(0), 0);
|
||||
QCOMPARE(p.pairIndexForChunk(1), 1);
|
||||
QCOMPARE(p.pairIndexForChunk(2), 1);
|
||||
QCOMPARE(p.pairIndexForChunk(3), 2);
|
||||
QCOMPARE(p.pairIndexForChunk(4), 2);
|
||||
QCOMPARE(p.pairIndexForChunk(99), 0); // not found falls back to 0
|
||||
}
|
||||
|
||||
void anchorStableAcrossDoubleToggle()
|
||||
{
|
||||
const QVector<QSize> sizes = {portrait(), portrait(), portrait(),
|
||||
portrait(), portrait()};
|
||||
MangaPagination p;
|
||||
p.rebuild(sizes, true, /*shift=*/false); // pairs: [0], (1,2), (3,4)
|
||||
|
||||
// Viewing pair (1,2); toggle shift ON → anchor on .second (chunk 2)
|
||||
const int anchorOn = p.anchorChunkForShiftToggle(1, /*shiftTurningOn=*/true);
|
||||
QCOMPARE(anchorOn, 2);
|
||||
|
||||
p.rebuild(sizes, true, /*shift=*/true); // pairs: [0], [1], (2,3), [4]
|
||||
const int pairOn = p.pairIndexForChunk(anchorOn);
|
||||
QCOMPARE(pairOn, 2);
|
||||
|
||||
// Toggle back OFF → anchor on .first (chunk 2), land on (1,2) again
|
||||
const int anchorOff = p.anchorChunkForShiftToggle(pairOn, /*shiftTurningOn=*/false);
|
||||
QCOMPARE(anchorOff, 2);
|
||||
p.rebuild(sizes, true, /*shift=*/false);
|
||||
QCOMPARE(p.pairIndexForChunk(anchorOff), 1);
|
||||
}
|
||||
|
||||
void clearEmpties()
|
||||
{
|
||||
MangaPagination p;
|
||||
p.rebuild({portrait()}, false, false);
|
||||
QCOMPARE(p.count(), 1);
|
||||
p.clear();
|
||||
QCOMPARE(p.count(), 0);
|
||||
QCOMPARE(p.at(0).first, -1); // out of range is safe
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(MangaPaginationTest)
|
||||
#include "MangaPaginationTest.moc"
|
||||
Reference in new issue
Block a user