]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kstandarditemmodeltest.cpp
daa8f1e56ceccee72506ddb895bf46c5e6451df7
[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 "dolphindebug.h"
22
23 #include "kitemviews/kstandarditem.h"
24 #include "kitemviews/kstandarditemmodel.h"
25
26 #include <QTest>
27
28 class KStandardItemModelTest : public QObject
29 {
30 Q_OBJECT
31
32 private slots:
33 void init();
34 void cleanup();
35
36 void testNewItems();
37 void testRemoveItems();
38
39 private:
40 bool isModelConsistent() const;
41
42 private:
43 KStandardItemModel* m_model;
44 };
45
46 void KStandardItemModelTest::init()
47 {
48 m_model = new KStandardItemModel();
49 }
50
51 void KStandardItemModelTest::cleanup()
52 {
53 delete m_model;
54 m_model = nullptr;
55 }
56
57 void KStandardItemModelTest::testNewItems()
58 {
59 m_model->insertItem(0, new KStandardItem("item 1"));
60 m_model->insertItem(0, new KStandardItem("item 2"));
61 m_model->insertItem(2, new KStandardItem("item 3"));
62 m_model->appendItem(new KStandardItem("item 4"));
63 m_model->insertItem(-1, new KStandardItem("invalid 1"));
64 m_model->insertItem(5, new KStandardItem("invalid 2"));
65 QCOMPARE(m_model->count(), 4);
66 QCOMPARE(m_model->item(0)->text(), QString("item 2"));
67 QCOMPARE(m_model->item(1)->text(), QString("item 1"));
68 QCOMPARE(m_model->item(2)->text(), QString("item 3"));
69 QCOMPARE(m_model->item(3)->text(), QString("item 4"));
70
71 QVERIFY(isModelConsistent());
72 }
73
74 void KStandardItemModelTest::testRemoveItems()
75 {
76 for (int i = 1; i <= 10; ++i) {
77 m_model->appendItem(new KStandardItem("item " + QString::number(i)));
78 }
79
80 m_model->removeItem(0);
81 m_model->removeItem(3);
82 m_model->removeItem(5);
83 m_model->removeItem(-1);
84 QCOMPARE(m_model->count(), 7);
85 QCOMPARE(m_model->item(0)->text(), QString("item 2"));
86 QCOMPARE(m_model->item(1)->text(), QString("item 3"));
87 QCOMPARE(m_model->item(2)->text(), QString("item 4"));
88 QCOMPARE(m_model->item(3)->text(), QString("item 6"));
89 QCOMPARE(m_model->item(4)->text(), QString("item 7"));
90 QCOMPARE(m_model->item(5)->text(), QString("item 9"));
91 QCOMPARE(m_model->item(6)->text(), QString("item 10"));
92 }
93
94 bool KStandardItemModelTest::isModelConsistent() const
95 {
96 if (m_model->m_items.count() != m_model->m_indexesForItems.count()) {
97 return false;
98 }
99
100 for (int i = 0; i < m_model->count(); ++i) {
101 const KStandardItem* item = m_model->item(i);
102 if (!item) {
103 qCWarning(DolphinDebug) << "Item" << i << "is null";
104 return false;
105 }
106
107 const int itemIndex = m_model->index(item);
108 if (itemIndex != i) {
109 qCWarning(DolphinDebug) << "Item" << i << "has a wrong index:" << itemIndex;
110 return false;
111 }
112 }
113
114 return true;
115 }
116
117 QTEST_GUILESS_MAIN(KStandardItemModelTest)
118
119 #include "kstandarditemmodeltest.moc"