]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Merge branch 'release/20.08' into master
[dolphin.git] / src / tests / kitemlistselectionmanagertest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2011 Frank Reininghaus <frank78ac@googlemail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "kitemviews/kitemmodelbase.h"
9 #include "kitemviews/kitemlistselectionmanager.h"
10
11 #include <QTest>
12 #include <QSignalSpy>
13
14 class DummyModel : public KItemModelBase
15 {
16 Q_OBJECT
17 public:
18 DummyModel();
19 void setCount(int count);
20 int count() const override;
21 QHash<QByteArray, QVariant> data(int index) const override;
22
23 private:
24 int m_count;
25 };
26
27 DummyModel::DummyModel() :
28 KItemModelBase(),
29 m_count(100)
30 {
31 }
32
33 void DummyModel::setCount(int count)
34 {
35 m_count = count;
36 }
37
38 int DummyModel::count() const
39 {
40 return m_count;
41 }
42
43 QHash<QByteArray, QVariant> DummyModel::data(int index) const
44 {
45 Q_UNUSED(index)
46 return QHash<QByteArray, QVariant>();
47 }
48
49
50 class KItemListSelectionManagerTest : public QObject
51 {
52 Q_OBJECT
53
54 private slots:
55 void init();
56 void cleanup();
57
58 void testConstructor();
59
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();
71
72 private:
73 void verifySelectionChange(QSignalSpy& spy, const KItemSet& currentSelection, const KItemSet& previousSelection) const;
74
75 KItemListSelectionManager* m_selectionManager;
76 DummyModel* m_model;
77 };
78
79 void KItemListSelectionManagerTest::init()
80 {
81 m_model = new DummyModel();
82 m_selectionManager = new KItemListSelectionManager();
83 m_selectionManager->setModel(m_model);
84 }
85
86 void KItemListSelectionManagerTest::cleanup()
87 {
88 delete m_selectionManager;
89 m_selectionManager = nullptr;
90
91 delete m_model;
92 m_model = nullptr;
93 }
94
95 void KItemListSelectionManagerTest::testConstructor()
96 {
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);
101 }
102
103 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
104 {
105 QSignalSpy spyCurrent(m_selectionManager, &KItemListSelectionManager::currentChanged);
106
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();
113
114 // Begin an anchored selection.
115 m_selectionManager->beginAnchoredSelection(5);
116 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
117 QCOMPARE(m_selectionManager->m_anchorItem, 5);
118
119 // Items between current and anchor should be selected now
120 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
121 QVERIFY(m_selectionManager->hasSelection());
122
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();
130
131 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
132 QVERIFY(m_selectionManager->hasSelection());
133
134 // Inserting items should update current item and anchor item.
135 m_selectionManager->itemsInserted(KItemRangeList() <<
136 KItemRange(0, 1) <<
137 KItemRange(2, 2) <<
138 KItemRange(6, 3));
139
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();
145
146 QCOMPARE(m_selectionManager->m_anchorItem, 8);
147
148 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
149 QVERIFY(m_selectionManager->hasSelection());
150
151 // Removing items should update current item and anchor item.
152 m_selectionManager->itemsRemoved(KItemRangeList() <<
153 KItemRange(0, 2) <<
154 KItemRange(2, 1) <<
155 KItemRange(9, 2));
156
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();
162
163 QCOMPARE(m_selectionManager->m_anchorItem, 5);
164
165 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
166 QVERIFY(m_selectionManager->hasSelection());
167
168 // Verify that clearSelection() also clears the anchored selection.
169 m_selectionManager->clearSelection();
170 QCOMPARE(m_selectionManager->selectedItems(), KItemSet());
171 QVERIFY(!m_selectionManager->hasSelection());
172
173 m_selectionManager->endAnchoredSelection();
174 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
175 }
176
177 void KItemListSelectionManagerTest::testSetSelected_data()
178 {
179 QTest::addColumn<int>("index");
180 QTest::addColumn<int>("count");
181 QTest::addColumn<int>("expectedSelectionCount");
182
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;
190 }
191
192 void KItemListSelectionManagerTest::testSetSelected()
193 {
194 QFETCH(int, index);
195 QFETCH(int, count);
196 QFETCH(int, expectedSelectionCount);
197 m_selectionManager->setSelected(index, count);
198 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
199 }
200
201 void KItemListSelectionManagerTest::testItemsInserted()
202 {
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));
210
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));
218
219 // Insert 3 items between the selections
220 m_selectionManager->itemsInserted(KItemRangeList() <<
221 KItemRange(15, 1) <<
222 KItemRange(16, 1) <<
223 KItemRange(17, 1));
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));
229 }
230
231 void KItemListSelectionManagerTest::testItemsRemoved()
232 {
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));
239 }
240
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));
247 }
248
249 // Remove the items 6 , 8 and 10
250 m_selectionManager->itemsRemoved(KItemRangeList() <<
251 KItemRange(6, 1) <<
252 KItemRange(8, 1) <<
253 KItemRange(10, 1));
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));
259 }
260
261 void KItemListSelectionManagerTest::testAnchoredSelection()
262 {
263 m_selectionManager->beginAnchoredSelection(5);
264 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
265 QCOMPARE(m_selectionManager->m_anchorItem, 5);
266
267 m_selectionManager->setCurrentItem(6);
268 QCOMPARE(m_selectionManager->currentItem(), 6);
269 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6);
270
271 m_selectionManager->setCurrentItem(4);
272 QCOMPARE(m_selectionManager->currentItem(), 4);
273 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
274
275 m_selectionManager->setCurrentItem(7);
276 QCOMPARE(m_selectionManager->currentItem(), 7);
277 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7);
278
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);
283
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);
288
289 m_selectionManager->setCurrentItem(6);
290 QCOMPARE(m_selectionManager->currentItem(), 6);
291 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
292
293 m_selectionManager->setCurrentItem(10);
294 QCOMPARE(m_selectionManager->currentItem(), 10);
295 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
296
297 m_selectionManager->endAnchoredSelection();
298 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
299 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
300 }
301
302 namespace {
303 enum ChangeType {
304 NoChange,
305 InsertItems,
306 RemoveItems,
307 MoveItems,
308 EndAnchoredSelection,
309 SetSelected
310 };
311 }
312
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>)
319
320 /**
321 * The following function provides a generic way to test the selection functionality.
322 *
323 * The test is data-driven and takes the following arguments:
324 *
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:
330 * - NoChange
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.
343 *
344 */
345 void KItemListSelectionManagerTest::testChangeSelection_data()
346 {
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");
354
355 QTest::newRow("No change")
356 << (KItemSet() << 5 << 6)
357 << 2 << 3
358 << (KItemSet() << 2 << 3 << 5 << 6)
359 << NoChange
360 << QList<QVariant>{}
361 << (KItemSet() << 2 << 3 << 5 << 6);
362
363 QTest::newRow("Insert Items")
364 << (KItemSet() << 5 << 6)
365 << 2 << 3
366 << (KItemSet() << 2 << 3 << 5 << 6)
367 << InsertItems
368 << QList<QVariant>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
369 << (KItemSet() << 3 << 4 << 8 << 9);
370
371 QTest::newRow("Remove Items")
372 << (KItemSet() << 5 << 6)
373 << 2 << 3
374 << (KItemSet() << 2 << 3 << 5 << 6)
375 << RemoveItems
376 << QList<QVariant>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
377 << (KItemSet() << 1 << 2 << 3 << 4);
378
379 QTest::newRow("Empty Anchored Selection")
380 << KItemSet()
381 << 2 << 2
382 << KItemSet()
383 << EndAnchoredSelection
384 << QList<QVariant>{}
385 << KItemSet();
386
387 QTest::newRow("Toggle selection")
388 << (KItemSet() << 1 << 3 << 4)
389 << 6 << 8
390 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
391 << SetSelected
392 << QList<QVariant>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle)}
393 << (KItemSet() << 0 << 2 << 5 << 9);
394
395 // Swap items 2, 3 and 4, 5
396 QTest::newRow("Move items")
397 << (KItemSet() << 0 << 1 << 2 << 3)
398 << -1 << -1
399 << (KItemSet() << 0 << 1 << 2 << 3)
400 << MoveItems
401 << QList<QVariant>{QVariant::fromValue(KItemRange(2, 4)),
402 QVariant::fromValue(QList<int>{4, 5, 2, 3})}
403 << (KItemSet() << 0 << 1 << 4 << 5);
404
405 QTest::newRow("Move items with active anchored selection")
406 << KItemSet()
407 << 0 << 3
408 << (KItemSet() << 0 << 1 << 2 << 3)
409 << MoveItems
410 << QList<QVariant>{QVariant::fromValue(KItemRange(2, 4)),
411 QVariant::fromValue(QList<int>{4, 5, 2, 3})}
412 << (KItemSet() << 0 << 1 << 4 << 5);
413
414 // Revert sort order
415 QTest::newRow("Revert sort order")
416 << (KItemSet() << 0 << 1)
417 << 3 << 4
418 << (KItemSet() << 0 << 1 << 3 << 4)
419 << MoveItems
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);
423 }
424
425 void KItemListSelectionManagerTest::testChangeSelection()
426 {
427 QFETCH(KItemSet, initialSelection);
428 QFETCH(int, anchor);
429 QFETCH(int, current);
430 QFETCH(KItemSet, expectedSelection);
431 QFETCH(ChangeType, changeType);
432 QFETCH(QList<QVariant>, data);
433 QFETCH(KItemSet, finalSelection);
434
435 QSignalSpy spySelectionChanged(m_selectionManager, &KItemListSelectionManager::selectionChanged);
436
437 // Initial selection should be empty
438 QVERIFY(!m_selectionManager->hasSelection());
439 QVERIFY(m_selectionManager->selectedItems().isEmpty());
440
441 // Perform the initial selection
442 m_selectionManager->setSelectedItems(initialSelection);
443
444 verifySelectionChange(spySelectionChanged, initialSelection, KItemSet());
445
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);
454
455 verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
456
457 // Change the model by inserting or removing items.
458 switch (changeType) {
459 case InsertItems:
460 m_selectionManager->itemsInserted(data.at(0).value<KItemRangeList>());
461 break;
462 case RemoveItems:
463 m_selectionManager->itemsRemoved(data.at(0).value<KItemRangeList>());
464 break;
465 case MoveItems:
466 m_selectionManager->itemsMoved(data.at(0).value<KItemRange>(),
467 data.at(1).value<QList<int>>());
468 break;
469 case EndAnchoredSelection:
470 m_selectionManager->endAnchoredSelection();
471 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
472 break;
473 case SetSelected:
474 m_selectionManager->setSelected(data.at(0).value<int>(), // index
475 data.at(1).value<int>(), // count
476 data.at(2).value<KItemListSelectionManager::SelectionMode>());
477 break;
478 case NoChange:
479 break;
480 }
481
482 verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
483
484 // Finally, clear the selection
485 m_selectionManager->clearSelection();
486
487 verifySelectionChange(spySelectionChanged, KItemSet(), finalSelection);
488 }
489
490 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
491 {
492 QTest::addColumn<int>("oldCurrentItemIndex");
493 QTest::addColumn<int>("removeIndex");
494 QTest::addColumn<int>("removeCount");
495 QTest::addColumn<int>("newCurrentItemIndex");
496
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;
502 }
503
504 void KItemListSelectionManagerTest::testDeleteCurrentItem()
505 {
506 QFETCH(int, oldCurrentItemIndex);
507 QFETCH(int, removeIndex);
508 QFETCH(int, removeCount);
509 QFETCH(int, newCurrentItemIndex);
510
511 m_selectionManager->setCurrentItem(oldCurrentItemIndex);
512
513 const int newCount = m_model->count() - removeCount;
514 m_model->setCount(newCount);
515 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(removeIndex, removeCount));
516
517 QCOMPARE(m_selectionManager->currentItem(), newCurrentItemIndex);
518 }
519
520 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
521 {
522 m_selectionManager->setCurrentItem(4);
523 m_selectionManager->beginAnchoredSelection(4);
524
525 // Reverse the items between 0 and 5.
526 m_selectionManager->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
527
528 QCOMPARE(m_selectionManager->currentItem(), 1);
529 QCOMPARE(m_selectionManager->m_anchorItem, 1);
530
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);
534 }
535
536 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
537 const KItemSet& currentSelection,
538 const KItemSet& previousSelection) const
539 {
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));
545 }
546 else {
547 QVERIFY(!m_selectionManager->isSelected(index));
548 }
549 }
550
551 if (currentSelection == previousSelection) {
552 QCOMPARE(spy.count(), 0);
553 }
554 else {
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);
559 }
560 }
561
562 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest)
563
564 #include "kitemlistselectionmanagertest.moc"