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 <qtest_kde.h>
23 #include "kitemviews/kitemmodelbase.h"
24 #include "kitemviews/kitemlistselectionmanager.h"
26 class DummyModel
: public KItemModelBase
30 void setCount(int count
);
31 virtual int count() const;
32 virtual QHash
<QByteArray
, QVariant
> data(int index
) const;
38 DummyModel::DummyModel() :
44 void DummyModel::setCount(int count
)
49 int DummyModel::count() const
54 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
57 return QHash
<QByteArray
, QVariant
>();
61 class KItemListSelectionManagerTest
: public QObject
69 void testConstructor();
71 void testCurrentItemAnchorItem();
72 void testSetSelected_data();
73 void testSetSelected();
74 void testItemsInserted();
75 void testItemsRemoved();
76 void testAnchoredSelection();
77 void testChangeSelection_data();
78 void testChangeSelection();
79 void testDeleteCurrentItem_data();
80 void testDeleteCurrentItem();
81 void testAnchoredSelectionAfterMovingItems();
84 void verifySelectionChange(QSignalSpy
& spy
, const KItemSet
& currentSelection
, const KItemSet
& previousSelection
) const;
86 KItemListSelectionManager
* m_selectionManager
;
90 void KItemListSelectionManagerTest::init()
92 m_model
= new DummyModel();
93 m_selectionManager
= new KItemListSelectionManager();
94 m_selectionManager
->setModel(m_model
);
97 void KItemListSelectionManagerTest::cleanup()
99 delete m_selectionManager
;
100 m_selectionManager
= 0;
106 void KItemListSelectionManagerTest::testConstructor()
108 QVERIFY(!m_selectionManager
->hasSelection());
109 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
110 QCOMPARE(m_selectionManager
->currentItem(), 0);
111 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
114 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
116 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
118 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
119 m_selectionManager
->setCurrentItem(4);
120 QCOMPARE(m_selectionManager
->currentItem(), 4);
121 QCOMPARE(spyCurrent
.count(), 1);
122 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
123 spyCurrent
.takeFirst();
125 // Begin an anchored selection.
126 m_selectionManager
->beginAnchoredSelection(5);
127 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
128 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
130 // Items between current and anchor should be selected now
131 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
132 QVERIFY(m_selectionManager
->hasSelection());
134 // Change current item again and check the selection
135 m_selectionManager
->setCurrentItem(2);
136 QCOMPARE(m_selectionManager
->currentItem(), 2);
137 QCOMPARE(spyCurrent
.count(), 1);
138 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
139 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
140 spyCurrent
.takeFirst();
142 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
143 QVERIFY(m_selectionManager
->hasSelection());
145 // Inserting items should update current item and anchor item.
146 m_selectionManager
->itemsInserted(KItemRangeList() <<
151 QCOMPARE(m_selectionManager
->currentItem(), 5);
152 QCOMPARE(spyCurrent
.count(), 1);
153 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
154 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
155 spyCurrent
.takeFirst();
157 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
159 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8);
160 QVERIFY(m_selectionManager
->hasSelection());
162 // Removing items should update current item and anchor item.
163 m_selectionManager
->itemsRemoved(KItemRangeList() <<
168 QCOMPARE(m_selectionManager
->currentItem(), 2);
169 QCOMPARE(spyCurrent
.count(), 1);
170 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
171 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
172 spyCurrent
.takeFirst();
174 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
176 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 2 << 3 << 4 << 5);
177 QVERIFY(m_selectionManager
->hasSelection());
179 // Verify that clearSelection() also clears the anchored selection.
180 m_selectionManager
->clearSelection();
181 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet());
182 QVERIFY(!m_selectionManager
->hasSelection());
184 m_selectionManager
->endAnchoredSelection();
185 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
188 void KItemListSelectionManagerTest::testSetSelected_data()
190 QTest::addColumn
<int>("index");
191 QTest::addColumn
<int>("count");
192 QTest::addColumn
<int>("expectedSelectionCount");
194 QTest::newRow("Select all") << 0 << 100 << 100;
195 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
196 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
197 QTest::newRow("Too small index") << -1 << 100 << 0;
198 QTest::newRow("Too large index") << 100 << 100 << 0;
199 QTest::newRow("Too large count") << 0 << 100000 << 100;
200 QTest::newRow("Too small count") << 0 << 0 << 0;
203 void KItemListSelectionManagerTest::testSetSelected()
207 QFETCH(int, expectedSelectionCount
);
208 m_selectionManager
->setSelected(index
, count
);
209 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
212 void KItemListSelectionManagerTest::testItemsInserted()
214 // Select items 10 to 12
215 m_selectionManager
->setSelected(10, 3);
216 KItemSet selectedItems
= m_selectionManager
->selectedItems();
217 QCOMPARE(selectedItems
.count(), 3);
218 QVERIFY(selectedItems
.contains(10));
219 QVERIFY(selectedItems
.contains(11));
220 QVERIFY(selectedItems
.contains(12));
222 // Insert items 0 to 4 -> selection must be 15 to 17
223 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
224 selectedItems
= m_selectionManager
->selectedItems();
225 QCOMPARE(selectedItems
.count(), 3);
226 QVERIFY(selectedItems
.contains(15));
227 QVERIFY(selectedItems
.contains(16));
228 QVERIFY(selectedItems
.contains(17));
230 // Insert 3 items between the selections
231 m_selectionManager
->itemsInserted(KItemRangeList() <<
235 selectedItems
= m_selectionManager
->selectedItems();
236 QCOMPARE(selectedItems
.count(), 3);
237 QVERIFY(selectedItems
.contains(16));
238 QVERIFY(selectedItems
.contains(18));
239 QVERIFY(selectedItems
.contains(20));
242 void KItemListSelectionManagerTest::testItemsRemoved()
244 // Select items 10 to 15
245 m_selectionManager
->setSelected(10, 6);
246 KItemSet selectedItems
= m_selectionManager
->selectedItems();
247 QCOMPARE(selectedItems
.count(), 6);
248 for (int i
= 10; i
<= 15; ++i
) {
249 QVERIFY(selectedItems
.contains(i
));
252 // Remove items 0 to 4 -> selection must be 5 to 10
253 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
254 selectedItems
= m_selectionManager
->selectedItems();
255 QCOMPARE(selectedItems
.count(), 6);
256 for (int i
= 5; i
<= 10; ++i
) {
257 QVERIFY(selectedItems
.contains(i
));
260 // Remove the items 6 , 8 and 10
261 m_selectionManager
->itemsRemoved(KItemRangeList() <<
265 selectedItems
= m_selectionManager
->selectedItems();
266 QCOMPARE(selectedItems
.count(), 3);
267 QVERIFY(selectedItems
.contains(5));
268 QVERIFY(selectedItems
.contains(6));
269 QVERIFY(selectedItems
.contains(7));
272 void KItemListSelectionManagerTest::testAnchoredSelection()
274 m_selectionManager
->beginAnchoredSelection(5);
275 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
276 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
278 m_selectionManager
->setCurrentItem(6);
279 QCOMPARE(m_selectionManager
->currentItem(), 6);
280 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6);
282 m_selectionManager
->setCurrentItem(4);
283 QCOMPARE(m_selectionManager
->currentItem(), 4);
284 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 4 << 5);
286 m_selectionManager
->setCurrentItem(7);
287 QCOMPARE(m_selectionManager
->currentItem(), 7);
288 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
290 // Ending the anchored selection should not change the selected items.
291 m_selectionManager
->endAnchoredSelection();
292 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
293 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7);
295 // Start a new anchored selection that overlaps the previous one
296 m_selectionManager
->beginAnchoredSelection(9);
297 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
298 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
300 m_selectionManager
->setCurrentItem(6);
301 QCOMPARE(m_selectionManager
->currentItem(), 6);
302 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 8 << 9);
304 m_selectionManager
->setCurrentItem(10);
305 QCOMPARE(m_selectionManager
->currentItem(), 10);
306 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
308 m_selectionManager
->endAnchoredSelection();
309 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
310 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 5 << 6 << 7 << 9 << 10);
319 EndAnchoredSelection
,
324 Q_DECLARE_METATYPE(KItemSet
);
325 Q_DECLARE_METATYPE(ChangeType
);
326 Q_DECLARE_METATYPE(KItemRange
);
327 Q_DECLARE_METATYPE(KItemRangeList
);
328 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
);
329 Q_DECLARE_METATYPE(QList
<int>);
332 * The following function provides a generic way to test the selection functionality.
334 * The test is data-driven and takes the following arguments:
336 * \param initialSelection The selection at the beginning.
337 * \param anchor This item will be the anchor item.
338 * \param current This item will be the current item.
339 * \param expectedSelection Expected selection after anchor and current are set.
340 * \param changeType Type of the change that is done then:
342 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
343 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
344 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
345 * data.at(1) provides the list containing the new indices
346 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
347 * - EndAnchoredSelection
348 * - SetSelected -> data.at(0) provides the index where the selection process starts,
349 * data.at(1) provides the number of indices to be selected,
350 * data.at(2) provides the selection mode.
351 * \sa KItemListSelectionManager::setSelected()
352 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
353 * \param finalSelection The expected final selection.
357 void KItemListSelectionManagerTest::testChangeSelection_data()
359 QTest::addColumn
<KItemSet
>("initialSelection");
360 QTest::addColumn
<int>("anchor");
361 QTest::addColumn
<int>("current");
362 QTest::addColumn
<KItemSet
>("expectedSelection");
363 QTest::addColumn
<ChangeType
>("changeType");
364 QTest::addColumn
<QList
<QVariant
> >("data");
365 QTest::addColumn
<KItemSet
>("finalSelection");
367 QTest::newRow("No change")
368 << (KItemSet() << 5 << 6)
370 << (KItemSet() << 2 << 3 << 5 << 6)
373 << (KItemSet() << 2 << 3 << 5 << 6);
375 QTest::newRow("Insert Items")
376 << (KItemSet() << 5 << 6)
378 << (KItemSet() << 2 << 3 << 5 << 6)
380 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5)))
381 << (KItemSet() << 3 << 4 << 8 << 9);
383 QTest::newRow("Remove Items")
384 << (KItemSet() << 5 << 6)
386 << (KItemSet() << 2 << 3 << 5 << 6)
388 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5)))
389 << (KItemSet() << 1 << 2 << 3 << 4);
391 QTest::newRow("Empty Anchored Selection")
395 << EndAnchoredSelection
399 QTest::newRow("Toggle selection")
400 << (KItemSet() << 1 << 3 << 4)
402 << (KItemSet() << 1 << 3 << 4 << 6 << 7 << 8)
404 << (QList
<QVariant
>() << 0 << 10 << QVariant::fromValue(KItemListSelectionManager::Toggle
))
405 << (KItemSet() << 0 << 2 << 5 << 9);
407 // Swap items 2, 3 and 4, 5
408 QTest::newRow("Move items")
409 << (KItemSet() << 0 << 1 << 2 << 3)
411 << (KItemSet() << 0 << 1 << 2 << 3)
413 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(2, 4))
414 << QVariant::fromValue(QList
<int>() << 4 << 5 << 2 << 3))
415 << (KItemSet() << 0 << 1 << 4 << 5);
417 QTest::newRow("Move items with active anchored selection")
420 << (KItemSet() << 0 << 1 << 2 << 3)
422 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(2, 4))
423 << QVariant::fromValue(QList
<int>() << 4 << 5 << 2 << 3))
424 << (KItemSet() << 0 << 1 << 4 << 5);
427 QTest::newRow("Revert sort order")
428 << (KItemSet() << 0 << 1)
430 << (KItemSet() << 0 << 1 << 3 << 4)
432 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(0, 10))
433 << QVariant::fromValue(QList
<int>() << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1 << 0))
434 << (KItemSet() << 5 << 6 << 8 << 9);
437 void KItemListSelectionManagerTest::testChangeSelection()
439 QFETCH(KItemSet
, initialSelection
);
441 QFETCH(int, current
);
442 QFETCH(KItemSet
, expectedSelection
);
443 QFETCH(ChangeType
, changeType
);
444 QFETCH(QList
<QVariant
>, data
);
445 QFETCH(KItemSet
, finalSelection
);
447 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(KItemSet
,KItemSet
)));
449 // Initial selection should be empty
450 QVERIFY(!m_selectionManager
->hasSelection());
451 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
453 // Perform the initial selectiion
454 m_selectionManager
->setSelectedItems(initialSelection
);
456 verifySelectionChange(spySelectionChanged
, initialSelection
, KItemSet());
458 // Perform an anchored selection.
459 // Note that current and anchor index are equal first because this is the case in typical uses of the
460 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
461 m_selectionManager
->setCurrentItem(anchor
);
462 m_selectionManager
->beginAnchoredSelection(anchor
);
463 m_selectionManager
->setCurrentItem(current
);
464 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
465 QCOMPARE(m_selectionManager
->currentItem(), current
);
467 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
469 // Change the model by inserting or removing items.
470 switch (changeType
) {
472 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
475 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
478 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
479 data
.at(1).value
<QList
<int> >());
481 case EndAnchoredSelection
:
482 m_selectionManager
->endAnchoredSelection();
483 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
486 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
487 data
.at(1).value
<int>(), // count
488 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
494 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
496 // Finally, clear the selection
497 m_selectionManager
->clearSelection();
499 verifySelectionChange(spySelectionChanged
, KItemSet(), finalSelection
);
502 void KItemListSelectionManagerTest::testDeleteCurrentItem_data()
504 QTest::addColumn
<int>("oldCurrentItemIndex");
505 QTest::addColumn
<int>("removeIndex");
506 QTest::addColumn
<int>("removeCount");
507 QTest::addColumn
<int>("newCurrentItemIndex");
509 QTest::newRow("Remove before") << 50 << 0 << 10 << 40;
510 QTest::newRow("Remove after") << 50 << 51 << 10 << 50;
511 QTest::newRow("Remove exactly current item") << 50 << 50 << 1 << 50;
512 QTest::newRow("Remove around current item") << 50 << 45 << 10 << 45;
513 QTest::newRow("Remove all except one item") << 50 << 1 << 99 << 0;
516 void KItemListSelectionManagerTest::testDeleteCurrentItem()
518 QFETCH(int, oldCurrentItemIndex
);
519 QFETCH(int, removeIndex
);
520 QFETCH(int, removeCount
);
521 QFETCH(int, newCurrentItemIndex
);
523 m_selectionManager
->setCurrentItem(oldCurrentItemIndex
);
525 const int newCount
= m_model
->count() - removeCount
;
526 m_model
->setCount(newCount
);
527 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(removeIndex
, removeCount
));
529 QCOMPARE(m_selectionManager
->currentItem(), newCurrentItemIndex
);
532 void KItemListSelectionManagerTest::testAnchoredSelectionAfterMovingItems()
534 m_selectionManager
->setCurrentItem(4);
535 m_selectionManager
->beginAnchoredSelection(4);
537 // Reverse the items between 0 and 5.
538 m_selectionManager
->itemsMoved(KItemRange(0, 6), QList
<int>() << 5 << 4 << 3 << 2 << 1 << 0);
540 QCOMPARE(m_selectionManager
->currentItem(), 1);
541 QCOMPARE(m_selectionManager
->m_anchorItem
, 1);
543 // Make 2 the current item -> 1 and 2 should be selected.
544 m_selectionManager
->setCurrentItem(2);
545 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << 1 << 2);
548 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
549 const KItemSet
& currentSelection
,
550 const KItemSet
& previousSelection
) const
552 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
553 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
554 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
555 if (currentSelection
.contains(index
)) {
556 QVERIFY(m_selectionManager
->isSelected(index
));
559 QVERIFY(!m_selectionManager
->isSelected(index
));
563 if (currentSelection
== previousSelection
) {
564 QCOMPARE(spy
.count(), 0);
567 QCOMPARE(spy
.count(), 1);
568 QList
<QVariant
> arguments
= spy
.takeFirst();
569 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(0)), currentSelection
);
570 QCOMPARE(qvariant_cast
<KItemSet
>(arguments
.at(1)), previousSelection
);
574 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
576 #include "kitemlistselectionmanagertest.moc"