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/kitemlistselectionmanager.h"
9 #include "kitemviews/kitemmodelbase.h"
12 #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
>();
50 class KItemListSelectionManagerTest
: public QObject
59 void testConstructor();
61 void testCurrentItemAnchorItem();
62 void testSetSelected_data();
63 void testSetSelected();
64 void testItemsInserted();
65 void testItemsRemoved();
66 void testAnchoredSelection();
67 void testChangeSelection_data();
68 void testChangeSelection();
69 void testDeleteCurrentItem_data();
70 void testDeleteCurrentItem();
71 void testAnchoredSelectionAfterMovingItems();
74 void verifySelectionChange(QSignalSpy
&spy
, const KItemSet
¤tSelection
, const KItemSet
&previousSelection
) const;
76 KItemListSelectionManager
*m_selectionManager
;
80 void KItemListSelectionManagerTest::initTestCase()
82 QStandardPaths::setTestModeEnabled(true);
85 void KItemListSelectionManagerTest::init()
87 m_model
= new DummyModel();
88 m_selectionManager
= new KItemListSelectionManager();
89 m_selectionManager
->setModel(m_model
);
92 void KItemListSelectionManagerTest::cleanup()
94 delete m_selectionManager
;
95 m_selectionManager
= nullptr;
101 void KItemListSelectionManagerTest::testConstructor()
103 QVERIFY(!m_selectionManager
->hasSelection());
104 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
105 QCOMPARE(m_selectionManager
->currentItem(), 0);
106 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
109 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
111 QSignalSpy
spyCurrent(m_selectionManager
, &KItemListSelectionManager::currentChanged
);
113 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
114 m_selectionManager
->setCurrentItem(4);
115 QCOMPARE(m_selectionManager
->currentItem(), 4);
116 QCOMPARE(spyCurrent
.count(), 1);
117 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
118 spyCurrent
.takeFirst();
120 // Begin an anchored selection.
121 m_selectionManager
->beginAnchoredSelection(5);
122 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
123 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
125 // Items between current and anchor should be selected now
126 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
127 QVERIFY(m_selectionManager
->hasSelection());
129 // Change current item again and check the selection
130 m_selectionManager
->setCurrentItem(2);
131 QCOMPARE(m_selectionManager
->currentItem(), 2);
132 QCOMPARE(spyCurrent
.count(), 1);
133 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
134 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
135 spyCurrent
.takeFirst();
137 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
138 QVERIFY(m_selectionManager
->hasSelection());
140 // Inserting items should update current item and anchor item.
141 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 1) << KItemRange(2, 2) << KItemRange(6, 3));
143 QCOMPARE(m_selectionManager
->currentItem(), 5);
144 QCOMPARE(spyCurrent
.count(), 1);
145 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
146 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
147 spyCurrent
.takeFirst();
149 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
151 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
152 QVERIFY(m_selectionManager
->hasSelection());
154 // Removing items should update current item and anchor item.
155 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 1) << KItemRange(9, 2));
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() << KItemRange(15, 1) << KItemRange(16, 1) << KItemRange(17, 1));
221 selectedItems
= m_selectionManager
->selectedItems();
222 QCOMPARE(selectedItems
.count(), 3);
223 QVERIFY(selectedItems
.contains(16));
224 QVERIFY(selectedItems
.contains(18));
225 QVERIFY(selectedItems
.contains(20));
228 void KItemListSelectionManagerTest::testItemsRemoved()
230 // Select items 10 to 15
231 m_selectionManager
->setSelected(10, 6);
232 KItemSet selectedItems
= m_selectionManager
->selectedItems();
233 QCOMPARE(selectedItems
.count(), 6);
234 for (int i
= 10; i
<= 15; ++i
) {
235 QVERIFY(selectedItems
.contains(i
));
238 // Remove items 0 to 4 -> selection must be 5 to 10
239 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
240 selectedItems
= m_selectionManager
->selectedItems();
241 QCOMPARE(selectedItems
.count(), 6);
242 for (int i
= 5; i
<= 10; ++i
) {
243 QVERIFY(selectedItems
.contains(i
));
246 // Remove the items 6 , 8 and 10
247 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(6, 1) << KItemRange(8, 1) << KItemRange(10, 1));
248 selectedItems
= m_selectionManager
->selectedItems();
249 QCOMPARE(selectedItems
.count(), 3);
250 QVERIFY(selectedItems
.contains(5));
251 QVERIFY(selectedItems
.contains(6));
252 QVERIFY(selectedItems
.contains(7));
255 void KItemListSelectionManagerTest::testAnchoredSelection()
257 m_selectionManager
->beginAnchoredSelection(5);
258 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
259 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
261 m_selectionManager
->setCurrentItem(6);
262 QCOMPARE(m_selectionManager
->currentItem(), 6);
263 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
265 m_selectionManager
->setCurrentItem(4);
266 QCOMPARE(m_selectionManager
->currentItem(), 4);
267 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
269 m_selectionManager
->setCurrentItem(7);
270 QCOMPARE(m_selectionManager
->currentItem(), 7);
271 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
273 // Ending the anchored selection should not change the selected items.
274 m_selectionManager
->endAnchoredSelection();
275 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
276 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
278 // Start a new anchored selection that overlaps the previous one
279 m_selectionManager
->beginAnchoredSelection(9);
280 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
281 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
283 m_selectionManager
->setCurrentItem(6);
284 QCOMPARE(m_selectionManager
->currentItem(), 6);
285 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
287 m_selectionManager
->setCurrentItem(10);
288 QCOMPARE(m_selectionManager
->currentItem(), 10);
289 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
291 m_selectionManager
->endAnchoredSelection();
292 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
293 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
298 enum ChangeType
{ NoChange
, InsertItems
, RemoveItems
, MoveItems
, EndAnchoredSelection
, SetSelected
};
301 Q_DECLARE_METATYPE(KItemSet
)
302 Q_DECLARE_METATYPE(ChangeType
)
303 Q_DECLARE_METATYPE(KItemRange
)
304 Q_DECLARE_METATYPE(KItemRangeList
)
305 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
)
306 Q_DECLARE_METATYPE(QList
<int>)
309 * The following function provides a generic way to test the selection functionality.
311 * The test is data-driven and takes the following arguments:
313 * param initialSelection The selection at the beginning.
314 * param anchor This item will be the anchor item.
315 * param current This item will be the current item.
316 * param expectedSelection Expected selection after anchor and current are set.
317 * param changeType Type of the change that is done then:
319 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
320 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
321 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
322 * data.at(1) provides the list containing the new indices
323 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
324 * - EndAnchoredSelection
325 * - SetSelected -> data.at(0) provides the index where the selection process starts,
326 * data.at(1) provides the number of indices to be selected,
327 * data.at(2) provides the selection mode.
328 * \sa KItemListSelectionManager::setSelected()
329 * param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
330 * param finalSelection The expected final selection.
333 void KItemListSelectionManagerTest::testChangeSelection_data()
335 QTest::addColumn
<KItemSet
>("initialSelection");
336 QTest::addColumn
<int>("anchor");
337 QTest::addColumn
<int>("current");
338 QTest::addColumn
<KItemSet
>("expectedSelection");
339 QTest::addColumn
<ChangeType
>("changeType");
340 QTest::addColumn
<QList
<QVariant
>>("data");
341 QTest::addColumn
<KItemSet
>("finalSelection");
343 QTest::newRow("No change") << (KItemSet() << 5 << 6) << 2 << 3 << (KItemSet() << 2 << 3 << 5 << 6) << NoChange
<< QList
<QVariant
>{}
344 << (KItemSet() << 2 << 3 << 5 << 6);
346 QTest::newRow("Insert Items") << (KItemSet() << 5 << 6) << 2 << 3 << (KItemSet() << 2 << 3 << 5 << 6) << InsertItems
347 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
348 << (KItemSet() << 3 << 4 << 8 << 9);
350 QTest::newRow("Remove Items") << (KItemSet() << 5 << 6) << 2 << 3 << (KItemSet() << 2 << 3 << 5 << 6) << RemoveItems
351 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
352 << (KItemSet() << 1 << 2 << 3 << 4);
354 QTest::newRow("Empty Anchored Selection") << KItemSet() << 2 << 2 << KItemSet() << EndAnchoredSelection
<< QList
<QVariant
>{} << KItemSet();
356 QTest::newRow("Toggle selection") << (KItemSet() << 1 << 3 << 4) << 6 << 8 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8) << SetSelected
357 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)} << (KItemSet() << 0 << 2 << 5 << 9);
359 // Swap items 2, 3 and 4, 5
360 QTest::newRow("Move items") << (KItemSet() << 0 << 1 << 2 << 3) << -1 << -1 << (KItemSet() << 0 << 1 << 2 << 3) << MoveItems
361 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)), QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
362 << (KItemSet() << 0 << 1 << 4 << 5);
364 QTest::newRow("Move items with active anchored selection")
365 << KItemSet() << 0 << 3 << (KItemSet() << 0 << 1 << 2 << 3) << MoveItems
366 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)), QVariant::fromValue(QList
<int>{4, 5, 2, 3})} << (KItemSet() << 0 << 1 << 4 << 5);
369 QTest::newRow("Revert sort order") << (KItemSet() << 0 << 1) << 3 << 4 << (KItemSet() << 0 << 1 << 3 << 4) << MoveItems
370 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)), QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
371 << (KItemSet() << 5 << 6 << 8 << 9);
374 void KItemListSelectionManagerTest::testChangeSelection()
376 QFETCH(KItemSet
, initialSelection
);
378 QFETCH(int, current
);
379 QFETCH(KItemSet
, expectedSelection
);
380 QFETCH(ChangeType
, changeType
);
381 QFETCH(QList
<QVariant
>, data
);
382 QFETCH(KItemSet
, finalSelection
);
384 QSignalSpy
spySelectionChanged(m_selectionManager
, &KItemListSelectionManager::selectionChanged
);
386 // Initial selection should be empty
387 QVERIFY(!m_selectionManager
->hasSelection());
388 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
390 // Perform the initial selection
391 m_selectionManager
->setSelectedItems(initialSelection
);
393 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
395 // Perform an anchored selection.
396 // Note that current and anchor index are equal first because this is the case in typical uses of the
397 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
398 m_selectionManager
->setCurrentItem(anchor
);
399 m_selectionManager
->beginAnchoredSelection(anchor
);
400 m_selectionManager
->setCurrentItem(current
);
401 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
402 QCOMPARE(m_selectionManager
->currentItem(), current
);
404 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
406 // Change the model by inserting or removing items.
407 switch (changeType
) {
409 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
412 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
415 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(), data
.at(1).value
<QList
<int>>());
417 case EndAnchoredSelection
:
418 m_selectionManager
->endAnchoredSelection();
419 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
422 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
423 data
.at(1).value
<int>(), // count
424 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
430 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
432 // Finally, clear the selection
433 m_selectionManager
->clearSelection();
435 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
438 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
440 QTest::addColumn
<int>("oldCurrentItemIndex");
441 QTest::addColumn
<int>("removeIndex");
442 QTest::addColumn
<int>("removeCount");
443 QTest::addColumn
<int>("newCurrentItemIndex");
445 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
446 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
447 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
448 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
449 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
452 void KItemListSelectionManagerTest::testDeleteCurrentItem()
454 QFETCH(int, oldCurrentItemIndex
);
455 QFETCH(int, removeIndex
);
456 QFETCH(int, removeCount
);
457 QFETCH(int, newCurrentItemIndex
);
459 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
461 const int newCount
= m_model
->count() - removeCount
;
462 m_model
->setCount(newCount
);
463 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
465 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
468 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
470 m_selectionManager
->setCurrentItem(4);
471 m_selectionManager
->beginAnchoredSelection(4);
473 // Reverse the items between 0 and 5.
474 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
476 QCOMPARE(m_selectionManager
->currentItem(), 1);
477 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
479 // Make 2 the current item -> 1 and 2 should be selected.
480 m_selectionManager
->setCurrentItem(2);
481 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
484 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
&spy
, const KItemSet
¤tSelection
, const KItemSet
&previousSelection
) const
486 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
487 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
488 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
489 if (currentSelection
.contains(index
)) {
490 QVERIFY(m_selectionManager
->isSelected(index
));
492 QVERIFY(!m_selectionManager
->isSelected(index
));
496 if (currentSelection
== previousSelection
) {
497 QCOMPARE(spy
.count(), 0);
499 QCOMPARE(spy
.count(), 1);
500 QList
<QVariant
> arguments
= spy
.takeFirst();
501 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
502 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
506 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
508 #include "kitemlistselectionmanagertest.moc"