diff --git a/autotests/kfileplacesviewtest.cpp b/autotests/kfileplacesviewtest.cpp index d95b66a3..7078e8b3 100644 --- a/autotests/kfileplacesviewtest.cpp +++ b/autotests/kfileplacesviewtest.cpp @@ -1,116 +1,117 @@ /* This file is part of the KDE project Copyright (C) 2017 Renato Araujo Oliveira Filho This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include #include #include #include #include #include static QString bookmarksFile() { return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/user-places.xbel"; } class KFilePlacesViewTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void cleanupTestCase(); void testUrlChanged_data(); void testUrlChanged(); private: QTemporaryDir m_tmpHome; }; void KFilePlacesViewTest::initTestCase() { QVERIFY(m_tmpHome.isValid()); qputenv("HOME", m_tmpHome.path().toUtf8()); qputenv("KDE_FORK_SLAVES", "yes"); // to avoid a runtime dependency on klauncher QStandardPaths::setTestModeEnabled(true); cleanupTestCase(); KConfig config(QStringLiteral("baloofilerc")); KConfigGroup basicSettings = config.group("Basic Settings"); basicSettings.writeEntry("Indexing-Enabled", true); config.sync(); qRegisterMetaType(); } void KFilePlacesViewTest::cleanupTestCase() { QFile::remove(bookmarksFile()); } void KFilePlacesViewTest::testUrlChanged_data() { QTest::addColumn("row"); QTest::addColumn("expectedUrl"); const QDate currentDate = QDate::currentDate(); const QDate yesterdayDate = currentDate.addDays(-1); QTest::newRow("Today") << 4 << QStringLiteral("timeline:/today"); QTest::newRow("Yesterday") << 5 << QString("timeline:/%1-%2/%1-%2-%3") .arg(yesterdayDate.year()) .arg(yesterdayDate.month(), 2, 10, QChar('0')) .arg(yesterdayDate.day(), 2, 10, QChar('0')); // search QTest::newRow("Documents") << 6 << QStringLiteral("baloosearch:/documents"); QTest::newRow("Images") << 7 << QStringLiteral("baloosearch:/images"); QTest::newRow("Audio Files") << 8 << QStringLiteral("baloosearch:/audio"); QTest::newRow("Videos") << 9 << QStringLiteral("baloosearch:/videos"); } void KFilePlacesViewTest::testUrlChanged() { QFETCH(int, row); QFETCH(QString, expectedUrl); KFilePlacesView pv; pv.show(); + pv.activateWindow(); pv.setModel(new KFilePlacesModel()); QVERIFY(QTest::qWaitForWindowActive(&pv)); QSignalSpy urlChangedSpy(&pv, &KFilePlacesView::urlChanged); const QModelIndex targetIndex = pv.model()->index(row, 0); pv.scrollTo(targetIndex); pv.clicked(targetIndex); QTRY_COMPARE(urlChangedSpy.count(), 1); const QList args = urlChangedSpy.takeFirst(); QCOMPARE(args.at(0).toUrl().toString(), expectedUrl); } QTEST_MAIN(KFilePlacesViewTest) #include "kfileplacesviewtest.moc" diff --git a/autotests/kfilewidgettest.cpp b/autotests/kfilewidgettest.cpp index 5225bae7..18555f7e 100644 --- a/autotests/kfilewidgettest.cpp +++ b/autotests/kfilewidgettest.cpp @@ -1,246 +1,252 @@ /* This file is part of the KIO framework tests Copyright (c) 2016 Albert Astals Cid This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kfilewidget.h" #include #include #include #include #include #include #include /** * Unit test for KFileWidget */ class KFileWidgetTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase() { // To avoid a runtime dependency on klauncher qputenv("KDE_FORK_SLAVES", "yes"); QStandardPaths::setTestModeEnabled(true); QVERIFY(QDir::homePath() != QDir::tempPath()); } void cleanupTestCase() { } QWidget *findLocationLabel(QWidget *parent) { const QList labels = parent->findChildren(); foreach(QLabel *label, labels) { if (label->text() == i18n("&Name:")) return label->buddy(); } Q_ASSERT(false); return nullptr; } void testFocusOnLocationEdit() { KFileWidget fw(QUrl::fromLocalFile(QDir::homePath())); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); QVERIFY(findLocationLabel(&fw)->hasFocus()); } void testFocusOnLocationEditChangeDir() { KFileWidget fw(QUrl::fromLocalFile(QDir::homePath())); fw.setUrl(QUrl::fromLocalFile(QDir::tempPath())); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); QVERIFY(findLocationLabel(&fw)->hasFocus()); } void testFocusOnLocationEditChangeDir2() { KFileWidget fw(QUrl::fromLocalFile(QDir::homePath())); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); fw.setUrl(QUrl::fromLocalFile(QDir::tempPath())); QVERIFY(findLocationLabel(&fw)->hasFocus()); } void testFocusOnDirOps() { KFileWidget fw(QUrl::fromLocalFile(QDir::homePath())); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); const QList nav = fw.findChildren(); QCOMPARE(nav.count(), 1); nav[0]->setFocus(); fw.setUrl(QUrl::fromLocalFile(QDir::tempPath())); const QList ops = fw.findChildren(); QCOMPARE(ops.count(), 1); QVERIFY(ops[0]->hasFocus()); } void testGetStartUrl() { QString recentDirClass; QString outFileName; QUrl localUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///attachmentDir")), recentDirClass, outFileName); QCOMPARE(recentDirClass, QStringLiteral(":attachmentDir")); QCOMPARE(localUrl.path(), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)); QVERIFY(outFileName.isEmpty()); localUrl = KFileWidget::getStartUrl(QUrl(QStringLiteral("kfiledialog:///attachments/foo.txt?global")), recentDirClass, outFileName); QCOMPARE(recentDirClass, QStringLiteral("::attachments")); QCOMPARE(localUrl.path(), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation)); QCOMPARE(outFileName, QStringLiteral("foo.txt")); } void testSetSelection_data() { QTest::addColumn("baseDir"); QTest::addColumn("selection"); QTest::addColumn("expectedBaseDir"); QTest::addColumn("expectedCurrentText"); const QString baseDir = QDir::homePath(); // A nice filename to detect URL encoding issues const QString fileName = QStringLiteral("some:fi#le"); // Bug 369216, kdialog calls setSelection(path) QTest::newRow("path") << baseDir << baseDir + QLatin1Char('/') + fileName << baseDir << fileName; QTest::newRow("differentPath") << QDir::rootPath() << baseDir + QLatin1Char('/') + fileName << baseDir << fileName; // kdeplatformfiledialoghelper.cpp calls setSelection(URL as string) QTest::newRow("url") << baseDir << QUrl::fromLocalFile(baseDir + QLatin1Char('/') + fileName).toString() << baseDir << fileName; // What if someone calls setSelection(fileName)? That breaks, hence e70f8134a2b in plasma-integration.git QTest::newRow("filename") << baseDir << fileName << baseDir << fileName; } void testSetSelection() { // GIVEN QFETCH(QString, baseDir); QFETCH(QString, selection); QFETCH(QString, expectedBaseDir); QFETCH(QString, expectedCurrentText); const QUrl baseUrl = QUrl::fromLocalFile(baseDir).adjusted(QUrl::StripTrailingSlash); const QUrl expectedBaseUrl = QUrl::fromLocalFile(expectedBaseDir); KFileWidget fw(baseUrl); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); // WHEN fw.setSelection(selection); // now deprecated, this test shows why ;) // THEN QCOMPARE(fw.baseUrl().adjusted(QUrl::StripTrailingSlash), expectedBaseUrl); //if (QByteArray(QTest::currentDataTag()) == "filename") { QEXPECT_FAIL("filename", "setSelection cannot work with filenames, bad API", Continue); //} QCOMPARE(fw.locationEdit()->currentText(), expectedCurrentText); } void testSetSelectedUrl_data() { QTest::addColumn("baseDir"); QTest::addColumn("selectionUrl"); QTest::addColumn("expectedBaseDir"); QTest::addColumn("expectedCurrentText"); const QString baseDir = QDir::homePath(); // A nice filename to detect URL encoding issues const QString fileName = QStringLiteral("some:fi#le"); const QUrl fileUrl = QUrl::fromLocalFile(baseDir + QLatin1Char('/') + fileName); QTest::newRow("path") << baseDir << fileUrl << baseDir << fileName; QTest::newRow("differentPath") << QDir::rootPath() << fileUrl << baseDir << fileName; QTest::newRow("url") << baseDir << QUrl::fromLocalFile(baseDir + QLatin1Char('/') + fileName) << baseDir << fileName; QUrl relativeUrl; relativeUrl.setPath(fileName); QTest::newRow("filename") << baseDir << relativeUrl << baseDir << fileName; } void testSetSelectedUrl() { // GIVEN QFETCH(QString, baseDir); QFETCH(QUrl, selectionUrl); QFETCH(QString, expectedBaseDir); QFETCH(QString, expectedCurrentText); const QUrl baseUrl = QUrl::fromLocalFile(baseDir).adjusted(QUrl::StripTrailingSlash); const QUrl expectedBaseUrl = QUrl::fromLocalFile(expectedBaseDir); KFileWidget fw(baseUrl); fw.show(); + fw.activateWindow(); QVERIFY(QTest::qWaitForWindowActive(&fw)); // WHEN fw.setSelectedUrl(selectionUrl); // THEN QCOMPARE(fw.baseUrl().adjusted(QUrl::StripTrailingSlash), expectedBaseUrl); QCOMPARE(fw.locationEdit()->currentText(), expectedCurrentText); } void testEnterUrl_data() { QTest::addColumn("expectedUrl"); // Check if the root urls are well transformed into themself, otherwise // when going up from file:///home/ it will become file:///home/user QTest::newRow("file") << QUrl::fromLocalFile("/"); QTest::newRow("trash") << QUrl("trash:/"); QTest::newRow("sftp") << QUrl("sftp://127.0.0.1/"); } void testEnterUrl() { // GIVEN QFETCH(QUrl, expectedUrl); // WHEN // These lines are copied from src/filewidgets/kfilewidget.cpp // void KFileWidgetPrivate::_k_enterUrl(const QUrl &url) QUrl u(expectedUrl); if (!u.path().isEmpty() && !u.path().endsWith(QLatin1Char('/'))) { u.setPath(u.path() + QLatin1Char('/')); } // THEN QVERIFY(u.isValid()); QCOMPARE(u, expectedUrl); } }; QTEST_MAIN(KFileWidgetTest) #include "kfilewidgettest.moc"