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
= 5000;
30 Q_DECLARE_METATYPE(KItemRangeList
)
32 class KFileItemModelTest
: public QObject
40 void testDefaultRoles();
41 void testDefaultSortRole();
42 void testDefaultGroupRole();
44 void testModelConsistencyWhenInsertingItems();
45 void testItemRangeConsistencyWhenInsertingItems();
47 void testExpansionLevelsCompare_data();
48 void testExpansionLevelsCompare();
51 bool isModelConsistent() const;
54 KFileItemModel
* m_model
;
55 KDirLister
* m_dirLister
;
59 void KFileItemModelTest::init()
61 qRegisterMetaType
<KItemRangeList
>("KItemRangeList");
62 qRegisterMetaType
<KFileItemList
>("KFileItemList");
64 m_testDir
= new TestDir();
65 m_dirLister
= new KDirLister();
66 m_model
= new KFileItemModel(m_dirLister
);
69 void KFileItemModelTest::cleanup()
81 void KFileItemModelTest::testDefaultRoles()
83 const QSet
<QByteArray
> roles
= m_model
->roles();
84 QCOMPARE(roles
.count(), 2);
85 QVERIFY(roles
.contains("name"));
86 QVERIFY(roles
.contains("isDir"));
89 void KFileItemModelTest::testDefaultSortRole()
91 QCOMPARE(m_model
->sortRole(), QByteArray("name"));
94 files
<< "c.txt" << "a.txt" << "b.txt";
96 m_testDir
->createFiles(files
);
98 m_dirLister
->openUrl(m_testDir
->url());
99 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
101 QCOMPARE(m_model
->count(), 3);
102 QCOMPARE(m_model
->data(0)["name"].toString(), QString("a.txt"));
103 QCOMPARE(m_model
->data(1)["name"].toString(), QString("b.txt"));
104 QCOMPARE(m_model
->data(2)["name"].toString(), QString("c.txt"));
107 void KFileItemModelTest::testDefaultGroupRole()
109 QVERIFY(m_model
->groupRole().isEmpty());
112 void KFileItemModelTest::testNewItems()
115 files
<< "a.txt" << "b.txt" << "c.txt";
116 m_testDir
->createFiles(files
);
118 m_dirLister
->openUrl(m_testDir
->url());
119 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
121 QCOMPARE(m_model
->count(), 3);
123 QVERIFY(isModelConsistent());
126 void KFileItemModelTest::testModelConsistencyWhenInsertingItems()
128 QSKIP("Temporary disabled", SkipSingle
);
130 // KFileItemModel prevents that inserting a punch of items sequentially
131 // results in an itemsInserted()-signal for each item. Instead internally
132 // a timeout is given that collects such operations and results in only
133 // one itemsInserted()-signal. However in this test we want to stress
134 // KFileItemModel to do a lot of insert operation and hence decrease
135 // the timeout to 1 millisecond.
136 m_model
->m_minimumUpdateIntervalTimer
->setInterval(1);
138 m_testDir
->createFile("1");
139 m_dirLister
->openUrl(m_testDir
->url());
140 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
141 QCOMPARE(m_model
->count(), 1);
143 // Insert 10 items for 20 times. After each insert operation the model consistency
145 QSet
<int> insertedItems
;
146 for (int i
= 0; i
< 20; ++i
) {
147 QSignalSpy
spy(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
149 for (int j
= 0; j
< 10; ++j
) {
150 int itemName
= qrand();
151 while (insertedItems
.contains(itemName
)) {
154 insertedItems
.insert(itemName
);
156 m_testDir
->createFile(QString::number(itemName
));
159 m_dirLister
->updateDirectory(m_testDir
->url());
160 if (spy
.count() == 0) {
161 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
164 QVERIFY(isModelConsistent());
167 QCOMPARE(m_model
->count(), 201);
170 void KFileItemModelTest::testItemRangeConsistencyWhenInsertingItems()
173 files
<< "B" << "E" << "G";
174 m_testDir
->createFiles(files
);
176 // Due to inserting the 3 items one item-range with index == 0 and
177 // count == 3 must be given
178 QSignalSpy
spy1(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
179 m_dirLister
->openUrl(m_testDir
->url());
180 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
182 QCOMPARE(spy1
.count(), 1);
183 QList
<QVariant
> arguments
= spy1
.takeFirst();
184 KItemRangeList itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
185 QCOMPARE(itemRangeList
.count(), 1);
186 KItemRange itemRange
= itemRangeList
.first();
187 QCOMPARE(itemRange
.index
, 0);
188 QCOMPARE(itemRange
.count
, 3);
190 // The indexes of the item-ranges must always be related to the model before
191 // the items have been inserted. Having:
194 // and inserting A, C, D, F the resulting model will be:
197 // and the item-ranges must be:
198 // index: 0, count: 1 for A
199 // index: 1, count: 2 for B, C
200 // index: 2, count: 1 for G
203 files
<< "A" << "C" << "D" << "F";
204 m_testDir
->createFiles(files
);
206 QSignalSpy
spy2(m_model
, SIGNAL(itemsInserted(KItemRangeList
)));
207 m_dirLister
->updateDirectory(m_testDir
->url());
208 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), DefaultTimeout
));
210 QCOMPARE(spy2
.count(), 1);
211 arguments
= spy2
.takeFirst();
212 itemRangeList
= arguments
.at(0).value
<KItemRangeList
>();
213 QCOMPARE(itemRangeList
.count(), 3);
214 QCOMPARE(itemRangeList
.at(0).index
, 0);
215 QCOMPARE(itemRangeList
.at(0).count
, 1);
216 QCOMPARE(itemRangeList
.at(1).index
, 1);
217 QCOMPARE(itemRangeList
.at(1).count
, 2);
218 QCOMPARE(itemRangeList
.at(2).index
, 2);
219 QCOMPARE(itemRangeList
.at(2).count
, 1);
222 void KFileItemModelTest::testExpansionLevelsCompare_data()
224 QTest::addColumn
<QString
>("urlA");
225 QTest::addColumn
<QString
>("urlB");
226 QTest::addColumn
<int>("result");
228 QTest::newRow("Equal") << "/a/b" << "/a/b" << 0;
229 QTest::newRow("Sub path: A < B") << "/a/b" << "/a/b/c" << -1;
230 QTest::newRow("Sub path: A > B") << "/a/b/c" << "/a/b" << +1;
231 QTest::newRow("Same level: /a/1 < /a-1/1") << "/a/1" << "/a-1/1" << -1;
234 void KFileItemModelTest::testExpansionLevelsCompare()
236 QFETCH(QString
, urlA
);
237 QFETCH(QString
, urlB
);
240 const KFileItem
a(KUrl(urlA
), QString(), mode_t(-1));
241 const KFileItem
b(KUrl(urlB
), QString(), mode_t(-1));
242 QCOMPARE(m_model
->expansionLevelsCompare(a
, b
), result
);
245 bool KFileItemModelTest::isModelConsistent() const
247 for (int i
= 0; i
< m_model
->count(); ++i
) {
248 const KFileItem item
= m_model
->fileItem(i
);
250 qWarning() << "Item" << i
<< "is null";
254 const int itemIndex
= m_model
->index(item
);
255 if (itemIndex
!= i
) {
256 qWarning() << "Item" << i
<< "has a wrong index:" << itemIndex
;
264 QTEST_KDEMAIN(KFileItemModelTest
, NoGUI
)
266 #include "kfileitemmodeltest.moc"