File indexing completed on 2024-12-01 13:01:14
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2015 Jarosław Staniek <staniek@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Lesser General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public 0015 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "PluginsTest.h" 0019 0020 #include <KReportPluginManager> 0021 #include <KReportPluginInterface> 0022 #include <KReportPluginMetaData> 0023 0024 #include <KAboutData> 0025 0026 #include <QTest> 0027 #include <QDebug> 0028 #include <QSet> 0029 #include <QList> 0030 0031 QTEST_MAIN(PluginsTest) 0032 0033 const struct Init { 0034 Init() 0035 { 0036 // Initialize before constructing QGuiApplication to avoid issue 0037 // "Qt WebEngine seems to be initialized from a plugin. Please set 0038 // Qt::AA_ShareOpenGLContexts using QCoreApplication::setAttribute before constructing 0039 // QGuiApplication." 0040 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts); 0041 } 0042 } initialize; 0043 0044 void PluginsTest::initTestCase() 0045 { 0046 //QCoreApplication::setLibraryPaths(QStringList() << QDir::currentPath()); 0047 } 0048 0049 void PluginsTest::listPlugins() 0050 { 0051 KReportPluginManager* manager = KReportPluginManager::self(); 0052 QStringList pluginIds = manager->pluginIds(); 0053 qDebug() << pluginIds; 0054 QCOMPARE(pluginIds.toSet().count(), pluginIds.count()); 0055 foreach(const QString &pluginId, pluginIds) { 0056 qDebug() << "Checking" << pluginId; 0057 QVERIFY2(!pluginId.isEmpty(), "Plugin id not empty"); 0058 KReportPluginInterface* iface = manager->plugin(pluginId); 0059 QVERIFY2(iface, "Plugin interface"); 0060 const KReportPluginMetaData *metaData = manager->pluginMetaData(pluginId); 0061 //! @todo info->priority() 0062 QVERIFY2(metaData, "Plugin metadata"); 0063 QCOMPARE(metaData, iface->metaData()); 0064 QVERIFY2(metaData->isValid(), "Plugin is valid"); 0065 QVERIFY(!metaData->id().isEmpty()); 0066 QCOMPARE(metaData->id(), pluginId); 0067 QVERIFY(!metaData->name().isEmpty()); 0068 QVERIFY(!metaData->description().isEmpty()); 0069 QVERIFY(!metaData->iconName().isEmpty()); 0070 QVERIFY(!metaData->authors().isEmpty()); 0071 QVERIFY(!metaData->version().isEmpty()); 0072 QVERIFY(!metaData->website().isEmpty()); 0073 QVERIFY(!metaData->license().isEmpty()); 0074 QVERIFY(!metaData->serviceTypes().isEmpty()); 0075 QCOMPARE(metaData->isStatic(), metaData->fileName().isEmpty()); 0076 } 0077 } 0078 0079 //! Compares @a actual and @a expected set, fails when not equal 0080 //! On failure, all elements are displayed. 0081 #define QCOMPARE_SET(type, actual, expected) \ 0082 do { \ 0083 if (actual == expected) { \ 0084 break; \ 0085 } \ 0086 QTest::qFail(#actual " != " #expected, __FILE__, __LINE__); \ 0087 QList<type> actualList = actual.toList(); \ 0088 QList<type> expectedList = expected.toList(); \ 0089 std::sort(actualList.begin(), actualList.end()); \ 0090 std::sort(expectedList.begin(), expectedList.end()); \ 0091 QList<type>::ConstIterator actualListIt = actualList.constBegin(); \ 0092 QList<type>::ConstIterator expectedListIt = expectedList.constBegin(); \ 0093 const int count = qMax(actualList.count(), expectedList.count()); \ 0094 int i = 0; \ 0095 bool stop; \ 0096 forever { \ 0097 stop = true; \ 0098 QByteArray sign = "!="; \ 0099 QString v1; \ 0100 if (actualListIt == actualList.constEnd()) { \ 0101 v1 = "<none>"; \ 0102 } \ 0103 else { \ 0104 v1 = QVariant(*actualListIt).toString(); \ 0105 stop = false; \ 0106 } \ 0107 QString v2; \ 0108 if (expectedListIt == expectedList.constEnd()) { \ 0109 v2 = "<none>"; \ 0110 } \ 0111 else { \ 0112 v2 = QVariant(*expectedListIt).toString(); \ 0113 stop = false; \ 0114 if (actualListIt != actualList.constEnd()) { \ 0115 if (*actualListIt == *expectedListIt) { \ 0116 sign = "=="; \ 0117 } \ 0118 } \ 0119 } \ 0120 if (stop) { \ 0121 break; \ 0122 } \ 0123 qDebug() << qPrintable(QString::fromLatin1("Actual/expected item %1 of %2: %3 %4 %5") \ 0124 .arg(i + 1).arg(count).arg(v1).arg(sign.constData()).arg(v2)); \ 0125 if (actualListIt != actualList.constEnd()) { \ 0126 ++actualListIt; \ 0127 } \ 0128 if (expectedListIt != expectedList.constEnd()) { \ 0129 ++expectedListIt; \ 0130 } \ 0131 ++i; \ 0132 } \ 0133 return; \ 0134 } while (0) 0135 0136 void PluginsTest::checkBuiltInPlugins() 0137 { 0138 KReportPluginManager* manager = KReportPluginManager::self(); 0139 QStringList pluginIds = manager->pluginIds(); 0140 QCOMPARE(pluginIds.toSet().count(), pluginIds.count()); 0141 QSet<QString> builtInPlugins( 0142 QSet<QString>() << "org.kde.kreport.checkbox" 0143 << "org.kde.kreport.field" 0144 << "org.kde.kreport.image" 0145 << "org.kde.kreport.label" 0146 << "org.kde.kreport.text"); 0147 QSet<QString> foundBuiltInPlugins; 0148 foreach(const QString &pluginId, pluginIds) { 0149 KReportPluginInterface* iface = manager->plugin(pluginId); 0150 QVERIFY(iface); 0151 if (iface->metaData()->isBuiltIn()) { 0152 foundBuiltInPlugins.insert(pluginId); 0153 } 0154 } 0155 QCOMPARE_SET(QString, builtInPlugins, foundBuiltInPlugins); 0156 }