FolderListModel: Do not return invalid URLs like "file:" when navigating up.
authorFriedemann Kleint <Friedemann.Kleint@digia.com>
Fri, 26 Jul 2013 12:40:09 +0000 (14:40 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 29 Jul 2013 11:49:48 +0000 (13:49 +0200)
Task-number: QTBUG-32139

Change-Id: I715b97eb85bc4235de6a2bb696131efae56477fd
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
src/imports/folderlistmodel/qquickfolderlistmodel.cpp
tests/auto/qml/qquickfolderlistmodel/tst_qquickfolderlistmodel.cpp

index ca07392..2f61a9a 100644 (file)
@@ -483,16 +483,12 @@ QUrl QQuickFolderListModel::parentFolder() const
     QString localFile = d->currentDir.toLocalFile();
     if (!localFile.isEmpty()) {
         QDir dir(localFile);
-#if defined(Q_OS_WIN)
-        if (dir.isRoot())
-            dir.setPath("");
-        else
-#endif
-            dir.cdUp();
+        if (dir.isRoot() || !dir.cdUp())
+            return QUrl();
         localFile = dir.path();
     } else {
-        int pos = d->currentDir.path().lastIndexOf(QLatin1Char('/'));
-        if (pos == -1)
+        const int pos = d->currentDir.path().lastIndexOf(QLatin1Char('/'));
+        if (pos <= 0)
             return QUrl();
         localFile = d->currentDir.path().left(pos);
     }
index 9230608..ef24e0a 100644 (file)
@@ -73,6 +73,7 @@ private slots:
     void basicProperties();
     void resetFiltering();
     void refresh();
+    void cdUp();
 #if defined (Q_OS_WIN) && !defined (Q_OS_WINCE)
     // WinCE does not have drive concept, so lets execute this test only on desktop Windows.
     void changeDrive();
@@ -182,6 +183,33 @@ void tst_qquickfolderlistmodel::refresh()
     QTRY_COMPARE(removeEnd, count-1); // wait for refresh
 }
 
+void tst_qquickfolderlistmodel::cdUp()
+{
+    enum { maxIterations = 50 };
+    QQmlComponent component(&engine, testFileUrl("basic.qml"));
+    checkNoErrors(component);
+
+    QAbstractListModel *flm = qobject_cast<QAbstractListModel*>(component.create());
+    QVERIFY(flm != 0);
+    const QUrl startFolder = flm->property("folder").toUrl();
+    QVERIFY(startFolder.isValid());
+
+    // QTBUG-32139: Ensure navigating upwards terminates cleanly and does not
+    // return invalid Urls like "file:".
+    for (int i = 0; ; ++i) {
+        const QVariant folderV = flm->property("parentFolder");
+        const QUrl folder = folderV.toUrl();
+        if (!folder.isValid())
+            break;
+        QVERIFY(folder.toString() != QLatin1String("file:"));
+        QVERIFY2(i < maxIterations,
+                 qPrintable(QString::fromLatin1("Unable to reach root after %1 iterations starting from %2, stuck at %3")
+                            .arg(maxIterations).arg(QDir::toNativeSeparators(startFolder.toLocalFile()),
+                                                    QDir::toNativeSeparators(folder.toLocalFile()))));
+        flm->setProperty("folder", folderV);
+    }
+}
+
 #if defined (Q_OS_WIN) && !defined (Q_OS_WINCE)
 void tst_qquickfolderlistmodel::changeDrive()
 {