diff --git a/kioslave/desktop/kio_desktop.cpp b/kioslave/desktop/kio_desktop.cpp index 06616d1ae..69d88b6aa 100644 --- a/kioslave/desktop/kio_desktop.cpp +++ b/kioslave/desktop/kio_desktop.cpp @@ -113,7 +113,15 @@ void DesktopProtocol::checkLocalInstall() bool DesktopProtocol::rewriteUrl(const QUrl &url, QUrl &newUrl) { newUrl.setScheme(QStringLiteral("file")); - newUrl.setPath(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) + url.path()); + QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); + if (desktopPath.endsWith('/')) { + desktopPath.chop(1); + } + QString filePath = desktopPath + url.path(); + if (filePath.endsWith('/')) { + filePath.chop(1); // ForwardingSlaveBase always appends a '/' + } + newUrl.setPath(filePath); return true; } diff --git a/kioslave/desktop/tests/CMakeLists.txt b/kioslave/desktop/tests/CMakeLists.txt index cb5f343de..1784f50ed 100644 --- a/kioslave/desktop/tests/CMakeLists.txt +++ b/kioslave/desktop/tests/CMakeLists.txt @@ -1,5 +1,5 @@ add_executable(testdesktop kio_desktop_test.cpp) -target_link_libraries(testdesktop KF5::KIOWidgets KF5::Solid Qt5::Test) +target_link_libraries(testdesktop KF5::KIOWidgets KF5::Solid Qt5::Test Qt5::DBus) ecm_mark_as_test(testdesktop) add_test(testdesktop testdesktop) diff --git a/kioslave/desktop/tests/kio_desktop_test.cpp b/kioslave/desktop/tests/kio_desktop_test.cpp index 0ea74d404..71ed2df42 100644 --- a/kioslave/desktop/tests/kio_desktop_test.cpp +++ b/kioslave/desktop/tests/kio_desktop_test.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -111,48 +112,64 @@ private Q_SLOTS: void testRename_data() { QTest::addColumn("withDirListerCache"); - QTest::addColumn("srcFile"); - QTest::addColumn("destFile"); - - const QString orig = "desktop:/" + m_testFileName; - const QString part = orig + ".part"; - QTest::newRow("from orig to .part") << false << orig << part; - QTest::newRow("from .part to orig") << false << part << orig; + QTest::addColumn("srcUrl"); + QTest::addColumn("destUrl"); + + const QString str = "desktop:/" + m_testFileName; + const QUrl orig(str); + const QUrl part(str + ".part"); + QTest::newRow("orig_to_part") << false << orig << part; + QTest::newRow("part_to_orig") << false << part << orig; // Warnings: all tests without dirlister cache should above this line // and all tests with it should be below - the cache stays forever once it exists. - QTest::newRow("from orig to .part, with cache") << true << orig << part; - QTest::newRow("from .part to orig, with cache (#218719)") << true << part << orig; + QTest::newRow("orig_to_part_with_cache") << true << orig << part; + QTest::newRow("part_to_orig_with_cache") << true << part << orig; // #218719 } void testRename() // relies on testCopyToDesktop being run before { QFETCH(bool, withDirListerCache); - QFETCH(QString, srcFile); - QFETCH(QString, destFile); + QFETCH(QUrl, srcUrl); + QFETCH(QUrl, destUrl); + QScopedPointer lister(nullptr); if (withDirListerCache) { - KDirLister lister; - lister.openUrl(QUrl(QStringLiteral("desktop:/"))); - QEventLoop eventLoop; - connect(&lister, static_cast(&KDirLister::completed), &eventLoop, &QEventLoop::quit); - eventLoop.exec(QEventLoop::ExcludeUserInputEvents); + lister.reset(new KDirLister); + lister->openUrl(QUrl(QStringLiteral("desktop:/"))); + QSignalSpy spyCompleted(lister.data(), static_cast(&KDirLister::completed)); + spyCompleted.wait(); } - const QUrl srcUrl = QUrl(srcFile); - const QUrl destUrl = QUrl(destFile); + org::kde::KDirNotify kdirnotify(QString(), QString(), QDBusConnection::sessionBus(), this); + QSignalSpy spyFilesAdded(&kdirnotify, &org::kde::KDirNotify::FilesAdded); + QSignalSpy spyFileRenamed(&kdirnotify, &org::kde::KDirNotify::FileRenamed); + QSignalSpy spyFileRenamedWithLocalPath(&kdirnotify, &org::kde::KDirNotify::FileRenamedWithLocalPath); const QString srcFilePath(m_desktopPath + srcUrl.path()); QVERIFY(QFile::exists(srcFilePath)); const QString destFilePath(m_desktopPath + destUrl.path()); QVERIFY(!QFile::exists(destFilePath)); - KIO::CopyJob* job = KIO::move(srcUrl, destUrl, KIO::HideProgressInfo); + KIO::Job* job = KIO::rename(srcUrl, destUrl, KIO::HideProgressInfo); job->setUiDelegate(0); QVERIFY(job); bool ok = job->exec(); QVERIFY(ok); QVERIFY(!QFile::exists(srcFilePath)); QVERIFY(QFile::exists(destFilePath)); + + // kio_desktop's rename() calls org::kde::KDirNotify::emitFileRenamedWithLocalPath + QTRY_COMPARE(spyFileRenamed.count(), 1); + QTRY_COMPARE(spyFileRenamedWithLocalPath.count(), 1); + // and then desktopnotifier notices something changed and emits KDirNotify::FilesAdded + QTRY_VERIFY(spyFilesAdded.count() >= 1); // can be 3, depending on kdirwatch's behaviour in desktopnotifier + + // check that KDirLister now has the correct item (#382341) + if (lister) { + const KFileItem fileItem = lister->findByUrl(destUrl); + QVERIFY(!fileItem.isNull()); + QCOMPARE(fileItem.targetUrl(), QUrl::fromLocalFile(destFilePath)); + } } private: