+
+ verifySelectionChange(spySelectionChanged, KItemSet(), finalSelection);
+}
+
+void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
+{
+ QTest::addColumn<int>("oldCurrentItemIndex");
+ QTest::addColumn<int>("removeIndex");
+ QTest::addColumn<int>("removeCount");
+ QTest::addColumn<int>("newCurrentItemIndex");
+
+ QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
+ QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
+ QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
+ QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
+ QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
+}
+
+void KItemListSelectionManagerTest::testDeleteCurrentItem()
+{
+ QFETCH(int, oldCurrentItemIndex);
+ QFETCH(int, removeIndex);
+ QFETCH(int, removeCount);
+ QFETCH(int, newCurrentItemIndex);
+
+ m_selectionManager->setCurrentItem(oldCurrentItemIndex);
+
+ const int newCount = m_model->count() - removeCount;
+ m_model->setCount(newCount);
+ m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(removeIndex, removeCount));
+
+ QCOMPARE(m_selectionManager->currentItem(), newCurrentItemIndex);
+}
+
+void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
+{
+ m_selectionManager->setCurrentItem(4);
+ m_selectionManager->beginAnchoredSelection(4);
+
+ // Reverse the items between 0 and 5.
+ m_selectionManager->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
+
+ QCOMPARE(m_selectionManager->currentItem(), 1);
+ QCOMPARE(m_selectionManager->m_anchorItem, 1);
+
+ // Make 2 the current item -> 1 and 2 should be selected.
+ m_selectionManager->setCurrentItem(2);
+ QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 1 << 2);
+}
+
+void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
+ const KItemSet& currentSelection,
+ const KItemSet& previousSelection) const
+{
+ QCOMPARE(m_selectionManager->selectedItems(), currentSelection);
+ QCOMPARE(m_selectionManager->hasSelection(), !currentSelection.isEmpty());
+ for (int index = 0; index < m_selectionManager->model()->count(); ++index) {
+ if (currentSelection.contains(index)) {
+ QVERIFY(m_selectionManager->isSelected(index));
+ }
+ else {
+ QVERIFY(!m_selectionManager->isSelected(index));
+ }
+ }
+
+ if (currentSelection == previousSelection) {
+ QCOMPARE(spy.count(), 0);