File indexing completed on 2024-12-08 12:17:03

0001 /*
0002     SPDX-FileCopyrightText: 2014 Montel Laurent <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 // test object
0008 #include "kdelibs4configmigrator.h"
0009 // Qt
0010 #include <QFile>
0011 #include <QObject>
0012 #include <QStandardPaths>
0013 #include <QTemporaryDir>
0014 #include <QTest>
0015 
0016 class Kdelibs4ConfigMigratorTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void initTestCase();
0022     void shouldNotMigrateIfKde4HomeDirDoesntExist();
0023     void shouldMigrateIfKde4HomeDirExist();
0024     void shouldMigrateConfigFiles();
0025     void shouldMigrateUiFiles();
0026 };
0027 
0028 void Kdelibs4ConfigMigratorTest::initTestCase()
0029 {
0030     QStandardPaths::setTestModeEnabled(true);
0031     const QString configHome = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
0032     QDir(configHome).removeRecursively();
0033     const QString dataHome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0034     QDir(dataHome).removeRecursively();
0035 }
0036 
0037 void Kdelibs4ConfigMigratorTest::shouldNotMigrateIfKde4HomeDirDoesntExist()
0038 {
0039     qputenv("KDEHOME", "");
0040     Kdelibs4ConfigMigrator migration(QLatin1String("foo"));
0041     QCOMPARE(migration.migrate(), false);
0042 }
0043 
0044 void Kdelibs4ConfigMigratorTest::shouldMigrateIfKde4HomeDirExist()
0045 {
0046     QTemporaryDir kdehomeDir;
0047     QVERIFY(kdehomeDir.isValid());
0048     const QString kdehome = kdehomeDir.path();
0049     qputenv("KDEHOME", QFile::encodeName(kdehome));
0050     Kdelibs4ConfigMigrator migration(QLatin1String("foo"));
0051     QCOMPARE(migration.migrate(), true);
0052 }
0053 
0054 void Kdelibs4ConfigMigratorTest::shouldMigrateConfigFiles()
0055 {
0056     QTemporaryDir kdehomeDir;
0057     const QString kdehome = kdehomeDir.path();
0058     qputenv("KDEHOME", QFile::encodeName(kdehome));
0059 
0060     // Generate kde4 config dir
0061     const QString configPath = kdehome + QLatin1Char('/') + QLatin1String("share/config/");
0062     QDir().mkpath(configPath);
0063     QVERIFY(QDir(configPath).exists());
0064 
0065     const QStringList listConfig = {QLatin1String("foorc"), QLatin1String("foo1rc")};
0066     for (const QString &config : listConfig) {
0067         QFile fooConfigFile(QLatin1String(KDELIBS4CONFIGMIGRATOR_DATA_DIR) + QLatin1Char('/') + config);
0068         QVERIFY(fooConfigFile.exists());
0069         const QString storedConfigFilePath = configPath + QLatin1Char('/') + config;
0070         QVERIFY(QFile::copy(fooConfigFile.fileName(), storedConfigFilePath));
0071         QCOMPARE(QStandardPaths::locate(QStandardPaths::GenericConfigLocation, config), QString());
0072     }
0073 
0074     Kdelibs4ConfigMigrator migration(QLatin1String("foo"));
0075     migration.setConfigFiles(listConfig);
0076     QVERIFY(migration.migrate());
0077 
0078     for (const QString &config : listConfig) {
0079         const QString migratedConfigFile = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, config);
0080         QVERIFY(!migratedConfigFile.isEmpty());
0081         QVERIFY(QFile(migratedConfigFile).exists());
0082         QFile::remove(migratedConfigFile);
0083     }
0084 }
0085 
0086 void Kdelibs4ConfigMigratorTest::shouldMigrateUiFiles()
0087 {
0088     QTemporaryDir kdehomeDir;
0089     const QString kdehome = kdehomeDir.path();
0090     qputenv("KDEHOME", QFile::encodeName(kdehome));
0091 
0092     const QString appName = QLatin1String("foo");
0093 
0094     // Generate kde4 data dir
0095     const QString dataPath = kdehome + QLatin1Char('/') + QLatin1String("share/apps/");
0096     QDir().mkpath(dataPath);
0097     QVERIFY(QDir(dataPath).exists());
0098 
0099     const QString xdgDatahome = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
0100 
0101     QStringList listUi;
0102     listUi << QLatin1String("appuirc") << QLatin1String("appui1rc");
0103     for (const QString &uifile : std::as_const(listUi)) {
0104         QFile fooConfigFile(QLatin1String(KDELIBS4CONFIGMIGRATOR_DATA_DIR) + QLatin1Char('/') + uifile);
0105         QVERIFY(fooConfigFile.exists());
0106         QDir().mkpath(dataPath + QLatin1Char('/') + appName);
0107         const QString storedConfigFilePath = dataPath + QLatin1Char('/') + appName + QLatin1Char('/') + uifile;
0108         QVERIFY(QFile::copy(fooConfigFile.fileName(), storedConfigFilePath));
0109 
0110         const QString xdgUiFile = xdgDatahome + QLatin1String("/kxmlgui5/") + appName + QLatin1Char('/') + uifile;
0111         QVERIFY(!QFile::exists(xdgUiFile));
0112     }
0113 
0114     Kdelibs4ConfigMigrator migration(appName);
0115     migration.setUiFiles(QStringList() << listUi);
0116     QVERIFY(migration.migrate());
0117 
0118     for (const QString &uifile : std::as_const(listUi)) {
0119         const QString xdgUiFile = xdgDatahome + QLatin1String("/kxmlgui5/") + appName + QLatin1Char('/') + uifile;
0120         QVERIFY(QFile(xdgUiFile).exists());
0121         QFile::remove(xdgUiFile);
0122     }
0123 }
0124 
0125 QTEST_MAIN(Kdelibs4ConfigMigratorTest)
0126 
0127 #include "kdelibs4configmigratortest.moc"