File indexing completed on 2024-04-21 03:56:31

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 "packagejob.h"
0015 #include "packageloader.h"
0016 #include "packagestructure.h"
0017 
0018 #include "config.h"
0019 
0020 void QueryTest::initTestCase()
0021 {
0022     QStandardPaths::setTestModeEnabled(true);
0023     // Remove any eventual installed package globally on the system
0024     qputenv("XDG_DATA_DIRS", "/not/valid");
0025     qputenv("KPACKAGE_DEP_RESOLVERS_PATH", KPACKAGE_DEP_RESOLVERS);
0026 
0027     m_dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0028 
0029     // verify that the test plugin if found
0030     QVERIFY(KPackage::PackageLoader::self()->loadPackageStructure("Plasma/TestKPackageInternalPlasmoid"));
0031 }
0032 
0033 static bool checkedInstall(const QString &packageFormat, const QString &source, int expectedError)
0034 {
0035     auto job = KPackage::PackageJob::install(packageFormat, source, {});
0036     QEventLoop l;
0037     QObject::connect(job, &KJob::result, &l, [&l]() {
0038         l.quit();
0039     });
0040     l.exec();
0041     if (job->error() == expectedError) {
0042         return true;
0043     }
0044     qWarning() << "Unexpected error" << job->error() << "while installing" << source << job->errorString();
0045     return false;
0046 }
0047 
0048 void QueryTest::installAndQuery()
0049 {
0050     m_dataDir.removeRecursively();
0051     // verify that no packages are installed
0052     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 0);
0053 
0054     // install some packages
0055     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackage"), KJob::NoError));
0056     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testfallbackpackage"), KJob::NoError));
0057     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 2);
0058 
0059     // installing package with invalid metadata should not be possible
0060     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testinvalidmetadata"), KPackage::PackageJob::PluginIdInvalidError));
0061     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 2);
0062 
0063     // package with valid dep information should be installed
0064     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackagesdep"), KJob::NoError));
0065     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0066 
0067     // package with invalid dep information should not be installed
0068     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackagesdepinvalid"), KPackage::PackageJob::JobError::PackageCopyError));
0069     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0070 }
0071 
0072 void QueryTest::queryCustomPlugin()
0073 {
0074     m_dataDir.removeRecursively();
0075 
0076     // verify that no packages are installed
0077     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 0);
0078 
0079     auto testPackageStructure = KPackage::PackageLoader::self()->loadPackage(QStringLiteral("Plasma/TestKPackageInternalPlasmoid"));
0080     // install some packages
0081     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackage"), KJob::NoError));
0082     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testfallbackpackage"), KJob::NoError));
0083     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 2);
0084 
0085     // installing package with invalid metadata should not be possible
0086     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testinvalidmetadata"), KPackage::PackageJob::JobError::PluginIdInvalidError));
0087     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 2);
0088 
0089     // package with valid dep information should be installed
0090     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackagesdep"), KJob::NoError));
0091     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0092 
0093     // package with invalid dep information should not be installed
0094     QVERIFY(checkedInstall(packageFormat, QFINDTESTDATA("data/testpackagesdepinvalid"), KPackage::PackageJob::JobError::PackageCopyError));
0095 
0096     QCOMPARE(KPackage::PackageLoader::self()->listPackages(QStringLiteral("Plasma/TestKPackageInternalPlasmoid")).count(), 3);
0097 }
0098 
0099 QTEST_MAIN(QueryTest)
0100 
0101 #include "moc_querytest.cpp"