Add QMLThemeConfigTest

Using Qt Quick Test with a .cpp part for setup and a .qml part for test code.
This commit is contained in:
Fabian Vogt 2023-02-02 20:44:41 +01:00
parent 14623cf5a8
commit fdaecbb21d
4 changed files with 115 additions and 2 deletions

View File

@ -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)

View File

@ -0,0 +1,39 @@
/***************************************************************************
* Copyright (c) 2023 Fabian Vogt <fabian@ritter-vogt.de>
*
* 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 <QQmlContext>
#include <QQmlEngine>
#include <QtQuickTest>
#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"

View File

@ -0,0 +1,67 @@
/***************************************************************************
* Copyright (c) 2023 Fabian Vogt <fabian@ritter-vogt.de>
*
* 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);
}
}

6
test/theme.conf Normal file
View File

@ -0,0 +1,6 @@
[General]
someTrueBool=yes
someFalseBool=false
someInteger=042
someRealNumber=01.5
someString="Pie/180"