File indexing completed on 2024-04-28 15:29:18

0001 /*
0002     SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <KParts/PartLoader>
0008 #include <KParts/ReadOnlyPart>
0009 #include <QTest>
0010 
0011 #include <KPluginMetaData>
0012 #include <QDebug>
0013 #include <QStandardPaths>
0014 
0015 class PartLoaderTest : public QObject
0016 {
0017     Q_OBJECT
0018 private Q_SLOTS:
0019     void initTestCase()
0020     {
0021         QStandardPaths::setTestModeEnabled(true);
0022         cleanupTestCase();
0023 
0024         const QString desktopFile = QFINDTESTDATA(QStringLiteral("notepad.desktop"));
0025         QVERIFY(!desktopFile.isEmpty());
0026         const QString destDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kservices5");
0027         QDir().mkpath(destDir);
0028         QVERIFY(QFile::copy(desktopFile, destDir + QLatin1String("/notepad.desktop")));
0029         // Ensure notepadpart is preferred over other installed parts.
0030         // This also tests the mimeapps.list parsing in PartLoader
0031         const QByteArray contents =
0032             "[Added KDE Service Associations]\n"
0033             "text/plain=notepad.desktop;\n";
0034         const QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
0035         QDir().mkpath(configDir);
0036         const QString mimeAppsPath = configDir + QLatin1String("/mimeapps.list");
0037         QFile mimeAppsFile(mimeAppsPath);
0038         QVERIFY(mimeAppsFile.open(QIODevice::WriteOnly));
0039         mimeAppsFile.write(contents);
0040     }
0041 
0042     void cleanupTestCase()
0043     {
0044         const QString destDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kservices5");
0045         QFile::remove(destDir + QLatin1String("/notepad.desktop"));
0046     }
0047 
0048     void shouldListParts()
0049     {
0050         // GIVEN
0051         const QString mimeType = QStringLiteral("text/plain");
0052 
0053         // WHEN
0054         const QVector<KPluginMetaData> plugins = KParts::PartLoader::partsForMimeType(mimeType);
0055 
0056         // THEN
0057         QVERIFY(!plugins.isEmpty());
0058         //        for (const KPluginMetaData &metaData : plugins) {
0059         //            qDebug() << metaData.fileName() << metaData.pluginId();
0060         //        }
0061         QCOMPARE(plugins.at(0).pluginId(), QStringLiteral("notepadpart")); // basename of plugin
0062         const QString fileName = plugins.at(0).fileName();
0063         QVERIFY2(fileName.contains(QLatin1String("notepadpart")), qPrintable(fileName));
0064     }
0065 
0066     void shouldLoadPlainTextPart()
0067     {
0068         // GIVEN
0069         const QString mimeType = QStringLiteral("text/plain");
0070         const QString testFile = QFINDTESTDATA("partloadertest.cpp");
0071         QVERIFY(!testFile.isEmpty());
0072         QWidget parentWidget;
0073         QString errorString;
0074 
0075         // WHEN
0076         KParts::ReadOnlyPart *part = KParts::PartLoader::createPartInstanceForMimeType<KParts::ReadOnlyPart>(mimeType, &parentWidget, this, &errorString);
0077 
0078         // THEN
0079         QVERIFY(part);
0080         QCOMPARE(errorString, QString());
0081         QCOMPARE(part->metaObject()->className(), "NotepadPart");
0082         QVERIFY(part->openUrl(QUrl::fromLocalFile(testFile)));
0083     }
0084 
0085     void shouldHandleNoPartError()
0086     {
0087         // GIVEN
0088         // can't use an unlikely mimetype here, okteta is associated with application/octet-stream :-)
0089         const QString mimeType = QStringLiteral("does/not/exist");
0090         QWidget parentWidget;
0091 
0092         // WHEN
0093         const KPluginFactory::Result result = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, &parentWidget, this, {});
0094         KParts::ReadOnlyPart *part = result.plugin;
0095         QString errorString = result.errorString;
0096 
0097         // THEN
0098         QVERIFY2(!part, part ? part->metaObject()->className() : nullptr);
0099         QCOMPARE(errorString, QStringLiteral("No part was found for mimeType does/not/exist"));
0100     }
0101 
0102     void shouldInstantiatePart()
0103     {
0104         // GIVEN
0105         const KPluginMetaData md(QStringLiteral("kf" QT_STRINGIFY(QT_VERSION_MAJOR) "/parts/notepadpart"));
0106         QVERIFY(md.isValid());
0107 
0108         QWidget parentWidget;
0109 
0110         // WHEN
0111         const KPluginFactory::Result result = KParts::PartLoader::instantiatePart<KParts::ReadOnlyPart>(md, &parentWidget, this, {});
0112 
0113         // THEN
0114         QVERIFY(result);
0115         QVERIFY(result.plugin);
0116         QCOMPARE(result.plugin->metaObject()->className(), "NotepadPart");
0117     }
0118 };
0119 
0120 QTEST_MAIN(PartLoaderTest)
0121 
0122 #include "partloadertest.moc"