File indexing completed on 2024-12-08 12:21:10

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2017 Renato Araujo Oliveira Filho <renato.araujo@kdab.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #include <QFile>
0009 #include <QObject>
0010 #include <QStandardPaths>
0011 #include <QTemporaryDir>
0012 
0013 #include <KConfig>
0014 #include <KConfigGroup>
0015 #include <KProtocolInfo>
0016 #include <kfileplacesmodel.h>
0017 #include <kfileplacesview.h>
0018 
0019 #include <QSignalSpy>
0020 #include <QTest>
0021 
0022 static QString bookmarksFile()
0023 {
0024     return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel";
0025 }
0026 
0027 class KFilePlacesViewTest : public QObject
0028 {
0029     Q_OBJECT
0030 
0031 private Q_SLOTS:
0032     void initTestCase();
0033     void cleanupTestCase();
0034 
0035     void testUrlChanged_data();
0036     void testUrlChanged();
0037 
0038 private:
0039     QTemporaryDir m_tmpHome;
0040 };
0041 
0042 void KFilePlacesViewTest::initTestCase()
0043 {
0044     QVERIFY(m_tmpHome.isValid());
0045     qputenv("HOME", m_tmpHome.path().toUtf8());
0046     qputenv("KDE_FULL_SESSION", "1"); // attempt to enable recentlyused:/ if present, so we only need to test for isKnownProtocol below
0047     QStandardPaths::setTestModeEnabled(true);
0048 
0049     cleanupTestCase();
0050 
0051     KConfig config(QStringLiteral("baloofilerc"));
0052     KConfigGroup basicSettings = config.group("Basic Settings");
0053     basicSettings.writeEntry("Indexing-Enabled", true);
0054     config.sync();
0055 
0056     qRegisterMetaType<QModelIndex>();
0057 
0058     // Debug code, to help understanding the actual test
0059     KFilePlacesModel model;
0060     for (int row = 0; row < model.rowCount(); ++row) {
0061         const QModelIndex index = model.index(row, 0);
0062         qDebug() << model.url(index);
0063     }
0064 }
0065 
0066 void KFilePlacesViewTest::cleanupTestCase()
0067 {
0068     QFile::remove(bookmarksFile());
0069 }
0070 
0071 void KFilePlacesViewTest::testUrlChanged_data()
0072 {
0073     QTest::addColumn<int>("row");
0074     QTest::addColumn<QString>("expectedUrl");
0075 
0076     int idx = 3; // skip home, trash, remote
0077     if (KProtocolInfo::isKnownProtocol(QStringLiteral("recentlyused"))) {
0078         QTest::newRow("Recent Files") << idx++ << QStringLiteral("recentlyused:/files");
0079         QTest::newRow("Recent Locations") << idx++ << QStringLiteral("recentlyused:/locations");
0080     } else {
0081         QTest::newRow("Modified Today") << idx++ << QStringLiteral("timeline:/today");
0082         ++idx; // Modified Yesterday gets turned into "timeline:/2020-06/2020-06-05"
0083     }
0084 }
0085 
0086 void KFilePlacesViewTest::testUrlChanged()
0087 {
0088     QFETCH(int, row);
0089     QFETCH(QString, expectedUrl);
0090 
0091     KFilePlacesView pv;
0092     pv.setModel(new KFilePlacesModel());
0093 
0094     QSignalSpy urlChangedSpy(&pv, &KFilePlacesView::urlChanged);
0095     const QModelIndex targetIndex = pv.model()->index(row, 0);
0096     pv.scrollTo(targetIndex);
0097     Q_EMIT pv.clicked(targetIndex);
0098     QTRY_COMPARE(urlChangedSpy.count(), 1);
0099     const QList<QVariant> args = urlChangedSpy.takeFirst();
0100     QCOMPARE(args.at(0).toUrl().toString(), expectedUrl);
0101 }
0102 
0103 QTEST_MAIN(KFilePlacesViewTest)
0104 
0105 #include "kfileplacesviewtest.moc"