diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3ea60b3..c52dddb 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,5 +10,6 @@ target_link_libraries(ConfigurationTest Qt5::Core Qt5::Test) set(QMLThemeConfigTest_SRCS QMLThemeConfigTest.cpp ../src/common/ThemeConfig.cpp ../src/common/ThemeConfig.h) add_executable(QMLThemeConfigTest ${QMLThemeConfigTest_SRCS}) -add_test(NAME QMLThemeConfig COMMAND QMLThemeConfigTest) -target_link_libraries(QMLThemeConfigTest Qt5::QuickTest) +target_include_directories(QMLThemeConfigTest PRIVATE ../src/common/) +add_test(NAME QMLThemeConfig COMMAND QMLThemeConfigTest -platform offscreen -input ${CMAKE_CURRENT_SOURCE_DIR}/QMLThemeConfigTest.qml WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(QMLThemeConfigTest PRIVATE Qt5::Quick Qt5::QuickTest) diff --git a/test/QMLThemeConfigTest.cpp b/test/QMLThemeConfigTest.cpp new file mode 100644 index 0000000..800f14f --- /dev/null +++ b/test/QMLThemeConfigTest.cpp @@ -0,0 +1,39 @@ +/*************************************************************************** +* Copyright (c) 2023 Fabian Vogt +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the +* Free Software Foundation, Inc., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +***************************************************************************/ + +#include +#include +#include + +#include "ThemeConfig.h" + +class Setup : public QObject +{ + Q_OBJECT +public slots: + void qmlEngineAvailable(QQmlEngine *engine) + { + auto *config = new SDDM::ThemeConfig(QStringLiteral("theme.conf"), this); + engine->rootContext()->setContextProperty(QStringLiteral("config"), config); + } +}; + +QUICK_TEST_MAIN_WITH_SETUP(QMLThemeConfigTest, Setup) + +#include "QMLThemeConfigTest.moc" diff --git a/test/QMLThemeConfigTest.qml b/test/QMLThemeConfigTest.qml new file mode 100644 index 0000000..a658fad --- /dev/null +++ b/test/QMLThemeConfigTest.qml @@ -0,0 +1,67 @@ +/*************************************************************************** +* Copyright (c) 2023 Fabian Vogt +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the +* Free Software Foundation, Inc., +* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +***************************************************************************/ + +import QtQuick 2.3 +import QtTest 1.0 + +TestCase { + name: "QMLThemeConfigTest" + + function test_keys() { + let keys = Object.keys(config); + compare(keys.indexOf("doesnotexist"), -1); + verify(keys.indexOf("someInteger") >= 0); + keys = config.keys(); + compare(keys.indexOf("doesnotexist"), -1); + verify(keys.indexOf("someInteger") >= 0); + } + + // Everything is a string + function test_propertyAPI() { + compare(config.doesnotexist, undefined); + compare(config.someTrueBool, "yes"); + compare(!!config.someTrueBool, true); + compare(config.someFalseBool, "false"); + // "false" as a string is truthy! + compare(!!config.someFalseBool, true); + compare(config.someInteger, "042"); + compare(+config.someInteger, 42); + compare(config.someRealNumber, "01.5"); + compare(+config.someRealNumber, 1.5); + compare(config.someString, "Pie/180"); + } + + // Strings are converted to specific types + function test_typedAPI() { + compare(config.stringValue("doesnotexist"), ""); + compare(config.boolValue("someTrueBool"), true); + compare(config.boolValue("someFalseBool"), false); + // "false" as a string is truthy! + compare(!!config.someFalseBool, true); + compare(config.stringValue("someInteger"), "042"); + compare(config.intValue("someInteger"), 42); + compare(config.realValue("someRealNumber"), 1.5); + // conversion fails -> 0 + compare(config.intValue("someRealNumber"), 0); + compare(config.stringValue("someString"), "Pie/180"); + // conversion fails -> 0 + compare(config.intValue("someString"), 0); + compare(config.realValue("someString"), 0); + } +} diff --git a/test/theme.conf b/test/theme.conf new file mode 100644 index 0000000..cb74cf0 --- /dev/null +++ b/test/theme.conf @@ -0,0 +1,6 @@ +[General] +someTrueBool=yes +someFalseBool=false +someInteger=042 +someRealNumber=01.5 +someString="Pie/180"