]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Keep current item and selection when resorting, part 1
[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->m_anchorItem, -1);
94 }
95
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
97 {
98 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
99
100 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
101 m_selectionManager->setCurrentItem(4);
102 QCOMPARE(m_selectionManager->currentItem(), 4);
103 QCOMPARE(spyCurrent.count(), 1);
104 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 4);
105 spyCurrent.takeFirst();
106
107 // Begin an anchored selection.
108 m_selectionManager->beginAnchoredSelection(5);
109 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
110 QCOMPARE(m_selectionManager->m_anchorItem, 5);
111
112 // Items between current and anchor should be selected now
113 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
114 QVERIFY(m_selectionManager->hasSelection());
115
116 // Change current item again and check the selection
117 m_selectionManager->setCurrentItem(2);
118 QCOMPARE(m_selectionManager->currentItem(), 2);
119 QCOMPARE(spyCurrent.count(), 1);
120 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
121 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 4);
122 spyCurrent.takeFirst();
123
124 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
125 QVERIFY(m_selectionManager->hasSelection());
126
127 // Inserting items should update current item and anchor item.
128 m_selectionManager->itemsInserted(KItemRangeList() <<
129 KItemRange(0, 1) <<
130 KItemRange(2, 2) <<
131 KItemRange(6, 3));
132
133 QCOMPARE(m_selectionManager->currentItem(), 5);
134 QCOMPARE(spyCurrent.count(), 1);
135 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
136 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
137 spyCurrent.takeFirst();
138
139 QCOMPARE(m_selectionManager->m_anchorItem, 8);
140
141 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
142 QVERIFY(m_selectionManager->hasSelection());
143
144 // Removing items should update current item and anchor item.
145 m_selectionManager->itemsRemoved(KItemRangeList() <<
146 KItemRange(0, 2) <<
147 KItemRange(2, 1) <<
148 KItemRange(9, 2));
149
150 QCOMPARE(m_selectionManager->currentItem(), 2);
151 QCOMPARE(spyCurrent.count(), 1);
152 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
153 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
154 spyCurrent.takeFirst();
155
156 QCOMPARE(m_selectionManager->m_anchorItem, 5);
157
158 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
159 QVERIFY(m_selectionManager->hasSelection());
160
161 // Verify that clearSelection() also clears the anchored selection.
162 m_selectionManager->clearSelection();
163 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
164 QVERIFY(!m_selectionManager->hasSelection());
165
166 m_selectionManager->endAnchoredSelection();
167 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
168 }
169
170 void KItemListSelectionManagerTest::testSetSelected_data()
171 {
172 QTest::addColumn<int>("index");
173 QTest::addColumn<int>("count");
174 QTest::addColumn<int>("expectedSelectionCount");
175
176 QTest::newRow("Select all") << 0 << 100 << 100;
177 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
178 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
179 QTest::newRow("Too small index") << -1 << 100 << 0;
180 QTest::newRow("Too large index") << 100 << 100 << 0;
181 QTest::newRow("Too large count") << 0 << 100000 << 100;
182 QTest::newRow("Too small count") << 0 << 0 << 0;
183 }
184
185 void KItemListSelectionManagerTest::testSetSelected()
186 {
187 QFETCH(int, index);
188 QFETCH(int, count);
189 QFETCH(int, expectedSelectionCount);
190 m_selectionManager->setSelected(index, count);
191 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
192 }
193
194 void KItemListSelectionManagerTest::testItemsInserted()
195 {
196 // Select items 10 to 12
197 m_selectionManager->setSelected(10, 3);
198 QSet<int> selectedItems = m_selectionManager->selectedItems();
199 QCOMPARE(selectedItems.count(), 3);
200 QVERIFY(selectedItems.contains(10));
201 QVERIFY(selectedItems.contains(11));
202 QVERIFY(selectedItems.contains(12));
203
204 // Insert items 0 to 4 -> selection must be 15 to 17
205 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 5));
206 selectedItems = m_selectionManager->selectedItems();
207 QCOMPARE(selectedItems.count(), 3);
208 QVERIFY(selectedItems.contains(15));
209 QVERIFY(selectedItems.contains(16));
210 QVERIFY(selectedItems.contains(17));
211
212 // Insert 3 items between the selections
213 m_selectionManager->itemsInserted(KItemRangeList() <<
214 KItemRange(15, 1) <<
215 KItemRange(16, 1) <<
216 KItemRange(17, 1));
217 selectedItems = m_selectionManager->selectedItems();
218 QCOMPARE(selectedItems.count(), 3);
219 QVERIFY(selectedItems.contains(16));
220 QVERIFY(selectedItems.contains(18));
221 QVERIFY(selectedItems.contains(20));
222 }
223
224 void KItemListSelectionManagerTest::testItemsRemoved()
225 {
226 // Select items 10 to 15
227 m_selectionManager->setSelected(10, 6);
228 QSet<int> selectedItems = m_selectionManager->selectedItems();
229 QCOMPARE(selectedItems.count(), 6);
230 for (int i = 10; i <= 15; ++i) {
231 QVERIFY(selectedItems.contains(i));
232 }
233
234 // Remove items 0 to 4 -> selection must be 5 to 10
235 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
236 selectedItems = m_selectionManager->selectedItems();
237 QCOMPARE(selectedItems.count(), 6);
238 for (int i = 5; i <= 10; ++i) {
239 QVERIFY(selectedItems.contains(i));
240 }
241
242 // Remove the items 6 , 8 and 10
243 m_selectionManager->itemsRemoved(KItemRangeList() <<
244 KItemRange(6, 1) <<
245 KItemRange(8, 1) <<
246 KItemRange(10, 1));
247 selectedItems = m_selectionManager->selectedItems();
248 QCOMPARE(selectedItems.count(), 3);
249 QVERIFY(selectedItems.contains(5));
250 QVERIFY(selectedItems.contains(6));
251 QVERIFY(selectedItems.contains(7));
252 }
253
254 void KItemListSelectionManagerTest::testAnchoredSelection()
255 {
256 m_selectionManager->beginAnchoredSelection(5);
257 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
258 QCOMPARE(m_selectionManager->m_anchorItem, 5);
259
260 m_selectionManager->setCurrentItem(6);
261 QCOMPARE(m_selectionManager->currentItem(), 6);
262 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
263
264 m_selectionManager->setCurrentItem(4);
265 QCOMPARE(m_selectionManager->currentItem(), 4);
266 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
267
268 m_selectionManager->setCurrentItem(7);
269 QCOMPARE(m_selectionManager->currentItem(), 7);
270 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
271
272 // Ending the anchored selection should not change the selected items.
273 m_selectionManager->endAnchoredSelection();
274 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
275 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
276
277 // Start a new anchored selection that overlaps the previous one
278 m_selectionManager->beginAnchoredSelection(9);
279 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
280 QCOMPARE(m_selectionManager->m_anchorItem, 9);
281
282 m_selectionManager->setCurrentItem(6);
283 QCOMPARE(m_selectionManager->currentItem(), 6);
284 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
285
286 m_selectionManager->setCurrentItem(10);
287 QCOMPARE(m_selectionManager->currentItem(), 10);
288 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
289
290 m_selectionManager->endAnchoredSelection();
291 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
292 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
293 }
294
295 namespace {
296 enum ChangeType {
297 NoChange,
298 InsertItems,
299 RemoveItems,
300 MoveItems,
301 EndAnchoredSelection,
302 SetSelected
303 };
304 }
305
306 Q_DECLARE_METATYPE(QSet<int>);
307 Q_DECLARE_METATYPE(ChangeType);
308 Q_DECLARE_METATYPE(KItemRange);
309 Q_DECLARE_METATYPE(KItemRangeList);
310 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode);
311 Q_DECLARE_METATYPE(QList<int>);
312
313 /**
314 * The following function provides a generic way to test the selection functionality.
315 *
316 * The test is data-driven and takes the following arguments:
317 *
318 * \param initialSelection The selection at the beginning.
319 * \param anchor This item will be the anchor item.
320 * \param current This item will be the current item.
321 * \param expectedSelection Expected selection after anchor and current are set.
322 * \param changeType Type of the change that is done then:
323 * - NoChange
324 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
325 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
326 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
327 * data.at(1) provides the list containing the new indices
328 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
329 * - EndAnchoredSelection
330 * - SetSelected -> data.at(0) provides the index where the selection process starts,
331 * data.at(1) provides the number of indices to be selected,
332 * data.at(2) provides the selection mode.
333 * \sa KItemListSelectionManager::setSelected()
334 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
335 * \param finalSelection The expected final selection.
336 *
337 */
338
339 void KItemListSelectionManagerTest::testChangeSelection_data()
340 {
341 QTest::addColumn<QSet<int> >("initialSelection");
342 QTest::addColumn<int>("anchor");
343 QTest::addColumn<int>("current");
344 QTest::addColumn<QSet<int> >("expectedSelection");
345 QTest::addColumn<ChangeType>("changeType");
346 QTest::addColumn<QList<QVariant> >("data");
347 QTest::addColumn<QSet<int> >("finalSelection");
348
349 QTest::newRow("No change")
350 << (QSet<int>() << 5 << 6)
351 << 2 << 3
352 << (QSet<int>() << 2 << 3 << 5 << 6)
353 << NoChange
354 << QList<QVariant>()
355 << (QSet<int>() << 2 << 3 << 5 << 6);
356
357 QTest::newRow("Insert Items")
358 << (QSet<int>() << 5 << 6)
359 << 2 << 3
360 << (QSet<int>() << 2 << 3 << 5 << 6)
361 << InsertItems
362 << (QList<QVariant>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5)))
363 << (QSet<int>() << 3 << 4 << 8 << 9);
364
365 QTest::newRow("Remove Items")
366 << (QSet<int>() << 5 << 6)
367 << 2 << 3
368 << (QSet<int>() << 2 << 3 << 5 << 6)
369 << RemoveItems
370 << (QList<QVariant>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5)))
371 << (QSet<int>() << 1 << 2 << 3 << 4);
372
373 QTest::newRow("Empty Anchored Selection")
374 << QSet<int>()
375 << 2 << 2
376 << QSet<int>()
377 << EndAnchoredSelection
378 << QList<QVariant>()
379 << QSet<int>();
380
381 QTest::newRow("Toggle selection")
382 << (QSet<int>() << 1 << 3 << 4)
383 << 6 << 8
384 << (QSet<int>() << 1 << 3 << 4 << 6 << 7 << 8)
385 << SetSelected
386 << (QList<QVariant>() << 0 << 10 << QVariant::fromValue(KItemListSelectionManager::Toggle))
387 << (QSet<int>() << 0 << 2 << 5 << 9);
388
389 // Swap items 2, 3 and 4, 5
390 QTest::newRow("Move items")
391 << (QSet<int>() << 0 << 1 << 2 << 3)
392 << -1 << -1
393 << (QSet<int>() << 0 << 1 << 2 << 3)
394 << MoveItems
395 << (QList<QVariant>() << QVariant::fromValue(KItemRange(2, 4))
396 << QVariant::fromValue(QList<int>() << 4 << 5 << 2 << 3))
397 << (QSet<int>() << 0 << 1 << 4 << 5);
398
399 // Revert sort order
400 QTest::newRow("Revert sort order")
401 << (QSet<int>() << 0 << 1)
402 << 3 << 4
403 << (QSet<int>() << 0 << 1 << 3 << 4)
404 << MoveItems
405 << (QList<QVariant>() << QVariant::fromValue(KItemRange(0, 10))
406 << QVariant::fromValue(QList<int>() << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1 << 0))
407 << (QSet<int>() << 5 << 6 << 8 << 9);
408 }
409
410 void KItemListSelectionManagerTest::testChangeSelection()
411 {
412 QFETCH(QSet<int>, initialSelection);
413 QFETCH(int, anchor);
414 QFETCH(int, current);
415 QFETCH(QSet<int>, expectedSelection);
416 QFETCH(ChangeType, changeType);
417 QFETCH(QList<QVariant>, data);
418 QFETCH(QSet<int>, finalSelection);
419
420 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
421
422 // Initial selection should be empty
423 QVERIFY(!m_selectionManager->hasSelection());
424 QVERIFY(m_selectionManager->selectedItems().isEmpty());
425
426 // Perform the initial selectiion
427 m_selectionManager->setSelectedItems(initialSelection);
428 QCOMPARE(m_selectionManager->selectedItems(), initialSelection);
429 if (initialSelection.isEmpty()) {
430 QVERIFY(!m_selectionManager->hasSelection());
431 QCOMPARE(spySelectionChanged.count(), 0);
432 }
433 else {
434 QVERIFY(m_selectionManager->hasSelection());
435 QCOMPARE(spySelectionChanged.count(), 1);
436 QList<QVariant> arguments = spySelectionChanged.takeFirst();
437 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
438 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
439 }
440
441 // Perform an anchored selection.
442 // Note that current and anchor index are equal first because this is the case in typical uses of the
443 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
444 m_selectionManager->setCurrentItem(anchor);
445 m_selectionManager->beginAnchoredSelection(anchor);
446 m_selectionManager->setCurrentItem(current);
447 QCOMPARE(m_selectionManager->m_anchorItem, anchor);
448 QCOMPARE(m_selectionManager->currentItem(), current);
449 QCOMPARE(m_selectionManager->selectedItems(), expectedSelection);
450 QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty());
451 if (expectedSelection == initialSelection) {
452 QCOMPARE(spySelectionChanged.count(), 0);
453 }
454 else {
455 QCOMPARE(spySelectionChanged.count(), 1);
456 QList<QVariant> arguments = spySelectionChanged.takeFirst();
457 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
458 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), initialSelection);
459 }
460
461 // Change the model by inserting or removing items.
462 switch (changeType) {
463 case InsertItems:
464 m_selectionManager->itemsInserted(data.at(0).value<KItemRangeList>());
465 break;
466 case RemoveItems:
467 m_selectionManager->itemsRemoved(data.at(0).value<KItemRangeList>());
468 break;
469 case MoveItems:
470 m_selectionManager->itemsMoved(data.at(0).value<KItemRange>(),
471 data.at(1).value<QList<int> >());
472 break;
473 case EndAnchoredSelection:
474 m_selectionManager->endAnchoredSelection();
475 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
476 break;
477 case SetSelected:
478 m_selectionManager->setSelected(data.at(0).value<int>(), // index
479 data.at(1).value<int>(), // count
480 data.at(2).value<KItemListSelectionManager::SelectionMode>());
481 break;
482 case NoChange:
483 break;
484 }
485
486 QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
487 QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
488 if (finalSelection == expectedSelection) {
489 QCOMPARE(spySelectionChanged.count(), 0);
490 }
491 else {
492 QCOMPARE(spySelectionChanged.count(), 1);
493 QList<QVariant> arguments = spySelectionChanged.takeFirst();
494 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
495 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
496 }
497
498 // Finally, clear the selection
499 m_selectionManager->clearSelection();
500 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
501 QVERIFY(!m_selectionManager->hasSelection());
502 if (finalSelection.isEmpty()) {
503 // Selection has been empty already
504 QCOMPARE(spySelectionChanged.count(), 0);
505 }
506 else {
507 QCOMPARE(spySelectionChanged.count(), 1);
508 QList<QVariant> arguments = spySelectionChanged.takeFirst();
509 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
510 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
511 }
512 }
513
514 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
515
516 #include "kitemlistselectionmanagertest.moc"