Commit c996431c authored by Nicolas Fella's avatar Nicolas Fella
Browse files

Add autotest for thumbnails

Thumbnailing is complex code that isn't easy to manually test given the caching mechanism and variety in input data

Add a basic autotest that generates previews for a file and compares them to an expected file

Currently only for png and jpg images, but to be extended to other formats/scenarios
parent 13764bef
if (BUILD_TESTING)
add_subdirectory(autotests)
endif()
add_definitions(-DTRANSLATION_DOMAIN=\"kio5_thumbnail\")
remove_definitions(-DQT_NO_CAST_FROM_ASCII)
......
# allow coercing cstring to qstring, easier to write
remove_definitions(-DQT_NO_CAST_FROM_ASCII)
find_package(Qt${QT_MAJOR_VERSION}Test ${QT_MIN_VERSION} CONFIG REQUIRED)
include(ECMAddTests)
ecm_add_tests(
thumbnailtest.cpp
LINK_LIBRARIES
Qt::Test
KF5::KIOWidgets
)
/*
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
*/
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
#include <KIO/PreviewJob>
class ThumbnailTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testThumbnail_data()
{
QTest::addColumn<QString>("inputFile");
QTest::addColumn<QString>("expectedThumbnail");
QTest::addColumn<qreal>("dpr");
QTest::addRow("png") << "cherry_tree.png"
<< "cherry_tree_thumb.png" << 1.0;
QTest::addRow("png_dpr2") << "cherry_tree.png"
<< "cherry_tree_thumb@2.png" << 2.0;
QTest::addRow("jpg") << "boxes.jpg"
<< "boxes_thumb.png" << 1.0;
QTest::addRow("jpg_dpr2") << "boxes.jpg"
<< "boxes_thumb@2.png" << 2.0;
QTest::addRow("jpg_embedded_thumbnail") << "castle.jpg"
<< "castle_thumb.png" << 1.0;
}
void testThumbnail()
{
QFETCH(QString, inputFile);
QFETCH(QString, expectedThumbnail);
QFETCH(qreal, dpr);
QStandardPaths::setTestModeEnabled(true);
qputenv("KIOSLAVE_ENABLE_TESTMODE", "1"); // ensure the ioslaves call QStandardPaths::setTestModeEnabled too
// wipe thumbnail cache so we always start clean
QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation));
cacheDir.removeRecursively();
QString path = QFINDTESTDATA("data/" + inputFile);
KFileItemList items;
items.append(KFileItem(QUrl::fromLocalFile(path)));
QStringList enabledPlugins{"imagethumbnail", "jpegthumbnail"};
auto *job = KIO::filePreview(items, QSize(128, 128), &enabledPlugins);
job->setDevicePixelRatio(dpr);
connect(job, &KIO::PreviewJob::gotPreview, this, [path, expectedThumbnail, dpr](const KFileItem &item, const QPixmap &preview) {
QCOMPARE(item.url(), QUrl::fromLocalFile(path));
QImage expectedImage;
expectedImage.load(QFINDTESTDATA("data/" + expectedThumbnail));
expectedImage.setDevicePixelRatio(dpr);
QCOMPARE(preview.toImage(), expectedImage);
});
QSignalSpy failedSpy(job, &KIO::PreviewJob::failed);
QSignalSpy gotPreviewSpy(job, &KIO::PreviewJob::gotPreview);
QSignalSpy resultSpy(job, &KIO::PreviewJob::result);
gotPreviewSpy.wait();
resultSpy.wait();
QVERIFY(failedSpy.empty());
}
};
QTEST_MAIN(ThumbnailTest)
#include "thumbnailtest.moc"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment