1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include <qtest_kde.h>
23 #include "kitemviews/kfileitemmodel.h"
27 const int DefaultTimeout
= 2000;
30 class KFileItemModelTest
: public QObject
38 void testDefaultRoles();
39 void testDefaultSortRole();
40 void testDefaultGroupRole();
42 void testInsertingItems();
44 void testExpansionLevelsCompare_data();
45 void testExpansionLevelsCompare();
48 bool isModelConsistent() const;
51 KFileItemModel
* m_model
;
52 KDirLister
* m_dirLister
;
56 void KFileItemModelTest::init()
58 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
59 qRegisterMetaType
<KFileItemList
>("KFileItemList");
61 m_testDir
= new TestDir();
62 m_dirLister
= new KDirLister();
63 m_model
= new KFileItemModel(m_dirLister
);
66 void KFileItemModelTest::cleanup()
78 void KFileItemModelTest::testDefaultRoles()
80 const QSet
<QByteArray
> roles
= m_model
->roles();
81 QCOMPARE(roles
.count(), 2);
82 QVERIFY(roles
.contains("name"));
83 QVERIFY(roles
.contains("isDir"));
86 void KFileItemModelTest::testDefaultSortRole()
88 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
91 files
<< "c.txt" << "a.txt" << "b.txt";
93 m_testDir
->createFiles(files
);
95 m_dirLister
->openUrl(m_testDir
->url());
96 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
98 QCOMPARE(m_model
->count(), 3);
99 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
100 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
101 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
104 void KFileItemModelTest::testDefaultGroupRole()
106 QVERIFY(m_model
->groupRole().isEmpty());
109 void KFileItemModelTest::testNewItems()
112 files
<< "a.txt" << "b.txt" << "c.txt";
113 m_testDir
->createFiles(files
);
115 m_dirLister
->openUrl(m_testDir
->url());
116 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
118 QCOMPARE(m_model
->count(), 3);
120 QVERIFY(isModelConsistent());
123 void KFileItemModelTest::testInsertingItems()
125 // QSKIP("Temporary disabled", SkipSingle);
127 // KFileItemModel prevents that inserting a punch of items sequentially
128 // results in an itemsInserted()-signal for each item. Instead internally
129 // a timeout is given that collects such operations and results in only
130 // one itemsInserted()-signal. However in this test we want to stress
131 // KFileItemModel to do a lot of insert operation and hence decrease
132 // the timeout to 1 millisecond.
133 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
135 m_testDir
->createFile("1");
136 m_dirLister
->openUrl(m_testDir
->url());
137 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
138 QCOMPARE(m_model
->count(), 1);
140 // Insert 10 items for 20 times. After each insert operation the model consistency
142 QSet
<int> insertedItems
;
143 for (int i
= 0; i
< 20; ++i
) {
144 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
146 for (int j
= 0; j
< 10; ++j
) {
147 int itemName
= qrand();
148 while (insertedItems
.contains(itemName
)) {
151 insertedItems
.insert(itemName
);
153 m_testDir
->createFile(QString::number(itemName
));
156 m_dirLister
->updateDirectory(m_testDir
->url());
157 if (spy
.count() == 0) {
158 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
161 QVERIFY(isModelConsistent());
164 QCOMPARE(m_model
->count(), 201);
167 void KFileItemModelTest::testExpansionLevelsCompare_data()
169 QTest::addColumn
<QString
>("urlA");
170 QTest::addColumn
<QString
>("urlB");
171 QTest::addColumn
<int>("result");
173 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
174 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
175 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
178 void KFileItemModelTest::testExpansionLevelsCompare()
180 QFETCH(QString
, urlA
);
181 QFETCH(QString
, urlB
);
184 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
185 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
186 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
189 bool KFileItemModelTest::isModelConsistent() const
191 for (int i
= 0; i
< m_model
->count(); ++i
) {
192 const KFileItem item
= m_model
->fileItem(i
);
194 qWarning() << "Item" << i
<< "is null";
198 const int itemIndex
= m_model
->index(item
);
199 if (itemIndex
!= i
) {
200 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
208 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
210 #include "kfileitemmodeltest.moc"