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 KItemListSelectionManager
* m_selectionManager
;
75 void KItemListSelectionManagerTest::init()
77 m_selectionManager
= new KItemListSelectionManager();
78 m_selectionManager
->setModel(new DummyModel());
81 void KItemListSelectionManagerTest::cleanup()
83 delete m_selectionManager
->model();
84 delete m_selectionManager
;
85 m_selectionManager
= 0;
88 void KItemListSelectionManagerTest::testConstructor()
90 QVERIFY(!m_selectionManager
->hasSelection());
91 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
92 QCOMPARE(m_selectionManager
->currentItem(), 0);
93 QCOMPARE(m_selectionManager
->m_anchorItem
, -1);
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
98 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
100 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
101 m_selectionManager
->setCurrentItem(4);
102 QCOMPARE(m_selectionManager
->currentItem(), 4);
103 QCOMPARE(spyCurrent
.count(), 1);
104 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
105 spyCurrent
.takeFirst();
107 // Begin an anchored selection.
108 m_selectionManager
->beginAnchoredSelection(5);
109 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
110 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
112 // Items between current and anchor should be selected now
113 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
114 QVERIFY(m_selectionManager
->hasSelection());
116 // Change current item again and check the selection
117 m_selectionManager
->setCurrentItem(2);
118 QCOMPARE(m_selectionManager
->currentItem(), 2);
119 QCOMPARE(spyCurrent
.count(), 1);
120 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
121 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
122 spyCurrent
.takeFirst();
124 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
125 QVERIFY(m_selectionManager
->hasSelection());
127 // Inserting items should update current item and anchor item.
128 m_selectionManager
->itemsInserted(KItemRangeList() <<
133 QCOMPARE(m_selectionManager
->currentItem(), 5);
134 QCOMPARE(spyCurrent
.count(), 1);
135 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
136 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
137 spyCurrent
.takeFirst();
139 QCOMPARE(m_selectionManager
->m_anchorItem
, 8);
141 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8);
142 QVERIFY(m_selectionManager
->hasSelection());
144 // Removing items should update current item and anchor item.
145 m_selectionManager
->itemsRemoved(KItemRangeList() <<
150 QCOMPARE(m_selectionManager
->currentItem(), 2);
151 QCOMPARE(spyCurrent
.count(), 1);
152 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
153 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
154 spyCurrent
.takeFirst();
156 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
158 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
159 QVERIFY(m_selectionManager
->hasSelection());
161 // Verify that clearSelection() also clears the anchored selection.
162 m_selectionManager
->clearSelection();
163 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
164 QVERIFY(!m_selectionManager
->hasSelection());
166 m_selectionManager
->endAnchoredSelection();
167 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
170 void KItemListSelectionManagerTest::testSetSelected_data()
172 QTest::addColumn
<int>("index");
173 QTest::addColumn
<int>("count");
174 QTest::addColumn
<int>("expectedSelectionCount");
176 QTest::newRow("Select all") << 0 << 100 << 100;
177 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
178 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
179 QTest::newRow("Too small index") << -1 << 100 << 0;
180 QTest::newRow("Too large index") << 100 << 100 << 0;
181 QTest::newRow("Too large count") << 0 << 100000 << 100;
182 QTest::newRow("Too small count") << 0 << 0 << 0;
185 void KItemListSelectionManagerTest::testSetSelected()
189 QFETCH(int, expectedSelectionCount
);
190 m_selectionManager
->setSelected(index
, count
);
191 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
194 void KItemListSelectionManagerTest::testItemsInserted()
196 // Select items 10 to 12
197 m_selectionManager
->setSelected(10, 3);
198 QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
199 QCOMPARE(selectedItems
.count(), 3);
200 QVERIFY(selectedItems
.contains(10));
201 QVERIFY(selectedItems
.contains(11));
202 QVERIFY(selectedItems
.contains(12));
204 // Insert items 0 to 4 -> selection must be 15 to 17
205 m_selectionManager
->itemsInserted(KItemRangeList() << KItemRange(0, 5));
206 selectedItems
= m_selectionManager
->selectedItems();
207 QCOMPARE(selectedItems
.count(), 3);
208 QVERIFY(selectedItems
.contains(15));
209 QVERIFY(selectedItems
.contains(16));
210 QVERIFY(selectedItems
.contains(17));
212 // Insert 3 items between the selections
213 m_selectionManager
->itemsInserted(KItemRangeList() <<
217 selectedItems
= m_selectionManager
->selectedItems();
218 QCOMPARE(selectedItems
.count(), 3);
219 QVERIFY(selectedItems
.contains(16));
220 QVERIFY(selectedItems
.contains(18));
221 QVERIFY(selectedItems
.contains(20));
224 void KItemListSelectionManagerTest::testItemsRemoved()
226 // Select items 10 to 15
227 m_selectionManager
->setSelected(10, 6);
228 QSet
<int> selectedItems
= m_selectionManager
->selectedItems();
229 QCOMPARE(selectedItems
.count(), 6);
230 for (int i
= 10; i
<= 15; ++i
) {
231 QVERIFY(selectedItems
.contains(i
));
234 // Remove items 0 to 4 -> selection must be 5 to 10
235 m_selectionManager
->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
236 selectedItems
= m_selectionManager
->selectedItems();
237 QCOMPARE(selectedItems
.count(), 6);
238 for (int i
= 5; i
<= 10; ++i
) {
239 QVERIFY(selectedItems
.contains(i
));
242 // Remove the items 6 , 8 and 10
243 m_selectionManager
->itemsRemoved(KItemRangeList() <<
247 selectedItems
= m_selectionManager
->selectedItems();
248 QCOMPARE(selectedItems
.count(), 3);
249 QVERIFY(selectedItems
.contains(5));
250 QVERIFY(selectedItems
.contains(6));
251 QVERIFY(selectedItems
.contains(7));
254 void KItemListSelectionManagerTest::testAnchoredSelection()
256 m_selectionManager
->beginAnchoredSelection(5);
257 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
258 QCOMPARE(m_selectionManager
->m_anchorItem
, 5);
260 m_selectionManager
->setCurrentItem(6);
261 QCOMPARE(m_selectionManager
->currentItem(), 6);
262 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6);
264 m_selectionManager
->setCurrentItem(4);
265 QCOMPARE(m_selectionManager
->currentItem(), 4);
266 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
268 m_selectionManager
->setCurrentItem(7);
269 QCOMPARE(m_selectionManager
->currentItem(), 7);
270 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7);
272 // Ending the anchored selection should not change the selected items.
273 m_selectionManager
->endAnchoredSelection();
274 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
275 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7);
277 // Start a new anchored selection that overlaps the previous one
278 m_selectionManager
->beginAnchoredSelection(9);
279 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
280 QCOMPARE(m_selectionManager
->m_anchorItem
, 9);
282 m_selectionManager
->setCurrentItem(6);
283 QCOMPARE(m_selectionManager
->currentItem(), 6);
284 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8 << 9);
286 m_selectionManager
->setCurrentItem(10);
287 QCOMPARE(m_selectionManager
->currentItem(), 10);
288 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
290 m_selectionManager
->endAnchoredSelection();
291 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
292 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
301 EndAnchoredSelection
,
306 Q_DECLARE_METATYPE(QSet
<int>);
307 Q_DECLARE_METATYPE(ChangeType
);
308 Q_DECLARE_METATYPE(KItemRange
);
309 Q_DECLARE_METATYPE(KItemRangeList
);
310 Q_DECLARE_METATYPE(KItemListSelectionManager::SelectionMode
);
311 Q_DECLARE_METATYPE(QList
<int>);
314 * The following function provides a generic way to test the selection functionality.
316 * The test is data-driven and takes the following arguments:
318 * \param initialSelection The selection at the beginning.
319 * \param anchor This item will be the anchor item.
320 * \param current This item will be the current item.
321 * \param expectedSelection Expected selection after anchor and current are set.
322 * \param changeType Type of the change that is done then:
324 * - InsertItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsInserted()
325 * - RemoveItems -> data.at(0) provides the KItemRangeList. \sa KItemListSelectionManager::itemsRemoved()
326 * - MoveItems -> data.at(0) provides the KItemRange containing the original indices,
327 * data.at(1) provides the list containing the new indices
328 * \sa KItemListSelectionManager::itemsMoved(), KItemModelBase::itemsMoved()
329 * - EndAnchoredSelection
330 * - SetSelected -> data.at(0) provides the index where the selection process starts,
331 * data.at(1) provides the number of indices to be selected,
332 * data.at(2) provides the selection mode.
333 * \sa KItemListSelectionManager::setSelected()
334 * \param data A list of QVariants which will be cast to the arguments needed for the chosen ChangeType (see above).
335 * \param finalSelection The expected final selection.
339 void KItemListSelectionManagerTest::testChangeSelection_data()
341 QTest::addColumn
<QSet
<int> >("initialSelection");
342 QTest::addColumn
<int>("anchor");
343 QTest::addColumn
<int>("current");
344 QTest::addColumn
<QSet
<int> >("expectedSelection");
345 QTest::addColumn
<ChangeType
>("changeType");
346 QTest::addColumn
<QList
<QVariant
> >("data");
347 QTest::addColumn
<QSet
<int> >("finalSelection");
349 QTest::newRow("No change")
350 << (QSet
<int>() << 5 << 6)
352 << (QSet
<int>() << 2 << 3 << 5 << 6)
355 << (QSet
<int>() << 2 << 3 << 5 << 6);
357 QTest::newRow("Insert Items")
358 << (QSet
<int>() << 5 << 6)
360 << (QSet
<int>() << 2 << 3 << 5 << 6)
362 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5)))
363 << (QSet
<int>() << 3 << 4 << 8 << 9);
365 QTest::newRow("Remove Items")
366 << (QSet
<int>() << 5 << 6)
368 << (QSet
<int>() << 2 << 3 << 5 << 6)
370 << (QList
<QVariant
>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5)))
371 << (QSet
<int>() << 1 << 2 << 3 << 4);
373 QTest::newRow("Empty Anchored Selection")
377 << EndAnchoredSelection
381 QTest::newRow("Toggle selection")
382 << (QSet
<int>() << 1 << 3 << 4)
384 << (QSet
<int>() << 1 << 3 << 4 << 6 << 7 << 8)
386 << (QList
<QVariant
>() << 0 << 10 << QVariant::fromValue(KItemListSelectionManager::Toggle
))
387 << (QSet
<int>() << 0 << 2 << 5 << 9);
389 // Swap items 2, 3 and 4, 5
390 QTest::newRow("Move items")
391 << (QSet
<int>() << 0 << 1 << 2 << 3)
393 << (QSet
<int>() << 0 << 1 << 2 << 3)
395 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(2, 4))
396 << QVariant::fromValue(QList
<int>() << 4 << 5 << 2 << 3))
397 << (QSet
<int>() << 0 << 1 << 4 << 5);
400 QTest::newRow("Revert sort order")
401 << (QSet
<int>() << 0 << 1)
403 << (QSet
<int>() << 0 << 1 << 3 << 4)
405 << (QList
<QVariant
>() << QVariant::fromValue(KItemRange(0, 10))
406 << QVariant::fromValue(QList
<int>() << 9 << 8 << 7 << 6 << 5 << 4 << 3 << 2 << 1 << 0))
407 << (QSet
<int>() << 5 << 6 << 8 << 9);
410 void KItemListSelectionManagerTest::testChangeSelection()
412 QFETCH(QSet
<int>, initialSelection
);
414 QFETCH(int, current
);
415 QFETCH(QSet
<int>, expectedSelection
);
416 QFETCH(ChangeType
, changeType
);
417 QFETCH(QList
<QVariant
>, data
);
418 QFETCH(QSet
<int>, finalSelection
);
420 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)));
422 // Initial selection should be empty
423 QVERIFY(!m_selectionManager
->hasSelection());
424 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
426 // Perform the initial selectiion
427 m_selectionManager
->setSelectedItems(initialSelection
);
428 QCOMPARE(m_selectionManager
->selectedItems(), initialSelection
);
429 if (initialSelection
.isEmpty()) {
430 QVERIFY(!m_selectionManager
->hasSelection());
431 QCOMPARE(spySelectionChanged
.count(), 0);
434 QVERIFY(m_selectionManager
->hasSelection());
435 QCOMPARE(spySelectionChanged
.count(), 1);
436 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
437 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), initialSelection
);
438 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), QSet
<int>());
441 // Perform an anchored selection.
442 // Note that current and anchor index are equal first because this is the case in typical uses of the
443 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
444 m_selectionManager
->setCurrentItem(anchor
);
445 m_selectionManager
->beginAnchoredSelection(anchor
);
446 m_selectionManager
->setCurrentItem(current
);
447 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
448 QCOMPARE(m_selectionManager
->currentItem(), current
);
449 QCOMPARE(m_selectionManager
->selectedItems(), expectedSelection
);
450 QCOMPARE(m_selectionManager
->hasSelection(), !expectedSelection
.isEmpty());
451 if (expectedSelection
== initialSelection
) {
452 QCOMPARE(spySelectionChanged
.count(), 0);
455 QCOMPARE(spySelectionChanged
.count(), 1);
456 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
457 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), expectedSelection
);
458 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), initialSelection
);
461 // Change the model by inserting or removing items.
462 switch (changeType
) {
464 m_selectionManager
->itemsInserted(data
.at(0).value
<KItemRangeList
>());
467 m_selectionManager
->itemsRemoved(data
.at(0).value
<KItemRangeList
>());
470 m_selectionManager
->itemsMoved(data
.at(0).value
<KItemRange
>(),
471 data
.at(1).value
<QList
<int> >());
473 case EndAnchoredSelection
:
474 m_selectionManager
->endAnchoredSelection();
475 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
478 m_selectionManager
->setSelected(data
.at(0).value
<int>(), // index
479 data
.at(1).value
<int>(), // count
480 data
.at(2).value
<KItemListSelectionManager::SelectionMode
>());
486 QCOMPARE(m_selectionManager
->selectedItems(), finalSelection
);
487 QCOMPARE(m_selectionManager
->hasSelection(), !finalSelection
.isEmpty());
488 if (finalSelection
== expectedSelection
) {
489 QCOMPARE(spySelectionChanged
.count(), 0);
492 QCOMPARE(spySelectionChanged
.count(), 1);
493 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
494 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), finalSelection
);
495 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), expectedSelection
);
498 // Finally, clear the selection
499 m_selectionManager
->clearSelection();
500 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
501 QVERIFY(!m_selectionManager
->hasSelection());
502 if (finalSelection
.isEmpty()) {
503 // Selection has been empty already
504 QCOMPARE(spySelectionChanged
.count(), 0);
507 QCOMPARE(spySelectionChanged
.count(), 1);
508 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
509 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), QSet
<int>());
510 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), finalSelection
);
514 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
516 #include "kitemlistselectionmanagertest.moc"