Files
asteria-qt/tests/formats/ImageUrlTest.cpp
T
2026-07-31 17:31:02 +02:00

84 lines
3.1 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 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"