File indexing completed on 2024-03-24 15:36:59

0001 /*
0002     SPDX-FileCopyrightText: 2007 Aaron Seigo <aseigo@kde.org>
0003     SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "querytest.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QCoreApplication>
0012 #include <QStandardPaths>
0013 
0014 #include "packageloader.h"
0015 #include "packagestructure.h"
0016 
0017 #include "config.h"
0018 
0019 void QueryTest::initTestCase()
0020 {
0021     QStandardPaths::setTestModeEnabled(true);
0022     // Remove any eventual installed package globally on the system
0023     qputenv("XDG_DATA_DIRS", "/not/valid");
0024     qputenv("KPACKAGE_DEP_RESOLVERS_PATH", KPACKAGE_DEP_RESOLVERS);
0025 
0026     m_dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0027     m_dataDir.removeRecursively();
0028 
0029     QVERIFY(m_dataDir.mkpath(QStringLiteral(".")));
0030 
0031     ps = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("KPackage/Generic"));
0032     ps.addFileDefinition("mainscript", QStringLiteral("ui/main.qml"), i18n("Main Script File"));
0033 }
0034 
0035 void QueryTest::cleanupTestCase()
0036 {
0037     m_dataDir.removeRecursively();
0038 }
0039 
0040 static bool checkedInstall(KPackage::Package &ps, const QString &source, int expectedError)
0041 {
0042     auto job = ps.install(source);
0043     job->exec();
0044     if (job->error() == expectedError) {
0045         return true;
0046     }
0047     qWarning() << "Unexpected error" << job->error() << "while installing" << source << job->errorString();
0048     return false;
0049 }
0050 
0051 void QueryTest::installAndQuery()
0052 {
0053     // verify that no packages are installed
0054     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).count(), 0);
0055 
0056     // install some packages
0057     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testpackage"), KJob::NoError));
0058     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testfallbackpackage"), KJob::NoError));
0059     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testjsonmetadatapackage"), KJob::NoError));
0060     system("find '/Users/alex/.qttest/Library/Application Support'");
0061     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).count(), 3);
0062 
0063     // installing package with invalid metadata should not be possible
0064     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testinvalidmetadata"), KPackage::Package::MetadataFileMissingError));
0065     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).count(), 3);
0066 
0067     // package with valid dep information should be installed
0068     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testpackagesdep"), KJob::NoError));
0069     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).count(), 4);
0070 
0071     // package with invalid dep information should not be installed
0072     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testpackagesdepinvalid"), KPackage::Package::JobError::PackageCopyError));
0073     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).count(), 4);
0074 }
0075 
0076 void QueryTest::queryCustomPlugin()
0077 {
0078     // verify that the test plugin if found
0079     auto packageStructure = KPackage::PackageLoader::self()->loadPackageStructure("Plasma/TestKPackageInternalPlasmoid");
0080     QVERIFY(packageStructure);
0081 
0082     // verify that no packages are installed
0083     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 0);
0084 
0085     auto testPackageStructure = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/TestKPackageInternalPlasmoid"));
0086     // install some packages
0087     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testpackage"), KJob::NoError));
0088     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testfallbackpackage"), KJob::NoError));
0089     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testjsonmetadatapackage"), KJob::NoError));
0090     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0091 
0092     // installing package with invalid metadata should not be possible
0093     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testinvalidmetadata"), KPackage::Package::MetadataFileMissingError));
0094     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0095 
0096     // package with valid dep information should be installed
0097     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testpackagesdep"), KJob::NoError));
0098     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 4);
0099 
0100     // package with invalid dep information should not be installed
0101     QVERIFY(checkedInstall(testPackageStructure, QFINDTESTDATA("data/testpackagesdepinvalid"), KPackage::Package::JobError::PackageCopyError));
0102 
0103     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 4);
0104 }
0105 
0106 void QueryTest::noServiceTypesPackage()
0107 {
0108 #if QT_VERSION_MAJOR >= 6
0109     QSKIP("desktop metadata files are not supported in Qt6");
0110 #endif
0111     const int oldSize = KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).size();
0112 
0113     QVERIFY(checkedInstall(ps, QFINDTESTDATA("data/testplasmathemepackagewithoutservicetypes"), KJob::NoError));
0114 
0115     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("KPackage/Generic")).size(), oldSize + 1);
0116 }
0117 
0118 QTEST_MAIN(QueryTest)
0119 
0120 #include "moc_querytest.cpp"