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
->anchorItem(), -1);
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
98 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
99 QSignalSpy
spyAnchor(m_selectionManager
, SIGNAL(anchorChanged(int,int)));
101 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
102 m_selectionManager
->setCurrentItem(4);
103 QCOMPARE(m_selectionManager
->currentItem(), 4);
104 QCOMPARE(spyCurrent
.count(), 1);
105 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
106 spyCurrent
.takeFirst();
108 // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
109 m_selectionManager
->beginAnchoredSelection(3);
110 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
111 QCOMPARE(m_selectionManager
->anchorItem(), 3);
112 QCOMPARE(spyAnchor
.count(), 1);
113 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 3);
114 spyAnchor
.takeFirst();
116 m_selectionManager
->setAnchorItem(5);
117 QCOMPARE(m_selectionManager
->anchorItem(), 5);
118 QCOMPARE(spyAnchor
.count(), 1);
119 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 5);
120 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 3);
121 spyAnchor
.takeFirst();
123 // Items between current and anchor should be selected now
124 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
125 QVERIFY(m_selectionManager
->hasSelection());
127 // Change current item again and check the selection
128 m_selectionManager
->setCurrentItem(2);
129 QCOMPARE(m_selectionManager
->currentItem(), 2);
130 QCOMPARE(spyCurrent
.count(), 1);
131 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
132 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
133 spyCurrent
.takeFirst();
135 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
136 QVERIFY(m_selectionManager
->hasSelection());
138 // Inserting items should update current item and anchor item.
139 m_selectionManager
->itemsInserted(KItemRangeList() <<
144 QCOMPARE(m_selectionManager
->currentItem(), 5);
145 QCOMPARE(spyCurrent
.count(), 1);
146 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
147 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
148 spyCurrent
.takeFirst();
150 QCOMPARE(m_selectionManager
->anchorItem(), 8);
151 QCOMPARE(spyAnchor
.count(), 1);
152 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 8);
153 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 5);
154 spyAnchor
.takeFirst();
156 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8);
157 QVERIFY(m_selectionManager
->hasSelection());
159 // Removing items should update current item and anchor item.
160 m_selectionManager
->itemsRemoved(KItemRangeList() <<
165 QCOMPARE(m_selectionManager
->currentItem(), 2);
166 QCOMPARE(spyCurrent
.count(), 1);
167 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
168 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
169 spyCurrent
.takeFirst();
171 QCOMPARE(m_selectionManager
->anchorItem(), 5);
172 QCOMPARE(spyAnchor
.count(), 1);
173 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 5);
174 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 8);
175 spyAnchor
.takeFirst();
177 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 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(), QSet
<int>());
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 QSet
<int> 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 QSet
<int> 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
->anchorItem(), 5);
279 m_selectionManager
->setCurrentItem(6);
280 QCOMPARE(m_selectionManager
->currentItem(), 6);
281 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6);
283 m_selectionManager
->setCurrentItem(4);
284 QCOMPARE(m_selectionManager
->currentItem(), 4);
285 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
287 m_selectionManager
->setCurrentItem(7);
288 QCOMPARE(m_selectionManager
->currentItem(), 7);
289 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 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(), QSet
<int>() << 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
->anchorItem(), 9);
301 m_selectionManager
->setCurrentItem(6);
302 QCOMPARE(m_selectionManager
->currentItem(), 6);
303 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8 << 9);
305 m_selectionManager
->setCurrentItem(10);
306 QCOMPARE(m_selectionManager
->currentItem(), 10);
307 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
309 m_selectionManager
->endAnchoredSelection();
310 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
311 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
319 EndAnchoredSelection
,
324 Q_DECLARE_METATYPE(QSet
<int>);
325 Q_DECLARE_METATYPE(ChangeType
);
326 Q_DECLARE_METATYPE(KItemRangeList
);
328 void KItemListSelectionManagerTest::testChangeSelection_data()
330 QTest::addColumn
<QSet
<int> >("initialSelection");
331 QTest::addColumn
<int>("anchor");
332 QTest::addColumn
<int>("current");
333 QTest::addColumn
<QSet
<int> >("expectedSelection");
334 QTest::addColumn
<ChangeType
>("changeType");
335 QTest::addColumn
<KItemRangeList
>("changedItems");
336 QTest::addColumn
<QSet
<int> >("finalSelection");
338 QTest::newRow("No change")
339 << (QSet
<int>() << 5 << 6)
341 << (QSet
<int>() << 2 << 3 << 5 << 6)
342 << NoChange
<< KItemRangeList()
343 << (QSet
<int>() << 2 << 3 << 5 << 6);
345 QTest::newRow("Insert Items")
346 << (QSet
<int>() << 5 << 6)
348 << (QSet
<int>() << 2 << 3 << 5 << 6)
349 << InsertItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))
350 << (QSet
<int>() << 3 << 4 << 8 << 9);
352 QTest::newRow("Remove Items")
353 << (QSet
<int>() << 5 << 6)
355 << (QSet
<int>() << 2 << 3 << 5 << 6)
356 << RemoveItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))
357 << (QSet
<int>() << 1 << 2 << 3 << 4);
359 QTest::newRow("Empty Anchored Selection")
363 << EndAnchoredSelection
<< KItemRangeList()
366 QTest::newRow("Toggle selection")
367 << (QSet
<int>() << 1 << 3 << 4)
369 << (QSet
<int>() << 1 << 3 << 4 << 6 << 7 << 8)
370 << ToggleSelected
<< (KItemRangeList() << KItemRange(0, 10))
371 << (QSet
<int>() << 0 << 2 << 5 << 9);
374 void KItemListSelectionManagerTest::testChangeSelection()
376 QFETCH(QSet
<int>, initialSelection
);
378 QFETCH(int, current
);
379 QFETCH(QSet
<int> , expectedSelection
);
380 QFETCH(ChangeType
, changeType
);
381 QFETCH(KItemRangeList
, changedItems
);
382 QFETCH(QSet
<int> , finalSelection
);
384 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)));
386 // Initial selection should be empty
387 QVERIFY(!m_selectionManager
->hasSelection());
388 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
390 // Perform the initial selectiion
391 m_selectionManager
->setSelectedItems(initialSelection
);
392 QCOMPARE(m_selectionManager
->selectedItems(), initialSelection
);
393 if (initialSelection
.isEmpty()) {
394 QVERIFY(!m_selectionManager
->hasSelection());
395 QCOMPARE(spySelectionChanged
.count(), 0);
398 QVERIFY(m_selectionManager
->hasSelection());
399 QCOMPARE(spySelectionChanged
.count(), 1);
400 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
401 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), initialSelection
);
402 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), QSet
<int>());
405 // Perform an anchored selection.
406 // Note that current and anchor index are equal first because this is the case in typical uses of the
407 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
408 m_selectionManager
->setCurrentItem(anchor
);
409 m_selectionManager
->beginAnchoredSelection(anchor
);
410 m_selectionManager
->setCurrentItem(current
);
411 QCOMPARE(m_selectionManager
->selectedItems(), expectedSelection
);
412 QCOMPARE(m_selectionManager
->hasSelection(), !expectedSelection
.isEmpty());
413 if (expectedSelection
== initialSelection
) {
414 QCOMPARE(spySelectionChanged
.count(), 0);
417 QCOMPARE(spySelectionChanged
.count(), 1);
418 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
419 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), expectedSelection
);
420 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), initialSelection
);
423 // Change the model by inserting or removing items.
424 switch (changeType
) {
426 m_selectionManager
->itemsInserted(changedItems
);
429 m_selectionManager
->itemsRemoved(changedItems
);
431 case EndAnchoredSelection
:
432 m_selectionManager
->endAnchoredSelection();
433 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
436 foreach(const KItemRange
& range
, changedItems
) {
437 m_selectionManager
->setSelected(range
.index
, range
.count
, KItemListSelectionManager::Toggle
);
444 QCOMPARE(m_selectionManager
->selectedItems(), finalSelection
);
445 QCOMPARE(m_selectionManager
->hasSelection(), !finalSelection
.isEmpty());
446 if (finalSelection
== expectedSelection
) {
447 QCOMPARE(spySelectionChanged
.count(), 0);
450 QCOMPARE(spySelectionChanged
.count(), 1);
451 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
452 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), finalSelection
);
453 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), expectedSelection
);
456 // Finally, clear the selection
457 m_selectionManager
->clearSelection();
458 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
459 QVERIFY(!m_selectionManager
->hasSelection());
460 if (finalSelection
.isEmpty()) {
461 // Selection has been empty already
462 QCOMPARE(spySelectionChanged
.count(), 0);
465 QCOMPARE(spySelectionChanged
.count(), 1);
466 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
467 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), QSet
<int>());
468 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), finalSelection
);
472 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
474 #include "kitemlistselectionmanagertest.moc"