]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Fix smooth-scrolling issue in combination with key-presses
[dolphin.git] / src / tests / kitemlistselectionmanagertest.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/kitemmodelbase.h"
24 #include "kitemviews/kitemlistselectionmanager.h"
25
26 class DummyModel : public KItemModelBase
27 {
28 public:
29 DummyModel();
30 virtual int count() const;
31 virtual QHash<QByteArray, QVariant> data(int index) const;
32 };
33
34 DummyModel::DummyModel() :
35 KItemModelBase()
36 {
37 }
38
39 int DummyModel::count() const
40 {
41 return 100;
42 }
43
44 QHash<QByteArray, QVariant> DummyModel::data(int index) const
45 {
46 Q_UNUSED(index);
47 return QHash<QByteArray, QVariant>();
48 }
49
50
51
52 class KItemListSelectionManagerTest : public QObject
53 {
54 Q_OBJECT
55
56 private slots:
57 void init();
58 void cleanup();
59
60 void testConstructor();
61
62 void testCurrentItemAnchorItem();
63 void testSetSelected_data();
64 void testSetSelected();
65 void testItemsInserted();
66 void testItemsRemoved();
67 void testAnchoredSelection();
68 void testChangeSelection_data();
69 void testChangeSelection();
70
71 private:
72 KItemListSelectionManager* m_selectionManager;
73 };
74
75 void KItemListSelectionManagerTest::init()
76 {
77 m_selectionManager = new KItemListSelectionManager();
78 m_selectionManager->setModel(new DummyModel());
79 }
80
81 void KItemListSelectionManagerTest::cleanup()
82 {
83 delete m_selectionManager->model();
84 delete m_selectionManager;
85 m_selectionManager = 0;
86 }
87
88 void KItemListSelectionManagerTest::testConstructor()
89 {
90 QVERIFY(!m_selectionManager->hasSelection());
91 QCOMPARE(m_selectionManager->selectedItems().count(), 0);
92 QCOMPARE(m_selectionManager->currentItem(), 0);
93 QCOMPARE(m_selectionManager->anchorItem(), -1);
94 }
95
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
97 {
98 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
99 QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
100
101 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
102 m_selectionManager->setCurrentItem(4);
103 QCOMPARE(m_selectionManager->currentItem(), 4);
104 QCOMPARE(spyCurrent.count(), 1);
105 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 4);
106 spyCurrent.takeFirst();
107
108 // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
109 m_selectionManager->beginAnchoredSelection(3);
110 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
111 QCOMPARE(m_selectionManager->anchorItem(), 3);
112 QCOMPARE(spyAnchor.count(), 1);
113 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 3);
114 spyAnchor.takeFirst();
115
116 m_selectionManager->setAnchorItem(5);
117 QCOMPARE(m_selectionManager->anchorItem(), 5);
118 QCOMPARE(spyAnchor.count(), 1);
119 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
120 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
121 spyAnchor.takeFirst();
122
123 // Items between current and anchor should be selected now
124 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
125 QVERIFY(m_selectionManager->hasSelection());
126
127 // Change current item again and check the selection
128 m_selectionManager->setCurrentItem(2);
129 QCOMPARE(m_selectionManager->currentItem(), 2);
130 QCOMPARE(spyCurrent.count(), 1);
131 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
132 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 4);
133 spyCurrent.takeFirst();
134
135 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
136 QVERIFY(m_selectionManager->hasSelection());
137
138 // Inserting items should update current item and anchor item.
139 m_selectionManager->itemsInserted(KItemRangeList() <<
140 KItemRange(0, 1) <<
141 KItemRange(2, 2) <<
142 KItemRange(6, 3));
143
144 QCOMPARE(m_selectionManager->currentItem(), 5);
145 QCOMPARE(spyCurrent.count(), 1);
146 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
147 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
148 spyCurrent.takeFirst();
149
150 QCOMPARE(m_selectionManager->anchorItem(), 8);
151 QCOMPARE(spyAnchor.count(), 1);
152 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
153 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
154 spyAnchor.takeFirst();
155
156 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
157 QVERIFY(m_selectionManager->hasSelection());
158
159 // Removing items should update current item and anchor item.
160 m_selectionManager->itemsRemoved(KItemRangeList() <<
161 KItemRange(0, 2) <<
162 KItemRange(2, 1) <<
163 KItemRange(9, 2));
164
165 QCOMPARE(m_selectionManager->currentItem(), 2);
166 QCOMPARE(spyCurrent.count(), 1);
167 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
168 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
169 spyCurrent.takeFirst();
170
171 QCOMPARE(m_selectionManager->anchorItem(), 5);
172 QCOMPARE(spyAnchor.count(), 1);
173 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
174 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
175 spyAnchor.takeFirst();
176
177 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
178 QVERIFY(m_selectionManager->hasSelection());
179
180 // Verify that clearSelection() also clears the anchored selection.
181 m_selectionManager->clearSelection();
182 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
183 QVERIFY(!m_selectionManager->hasSelection());
184
185 m_selectionManager->endAnchoredSelection();
186 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
187 }
188
189 void KItemListSelectionManagerTest::testSetSelected_data()
190 {
191 QTest::addColumn<int>("index");
192 QTest::addColumn<int>("count");
193 QTest::addColumn<int>("expectedSelectionCount");
194
195 QTest::newRow("Select all") << 0 << 100 << 100;
196 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
197 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
198 QTest::newRow("Too small index") << -1 << 100 << 0;
199 QTest::newRow("Too large index") << 100 << 100 << 0;
200 QTest::newRow("Too large count") << 0 << 100000 << 100;
201 QTest::newRow("Too small count") << 0 << 0 << 0;
202 }
203
204 void KItemListSelectionManagerTest::testSetSelected()
205 {
206 QFETCH(int, index);
207 QFETCH(int, count);
208 QFETCH(int, expectedSelectionCount);
209 m_selectionManager->setSelected(index, count);
210 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
211 }
212
213 void KItemListSelectionManagerTest::testItemsInserted()
214 {
215 // Select items 10 to 12
216 m_selectionManager->setSelected(10, 3);
217 QSet<int> selectedItems = m_selectionManager->selectedItems();
218 QCOMPARE(selectedItems.count(), 3);
219 QVERIFY(selectedItems.contains(10));
220 QVERIFY(selectedItems.contains(11));
221 QVERIFY(selectedItems.contains(12));
222
223 // Insert items 0 to 4 -> selection must be 15 to 17
224 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 5));
225 selectedItems = m_selectionManager->selectedItems();
226 QCOMPARE(selectedItems.count(), 3);
227 QVERIFY(selectedItems.contains(15));
228 QVERIFY(selectedItems.contains(16));
229 QVERIFY(selectedItems.contains(17));
230
231 // Insert 3 items between the selections
232 m_selectionManager->itemsInserted(KItemRangeList() <<
233 KItemRange(15, 1) <<
234 KItemRange(16, 1) <<
235 KItemRange(17, 1));
236 selectedItems = m_selectionManager->selectedItems();
237 QCOMPARE(selectedItems.count(), 3);
238 QVERIFY(selectedItems.contains(16));
239 QVERIFY(selectedItems.contains(18));
240 QVERIFY(selectedItems.contains(20));
241 }
242
243 void KItemListSelectionManagerTest::testItemsRemoved()
244 {
245 // Select items 10 to 15
246 m_selectionManager->setSelected(10, 6);
247 QSet<int> selectedItems = m_selectionManager->selectedItems();
248 QCOMPARE(selectedItems.count(), 6);
249 for (int i = 10; i <= 15; ++i) {
250 QVERIFY(selectedItems.contains(i));
251 }
252
253 // Remove items 0 to 4 -> selection must be 5 to 10
254 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
255 selectedItems = m_selectionManager->selectedItems();
256 QCOMPARE(selectedItems.count(), 6);
257 for (int i = 5; i <= 10; ++i) {
258 QVERIFY(selectedItems.contains(i));
259 }
260
261 // Remove the items 6 , 8 and 10
262 m_selectionManager->itemsRemoved(KItemRangeList() <<
263 KItemRange(6, 1) <<
264 KItemRange(8, 1) <<
265 KItemRange(10, 1));
266 selectedItems = m_selectionManager->selectedItems();
267 QCOMPARE(selectedItems.count(), 3);
268 QVERIFY(selectedItems.contains(5));
269 QVERIFY(selectedItems.contains(6));
270 QVERIFY(selectedItems.contains(7));
271 }
272
273 void KItemListSelectionManagerTest::testAnchoredSelection()
274 {
275 m_selectionManager->beginAnchoredSelection(5);
276 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
277 QCOMPARE(m_selectionManager->anchorItem(), 5);
278
279 m_selectionManager->setCurrentItem(6);
280 QCOMPARE(m_selectionManager->currentItem(), 6);
281 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
282
283 m_selectionManager->setCurrentItem(4);
284 QCOMPARE(m_selectionManager->currentItem(), 4);
285 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
286
287 m_selectionManager->setCurrentItem(7);
288 QCOMPARE(m_selectionManager->currentItem(), 7);
289 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
290
291 // Ending the anchored selection should not change the selected items.
292 m_selectionManager->endAnchoredSelection();
293 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
294 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
295
296 // Start a new anchored selection that overlaps the previous one
297 m_selectionManager->beginAnchoredSelection(9);
298 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
299 QCOMPARE(m_selectionManager->anchorItem(), 9);
300
301 m_selectionManager->setCurrentItem(6);
302 QCOMPARE(m_selectionManager->currentItem(), 6);
303 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
304
305 m_selectionManager->setCurrentItem(10);
306 QCOMPARE(m_selectionManager->currentItem(), 10);
307 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
308
309 m_selectionManager->endAnchoredSelection();
310 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
311 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
312 }
313
314 namespace {
315 enum ChangeType {
316 NoChange,
317 InsertItems,
318 RemoveItems,
319 EndAnchoredSelection,
320 ToggleSelected
321 };
322 }
323
324 Q_DECLARE_METATYPE(QSet<int>);
325 Q_DECLARE_METATYPE(ChangeType);
326 Q_DECLARE_METATYPE(KItemRangeList);
327
328 void KItemListSelectionManagerTest::testChangeSelection_data()
329 {
330 QTest::addColumn<QSet<int> >("initialSelection");
331 QTest::addColumn<int>("anchor");
332 QTest::addColumn<int>("current");
333 QTest::addColumn<QSet<int> >("expectedSelection");
334 QTest::addColumn<ChangeType>("changeType");
335 QTest::addColumn<KItemRangeList>("changedItems");
336 QTest::addColumn<QSet<int> >("finalSelection");
337
338 QTest::newRow("No change")
339 << (QSet<int>() << 5 << 6)
340 << 2 << 3
341 << (QSet<int>() << 2 << 3 << 5 << 6)
342 << NoChange << KItemRangeList()
343 << (QSet<int>() << 2 << 3 << 5 << 6);
344
345 QTest::newRow("Insert Items")
346 << (QSet<int>() << 5 << 6)
347 << 2 << 3
348 << (QSet<int>() << 2 << 3 << 5 << 6)
349 << InsertItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))
350 << (QSet<int>() << 3 << 4 << 8 << 9);
351
352 QTest::newRow("Remove Items")
353 << (QSet<int>() << 5 << 6)
354 << 2 << 3
355 << (QSet<int>() << 2 << 3 << 5 << 6)
356 << RemoveItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))
357 << (QSet<int>() << 1 << 2 << 3 << 4);
358
359 QTest::newRow("Empty Anchored Selection")
360 << QSet<int>()
361 << 2 << 2
362 << QSet<int>()
363 << EndAnchoredSelection << KItemRangeList()
364 << QSet<int>();
365
366 QTest::newRow("Toggle selection")
367 << (QSet<int>() << 1 << 3 << 4)
368 << 6 << 8
369 << (QSet<int>() << 1 << 3 << 4 << 6 << 7 << 8)
370 << ToggleSelected << (KItemRangeList() << KItemRange(0, 10))
371 << (QSet<int>() << 0 << 2 << 5 << 9);
372 }
373
374 void KItemListSelectionManagerTest::testChangeSelection()
375 {
376 QFETCH(QSet<int>, initialSelection);
377 QFETCH(int, anchor);
378 QFETCH(int, current);
379 QFETCH(QSet<int> , expectedSelection);
380 QFETCH(ChangeType, changeType);
381 QFETCH(KItemRangeList, changedItems);
382 QFETCH(QSet<int> , finalSelection);
383
384 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
385
386 // Initial selection should be empty
387 QVERIFY(!m_selectionManager->hasSelection());
388 QVERIFY(m_selectionManager->selectedItems().isEmpty());
389
390 // Perform the initial selectiion
391 m_selectionManager->setSelectedItems(initialSelection);
392 QCOMPARE(m_selectionManager->selectedItems(), initialSelection);
393 if (initialSelection.isEmpty()) {
394 QVERIFY(!m_selectionManager->hasSelection());
395 QCOMPARE(spySelectionChanged.count(), 0);
396 }
397 else {
398 QVERIFY(m_selectionManager->hasSelection());
399 QCOMPARE(spySelectionChanged.count(), 1);
400 QList<QVariant> arguments = spySelectionChanged.takeFirst();
401 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
402 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
403 }
404
405 // Perform an anchored selection.
406 // Note that current and anchor index are equal first because this is the case in typical uses of the
407 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
408 m_selectionManager->setCurrentItem(anchor);
409 m_selectionManager->beginAnchoredSelection(anchor);
410 m_selectionManager->setCurrentItem(current);
411 QCOMPARE(m_selectionManager->selectedItems(), expectedSelection);
412 QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty());
413 if (expectedSelection == initialSelection) {
414 QCOMPARE(spySelectionChanged.count(), 0);
415 }
416 else {
417 QCOMPARE(spySelectionChanged.count(), 1);
418 QList<QVariant> arguments = spySelectionChanged.takeFirst();
419 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
420 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), initialSelection);
421 }
422
423 // Change the model by inserting or removing items.
424 switch (changeType) {
425 case InsertItems:
426 m_selectionManager->itemsInserted(changedItems);
427 break;
428 case RemoveItems:
429 m_selectionManager->itemsRemoved(changedItems);
430 break;
431 case EndAnchoredSelection:
432 m_selectionManager->endAnchoredSelection();
433 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
434 break;
435 case ToggleSelected:
436 foreach(const KItemRange& range, changedItems) {
437 m_selectionManager->setSelected(range.index, range.count, KItemListSelectionManager::Toggle);
438 }
439 break;
440 case NoChange:
441 break;
442 }
443
444 QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
445 QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
446 if (finalSelection == expectedSelection) {
447 QCOMPARE(spySelectionChanged.count(), 0);
448 }
449 else {
450 QCOMPARE(spySelectionChanged.count(), 1);
451 QList<QVariant> arguments = spySelectionChanged.takeFirst();
452 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
453 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
454 }
455
456 // Finally, clear the selection
457 m_selectionManager->clearSelection();
458 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
459 QVERIFY(!m_selectionManager->hasSelection());
460 if (finalSelection.isEmpty()) {
461 // Selection has been empty already
462 QCOMPARE(spySelectionChanged.count(), 0);
463 }
464 else {
465 QCOMPARE(spySelectionChanged.count(), 1);
466 QList<QVariant> arguments = spySelectionChanged.takeFirst();
467 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
468 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
469 }
470 }
471
472 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
473
474 #include "kitemlistselectionmanagertest.moc"