File indexing completed on 2024-04-28 15:11:57

0001 #include <QTemporaryFile>
0002 #include "testkspaths.h"
0003 
0004 #include "../testhelpers.h"
0005 
0006 TestKSPaths::TestKSPaths(QObject * parent): QObject(parent)
0007 {
0008 
0009 }
0010 
0011 void TestKSPaths::initTestCase()
0012 {
0013 }
0014 
0015 void TestKSPaths::cleanupTestCase()
0016 {
0017 }
0018 
0019 void TestKSPaths::init()
0020 {
0021     KTEST_BEGIN();
0022 }
0023 
0024 void TestKSPaths::cleanup()
0025 {
0026     KTEST_END();
0027 }
0028 
0029 void TestKSPaths::testStandardPaths_data()
0030 {
0031 #if QT_VERSION < 0x050900
0032     QSKIP("Skipping fixture-based test on old QT version.");
0033 #else
0034     QTest::addColumn<QStandardPaths::StandardLocation>("TYPE");
0035 
0036     QTest::addColumn<bool>("ISALTERED");
0037     //bool const isAltered = true;
0038     bool const isStandard = false;
0039 
0040     QTest::addColumn<bool>("ISWRITABLE");
0041     bool const isWritable = true;
0042     //bool const isNotWritable = false;
0043 
0044     // This fixture verifies that generic locations get the application name appended (which is unexpected)
0045     QTest::addRow("DesktopLocation")        << QStandardPaths::DesktopLocation          << isStandard   << isWritable;
0046     QTest::addRow("DocumentsLocation")      << QStandardPaths::DocumentsLocation        << isStandard   << isWritable;
0047     QTest::addRow("FontsLocation")          << QStandardPaths::FontsLocation            << isStandard   << isWritable;
0048     QTest::addRow("ApplicationsLocation")   << QStandardPaths::ApplicationsLocation     << isStandard   << isWritable;
0049     QTest::addRow("MusicLocation")          << QStandardPaths::MusicLocation            << isStandard   << isWritable;
0050     QTest::addRow("MoviesLocation")         << QStandardPaths::MoviesLocation           << isStandard   << isWritable;
0051     QTest::addRow("PicturesLocation")       << QStandardPaths::PicturesLocation         << isStandard   << isWritable;
0052     QTest::addRow("TempLocation")           << QStandardPaths::TempLocation             << isStandard   << isWritable;
0053     QTest::addRow("HomeLocation")           << QStandardPaths::HomeLocation             << isStandard   << isWritable;
0054     QTest::addRow("DataLocation")           << QStandardPaths::DataLocation             << isStandard   << isWritable;
0055     QTest::addRow("CacheLocation")          << QStandardPaths::CacheLocation            << isStandard   << isWritable;
0056     QTest::addRow("GenericCacheLocation")   << QStandardPaths::GenericCacheLocation     << isStandard   << isWritable;
0057     QTest::addRow("GenericDataLocation")    << QStandardPaths::GenericDataLocation      << isStandard   << isWritable;
0058     // JM 2021.07.08 Both FreeBSD and OpenSUSE return RunTimeLocation with trailing slash.
0059     // /tmp/runtime-kdeci//test_file.XXXXXX
0060     // Do not know the cause, so disabling this test for now. Maybe test environment specific issue?
0061 #if 0
0062     QTest::addRow("RuntimeLocation")        << QStandardPaths::RuntimeLocation          << isStandard   << isWritable;
0063 #endif
0064     QTest::addRow("ConfigLocation")         << QStandardPaths::ConfigLocation           << isStandard   << isWritable;
0065     QTest::addRow("DownloadLocation")       << QStandardPaths::DownloadLocation         << isStandard   << isWritable;
0066     QTest::addRow("GenericConfigLocation")  << QStandardPaths::GenericConfigLocation    << isStandard   << isWritable;
0067     QTest::addRow("AppDataLocation")        << QStandardPaths::AppDataLocation          << isStandard   << isWritable;
0068     QTest::addRow("AppLocalDataLocation")   << QStandardPaths::AppLocalDataLocation     << isStandard   << isWritable;
0069     QTest::addRow("AppConfigLocation")      << QStandardPaths::AppConfigLocation        << isStandard   << isWritable;
0070 #endif
0071 }
0072 
0073 void TestKSPaths::testStandardPaths()
0074 {
0075 #if QT_VERSION < 0x050900
0076     QSKIP("Skipping fixture-based test on old QT version.");
0077 #else
0078     QFETCH(QStandardPaths::StandardLocation, TYPE);
0079     QFETCH(bool, ISALTERED);
0080     QFETCH(bool, ISWRITABLE);
0081 
0082     QString const tested_writable_dir = KSPaths::writableLocation(TYPE);
0083 
0084     if (ISALTERED)
0085         QEXPECT_FAIL("", qPrintable(QString("Location associated with QStandardPaths type %1 is altered by KSPaths.").arg(TYPE)),
0086                      Continue);
0087 
0088     QCOMPARE(tested_writable_dir, QStandardPaths::writableLocation(TYPE));
0089 
0090     if (ISALTERED)
0091         QCOMPARE(tested_writable_dir, QDir(QStandardPaths::writableLocation(TYPE)).path() + QDir::separator());
0092 
0093     if (ISWRITABLE)
0094         QVERIFY2(QDir(tested_writable_dir).mkpath("."),
0095                  qPrintable(QString("Directory '%1' must be writable.").arg(KSPaths::writableLocation(TYPE))));
0096 
0097     QTemporaryFile test_file(tested_writable_dir + "/test_file.XXXXXX");
0098 
0099     if (ISALTERED)
0100         QEXPECT_FAIL("", "Path built with KSPaths::writableLocation contains no duplicate separator", Continue);
0101     QVERIFY2(!test_file.fileTemplate().contains(QString("%1%1").arg(QDir::separator())),
0102              qPrintable(QString("%1 contains duplicate separator").arg(test_file.fileTemplate())));
0103 
0104     if (ISWRITABLE)
0105     {
0106         QDir const path_to_test_file = QFileInfo(test_file.fileName()).dir();
0107         QVERIFY2(path_to_test_file.mkpath("."), qPrintable(QString("Dir '%1' must be writable.").arg(path_to_test_file.path())));
0108         QVERIFY2(test_file.open(), qPrintable(QString("File '%1' must be writable.").arg(test_file.fileTemplate())));
0109     }
0110 
0111     if (test_file.isOpen())
0112     {
0113         QVERIFY(test_file.write(qPrintable(QApplication::applicationName())));
0114         test_file.close();
0115 
0116         QString recovered_test_file_name = QDir::cleanPath(KSPaths::locate(TYPE, QFileInfo(test_file.fileName()).fileName()));
0117         QCOMPARE(qPrintable(test_file.fileName()), qPrintable(recovered_test_file_name));
0118 
0119         QVERIFY(test_file.remove());
0120     }
0121 #endif
0122 }
0123 
0124 void TestKSPaths::testKStarsInstallation_data()
0125 {
0126 #if QT_VERSION < 0x050900
0127     QSKIP("Skipping fixture-based test on old QT version.");
0128 #else
0129     QTest::addColumn<QStandardPaths::StandardLocation>("TYPE");
0130     QTest::addColumn<QString>("FILE");
0131 
0132     typedef QPair <QStandardPaths::StandardLocation, QString> KPair;
0133     QList <KPair> fixtures;
0134 
0135     fixtures << KPair(QStandardPaths::AppLocalDataLocation, "TZrules.dat");
0136     fixtures << KPair(QStandardPaths::AppLocalDataLocation, "themes/whitebalance.colors");
0137     fixtures << KPair(QStandardPaths::AppLocalDataLocation, "textures/moon00.png");
0138     //fixtures << KPair(QStandardPaths::GenericDataLocation, "skycomponent.sqlite");
0139     fixtures << KPair(QStandardPaths::GenericDataLocation, "sounds/KDE-KStars-Alert.ogg");
0140     fixtures << KPair(QStandardPaths::AppLocalDataLocation, "kstars.png");
0141 
0142     for (auto pair : fixtures)
0143         QTest::addRow("%s", qPrintable(pair.second)) << pair.first << pair.second;
0144 #endif
0145 }
0146 
0147 void TestKSPaths::testKStarsInstallation()
0148 {
0149 #if QT_VERSION < 0x050900
0150     QSKIP("Skipping fixture-based test on old QT version.");
0151 #else
0152     // We can only verify the installation path of 'kstars', there is no install for the test
0153     // We don't execute kstars, so no user configuration is available
0154 
0155     QFETCH(QStandardPaths::StandardLocation, TYPE);
0156     QFETCH(QString, FILE);
0157 
0158     QVERIFY(!KSPaths::locate(TYPE, FILE).isEmpty());
0159     QVERIFY(!KSPaths::locateAll(TYPE, FILE).isEmpty());
0160 #endif
0161 }
0162 
0163 QTEST_GUILESS_MAIN(TestKSPaths)