diff --git a/autotests/kfilewidgettest.cpp b/autotests/kfilewidgettest.cpp --- a/autotests/kfilewidgettest.cpp +++ b/autotests/kfilewidgettest.cpp @@ -211,6 +211,34 @@ QCOMPARE(fw.baseUrl().adjusted(QUrl::StripTrailingSlash), expectedBaseUrl); QCOMPARE(fw.locationEdit()->currentText(), expectedCurrentText); } + + void testCdUpToRoot_data() + { + QTest::addColumn("baseUrl"); + QTest::addColumn("expectedUrl"); + + // When going up from file:///home/, it should become file:/// not file:///home/user + QTest::newRow("file") << QUrl::fromLocalFile("/home/") << QUrl::fromLocalFile("/"); + QTest::newRow("trash") << QUrl("trash://home/") << QUrl("trash:/"); + QTest::newRow("sftp") << QUrl("sftp://127.0.0.1/home/") << QUrl("sftp://127.0.0.1/"); + } + + void testCdUpToRoot() + { + // GIVEN + QFETCH(QUrl, baseUrl); + QFETCH(QUrl, expectedUrl); + + KFileWidget fw(baseUrl); + fw.show(); + QVERIFY(QTest::qWaitForWindowActive(&fw)); + + // WHEN + fw.dirOperator()->cdUp(); + + // THEN + QCOMPARE(fw.dirOperator()->url(), expectedUrl); + } }; QTEST_MAIN(KFileWidgetTest) diff --git a/src/filewidgets/kfilewidget.cpp b/src/filewidgets/kfilewidget.cpp --- a/src/filewidgets/kfilewidget.cpp +++ b/src/filewidgets/kfilewidget.cpp @@ -1541,8 +1541,8 @@ // append '/' if needed: url combo does not add it // tokenize() expects it because it uses QUrl::adjusted(QUrl::RemoveFilename) QUrl u(url); - if (!u.path().isEmpty()) { - u.setPath(u.path() + '/'); + if (!u.path().isEmpty() && !u.path().endsWith(QLatin1Char('/'))) { + u.setPath(u.path() + QLatin1Char('/')); } q->setUrl(u);