Files
asteria-qt/tests/formats/ImageUrlTest.cpp
T
2026-10-25 15:43:51 +01:00

85 lines
3.3 KiB
C++

#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 providerIdWithoutQueryHasEmptySourcePath()
{
const auto parsed = ImageUrl::parseProviderId(QStringLiteral("page01.png"));
QCOMPARE(parsed.entry, QStringLiteral("page01.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 resolveWebUrls()
{
const QString book = QStringLiteral("/books/test.epub");
QCOMPARE(ImageUrl::resolve(QStringLiteral("epub"), QStringLiteral("http://example.com/pic.png"), book),
QStringLiteral("image://web/http://example.com/pic.png"));
QCOMPARE(ImageUrl::resolve(QStringLiteral("epub"), QStringLiteral("https://example.com/pic.png"), book),
QStringLiteral("image://web/https://example.com/pic.png"));
}
void resolveRelativeUrlsWithBaseDir()
{
const QString book = QStringLiteral("/books/test.epub");
const QString url = ImageUrl::resolve(QStringLiteral("epub"),
QStringLiteral("../images/pic.png"),
book,
QStringLiteral("OEBPS/text/"));
QVERIFY(url.startsWith(QStringLiteral("image://epub/OEBPS/images/pic.png?src=")));
}
void resolveFb2Fragment()
{
const QString book = QStringLiteral("/books/test.fb2");
const QString url = ImageUrl::resolve(QStringLiteral("fb2"),
QStringLiteral("#cover.png"),
book);
QVERIFY(url.startsWith(QStringLiteral("image://fb2/cover.png?src=")));
}
};
QTEST_MAIN(ImageUrlTest)
#include "ImageUrlTest.moc"