1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
21 #include "kitemviews/kitemmodelbase.h"
22 #include "kitemviews/kitemlistselectionmanager.h"
27 class DummyModel
: public KItemModelBase
32 void setCount(int count
);
33 int count() const Q_DECL_OVERRIDE
;
34 QHash
<QByteArray
, QVariant
> data(int index
) const Q_DECL_OVERRIDE
;
40 DummyModel::DummyModel() :
46 void DummyModel::setCount(int count
)
51 int DummyModel::count() const
56 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
59 return QHash
<QByteArray
, QVariant
>();
63 class KItemListSelectionManagerTest
: public QObject
71 void testConstructor();
73 void testCurrentItemAnchorItem();
74 void testSetSelected_data();
75 void testSetSelected();
76 void testItemsInserted();
77 void testItemsRemoved();
78 void testAnchoredSelection();
79 void testChangeSelection_data();
80 void testChangeSelection();
81 void testDeleteCurrentItem_data();
82 void testDeleteCurrentItem();
83 void testAnchoredSelectionAfterMovingItems();
86 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
88 KItemListSelectionManager
* m_selectionManager
;
92 void KItemListSelectionManagerTest::init()
94 m_model
= new DummyModel();
95 m_selectionManager
= new KItemListSelectionManager();
96 m_selectionManager
->setModel(m_model
);
99 void KItemListSelectionManagerTest::cleanup()
101 delete m_selectionManager
;
102 m_selectionManager
= 0;
108 void KItemListSelectionManagerTest::testConstructor()
110 QVERIFY(!m_selectionManager
->hasSelection());
111 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
112 QCOMPARE(m_selectionManager
->currentItem(), 0);
113 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
116 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
118 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
120 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
121 m_selectionManager
->setCurrentItem(4);
122 QCOMPARE(m_selectionManager
->currentItem(), 4);
123 QCOMPARE(spyCurrent
.count(), 1);
124 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
125 spyCurrent
.takeFirst();
127 // Begin an anchored selection.
128 m_selectionManager
->beginAnchoredSelection(5);
129 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
130 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
132 // Items between current and anchor should be selected now
133 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
134 QVERIFY(m_selectionManager
->hasSelection());
136 // Change current item again and check the selection
137 m_selectionManager
->setCurrentItem(2);
138 QCOMPARE(m_selectionManager
->currentItem(), 2);
139 QCOMPARE(spyCurrent
.count(), 1);
140 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
141 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
142 spyCurrent
.takeFirst();
144 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
145 QVERIFY(m_selectionManager
->hasSelection());
147 // Inserting items should update current item and anchor item.
148 m_selectionManager
->itemsInserted(KItemRangeList() <<
153 QCOMPARE(m_selectionManager
->currentItem(), 5);
154 QCOMPARE(spyCurrent
.count(), 1);
155 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
156 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
157 spyCurrent
.takeFirst();
159 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
161 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
162 QVERIFY(m_selectionManager
->hasSelection());
164 // Removing items should update current item and anchor item.
165 m_selectionManager
->itemsRemoved(KItemRangeList() <<
170 QCOMPARE(m_selectionManager
->currentItem(), 2);
171 QCOMPARE(spyCurrent
.count(), 1);
172 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
173 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
174 spyCurrent
.takeFirst();
176 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
178 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
179 QVERIFY(m_selectionManager
->hasSelection());
181 // Verify that clearSelection() also clears the anchored selection.
182 m_selectionManager
->clearSelection();
183 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
184 QVERIFY(!m_selectionManager
->hasSelection());
186 m_selectionManager
->endAnchoredSelection();
187 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
190 void KItemListSelectionManagerTest::testSetSelected_data()
192 QTest::addColumn
<int>("index");
193 QTest::addColumn
<int>("count");
194 QTest::addColumn
<int>("expectedSelectionCount");
196 QTest::newRow("Select all") << 0 << 100 << 100;
197 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
198 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
199 QTest::newRow("Too small index") << -1 << 100 << 0;
200 QTest::newRow("Too large index") << 100 << 100 << 0;
201 QTest::newRow("Too large count") << 0 << 100000 << 100;
202 QTest::newRow("Too small count") << 0 << 0 << 0;
205 void KItemListSelectionManagerTest::testSetSelected()
209 QFETCH(int, expectedSelectionCount
);
210 m_selectionManager
->setSelected(index
, count
);
211 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
214 void KItemListSelectionManagerTest::testItemsInserted()
216 // Select items 10 to 12
217 m_selectionManager
->setSelected(10, 3);
218 KItemSet selectedItems
= m_selectionManager
->selectedItems();
219 QCOMPARE(selectedItems
.count(), 3);
220 QVERIFY(selectedItems
.contains(10));
221 QVERIFY(selectedItems
.contains(11));
222 QVERIFY(selectedItems
.contains(12));
224 // Insert items 0 to 4 -> selection must be 15 to 17
225 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
226 selectedItems
= m_selectionManager
->selectedItems();
227 QCOMPARE(selectedItems
.count(), 3);
228 QVERIFY(selectedItems
.contains(15));
229 QVERIFY(selectedItems
.contains(16));
230 QVERIFY(selectedItems
.contains(17));
232 // Insert 3 items between the selections
233 m_selectionManager
->itemsInserted(KItemRangeList() <<
237 selectedItems
= m_selectionManager
->selectedItems();
238 QCOMPARE(selectedItems
.count(), 3);
239 QVERIFY(selectedItems
.contains(16));
240 QVERIFY(selectedItems
.contains(18));
241 QVERIFY(selectedItems
.contains(20));
244 void KItemListSelectionManagerTest::testItemsRemoved()
246 // Select items 10 to 15
247 m_selectionManager
->setSelected(10, 6);
248 KItemSet selectedItems
= m_selectionManager
->selectedItems();
249 QCOMPARE(selectedItems
.count(), 6);
250 for (int i
= 10; i
<= 15; ++i
) {
251 QVERIFY(selectedItems
.contains(i
));
254 // Remove items 0 to 4 -> selection must be 5 to 10
255 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
256 selectedItems
= m_selectionManager
->selectedItems();
257 QCOMPARE(selectedItems
.count(), 6);
258 for (int i
= 5; i
<= 10; ++i
) {
259 QVERIFY(selectedItems
.contains(i
));
262 // Remove the items 6 , 8 and 10
263 m_selectionManager
->itemsRemoved(KItemRangeList() <<
267 selectedItems
= m_selectionManager
->selectedItems();
268 QCOMPARE(selectedItems
.count(), 3);
269 QVERIFY(selectedItems
.contains(5));
270 QVERIFY(selectedItems
.contains(6));
271 QVERIFY(selectedItems
.contains(7));
274 void KItemListSelectionManagerTest::testAnchoredSelection()
276 m_selectionManager
->beginAnchoredSelection(5);
277 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
278 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
280 m_selectionManager
->setCurrentItem(6);
281 QCOMPARE(m_selectionManager
->currentItem(), 6);
282 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
284 m_selectionManager
->setCurrentItem(4);
285 QCOMPARE(m_selectionManager
->currentItem(), 4);
286 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
288 m_selectionManager
->setCurrentItem(7);
289 QCOMPARE(m_selectionManager
->currentItem(), 7);
290 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
292 // Ending the anchored selection should not change the selected items.
293 m_selectionManager
->endAnchoredSelection();
294 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
295 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
297 // Start a new anchored selection that overlaps the previous one
298 m_selectionManager
->beginAnchoredSelection(9);
299 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
300 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
302 m_selectionManager
->setCurrentItem(6);
303 QCOMPARE(m_selectionManager
->currentItem(), 6);
304 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
306 m_selectionManager
->setCurrentItem(10);
307 QCOMPARE(m_selectionManager
->currentItem(), 10);
308 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
310 m_selectionManager
->endAnchoredSelection();
311 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
312 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
321 EndAnchoredSelection
,
326 Q_DECLARE_METATYPE(KItemSet
)
327 Q_DECLARE_METATYPE(ChangeType
)
328 Q_DECLARE_METATYPE(KItemRange
)
329 Q_DECLARE_METATYPE(KItemRangeList
)
330 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
)
331 Q_DECLARE_METATYPE(QList
<int>)
334 * The following function provides a generic way to test the selection functionality.
336 * The test is data-driven and takes the following arguments:
338 * param initialSelection The selection at the beginning.
339 * param anchor This item will be the anchor item.
340 * param current This item will be the current item.
341 * param expectedSelection Expected selection after anchor and current are set.
342 * param changeType Type of the change that is done then:
344 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
345 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
346 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
347 * data.at(1) provides the list containing the new indices
348 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
349 * - EndAnchoredSelection
350 * - SetSelected -> data.at(0) provides the index where the selection process starts,
351 * data.at(1) provides the number of indices to be selected,
352 * data.at(2) provides the selection mode.
353 * \sa KItemListSelectionManager::setSelected()
354 * param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
355 * param finalSelection The expected final selection.
358 void KItemListSelectionManagerTest::testChangeSelection_data()
360 QTest::addColumn
<KItemSet
>("initialSelection");
361 QTest::addColumn
<int>("anchor");
362 QTest::addColumn
<int>("current");
363 QTest::addColumn
<KItemSet
>("expectedSelection");
364 QTest::addColumn
<ChangeType
>("changeType");
365 QTest::addColumn
<QList
<QVariant
> >("data");
366 QTest::addColumn
<KItemSet
>("finalSelection");
368 QTest::newRow("No change")
369 << (KItemSet() << 5 << 6)
371 << (KItemSet() << 2 << 3 << 5 << 6)
374 << (KItemSet() << 2 << 3 << 5 << 6);
376 QTest::newRow("Insert Items")
377 << (KItemSet() << 5 << 6)
379 << (KItemSet() << 2 << 3 << 5 << 6)
381 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
382 << (KItemSet() << 3 << 4 << 8 << 9);
384 QTest::newRow("Remove Items")
385 << (KItemSet() << 5 << 6)
387 << (KItemSet() << 2 << 3 << 5 << 6)
389 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
390 << (KItemSet() << 1 << 2 << 3 << 4);
392 QTest::newRow("Empty Anchored Selection")
396 << EndAnchoredSelection
400 QTest::newRow("Toggle selection")
401 << (KItemSet() << 1 << 3 << 4)
403 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
405 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)}
406 << (KItemSet() << 0 << 2 << 5 << 9);
408 // Swap items 2, 3 and 4, 5
409 QTest::newRow("Move items")
410 << (KItemSet() << 0 << 1 << 2 << 3)
412 << (KItemSet() << 0 << 1 << 2 << 3)
414 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
415 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
416 << (KItemSet() << 0 << 1 << 4 << 5);
418 QTest::newRow("Move items with active anchored selection")
421 << (KItemSet() << 0 << 1 << 2 << 3)
423 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
424 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
425 << (KItemSet() << 0 << 1 << 4 << 5);
428 QTest::newRow("Revert sort order")
429 << (KItemSet() << 0 << 1)
431 << (KItemSet() << 0 << 1 << 3 << 4)
433 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)),
434 QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
435 << (KItemSet() << 5 << 6 << 8 << 9);
438 void KItemListSelectionManagerTest::testChangeSelection()
440 QFETCH(KItemSet
, initialSelection
);
442 QFETCH(int, current
);
443 QFETCH(KItemSet
, expectedSelection
);
444 QFETCH(ChangeType
, changeType
);
445 QFETCH(QList
<QVariant
>, data
);
446 QFETCH(KItemSet
, finalSelection
);
448 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(KItemSet
,KItemSet
)));
450 // Initial selection should be empty
451 QVERIFY(!m_selectionManager
->hasSelection());
452 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
454 // Perform the initial selectiion
455 m_selectionManager
->setSelectedItems(initialSelection
);
457 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
459 // Perform an anchored selection.
460 // Note that current and anchor index are equal first because this is the case in typical uses of the
461 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
462 m_selectionManager
->setCurrentItem(anchor
);
463 m_selectionManager
->beginAnchoredSelection(anchor
);
464 m_selectionManager
->setCurrentItem(current
);
465 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
466 QCOMPARE(m_selectionManager
->currentItem(), current
);
468 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
470 // Change the model by inserting or removing items.
471 switch (changeType
) {
473 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
476 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
479 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
480 data
.at(1).value
<QList
<int>>());
482 case EndAnchoredSelection
:
483 m_selectionManager
->endAnchoredSelection();
484 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
487 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
488 data
.at(1).value
<int>(), // count
489 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
495 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
497 // Finally, clear the selection
498 m_selectionManager
->clearSelection();
500 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
503 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
505 QTest::addColumn
<int>("oldCurrentItemIndex");
506 QTest::addColumn
<int>("removeIndex");
507 QTest::addColumn
<int>("removeCount");
508 QTest::addColumn
<int>("newCurrentItemIndex");
510 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
511 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
512 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
513 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
514 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
517 void KItemListSelectionManagerTest::testDeleteCurrentItem()
519 QFETCH(int, oldCurrentItemIndex
);
520 QFETCH(int, removeIndex
);
521 QFETCH(int, removeCount
);
522 QFETCH(int, newCurrentItemIndex
);
524 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
526 const int newCount
= m_model
->count() - removeCount
;
527 m_model
->setCount(newCount
);
528 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
530 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
533 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
535 m_selectionManager
->setCurrentItem(4);
536 m_selectionManager
->beginAnchoredSelection(4);
538 // Reverse the items between 0 and 5.
539 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
541 QCOMPARE(m_selectionManager
->currentItem(), 1);
542 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
544 // Make 2 the current item -> 1 and 2 should be selected.
545 m_selectionManager
->setCurrentItem(2);
546 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
549 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
550 const KItemSet
& currentSelection
,
551 const KItemSet
& previousSelection
) const
553 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
554 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
555 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
556 if (currentSelection
.contains(index
)) {
557 QVERIFY(m_selectionManager
->isSelected(index
));
560 QVERIFY(!m_selectionManager
->isSelected(index
));
564 if (currentSelection
== previousSelection
) {
565 QCOMPARE(spy
.count(), 0);
568 QCOMPARE(spy
.count(), 1);
569 QList
<QVariant
> arguments
= spy
.takeFirst();
570 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
571 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
575 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
577 #include "kitemlistselectionmanagertest.moc"