]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Create unit-test for KStandardItemModel
authorPeter Penz <peter.penz19@gmail.com>
Thu, 17 May 2012 19:59:51 +0000 (21:59 +0200)
committerPeter Penz <peter.penz19@gmail.com>
Thu, 17 May 2012 20:00:39 +0000 (22:00 +0200)
src/kitemviews/kfileitemmodel.h
src/kitemviews/kstandarditemmodel.cpp
src/kitemviews/kstandarditemmodel.h
src/tests/CMakeLists.txt
src/tests/kstandarditemmodeltest.cpp [new file with mode: 0644]

index 2bbdfc7972122ff1a66740626be422106979ccb4..783a0fecbf1b68b42c27c3304443b63be0a1bd10 100644 (file)
@@ -459,8 +459,8 @@ private:
 
     friend class KFileItemModelSortAlgorithm;  // Accesses lessThan() method
     friend class KFileItemModelRolesUpdater;   // Accesses emitSortProgress() method
-    friend class KFileItemModelTest;           // For unit testing (accesses m_dirLister)
-    friend class KFileItemListViewTest;        // For unit testing (accesses m_dirLister)
+    friend class KFileItemModelTest;           // For unit testing
+    friend class KFileItemListViewTest;        // For unit testing
 };
 
 inline bool KFileItemModel::isChildItem(int index) const
index 11d72a4ac443d64e4821d31e66602fa604fee252..cde86af49ee1e6647e5826c3c5ffde6b0b99bf7b 100644 (file)
@@ -38,6 +38,11 @@ KStandardItemModel::~KStandardItemModel()
 
 void KStandardItemModel::insertItem(int index, KStandardItem* item)
 {
+    if (index < 0 || index > count() || !item) {
+        delete item;
+        return;
+    }
+
     if (item && !m_indexesForItems.contains(item)) {
         item->m_model = this;
         m_items.insert(index, item);
@@ -58,40 +63,39 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item)
 
 void KStandardItemModel::changeItem(int index, KStandardItem* item)
 {
-    if (item && index >= 0 && index < count()) {
-        item->m_model = this;
+    if (index < 0 || index > count() || !item) {
+        delete item;
+        return;
+    }
 
-        QSet<QByteArray> changedRoles;
-
-        KStandardItem* oldItem = m_items[index];
-        const QHash<QByteArray, QVariant> oldData = oldItem->data();
-        const QHash<QByteArray, QVariant> newData = item->data();
-
-        // Determine which roles have been changed
-        QHashIterator<QByteArray, QVariant> it(oldData);
-        while (it.hasNext()) {
-            it.next();
-            const QByteArray role = it.key();
-            const QVariant oldValue = it.value();
-            if (newData.contains(role) && newData.value(role) != oldValue) {
-                changedRoles.insert(role);
-            }
-        }
+    item->m_model = this;
 
-        m_indexesForItems.remove(oldItem);
-        delete oldItem;
-        oldItem = 0;
+    QSet<QByteArray> changedRoles;
 
-        m_items[index] = item;
-        m_indexesForItems.insert(item, index);
+    KStandardItem* oldItem = m_items[index];
+    const QHash<QByteArray, QVariant> oldData = oldItem->data();
+    const QHash<QByteArray, QVariant> newData = item->data();
 
-        onItemChanged(index, changedRoles);
-        emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
-    } else {
-        kWarning() << "No item available to replace on the given index" << index;
-        delete item;
-        item = 0;
+    // Determine which roles have been changed
+    QHashIterator<QByteArray, QVariant> it(oldData);
+    while (it.hasNext()) {
+        it.next();
+        const QByteArray role = it.key();
+        const QVariant oldValue = it.value();
+        if (newData.contains(role) && newData.value(role) != oldValue) {
+            changedRoles.insert(role);
+        }
     }
+
+    m_indexesForItems.remove(oldItem);
+    delete oldItem;
+    oldItem = 0;
+
+    m_items[index] = item;
+    m_indexesForItems.insert(item, index);
+
+    onItemChanged(index, changedRoles);
+    emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
 }
 
 void KStandardItemModel::removeItem(int index)
index 047c1e9b685e22d19391d946e1e5f697b72f8a42..34032bf07319fde4814612e3f0a976664dee623b 100644 (file)
@@ -47,14 +47,16 @@ public:
      * Inserts the item \a item at the index \a index. If the index
      * is equal to the number of items of the model, the item
      * gets appended as last element. KStandardItemModel takes
-     * the ownership of the item.
+     * the ownership of the item. If the index is invalid, the item
+     * gets deleted.
      */
     void insertItem(int index, KStandardItem* item);
 
     /**
      * Changes the item on the index \a index to \a item.
      * KStandardItemModel takes the ownership of the item. The
-     * old item gets deleted.
+     * old item gets deleted. If the index is invalid, the item
+     * gets deleted.
      */
     void changeItem(int index, KStandardItem* item);
 
@@ -102,6 +104,7 @@ private:
     QHash<const KStandardItem*, int> m_indexesForItems;
 
     friend class KStandardItem;
+    friend class KStandardItemModelTest;  // For unit testing
 };
 
 #endif
index 830138e74b4198d95434d107ff0e99c22fbb8de0..e5d3d6468ab0dd05101020238f158fa8ed27d4e4 100644 (file)
@@ -68,3 +68,14 @@ if (Nepomuk_FOUND)
   kde4_add_unit_test(dolphinsearchboxtest TEST ${dolphinsearchboxtest_SRCS})
   target_link_libraries(dolphinsearchboxtest ${KDE4_KIO_LIBS} ${SOPRANO_LIBRARIES} ${NEPOMUK_LIBRARIES} ${NEPOMUK_QUERY_LIBRARIES} nepomukutils ${QT_QTTEST_LIBRARY})
 endif (Nepomuk_FOUND)
+
+# KStandardItemModelTest
+set(kstandarditemmodeltest_SRCS
+    kstandarditemmodeltest.cpp
+    ../kitemviews/kstandarditem.cpp
+    ../kitemviews/kstandarditemmodel.cpp
+    ../kitemviews/kitemmodelbase.cpp
+)
+kde4_add_unit_test(kstandarditemmodeltest TEST ${kstandarditemmodeltest_SRCS})
+target_link_libraries(kstandarditemmodeltest dolphinprivate ${KDE4_KIO_LIBS} ${QT_QTTEST_LIBRARY})
+
diff --git a/src/tests/kstandarditemmodeltest.cpp b/src/tests/kstandarditemmodeltest.cpp
new file mode 100644 (file)
index 0000000..2de0d0b
--- /dev/null
@@ -0,0 +1,117 @@
+/***************************************************************************
+ *   Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com>    *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include <qtest_kde.h>
+
+#include "kitemviews/kstandarditem.h"
+#include "kitemviews/kstandarditemmodel.h"
+
+class KStandardItemModelTest : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    void init();
+    void cleanup();
+
+    void testNewItems();
+    void testRemoveItems();
+
+private:
+    bool isModelConsistent() const;
+
+private:
+    KStandardItemModel* m_model;
+};
+
+void KStandardItemModelTest::init()
+{
+    m_model = new KStandardItemModel();
+}
+
+void KStandardItemModelTest::cleanup()
+{
+    delete m_model;
+    m_model = 0;
+}
+
+void KStandardItemModelTest::testNewItems()
+{
+    m_model->insertItem(0, new KStandardItem("item 1"));
+    m_model->insertItem(0, new KStandardItem("item 2"));
+    m_model->insertItem(2, new KStandardItem("item 3"));
+    m_model->appendItem(new KStandardItem("item 4"));
+    m_model->insertItem(-1, new KStandardItem("invalid 1"));
+    m_model->insertItem(5, new KStandardItem("invalid 2"));
+    QCOMPARE(m_model->count(), 4);
+    QCOMPARE(m_model->item(0)->text(), QString("item 2"));
+    QCOMPARE(m_model->item(1)->text(), QString("item 1"));
+    QCOMPARE(m_model->item(2)->text(), QString("item 3"));
+    QCOMPARE(m_model->item(3)->text(), QString("item 4"));
+
+    QVERIFY(isModelConsistent());
+}
+
+void KStandardItemModelTest::testRemoveItems()
+{
+    for (int i = 1; i <= 10; ++i) {
+        m_model->appendItem(new KStandardItem("item " + QString::number(i)));
+    }
+
+    m_model->removeItem(0);
+    m_model->removeItem(3);
+    m_model->removeItem(5);
+    m_model->removeItem(-1);
+    QCOMPARE(m_model->count(), 7);
+    QCOMPARE(m_model->item(0)->text(), QString("item 2"));
+    QCOMPARE(m_model->item(1)->text(), QString("item 3"));
+    QCOMPARE(m_model->item(2)->text(), QString("item 4"));
+    QCOMPARE(m_model->item(3)->text(), QString("item 6"));
+    QCOMPARE(m_model->item(4)->text(), QString("item 7"));
+    QCOMPARE(m_model->item(5)->text(), QString("item 9"));
+    QCOMPARE(m_model->item(6)->text(), QString("item 10"));
+}
+
+bool KStandardItemModelTest::isModelConsistent() const
+{
+    if (m_model->m_items.count() != m_model->m_indexesForItems.count()) {
+        return false;
+    }
+
+    for (int i = 0; i < m_model->count(); ++i) {
+        const KStandardItem* item = m_model->item(i);
+        if (!item) {
+            qWarning() << "Item" << i << "is null";
+            return false;
+        }
+
+        const int itemIndex = m_model->index(item);
+        if (itemIndex != i) {
+            qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
+            return false;
+        }
+    }
+
+    return true;
+}
+
+QTEST_KDEMAIN(KStandardItemModelTest, NoGUI)
+
+#include "kstandarditemmodeltest.moc"