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);
300 EndAnchoredSelection
,
305 Q_DECLARE_METATYPE(QSet
<int>);
306 Q_DECLARE_METATYPE(ChangeType
);
307 Q_DECLARE_METATYPE(KItemRangeList
);
309 void KItemListSelectionManagerTest::testChangeSelection_data()
311 QTest::addColumn
<QSet
<int> >("initialSelection");
312 QTest::addColumn
<int>("anchor");
313 QTest::addColumn
<int>("current");
314 QTest::addColumn
<QSet
<int> >("expectedSelection");
315 QTest::addColumn
<ChangeType
>("changeType");
316 QTest::addColumn
<KItemRangeList
>("changedItems");
317 QTest::addColumn
<QSet
<int> >("finalSelection");
319 QTest::newRow("No change")
320 << (QSet
<int>() << 5 << 6)
322 << (QSet
<int>() << 2 << 3 << 5 << 6)
323 << NoChange
<< KItemRangeList()
324 << (QSet
<int>() << 2 << 3 << 5 << 6);
326 QTest::newRow("Insert Items")
327 << (QSet
<int>() << 5 << 6)
329 << (QSet
<int>() << 2 << 3 << 5 << 6)
330 << InsertItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))
331 << (QSet
<int>() << 3 << 4 << 8 << 9);
333 QTest::newRow("Remove Items")
334 << (QSet
<int>() << 5 << 6)
336 << (QSet
<int>() << 2 << 3 << 5 << 6)
337 << RemoveItems
<< (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))
338 << (QSet
<int>() << 1 << 2 << 3 << 4);
340 QTest::newRow("Empty Anchored Selection")
344 << EndAnchoredSelection
<< KItemRangeList()
347 QTest::newRow("Toggle selection")
348 << (QSet
<int>() << 1 << 3 << 4)
350 << (QSet
<int>() << 1 << 3 << 4 << 6 << 7 << 8)
351 << ToggleSelected
<< (KItemRangeList() << KItemRange(0, 10))
352 << (QSet
<int>() << 0 << 2 << 5 << 9);
355 void KItemListSelectionManagerTest::testChangeSelection()
357 QFETCH(QSet
<int>, initialSelection
);
359 QFETCH(int, current
);
360 QFETCH(QSet
<int> , expectedSelection
);
361 QFETCH(ChangeType
, changeType
);
362 QFETCH(KItemRangeList
, changedItems
);
363 QFETCH(QSet
<int> , finalSelection
);
365 QSignalSpy
spySelectionChanged(m_selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)));
367 // Initial selection should be empty
368 QVERIFY(!m_selectionManager
->hasSelection());
369 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
371 // Perform the initial selectiion
372 m_selectionManager
->setSelectedItems(initialSelection
);
373 QCOMPARE(m_selectionManager
->selectedItems(), initialSelection
);
374 if (initialSelection
.isEmpty()) {
375 QVERIFY(!m_selectionManager
->hasSelection());
376 QCOMPARE(spySelectionChanged
.count(), 0);
379 QVERIFY(m_selectionManager
->hasSelection());
380 QCOMPARE(spySelectionChanged
.count(), 1);
381 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
382 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), initialSelection
);
383 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), QSet
<int>());
386 // Perform an anchored selection.
387 // Note that current and anchor index are equal first because this is the case in typical uses of the
388 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
389 m_selectionManager
->setCurrentItem(anchor
);
390 m_selectionManager
->beginAnchoredSelection(anchor
);
391 m_selectionManager
->setCurrentItem(current
);
392 QCOMPARE(m_selectionManager
->m_anchorItem
, anchor
);
393 QCOMPARE(m_selectionManager
->currentItem(), 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
);
414 case EndAnchoredSelection
:
415 m_selectionManager
->endAnchoredSelection();
416 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
419 foreach(const KItemRange
& range
, changedItems
) {
420 m_selectionManager
->setSelected(range
.index
, range
.count
, KItemListSelectionManager::Toggle
);
427 QCOMPARE(m_selectionManager
->selectedItems(), finalSelection
);
428 QCOMPARE(m_selectionManager
->hasSelection(), !finalSelection
.isEmpty());
429 if (finalSelection
== expectedSelection
) {
430 QCOMPARE(spySelectionChanged
.count(), 0);
433 QCOMPARE(spySelectionChanged
.count(), 1);
434 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
435 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), finalSelection
);
436 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), expectedSelection
);
439 // Finally, clear the selection
440 m_selectionManager
->clearSelection();
441 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>());
442 QVERIFY(!m_selectionManager
->hasSelection());
443 if (finalSelection
.isEmpty()) {
444 // Selection has been empty already
445 QCOMPARE(spySelectionChanged
.count(), 0);
448 QCOMPARE(spySelectionChanged
.count(), 1);
449 QList
<QVariant
> arguments
= spySelectionChanged
.takeFirst();
450 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(0)), QSet
<int>());
451 QCOMPARE(qvariant_cast
<QSet
<int> >(arguments
.at(1)), finalSelection
);
455 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
457 #include "kitemlistselectionmanagertest.moc"