Verified Commit 451a6a07 authored by Fushan Wen's avatar Fushan Wen 💬
Browse files

wallpapaers/image: Add autotests for ImageBackend

Use Qt Quick Test to test the backend can provide correct model images.
parent 4e834857
include(ECMMarkAsTest)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/..)
find_package(Qt${QT_MAJOR_VERSION} ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS QuickTest)
# ImageSizeFinder test
ecm_add_test(test_imagesizefinder.cpp TEST_NAME testimagesizefinder
LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static)
......@@ -32,3 +34,17 @@ ecm_add_test(test_slidemodel.cpp TEST_NAME testslidemodel
# SlideFilterModel test
ecm_add_test(test_slidefiltermodel.cpp TEST_NAME testslidefiltermodel
LINK_LIBRARIES Qt::Test plasma_wallpaper_imageplugin_static)
# ImageBackend test
add_executable(testimagebackend tst_imagebackend.cpp)
target_link_libraries(testimagebackend
Qt::Qml
Qt::QuickTest
Qt::Test
)
add_test(NAME testimagebackend
COMMAND testimagebackend
-import ${CMAKE_BINARY_DIR}/bin
-input tst_imagebackend.qml
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <QQmlContext>
#include <QQmlEngine>
#include <QtQuickTest>
#include <QtTest>
class TestSetup : public QObject
{
Q_OBJECT
public:
TestSetup();
public Q_SLOTS:
void qmlEngineAvailable(QQmlEngine *engine);
private:
QDir m_dataDir;
QString m_wallpaperPath;
QString m_packagePath;
};
TestSetup::TestSetup()
{
// For target size test
QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
m_dataDir = QDir(QFINDTESTDATA("testdata/default"));
m_wallpaperPath = m_dataDir.absoluteFilePath(QStringLiteral("wallpaper.jpg.jpg"));
m_packagePath = m_dataDir.absoluteFilePath(QStringLiteral("package/"));
}
void TestSetup::qmlEngineAvailable(QQmlEngine *engine)
{
engine->rootContext()->setContextProperty(QStringLiteral("testDirs"), QStringList{m_dataDir.absolutePath()});
engine->rootContext()->setContextProperty(QStringLiteral("testImage"), QUrl::fromLocalFile(m_wallpaperPath));
engine->rootContext()->setContextProperty(QStringLiteral("testPackage"), QUrl::fromLocalFile(m_packagePath));
}
QUICK_TEST_MAIN_WITH_SETUP(ImageBackend, TestSetup)
#include "tst_imagebackend.moc"
/*
SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
import QtQuick 2.5
import QtQuick.Window 2.15
import QtTest 1.0
import org.kde.plasma.wallpapers.image 2.0 as Wallpaper
TestCase {
id: root
width: 320
height: 240
when: windowShown
name: "ImageBackendTest"
Wallpaper.ImageBackend {
id: imageWallpaper
usedInConfig: false
//the oneliner of difference between image and slideshow wallpapers
renderingMode: Wallpaper.ImageBackend.SingleImage
targetSize: Qt.size(root.width, root.height)
}
SignalSpy {
id: modelImageChangedSignalSpy
target: imageWallpaper
signalName: "modelImageChanged"
}
Component {
id: mainImage
Image {
anchors.fill: parent
asynchronous: false
fillMode: Image.Stretch
sourceSize: imageWallpaper.targetSize
function wait() {
while (status === Image.Loading) {
statusSpy.wait(5000);
}
}
SignalSpy {
id: statusSpy
target: parent
signalName: "statusChanged"
}
}
}
Window {
id: window
width: root.width
height: root.height
visible: true
title: imageWallpaper.modelImage.toString()
}
function test_setSingleImage() {
imageWallpaper.renderingMode = Wallpaper.ImageBackend.SingleImage
verify(testImage.toString().length > 0);
imageWallpaper.image = testImage;
compare(imageWallpaper.modelImage, testImage);
const image = createTemporaryObject(mainImage, window, {source: imageWallpaper.modelImage});
compare(image.status, Image.Ready);
const grabbed = grabImage(image);
compare(grabbed.pixel(0, 0), Qt.rgba(0, 0, 0, 255));
}
function test_setPackageImage() {
imageWallpaper.renderingMode = Wallpaper.ImageBackend.SingleImage
verify(testPackage.toString().length > 0);
imageWallpaper.image = testPackage;
compare(imageWallpaper.modelImage.toString().indexOf("image://package/get?dir="), 0);
const image = createTemporaryObject(mainImage, window, {source: imageWallpaper.modelImage});
compare(image.status, Image.Loading);
image.wait();
compare(image.status, Image.Ready);
const grabbed1 = grabImage(image);
compare(grabbed1.pixel(0, 0), Qt.rgba(255, 255, 255, 255));
// Change target size
image.sourceSize = Qt.size(1920, 1080);
compare(image.status, Image.Loading);
image.wait();
compare(image.status, Image.Ready);
const grabbed2 = grabImage(image);
compare(grabbed2.pixel(0, 0), Qt.rgba(0, 0, 0, 255));
// Change backend's target size
modelImageChangedSignalSpy.clear();
imageWallpaper.targetSize = Qt.size(root.width + 1, root.height + 1);
compare(modelImageChangedSignalSpy.count, 1);
}
function test_startSlideShow() {
verify(testDirs[0].length > 0);
imageWallpaper.slidePaths = testDirs;
imageWallpaper.slideTimer = 1000; // use nextSlide
imageWallpaper.slideshowMode = Wallpaper.SortingMode.Alphabetical;
imageWallpaper.slideshowFoldersFirst = false;
imageWallpaper.renderingMode = Wallpaper.ImageBackend.SlideShow;
wait(1000); // &SlideModel::done
const image1 = imageWallpaper.modelImage;
imageWallpaper.nextSlide();
verify(image1 != imageWallpaper.modelImage);
imageWallpaper.nextSlide();
compare(image1, imageWallpaper.modelImage);
}
}
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