]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kstandarditemmodeltest.cpp
2de0d0b784368aa3f9b7e0acb85993c40a046de4
[dolphin.git] / src / tests / kstandarditemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include <qtest_kde.h>
22
23 #include "kitemviews/kstandarditem.h"
24 #include "kitemviews/kstandarditemmodel.h"
25
26 class KStandardItemModelTest : public QObject
27 {
28 Q_OBJECT
29
30 private slots:
31 void init();
32 void cleanup();
33
34 void testNewItems();
35 void testRemoveItems();
36
37 private:
38 bool isModelConsistent() const;
39
40 private:
41 KStandardItemModel* m_model;
42 };
43
44 void KStandardItemModelTest::init()
45 {
46 m_model = new KStandardItemModel();
47 }
48
49 void KStandardItemModelTest::cleanup()
50 {
51 delete m_model;
52 m_model = 0;
53 }
54
55 void KStandardItemModelTest::testNewItems()
56 {
57 m_model->insertItem(0, new KStandardItem("item 1"));
58 m_model->insertItem(0, new KStandardItem("item 2"));
59 m_model->insertItem(2, new KStandardItem("item 3"));
60 m_model->appendItem(new KStandardItem("item 4"));
61 m_model->insertItem(-1, new KStandardItem("invalid 1"));
62 m_model->insertItem(5, new KStandardItem("invalid 2"));
63 QCOMPARE(m_model->count(), 4);
64 QCOMPARE(m_model->item(0)->text(), QString("item 2"));
65 QCOMPARE(m_model->item(1)->text(), QString("item 1"));
66 QCOMPARE(m_model->item(2)->text(), QString("item 3"));
67 QCOMPARE(m_model->item(3)->text(), QString("item 4"));
68
69 QVERIFY(isModelConsistent());
70 }
71
72 void KStandardItemModelTest::testRemoveItems()
73 {
74 for (int i = 1; i <= 10; ++i) {
75 m_model->appendItem(new KStandardItem("item " + QString::number(i)));
76 }
77
78 m_model->removeItem(0);
79 m_model->removeItem(3);
80 m_model->removeItem(5);
81 m_model->removeItem(-1);
82 QCOMPARE(m_model->count(), 7);
83 QCOMPARE(m_model->item(0)->text(), QString("item 2"));
84 QCOMPARE(m_model->item(1)->text(), QString("item 3"));
85 QCOMPARE(m_model->item(2)->text(), QString("item 4"));
86 QCOMPARE(m_model->item(3)->text(), QString("item 6"));
87 QCOMPARE(m_model->item(4)->text(), QString("item 7"));
88 QCOMPARE(m_model->item(5)->text(), QString("item 9"));
89 QCOMPARE(m_model->item(6)->text(), QString("item 10"));
90 }
91
92 bool KStandardItemModelTest::isModelConsistent() const
93 {
94 if (m_model->m_items.count() != m_model->m_indexesForItems.count()) {
95 return false;
96 }
97
98 for (int i = 0; i < m_model->count(); ++i) {
99 const KStandardItem* item = m_model->item(i);
100 if (!item) {
101 qWarning() << "Item" << i << "is null";
102 return false;
103 }
104
105 const int itemIndex = m_model->index(item);
106 if (itemIndex != i) {
107 qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
108 return false;
109 }
110 }
111
112 return true;
113 }
114
115 QTEST_KDEMAIN(KStandardItemModelTest, NoGUI)
116
117 #include "kstandarditemmodeltest.moc"