]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/tests/kitemlistselectionmanagertest.cpp
Fix crash when expanding/closing a sub-tree
[dolphin.git] / src / tests / kitemlistselectionmanagertest.cpp
index 7f79332a396692ef1065c67f18e1aaf408aa956f..c27096177038a7d53c88be17e7792a5d2253910b 100644 (file)
@@ -90,16 +90,12 @@ void KItemListSelectionManagerTest::testConstructor()
     QVERIFY(!m_selectionManager->hasSelection());
     QCOMPARE(m_selectionManager->selectedItems().count(), 0);
     QCOMPARE(m_selectionManager->currentItem(), 0);
-    QCOMPARE(m_selectionManager->anchorItem(), -1);
+    QCOMPARE(m_selectionManager->m_anchorItem, -1);
 }
 
 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
 {
     QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
-    QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
-
-    m_selectionManager->setAnchoredSelectionActive(true);
-    QVERIFY(m_selectionManager->isAnchoredSelectionActive());
 
     // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
     m_selectionManager->setCurrentItem(4);
@@ -108,19 +104,10 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 4);
     spyCurrent.takeFirst();
 
-    // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
-    m_selectionManager->setAnchorItem(3);
-    QCOMPARE(m_selectionManager->anchorItem(), 3);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 3);
-    spyAnchor.takeFirst();
-
-    m_selectionManager->setAnchorItem(5);
-    QCOMPARE(m_selectionManager->anchorItem(), 5);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
-    spyAnchor.takeFirst();
+    // Begin an anchored selection.
+    m_selectionManager->beginAnchoredSelection(5);
+    QVERIFY(m_selectionManager->isAnchoredSelectionActive());
+    QCOMPARE(m_selectionManager->m_anchorItem, 5);
 
     // Items between current and anchor should be selected now
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
@@ -149,11 +136,7 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
     spyCurrent.takeFirst();
 
-    QCOMPARE(m_selectionManager->anchorItem(), 8);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
-    spyAnchor.takeFirst();
+    QCOMPARE(m_selectionManager->m_anchorItem, 8);
 
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
     QVERIFY(m_selectionManager->hasSelection());
@@ -170,11 +153,7 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
     spyCurrent.takeFirst();
 
-    QCOMPARE(m_selectionManager->anchorItem(), 5);
-    QCOMPARE(spyAnchor.count(), 1);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
-    QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
-    spyAnchor.takeFirst();
+    QCOMPARE(m_selectionManager->m_anchorItem, 5);
 
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
     QVERIFY(m_selectionManager->hasSelection());
@@ -183,6 +162,9 @@ void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
     m_selectionManager->clearSelection();
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
     QVERIFY(!m_selectionManager->hasSelection());
+
+    m_selectionManager->endAnchoredSelection();
+    QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
 }
 
 void KItemListSelectionManagerTest::testSetSelected_data()
@@ -273,7 +255,7 @@ void KItemListSelectionManagerTest::testAnchoredSelection()
 {
     m_selectionManager->beginAnchoredSelection(5);
     QVERIFY(m_selectionManager->isAnchoredSelectionActive());
-    QCOMPARE(m_selectionManager->anchorItem(), 5);
+    QCOMPARE(m_selectionManager->m_anchorItem, 5);
 
     m_selectionManager->setCurrentItem(6);
     QCOMPARE(m_selectionManager->currentItem(), 6);
@@ -295,7 +277,7 @@ void KItemListSelectionManagerTest::testAnchoredSelection()
     // Start a new anchored selection that overlaps the previous one
     m_selectionManager->beginAnchoredSelection(9);
     QVERIFY(m_selectionManager->isAnchoredSelectionActive());
-    QCOMPARE(m_selectionManager->anchorItem(), 9);
+    QCOMPARE(m_selectionManager->m_anchorItem, 9);
 
     m_selectionManager->setCurrentItem(6);
     QCOMPARE(m_selectionManager->currentItem(), 6);
@@ -315,7 +297,8 @@ namespace {
         NoChange,
         InsertItems,
         RemoveItems,
-        EndAnchoredSelection
+        EndAnchoredSelection,
+        ToggleSelected
     };
 }
 
@@ -338,7 +321,7 @@ void KItemListSelectionManagerTest::testChangeSelection_data()
         << 2 << 3
         << (QSet<int>() << 2 << 3 << 5 << 6)
         << NoChange << KItemRangeList()
-        << (QSet<int>() << 2 << 3 << 5 << 6);    
+        << (QSet<int>() << 2 << 3 << 5 << 6);
 
     QTest::newRow("Insert Items")
         << (QSet<int>() << 5 << 6)
@@ -360,6 +343,13 @@ void KItemListSelectionManagerTest::testChangeSelection_data()
         << QSet<int>()
         << EndAnchoredSelection << KItemRangeList()
         << QSet<int>();
+
+    QTest::newRow("Toggle selection")
+        << (QSet<int>() << 1 << 3 << 4)
+        << 6 << 8
+        << (QSet<int>() << 1 << 3 << 4 << 6 << 7 << 8)
+        << ToggleSelected << (KItemRangeList() << KItemRange(0, 10))
+        << (QSet<int>() << 0 << 2 << 5 << 9);
 }
 
 void KItemListSelectionManagerTest::testChangeSelection()
@@ -399,6 +389,8 @@ void KItemListSelectionManagerTest::testChangeSelection()
     m_selectionManager->setCurrentItem(anchor);
     m_selectionManager->beginAnchoredSelection(anchor);
     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) {
@@ -423,6 +415,11 @@ void KItemListSelectionManagerTest::testChangeSelection()
         m_selectionManager->endAnchoredSelection();
         QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
         break;
+    case ToggleSelected:
+        foreach(const KItemRange& range, changedItems) {
+            m_selectionManager->setSelected(range.index, range.count, KItemListSelectionManager::Toggle);
+        }
+        break;
     case NoChange:
         break;
     }