File indexing completed on 2024-05-19 05:38:45

0001 /*
0002     SPDX-FileCopyrightText: 2022 Fushan Wen <qydwhotmail@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <QQmlApplicationEngine>
0008 #include <QSignalSpy>
0009 #include <QTest>
0010 
0011 #include "abstracttasksmodel.h" // For enums
0012 #include "tasksmodel.h"
0013 
0014 using namespace TaskManager;
0015 
0016 class TasksModelTest : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 private Q_SLOTS:
0021     void initTestCase();
0022 
0023     /**
0024      * Test moving a launcher in the 'Task Manager' with "Keep launchers separate",
0025      * "Group by program name", "Launch in place"  and Manual sorting
0026      * @see https://bugs.kde.org/472524
0027      */
0028     void test_moveLauncherBug472524();
0029 
0030     /**
0031      * Task manager open entries jump around when pinned apps are moved
0032      * in the 'Task Manager' with "Keep launchers separate" option
0033      * unchecked
0034      * @see https://bugs.kde.org/444816
0035      */
0036     void test_moveBug444816();
0037 
0038     /**
0039      * Pinned apps with a preferred://[something] URI that resolves to nothing should be hidden
0040      *
0041      * @see https://bugs.kde.org/436667
0042      */
0043     void test_filterOutInvalidPreferredLaunchers();
0044 };
0045 
0046 void TasksModelTest::initTestCase()
0047 {
0048     QGuiApplication::setQuitOnLastWindowClosed(false);
0049 }
0050 
0051 void TasksModelTest::test_moveLauncherBug472524()
0052 {
0053     // Prepare two launchers and swap them
0054     TasksModel model;
0055 
0056     // Follow required settings in BUG 472524
0057     model.setSeparateLaunchers(true);
0058     model.setGroupMode(TasksModel::GroupApplications);
0059     model.setLaunchInPlace(true);
0060     model.setSortMode(TasksModel::SortManual);
0061 
0062     QSignalSpy rowInsertedSpy(&model, &TasksModel::rowsInserted);
0063 
0064     int rowCount = model.rowCount();
0065     QVERIFY(model.launcherList().empty());
0066     const QUrl launcherUrl = QUrl::fromLocalFile(QFINDTESTDATA("data/applications/GammaRay.desktop"));
0067     const QUrl launcherUrl2 = QUrl::fromLocalFile(QFINDTESTDATA("data/applications/org.kde.gwenview_importer.desktop"));
0068     model.setLauncherList(QStringList{launcherUrl.toString(), launcherUrl2.toString()});
0069 
0070     // Both launchers are added as expected
0071     QCOMPARE(model.launcherList().size(), 2);
0072     QCOMPARE(model.rowCount(), rowCount + 2);
0073 
0074     // The launchers are in the expected positions
0075     QCOMPARE(model.index(0, 0).data(Qt::DisplayRole).toString(), QStringLiteral("GammaRay"));
0076     QCOMPARE(model.index(1, 0).data(Qt::DisplayRole).toString(), QStringLiteral("Gwenview Importer"));
0077 
0078     // Swap the first and second launcher
0079     QVERIFY(model.move(0, 1));
0080     QCoreApplication::processEvents();
0081 
0082     // The launchers are swapped
0083     QCOMPARE(model.index(0, 0).data(Qt::DisplayRole).toString(), QStringLiteral("Gwenview Importer"));
0084     QCOMPARE(model.index(1, 0).data(Qt::DisplayRole).toString(), QStringLiteral("GammaRay"));
0085 }
0086 
0087 void TasksModelTest::test_moveBug444816()
0088 {
0089     // Prepare launchers and running tasks
0090     TasksModel model;
0091 
0092     // Follow required settings in BUG 444816
0093     model.setGroupMode(TasksModel::GroupDisabled);
0094     model.setSeparateLaunchers(false);
0095     model.setSortMode(TasksModel::SortManual);
0096 
0097     QSignalSpy rowInsertedSpy(&model, &TasksModel::rowsInserted);
0098 
0099     int rowCount = model.rowCount();
0100     QVERIFY(model.launcherList().empty());
0101     const QUrl launcherUrl = QUrl::fromLocalFile(QFINDTESTDATA("data/applications/GammaRay.desktop"));
0102     model.setLauncherList(QStringList{launcherUrl.toString()});
0103     // A launcher is added as expected
0104     QCOMPARE(model.launcherList().size(), 1);
0105     QCOMPARE(++rowCount, model.rowCount());
0106 
0107     // Create two new windows
0108     QVariantMap firstWindowProperties;
0109     firstWindowProperties.insert(QStringLiteral("title"), QStringLiteral("__testwindow__firstwindow__"));
0110     QVariantMap secondWindowProperties;
0111     secondWindowProperties.insert(QStringLiteral("title"), QStringLiteral("__testwindow__secondwindow__"));
0112     QVariantMap initialProperties;
0113     initialProperties.insert(QStringLiteral("windowInitialProperties"), QVariantList{firstWindowProperties, secondWindowProperties});
0114 
0115     QQmlApplicationEngine engine;
0116     engine.setInitialProperties(initialProperties);
0117     const QString qmlFileName = QFINDTESTDATA("data/windows/ManyWindows.qml");
0118     engine.load(qmlFileName);
0119 
0120     // Make sure two new windows have been created
0121     for (int i = 0; i < initialProperties[QStringLiteral("windowInitialProperties")].toList().size(); ++i) {
0122         rowInsertedSpy.wait(1000);
0123     }
0124     QCOMPARE(++ ++rowCount, model.rowCount());
0125 
0126     // TasksModel now looks like: [Launcher] [...] [__testwindow__firstwindow__] [__testwindow__secondwindow__]
0127     // This test tries to move [Launcher] to the position between the two tasks
0128     int launcherRow = -1;
0129     for (int i = 0; i < model.rowCount(); ++i) {
0130         if (model.index(i, 0).data(AbstractTasksModel::IsLauncher).toBool()) {
0131             launcherRow = i;
0132             break;
0133         }
0134     }
0135     QVERIFY(launcherRow >= 0);
0136 
0137     int firstWindowRow = -1;
0138     for (int i = 0; i < model.rowCount(); ++i) {
0139         if (model.index(i, 0).data(Qt::DisplayRole).toString() == firstWindowProperties[QStringLiteral("title")]) {
0140             firstWindowRow = i;
0141             break;
0142         }
0143     }
0144     QVERIFY(firstWindowRow >= 0);
0145 
0146     qDebug() << "********* BEGIN Before *********";
0147     for (int i = 0; i < model.rowCount(); ++i) {
0148         qDebug() << i << model.index(i, 0).data(Qt::DisplayRole).toString();
0149     }
0150     qDebug() << "********* End Before *********";
0151 
0152     QVERIFY(model.move(launcherRow, firstWindowRow));
0153     QCoreApplication::processEvents();
0154 
0155     // Verify the order
0156     for (int i = 0; i < model.rowCount(); ++i) {
0157         if (model.index(i, 0).data(AbstractTasksModel::IsLauncher).toBool()) {
0158             launcherRow = i;
0159             break;
0160         }
0161     }
0162     for (int i = 0; i < model.rowCount(); ++i) {
0163         if (model.index(i, 0).data(Qt::DisplayRole).toString() == firstWindowProperties[QStringLiteral("title")]) {
0164             firstWindowRow = i;
0165             break;
0166         }
0167     }
0168 
0169     qDebug() << "********* BEGIN After *********";
0170     for (int i = 0; i < model.rowCount(); ++i) {
0171         qDebug() << i << model.index(i, 0).data(Qt::DisplayRole).toString();
0172     }
0173     qDebug() << "********* END After *********";
0174 
0175     QCOMPARE(firstWindowRow + 1, launcherRow);
0176 }
0177 
0178 void TasksModelTest::test_filterOutInvalidPreferredLaunchers()
0179 {
0180     // Prepare launchers and running tasks
0181     TasksModel model;
0182 
0183     model.setLauncherList(QStringList{
0184         "preferred://nonexistent",
0185     });
0186     QCOMPARE(model.launcherList().size(), 0);
0187 
0188     const QUrl launcherUrl = QUrl::fromLocalFile(QFINDTESTDATA("data/applications/GammaRay.desktop"));
0189     model.setLauncherList(QStringList{
0190         "preferred://nonexistent",
0191         launcherUrl.toString(),
0192     });
0193     QCOMPARE(model.launcherList().size(), 1);
0194 }
0195 
0196 QTEST_MAIN(TasksModelTest)
0197 
0198 #include "tasksmodeltest.moc"