]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
GIT_SILENT Sync po/docbooks with svn
[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/kitemlistselectionmanager.h"
9 #include "kitemviews/kitemmodelbase.h"
10
11 #include <QSignalSpy>
12 #include <QStandardPaths>
13 #include <QTest>
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 class KItemListSelectionManagerTest : public QObject
51 {
52 Q_OBJECT
53
54 private Q_SLOTS:
55 void initTestCase();
56 void init();
57 void cleanup();
58
59 void testConstructor();
60
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();
72
73 private:
74 void verifySelectionChange(QSignalSpy &spy, const KItemSet &currentSelection, const KItemSet &previousSelection) const;
75
76 KItemListSelectionManager *m_selectionManager;
77 DummyModel *m_model;
78 };
79
80 void KItemListSelectionManagerTest::initTestCase()
81 {
82 QStandardPaths::setTestModeEnabled(true);
83 }
84
85 void KItemListSelectionManagerTest::init()
86 {
87 m_model = new DummyModel();
88 m_selectionManager = new KItemListSelectionManager();
89 m_selectionManager->setModel(m_model);
90 }
91
92 void KItemListSelectionManagerTest::cleanup()
93 {
94 delete m_selectionManager;
95 m_selectionManager = nullptr;
96
97 delete m_model;
98 m_model = nullptr;
99 }
100
101 void KItemListSelectionManagerTest::testConstructor()
102 {
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);
107 }
108
109 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
110 {
111 QSignalSpy spyCurrent(m_selectionManager, &KItemListSelectionManager::currentChanged);
112
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();
119
120 // Begin an anchored selection.
121 m_selectionManager->beginAnchoredSelection(5);
122 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
123 QCOMPARE(m_selectionManager->m_anchorItem, 5);
124
125 // Items between current and anchor should be selected now
126 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
127 QVERIFY(m_selectionManager->hasSelection());
128
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();
136
137 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
138 QVERIFY(m_selectionManager->hasSelection());
139
140 // Inserting items should update current item and anchor item.
141 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 1) << KItemRange(2, 2) << KItemRange(6, 3));
142
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();
148
149 QCOMPARE(m_selectionManager->m_anchorItem, 8);
150
151 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
152 QVERIFY(m_selectionManager->hasSelection());
153
154 // Removing items should update current item and anchor item.
155 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 2) << KItemRange(2, 1) << 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() << 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));
226 }
227
228 void KItemListSelectionManagerTest::testItemsRemoved()
229 {
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));
236 }
237
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));
244 }
245
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));
253 }
254
255 void KItemListSelectionManagerTest::testAnchoredSelection()
256 {
257 m_selectionManager->beginAnchoredSelection(5);
258 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
259 QCOMPARE(m_selectionManager->m_anchorItem, 5);
260
261 m_selectionManager->setCurrentItem(6);
262 QCOMPARE(m_selectionManager->currentItem(), 6);
263 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6);
264
265 m_selectionManager->setCurrentItem(4);
266 QCOMPARE(m_selectionManager->currentItem(), 4);
267 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 4 << 5);
268
269 m_selectionManager->setCurrentItem(7);
270 QCOMPARE(m_selectionManager->currentItem(), 7);
271 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7);
272
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);
277
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);
282
283 m_selectionManager->setCurrentItem(6);
284 QCOMPARE(m_selectionManager->currentItem(), 6);
285 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
286
287 m_selectionManager->setCurrentItem(10);
288 QCOMPARE(m_selectionManager->currentItem(), 10);
289 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
290
291 m_selectionManager->endAnchoredSelection();
292 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
293 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
294 }
295
296 namespace
297 {
298 enum ChangeType { NoChange, InsertItems, RemoveItems, MoveItems, EndAnchoredSelection, SetSelected };
299 }
300
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>)
307
308 /**
309 * The following function provides a generic way to test the selection functionality.
310 *
311 * The test is data-driven and takes the following arguments:
312 *
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:
318 * - NoChange
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.
331 *
332 */
333 void KItemListSelectionManagerTest::testChangeSelection_data()
334 {
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");
342
343 QTest::newRow("No change") << (KItemSet() << 5 << 6) << 2 << 3 << (KItemSet() << 2 << 3 << 5 << 6) << NoChange << QList<QVariant>{}
344 << (KItemSet() << 2 << 3 << 5 << 6);
345
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);
349
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);
353
354 QTest::newRow("Empty Anchored Selection") << KItemSet() << 2 << 2 << KItemSet() << EndAnchoredSelection << QList<QVariant>{} << KItemSet();
355
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);
358
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);
363
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);
367
368 // Revert sort order
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);
372 }
373
374 void KItemListSelectionManagerTest::testChangeSelection()
375 {
376 QFETCH(KItemSet, initialSelection);
377 QFETCH(int, anchor);
378 QFETCH(int, current);
379 QFETCH(KItemSet, expectedSelection);
380 QFETCH(ChangeType, changeType);
381 QFETCH(QList<QVariant>, data);
382 QFETCH(KItemSet, finalSelection);
383
384 QSignalSpy spySelectionChanged(m_selectionManager, &KItemListSelectionManager::selectionChanged);
385
386 // Initial selection should be empty
387 QVERIFY(!m_selectionManager->hasSelection());
388 QVERIFY(m_selectionManager->selectedItems().isEmpty());
389
390 // Perform the initial selection
391 m_selectionManager->setSelectedItems(initialSelection);
392
393 verifySelectionChange(spySelectionChanged, initialSelection, KItemSet());
394
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);
403
404 verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
405
406 // Change the model by inserting or removing items.
407 switch (changeType) {
408 case InsertItems:
409 m_selectionManager->itemsInserted(data.at(0).value<KItemRangeList>());
410 break;
411 case RemoveItems:
412 m_selectionManager->itemsRemoved(data.at(0).value<KItemRangeList>());
413 break;
414 case MoveItems:
415 m_selectionManager->itemsMoved(data.at(0).value<KItemRange>(), data.at(1).value<QList<int>>());
416 break;
417 case EndAnchoredSelection:
418 m_selectionManager->endAnchoredSelection();
419 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
420 break;
421 case SetSelected:
422 m_selectionManager->setSelected(data.at(0).value<int>(), // index
423 data.at(1).value<int>(), // count
424 data.at(2).value<KItemListSelectionManager::SelectionMode>());
425 break;
426 case NoChange:
427 break;
428 }
429
430 verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
431
432 // Finally, clear the selection
433 m_selectionManager->clearSelection();
434
435 verifySelectionChange(spySelectionChanged, KItemSet(), finalSelection);
436 }
437
438 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
439 {
440 QTest::addColumn<int>("oldCurrentItemIndex");
441 QTest::addColumn<int>("removeIndex");
442 QTest::addColumn<int>("removeCount");
443 QTest::addColumn<int>("newCurrentItemIndex");
444
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;
450 }
451
452 void KItemListSelectionManagerTest::testDeleteCurrentItem()
453 {
454 QFETCH(int, oldCurrentItemIndex);
455 QFETCH(int, removeIndex);
456 QFETCH(int, removeCount);
457 QFETCH(int, newCurrentItemIndex);
458
459 m_selectionManager->setCurrentItem(oldCurrentItemIndex);
460
461 const int newCount = m_model->count() - removeCount;
462 m_model->setCount(newCount);
463 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(removeIndex, removeCount));
464
465 QCOMPARE(m_selectionManager->currentItem(), newCurrentItemIndex);
466 }
467
468 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
469 {
470 m_selectionManager->setCurrentItem(4);
471 m_selectionManager->beginAnchoredSelection(4);
472
473 // Reverse the items between 0 and 5.
474 m_selectionManager->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
475
476 QCOMPARE(m_selectionManager->currentItem(), 1);
477 QCOMPARE(m_selectionManager->m_anchorItem, 1);
478
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);
482 }
483
484 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy &spy, const KItemSet &currentSelection, const KItemSet &previousSelection) const
485 {
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));
491 } else {
492 QVERIFY(!m_selectionManager->isSelected(index));
493 }
494 }
495
496 if (currentSelection == previousSelection) {
497 QCOMPARE(spy.count(), 0);
498 } else {
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);
503 }
504 }
505
506 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest)
507
508 #include "kitemlistselectionmanagertest.moc"