]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kitemlistselectionmanagertest.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / tests / kitemlistselectionmanagertest.cpp
index 2a3601bb0a198dfeefcde7578b8191f351b55a7e..302985a5f4c72a0696d1f837a8d5ea1e95c6c643 100644 (file)
@@ -27,18 +27,28 @@ class DummyModel : public KItemModelBase
 {
 public:
     DummyModel();
+    void setCount(int count);
     virtual int count() const;
     virtual QHash<QByteArray, QVariant> data(int index) const;
+
+private:
+    int m_count;
 };
 
 DummyModel::DummyModel() :
-    KItemModelBase()
+    KItemModelBase(),
+    m_count(100)
+{
+}
+
+void DummyModel::setCount(int count)
 {
+    m_count = count;
 }
 
 int DummyModel::count() const
 {
-    return 100;
+    return m_count;
 }
 
 QHash<QByteArray, QVariant> DummyModel::data(int index) const
@@ -48,7 +58,6 @@ QHash<QByteArray, QVariant> DummyModel::data(int index) const
 }
 
 
-
 class KItemListSelectionManagerTest : public QObject
 {
     Q_OBJECT
@@ -67,22 +76,30 @@ private slots:
     void testAnchoredSelection();
     void testChangeSelection_data();
     void testChangeSelection();
+    void testDeleteCurrentItem_data();
+    void testDeleteCurrentItem();
 
 private:
+    void verifySelectionChange(QSignalSpy& spy, const QSet<int>& currentSelection, const QSet<int>& previousSelection) const;
+
     KItemListSelectionManager* m_selectionManager;
+    DummyModel* m_model;
 };
 
 void KItemListSelectionManagerTest::init()
 {
+    m_model = new DummyModel();
     m_selectionManager = new KItemListSelectionManager();
-    m_selectionManager->setModel(new DummyModel());
+    m_selectionManager->setModel(m_model);
 }
 
 void KItemListSelectionManagerTest::cleanup()
 {
-    delete m_selectionManager->model();
     delete m_selectionManager;
     m_selectionManager = 0;
+
+    delete m_model;
+    m_model = 0;
 }
 
 void KItemListSelectionManagerTest::testConstructor()
@@ -314,7 +331,7 @@ Q_DECLARE_METATYPE(QList<int>);
  * The following function provides a generic way to test the selection functionality.
  *
  * The test is data-driven and takes the following arguments:
- * 
+ *
  * \param initialSelection  The selection at the beginning.
  * \param anchor            This item will be the anchor item.
  * \param current           This item will be the current item.
@@ -425,18 +442,8 @@ void KItemListSelectionManagerTest::testChangeSelection()
 
     // Perform the initial selectiion
     m_selectionManager->setSelectedItems(initialSelection);
-    QCOMPARE(m_selectionManager->selectedItems(), initialSelection);
-    if (initialSelection.isEmpty()) {
-        QVERIFY(!m_selectionManager->hasSelection());
-        QCOMPARE(spySelectionChanged.count(), 0);
-    }
-    else {
-        QVERIFY(m_selectionManager->hasSelection());
-        QCOMPARE(spySelectionChanged.count(), 1);
-        QList<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
-    }
+
+    verifySelectionChange(spySelectionChanged, initialSelection, QSet<int>());
 
     // Perform an anchored selection.
     // Note that current and anchor index are equal first because this is the case in typical uses of the
@@ -446,17 +453,8 @@ void KItemListSelectionManagerTest::testChangeSelection()
     m_selectionManager->setCurrentItem(current);
     QCOMPARE(m_selectionManager->m_anchorItem, anchor);
     QCOMPARE(m_selectionManager->currentItem(), current);
-    QCOMPARE(m_selectionManager->selectedItems(), expectedSelection);
-    QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty());
-    if (expectedSelection == initialSelection) {
-        QCOMPARE(spySelectionChanged.count(), 0);
-    }
-    else {
-        QCOMPARE(spySelectionChanged.count(), 1);
-        QList<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), initialSelection);
-    }
+
+    verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
 
     // Change the model by inserting or removing items.
     switch (changeType) {
@@ -483,31 +481,67 @@ void KItemListSelectionManagerTest::testChangeSelection()
         break;
     }
 
-    QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
-    QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
-    if (finalSelection == expectedSelection) {
-        QCOMPARE(spySelectionChanged.count(), 0);
-    }
-    else {
-        QCOMPARE(spySelectionChanged.count(), 1);
-        QList<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
-    }
+    verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
 
     // Finally, clear the selection
     m_selectionManager->clearSelection();
-    QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
-    QVERIFY(!m_selectionManager->hasSelection());
-    if (finalSelection.isEmpty()) {
-        // Selection has been empty already
-        QCOMPARE(spySelectionChanged.count(), 0);
+
+    verifySelectionChange(spySelectionChanged, QSet<int>(), 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::verifySelectionChange(QSignalSpy& spy,
+                                                          const QSet<int>& currentSelection,
+                                                          const QSet<int>& 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);
     }
     else {
-        QCOMPARE(spySelectionChanged.count(), 1);
-        QList<QVariant> arguments = spySelectionChanged.takeFirst();
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
-        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
+        QCOMPARE(spy.count(), 1);
+        QList<QVariant> arguments = spy.takeFirst();
+        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), currentSelection);
+        QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), previousSelection);
     }
 }