115 lines
3.4 KiB
C++
115 lines
3.4 KiB
C++
#include <QtTest>
|
|
#include <QBuffer>
|
|
#include <QHash>
|
|
#include "backend/formats/WebImageProvider.h"
|
|
|
|
class MockWebStorage : public IWebImageStorage {
|
|
public:
|
|
bool contains(const QString &key) const override {
|
|
return m_store.contains(key);
|
|
}
|
|
QByteArray read(const QString &key) const override {
|
|
return m_store.value(key);
|
|
}
|
|
bool write(const QString &key, const QByteArray &data) override {
|
|
m_store.insert(key, data);
|
|
return true;
|
|
}
|
|
|
|
QHash<QString, QByteArray> m_store;
|
|
};
|
|
|
|
static QByteArray createSamplePng()
|
|
{
|
|
QImage img(100, 100, QImage::Format_RGB32);
|
|
img.fill(Qt::red);
|
|
QByteArray bytes;
|
|
QBuffer buf(&bytes);
|
|
buf.open(QIODevice::WriteOnly);
|
|
img.save(&buf, "PNG");
|
|
return bytes;
|
|
}
|
|
|
|
class WebImageProviderTest : public QObject {
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void cacheKeyFormat()
|
|
{
|
|
QString k1 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/photo.jpg"));
|
|
QVERIFY(k1.endsWith(QStringLiteral(".jpg")));
|
|
|
|
QString k2 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/vector.svg"));
|
|
QVERIFY(k2.endsWith(QStringLiteral(".svg")));
|
|
|
|
QString k3 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/noext"));
|
|
QVERIFY(!k3.contains(QLatin1Char('.')));
|
|
}
|
|
|
|
void fetchFromNetworkAndStore()
|
|
{
|
|
auto storage = std::make_shared<MockWebStorage>();
|
|
int fetchCount = 0;
|
|
const QByteArray sampleData = createSamplePng();
|
|
|
|
WebImageProvider::HttpFetcher fetcher = [&](const QString &url) -> QByteArray {
|
|
Q_UNUSED(url);
|
|
++fetchCount;
|
|
return sampleData;
|
|
};
|
|
|
|
WebImageProvider provider(storage, fetcher);
|
|
|
|
const QString url = QStringLiteral("https://example.com/test.png");
|
|
QImage img = provider.fetchImage(url);
|
|
QVERIFY(!img.isNull());
|
|
QCOMPARE(img.size(), QSize(100, 100));
|
|
QCOMPARE(fetchCount, 1);
|
|
QVERIFY(storage->contains(WebImageProvider::cacheKey(url)));
|
|
|
|
// Second fetch hits memory cache -> fetchCount remains 1
|
|
QImage img2 = provider.fetchImage(url);
|
|
QVERIFY(!img2.isNull());
|
|
QCOMPARE(fetchCount, 1);
|
|
}
|
|
|
|
void cacheHitFromStorageWithoutNetwork()
|
|
{
|
|
auto storage = std::make_shared<MockWebStorage>();
|
|
const QString url = QStringLiteral("https://example.com/cached.png");
|
|
const QString key = WebImageProvider::cacheKey(url);
|
|
storage->write(key, createSamplePng());
|
|
|
|
int fetchCount = 0;
|
|
WebImageProvider::HttpFetcher fetcher = [&](const QString &) -> QByteArray {
|
|
++fetchCount;
|
|
return {};
|
|
};
|
|
|
|
WebImageProvider provider(storage, fetcher);
|
|
|
|
QImage img = provider.fetchImage(url);
|
|
QVERIFY(!img.isNull());
|
|
QCOMPARE(fetchCount, 0); // never hit network!
|
|
}
|
|
|
|
void warmupReturnsNullImmediately()
|
|
{
|
|
int fetchCount = 0;
|
|
WebImageProvider::HttpFetcher fetcher = [&](const QString &) -> QByteArray {
|
|
++fetchCount;
|
|
return {};
|
|
};
|
|
WebImageProvider provider(std::make_shared<MockWebStorage>(), fetcher);
|
|
|
|
auto *resp = provider.requestImageResponse(QStringLiteral("__warmup__"), {});
|
|
QVERIFY(resp != nullptr);
|
|
QCOMPARE(fetchCount, 0);
|
|
delete resp;
|
|
}
|
|
};
|
|
|
|
QTEST_MAIN(WebImageProviderTest)
|
|
#include "WebImageProviderTest.moc"
|
|
|