X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/d19d94e60a778d4c19f646224e3b269da515c8fb..272aa65bb55dd31f58e8a8e24ea7bc8bd2712daa:/src/tests/kfileitemmodeltest.cpp diff --git a/src/tests/kfileitemmodeltest.cpp b/src/tests/kfileitemmodeltest.cpp index 74b2342c8..f527fbc61 100644 --- a/src/tests/kfileitemmodeltest.cpp +++ b/src/tests/kfileitemmodeltest.cpp @@ -1,25 +1,14 @@ -/*************************************************************************** - * Copyright (C) 2011 by Peter Penz * - * Copyright (C) 2011 by Frank Reininghaus * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2011 Peter Penz + * SPDX-FileCopyrightText: 2011 Frank Reininghaus + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#include #include #include +#include #include #include @@ -29,18 +18,20 @@ #include "kitemviews/private/kfileitemmodeldirlister.h" #include "testdir.h" -void myMessageOutput(QtMsgType type, const char* msg) +void myMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg) { + Q_UNUSED(context) + switch (type) { case QtDebugMsg: break; case QtWarningMsg: break; case QtCriticalMsg: - fprintf(stderr, "Critical: %s\n", msg); + fprintf(stderr, "Critical: %s\n", msg.toLocal8Bit().data()); break; case QtFatalMsg: - fprintf(stderr, "Fatal: %s\n", msg); + fprintf(stderr, "Fatal: %s\n", msg.toLocal8Bit().data()); abort(); default: break; @@ -55,8 +46,9 @@ class KFileItemModelTest : public QObject { Q_OBJECT -private slots: +private Q_SLOTS: void init(); + void initTestCase(); void cleanup(); void testDefaultRoles(); @@ -103,11 +95,16 @@ private: TestDir* m_testDir; }; +void KFileItemModelTest::initTestCase() +{ + QStandardPaths::setTestModeEnabled(true); +} + void KFileItemModelTest::init() { // The item-model tests result in a huge number of debugging // output from kdelibs. Only show critical and fatal messages. - qInstallMsgHandler(myMessageOutput); + qInstallMessageHandler(myMessageOutput); qRegisterMetaType("KItemRange"); qRegisterMetaType("KItemRangeList"); @@ -124,24 +121,26 @@ void KFileItemModelTest::init() void KFileItemModelTest::cleanup() { delete m_model; - m_model = 0; + m_model = nullptr; delete m_testDir; - m_testDir = 0; + m_testDir = nullptr; } void KFileItemModelTest::testDefaultRoles() { const QSet roles = m_model->roles(); - QCOMPARE(roles.count(), 3); + QCOMPARE(roles.count(), 4); QVERIFY(roles.contains("text")); QVERIFY(roles.contains("isDir")); QVERIFY(roles.contains("isLink")); + QVERIFY(roles.contains("isHidden")); } void KFileItemModelTest::testDefaultSortRole() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); + QVERIFY(itemsInsertedSpy.isValid()); QCOMPARE(m_model->sortRole(), QByteArray("text")); @@ -151,9 +150,9 @@ void KFileItemModelTest::testDefaultSortRole() QVERIFY(itemsInsertedSpy.wait()); QCOMPARE(m_model->count(), 3); - QCOMPARE(m_model->data(0)["text"].toString(), QString("a.txt")); - QCOMPARE(m_model->data(1)["text"].toString(), QString("b.txt")); - QCOMPARE(m_model->data(2)["text"].toString(), QString("c.txt")); + QCOMPARE(m_model->data(0).value("text").toString(), QString("a.txt")); + QCOMPARE(m_model->data(1).value("text").toString(), QString("b.txt")); + QCOMPARE(m_model->data(2).value("text").toString(), QString("c.txt")); } void KFileItemModelTest::testDefaultGroupedSorting() @@ -239,7 +238,9 @@ void KFileItemModelTest::testDirLoadingCompleted() void KFileItemModelTest::testSetData() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); + QVERIFY(itemsInsertedSpy.isValid()); QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged); + QVERIFY(itemsChangedSpy.isValid()); m_testDir->createFile("a.txt"); @@ -292,7 +293,9 @@ void KFileItemModelTest::testSetDataWithModifiedSortRole() QFETCH(int, ratingIndex2); QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); + QVERIFY(itemsInsertedSpy.isValid()); QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved); + QVERIFY(itemsMovedSpy.isValid()); // Changing the value of a sort-role must result in // a reordering of the items. @@ -346,6 +349,7 @@ void KFileItemModelTest::testChangeSortRole() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved); + QVERIFY(itemsMovedSpy.isValid()); QCOMPARE(m_model->sortRole(), QByteArray("text")); @@ -385,6 +389,7 @@ void KFileItemModelTest::testResortAfterChangingName() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved); + QVERIFY(itemsMovedSpy.isValid()); // We sort by size in a directory where all files have the same size. // Therefore, the files are sorted by their names. @@ -440,9 +445,9 @@ void KFileItemModelTest::testModelConsistencyWhenInsertingItems() itemsInsertedSpy.clear(); for (int j = 0; j < 10; ++j) { - int itemName = qrand(); + int itemName = QRandomGenerator::global()->generate(); while (insertedItems.contains(itemName)) { - itemName = qrand(); + itemName = QRandomGenerator::global()->generate(); } insertedItems.insert(itemName); @@ -450,7 +455,7 @@ void KFileItemModelTest::testModelConsistencyWhenInsertingItems() } m_model->m_dirLister->updateDirectory(m_testDir->url()); - if (itemsInsertedSpy.count() == 0) { + if (itemsInsertedSpy.isEmpty()) { QVERIFY(itemsInsertedSpy.wait()); } @@ -502,8 +507,11 @@ void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems() void KFileItemModelTest::testExpandItems() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); + QVERIFY(itemsInsertedSpy.isValid()); QSignalSpy itemsRemovedSpy(m_model, &KFileItemModel::itemsRemoved); + QVERIFY(itemsRemovedSpy.isValid()); QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted); + QVERIFY(loadingCompletedSpy.isValid()); // Test expanding subfolders in a folder with the items "a/", "a/a/", "a/a/1", "a/a-1/", "a/a-1/1". // Besides testing the basic item expansion functionality, the test makes sure that @@ -637,6 +645,7 @@ void KFileItemModelTest::testExpandParentItems() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted); + QVERIFY(loadingCompletedSpy.isValid()); // Create a tree structure of folders: // a 1/ @@ -803,8 +812,22 @@ void KFileItemModelTest::testRemoveFilteredExpandedItems() void KFileItemModelTest::testSorting() { + // testDir structure is as follows + // ./ + // ├─ a + // ├─ b + // ├─ c/ + // │ ├─ c-2/ + // │ │ ├─ c-3 + // │ ├─ c-1 + // ├─ d + // ├─ e + // ├─ .f + // ├─ .g/ + 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(); @@ -826,17 +849,27 @@ void KFileItemModelTest::testSorting() 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_testDir->createFile(".f"); + m_testDir->createDir(".g"); m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); + QCOMPARE(itemsInsertedSpy.count(), 1); + KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value(); + QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 5)); int index = m_model->index(QUrl(m_testDir->url().url() + "/c")); m_model->setExpanded(index, true); QVERIFY(itemsInsertedSpy.wait()); + QCOMPARE(itemsInsertedSpy.count(), 1); + itemRangeList = itemsInsertedSpy.takeFirst().at(0).value(); + QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(1, 2)); index = m_model->index(QUrl(m_testDir->url().url() + "/c/c-2")); m_model->setExpanded(index, true); QVERIFY(itemsInsertedSpy.wait()); + QCOMPARE(itemsInsertedSpy.count(), 1); + itemRangeList = itemsInsertedSpy.takeFirst().at(0).value(); + QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(2, 1)); // Default: Sort by Name, ascending QCOMPARE(m_model->sortRole(), QByteArray("text")); @@ -868,8 +901,8 @@ void KFileItemModelTest::testSorting() // Sort by Date, descending m_model->setSortDirectoriesFirst(true); - m_model->setSortRole("date"); - QCOMPARE(m_model->sortRole(), QByteArray("date")); + m_model->setSortRole("modificationtime"); + QCOMPARE(m_model->sortRole(), QByteArray("modificationtime")); QCOMPARE(m_model->sortOrder(), Qt::DescendingOrder); QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "b" << "d" << "a" << "e"); QCOMPARE(itemsMovedSpy.count(), 1); @@ -878,7 +911,7 @@ void KFileItemModelTest::testSorting() // Sort by Date, ascending m_model->setSortOrder(Qt::AscendingOrder); - QCOMPARE(m_model->sortRole(), QByteArray("date")); + QCOMPARE(m_model->sortRole(), QByteArray("modificationtime")); QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder); QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "a" << "d" << "b"); QCOMPARE(itemsMovedSpy.count(), 1); @@ -887,7 +920,7 @@ void KFileItemModelTest::testSorting() // Sort by Date, ascending, 'Sort Folders First' disabled m_model->setSortDirectoriesFirst(false); - QCOMPARE(m_model->sortRole(), QByteArray("date")); + QCOMPARE(m_model->sortRole(), QByteArray("modificationtime")); QCOMPARE(m_model->sortOrder(), Qt::AscendingOrder); QVERIFY(!m_model->sortDirectoriesFirst()); QCOMPARE(itemsInModel(), QStringList() << "e" << "a" << "c" << "c-1" << "c-2" << "c-3" << "d" << "b"); @@ -932,7 +965,36 @@ void KFileItemModelTest::testSorting() QCOMPARE(itemsMovedSpy.first().at(0).value(), KItemRange(4, 4)); QCOMPARE(itemsMovedSpy.takeFirst().at(1).value >(), QList() << 7 << 6 << 5 << 4); - // TODO: Sort by other roles; show/hide hidden files + // 'Show Hidden Files' enabled + m_model->setShowHiddenFiles(true); + QVERIFY(m_model->showHiddenFiles()); + QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "d" << "e" << "b" << "a" << ".g" << ".f"); + QCOMPARE(itemsMovedSpy.count(), 0); + QCOMPARE(itemsInsertedSpy.count(), 1); + QCOMPARE(itemsInsertedSpy.takeFirst().at(0).value(), KItemRangeList() << KItemRange(8, 2)); + + // Sort by Name + m_model->setSortRole("text"); + QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "e" << "d" << "b" << "a" << ".g" << ".f"); + QCOMPARE(itemsMovedSpy.count(), 1); + QCOMPARE(itemsMovedSpy.first().at(0).value(), KItemRange(4, 2)); + QCOMPARE(itemsMovedSpy.takeFirst().at(1).value >(), QList() << 5 << 4); + + // Sort ascending + m_model->setSortOrder(Qt::AscendingOrder); + QCOMPARE(itemsInModel(), QStringList() << "c" << "c-2" << "c-3" << "c-1" << "a" << "b" << "d" << "e" << ".g" << ".f"); + QCOMPARE(itemsMovedSpy.count(), 1); + QCOMPARE(itemsMovedSpy.first().at(0).value(), KItemRange(4, 4)); + QCOMPARE(itemsMovedSpy.takeFirst().at(1).value >(), QList() << 7 << 6 << 5 << 4); + + // 'Sort Folders First' disabled + m_model->setSortDirectoriesFirst(false); + QVERIFY(!m_model->sortDirectoriesFirst()); + QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c" << "c-1" << "c-2" << "c-3" << "d" << "e" << ".f" << ".g"); + QCOMPARE(itemsMovedSpy.count(), 1); + QCOMPARE(itemsMovedSpy.first().at(0).value(), KItemRange(0, 10)); + QCOMPARE(itemsMovedSpy.takeFirst().at(1).value >(), QList() << 2 << 4 << 5 << 3 << 0 << 1 << 6 << 7 << 9 << 8); + } void KFileItemModelTest::testIndexForKeyboardSearch() @@ -1046,6 +1108,7 @@ void KFileItemModelTest::testRefreshExpandedItem() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); QSignalSpy itemsChangedSpy(m_model, &KFileItemModel::itemsChanged); + QVERIFY(itemsChangedSpy.isValid()); QSet modelRoles = m_model->roles(); modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount"; @@ -1088,7 +1151,7 @@ void KFileItemModelTest::testRemoveHiddenItems() m_model->setShowHiddenFiles(true); m_model->loadDirectory(m_testDir->url()); QVERIFY(itemsInsertedSpy.wait()); - QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i"); + QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i" << ".a" << ".b" <<".f" << ".g"); QCOMPARE(itemsInsertedSpy.count(), 1); QCOMPARE(itemsRemovedSpy.count(), 0); KItemRangeList itemRangeList = itemsInsertedSpy.takeFirst().at(0).value(); @@ -1099,14 +1162,14 @@ void KFileItemModelTest::testRemoveHiddenItems() QCOMPARE(itemsInsertedSpy.count(), 0); QCOMPARE(itemsRemovedSpy.count(), 1); itemRangeList = itemsRemovedSpy.takeFirst().at(0).value(); - QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(4, 2)); + QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 4)); m_model->setShowHiddenFiles(true); - QCOMPARE(itemsInModel(), QStringList() << ".a" << ".b" << "c" << "d" <<".f" << ".g" << "h" << "i"); + QCOMPARE(itemsInModel(), QStringList() << "c" << "d" << "h" << "i" << ".a" << ".b" <<".f" << ".g"); QCOMPARE(itemsInsertedSpy.count(), 1); QCOMPARE(itemsRemovedSpy.count(), 0); itemRangeList = itemsInsertedSpy.takeFirst().at(0).value(); - QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 2)); + QCOMPARE(itemRangeList, KItemRangeList() << KItemRange(4, 4)); m_model->clear(); QCOMPARE(itemsInModel(), QStringList()); @@ -1317,7 +1380,9 @@ void KFileItemModelTest::testNameRoleGroups() { QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted); QSignalSpy itemsMovedSpy(m_model, &KFileItemModel::itemsMoved); + QVERIFY(itemsMovedSpy.isValid()); QSignalSpy groupsChangedSpy(m_model, &KFileItemModel::groupsChanged); + QVERIFY(groupsChangedSpy.isValid()); m_testDir->createFiles({"b.txt", "c.txt", "d.txt", "e.txt"}); @@ -1533,25 +1598,26 @@ void KFileItemModelTest::testChangeSortRoleWhileFiltering() { KFileItemList items; - KIO::UDSEntry entry; - entry.insert(KIO::UDSEntry::UDS_FILE_TYPE, 0100000); // S_IFREG might not be defined on non-Unix platforms. - entry.insert(KIO::UDSEntry::UDS_ACCESS, 07777); - entry.insert(KIO::UDSEntry::UDS_SIZE, 0); - entry.insert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0); - entry.insert(KIO::UDSEntry::UDS_GROUP, "group"); - entry.insert(KIO::UDSEntry::UDS_ACCESS_TIME, 0); + KIO::UDSEntry entry[3]; + + entry[0].fastInsert(KIO::UDSEntry::UDS_NAME, "a.txt"); + entry[0].fastInsert(KIO::UDSEntry::UDS_USER, "user-b"); - entry.insert(KIO::UDSEntry::UDS_NAME, "a.txt"); - entry.insert(KIO::UDSEntry::UDS_USER, "user-b"); - items.append(KFileItem(entry, m_testDir->url(), false, true)); + entry[1].fastInsert(KIO::UDSEntry::UDS_NAME, "b.txt"); + entry[1].fastInsert(KIO::UDSEntry::UDS_USER, "user-c"); - entry.insert(KIO::UDSEntry::UDS_NAME, "b.txt"); - entry.insert(KIO::UDSEntry::UDS_USER, "user-c"); - items.append(KFileItem(entry, m_testDir->url(), false, true)); + entry[2].fastInsert(KIO::UDSEntry::UDS_NAME, "c.txt"); + entry[2].fastInsert(KIO::UDSEntry::UDS_USER, "user-a"); - entry.insert(KIO::UDSEntry::UDS_NAME, "c.txt"); - entry.insert(KIO::UDSEntry::UDS_USER, "user-a"); - items.append(KFileItem(entry, m_testDir->url(), false, true)); + for (int i = 0; i < 3; ++i) { + entry[i].fastInsert(KIO::UDSEntry::UDS_FILE_TYPE, 0100000); // S_IFREG might not be defined on non-Unix platforms. + entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS, 07777); + entry[i].fastInsert(KIO::UDSEntry::UDS_SIZE, 0); + entry[i].fastInsert(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0); + entry[i].fastInsert(KIO::UDSEntry::UDS_GROUP, "group"); + entry[i].fastInsert(KIO::UDSEntry::UDS_ACCESS_TIME, 0); + items.append(KFileItem(entry[i], m_testDir->url(), false, true)); + } m_model->slotItemsAdded(m_testDir->url(), items); m_model->slotCompleted(); @@ -1657,7 +1723,7 @@ void KFileItemModelTest::testCollapseFolderWhileLoading() const KFileItem fileItemC1 = m_model->fileItem(2); KFileItem fileItemC2 = fileItemC1; QUrl urlC2 = fileItemC2.url(); - urlC2.adjusted(QUrl::RemoveFilename); + urlC2 = urlC2.adjusted(QUrl::RemoveFilename); urlC2.setPath(urlC2.path() + "c2.txt"); fileItemC2.setUrl(urlC2);