]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kfileitemmodeltest.cpp
Implement "Focus Places Panel"
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
index 38d2544aafa9434ad5a61955c154979a99dd0fc6..fad825dc018b48f7208ee9f7bc73b8b3f28fd2f7 100644 (file)
@@ -13,8 +13,7 @@
 #include <QTimer>
 
 #include <KDirLister>
-#include <kio/job.h>
-#include <kio_version.h>
+#include <KIO/SimpleJob>
 
 #include "kitemviews/kfileitemmodel.h"
 #include "testdir.h"
@@ -93,6 +92,7 @@ private Q_SLOTS:
     void testDeleteFileMoreThanOnce();
     void testInsertAfterExpand();
     void testCurrentDirRemoved();
+    void testSizeSortingAfterRefresh();
 
 private:
     QStringList itemsInModel() const;
@@ -847,6 +847,108 @@ void KFileItemModelTest::testRemoveFilteredExpandedItems()
                            << "file");
 }
 
+void KFileItemModelTest::testSizeSortingAfterRefresh()
+{
+    // testDir structure is as follows
+    // ./
+    // ├─ a
+    // ├─ b
+    // ├─ c/
+    // │  ├─ c-2/
+    // │  │  ├─ c-3
+    // │  ├─ c-1
+    // ├─ d
+    // ├─ e
+
+    QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+    QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved);
+    QVERIFY(itemsMovedSpy.isValid());
+
+    // Create some files with different sizes and modification times to check the different sorting options
+    QDateTime now = QDateTime::currentDateTime();
+
+    QSet<QByteArray> roles;
+    roles.insert("text");
+    roles.insert("isExpanded");
+    roles.insert("isExpandable");
+    roles.insert("expandedParentsCount");
+    m_model->setRoles(roles);
+
+    m_testDir->createDir("c/c-2");
+    m_testDir->createFile("c/c-2/c-3");
+    m_testDir->createFile("c/c-1");
+
+    m_testDir->createFile("a", "A file", now.addDays(-3));
+    m_testDir->createFile("b", "A larger file", now.addDays(0));
+    m_testDir->createDir("c", now.addDays(-2));
+    m_testDir->createFile("d", "The largest file in this directory", now.addDays(-1));
+    m_testDir->createFile("e", "An even larger file", now.addDays(-4));
+
+    m_model->loadDirectory(m_testDir->url());
+    QVERIFY(itemsInsertedSpy.wait());
+
+    int index = m_model->index(QUrl(m_testDir->url().url() + "/c"));
+    m_model->setExpanded(index, true);
+    QVERIFY(itemsInsertedSpy.wait());
+
+    index = m_model->index(QUrl(m_testDir->url().url() + "/c/c-2"));
+    m_model->setExpanded(index, true);
+    QVERIFY(itemsInsertedSpy.wait());
+
+    // Default: Sort by Name, ascending
+    QCOMPARE(m_model->sortRole(), QByteArray("text"));
+    QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(itemsInModel(),
+             QStringList() << "c"
+                           << "c-2"
+                           << "c-3"
+                           << "c-1"
+                           << "a"
+                           << "b"
+                           << "d"
+                           << "e");
+
+    // Sort by Size, ascending, before refresh
+    m_model->setSortRole("size");
+    QCOMPARE(m_model->sortRole(), QByteArray("size"));
+    QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(itemsInModel(),
+             QStringList() << "c"
+                           << "c-2"
+                           << "c-3"
+                           << "c-1"
+                           << "a"
+                           << "b"
+                           << "e"
+                           << "d");
+
+    // Refresh directory
+    m_model->refreshDirectory(m_model->directory());
+    QVERIFY(itemsInsertedSpy.wait());
+
+    // Expand folders again
+    index = m_model->index(QUrl(m_testDir->url().url() + "/c"));
+    m_model->setExpanded(index, true);
+    QVERIFY(itemsInsertedSpy.wait());
+
+    index = m_model->index(QUrl(m_testDir->url().url() + "/c/c-2"));
+    m_model->setExpanded(index, true);
+    QVERIFY(itemsInsertedSpy.wait());
+
+    // Sort by Size, ascending, after refresh
+    QCOMPARE(m_model->sortRole(), QByteArray("size"));
+    QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder);
+    QCOMPARE(itemsInModel(),
+             QStringList() << "c"
+                           << "c-2"
+                           << "c-3"
+                           << "c-1"
+                           << "a"
+                           << "b"
+                           << "e"
+                           << "d");
+}
+
 void KFileItemModelTest::testSorting()
 {
     // testDir structure is as follows
@@ -1654,10 +1756,10 @@ void KFileItemModelTest::collapseParentOfHiddenItems()
     // Set a name filter that matches nothing -> nothing should remain.
     m_model->setNameFilter("xyz");
     QCOMPARE(itemsRemovedSpy.count(), 1);
-    QCOMPARE(m_model->count(), 0); //Everything is hidden
+    QCOMPARE(m_model->count(), 0); // Everything is hidden
     QCOMPARE(itemsInModel(), QStringList());
 
-    //Filter by the file names. Folder "d" will be hidden since it was collapsed
+    // Filter by the file names. Folder "d" will be hidden since it was collapsed
     m_model->setNameFilter("1");
     QCOMPARE(itemsRemovedSpy.count(), 1); // nothing was removed, itemsRemovedSpy count will remain the same:
     QCOMPARE(m_model->count(), 6); // 6 items: "a/", "a/b/", "a/b/c", "a/b/c/1", "a/b/1", "a/1"
@@ -2479,9 +2581,6 @@ void KFileItemModelTest::testInsertAfterExpand()
 
     // Insert additional files into "a/b/"
     m_testDir->createFile("a/b/2");
-#if KIO_VERSION < QT_VERSION_CHECK(5, 92, 0)
-    QEXPECT_FAIL("", "Requires new API from frameworks", Abort);
-#endif
 
     QVERIFY(!itemsInsertedSpy.wait(5000));