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
31 void setCount(int count
);
32 virtual int count() const;
33 virtual QHash
<QByteArray
, QVariant
> data(int index
) const;
39 DummyModel::DummyModel() :
45 void DummyModel::setCount(int count
)
50 int DummyModel::count() const
55 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
58 return QHash
<QByteArray
, QVariant
>();
62 class KItemListSelectionManagerTest
: public QObject
70 void testConstructor();
72 void testCurrentItemAnchorItem();
73 void testSetSelected_data();
74 void testSetSelected();
75 void testItemsInserted();
76 void testItemsRemoved();
77 void testAnchoredSelection();
78 void testChangeSelection_data();
79 void testChangeSelection();
80 void testDeleteCurrentItem_data();
81 void testDeleteCurrentItem();
82 void testAnchoredSelectionAfterMovingItems();
85 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
87 KItemListSelectionManager
* m_selectionManager
;
91 void KItemListSelectionManagerTest::init()
93 m_model
= new DummyModel();
94 m_selectionManager
= new KItemListSelectionManager();
95 m_selectionManager
->setModel(m_model
);
98 void KItemListSelectionManagerTest::cleanup()
100 delete m_selectionManager
;
101 m_selectionManager
= 0;
107 void KItemListSelectionManagerTest::testConstructor()
109 QVERIFY(!m_selectionManager
->hasSelection());
110 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
111 QCOMPARE(m_selectionManager
->currentItem(), 0);
112 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
115 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
117 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
119 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
120 m_selectionManager
->setCurrentItem(4);
121 QCOMPARE(m_selectionManager
->currentItem(), 4);
122 QCOMPARE(spyCurrent
.count(), 1);
123 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
124 spyCurrent
.takeFirst();
126 // Begin an anchored selection.
127 m_selectionManager
->beginAnchoredSelection(5);
128 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
129 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
131 // Items between current and anchor should be selected now
132 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
133 QVERIFY(m_selectionManager
->hasSelection());
135 // Change current item again and check the selection
136 m_selectionManager
->setCurrentItem(2);
137 QCOMPARE(m_selectionManager
->currentItem(), 2);
138 QCOMPARE(spyCurrent
.count(), 1);
139 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
140 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
141 spyCurrent
.takeFirst();
143 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
144 QVERIFY(m_selectionManager
->hasSelection());
146 // Inserting items should update current item and anchor item.
147 m_selectionManager
->itemsInserted(KItemRangeList() <<
152 QCOMPARE(m_selectionManager
->currentItem(), 5);
153 QCOMPARE(spyCurrent
.count(), 1);
154 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
155 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
156 spyCurrent
.takeFirst();
158 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
160 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
161 QVERIFY(m_selectionManager
->hasSelection());
163 // Removing items should update current item and anchor item.
164 m_selectionManager
->itemsRemoved(KItemRangeList() <<
169 QCOMPARE(m_selectionManager
->currentItem(), 2);
170 QCOMPARE(spyCurrent
.count(), 1);
171 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
172 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
173 spyCurrent
.takeFirst();
175 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
177 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
178 QVERIFY(m_selectionManager
->hasSelection());
180 // Verify that clearSelection() also clears the anchored selection.
181 m_selectionManager
->clearSelection();
182 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
183 QVERIFY(!m_selectionManager
->hasSelection());
185 m_selectionManager
->endAnchoredSelection();
186 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
189 void KItemListSelectionManagerTest::testSetSelected_data()
191 QTest::addColumn
<int>("index");
192 QTest::addColumn
<int>("count");
193 QTest::addColumn
<int>("expectedSelectionCount");
195 QTest::newRow("Select all") << 0 << 100 << 100;
196 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
197 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
198 QTest::newRow("Too small index") << -1 << 100 << 0;
199 QTest::newRow("Too large index") << 100 << 100 << 0;
200 QTest::newRow("Too large count") << 0 << 100000 << 100;
201 QTest::newRow("Too small count") << 0 << 0 << 0;
204 void KItemListSelectionManagerTest::testSetSelected()
208 QFETCH(int, expectedSelectionCount
);
209 m_selectionManager
->setSelected(index
, count
);
210 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
213 void KItemListSelectionManagerTest::testItemsInserted()
215 // Select items 10 to 12
216 m_selectionManager
->setSelected(10, 3);
217 KItemSet selectedItems
= m_selectionManager
->selectedItems();
218 QCOMPARE(selectedItems
.count(), 3);
219 QVERIFY(selectedItems
.contains(10));
220 QVERIFY(selectedItems
.contains(11));
221 QVERIFY(selectedItems
.contains(12));
223 // Insert items 0 to 4 -> selection must be 15 to 17
224 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
225 selectedItems
= m_selectionManager
->selectedItems();
226 QCOMPARE(selectedItems
.count(), 3);
227 QVERIFY(selectedItems
.contains(15));
228 QVERIFY(selectedItems
.contains(16));
229 QVERIFY(selectedItems
.contains(17));
231 // Insert 3 items between the selections
232 m_selectionManager
->itemsInserted(KItemRangeList() <<
236 selectedItems
= m_selectionManager
->selectedItems();
237 QCOMPARE(selectedItems
.count(), 3);
238 QVERIFY(selectedItems
.contains(16));
239 QVERIFY(selectedItems
.contains(18));
240 QVERIFY(selectedItems
.contains(20));
243 void KItemListSelectionManagerTest::testItemsRemoved()
245 // Select items 10 to 15
246 m_selectionManager
->setSelected(10, 6);
247 KItemSet selectedItems
= m_selectionManager
->selectedItems();
248 QCOMPARE(selectedItems
.count(), 6);
249 for (int i
= 10; i
<= 15; ++i
) {
250 QVERIFY(selectedItems
.contains(i
));
253 // Remove items 0 to 4 -> selection must be 5 to 10
254 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
255 selectedItems
= m_selectionManager
->selectedItems();
256 QCOMPARE(selectedItems
.count(), 6);
257 for (int i
= 5; i
<= 10; ++i
) {
258 QVERIFY(selectedItems
.contains(i
));
261 // Remove the items 6 , 8 and 10
262 m_selectionManager
->itemsRemoved(KItemRangeList() <<
266 selectedItems
= m_selectionManager
->selectedItems();
267 QCOMPARE(selectedItems
.count(), 3);
268 QVERIFY(selectedItems
.contains(5));
269 QVERIFY(selectedItems
.contains(6));
270 QVERIFY(selectedItems
.contains(7));
273 void KItemListSelectionManagerTest::testAnchoredSelection()
275 m_selectionManager
->beginAnchoredSelection(5);
276 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
277 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
279 m_selectionManager
->setCurrentItem(6);
280 QCOMPARE(m_selectionManager
->currentItem(), 6);
281 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
283 m_selectionManager
->setCurrentItem(4);
284 QCOMPARE(m_selectionManager
->currentItem(), 4);
285 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
287 m_selectionManager
->setCurrentItem(7);
288 QCOMPARE(m_selectionManager
->currentItem(), 7);
289 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
291 // Ending the anchored selection should not change the selected items.
292 m_selectionManager
->endAnchoredSelection();
293 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
294 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
296 // Start a new anchored selection that overlaps the previous one
297 m_selectionManager
->beginAnchoredSelection(9);
298 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
299 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
301 m_selectionManager
->setCurrentItem(6);
302 QCOMPARE(m_selectionManager
->currentItem(), 6);
303 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
305 m_selectionManager
->setCurrentItem(10);
306 QCOMPARE(m_selectionManager
->currentItem(), 10);
307 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
309 m_selectionManager
->endAnchoredSelection();
310 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
311 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
320 EndAnchoredSelection
,
325 Q_DECLARE_METATYPE(KItemSet
);
326 Q_DECLARE_METATYPE(ChangeType
);
327 Q_DECLARE_METATYPE(KItemRange
);
328 Q_DECLARE_METATYPE(KItemRangeList
);
329 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
);
330 Q_DECLARE_METATYPE(QList
<int>);
333 * The following function provides a generic way to test the selection functionality.
335 * The test is data-driven and takes the following arguments:
337 * \param initialSelection The selection at the beginning.
338 * \param anchor This item will be the anchor item.
339 * \param current This item will be the current item.
340 * \param expectedSelection Expected selection after anchor and current are set.
341 * \param changeType Type of the change that is done then:
343 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
344 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
345 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
346 * data.at(1) provides the list containing the new indices
347 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
348 * - EndAnchoredSelection
349 * - SetSelected -> data.at(0) provides the index where the selection process starts,
350 * data.at(1) provides the number of indices to be selected,
351 * data.at(2) provides the selection mode.
352 * \sa KItemListSelectionManager::setSelected()
353 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
354 * \param finalSelection The expected final selection.
358 void KItemListSelectionManagerTest::testChangeSelection_data()
360 QTest::addColumn
<KItemSet
>("initialSelection");
361 QTest::addColumn
<int>("anchor");
362 QTest::addColumn
<int>("current");
363 QTest::addColumn
<KItemSet
>("expectedSelection");
364 QTest::addColumn
<ChangeType
>("changeType");
365 QTest::addColumn
<QList
<QVariant
> >("data");
366 QTest::addColumn
<KItemSet
>("finalSelection");
368 QTest::newRow("No change")
369 << (KItemSet() << 5 << 6)
371 << (KItemSet() << 2 << 3 << 5 << 6)
374 << (KItemSet() << 2 << 3 << 5 << 6);
376 QTest::newRow("Insert Items")
377 << (KItemSet() << 5 << 6)
379 << (KItemSet() << 2 << 3 << 5 << 6)
381 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))}
382 << (KItemSet() << 3 << 4 << 8 << 9);
384 QTest::newRow("Remove Items")
385 << (KItemSet() << 5 << 6)
387 << (KItemSet() << 2 << 3 << 5 << 6)
389 << QList
<QVariant
>{QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))}
390 << (KItemSet() << 1 << 2 << 3 << 4);
392 QTest::newRow("Empty Anchored Selection")
396 << EndAnchoredSelection
400 QTest::newRow("Toggle selection")
401 << (KItemSet() << 1 << 3 << 4)
403 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
405 << QList
<QVariant
>{0, 10, QVariant::fromValue(KItemListSelectionManager::Toggle
)}
406 << (KItemSet() << 0 << 2 << 5 << 9);
408 // Swap items 2, 3 and 4, 5
409 QTest::newRow("Move items")
410 << (KItemSet() << 0 << 1 << 2 << 3)
412 << (KItemSet() << 0 << 1 << 2 << 3)
414 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
415 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
416 << (KItemSet() << 0 << 1 << 4 << 5);
418 QTest::newRow("Move items with active anchored selection")
421 << (KItemSet() << 0 << 1 << 2 << 3)
423 << QList
<QVariant
>{QVariant::fromValue(KItemRange(2, 4)),
424 QVariant::fromValue(QList
<int>{4, 5, 2, 3})}
425 << (KItemSet() << 0 << 1 << 4 << 5);
428 QTest::newRow("Revert sort order")
429 << (KItemSet() << 0 << 1)
431 << (KItemSet() << 0 << 1 << 3 << 4)
433 << QList
<QVariant
>{QVariant::fromValue(KItemRange(0, 10)),
434 QVariant::fromValue(QList
<int>{9, 8, 7, 6, 5, 4, 3, 2, 1, 0})}
435 << (KItemSet() << 5 << 6 << 8 << 9);
438 void KItemListSelectionManagerTest::testChangeSelection()
440 QFETCH(KItemSet
, initialSelection
);
442 QFETCH(int, current
);
443 QFETCH(KItemSet
, expectedSelection
);
444 QFETCH(ChangeType
, changeType
);
445 QFETCH(QList
<QVariant
>, data
);
446 QFETCH(KItemSet
, finalSelection
);
448 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(KItemSet
,KItemSet
)));
450 // Initial selection should be empty
451 QVERIFY(!m_selectionManager
->hasSelection());
452 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
454 // Perform the initial selectiion
455 m_selectionManager
->setSelectedItems(initialSelection
);
457 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
459 // Perform an anchored selection.
460 // Note that current and anchor index are equal first because this is the case in typical uses of the
461 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
462 m_selectionManager
->setCurrentItem(anchor
);
463 m_selectionManager
->beginAnchoredSelection(anchor
);
464 m_selectionManager
->setCurrentItem(current
);
465 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
466 QCOMPARE(m_selectionManager
->currentItem(), current
);
468 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
470 // Change the model by inserting or removing items.
471 switch (changeType
) {
473 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
476 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
479 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
480 data
.at(1).value
<QList
<int>>());
482 case EndAnchoredSelection
:
483 m_selectionManager
->endAnchoredSelection();
484 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
487 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
488 data
.at(1).value
<int>(), // count
489 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
495 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
497 // Finally, clear the selection
498 m_selectionManager
->clearSelection();
500 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
503 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
505 QTest::addColumn
<int>("oldCurrentItemIndex");
506 QTest::addColumn
<int>("removeIndex");
507 QTest::addColumn
<int>("removeCount");
508 QTest::addColumn
<int>("newCurrentItemIndex");
510 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
511 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
512 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
513 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
514 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
517 void KItemListSelectionManagerTest::testDeleteCurrentItem()
519 QFETCH(int, oldCurrentItemIndex
);
520 QFETCH(int, removeIndex
);
521 QFETCH(int, removeCount
);
522 QFETCH(int, newCurrentItemIndex
);
524 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
526 const int newCount
= m_model
->count() - removeCount
;
527 m_model
->setCount(newCount
);
528 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
530 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
533 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
535 m_selectionManager
->setCurrentItem(4);
536 m_selectionManager
->beginAnchoredSelection(4);
538 // Reverse the items between 0 and 5.
539 m_selectionManager
->itemsMoved(KItemRange(0, 6), {5, 4, 3, 2, 1, 0});
541 QCOMPARE(m_selectionManager
->currentItem(), 1);
542 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
544 // Make 2 the current item -> 1 and 2 should be selected.
545 m_selectionManager
->setCurrentItem(2);
546 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
549 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
550 const KItemSet
& currentSelection
,
551 const KItemSet
& previousSelection
) const
553 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
554 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
555 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
556 if (currentSelection
.contains(index
)) {
557 QVERIFY(m_selectionManager
->isSelected(index
));
560 QVERIFY(!m_selectionManager
->isSelected(index
));
564 if (currentSelection
== previousSelection
) {
565 QCOMPARE(spy
.count(), 0);
568 QCOMPARE(spy
.count(), 1);
569 QList
<QVariant
> arguments
= spy
.takeFirst();
570 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
571 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
575 QTEST_GUILESS_MAIN(KItemListSelectionManagerTest
)
577 #include "kitemlistselectionmanagertest.moc"