File indexing completed on 2024-05-12 07:51:57

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:
0019     const QString m_plainTextMimetype = QStringLiteral("text/plain");
0020 private Q_SLOTS:
0021     void initTestCase()
0022     {
0023         QStandardPaths::setTestModeEnabled(true);
0024 
0025         // Ensure notepadpart is preferred over other installed parts.
0026         // This also tests the mimeapps.list parsing in PartLoader
0027         const QByteArray contents =
0028             "[Added KDE Service Associations]\n"
0029             "text/plain=notepad.desktop;\n";
0030         const QString configDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
0031         QDir().mkpath(configDir);
0032         const QString mimeAppsPath = configDir + QLatin1String("/mimeapps.list");
0033         QFile mimeAppsFile(mimeAppsPath);
0034         QVERIFY(mimeAppsFile.open(QIODevice::WriteOnly));
0035         mimeAppsFile.write(contents);
0036     }
0037 
0038     void shouldListParts()
0039     {
0040         const QList<KPluginMetaData> plugins = KParts::PartLoader::partsForMimeType(m_plainTextMimetype);
0041 
0042         QVERIFY(!plugins.isEmpty());
0043         QCOMPARE(plugins.at(0).pluginId(), QStringLiteral("notepadpart")); // basename of plugin
0044         const QString fileName = plugins.at(0).fileName();
0045         QVERIFY2(fileName.contains(QLatin1String("notepadpart")), qPrintable(fileName));
0046     }
0047 
0048     void shouldLoadPlainTextPart()
0049     {
0050         const QString testFile = QFINDTESTDATA("partloadertest.cpp");
0051         QVERIFY(!testFile.isEmpty());
0052         QWidget parentWidget;
0053 
0054         auto res = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(m_plainTextMimetype, &parentWidget, this);
0055 
0056         QVERIFY(res);
0057         QCOMPARE(res.errorString, QString());
0058         QCOMPARE(res.plugin->metaObject()->className(), "NotepadPart");
0059         QVERIFY(res.plugin->openUrl(QUrl::fromLocalFile(testFile)));
0060     }
0061 
0062     void shouldHandleNoPartError()
0063     {
0064         // can't use an unlikely mimetype here, okteta is associated with application/octet-stream :-)
0065         const QString mimeType = QStringLiteral("does/not/exist");
0066         QWidget parentWidget;
0067 
0068         const KPluginFactory::Result result = KParts::PartLoader::instantiatePartForMimeType<KParts::ReadOnlyPart>(mimeType, &parentWidget, this);
0069 
0070         QVERIFY2(!result, result ? result.plugin->metaObject()->className() : nullptr);
0071         QCOMPARE(result.errorString, QStringLiteral("No part was found for mimeType does/not/exist"));
0072     }
0073 
0074     void shouldInstantiatePart()
0075     {
0076         const KPluginMetaData md(QStringLiteral("kf6/parts/notepadpart"));
0077         QVERIFY(md.isValid());
0078 
0079         QWidget parentWidget;
0080         const KPluginFactory::Result result = KParts::PartLoader::instantiatePart<KParts::ReadOnlyPart>(md, &parentWidget, this);
0081 
0082         QVERIFY(result);
0083         QCOMPARE(result.plugin->metaObject()->className(), "NotepadPart");
0084     }
0085 };
0086 
0087 QTEST_MAIN(PartLoaderTest)
0088 
0089 #include "partloadertest.moc"