]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Enable test mode in all test
[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 #include <QStandardPaths>
14
15 class DummyModel : public KItemModelBase
16 {
17 Q_OBJECT
18 public:
19 DummyModel();
20 void setCount(int count);
21 int count() const override;
22 QHash<QByteArray, QVariant> data(int index) const override;
23
24 private:
25 int m_count;
26 };
27
28 DummyModel::DummyModel() :
29 KItemModelBase(),
30 m_count(100)
31 {
32 }
33
34 void DummyModel::setCount(int count)
35 {
36 m_count = count;
37 }
38
39 int DummyModel::count() const
40 {
41 return m_count;
42 }
43
44 QHash<QByteArray, QVariant> DummyModel::data(int index) const
45 {
46 Q_UNUSED(index)
47 return QHash<QByteArray, QVariant>();
48 }
49
50
51 class KItemListSelectionManagerTest : public QObject
52 {
53 Q_OBJECT
54
55 private Q_SLOTS:
56 void initTestCase();
57 void init();
58 void cleanup();
59
60 void testConstructor();
61
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();
73
74 private:
75 void verifySelectionChange(QSignalSpy& spy, const KItemSet& currentSelection, const KItemSet& previousSelection) const;
76
77 KItemListSelectionManager* m_selectionManager;
78 DummyModel* m_model;
79 };
80
81 void KItemListSelectionManagerTest::initTestCase()
82 {
83 QStandardPaths::setTestModeEnabled(true);
84 }
85
86 void KItemListSelectionManagerTest::init()
87 {
88 m_model = new DummyModel();
89 m_selectionManager = new KItemListSelectionManager();
90 m_selectionManager->setModel(m_model);
91 }
92
93 void KItemListSelectionManagerTest::cleanup()
94 {
95 delete m_selectionManager;
96 m_selectionManager = nullptr;
97
98 delete m_model;
99 m_model = nullptr;
100 }
101
102 void KItemListSelectionManagerTest::testConstructor()
103 {
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);
108 }
109
110 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
111 {
112 QSignalSpy spyCurrent(m_selectionManager, &KItemListSelectionManager::currentChanged);
113
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();
120
121 // Begin an anchored selection.
122 m_selectionManager->beginAnchoredSelection(5);
123 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
124 QCOMPARE(m_selectionManager->m_anchorItem, 5);
125
126 // Items between current and anchor should be selected now
127 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
128 QVERIFY(m_selectionManager->hasSelection());
129
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();
137
138 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
139 QVERIFY(m_selectionManager->hasSelection());
140
141 // Inserting items should update current item and anchor item.
142 m_selectionManager->itemsInserted(KItemRangeList() <<
143 KItemRange(0, 1) <<
144 KItemRange(2, 2) <<
145 KItemRange(6, 3));
146
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();
152
153 QCOMPARE(m_selectionManager->m_anchorItem, 8);
154
155 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
156 QVERIFY(m_selectionManager->hasSelection());
157
158 // Removing items should update current item and anchor item.
159 m_selectionManager->itemsRemoved(KItemRangeList() <<
160 KItemRange(0, 2) <<
161 KItemRange(2, 1) <<
162 KItemRange(9, 2));
163
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();
169
170 QCOMPARE(m_selectionManager->m_anchorItem, 5);
171
172 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
173 QVERIFY(m_selectionManager->hasSelection());
174
175 // Verify that clearSelection() also clears the anchored selection.
176 m_selectionManager->clearSelection();
177 QCOMPARE(m_selectionManager->selectedItems(), KItemSet());
178 QVERIFY(!m_selectionManager->hasSelection());
179
180 m_selectionManager->endAnchoredSelection();
181 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
182 }
183
184 void KItemListSelectionManagerTest::testSetSelected_data()
185 {
186 QTest::addColumn<int>("index");
187 QTest::addColumn<int>("count");
188 QTest::addColumn<int>("expectedSelectionCount");
189
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;
197 }
198
199 void KItemListSelectionManagerTest::testSetSelected()
200 {
201 QFETCH(int, index);
202 QFETCH(int, count);
203 QFETCH(int, expectedSelectionCount);
204 m_selectionManager->setSelected(index, count);
205 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
206 }
207
208 void KItemListSelectionManagerTest::testItemsInserted()
209 {
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));
217
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));
225
226 // Insert 3 items between the selections
227 m_selectionManager->itemsInserted(KItemRangeList() <<
228 KItemRange(15, 1) <<
229 KItemRange(16, 1) <<
230 KItemRange(17, 1));
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));
236 }
237
238 void KItemListSelectionManagerTest::testItemsRemoved()
239 {
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));
246 }
247
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));
254 }
255
256 // Remove the items 6 , 8 and 10
257 m_selectionManager->itemsRemoved(KItemRangeList() <<
258 KItemRange(6, 1) <<
259 KItemRange(8, 1) <<
260 KItemRange(10, 1));
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));
266 }
267
268 void KItemListSelectionManagerTest::testAnchoredSelection()
269 {
270 m_selectionManager->beginAnchoredSelection(5);
271 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
272 QCOMPARE(m_selectionManager->m_anchorItem, 5);
273
274 m_selectionManager->setCurrentItem(6);
275 QCOMPARE(m_selectionManager->currentItem(), 6);
276 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6);
277
278 m_selectionManager->setCurrentItem(4);
279 QCOMPARE(m_selectionManager->currentItem(), 4);
280 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
281
282 m_selectionManager->setCurrentItem(7);
283 QCOMPARE(m_selectionManager->currentItem(), 7);
284 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7);
285
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);
290
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);
295
296 m_selectionManager->setCurrentItem(6);
297 QCOMPARE(m_selectionManager->currentItem(), 6);
298 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
299
300 m_selectionManager->setCurrentItem(10);
301 QCOMPARE(m_selectionManager->currentItem(), 10);
302 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
303
304 m_selectionManager->endAnchoredSelection();
305 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
306 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
307 }
308
309 namespace {
310 enum ChangeType {
311 NoChange,
312 InsertItems,
313 RemoveItems,
314 MoveItems,
315 EndAnchoredSelection,
316 SetSelected
317 };
318 }
319
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>)
326
327 /**
328 * The following function provides a generic way to test the selection functionality.
329 *
330 * The test is data-driven and takes the following arguments:
331 *
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:
337 * - NoChange
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.
350 *
351 */
352 void KItemListSelectionManagerTest::testChangeSelection_data()
353 {
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");
361
362 QTest::newRow("No change")
363 << (KItemSet() << 5 << 6)
364 << 2 << 3
365 << (KItemSet() << 2 << 3 << 5 << 6)
366 << NoChange
367 << QList<QVariant>{}
368 << (KItemSet() << 2 << 3 << 5 << 6);
369
370 QTest::newRow("Insert Items")
371 << (KItemSet() << 5 << 6)
372 << 2 << 3
373 << (KItemSet() << 2 << 3 << 5 << 6)
374 << InsertItems
375 << QList<QVariant>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
376 << (KItemSet() << 3 << 4 << 8 << 9);
377
378 QTest::newRow("Remove Items")
379 << (KItemSet() << 5 << 6)
380 << 2 << 3
381 << (KItemSet() << 2 << 3 << 5 << 6)
382 << RemoveItems
383 << QList<QVariant>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
384 << (KItemSet() << 1 << 2 << 3 << 4);
385
386 QTest::newRow("Empty Anchored Selection")
387 << KItemSet()
388 << 2 << 2
389 << KItemSet()
390 << EndAnchoredSelection
391 << QList<QVariant>{}
392 << KItemSet();
393
394 QTest::newRow("Toggle selection")
395 << (KItemSet() << 1 << 3 << 4)
396 << 6 << 8
397 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
398 << SetSelected
399 << QList<QVariant>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle)}
400 << (KItemSet() << 0 << 2 << 5 << 9);
401
402 // Swap items 2, 3 and 4, 5
403 QTest::newRow("Move items")
404 << (KItemSet() << 0 << 1 << 2 << 3)
405 << -1 << -1
406 << (KItemSet() << 0 << 1 << 2 << 3)
407 << MoveItems
408 << QList<QVariant>{QVariant::fromValue(KItemRange(2, 4)),
409 QVariant::fromValue(QList<int>{4, 5, 2, 3})}
410 << (KItemSet() << 0 << 1 << 4 << 5);
411
412 QTest::newRow("Move items with active anchored selection")
413 << KItemSet()
414 << 0 << 3
415 << (KItemSet() << 0 << 1 << 2 << 3)
416 << MoveItems
417 << QList<QVariant>{QVariant::fromValue(KItemRange(2, 4)),
418 QVariant::fromValue(QList<int>{4, 5, 2, 3})}
419 << (KItemSet() << 0 << 1 << 4 << 5);
420
421 // Revert sort order
422 QTest::newRow("Revert sort order")
423 << (KItemSet() << 0 << 1)
424 << 3 << 4
425 << (KItemSet() << 0 << 1 << 3 << 4)
426 << MoveItems
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);
430 }
431
432 void KItemListSelectionManagerTest::testChangeSelection()
433 {
434 QFETCH(KItemSet, initialSelection);
435 QFETCH(int, anchor);
436 QFETCH(int, current);
437 QFETCH(KItemSet, expectedSelection);
438 QFETCH(ChangeType, changeType);
439 QFETCH(QList<QVariant>, data);
440 QFETCH(KItemSet, finalSelection);
441
442 QSignalSpy spySelectionChanged(m_selectionManager, &KItemListSelectionManager::selectionChanged);
443
444 // Initial selection should be empty
445 QVERIFY(!m_selectionManager->hasSelection());
446 QVERIFY(m_selectionManager->selectedItems().isEmpty());
447
448 // Perform the initial selection
449 m_selectionManager->setSelectedItems(initialSelection);
450
451 verifySelectionChange(spySelectionChanged, initialSelection, KItemSet());
452
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);
461
462 verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
463
464 // Change the model by inserting or removing items.
465 switch (changeType) {
466 case InsertItems:
467 m_selectionManager->itemsInserted(data.at(0).value<KItemRangeList>());
468 break;
469 case RemoveItems:
470 m_selectionManager->itemsRemoved(data.at(0).value<KItemRangeList>());
471 break;
472 case MoveItems:
473 m_selectionManager->itemsMoved(data.at(0).value<KItemRange>(),
474 data.at(1).value<QList<int>>());
475 break;
476 case EndAnchoredSelection:
477 m_selectionManager->endAnchoredSelection();
478 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
479 break;
480 case SetSelected:
481 m_selectionManager->setSelected(data.at(0).value<int>(), // index
482 data.at(1).value<int>(), // count
483 data.at(2).value<KItemListSelectionManager::SelectionMode>());
484 break;
485 case NoChange:
486 break;
487 }
488
489 verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
490
491 // Finally, clear the selection
492 m_selectionManager->clearSelection();
493
494 verifySelectionChange(spySelectionChanged, KItemSet(), finalSelection);
495 }
496
497 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
498 {
499 QTest::addColumn<int>("oldCurrentItemIndex");
500 QTest::addColumn<int>("removeIndex");
501 QTest::addColumn<int>("removeCount");
502 QTest::addColumn<int>("newCurrentItemIndex");
503
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;
509 }
510
511 void KItemListSelectionManagerTest::testDeleteCurrentItem()
512 {
513 QFETCH(int, oldCurrentItemIndex);
514 QFETCH(int, removeIndex);
515 QFETCH(int, removeCount);
516 QFETCH(int, newCurrentItemIndex);
517
518 m_selectionManager->setCurrentItem(oldCurrentItemIndex);
519
520 const int newCount = m_model->count() - removeCount;
521 m_model->setCount(newCount);
522 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(removeIndex, removeCount));
523
524 QCOMPARE(m_selectionManager->currentItem(), newCurrentItemIndex);
525 }
526
527 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
528 {
529 m_selectionManager->setCurrentItem(4);
530 m_selectionManager->beginAnchoredSelection(4);
531
532 // Reverse the items between 0 and 5.
533 m_selectionManager->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
534
535 QCOMPARE(m_selectionManager->currentItem(), 1);
536 QCOMPARE(m_selectionManager->m_anchorItem, 1);
537
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);
541 }
542
543 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
544 const KItemSet& currentSelection,
545 const KItemSet& previousSelection) const
546 {
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));
552 }
553 else {
554 QVERIFY(!m_selectionManager->isSelected(index));
555 }
556 }
557
558 if (currentSelection == previousSelection) {
559 QCOMPARE(spy.count(), 0);
560 }
561 else {
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);
566 }
567 }
568
569 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest)
570
571 #include "kitemlistselectionmanagertest.moc"