File indexing completed on 2024-04-21 15:02:37

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 "rccpackagetest.h"
0009 
0010 #include <QDebug>
0011 #include <QStandardPaths>
0012 
0013 #include "packageloader.h"
0014 #include "packagestructure.h"
0015 #include "packagestructures/plasmoidstructure.h"
0016 
0017 void RccPackageTest::initTestCase()
0018 {
0019     QStandardPaths::setTestModeEnabled(true);
0020 
0021     QString destination = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/plasma/plasmoids/testpackage-rcc/");
0022     QDir dir;
0023     dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation));
0024     dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/plasma/"));
0025     dir.mkpath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/plasma/plasmoids/"));
0026     dir.mkpath(destination);
0027     QVERIFY(dir.exists(destination));
0028 
0029     // verify the source files exist
0030     QVERIFY(QFile::exists(QStringLiteral("../autotests/testpackage-rcc/metadata.json")));
0031     QVERIFY(QFile::exists(QStringLiteral("../autotests/testpackage-rcc/contents.rcc")));
0032 
0033     QFile::copy(QStringLiteral("../autotests/testpackage-rcc/metadata.json"), destination + QStringLiteral("metadata.json"));
0034     QFile::copy(QStringLiteral("../autotests/testpackage-rcc/contents.rcc"), destination + QStringLiteral("contents.rcc"));
0035 
0036     // verify they have been copied correctly
0037     QVERIFY(QFile::exists(destination + QStringLiteral("metadata.json")));
0038     QVERIFY(QFile::exists(destination + QStringLiteral("contents.rcc")));
0039 
0040     m_packagePath = "testpackage-rcc";
0041     m_pkg = new KPackage::Package(new KPackage::PlasmoidPackage);
0042     m_pkg->setPath(m_packagePath);
0043     // Two package instances with the same resource
0044     m_pkg2 = new KPackage::Package(new KPackage::PlasmoidPackage);
0045     m_pkg2->setPath(m_packagePath);
0046 }
0047 
0048 void RccPackageTest::cleanupTestCase()
0049 {
0050     qDebug() << "cleaning up";
0051     // Clean things up.
0052     QDir(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)).removeRecursively();
0053 }
0054 
0055 void RccPackageTest::accessFiles()
0056 {
0057     QVERIFY(m_pkg->hasValidStructure());
0058     QVERIFY(m_pkg->hasValidStructure());
0059 
0060     // check for existence of all files
0061     QVERIFY(!m_pkg->filePath("ui", QStringLiteral("main.qml")).isEmpty());
0062     QVERIFY(!m_pkg->filePath("ui", QStringLiteral("otherfile.qml")).isEmpty());
0063     QVERIFY(!m_pkg->filePath("images", QStringLiteral("empty.png")).isEmpty());
0064 
0065     QVERIFY(!m_pkg2->filePath("ui", QStringLiteral("main.qml")).isEmpty());
0066     QVERIFY(!m_pkg2->filePath("ui", QStringLiteral("otherfile.qml")).isEmpty());
0067     QVERIFY(!m_pkg2->filePath("images", QStringLiteral("empty.png")).isEmpty());
0068 }
0069 
0070 void RccPackageTest::validate()
0071 {
0072 #ifdef Q_OS_WIN
0073     QEXPECT_FAIL("", "Windows gets a different sha1 here, why?", Abort);
0074 #endif
0075     QCOMPARE(m_pkg->cryptographicHash(QCryptographicHash::Sha1), QByteArrayLiteral("ed0959c062b7ef7eaab5243e83e2f9afe5b3b15f"));
0076     QCOMPARE(m_pkg2->cryptographicHash(QCryptographicHash::Sha1), QByteArrayLiteral("ed0959c062b7ef7eaab5243e83e2f9afe5b3b15f"));
0077 }
0078 
0079 void RccPackageTest::testResourceRefCount()
0080 {
0081     delete m_pkg;
0082     m_pkg = nullptr;
0083 
0084     QVERIFY(m_pkg2->isValid());
0085     QVERIFY(QFile::exists(QStringLiteral(":/plasma/plasmoids/testpackage-rcc/contents/ui/main.qml")));
0086 
0087     // no reference from this package anymore
0088     delete m_pkg2;
0089     m_pkg2 = nullptr;
0090     QVERIFY(!QFile::exists(QStringLiteral(":/plasma/plasmoids/testpackage-rcc/contents/ui/main.qml")));
0091 }
0092 
0093 QTEST_MAIN(RccPackageTest)
0094 
0095 #include "moc_rccpackagetest.cpp"