]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Add function KItemListSelectionManager::isSelected(int index)
[dolphin.git] / src / tests / kitemlistselectionmanagertest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include <qtest_kde.h>
22
23 #include "kitemviews/kitemmodelbase.h"
24 #include "kitemviews/kitemlistselectionmanager.h"
25
26 class DummyModel : public KItemModelBase
27 {
28 public:
29 DummyModel();
30 virtual int count() const;
31 virtual QHash<QByteArray, QVariant> data(int index) const;
32 };
33
34 DummyModel::DummyModel() :
35 KItemModelBase()
36 {
37 }
38
39 int DummyModel::count() const
40 {
41 return 100;
42 }
43
44 QHash<QByteArray, QVariant> DummyModel::data(int index) const
45 {
46 Q_UNUSED(index);
47 return QHash<QByteArray, QVariant>();
48 }
49
50
51
52 class KItemListSelectionManagerTest : public QObject
53 {
54 Q_OBJECT
55
56 private slots:
57 void init();
58 void cleanup();
59
60 void testConstructor();
61
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();
70
71 private:
72 void verifySelectionChange(QSignalSpy& spy, const QSet<int>& currentSelection, const QSet<int>& previousSelection) const;
73
74 KItemListSelectionManager* m_selectionManager;
75 };
76
77 void KItemListSelectionManagerTest::init()
78 {
79 m_selectionManager = new KItemListSelectionManager();
80 m_selectionManager->setModel(new DummyModel());
81 }
82
83 void KItemListSelectionManagerTest::cleanup()
84 {
85 delete m_selectionManager->model();
86 delete m_selectionManager;
87 m_selectionManager = 0;
88 }
89
90 void KItemListSelectionManagerTest::testConstructor()
91 {
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);
96 }
97
98 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
99 {
100 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
101
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();
108
109 // Begin an anchored selection.
110 m_selectionManager->beginAnchoredSelection(5);
111 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
112 QCOMPARE(m_selectionManager->m_anchorItem, 5);
113
114 // Items between current and anchor should be selected now
115 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
116 QVERIFY(m_selectionManager->hasSelection());
117
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();
125
126 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
127 QVERIFY(m_selectionManager->hasSelection());
128
129 // Inserting items should update current item and anchor item.
130 m_selectionManager->itemsInserted(KItemRangeList() <<
131 KItemRange(0, 1) <<
132 KItemRange(2, 2) <<
133 KItemRange(6, 3));
134
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();
140
141 QCOMPARE(m_selectionManager->m_anchorItem, 8);
142
143 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
144 QVERIFY(m_selectionManager->hasSelection());
145
146 // Removing items should update current item and anchor item.
147 m_selectionManager->itemsRemoved(KItemRangeList() <<
148 KItemRange(0, 2) <<
149 KItemRange(2, 1) <<
150 KItemRange(9, 2));
151
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();
157
158 QCOMPARE(m_selectionManager->m_anchorItem, 5);
159
160 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
161 QVERIFY(m_selectionManager->hasSelection());
162
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());
167
168 m_selectionManager->endAnchoredSelection();
169 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
170 }
171
172 void KItemListSelectionManagerTest::testSetSelected_data()
173 {
174 QTest::addColumn<int>("index");
175 QTest::addColumn<int>("count");
176 QTest::addColumn<int>("expectedSelectionCount");
177
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;
185 }
186
187 void KItemListSelectionManagerTest::testSetSelected()
188 {
189 QFETCH(int, index);
190 QFETCH(int, count);
191 QFETCH(int, expectedSelectionCount);
192 m_selectionManager->setSelected(index, count);
193 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
194 }
195
196 void KItemListSelectionManagerTest::testItemsInserted()
197 {
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));
205
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));
213
214 // Insert 3 items between the selections
215 m_selectionManager->itemsInserted(KItemRangeList() <<
216 KItemRange(15, 1) <<
217 KItemRange(16, 1) <<
218 KItemRange(17, 1));
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));
224 }
225
226 void KItemListSelectionManagerTest::testItemsRemoved()
227 {
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));
234 }
235
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));
242 }
243
244 // Remove the items 6 , 8 and 10
245 m_selectionManager->itemsRemoved(KItemRangeList() <<
246 KItemRange(6, 1) <<
247 KItemRange(8, 1) <<
248 KItemRange(10, 1));
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));
254 }
255
256 void KItemListSelectionManagerTest::testAnchoredSelection()
257 {
258 m_selectionManager->beginAnchoredSelection(5);
259 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
260 QCOMPARE(m_selectionManager->m_anchorItem, 5);
261
262 m_selectionManager->setCurrentItem(6);
263 QCOMPARE(m_selectionManager->currentItem(), 6);
264 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
265
266 m_selectionManager->setCurrentItem(4);
267 QCOMPARE(m_selectionManager->currentItem(), 4);
268 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
269
270 m_selectionManager->setCurrentItem(7);
271 QCOMPARE(m_selectionManager->currentItem(), 7);
272 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
273
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);
278
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);
283
284 m_selectionManager->setCurrentItem(6);
285 QCOMPARE(m_selectionManager->currentItem(), 6);
286 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
287
288 m_selectionManager->setCurrentItem(10);
289 QCOMPARE(m_selectionManager->currentItem(), 10);
290 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
291
292 m_selectionManager->endAnchoredSelection();
293 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
294 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
295 }
296
297 namespace {
298 enum ChangeType {
299 NoChange,
300 InsertItems,
301 RemoveItems,
302 MoveItems,
303 EndAnchoredSelection,
304 SetSelected
305 };
306 }
307
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>);
314
315 /**
316 * The following function provides a generic way to test the selection functionality.
317 *
318 * The test is data-driven and takes the following arguments:
319 *
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:
325 * - NoChange
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.
338 *
339 */
340
341 void KItemListSelectionManagerTest::testChangeSelection_data()
342 {
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");
350
351 QTest::newRow("No change")
352 << (QSet<int>() << 5 << 6)
353 << 2 << 3
354 << (QSet<int>() << 2 << 3 << 5 << 6)
355 << NoChange
356 << QList<QVariant>()
357 << (QSet<int>() << 2 << 3 << 5 << 6);
358
359 QTest::newRow("Insert Items")
360 << (QSet<int>() << 5 << 6)
361 << 2 << 3
362 << (QSet<int>() << 2 << 3 << 5 << 6)
363 << InsertItems
364 << (QList<QVariant>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5)))
365 << (QSet<int>() << 3 << 4 << 8 << 9);
366
367 QTest::newRow("Remove Items")
368 << (QSet<int>() << 5 << 6)
369 << 2 << 3
370 << (QSet<int>() << 2 << 3 << 5 << 6)
371 << RemoveItems
372 << (QList<QVariant>() << QVariant::fromValue(KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5)))
373 << (QSet<int>() << 1 << 2 << 3 << 4);
374
375 QTest::newRow("Empty Anchored Selection")
376 << QSet<int>()
377 << 2 << 2
378 << QSet<int>()
379 << EndAnchoredSelection
380 << QList<QVariant>()
381 << QSet<int>();
382
383 QTest::newRow("Toggle selection")
384 << (QSet<int>() << 1 << 3 << 4)
385 << 6 << 8
386 << (QSet<int>() << 1 << 3 << 4 << 6 << 7 << 8)
387 << SetSelected
388 << (QList<QVariant>() << 0 << 10 << QVariant::fromValue(KItemListSelectionManager::Toggle))
389 << (QSet<int>() << 0 << 2 << 5 << 9);
390
391 // Swap items 2, 3 and 4, 5
392 QTest::newRow("Move items")
393 << (QSet<int>() << 0 << 1 << 2 << 3)
394 << -1 << -1
395 << (QSet<int>() << 0 << 1 << 2 << 3)
396 << MoveItems
397 << (QList<QVariant>() << QVariant::fromValue(KItemRange(2, 4))
398 << QVariant::fromValue(QList<int>() << 4 << 5 << 2 << 3))
399 << (QSet<int>() << 0 << 1 << 4 << 5);
400
401 // Revert sort order
402 QTest::newRow("Revert sort order")
403 << (QSet<int>() << 0 << 1)
404 << 3 << 4
405 << (QSet<int>() << 0 << 1 << 3 << 4)
406 << MoveItems
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);
410 }
411
412 void KItemListSelectionManagerTest::testChangeSelection()
413 {
414 QFETCH(QSet<int>, initialSelection);
415 QFETCH(int, anchor);
416 QFETCH(int, current);
417 QFETCH(QSet<int>, expectedSelection);
418 QFETCH(ChangeType, changeType);
419 QFETCH(QList<QVariant>, data);
420 QFETCH(QSet<int>, finalSelection);
421
422 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
423
424 // Initial selection should be empty
425 QVERIFY(!m_selectionManager->hasSelection());
426 QVERIFY(m_selectionManager->selectedItems().isEmpty());
427
428 // Perform the initial selectiion
429 m_selectionManager->setSelectedItems(initialSelection);
430
431 verifySelectionChange(spySelectionChanged, initialSelection, QSet<int>());
432
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);
441
442 verifySelectionChange(spySelectionChanged, expectedSelection, initialSelection);
443
444 // Change the model by inserting or removing items.
445 switch (changeType) {
446 case InsertItems:
447 m_selectionManager->itemsInserted(data.at(0).value<KItemRangeList>());
448 break;
449 case RemoveItems:
450 m_selectionManager->itemsRemoved(data.at(0).value<KItemRangeList>());
451 break;
452 case MoveItems:
453 m_selectionManager->itemsMoved(data.at(0).value<KItemRange>(),
454 data.at(1).value<QList<int> >());
455 break;
456 case EndAnchoredSelection:
457 m_selectionManager->endAnchoredSelection();
458 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
459 break;
460 case SetSelected:
461 m_selectionManager->setSelected(data.at(0).value<int>(), // index
462 data.at(1).value<int>(), // count
463 data.at(2).value<KItemListSelectionManager::SelectionMode>());
464 break;
465 case NoChange:
466 break;
467 }
468
469 verifySelectionChange(spySelectionChanged, finalSelection, expectedSelection);
470
471 // Finally, clear the selection
472 m_selectionManager->clearSelection();
473
474 verifySelectionChange(spySelectionChanged, QSet<int>(), finalSelection);
475 }
476
477 void KItemListSelectionManagerTest::verifySelectionChange(QSignalSpy& spy,
478 const QSet<int>& currentSelection,
479 const QSet<int>& previousSelection) const
480 {
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));
486 }
487 else {
488 QVERIFY(!m_selectionManager->isSelected(index));
489 }
490 }
491
492 if (currentSelection == previousSelection) {
493 QCOMPARE(spy.count(), 0);
494 }
495 else {
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);
500 }
501 }
502
503 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
504
505 #include "kitemlistselectionmanagertest.moc"