]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kfileitemmodeltest.cpp
Improve performance for creating previews
[dolphin.git] / src / tests / kfileitemmodeltest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include <qtest_kde.h>
21
22 #include <KDirLister>
23 #include "kitemviews/kfileitemmodel.h"
24 #include "testdir.h"
25
26 namespace {
27 const int DefaultTimeout = 2000;
28 };
29
30 class KFileItemModelTest : public QObject
31 {
32 Q_OBJECT
33
34 private slots:
35 void init();
36 void cleanup();
37
38 void testDefaultRoles();
39 void testDefaultSortRole();
40 void testDefaultGroupRole();
41 void testNewItems();
42 void testInsertingItems();
43
44 void testExpansionLevelsCompare_data();
45 void testExpansionLevelsCompare();
46
47 private:
48 bool isModelConsistent() const;
49
50 private:
51 KFileItemModel* m_model;
52 KDirLister* m_dirLister;
53 TestDir* m_testDir;
54 };
55
56 void KFileItemModelTest::init()
57 {
58 qRegisterMetaType<KItemRangeList>("KItemRangeList");
59 qRegisterMetaType<KFileItemList>("KFileItemList");
60
61 m_testDir = new TestDir();
62 m_dirLister = new KDirLister();
63 m_model = new KFileItemModel(m_dirLister);
64 }
65
66 void KFileItemModelTest::cleanup()
67 {
68 delete m_model;
69 m_model = 0;
70
71 delete m_dirLister;
72 m_dirLister = 0;
73
74 delete m_testDir;
75 m_testDir = 0;
76 }
77
78 void KFileItemModelTest::testDefaultRoles()
79 {
80 const QSet<QByteArray> roles = m_model->roles();
81 QCOMPARE(roles.count(), 2);
82 QVERIFY(roles.contains("name"));
83 QVERIFY(roles.contains("isDir"));
84 }
85
86 void KFileItemModelTest::testDefaultSortRole()
87 {
88 QCOMPARE(m_model->sortRole(), QByteArray("name"));
89
90 QStringList files;
91 files << "c.txt" << "a.txt" << "b.txt";
92
93 m_testDir->createFiles(files);
94
95 m_dirLister->openUrl(m_testDir->url());
96 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
97
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"));
102 }
103
104 void KFileItemModelTest::testDefaultGroupRole()
105 {
106 QVERIFY(m_model->groupRole().isEmpty());
107 }
108
109 void KFileItemModelTest::testNewItems()
110 {
111 QStringList files;
112 files << "a.txt" << "b.txt" << "c.txt";
113 m_testDir->createFiles(files);
114
115 m_dirLister->openUrl(m_testDir->url());
116 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
117
118 QCOMPARE(m_model->count(), 3);
119
120 QVERIFY(isModelConsistent());
121 }
122
123 void KFileItemModelTest::testInsertingItems()
124 {
125 // QSKIP("Temporary disabled", SkipSingle);
126
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);
134
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);
139
140 // Insert 10 items for 20 times. After each insert operation the model consistency
141 // is checked.
142 QSet<int> insertedItems;
143 for (int i = 0; i < 20; ++i) {
144 QSignalSpy spy(m_model, SIGNAL(itemsInserted(KItemRangeList)));
145
146 for (int j = 0; j < 10; ++j) {
147 int itemName = qrand();
148 while (insertedItems.contains(itemName)) {
149 itemName = qrand();
150 }
151 insertedItems.insert(itemName);
152
153 m_testDir->createFile(QString::number(itemName));
154 }
155
156 m_dirLister->updateDirectory(m_testDir->url());
157 if (spy.count() == 0) {
158 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(itemsInserted(KItemRangeList)), DefaultTimeout));
159 }
160
161 QVERIFY(isModelConsistent());
162 }
163
164 QCOMPARE(m_model->count(), 201);
165 }
166
167 void KFileItemModelTest::testExpansionLevelsCompare_data()
168 {
169 QTest::addColumn<QString>("urlA");
170 QTest::addColumn<QString>("urlB");
171 QTest::addColumn<int>("result");
172
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;
176 }
177
178 void KFileItemModelTest::testExpansionLevelsCompare()
179 {
180 QFETCH(QString, urlA);
181 QFETCH(QString, urlB);
182 QFETCH(int, result);
183
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);
187 }
188
189 bool KFileItemModelTest::isModelConsistent() const
190 {
191 for (int i = 0; i < m_model->count(); ++i) {
192 const KFileItem item = m_model->fileItem(i);
193 if (item.isNull()) {
194 qWarning() << "Item" << i << "is null";
195 return false;
196 }
197
198 const int itemIndex = m_model->index(item);
199 if (itemIndex != i) {
200 qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
201 return false;
202 }
203 }
204
205 return true;
206 }
207
208 QTEST_KDEMAIN(KFileItemModelTest, NoGUI)
209
210 #include "kfileitemmodeltest.moc"