diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 747e8e095..216769ae8 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -28,7 +28,8 @@ MACRO(PLASMA_UNIT_TESTS) KF5::I18n KF5::KIOCore KF5::Service - KF5::IconThemes) + KF5::IconThemes + KF5::Declarative) if(QT_QTOPENGL_FOUND) target_link_libraries(${_testname} Qt5::OpenGL) endif() @@ -46,6 +47,7 @@ PLASMA_UNIT_TESTS( framesvgtest iconitemtest themetest + configmodeltest # plasmoidpackagetest ) diff --git a/autotests/configmodeltest.cpp b/autotests/configmodeltest.cpp new file mode 100644 index 000000000..04c9d2de1 --- /dev/null +++ b/autotests/configmodeltest.cpp @@ -0,0 +1,124 @@ +/****************************************************************************** +* Copyright 2016 David Rosca * +* * +* This library is free software; you can redistribute it and/or * +* modify it under the terms of the GNU Library General Public * +* License as published by the Free Software Foundation; either * +* version 2 of the License, or (at your option) any later version. * +* * +* This library 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 * +* Library General Public License for more details. * +* * +* You should have received a copy of the GNU Library General Public License * +* along with this library; see the file COPYING.LIB. If not, write to * +* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * +* Boston, MA 02110-1301, USA. * +*******************************************************************************/ + +#include "configmodeltest.h" + +#include "plasma/applet.h" +#include "plasma/package.h" +#include "plasmaquick/configmodel.h" +#include "plasmaquick/private/configcategory_p.cpp" + +#include + +#include +#include + +void ConfigModelTest::configSchemeFromPackage() +{ + Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(QFINDTESTDATA("data/testconfigpackage")); + + QCOMPARE(applet->configScheme()->groupList(), QStringList() << QStringLiteral("General")); + QCOMPARE(applet->configScheme()->findItemByName("testIntEntry")->property().toInt(), 23); + QCOMPARE(applet->configScheme()->findItemByName("testStringEntry")->property().toString(), QStringLiteral("string-value")); + + qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigModel"); + qmlRegisterType("org.kde.plasma.configuration", 2, 0, "ConfigCategory"); + + QQmlEngine engine; + QQmlComponent *component = new QQmlComponent(&engine, QUrl::fromLocalFile(applet->package().filePath("configmodel")), this); + QObject *object = component->create(engine.rootContext()); + PlasmaQuick::ConfigModel *configModel = qobject_cast(object); + + QCOMPARE(configModel->rowCount(), 1); + QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("name")).toString(), QStringLiteral("General")); + QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("icon")).toString(), QStringLiteral("plasma")); + QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("source")).toString(), QStringLiteral("ConfigGeneral.qml")); + QCOMPARE(configModel->get(0).toMap().value(QStringLiteral("pluginName")).toString(), QString()); + QVERIFY(!configModel->get(0).toMap().value(QStringLiteral("kcm")).value()); + + delete component; + delete applet; +} + +void ConfigModelTest::emptySourceWithApplet() +{ + Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(QFINDTESTDATA("data/testconfigpackage")); + PlasmaQuick::ConfigModel model; + + model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), QString(), QString()); + + QVERIFY(model.hasIndex(0, 0)); + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QString()); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QString()); + + model.setApplet(applet); + + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QString()); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QString()); + + delete applet; +} + +void ConfigModelTest::notEmptySourceWithApplet() +{ + const QString pkgPath = QFINDTESTDATA("data/testconfigpackage"); + + { + Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(pkgPath); + PlasmaQuick::ConfigModel model; + + // Relative source + model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), QStringLiteral("ConfigGeneral.qml"), QString()); + + QVERIFY(model.hasIndex(0, 0)); + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), QStringLiteral("ConfigGeneral.qml")); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), QStringLiteral("ConfigGeneral.qml")); + + model.setApplet(applet); + + const QUrl fullPath = QUrl::fromLocalFile(pkgPath + QStringLiteral("/contents/ui/ConfigGeneral.qml")); + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toUrl(), fullPath); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toUrl(), fullPath); + + delete applet; + } + + { + Plasma::Applet *applet = Plasma::Applet::loadPlasmoid(pkgPath); + PlasmaQuick::ConfigModel model; + + // Absolute source + const QString source = QStringLiteral("/test/contents/ui/ConfigGeneral.qml"); + model.appendCategory(QStringLiteral("plasma"), QStringLiteral("name"), source, QString()); + + QVERIFY(model.hasIndex(0, 0)); + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), source); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), source); + + model.setApplet(applet); + + QCOMPARE(model.data(model.index(0, 0), PlasmaQuick::ConfigModel::SourceRole).toString(), source); + QCOMPARE(model.get(0).toMap().value(QStringLiteral("source")).toString(), source); + + delete applet; + } +} + +QTEST_MAIN(ConfigModelTest) + diff --git a/autotests/configmodeltest.h b/autotests/configmodeltest.h new file mode 100644 index 000000000..87cd81c0a --- /dev/null +++ b/autotests/configmodeltest.h @@ -0,0 +1,39 @@ +/****************************************************************************** +* Copyright 2016 David Rosca * +* * +* This library is free software; you can redistribute it and/or * +* modify it under the terms of the GNU Library General Public * +* License as published by the Free Software Foundation; either * +* version 2 of the License, or (at your option) any later version. * +* * +* This library 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 * +* Library General Public License for more details. * +* * +* You should have received a copy of the GNU Library General Public License * +* along with this library; see the file COPYING.LIB. If not, write to * +* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * +* Boston, MA 02110-1301, USA. * +*******************************************************************************/ + +#pragma once + +#include + +namespace Plasma +{ +class Applet; +} + +class ConfigModelTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void configSchemeFromPackage(); + void emptySourceWithApplet(); + void notEmptySourceWithApplet(); + +}; + diff --git a/autotests/data/testconfigpackage/contents/config/config.qml b/autotests/data/testconfigpackage/contents/config/config.qml new file mode 100644 index 000000000..fe6ab8bd4 --- /dev/null +++ b/autotests/data/testconfigpackage/contents/config/config.qml @@ -0,0 +1,10 @@ +import QtQuick 2.0 +import org.kde.plasma.configuration 2.0 + +ConfigModel { + ConfigCategory { + name: "General" + icon: "plasma" + source: "ConfigGeneral.qml" + } +} diff --git a/autotests/data/testconfigpackage/contents/config/main.xml b/autotests/data/testconfigpackage/contents/config/main.xml new file mode 100644 index 000000000..7b4cb2fbc --- /dev/null +++ b/autotests/data/testconfigpackage/contents/config/main.xml @@ -0,0 +1,17 @@ + + + + + + + 23 + + + string-value + + + + diff --git a/autotests/data/testconfigpackage/contents/ui/ConfigGeneral.qml b/autotests/data/testconfigpackage/contents/ui/ConfigGeneral.qml new file mode 100644 index 000000000..cde850a9d --- /dev/null +++ b/autotests/data/testconfigpackage/contents/ui/ConfigGeneral.qml @@ -0,0 +1,7 @@ +import QtQuick 2.0 + +Rectangle { + id: root + color: "darkblue" +} + diff --git a/autotests/data/testconfigpackage/contents/ui/main.qml b/autotests/data/testconfigpackage/contents/ui/main.qml new file mode 100644 index 000000000..cde850a9d --- /dev/null +++ b/autotests/data/testconfigpackage/contents/ui/main.qml @@ -0,0 +1,7 @@ +import QtQuick 2.0 + +Rectangle { + id: root + color: "darkblue" +} + diff --git a/autotests/data/testconfigpackage/metadata.desktop b/autotests/data/testconfigpackage/metadata.desktop new file mode 100644 index 000000000..45dfeddda --- /dev/null +++ b/autotests/data/testconfigpackage/metadata.desktop @@ -0,0 +1,16 @@ +[Desktop Entry] +Encoding=UTF-8 +Keywords= +Name=Config Test Package +Type=Service + +X-KDE-ParentApp= +X-KDE-PluginInfo-Author=Joe Blow +X-KDE-PluginInfo-Category= +X-KDE-PluginInfo-Email=jblow@kde.org +X-KDE-PluginInfo-License=GPLv2+ +X-KDE-PluginInfo-Name=org.kde.configtestpackage +X-KDE-PluginInfo-Version= +X-KDE-PluginInfo-Website= +X-Plasma-MainScript=ui/main.qml +X-Plasma-API=declarativeappletscript diff --git a/src/plasmaquick/configmodel.cpp b/src/plasmaquick/configmodel.cpp index 3f69073f8..975854ad3 100644 --- a/src/plasmaquick/configmodel.cpp +++ b/src/plasmaquick/configmodel.cpp @@ -149,11 +149,7 @@ QVariant ConfigModelPrivate::get(int row) const value[QStringLiteral("name")] = categories.at(row)->name(); value[QStringLiteral("icon")] = categories.at(row)->icon(); value[QStringLiteral("pluginName")] = categories.at(row)->pluginName(); - if (appletInterface) { - value[QStringLiteral("source")] = QUrl::fromLocalFile(appletInterface.data()->package().filePath("ui", categories.at(row)->source())); - } else { - value[QStringLiteral("source")] = categories.at(row)->source(); - } + value[QStringLiteral("source")] = q->data(q->index(row, 0), ConfigModel::SourceRole); value[QStringLiteral("visible")] = categories.at(row)->visible(); value[QStringLiteral("kcm")] = q->data(q->index(row, 0), ConfigModel::KCMRole); @@ -202,7 +198,7 @@ QVariant ConfigModel::data(const QModelIndex &index, int role) const { const QString source = d->categories.at(index.row())->source(); // Quick check if source is an absolute path or not - if (d->appletInterface && !(source.startsWith('/') && source.endsWith(QLatin1String("qml")))) { + if (d->appletInterface && !source.isEmpty() && !(source.startsWith('/') && source.endsWith(QLatin1String("qml")))) { return QUrl::fromLocalFile(d->appletInterface.data()->package().filePath("ui", source)); } else { return source;