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"
13 #include <QStandardPaths>
15 class DummyModel
: public KItemModelBase
20 void setCount(int count
);
21 int count() const override
;
22 QHash
<QByteArray
, QVariant
> data(int index
) const override
;
28 DummyModel::DummyModel() :
34 void DummyModel::setCount(int count
)
39 int DummyModel::count() const
44 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
47 return QHash
<QByteArray
, QVariant
>();
51 class KItemListSelectionManagerTest
: public QObject
60 void testConstructor();
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 void testDeleteCurrentItem_data();
71 void testDeleteCurrentItem();
72 void testAnchoredSelectionAfterMovingItems();
75 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
77 KItemListSelectionManager
* m_selectionManager
;
81 void KItemListSelectionManagerTest::initTestCase()
83 QStandardPaths::setTestModeEnabled(true);
86 void KItemListSelectionManagerTest::init()
88 m_model
= new DummyModel();
89 m_selectionManager
= new KItemListSelectionManager();
90 m_selectionManager
->setModel(m_model
);
93 void KItemListSelectionManagerTest::cleanup()
95 delete m_selectionManager
;
96 m_selectionManager
= nullptr;
102 void KItemListSelectionManagerTest::testConstructor()
104 QVERIFY(!m_selectionManager
->hasSelection());
105 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
106 QCOMPARE(m_selectionManager
->currentItem(), 0);
107 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
110 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
112 QSignalSpy
spyCurrent(m_selectionManager
, &KItemListSelectionManager::currentChanged
);
114 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
115 m_selectionManager
->setCurrentItem(4);
116 QCOMPARE(m_selectionManager
->currentItem(), 4);
117 QCOMPARE(spyCurrent
.count(), 1);
118 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
119 spyCurrent
.takeFirst();
121 // Begin an anchored selection.
122 m_selectionManager
->beginAnchoredSelection(5);
123 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
124 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
126 // Items between current and anchor should be selected now
127 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
128 QVERIFY(m_selectionManager
->hasSelection());
130 // Change current item again and check the selection
131 m_selectionManager
->setCurrentItem(2);
132 QCOMPARE(m_selectionManager
->currentItem(), 2);
133 QCOMPARE(spyCurrent
.count(), 1);
134 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
135 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
136 spyCurrent
.takeFirst();
138 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
139 QVERIFY(m_selectionManager
->hasSelection());
141 // Inserting items should update current item and anchor item.
142 m_selectionManager
->itemsInserted(KItemRangeList() <<
147 QCOMPARE(m_selectionManager
->currentItem(), 5);
148 QCOMPARE(spyCurrent
.count(), 1);
149 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
150 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
151 spyCurrent
.takeFirst();
153 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
155 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
156 QVERIFY(m_selectionManager
->hasSelection());
158 // Removing items should update current item and anchor item.
159 m_selectionManager
->itemsRemoved(KItemRangeList() <<
164 QCOMPARE(m_selectionManager
->currentItem(), 2);
165 QCOMPARE(spyCurrent
.count(), 1);
166 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
167 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
168 spyCurrent
.takeFirst();
170 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
172 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
173 QVERIFY(m_selectionManager
->hasSelection());
175 // Verify that clearSelection() also clears the anchored selection.
176 m_selectionManager
->clearSelection();
177 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
178 QVERIFY(!m_selectionManager
->hasSelection());
180 m_selectionManager
->endAnchoredSelection();
181 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
184 void KItemListSelectionManagerTest::testSetSelected_data()
186 QTest::addColumn
<int>("index");
187 QTest::addColumn
<int>("count");
188 QTest::addColumn
<int>("expectedSelectionCount");
190 QTest::newRow("Select all") << 0 << 100 << 100;
191 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
192 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
193 QTest::newRow("Too small index") << -1 << 100 << 0;
194 QTest::newRow("Too large index") << 100 << 100 << 0;
195 QTest::newRow("Too large count") << 0 << 100000 << 100;
196 QTest::newRow("Too small count") << 0 << 0 << 0;
199 void KItemListSelectionManagerTest::testSetSelected()
203 QFETCH(int, expectedSelectionCount
);
204 m_selectionManager
->setSelected(index
, count
);
205 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
208 void KItemListSelectionManagerTest::testItemsInserted()
210 // Select items 10 to 12
211 m_selectionManager
->setSelected(10, 3);
212 KItemSet selectedItems
= m_selectionManager
->selectedItems();
213 QCOMPARE(selectedItems
.count(), 3);
214 QVERIFY(selectedItems
.contains(10));
215 QVERIFY(selectedItems
.contains(11));
216 QVERIFY(selectedItems
.contains(12));
218 // Insert items 0 to 4 -> selection must be 15 to 17
219 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
220 selectedItems
= m_selectionManager
->selectedItems();
221 QCOMPARE(selectedItems
.count(), 3);
222 QVERIFY(selectedItems
.contains(15));
223 QVERIFY(selectedItems
.contains(16));
224 QVERIFY(selectedItems
.contains(17));
226 // Insert 3 items between the selections
227 m_selectionManager
->itemsInserted(KItemRangeList() <<
231 selectedItems
= m_selectionManager
->selectedItems();
232 QCOMPARE(selectedItems
.count(), 3);
233 QVERIFY(selectedItems
.contains(16));
234 QVERIFY(selectedItems
.contains(18));
235 QVERIFY(selectedItems
.contains(20));
238 void KItemListSelectionManagerTest::testItemsRemoved()
240 // Select items 10 to 15
241 m_selectionManager
->setSelected(10, 6);
242 KItemSet selectedItems
= m_selectionManager
->selectedItems();
243 QCOMPARE(selectedItems
.count(), 6);
244 for (int i
= 10; i
<= 15; ++i
) {
245 QVERIFY(selectedItems
.contains(i
));
248 // Remove items 0 to 4 -> selection must be 5 to 10
249 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
250 selectedItems
= m_selectionManager
->selectedItems();
251 QCOMPARE(selectedItems
.count(), 6);
252 for (int i
= 5; i
<= 10; ++i
) {
253 QVERIFY(selectedItems
.contains(i
));
256 // Remove the items 6 , 8 and 10
257 m_selectionManager
->itemsRemoved(KItemRangeList() <<
261 selectedItems
= m_selectionManager
->selectedItems();
262 QCOMPARE(selectedItems
.count(), 3);
263 QVERIFY(selectedItems
.contains(5));
264 QVERIFY(selectedItems
.contains(6));
265 QVERIFY(selectedItems
.contains(7));
268 void KItemListSelectionManagerTest::testAnchoredSelection()
270 m_selectionManager
->beginAnchoredSelection(5);
271 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
272 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
274 m_selectionManager
->setCurrentItem(6);
275 QCOMPARE(m_selectionManager
->currentItem(), 6);
276 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
278 m_selectionManager
->setCurrentItem(4);
279 QCOMPARE(m_selectionManager
->currentItem(), 4);
280 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
282 m_selectionManager
->setCurrentItem(7);
283 QCOMPARE(m_selectionManager
->currentItem(), 7);
284 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
286 // Ending the anchored selection should not change the selected items.
287 m_selectionManager
->endAnchoredSelection();
288 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
289 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
291 // Start a new anchored selection that overlaps the previous one
292 m_selectionManager
->beginAnchoredSelection(9);
293 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
294 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
296 m_selectionManager
->setCurrentItem(6);
297 QCOMPARE(m_selectionManager
->currentItem(), 6);
298 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
300 m_selectionManager
->setCurrentItem(10);
301 QCOMPARE(m_selectionManager
->currentItem(), 10);
302 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
304 m_selectionManager
->endAnchoredSelection();
305 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
306 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
315 EndAnchoredSelection
,
320 Q_DECLARE_METATYPE(KItemSet
)
321 Q_DECLARE_METATYPE(ChangeType
)
322 Q_DECLARE_METATYPE(KItemRange
)
323 Q_DECLARE_METATYPE(KItemRangeList
)
324 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
)
325 Q_DECLARE_METATYPE(QList
<int>)
328 * The following function provides a generic way to test the selection functionality.
330 * The test is data-driven and takes the following arguments:
332 * param initialSelection The selection at the beginning.
333 * param anchor This item will be the anchor item.
334 * param current This item will be the current item.
335 * param expectedSelection Expected selection after anchor and current are set.
336 * param changeType Type of the change that is done then:
338 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
339 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
340 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
341 * data.at(1) provides the list containing the new indices
342 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
343 * - EndAnchoredSelection
344 * - SetSelected -> data.at(0) provides the index where the selection process starts,
345 * data.at(1) provides the number of indices to be selected,
346 * data.at(2) provides the selection mode.
347 * \sa KItemListSelectionManager::setSelected()
348 * param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
349 * param finalSelection The expected final selection.
352 void KItemListSelectionManagerTest::testChangeSelection_data()
354 QTest::addColumn
<KItemSet
>("initialSelection");
355 QTest::addColumn
<int>("anchor");
356 QTest::addColumn
<int>("current");
357 QTest::addColumn
<KItemSet
>("expectedSelection");
358 QTest::addColumn
<ChangeType
>("changeType");
359 QTest::addColumn
<QList
<QVariant
> >("data");
360 QTest::addColumn
<KItemSet
>("finalSelection");
362 QTest::newRow("No change")
363 << (KItemSet() << 5 << 6)
365 << (KItemSet() << 2 << 3 << 5 << 6)
368 << (KItemSet() << 2 << 3 << 5 << 6);
370 QTest::newRow("Insert Items")
371 << (KItemSet() << 5 << 6)
373 << (KItemSet() << 2 << 3 << 5 << 6)
375 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
376 << (KItemSet() << 3 << 4 << 8 << 9);
378 QTest::newRow("Remove Items")
379 << (KItemSet() << 5 << 6)
381 << (KItemSet() << 2 << 3 << 5 << 6)
383 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
384 << (KItemSet() << 1 << 2 << 3 << 4);
386 QTest::newRow("Empty Anchored Selection")
390 << EndAnchoredSelection
394 QTest::newRow("Toggle selection")
395 << (KItemSet() << 1 << 3 << 4)
397 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
399 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)}
400 << (KItemSet() << 0 << 2 << 5 << 9);
402 // Swap items 2, 3 and 4, 5
403 QTest::newRow("Move items")
404 << (KItemSet() << 0 << 1 << 2 << 3)
406 << (KItemSet() << 0 << 1 << 2 << 3)
408 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
409 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
410 << (KItemSet() << 0 << 1 << 4 << 5);
412 QTest::newRow("Move items with active anchored selection")
415 << (KItemSet() << 0 << 1 << 2 << 3)
417 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
418 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
419 << (KItemSet() << 0 << 1 << 4 << 5);
422 QTest::newRow("Revert sort order")
423 << (KItemSet() << 0 << 1)
425 << (KItemSet() << 0 << 1 << 3 << 4)
427 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)),
428 QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
429 << (KItemSet() << 5 << 6 << 8 << 9);
432 void KItemListSelectionManagerTest::testChangeSelection()
434 QFETCH(KItemSet
, initialSelection
);
436 QFETCH(int, current
);
437 QFETCH(KItemSet
, expectedSelection
);
438 QFETCH(ChangeType
, changeType
);
439 QFETCH(QList
<QVariant
>, data
);
440 QFETCH(KItemSet
, finalSelection
);
442 QSignalSpy
spySelectionChanged(m_selectionManager
, &KItemListSelectionManager::selectionChanged
);
444 // Initial selection should be empty
445 QVERIFY(!m_selectionManager
->hasSelection());
446 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
448 // Perform the initial selection
449 m_selectionManager
->setSelectedItems(initialSelection
);
451 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
453 // Perform an anchored selection.
454 // Note that current and anchor index are equal first because this is the case in typical uses of the
455 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
456 m_selectionManager
->setCurrentItem(anchor
);
457 m_selectionManager
->beginAnchoredSelection(anchor
);
458 m_selectionManager
->setCurrentItem(current
);
459 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
460 QCOMPARE(m_selectionManager
->currentItem(), current
);
462 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
464 // Change the model by inserting or removing items.
465 switch (changeType
) {
467 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
470 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
473 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
474 data
.at(1).value
<QList
<int>>());
476 case EndAnchoredSelection
:
477 m_selectionManager
->endAnchoredSelection();
478 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
481 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
482 data
.at(1).value
<int>(), // count
483 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
489 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
491 // Finally, clear the selection
492 m_selectionManager
->clearSelection();
494 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
497 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
499 QTest::addColumn
<int>("oldCurrentItemIndex");
500 QTest::addColumn
<int>("removeIndex");
501 QTest::addColumn
<int>("removeCount");
502 QTest::addColumn
<int>("newCurrentItemIndex");
504 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
505 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
506 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
507 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
508 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
511 void KItemListSelectionManagerTest::testDeleteCurrentItem()
513 QFETCH(int, oldCurrentItemIndex
);
514 QFETCH(int, removeIndex
);
515 QFETCH(int, removeCount
);
516 QFETCH(int, newCurrentItemIndex
);
518 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
520 const int newCount
= m_model
->count() - removeCount
;
521 m_model
->setCount(newCount
);
522 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
524 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
527 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
529 m_selectionManager
->setCurrentItem(4);
530 m_selectionManager
->beginAnchoredSelection(4);
532 // Reverse the items between 0 and 5.
533 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
535 QCOMPARE(m_selectionManager
->currentItem(), 1);
536 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
538 // Make 2 the current item -> 1 and 2 should be selected.
539 m_selectionManager
->setCurrentItem(2);
540 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
543 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
544 const KItemSet
& currentSelection
,
545 const KItemSet
& previousSelection
) const
547 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
548 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
549 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
550 if (currentSelection
.contains(index
)) {
551 QVERIFY(m_selectionManager
->isSelected(index
));
554 QVERIFY(!m_selectionManager
->isSelected(index
));
558 if (currentSelection
== previousSelection
) {
559 QCOMPARE(spy
.count(), 0);
562 QCOMPARE(spy
.count(), 1);
563 QList
<QVariant
> arguments
= spy
.takeFirst();
564 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
565 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
569 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
571 #include "kitemlistselectionmanagertest.moc"