[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kde-baseapps] dolphin/src: Test signal emission on selection changes
From:       Frank Reininghaus <frank78ac () googlemail ! com>
Date:       2011-08-11 12:24:18
Message-ID: 20110811122418.0131EA60B0 () git ! kde ! org
[Download RAW message or body]

Git commit 249a73573f4ca0854cf9a10aec77d54f10e09342 by Frank Reininghaus.
Committed on 11/08/2011 at 14:20.
Pushed by freininghaus into branch 'master'.

Test signal emission on selection changes

This commit adds a unit test that changes the selection in various
ways, verifies the result and checks that the selection manager's
selectionChanged signal has been emitted correctly. The test is
data-driven, so I hope that most further testing needs can be
fulfilled by adding new test data.

Moreover, I changed selectedItems() such that the anchored
selection is only taken into account if anchor and current item
are different. The reason is that in some situation the anchor
should not be selected initially (i.e., if an already selected
item is Control-clicked). If the anchor should be selected from
the beginning, it must be selected manually.

M  +2    -2    dolphin/src/kitemviews/kitemlistselectionmanager.cpp
M  +135  -0    dolphin/src/tests/kitemlistselectionmanagertest.cpp

http://commits.kde.org/kde-baseapps/249a73573f4ca0854cf9a10aec77d54f10e09342

diff --git a/dolphin/src/kitemviews/kitemlistselectionmanager.cpp \
b/dolphin/src/kitemviews/kitemlistselectionmanager.cpp index cfe847b..e0ec406 100644
--- a/dolphin/src/kitemviews/kitemlistselectionmanager.cpp
+++ b/dolphin/src/kitemviews/kitemlistselectionmanager.cpp
@@ -81,7 +81,7 @@ QSet<int> KItemListSelectionManager::selectedItems() const
 {
     QSet<int> selectedItems = m_selectedItems;
 
-    if (m_isAnchoredSelectionActive) {
+    if (m_isAnchoredSelectionActive && (m_anchorItem != m_currentItem)) {
         const int from = qMin(m_anchorItem, m_currentItem);
         const int to = qMax(m_anchorItem, m_currentItem);
 
@@ -95,7 +95,7 @@ QSet<int> KItemListSelectionManager::selectedItems() const
 
 bool KItemListSelectionManager::hasSelection() const
 {
-    return !m_selectedItems.isEmpty() || m_isAnchoredSelectionActive;
+    return !m_selectedItems.isEmpty() || (m_isAnchoredSelectionActive && \
(m_anchorItem != m_currentItem));  }
 
 void KItemListSelectionManager::setSelected(int index, int count, SelectionMode \
                mode)
diff --git a/dolphin/src/tests/kitemlistselectionmanagertest.cpp \
b/dolphin/src/tests/kitemlistselectionmanagertest.cpp index a79b9e6..3174a8b 100644
--- a/dolphin/src/tests/kitemlistselectionmanagertest.cpp
+++ b/dolphin/src/tests/kitemlistselectionmanagertest.cpp
@@ -65,6 +65,8 @@ private slots:
     void testItemsInserted();
     void testItemsRemoved();
     void testAnchoredSelection();
+    void testChangeSelection_data();
+    void testChangeSelection();
 
 private:
     KItemListSelectionManager* m_selectionManager;
@@ -308,6 +310,139 @@ void KItemListSelectionManagerTest::testAnchoredSelection()
     QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << \
10);  }
 
+namespace {
+    enum ModelChangeType {
+        NoChange,
+        InsertItems,
+        RemoveItems
+    };
+}
+
+Q_DECLARE_METATYPE(QSet<int>);
+Q_DECLARE_METATYPE(ModelChangeType);
+Q_DECLARE_METATYPE(KItemRangeList);
+
+void KItemListSelectionManagerTest::testChangeSelection_data()
+{
+    QTest::addColumn<QSet<int> >("initialSelection");
+    QTest::addColumn<int>("anchor");
+    QTest::addColumn<int>("current");
+    QTest::addColumn<QSet<int> >("expectedSelection");
+    QTest::addColumn<ModelChangeType>("changeType");
+    QTest::addColumn<KItemRangeList>("changedItems");
+    QTest::addColumn<QSet<int> >("finalSelection");
+
+    QTest::newRow("No change")
+        << (QSet<int>() << 5 << 6)
+        << 2 << 3
+        << (QSet<int>() << 2 << 3 << 5 << 6)
+        << NoChange << KItemRangeList()
+        << (QSet<int>() << 2 << 3 << 5 << 6);    
+
+    QTest::newRow("Insert Items")
+        << (QSet<int>() << 5 << 6)
+        << 2 << 3
+        << (QSet<int>() << 2 << 3 << 5 << 6)
+        << InsertItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) \
<< KItemRange(10, 5)) +        << (QSet<int>() << 3 << 4 << 8 << 9);
+
+    QTest::newRow("Remove Items")
+        << (QSet<int>() << 5 << 6)
+        << 2 << 3
+        << (QSet<int>() << 2 << 3 << 5 << 6)
+        << RemoveItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) \
<< KItemRange(10, 5)) +        << (QSet<int>() << 1 << 2 << 3 << 4);
+}
+
+void KItemListSelectionManagerTest::testChangeSelection()
+{
+    QFETCH(QSet<int>, initialSelection);
+    QFETCH(int, anchor);
+    QFETCH(int, current);
+    QFETCH(QSet<int> , expectedSelection);
+    QFETCH(ModelChangeType, changeType);
+    QFETCH(KItemRangeList, changedItems);
+    QFETCH(QSet<int> , finalSelection);
+
+    QSignalSpy spySelectionChanged(m_selectionManager, \
SIGNAL(selectionChanged(QSet<int>,QSet<int>))); +
+    // Initial selection should be empty
+    QVERIFY(!m_selectionManager->hasSelection());
+    QVERIFY(m_selectionManager->selectedItems().isEmpty());
+
+    // 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>());
+    }
+
+    // Perform an anchored selection.
+    // Note that current and anchor index are equal first because this is the case \
in typical uses of the +    // selection manager, and because this makes it easier to \
test the correctness of the signal's arguments. +    \
m_selectionManager->setCurrentItem(anchor); +    \
m_selectionManager->beginAnchoredSelection(anchor); +    \
m_selectionManager->setCurrentItem(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);
+    }
+
+    // Change the model by inserting or removing items.
+    switch (changeType) {
+    case InsertItems:
+        m_selectionManager->itemsInserted(changedItems);
+        break;
+    case RemoveItems:
+        m_selectionManager->itemsRemoved(changedItems);
+        break;
+    case NoChange:
+        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);
+    }
+
+    // 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);
+    }
+    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);
+    }
+}
+
 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
 
 #include "kitemlistselectionmanagertest.moc"


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic