X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/1826f905d706925456763394de17294bcb6d1c35..6e752f507a1dd82a40d4bd140457203842fc0c80:/src/tests/kfileitemmodeltest.cpp diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp index 16189a0fc..ea05f840b 100644 --- a/src/tests/kfileitemmodeltest.cpp +++ b/src/tests/kfileitemmodeltest.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include "kitemviews/kfileitemmodel.h" #include "testdir.h" @@ -92,6 +92,7 @@ private Q_SLOTS: void testDeleteFileMoreThanOnce(); void testInsertAfterExpand(); void testCurrentDirRemoved(); + void testSizeSortingAfterRefresh(); private: QStringList itemsInModel() const; @@ -163,7 +164,7 @@ void KFileItemModelTest::testDefaultSortRole() void KFileItemModelTest::testDefaultGroupedSorting() { - QCOMPARE(m_model->groupedSorting(), false); + QCOMPARE(m_model->groupedSorting(), true); } void KFileItemModelTest::testNewItems() @@ -846,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 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 @@ -1653,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" @@ -1904,6 +2007,7 @@ void KFileItemModelTest::testNameRoleGroups() m_testDir->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"}); m_model->setGroupedSorting(true); + m_model->setGroupRole("text"); m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); QCOMPARE(itemsInModel(), @@ -1990,6 +2094,8 @@ void KFileItemModelTest::testNameRoleGroupsWithExpandedItems() m_testDir->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"}); m_model->setGroupedSorting(true); + m_model->setGroupRole("text"); + m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); QCOMPARE(itemsInModel(), @@ -1999,6 +2105,7 @@ void KFileItemModelTest::testNameRoleGroupsWithExpandedItems() QList> expectedGroups; expectedGroups << QPair(0, QLatin1String("A")); expectedGroups << QPair(1, QLatin1String("D")); + QCOMPARE(m_model->groups(), expectedGroups); // Verify that expanding "a" and "d" will not change the groups (except for the index of "D").