File indexing completed on 2024-05-19 16:37:08

0001 /*
0002     SPDX-FileCopyrightText: 2016 David Rosca <nowrep@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "configmodeltest.h"
0008 
0009 #include "plasma/applet.h"
0010 #include "plasma/package.h"
0011 #include "plasmaquick/configmodel.h"
0012 #include "plasmaquick/private/configcategory_p.cpp"
0013 
0014 #include <KConfigLoader>
0015 
0016 #include <QQmlComponent>
0017 #include <QQmlEngine>
0018 
0019 void ConfigModelTest::configSchemeFromPackage()
0020 {
0021     Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(QFINDTESTDATA("data/testconfigpackage"));
0022 
0023     QCOMPARE(applet->configScheme()->groupList(), QStringList() << QStringLiteral("General"));
0024     QCOMPARE(applet->configScheme()->findItemByName("testIntEntry")->property().toInt(), 23);
0025     QCOMPARE(applet->configScheme()->findItemByName("testStringEntry")->property().toString(), QStringLiteral("string-value"));
0026 
0027     qmlRegisterType<PlasmaQuick::ConfigModel>("org.kde.plasma.configuration", 2, 0, "ConfigModel");
0028     qmlRegisterType<PlasmaQuick::ConfigCategory>("org.kde.plasma.configuration", 2, 0, "ConfigCategory");
0029 
0030     QQmlEngine engine;
0031     QQmlComponent *component = new QQmlComponent(&engine, applet->kPackage().fileUrl("configmodel"), this);
0032     QObject *object = component->create(engine.rootContext());
0033     PlasmaQuick::ConfigModel *configModel = qobject_cast<PlasmaQuick::ConfigModel *>(object);
0034 
0035     QCOMPARE(configModel->rowCount(), 1);
0036     QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("name")).toString(), QStringLiteral("General"));
0037     QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("icon")).toString(), QStringLiteral("plasma"));
0038     QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("source")).toString(), QStringLiteral("ConfigGeneral.qml"));
0039     QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("pluginName")).toString(), QString());
0040     QVERIFY(!configModel->get(0).toMap().value(QStringLiteral("kcm")).value<void *>());
0041 
0042     delete component;
0043     delete applet;
0044 }
0045 
0046 void ConfigModelTest::emptySourceWithApplet()
0047 {
0048     Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(QFINDTESTDATA("data/testconfigpackage"));
0049     PlasmaQuick::ConfigModel model;
0050 
0051     model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), QString(), QString());
0052 
0053     QVERIFY(model.hasIndex(0, 0));
0054     QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QString());
0055     QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QString());
0056 
0057     model.setApplet(applet);
0058 
0059     QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QString());
0060     QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QString());
0061 
0062     delete applet;
0063 }
0064 
0065 void ConfigModelTest::notEmptySourceWithApplet()
0066 {
0067     const QString pkgPath = QFINDTESTDATA("data/testconfigpackage");
0068 
0069     {
0070         Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(pkgPath);
0071         PlasmaQuick::ConfigModel model;
0072 
0073         // Relative source
0074         model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), QStringLiteral("ConfigGeneral.qml"), QString());
0075 
0076         QVERIFY(model.hasIndex(0, 0));
0077         QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QStringLiteral("ConfigGeneral.qml"));
0078         QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QStringLiteral("ConfigGeneral.qml"));
0079 
0080         model.setApplet(applet);
0081 
0082         const QUrl fullPath = QUrl::fromLocalFile(pkgPath + QStringLiteral("/contents/ui/ConfigGeneral.qml"));
0083         QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toUrl(), fullPath);
0084         QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toUrl(), fullPath);
0085 
0086         delete applet;
0087     }
0088 
0089     {
0090         Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(pkgPath);
0091         PlasmaQuick::ConfigModel model;
0092 
0093         // Absolute source
0094         const QString source = QStringLiteral("/test/contents/ui/ConfigGeneral.qml");
0095         model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), source, QString());
0096 
0097         QVERIFY(model.hasIndex(0, 0));
0098         QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), source);
0099         QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), source);
0100 
0101         model.setApplet(applet);
0102 
0103         QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), source);
0104         QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), source);
0105 
0106         delete applet;
0107     }
0108 }
0109 
0110 QTEST_MAIN(ConfigModelTest)
0111 
0112 #include "moc_configmodeltest.cpp"