]> 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 2fa6f677b4de579a33ce5e286fd460b893194102..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,24 +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()
@@ -316,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.
@@ -474,6 +489,36 @@ void KItemListSelectionManagerTest::testChangeSelection()
     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