]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Remove unneded function KItemListSelectionManager::anchorItem()
[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 KItemListSelectionManager* m_selectionManager;
73 };
74
75 void KItemListSelectionManagerTest::init()
76 {
77 m_selectionManager = new KItemListSelectionManager();
78 m_selectionManager->setModel(new DummyModel());
79 }
80
81 void KItemListSelectionManagerTest::cleanup()
82 {
83 delete m_selectionManager->model();
84 delete m_selectionManager;
85 m_selectionManager = 0;
86 }
87
88 void KItemListSelectionManagerTest::testConstructor()
89 {
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);
94 }
95
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
97 {
98 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
99
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();
106
107 // Begin an anchored selection.
108 m_selectionManager->beginAnchoredSelection(5);
109 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
110 QCOMPARE(m_selectionManager->m_anchorItem, 5);
111
112 // Items between current and anchor should be selected now
113 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
114 QVERIFY(m_selectionManager->hasSelection());
115
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();
123
124 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
125 QVERIFY(m_selectionManager->hasSelection());
126
127 // Inserting items should update current item and anchor item.
128 m_selectionManager->itemsInserted(KItemRangeList() <<
129 KItemRange(0, 1) <<
130 KItemRange(2, 2) <<
131 KItemRange(6, 3));
132
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();
138
139 QCOMPARE(m_selectionManager->m_anchorItem, 8);
140
141 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
142 QVERIFY(m_selectionManager->hasSelection());
143
144 // Removing items should update current item and anchor item.
145 m_selectionManager->itemsRemoved(KItemRangeList() <<
146 KItemRange(0, 2) <<
147 KItemRange(2, 1) <<
148 KItemRange(9, 2));
149
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();
155
156 QCOMPARE(m_selectionManager->m_anchorItem, 5);
157
158 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
159 QVERIFY(m_selectionManager->hasSelection());
160
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());
165
166 m_selectionManager->endAnchoredSelection();
167 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
168 }
169
170 void KItemListSelectionManagerTest::testSetSelected_data()
171 {
172 QTest::addColumn<int>("index");
173 QTest::addColumn<int>("count");
174 QTest::addColumn<int>("expectedSelectionCount");
175
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;
183 }
184
185 void KItemListSelectionManagerTest::testSetSelected()
186 {
187 QFETCH(int, index);
188 QFETCH(int, count);
189 QFETCH(int, expectedSelectionCount);
190 m_selectionManager->setSelected(index, count);
191 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
192 }
193
194 void KItemListSelectionManagerTest::testItemsInserted()
195 {
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));
203
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));
211
212 // Insert 3 items between the selections
213 m_selectionManager->itemsInserted(KItemRangeList() <<
214 KItemRange(15, 1) <<
215 KItemRange(16, 1) <<
216 KItemRange(17, 1));
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));
222 }
223
224 void KItemListSelectionManagerTest::testItemsRemoved()
225 {
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));
232 }
233
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));
240 }
241
242 // Remove the items 6 , 8 and 10
243 m_selectionManager->itemsRemoved(KItemRangeList() <<
244 KItemRange(6, 1) <<
245 KItemRange(8, 1) <<
246 KItemRange(10, 1));
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));
252 }
253
254 void KItemListSelectionManagerTest::testAnchoredSelection()
255 {
256 m_selectionManager->beginAnchoredSelection(5);
257 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
258 QCOMPARE(m_selectionManager->m_anchorItem, 5);
259
260 m_selectionManager->setCurrentItem(6);
261 QCOMPARE(m_selectionManager->currentItem(), 6);
262 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
263
264 m_selectionManager->setCurrentItem(4);
265 QCOMPARE(m_selectionManager->currentItem(), 4);
266 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
267
268 m_selectionManager->setCurrentItem(7);
269 QCOMPARE(m_selectionManager->currentItem(), 7);
270 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
271
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);
276
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);
281
282 m_selectionManager->setCurrentItem(6);
283 QCOMPARE(m_selectionManager->currentItem(), 6);
284 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
285
286 m_selectionManager->setCurrentItem(10);
287 QCOMPARE(m_selectionManager->currentItem(), 10);
288 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
289
290 m_selectionManager->endAnchoredSelection();
291 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
292 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
293 }
294
295 namespace {
296 enum ChangeType {
297 NoChange,
298 InsertItems,
299 RemoveItems,
300 EndAnchoredSelection,
301 ToggleSelected
302 };
303 }
304
305 Q_DECLARE_METATYPE(QSet<int>);
306 Q_DECLARE_METATYPE(ChangeType);
307 Q_DECLARE_METATYPE(KItemRangeList);
308
309 void KItemListSelectionManagerTest::testChangeSelection_data()
310 {
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");
318
319 QTest::newRow("No change")
320 << (QSet<int>() << 5 << 6)
321 << 2 << 3
322 << (QSet<int>() << 2 << 3 << 5 << 6)
323 << NoChange << KItemRangeList()
324 << (QSet<int>() << 2 << 3 << 5 << 6);
325
326 QTest::newRow("Insert Items")
327 << (QSet<int>() << 5 << 6)
328 << 2 << 3
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);
332
333 QTest::newRow("Remove Items")
334 << (QSet<int>() << 5 << 6)
335 << 2 << 3
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);
339
340 QTest::newRow("Empty Anchored Selection")
341 << QSet<int>()
342 << 2 << 2
343 << QSet<int>()
344 << EndAnchoredSelection << KItemRangeList()
345 << QSet<int>();
346
347 QTest::newRow("Toggle selection")
348 << (QSet<int>() << 1 << 3 << 4)
349 << 6 << 8
350 << (QSet<int>() << 1 << 3 << 4 << 6 << 7 << 8)
351 << ToggleSelected << (KItemRangeList() << KItemRange(0, 10))
352 << (QSet<int>() << 0 << 2 << 5 << 9);
353 }
354
355 void KItemListSelectionManagerTest::testChangeSelection()
356 {
357 QFETCH(QSet<int>, initialSelection);
358 QFETCH(int, anchor);
359 QFETCH(int, current);
360 QFETCH(QSet<int> , expectedSelection);
361 QFETCH(ChangeType, changeType);
362 QFETCH(KItemRangeList, changedItems);
363 QFETCH(QSet<int> , finalSelection);
364
365 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
366
367 // Initial selection should be empty
368 QVERIFY(!m_selectionManager->hasSelection());
369 QVERIFY(m_selectionManager->selectedItems().isEmpty());
370
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);
377 }
378 else {
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>());
384 }
385
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);
398 }
399 else {
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);
404 }
405
406 // Change the model by inserting or removing items.
407 switch (changeType) {
408 case InsertItems:
409 m_selectionManager->itemsInserted(changedItems);
410 break;
411 case RemoveItems:
412 m_selectionManager->itemsRemoved(changedItems);
413 break;
414 case EndAnchoredSelection:
415 m_selectionManager->endAnchoredSelection();
416 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
417 break;
418 case ToggleSelected:
419 foreach(const KItemRange& range, changedItems) {
420 m_selectionManager->setSelected(range.index, range.count, KItemListSelectionManager::Toggle);
421 }
422 break;
423 case NoChange:
424 break;
425 }
426
427 QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
428 QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
429 if (finalSelection == expectedSelection) {
430 QCOMPARE(spySelectionChanged.count(), 0);
431 }
432 else {
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);
437 }
438
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);
446 }
447 else {
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);
452 }
453 }
454
455 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
456
457 #include "kitemlistselectionmanagertest.moc"