2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "kitemviews/kitemmodelbase.h"
9 #include "kitemviews/kitemlistselectionmanager.h"
14 class DummyModel
: public KItemModelBase
19 void setCount(int count
);
20 int count() const override
;
21 QHash
<QByteArray
, QVariant
> data(int index
) const override
;
27 DummyModel::DummyModel() :
33 void DummyModel::setCount(int count
)
38 int DummyModel::count() const
43 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
46 return QHash
<QByteArray
, QVariant
>();
50 class KItemListSelectionManagerTest
: public QObject
58 void testConstructor();
60 void testCurrentItemAnchorItem();
61 void testSetSelected_data();
62 void testSetSelected();
63 void testItemsInserted();
64 void testItemsRemoved();
65 void testAnchoredSelection();
66 void testChangeSelection_data();
67 void testChangeSelection();
68 void testDeleteCurrentItem_data();
69 void testDeleteCurrentItem();
70 void testAnchoredSelectionAfterMovingItems();
73 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
75 KItemListSelectionManager
* m_selectionManager
;
79 void KItemListSelectionManagerTest::init()
81 m_model
= new DummyModel();
82 m_selectionManager
= new KItemListSelectionManager();
83 m_selectionManager
->setModel(m_model
);
86 void KItemListSelectionManagerTest::cleanup()
88 delete m_selectionManager
;
89 m_selectionManager
= nullptr;
95 void KItemListSelectionManagerTest::testConstructor()
97 QVERIFY(!m_selectionManager
->hasSelection());
98 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
99 QCOMPARE(m_selectionManager
->currentItem(), 0);
100 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
103 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
105 QSignalSpy
spyCurrent(m_selectionManager
, &KItemListSelectionManager::currentChanged
);
107 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
108 m_selectionManager
->setCurrentItem(4);
109 QCOMPARE(m_selectionManager
->currentItem(), 4);
110 QCOMPARE(spyCurrent
.count(), 1);
111 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
112 spyCurrent
.takeFirst();
114 // Begin an anchored selection.
115 m_selectionManager
->beginAnchoredSelection(5);
116 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
117 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
119 // Items between current and anchor should be selected now
120 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
121 QVERIFY(m_selectionManager
->hasSelection());
123 // Change current item again and check the selection
124 m_selectionManager
->setCurrentItem(2);
125 QCOMPARE(m_selectionManager
->currentItem(), 2);
126 QCOMPARE(spyCurrent
.count(), 1);
127 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
128 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
129 spyCurrent
.takeFirst();
131 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
132 QVERIFY(m_selectionManager
->hasSelection());
134 // Inserting items should update current item and anchor item.
135 m_selectionManager
->itemsInserted(KItemRangeList() <<
140 QCOMPARE(m_selectionManager
->currentItem(), 5);
141 QCOMPARE(spyCurrent
.count(), 1);
142 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
143 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
144 spyCurrent
.takeFirst();
146 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
148 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
149 QVERIFY(m_selectionManager
->hasSelection());
151 // Removing items should update current item and anchor item.
152 m_selectionManager
->itemsRemoved(KItemRangeList() <<
157 QCOMPARE(m_selectionManager
->currentItem(), 2);
158 QCOMPARE(spyCurrent
.count(), 1);
159 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
160 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
161 spyCurrent
.takeFirst();
163 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
165 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
166 QVERIFY(m_selectionManager
->hasSelection());
168 // Verify that clearSelection() also clears the anchored selection.
169 m_selectionManager
->clearSelection();
170 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
171 QVERIFY(!m_selectionManager
->hasSelection());
173 m_selectionManager
->endAnchoredSelection();
174 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
177 void KItemListSelectionManagerTest::testSetSelected_data()
179 QTest::addColumn
<int>("index");
180 QTest::addColumn
<int>("count");
181 QTest::addColumn
<int>("expectedSelectionCount");
183 QTest::newRow("Select all") << 0 << 100 << 100;
184 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
185 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
186 QTest::newRow("Too small index") << -1 << 100 << 0;
187 QTest::newRow("Too large index") << 100 << 100 << 0;
188 QTest::newRow("Too large count") << 0 << 100000 << 100;
189 QTest::newRow("Too small count") << 0 << 0 << 0;
192 void KItemListSelectionManagerTest::testSetSelected()
196 QFETCH(int, expectedSelectionCount
);
197 m_selectionManager
->setSelected(index
, count
);
198 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
201 void KItemListSelectionManagerTest::testItemsInserted()
203 // Select items 10 to 12
204 m_selectionManager
->setSelected(10, 3);
205 KItemSet selectedItems
= m_selectionManager
->selectedItems();
206 QCOMPARE(selectedItems
.count(), 3);
207 QVERIFY(selectedItems
.contains(10));
208 QVERIFY(selectedItems
.contains(11));
209 QVERIFY(selectedItems
.contains(12));
211 // Insert items 0 to 4 -> selection must be 15 to 17
212 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
213 selectedItems
= m_selectionManager
->selectedItems();
214 QCOMPARE(selectedItems
.count(), 3);
215 QVERIFY(selectedItems
.contains(15));
216 QVERIFY(selectedItems
.contains(16));
217 QVERIFY(selectedItems
.contains(17));
219 // Insert 3 items between the selections
220 m_selectionManager
->itemsInserted(KItemRangeList() <<
224 selectedItems
= m_selectionManager
->selectedItems();
225 QCOMPARE(selectedItems
.count(), 3);
226 QVERIFY(selectedItems
.contains(16));
227 QVERIFY(selectedItems
.contains(18));
228 QVERIFY(selectedItems
.contains(20));
231 void KItemListSelectionManagerTest::testItemsRemoved()
233 // Select items 10 to 15
234 m_selectionManager
->setSelected(10, 6);
235 KItemSet selectedItems
= m_selectionManager
->selectedItems();
236 QCOMPARE(selectedItems
.count(), 6);
237 for (int i
= 10; i
<= 15; ++i
) {
238 QVERIFY(selectedItems
.contains(i
));
241 // Remove items 0 to 4 -> selection must be 5 to 10
242 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
243 selectedItems
= m_selectionManager
->selectedItems();
244 QCOMPARE(selectedItems
.count(), 6);
245 for (int i
= 5; i
<= 10; ++i
) {
246 QVERIFY(selectedItems
.contains(i
));
249 // Remove the items 6 , 8 and 10
250 m_selectionManager
->itemsRemoved(KItemRangeList() <<
254 selectedItems
= m_selectionManager
->selectedItems();
255 QCOMPARE(selectedItems
.count(), 3);
256 QVERIFY(selectedItems
.contains(5));
257 QVERIFY(selectedItems
.contains(6));
258 QVERIFY(selectedItems
.contains(7));
261 void KItemListSelectionManagerTest::testAnchoredSelection()
263 m_selectionManager
->beginAnchoredSelection(5);
264 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
265 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
267 m_selectionManager
->setCurrentItem(6);
268 QCOMPARE(m_selectionManager
->currentItem(), 6);
269 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
271 m_selectionManager
->setCurrentItem(4);
272 QCOMPARE(m_selectionManager
->currentItem(), 4);
273 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
275 m_selectionManager
->setCurrentItem(7);
276 QCOMPARE(m_selectionManager
->currentItem(), 7);
277 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
279 // Ending the anchored selection should not change the selected items.
280 m_selectionManager
->endAnchoredSelection();
281 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
282 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
284 // Start a new anchored selection that overlaps the previous one
285 m_selectionManager
->beginAnchoredSelection(9);
286 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
287 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
289 m_selectionManager
->setCurrentItem(6);
290 QCOMPARE(m_selectionManager
->currentItem(), 6);
291 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
293 m_selectionManager
->setCurrentItem(10);
294 QCOMPARE(m_selectionManager
->currentItem(), 10);
295 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
297 m_selectionManager
->endAnchoredSelection();
298 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
299 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
308 EndAnchoredSelection
,
313 Q_DECLARE_METATYPE(KItemSet
)
314 Q_DECLARE_METATYPE(ChangeType
)
315 Q_DECLARE_METATYPE(KItemRange
)
316 Q_DECLARE_METATYPE(KItemRangeList
)
317 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
)
318 Q_DECLARE_METATYPE(QList
<int>)
321 * The following function provides a generic way to test the selection functionality.
323 * The test is data-driven and takes the following arguments:
325 * param initialSelection The selection at the beginning.
326 * param anchor This item will be the anchor item.
327 * param current This item will be the current item.
328 * param expectedSelection Expected selection after anchor and current are set.
329 * param changeType Type of the change that is done then:
331 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
332 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
333 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
334 * data.at(1) provides the list containing the new indices
335 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
336 * - EndAnchoredSelection
337 * - SetSelected -> data.at(0) provides the index where the selection process starts,
338 * data.at(1) provides the number of indices to be selected,
339 * data.at(2) provides the selection mode.
340 * \sa KItemListSelectionManager::setSelected()
341 * param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
342 * param finalSelection The expected final selection.
345 void KItemListSelectionManagerTest::testChangeSelection_data()
347 QTest::addColumn
<KItemSet
>("initialSelection");
348 QTest::addColumn
<int>("anchor");
349 QTest::addColumn
<int>("current");
350 QTest::addColumn
<KItemSet
>("expectedSelection");
351 QTest::addColumn
<ChangeType
>("changeType");
352 QTest::addColumn
<QList
<QVariant
> >("data");
353 QTest::addColumn
<KItemSet
>("finalSelection");
355 QTest::newRow("No change")
356 << (KItemSet() << 5 << 6)
358 << (KItemSet() << 2 << 3 << 5 << 6)
361 << (KItemSet() << 2 << 3 << 5 << 6);
363 QTest::newRow("Insert Items")
364 << (KItemSet() << 5 << 6)
366 << (KItemSet() << 2 << 3 << 5 << 6)
368 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
369 << (KItemSet() << 3 << 4 << 8 << 9);
371 QTest::newRow("Remove Items")
372 << (KItemSet() << 5 << 6)
374 << (KItemSet() << 2 << 3 << 5 << 6)
376 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
377 << (KItemSet() << 1 << 2 << 3 << 4);
379 QTest::newRow("Empty Anchored Selection")
383 << EndAnchoredSelection
387 QTest::newRow("Toggle selection")
388 << (KItemSet() << 1 << 3 << 4)
390 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
392 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)}
393 << (KItemSet() << 0 << 2 << 5 << 9);
395 // Swap items 2, 3 and 4, 5
396 QTest::newRow("Move items")
397 << (KItemSet() << 0 << 1 << 2 << 3)
399 << (KItemSet() << 0 << 1 << 2 << 3)
401 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
402 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
403 << (KItemSet() << 0 << 1 << 4 << 5);
405 QTest::newRow("Move items with active anchored selection")
408 << (KItemSet() << 0 << 1 << 2 << 3)
410 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
411 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
412 << (KItemSet() << 0 << 1 << 4 << 5);
415 QTest::newRow("Revert sort order")
416 << (KItemSet() << 0 << 1)
418 << (KItemSet() << 0 << 1 << 3 << 4)
420 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)),
421 QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
422 << (KItemSet() << 5 << 6 << 8 << 9);
425 void KItemListSelectionManagerTest::testChangeSelection()
427 QFETCH(KItemSet
, initialSelection
);
429 QFETCH(int, current
);
430 QFETCH(KItemSet
, expectedSelection
);
431 QFETCH(ChangeType
, changeType
);
432 QFETCH(QList
<QVariant
>, data
);
433 QFETCH(KItemSet
, finalSelection
);
435 QSignalSpy
spySelectionChanged(m_selectionManager
, &KItemListSelectionManager::selectionChanged
);
437 // Initial selection should be empty
438 QVERIFY(!m_selectionManager
->hasSelection());
439 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
441 // Perform the initial selection
442 m_selectionManager
->setSelectedItems(initialSelection
);
444 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
446 // Perform an anchored selection.
447 // Note that current and anchor index are equal first because this is the case in typical uses of the
448 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
449 m_selectionManager
->setCurrentItem(anchor
);
450 m_selectionManager
->beginAnchoredSelection(anchor
);
451 m_selectionManager
->setCurrentItem(current
);
452 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
453 QCOMPARE(m_selectionManager
->currentItem(), current
);
455 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
457 // Change the model by inserting or removing items.
458 switch (changeType
) {
460 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
463 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
466 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
467 data
.at(1).value
<QList
<int>>());
469 case EndAnchoredSelection
:
470 m_selectionManager
->endAnchoredSelection();
471 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
474 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
475 data
.at(1).value
<int>(), // count
476 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
482 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
484 // Finally, clear the selection
485 m_selectionManager
->clearSelection();
487 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
490 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
492 QTest::addColumn
<int>("oldCurrentItemIndex");
493 QTest::addColumn
<int>("removeIndex");
494 QTest::addColumn
<int>("removeCount");
495 QTest::addColumn
<int>("newCurrentItemIndex");
497 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
498 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
499 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
500 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
501 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
504 void KItemListSelectionManagerTest::testDeleteCurrentItem()
506 QFETCH(int, oldCurrentItemIndex
);
507 QFETCH(int, removeIndex
);
508 QFETCH(int, removeCount
);
509 QFETCH(int, newCurrentItemIndex
);
511 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
513 const int newCount
= m_model
->count() - removeCount
;
514 m_model
->setCount(newCount
);
515 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
517 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
520 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
522 m_selectionManager
->setCurrentItem(4);
523 m_selectionManager
->beginAnchoredSelection(4);
525 // Reverse the items between 0 and 5.
526 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
528 QCOMPARE(m_selectionManager
->currentItem(), 1);
529 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
531 // Make 2 the current item -> 1 and 2 should be selected.
532 m_selectionManager
->setCurrentItem(2);
533 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
536 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
537 const KItemSet
& currentSelection
,
538 const KItemSet
& previousSelection
) const
540 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
541 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
542 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
543 if (currentSelection
.contains(index
)) {
544 QVERIFY(m_selectionManager
->isSelected(index
));
547 QVERIFY(!m_selectionManager
->isSelected(index
));
551 if (currentSelection
== previousSelection
) {
552 QCOMPARE(spy
.count(), 0);
555 QCOMPARE(spy
.count(), 1);
556 QList
<QVariant
> arguments
= spy
.takeFirst();
557 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
558 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
562 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
564 #include "kitemlistselectionmanagertest.moc"