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 m_selectionManager
->setAnchoredSelectionActive(true);
102 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
104 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
105 m_selectionManager
->setCurrentItem(4);
106 QCOMPARE(m_selectionManager
->currentItem(), 4);
107 QCOMPARE(spyCurrent
.count(), 1);
108 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 4);
109 spyCurrent
.takeFirst();
111 // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
112 m_selectionManager
->setAnchorItem(3);
113 QCOMPARE(m_selectionManager
->anchorItem(), 3);
114 QCOMPARE(spyAnchor
.count(), 1);
115 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 3);
116 spyAnchor
.takeFirst();
118 m_selectionManager
->setAnchorItem(5);
119 QCOMPARE(m_selectionManager
->anchorItem(), 5);
120 QCOMPARE(spyAnchor
.count(), 1);
121 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 5);
122 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 3);
123 spyAnchor
.takeFirst();
125 // Items between current and anchor should be selected now
126 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
127 QVERIFY(m_selectionManager
->hasSelection());
129 // Change current item again and check the selection
130 m_selectionManager
->setCurrentItem(2);
131 QCOMPARE(m_selectionManager
->currentItem(), 2);
132 QCOMPARE(spyCurrent
.count(), 1);
133 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
134 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 4);
135 spyCurrent
.takeFirst();
137 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
138 QVERIFY(m_selectionManager
->hasSelection());
140 // Inserting items should update current item and anchor item.
141 m_selectionManager
->itemsInserted(KItemRangeList() <<
146 QCOMPARE(m_selectionManager
->currentItem(), 5);
147 QCOMPARE(spyCurrent
.count(), 1);
148 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 5);
149 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 2);
150 spyCurrent
.takeFirst();
152 QCOMPARE(m_selectionManager
->anchorItem(), 8);
153 QCOMPARE(spyAnchor
.count(), 1);
154 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 8);
155 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 5);
156 spyAnchor
.takeFirst();
158 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8);
159 QVERIFY(m_selectionManager
->hasSelection());
161 // Removing items should update current item and anchor item.
162 m_selectionManager
->itemsRemoved(KItemRangeList() <<
167 QCOMPARE(m_selectionManager
->currentItem(), 2);
168 QCOMPARE(spyCurrent
.count(), 1);
169 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(0)), 2);
170 QCOMPARE(qvariant_cast
<int>(spyCurrent
.at(0).at(1)), 5);
171 spyCurrent
.takeFirst();
173 QCOMPARE(m_selectionManager
->anchorItem(), 5);
174 QCOMPARE(spyAnchor
.count(), 1);
175 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(0)), 5);
176 QCOMPARE(qvariant_cast
<int>(spyAnchor
.at(0).at(1)), 8);
177 spyAnchor
.takeFirst();
179 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
180 QVERIFY(m_selectionManager
->hasSelection());
182 // Verify that clearSelection() also clears the anchored selection.
183 m_selectionManager
->clearSelection();
184 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
185 QVERIFY(!m_selectionManager
->hasSelection());
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 QSet
<int> 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 QSet
<int> 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
->anchorItem(), 5);
278 m_selectionManager
->setCurrentItem(6);
279 QCOMPARE(m_selectionManager
->currentItem(), 6);
280 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6);
282 m_selectionManager
->setCurrentItem(4);
283 QCOMPARE(m_selectionManager
->currentItem(), 4);
284 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
286 m_selectionManager
->setCurrentItem(7);
287 QCOMPARE(m_selectionManager
->currentItem(), 7);
288 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 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(), QSet
<int>() << 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
->anchorItem(), 9);
300 m_selectionManager
->setCurrentItem(6);
301 QCOMPARE(m_selectionManager
->currentItem(), 6);
302 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8 << 9);
304 m_selectionManager
->setCurrentItem(10);
305 QCOMPARE(m_selectionManager
->currentItem(), 10);
306 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
308 m_selectionManager
->endAnchoredSelection();
309 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
310 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
314 enum ModelChangeType
{
321 Q_DECLARE_METATYPE(QSet
<int>);
322 Q_DECLARE_METATYPE(ModelChangeType
);
323 Q_DECLARE_METATYPE(KItemRangeList
);
325 void KItemListSelectionManagerTest::testChangeSelection_data()
327 QTest::addColumn
<QSet
<int> >("initialSelection");
328 QTest::addColumn
<int>("anchor");
329 QTest::addColumn
<int>("current");
330 QTest::addColumn
<QSet
<int> >("expectedSelection");
331 QTest::addColumn
<ModelChangeType
>("changeType");
332 QTest::addColumn
<KItemRangeList
>("changedItems");
333 QTest::addColumn
<QSet
<int> >("finalSelection");
335 QTest::newRow("No change")
336 << (QSet
<int>() << 5 << 6)
338 << (QSet
<int>() << 2 << 3 << 5 << 6)
339 << NoChange
<< KItemRangeList()
340 << (QSet
<int>() << 2 << 3 << 5 << 6);
342 QTest::newRow("Insert Items")
343 << (QSet
<int>() << 5 << 6)
345 << (QSet
<int>() << 2 << 3 << 5 << 6)
346 << InsertItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))
347 << (QSet
<int>() << 3 << 4 << 8 << 9);
349 QTest::newRow("Remove Items")
350 << (QSet
<int>() << 5 << 6)
352 << (QSet
<int>() << 2 << 3 << 5 << 6)
353 << RemoveItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))
354 << (QSet
<int>() << 1 << 2 << 3 << 4);
357 void KItemListSelectionManagerTest::testChangeSelection()
359 QFETCH(QSet
<int>, initialSelection
);
361 QFETCH(int, current
);
362 QFETCH(QSet
<int> , expectedSelection
);
363 QFETCH(ModelChangeType
, changeType
);
364 QFETCH(KItemRangeList
, changedItems
);
365 QFETCH(QSet
<int> , finalSelection
);
367 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)));
369 // Initial selection should be empty
370 QVERIFY(!m_selectionManager
->hasSelection());
371 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
373 // Perform the initial selectiion
374 m_selectionManager
->setSelectedItems(initialSelection
);
375 QCOMPARE(m_selectionManager
->selectedItems(), initialSelection
);
376 if (initialSelection
.isEmpty()) {
377 QVERIFY(!m_selectionManager
->hasSelection());
378 QCOMPARE(spySelectionChanged
.count(), 0);
381 QVERIFY(m_selectionManager
->hasSelection());
382 QCOMPARE(spySelectionChanged
.count(), 1);
383 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
384 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), initialSelection
);
385 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), QSet
<int>());
388 // Perform an anchored selection.
389 // Note that current and anchor index are equal first because this is the case in typical uses of the
390 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
391 m_selectionManager
->setCurrentItem(anchor
);
392 m_selectionManager
->beginAnchoredSelection(anchor
);
393 m_selectionManager
->setCurrentItem(current
);
394 QCOMPARE(m_selectionManager
->selectedItems(), expectedSelection
);
395 QCOMPARE(m_selectionManager
->hasSelection(), !expectedSelection
.isEmpty());
396 if (expectedSelection
== initialSelection
) {
397 QCOMPARE(spySelectionChanged
.count(), 0);
400 QCOMPARE(spySelectionChanged
.count(), 1);
401 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
402 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), expectedSelection
);
403 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), initialSelection
);
406 // Change the model by inserting or removing items.
407 switch (changeType
) {
409 m_selectionManager
->itemsInserted(changedItems
);
412 m_selectionManager
->itemsRemoved(changedItems
);
418 QCOMPARE(m_selectionManager
->selectedItems(), finalSelection
);
419 QCOMPARE(m_selectionManager
->hasSelection(), !finalSelection
.isEmpty());
420 if (finalSelection
== expectedSelection
) {
421 QCOMPARE(spySelectionChanged
.count(), 0);
424 QCOMPARE(spySelectionChanged
.count(), 1);
425 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
426 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), finalSelection
);
427 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), expectedSelection
);
430 // Finally, clear the selection
431 m_selectionManager
->clearSelection();
432 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
433 QVERIFY(!m_selectionManager
->hasSelection());
434 if (finalSelection
.isEmpty()) {
435 // Selection has been empty already
436 QCOMPARE(spySelectionChanged
.count(), 0);
439 QCOMPARE(spySelectionChanged
.count(), 1);
440 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
441 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), QSet
<int>());
442 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), finalSelection
);
446 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
448 #include "kitemlistselectionmanagertest.moc"