+void KFileItemModelTest::testNameRoleGroupsWithExpandedItems()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ QSet<QByteArray> modelRoles = m_model->roles();
+ modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
+ m_model->setRoles(modelRoles);
+
+ m_testDir->createFiles({"a/b.txt", "a/c.txt", "d/e.txt", "d/f.txt"});
+
+ m_model->setGroupedSorting(true);
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "d");
+
+ QList<QPair<int, QVariant> > expectedGroups;
+ expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
+ expectedGroups << QPair<int, QVariant>(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").
+ expectedGroups.clear();
+ expectedGroups << QPair<int, QVariant>(0, QLatin1String("A"));
+ expectedGroups << QPair<int, QVariant>(3, QLatin1String("D"));
+
+ m_model->setExpanded(0, true);
+ QVERIFY(m_model->isExpanded(0));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d");
+ QCOMPARE(m_model->groups(), expectedGroups);
+
+ m_model->setExpanded(3, true);
+ QVERIFY(m_model->isExpanded(3));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "b.txt" << "c.txt" << "d" << "e.txt" << "f.txt");
+ QCOMPARE(m_model->groups(), expectedGroups);
+}
+
+void KFileItemModelTest::testInconsistentModel()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ QSet<QByteArray> modelRoles = m_model->roles();
+ modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
+ m_model->setRoles(modelRoles);
+
+ m_testDir->createFiles({"a/b/c1.txt", "a/b/c2.txt"});
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a");
+
+ // Expand "a/" and "a/b/".
+ m_model->setExpanded(0, true);
+ QVERIFY(m_model->isExpanded(0));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "b");
+
+ m_model->setExpanded(1, true);
+ QVERIFY(m_model->isExpanded(1));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt");
+
+ // Add the files "c1.txt" and "c2.txt" to the model also as top-level items.
+ // Such a thing can in principle happen when performing a search, and there
+ // are files which
+ // (a) match the search string, and
+ // (b) are children of a folder that matches the search string and is expanded.
+ //
+ // Note that the first item in the list of added items must be new (i.e., not
+ // in the model yet). Otherwise, KFileItemModel::slotItemsAdded() will see that
+ // it receives items that are in the model already and ignore them.
+ QUrl url(m_model->directory().url() + "/a2");
+ KFileItem newItem(url);
+
+ KFileItemList items;
+ items << newItem << m_model->fileItem(2) << m_model->fileItem(3);
+ m_model->slotItemsAdded(m_model->directory(), items);
+ m_model->slotCompleted();
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "b" << "c1.txt" << "c2.txt" << "a2" << "c1.txt" << "c2.txt");
+
+ m_model->setExpanded(0, false);
+
+ // Test that the right items have been removed, see
+ // https://bugs.kde.org/show_bug.cgi?id=324371
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "a2" << "c1.txt" << "c2.txt");
+
+ // Test that resorting does not cause a crash, see
+ // https://bugs.kde.org/show_bug.cgi?id=325359
+ // The crash is not 100% reproducible, but Valgrind will report an invalid memory access.
+ m_model->resortAllItems();
+
+}
+
+void KFileItemModelTest::testChangeRolesForFilteredItems()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ QSet<QByteArray> modelRoles = m_model->roles();
+ modelRoles << "owner";
+ m_model->setRoles(modelRoles);
+
+ m_testDir->createFiles({"a.txt", "aa.txt", "aaa.txt"});
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
+
+ for (int index = 0; index < m_model->count(); ++index) {
+ // All items should have the "text" and "owner" roles, but not "group".
+ QVERIFY(m_model->data(index).contains("text"));
+ QVERIFY(m_model->data(index).contains("owner"));
+ QVERIFY(!m_model->data(index).contains("group"));
+ }
+
+ // Add a filter, such that only "aaa.txt" remains in the model.
+ m_model->setNameFilter("aaa");
+ QCOMPARE(itemsInModel(), QStringList() << "aaa.txt");
+
+ // Add the "group" role.
+ modelRoles << "group";
+ m_model->setRoles(modelRoles);
+
+ // Modify the filter, such that "aa.txt" reappears, and verify that all items have the expected roles.
+ m_model->setNameFilter("aa");
+ QCOMPARE(itemsInModel(), QStringList() << "aa.txt" << "aaa.txt");
+
+ for (int index = 0; index < m_model->count(); ++index) {
+ // All items should have the "text", "owner", and "group" roles.
+ QVERIFY(m_model->data(index).contains("text"));
+ QVERIFY(m_model->data(index).contains("owner"));
+ QVERIFY(m_model->data(index).contains("group"));
+ }
+
+ // Remove the "owner" role.
+ modelRoles.remove("owner");
+ m_model->setRoles(modelRoles);
+
+ // Clear the filter, and verify that all items have the expected roles
+ m_model->setNameFilter(QString());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "aa.txt" << "aaa.txt");
+
+ for (int index = 0; index < m_model->count(); ++index) {
+ // All items should have the "text" and "group" roles, but now "owner".
+ QVERIFY(m_model->data(index).contains("text"));
+ QVERIFY(!m_model->data(index).contains("owner"));
+ QVERIFY(m_model->data(index).contains("group"));
+ }
+}
+
+void KFileItemModelTest::testChangeSortRoleWhileFiltering()
+{
+ KFileItemList items;
+
+ KIO::UDSEntry entry[3];
+
+ entry[0].fastInsert(KIO::UDSEntry::UDS_NAME, "a.txt");
+ entry[0].fastInsert(KIO::UDSEntry::UDS_USER, "user-b");
+
+ entry[1].fastInsert(KIO::UDSEntry::UDS_NAME, "b.txt");
+ entry[1].fastInsert(KIO::UDSEntry::UDS_USER, "user-c");
+
+ entry[2].fastInsert(KIO::UDSEntry::UDS_NAME, "c.txt");
+ entry[2].fastInsert(KIO::UDSEntry::UDS_USER, "user-a");
+
+ 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();
+
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt");
+
+ // Add a filter.
+ m_model->setNameFilter("a");
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt");
+
+ // Sort by "owner".
+ m_model->setSortRole("owner");
+
+ // Clear the filter, and verify that the items are sorted correctly.
+ m_model->setNameFilter(QString());
+ QCOMPARE(itemsInModel(), QStringList() << "c.txt" << "a.txt" << "b.txt");
+}
+
+void KFileItemModelTest::testRefreshFilteredItems()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ m_testDir->createFiles({"a.txt", "b.txt", "c.jpg", "d.jpg"});
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.jpg" << "d.jpg");
+
+ const KFileItem fileItemC = m_model->fileItem(2);
+
+ // Show only the .txt files.
+ m_model->setNameFilter(".txt");
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt");
+
+ // Rename one of the .jpg files.
+ KFileItem fileItemE = fileItemC;
+ QUrl urlE = fileItemE.url().adjusted(QUrl::RemoveFilename);
+ urlE.setPath(urlE.path() + "/e.jpg");
+ fileItemE.setUrl(urlE);
+
+ m_model->slotRefreshItems({qMakePair(fileItemC, fileItemE)});
+
+ // Show all files again, and verify that the model has updated the file name.
+ m_model->setNameFilter(QString());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "d.jpg" << "e.jpg");
+}
+
+void KFileItemModelTest::testCreateMimeData()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ QSet<QByteArray> modelRoles = m_model->roles();
+ modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
+ m_model->setRoles(modelRoles);
+
+ m_testDir->createFile("a/1");
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a");
+
+ // Expand "a/".
+ m_model->setExpanded(0, true);
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a" << "1");
+
+ // Verify that creating the MIME data for a child of an expanded folder does
+ // not cause a crash, see https://bugs.kde.org/show_bug.cgi?id=329119
+ KItemSet selection;
+ selection.insert(1);
+ QMimeData* mimeData = m_model->createMimeData(selection);
+ delete mimeData;
+}
+
+void KFileItemModelTest::testCollapseFolderWhileLoading()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ QSet<QByteArray> modelRoles = m_model->roles();
+ modelRoles << "isExpanded" << "isExpandable" << "expandedParentsCount";
+ m_model->setRoles(modelRoles);
+
+ m_testDir->createFile("a2/b/c1.txt");
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a2");
+
+ // Expand "a2/".
+ m_model->setExpanded(0, true);
+ QVERIFY(m_model->isExpanded(0));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
+
+ // Expand "a2/b/".
+ m_model->setExpanded(1, true);
+ QVERIFY(m_model->isExpanded(1));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
+
+ // Simulate that a new item "c2.txt" appears, but that the dir lister's completed()
+ // signal is not emitted yet.
+ const KFileItem fileItemC1 = m_model->fileItem(2);
+ KFileItem fileItemC2 = fileItemC1;
+ QUrl urlC2 = fileItemC2.url();
+ urlC2 = urlC2.adjusted(QUrl::RemoveFilename);
+ urlC2.setPath(urlC2.path() + "c2.txt");
+ fileItemC2.setUrl(urlC2);
+
+ const QUrl urlB = m_model->fileItem(1).url();
+ m_model->slotItemsAdded(urlB, KFileItemList() << fileItemC2);
+ QCOMPARE(itemsInModel(), QStringList() << "a2" << "b" << "c1.txt");
+
+ // Collapse "a2/". This should also remove all its (indirect) children from
+ // the model and from the model's m_pendingItemsToInsert member.
+ m_model->setExpanded(0, false);
+ QCOMPARE(itemsInModel(), QStringList() << "a2");
+
+ // Simulate that the dir lister's completed() signal is emitted. If "c2.txt"
+ // is still in m_pendingItemsToInsert, then we might get a crash, see
+ // https://bugs.kde.org/show_bug.cgi?id=332102. Even if the crash is not
+ // reproducible here, Valgrind will complain, and the item "c2.txt" will appear
+ // without parent in the model.
+ m_model->slotCompleted();
+ QCOMPARE(itemsInModel(), QStringList() << "a2");
+
+ // Expand "a2/" again.
+ m_model->setExpanded(0, true);
+ QVERIFY(m_model->isExpanded(0));
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
+
+ // Now simulate that a new folder "a1/" is appears, but that the dir lister's
+ // completed() signal is not emitted yet.
+ const KFileItem fileItemA2 = m_model->fileItem(0);
+ KFileItem fileItemA1 = fileItemA2;
+ QUrl urlA1 = fileItemA1.url().adjusted(QUrl::RemoveFilename);
+ urlA1.setPath(urlA1.path() + "a1");
+ fileItemA1.setUrl(urlA1);
+
+ m_model->slotItemsAdded(m_model->directory(), KFileItemList() << fileItemA1);
+ QCOMPARE(itemsInModel(), QStringList() << "a2" << "b");
+
+ // Collapse "a2/". Note that this will cause "a1/" to be added to the model,
+ // i.e., the index of "a2/" will change from 0 to 1. Check that this does not
+ // confuse the code which collapses the folder.
+ m_model->setExpanded(0, false);
+ QCOMPARE(itemsInModel(), QStringList() << "a1" << "a2");
+ QVERIFY(!m_model->isExpanded(0));
+ QVERIFY(!m_model->isExpanded(1));
+}
+
+void KFileItemModelTest::testDeleteFileMoreThanOnce()
+{
+ QSignalSpy itemsInsertedSpy(m_model, &KFileItemModel::itemsInserted);
+
+ m_testDir->createFiles({"a.txt", "b.txt", "c.txt", "d.txt"});
+
+ m_model->loadDirectory(m_testDir->url());
+ QVERIFY(itemsInsertedSpy.wait());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "b.txt" << "c.txt" << "d.txt");
+
+ const KFileItem fileItemB = m_model->fileItem(1);
+
+ // Tell the model that a list of items has been deleted, where "b.txt" appears twice in the list.
+ KFileItemList list;
+ list << fileItemB << fileItemB;
+ m_model->slotItemsDeleted(list);
+
+ QVERIFY(m_model->isConsistent());
+ QCOMPARE(itemsInModel(), QStringList() << "a.txt" << "c.txt" << "d.txt");
+}
+