1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "kitemviews/kitemmodelbase.h"
22 #include "kitemviews/kitemlistselectionmanager.h"
27 class DummyModel
: public KItemModelBase
32 void setCount(int count
);
33 virtual int count() const;
34 virtual QHash
<QByteArray
, QVariant
> data(int index
) const;
40 DummyModel::DummyModel() :
46 void DummyModel::setCount(int count
)
51 int DummyModel::count() const
56 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
59 return QHash
<QByteArray
, QVariant
>();
63 class KItemListSelectionManagerTest
: public QObject
71 void testConstructor();
73 void testCurrentItemAnchorItem();
74 void testSetSelected_data();
75 void testSetSelected();
76 void testItemsInserted();
77 void testItemsRemoved();
78 void testAnchoredSelection();
79 void testChangeSelection_data();
80 void testChangeSelection();
81 void testDeleteCurrentItem_data();
82 void testDeleteCurrentItem();
83 void testAnchoredSelectionAfterMovingItems();
86 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
88 KItemListSelectionManager
* m_selectionManager
;
92 void KItemListSelectionManagerTest::init()
94 m_model
= new DummyModel();
95 m_selectionManager
= new KItemListSelectionManager();
96 m_selectionManager
->setModel(m_model
);
99 void KItemListSelectionManagerTest::cleanup()
101 delete m_selectionManager
;
102 m_selectionManager
= 0;
108 void KItemListSelectionManagerTest::testConstructor()
110 QVERIFY(!m_selectionManager
->hasSelection());
111 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
112 QCOMPARE(m_selectionManager
->currentItem(), 0);
113 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
116 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
118 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
120 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
121 m_selectionManager
->setCurrentItem(4);
122 QCOMPARE(m_selectionManager
->currentItem(), 4);
123 QCOMPARE(spyCurrent
.count(), 1);
124 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
125 spyCurrent
.takeFirst();
127 // Begin an anchored selection.
128 m_selectionManager
->beginAnchoredSelection(5);
129 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
130 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
132 // Items between current and anchor should be selected now
133 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
134 QVERIFY(m_selectionManager
->hasSelection());
136 // Change current item again and check the selection
137 m_selectionManager
->setCurrentItem(2);
138 QCOMPARE(m_selectionManager
->currentItem(), 2);
139 QCOMPARE(spyCurrent
.count(), 1);
140 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
141 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
142 spyCurrent
.takeFirst();
144 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
145 QVERIFY(m_selectionManager
->hasSelection());
147 // Inserting items should update current item and anchor item.
148 m_selectionManager
->itemsInserted(KItemRangeList() <<
153 QCOMPARE(m_selectionManager
->currentItem(), 5);
154 QCOMPARE(spyCurrent
.count(), 1);
155 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
156 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
157 spyCurrent
.takeFirst();
159 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
161 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
162 QVERIFY(m_selectionManager
->hasSelection());
164 // Removing items should update current item and anchor item.
165 m_selectionManager
->itemsRemoved(KItemRangeList() <<
170 QCOMPARE(m_selectionManager
->currentItem(), 2);
171 QCOMPARE(spyCurrent
.count(), 1);
172 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
173 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
174 spyCurrent
.takeFirst();
176 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
178 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
179 QVERIFY(m_selectionManager
->hasSelection());
181 // Verify that clearSelection() also clears the anchored selection.
182 m_selectionManager
->clearSelection();
183 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
184 QVERIFY(!m_selectionManager
->hasSelection());
186 m_selectionManager
->endAnchoredSelection();
187 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
190 void KItemListSelectionManagerTest::testSetSelected_data()
192 QTest::addColumn
<int>("index");
193 QTest::addColumn
<int>("count");
194 QTest::addColumn
<int>("expectedSelectionCount");
196 QTest::newRow("Select all") << 0 << 100 << 100;
197 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
198 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
199 QTest::newRow("Too small index") << -1 << 100 << 0;
200 QTest::newRow("Too large index") << 100 << 100 << 0;
201 QTest::newRow("Too large count") << 0 << 100000 << 100;
202 QTest::newRow("Too small count") << 0 << 0 << 0;
205 void KItemListSelectionManagerTest::testSetSelected()
209 QFETCH(int, expectedSelectionCount
);
210 m_selectionManager
->setSelected(index
, count
);
211 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
214 void KItemListSelectionManagerTest::testItemsInserted()
216 // Select items 10 to 12
217 m_selectionManager
->setSelected(10, 3);
218 KItemSet selectedItems
= m_selectionManager
->selectedItems();
219 QCOMPARE(selectedItems
.count(), 3);
220 QVERIFY(selectedItems
.contains(10));
221 QVERIFY(selectedItems
.contains(11));
222 QVERIFY(selectedItems
.contains(12));
224 // Insert items 0 to 4 -> selection must be 15 to 17
225 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
226 selectedItems
= m_selectionManager
->selectedItems();
227 QCOMPARE(selectedItems
.count(), 3);
228 QVERIFY(selectedItems
.contains(15));
229 QVERIFY(selectedItems
.contains(16));
230 QVERIFY(selectedItems
.contains(17));
232 // Insert 3 items between the selections
233 m_selectionManager
->itemsInserted(KItemRangeList() <<
237 selectedItems
= m_selectionManager
->selectedItems();
238 QCOMPARE(selectedItems
.count(), 3);
239 QVERIFY(selectedItems
.contains(16));
240 QVERIFY(selectedItems
.contains(18));
241 QVERIFY(selectedItems
.contains(20));
244 void KItemListSelectionManagerTest::testItemsRemoved()
246 // Select items 10 to 15
247 m_selectionManager
->setSelected(10, 6);
248 KItemSet selectedItems
= m_selectionManager
->selectedItems();
249 QCOMPARE(selectedItems
.count(), 6);
250 for (int i
= 10; i
<= 15; ++i
) {
251 QVERIFY(selectedItems
.contains(i
));
254 // Remove items 0 to 4 -> selection must be 5 to 10
255 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
256 selectedItems
= m_selectionManager
->selectedItems();
257 QCOMPARE(selectedItems
.count(), 6);
258 for (int i
= 5; i
<= 10; ++i
) {
259 QVERIFY(selectedItems
.contains(i
));
262 // Remove the items 6 , 8 and 10
263 m_selectionManager
->itemsRemoved(KItemRangeList() <<
267 selectedItems
= m_selectionManager
->selectedItems();
268 QCOMPARE(selectedItems
.count(), 3);
269 QVERIFY(selectedItems
.contains(5));
270 QVERIFY(selectedItems
.contains(6));
271 QVERIFY(selectedItems
.contains(7));
274 void KItemListSelectionManagerTest::testAnchoredSelection()
276 m_selectionManager
->beginAnchoredSelection(5);
277 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
278 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
280 m_selectionManager
->setCurrentItem(6);
281 QCOMPARE(m_selectionManager
->currentItem(), 6);
282 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
284 m_selectionManager
->setCurrentItem(4);
285 QCOMPARE(m_selectionManager
->currentItem(), 4);
286 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
288 m_selectionManager
->setCurrentItem(7);
289 QCOMPARE(m_selectionManager
->currentItem(), 7);
290 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
292 // Ending the anchored selection should not change the selected items.
293 m_selectionManager
->endAnchoredSelection();
294 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
295 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
297 // Start a new anchored selection that overlaps the previous one
298 m_selectionManager
->beginAnchoredSelection(9);
299 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
300 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
302 m_selectionManager
->setCurrentItem(6);
303 QCOMPARE(m_selectionManager
->currentItem(), 6);
304 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
306 m_selectionManager
->setCurrentItem(10);
307 QCOMPARE(m_selectionManager
->currentItem(), 10);
308 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
310 m_selectionManager
->endAnchoredSelection();
311 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
312 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
321 EndAnchoredSelection
,
326 Q_DECLARE_METATYPE(KItemSet
)
327 Q_DECLARE_METATYPE(ChangeType
)
328 Q_DECLARE_METATYPE(KItemRange
)
329 Q_DECLARE_METATYPE(KItemRangeList
)
330 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
)
331 Q_DECLARE_METATYPE(QList
<int>)
334 * The following function provides a generic way to test the selection functionality.
336 * The test is data-driven and takes the following arguments:
338 * \param initialSelection The selection at the beginning.
339 * \param anchor This item will be the anchor item.
340 * \param current This item will be the current item.
341 * \param expectedSelection Expected selection after anchor and current are set.
342 * \param changeType Type of the change that is done then:
344 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
345 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
346 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
347 * data.at(1) provides the list containing the new indices
348 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
349 * - EndAnchoredSelection
350 * - SetSelected -> data.at(0) provides the index where the selection process starts,
351 * data.at(1) provides the number of indices to be selected,
352 * data.at(2) provides the selection mode.
353 * \sa KItemListSelectionManager::setSelected()
354 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
355 * \param finalSelection The expected final selection.
359 void KItemListSelectionManagerTest::testChangeSelection_data()
361 QTest::addColumn
<KItemSet
>("initialSelection");
362 QTest::addColumn
<int>("anchor");
363 QTest::addColumn
<int>("current");
364 QTest::addColumn
<KItemSet
>("expectedSelection");
365 QTest::addColumn
<ChangeType
>("changeType");
366 QTest::addColumn
<QList
<QVariant
> >("data");
367 QTest::addColumn
<KItemSet
>("finalSelection");
369 QTest::newRow("No change")
370 << (KItemSet() << 5 << 6)
372 << (KItemSet() << 2 << 3 << 5 << 6)
375 << (KItemSet() << 2 << 3 << 5 << 6);
377 QTest::newRow("Insert Items")
378 << (KItemSet() << 5 << 6)
380 << (KItemSet() << 2 << 3 << 5 << 6)
382 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
383 << (KItemSet() << 3 << 4 << 8 << 9);
385 QTest::newRow("Remove Items")
386 << (KItemSet() << 5 << 6)
388 << (KItemSet() << 2 << 3 << 5 << 6)
390 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
391 << (KItemSet() << 1 << 2 << 3 << 4);
393 QTest::newRow("Empty Anchored Selection")
397 << EndAnchoredSelection
401 QTest::newRow("Toggle selection")
402 << (KItemSet() << 1 << 3 << 4)
404 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
406 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)}
407 << (KItemSet() << 0 << 2 << 5 << 9);
409 // Swap items 2, 3 and 4, 5
410 QTest::newRow("Move items")
411 << (KItemSet() << 0 << 1 << 2 << 3)
413 << (KItemSet() << 0 << 1 << 2 << 3)
415 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
416 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
417 << (KItemSet() << 0 << 1 << 4 << 5);
419 QTest::newRow("Move items with active anchored selection")
422 << (KItemSet() << 0 << 1 << 2 << 3)
424 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
425 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
426 << (KItemSet() << 0 << 1 << 4 << 5);
429 QTest::newRow("Revert sort order")
430 << (KItemSet() << 0 << 1)
432 << (KItemSet() << 0 << 1 << 3 << 4)
434 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)),
435 QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
436 << (KItemSet() << 5 << 6 << 8 << 9);
439 void KItemListSelectionManagerTest::testChangeSelection()
441 QFETCH(KItemSet
, initialSelection
);
443 QFETCH(int, current
);
444 QFETCH(KItemSet
, expectedSelection
);
445 QFETCH(ChangeType
, changeType
);
446 QFETCH(QList
<QVariant
>, data
);
447 QFETCH(KItemSet
, finalSelection
);
449 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(KItemSet
,KItemSet
)));
451 // Initial selection should be empty
452 QVERIFY(!m_selectionManager
->hasSelection());
453 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
455 // Perform the initial selectiion
456 m_selectionManager
->setSelectedItems(initialSelection
);
458 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
460 // Perform an anchored selection.
461 // Note that current and anchor index are equal first because this is the case in typical uses of the
462 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
463 m_selectionManager
->setCurrentItem(anchor
);
464 m_selectionManager
->beginAnchoredSelection(anchor
);
465 m_selectionManager
->setCurrentItem(current
);
466 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
467 QCOMPARE(m_selectionManager
->currentItem(), current
);
469 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
471 // Change the model by inserting or removing items.
472 switch (changeType
) {
474 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
477 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
480 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
481 data
.at(1).value
<QList
<int>>());
483 case EndAnchoredSelection
:
484 m_selectionManager
->endAnchoredSelection();
485 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
488 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
489 data
.at(1).value
<int>(), // count
490 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
496 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
498 // Finally, clear the selection
499 m_selectionManager
->clearSelection();
501 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
504 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
506 QTest::addColumn
<int>("oldCurrentItemIndex");
507 QTest::addColumn
<int>("removeIndex");
508 QTest::addColumn
<int>("removeCount");
509 QTest::addColumn
<int>("newCurrentItemIndex");
511 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
512 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
513 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
514 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
515 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
518 void KItemListSelectionManagerTest::testDeleteCurrentItem()
520 QFETCH(int, oldCurrentItemIndex
);
521 QFETCH(int, removeIndex
);
522 QFETCH(int, removeCount
);
523 QFETCH(int, newCurrentItemIndex
);
525 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
527 const int newCount
= m_model
->count() - removeCount
;
528 m_model
->setCount(newCount
);
529 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
531 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
534 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
536 m_selectionManager
->setCurrentItem(4);
537 m_selectionManager
->beginAnchoredSelection(4);
539 // Reverse the items between 0 and 5.
540 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
542 QCOMPARE(m_selectionManager
->currentItem(), 1);
543 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
545 // Make 2 the current item -> 1 and 2 should be selected.
546 m_selectionManager
->setCurrentItem(2);
547 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
550 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
551 const KItemSet
& currentSelection
,
552 const KItemSet
& previousSelection
) const
554 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
555 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
556 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
557 if (currentSelection
.contains(index
)) {
558 QVERIFY(m_selectionManager
->isSelected(index
));
561 QVERIFY(!m_selectionManager
->isSelected(index
));
565 if (currentSelection
== previousSelection
) {
566 QCOMPARE(spy
.count(), 0);
569 QCOMPARE(spy
.count(), 1);
570 QList
<QVariant
> arguments
= spy
.takeFirst();
571 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
572 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
576 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
578 #include "kitemlistselectionmanagertest.moc"