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 virtual int count() const;
31 virtual QHash
<QByteArray
, QVariant
> data(int index
) const;
34 DummyModel::DummyModel() :
39 int DummyModel::count() const
44 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
47 return QHash
<QByteArray
, QVariant
>();
52 class KItemListSelectionManagerTest
: public QObject
60 void testConstructor();
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();
72 void verifySelectionChange(QSignalSpy
& spy
, const QSet
<int>& currentSelection
, const QSet
<int>& previousSelection
) const;
74 KItemListSelectionManager
* m_selectionManager
;
77 void KItemListSelectionManagerTest::init()
79 m_selectionManager
= new KItemListSelectionManager();
80 m_selectionManager
->setModel(new DummyModel());
83 void KItemListSelectionManagerTest::cleanup()
85 delete m_selectionManager
->model();
86 delete m_selectionManager
;
87 m_selectionManager
= 0;
90 void KItemListSelectionManagerTest::testConstructor()
92 QVERIFY(!m_selectionManager
->hasSelection());
93 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
94 QCOMPARE(m_selectionManager
->currentItem(), 0);
95 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
98 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
100 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
102 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
103 m_selectionManager
->setCurrentItem(4);
104 QCOMPARE(m_selectionManager
->currentItem(), 4);
105 QCOMPARE(spyCurrent
.count(), 1);
106 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
107 spyCurrent
.takeFirst();
109 // Begin an anchored selection.
110 m_selectionManager
->beginAnchoredSelection(5);
111 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
112 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
114 // Items between current and anchor should be selected now
115 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
116 QVERIFY(m_selectionManager
->hasSelection());
118 // Change current item again and check the selection
119 m_selectionManager
->setCurrentItem(2);
120 QCOMPARE(m_selectionManager
->currentItem(), 2);
121 QCOMPARE(spyCurrent
.count(), 1);
122 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
123 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
124 spyCurrent
.takeFirst();
126 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
127 QVERIFY(m_selectionManager
->hasSelection());
129 // Inserting items should update current item and anchor item.
130 m_selectionManager
->itemsInserted(KItemRangeList() <<
135 QCOMPARE(m_selectionManager
->currentItem(), 5);
136 QCOMPARE(spyCurrent
.count(), 1);
137 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
138 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
139 spyCurrent
.takeFirst();
141 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
143 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8);
144 QVERIFY(m_selectionManager
->hasSelection());
146 // Removing items should update current item and anchor item.
147 m_selectionManager
->itemsRemoved(KItemRangeList() <<
152 QCOMPARE(m_selectionManager
->currentItem(), 2);
153 QCOMPARE(spyCurrent
.count(), 1);
154 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
155 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
156 spyCurrent
.takeFirst();
158 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
160 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
161 QVERIFY(m_selectionManager
->hasSelection());
163 // Verify that clearSelection() also clears the anchored selection.
164 m_selectionManager
->clearSelection();
165 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
166 QVERIFY(!m_selectionManager
->hasSelection());
168 m_selectionManager
->endAnchoredSelection();
169 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
172 void KItemListSelectionManagerTest::testSetSelected_data()
174 QTest::addColumn
<int>("index");
175 QTest::addColumn
<int>("count");
176 QTest::addColumn
<int>("expectedSelectionCount");
178 QTest::newRow("Select all") << 0 << 100 << 100;
179 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
180 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
181 QTest::newRow("Too small index") << -1 << 100 << 0;
182 QTest::newRow("Too large index") << 100 << 100 << 0;
183 QTest::newRow("Too large count") << 0 << 100000 << 100;
184 QTest::newRow("Too small count") << 0 << 0 << 0;
187 void KItemListSelectionManagerTest::testSetSelected()
191 QFETCH(int, expectedSelectionCount
);
192 m_selectionManager
->setSelected(index
, count
);
193 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
196 void KItemListSelectionManagerTest::testItemsInserted()
198 // Select items 10 to 12
199 m_selectionManager
->setSelected(10, 3);
200 QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
201 QCOMPARE(selectedItems
.count(), 3);
202 QVERIFY(selectedItems
.contains(10));
203 QVERIFY(selectedItems
.contains(11));
204 QVERIFY(selectedItems
.contains(12));
206 // Insert items 0 to 4 -> selection must be 15 to 17
207 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
208 selectedItems
= m_selectionManager
->selectedItems();
209 QCOMPARE(selectedItems
.count(), 3);
210 QVERIFY(selectedItems
.contains(15));
211 QVERIFY(selectedItems
.contains(16));
212 QVERIFY(selectedItems
.contains(17));
214 // Insert 3 items between the selections
215 m_selectionManager
->itemsInserted(KItemRangeList() <<
219 selectedItems
= m_selectionManager
->selectedItems();
220 QCOMPARE(selectedItems
.count(), 3);
221 QVERIFY(selectedItems
.contains(16));
222 QVERIFY(selectedItems
.contains(18));
223 QVERIFY(selectedItems
.contains(20));
226 void KItemListSelectionManagerTest::testItemsRemoved()
228 // Select items 10 to 15
229 m_selectionManager
->setSelected(10, 6);
230 QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
231 QCOMPARE(selectedItems
.count(), 6);
232 for (int i
= 10; i
<= 15; ++i
) {
233 QVERIFY(selectedItems
.contains(i
));
236 // Remove items 0 to 4 -> selection must be 5 to 10
237 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
238 selectedItems
= m_selectionManager
->selectedItems();
239 QCOMPARE(selectedItems
.count(), 6);
240 for (int i
= 5; i
<= 10; ++i
) {
241 QVERIFY(selectedItems
.contains(i
));
244 // Remove the items 6 , 8 and 10
245 m_selectionManager
->itemsRemoved(KItemRangeList() <<
249 selectedItems
= m_selectionManager
->selectedItems();
250 QCOMPARE(selectedItems
.count(), 3);
251 QVERIFY(selectedItems
.contains(5));
252 QVERIFY(selectedItems
.contains(6));
253 QVERIFY(selectedItems
.contains(7));
256 void KItemListSelectionManagerTest::testAnchoredSelection()
258 m_selectionManager
->beginAnchoredSelection(5);
259 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
260 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
262 m_selectionManager
->setCurrentItem(6);
263 QCOMPARE(m_selectionManager
->currentItem(), 6);
264 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6);
266 m_selectionManager
->setCurrentItem(4);
267 QCOMPARE(m_selectionManager
->currentItem(), 4);
268 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
270 m_selectionManager
->setCurrentItem(7);
271 QCOMPARE(m_selectionManager
->currentItem(), 7);
272 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7);
274 // Ending the anchored selection should not change the selected items.
275 m_selectionManager
->endAnchoredSelection();
276 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
277 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7);
279 // Start a new anchored selection that overlaps the previous one
280 m_selectionManager
->beginAnchoredSelection(9);
281 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
282 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
284 m_selectionManager
->setCurrentItem(6);
285 QCOMPARE(m_selectionManager
->currentItem(), 6);
286 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8 << 9);
288 m_selectionManager
->setCurrentItem(10);
289 QCOMPARE(m_selectionManager
->currentItem(), 10);
290 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
292 m_selectionManager
->endAnchoredSelection();
293 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
294 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
303 EndAnchoredSelection
,
308 Q_DECLARE_METATYPE(QSet
<int>);
309 Q_DECLARE_METATYPE(ChangeType
);
310 Q_DECLARE_METATYPE(KItemRange
);
311 Q_DECLARE_METATYPE(KItemRangeList
);
312 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
);
313 Q_DECLARE_METATYPE(QList
<int>);
316 * The following function provides a generic way to test the selection functionality.
318 * The test is data-driven and takes the following arguments:
320 * \param initialSelection The selection at the beginning.
321 * \param anchor This item will be the anchor item.
322 * \param current This item will be the current item.
323 * \param expectedSelection Expected selection after anchor and current are set.
324 * \param changeType Type of the change that is done then:
326 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
327 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
328 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
329 * data.at(1) provides the list containing the new indices
330 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
331 * - EndAnchoredSelection
332 * - SetSelected -> data.at(0) provides the index where the selection process starts,
333 * data.at(1) provides the number of indices to be selected,
334 * data.at(2) provides the selection mode.
335 * \sa KItemListSelectionManager::setSelected()
336 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
337 * \param finalSelection The expected final selection.
341 void KItemListSelectionManagerTest::testChangeSelection_data()
343 QTest::addColumn
<QSet
<int> >("initialSelection");
344 QTest::addColumn
<int>("anchor");
345 QTest::addColumn
<int>("current");
346 QTest::addColumn
<QSet
<int> >("expectedSelection");
347 QTest::addColumn
<ChangeType
>("changeType");
348 QTest::addColumn
<QList
<QVariant
> >("data");
349 QTest::addColumn
<QSet
<int> >("finalSelection");
351 QTest::newRow("No change")
352 << (QSet
<int>() << 5 << 6)
354 << (QSet
<int>() << 2 << 3 << 5 << 6)
357 << (QSet
<int>() << 2 << 3 << 5 << 6);
359 QTest::newRow("Insert Items")
360 << (QSet
<int>() << 5 << 6)
362 << (QSet
<int>() << 2 << 3 << 5 << 6)
364 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5)))
365 << (QSet
<int>() << 3 << 4 << 8 << 9);
367 QTest::newRow("Remove Items")
368 << (QSet
<int>() << 5 << 6)
370 << (QSet
<int>() << 2 << 3 << 5 << 6)
372 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5)))
373 << (QSet
<int>() << 1 << 2 << 3 << 4);
375 QTest::newRow("Empty Anchored Selection")
379 << EndAnchoredSelection
383 QTest::newRow("Toggle selection")
384 << (QSet
<int>() << 1 << 3 << 4)
386 << (QSet
<int>() << 1 << 3 << 4 << 6 << 7 << 8)
388 << (QList
<QVariant
>() << 0 << 10 << QVariant::fromValue(KItemListSelectionManager::Toggle
))
389 << (QSet
<int>() << 0 << 2 << 5 << 9);
391 // Swap items 2, 3 and 4, 5
392 QTest::newRow("Move items")
393 << (QSet
<int>() << 0 << 1 << 2 << 3)
395 << (QSet
<int>() << 0 << 1 << 2 << 3)
397 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(2, 4))
398 << QVariant::fromValue(QList
<int>() << 4 << 5 << 2 << 3))
399 << (QSet
<int>() << 0 << 1 << 4 << 5);
402 QTest::newRow("Revert sort order")
403 << (QSet
<int>() << 0 << 1)
405 << (QSet
<int>() << 0 << 1 << 3 << 4)
407 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(0, 10))
408 << QVariant::fromValue(QList
<int>() << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1 << 0))
409 << (QSet
<int>() << 5 << 6 << 8 << 9);
412 void KItemListSelectionManagerTest::testChangeSelection()
414 QFETCH(QSet
<int>, initialSelection
);
416 QFETCH(int, current
);
417 QFETCH(QSet
<int>, expectedSelection
);
418 QFETCH(ChangeType
, changeType
);
419 QFETCH(QList
<QVariant
>, data
);
420 QFETCH(QSet
<int>, finalSelection
);
422 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)));
424 // Initial selection should be empty
425 QVERIFY(!m_selectionManager
->hasSelection());
426 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
428 // Perform the initial selectiion
429 m_selectionManager
->setSelectedItems(initialSelection
);
431 verifySelectionChange(spySelectionChanged
, initialSelection
, QSet
<int>());
433 // Perform an anchored selection.
434 // Note that current and anchor index are equal first because this is the case in typical uses of the
435 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
436 m_selectionManager
->setCurrentItem(anchor
);
437 m_selectionManager
->beginAnchoredSelection(anchor
);
438 m_selectionManager
->setCurrentItem(current
);
439 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
440 QCOMPARE(m_selectionManager
->currentItem(), current
);
442 verifySelectionChange(spySelectionChanged
, expectedSelection
, initialSelection
);
444 // Change the model by inserting or removing items.
445 switch (changeType
) {
447 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
450 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
453 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
454 data
.at(1).value
<QList
<int> >());
456 case EndAnchoredSelection
:
457 m_selectionManager
->endAnchoredSelection();
458 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
461 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
462 data
.at(1).value
<int>(), // count
463 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
469 verifySelectionChange(spySelectionChanged
, finalSelection
, expectedSelection
);
471 // Finally, clear the selection
472 m_selectionManager
->clearSelection();
474 verifySelectionChange(spySelectionChanged
, QSet
<int>(), finalSelection
);
477 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy
& spy
,
478 const QSet
<int>& currentSelection
,
479 const QSet
<int>& previousSelection
) const
481 QCOMPARE(m_selectionManager
->selectedItems(), currentSelection
);
482 QCOMPARE(m_selectionManager
->hasSelection(), !currentSelection
.isEmpty());
483 for (int index
= 0; index
< m_selectionManager
->model()->count(); ++index
) {
484 if (currentSelection
.contains(index
)) {
485 QVERIFY(m_selectionManager
->isSelected(index
));
488 QVERIFY(!m_selectionManager
->isSelected(index
));
492 if (currentSelection
== previousSelection
) {
493 QCOMPARE(spy
.count(), 0);
496 QCOMPARE(spy
.count(), 1);
497 QList
<QVariant
> arguments
= spy
.takeFirst();
498 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), currentSelection
);
499 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), previousSelection
);
503 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
505 #include "kitemlistselectionmanagertest.moc"