]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Implement selection of items using mouse clicks.
[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->anchorItem(), -1);
94 }
95
96 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
97 {
98 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
99 QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
100
101 m_selectionManager->setAnchoredSelectionActive(true);
102 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
103
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();
110
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();
117
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();
124
125 // Items between current and anchor should be selected now
126 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
127 QVERIFY(m_selectionManager->hasSelection());
128
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();
136
137 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
138 QVERIFY(m_selectionManager->hasSelection());
139
140 // Inserting items should update current item and anchor item.
141 m_selectionManager->itemsInserted(KItemRangeList() <<
142 KItemRange(0, 1) <<
143 KItemRange(2, 2) <<
144 KItemRange(6, 3));
145
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();
151
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();
157
158 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
159 QVERIFY(m_selectionManager->hasSelection());
160
161 // Removing items should update current item and anchor item.
162 m_selectionManager->itemsRemoved(KItemRangeList() <<
163 KItemRange(0, 2) <<
164 KItemRange(2, 1) <<
165 KItemRange(9, 2));
166
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();
172
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();
178
179 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
180 QVERIFY(m_selectionManager->hasSelection());
181
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());
186 }
187
188 void KItemListSelectionManagerTest::testSetSelected_data()
189 {
190 QTest::addColumn<int>("index");
191 QTest::addColumn<int>("count");
192 QTest::addColumn<int>("expectedSelectionCount");
193
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;
201 }
202
203 void KItemListSelectionManagerTest::testSetSelected()
204 {
205 QFETCH(int, index);
206 QFETCH(int, count);
207 QFETCH(int, expectedSelectionCount);
208 m_selectionManager->setSelected(index, count);
209 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
210 }
211
212 void KItemListSelectionManagerTest::testItemsInserted()
213 {
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));
221
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));
229
230 // Insert 3 items between the selections
231 m_selectionManager->itemsInserted(KItemRangeList() <<
232 KItemRange(15, 1) <<
233 KItemRange(16, 1) <<
234 KItemRange(17, 1));
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));
240 }
241
242 void KItemListSelectionManagerTest::testItemsRemoved()
243 {
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));
250 }
251
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));
258 }
259
260 // Remove the items 6 , 8 and 10
261 m_selectionManager->itemsRemoved(KItemRangeList() <<
262 KItemRange(6, 1) <<
263 KItemRange(8, 1) <<
264 KItemRange(10, 1));
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));
270 }
271
272 void KItemListSelectionManagerTest::testAnchoredSelection()
273 {
274 m_selectionManager->beginAnchoredSelection(5);
275 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
276 QCOMPARE(m_selectionManager->anchorItem(), 5);
277
278 m_selectionManager->setCurrentItem(6);
279 QCOMPARE(m_selectionManager->currentItem(), 6);
280 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
281
282 m_selectionManager->setCurrentItem(4);
283 QCOMPARE(m_selectionManager->currentItem(), 4);
284 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
285
286 m_selectionManager->setCurrentItem(7);
287 QCOMPARE(m_selectionManager->currentItem(), 7);
288 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
289
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);
294
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);
299
300 m_selectionManager->setCurrentItem(6);
301 QCOMPARE(m_selectionManager->currentItem(), 6);
302 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
303
304 m_selectionManager->setCurrentItem(10);
305 QCOMPARE(m_selectionManager->currentItem(), 10);
306 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
307
308 m_selectionManager->endAnchoredSelection();
309 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
310 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
311 }
312
313 namespace {
314 enum ChangeType {
315 NoChange,
316 InsertItems,
317 RemoveItems,
318 EndAnchoredSelection
319 };
320 }
321
322 Q_DECLARE_METATYPE(QSet<int>);
323 Q_DECLARE_METATYPE(ChangeType);
324 Q_DECLARE_METATYPE(KItemRangeList);
325
326 void KItemListSelectionManagerTest::testChangeSelection_data()
327 {
328 QTest::addColumn<QSet<int> >("initialSelection");
329 QTest::addColumn<int>("anchor");
330 QTest::addColumn<int>("current");
331 QTest::addColumn<QSet<int> >("expectedSelection");
332 QTest::addColumn<ChangeType>("changeType");
333 QTest::addColumn<KItemRangeList>("changedItems");
334 QTest::addColumn<QSet<int> >("finalSelection");
335
336 QTest::newRow("No change")
337 << (QSet<int>() << 5 << 6)
338 << 2 << 3
339 << (QSet<int>() << 2 << 3 << 5 << 6)
340 << NoChange << KItemRangeList()
341 << (QSet<int>() << 2 << 3 << 5 << 6);
342
343 QTest::newRow("Insert Items")
344 << (QSet<int>() << 5 << 6)
345 << 2 << 3
346 << (QSet<int>() << 2 << 3 << 5 << 6)
347 << InsertItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(5, 2) << KItemRange(10, 5))
348 << (QSet<int>() << 3 << 4 << 8 << 9);
349
350 QTest::newRow("Remove Items")
351 << (QSet<int>() << 5 << 6)
352 << 2 << 3
353 << (QSet<int>() << 2 << 3 << 5 << 6)
354 << RemoveItems << (KItemRangeList() << KItemRange(1, 1) << KItemRange(3, 1) << KItemRange(10, 5))
355 << (QSet<int>() << 1 << 2 << 3 << 4);
356
357 QTest::newRow("Empty Anchored Selection")
358 << QSet<int>()
359 << 2 << 2
360 << QSet<int>()
361 << EndAnchoredSelection << KItemRangeList()
362 << QSet<int>();
363 }
364
365 void KItemListSelectionManagerTest::testChangeSelection()
366 {
367 QFETCH(QSet<int>, initialSelection);
368 QFETCH(int, anchor);
369 QFETCH(int, current);
370 QFETCH(QSet<int> , expectedSelection);
371 QFETCH(ChangeType, changeType);
372 QFETCH(KItemRangeList, changedItems);
373 QFETCH(QSet<int> , finalSelection);
374
375 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
376
377 // Initial selection should be empty
378 QVERIFY(!m_selectionManager->hasSelection());
379 QVERIFY(m_selectionManager->selectedItems().isEmpty());
380
381 // Perform the initial selectiion
382 m_selectionManager->setSelectedItems(initialSelection);
383 QCOMPARE(m_selectionManager->selectedItems(), initialSelection);
384 if (initialSelection.isEmpty()) {
385 QVERIFY(!m_selectionManager->hasSelection());
386 QCOMPARE(spySelectionChanged.count(), 0);
387 }
388 else {
389 QVERIFY(m_selectionManager->hasSelection());
390 QCOMPARE(spySelectionChanged.count(), 1);
391 QList<QVariant> arguments = spySelectionChanged.takeFirst();
392 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), initialSelection);
393 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), QSet<int>());
394 }
395
396 // Perform an anchored selection.
397 // Note that current and anchor index are equal first because this is the case in typical uses of the
398 // selection manager, and because this makes it easier to test the correctness of the signal's arguments.
399 m_selectionManager->setCurrentItem(anchor);
400 m_selectionManager->beginAnchoredSelection(anchor);
401 m_selectionManager->setCurrentItem(current);
402 QCOMPARE(m_selectionManager->selectedItems(), expectedSelection);
403 QCOMPARE(m_selectionManager->hasSelection(), !expectedSelection.isEmpty());
404 if (expectedSelection == initialSelection) {
405 QCOMPARE(spySelectionChanged.count(), 0);
406 }
407 else {
408 QCOMPARE(spySelectionChanged.count(), 1);
409 QList<QVariant> arguments = spySelectionChanged.takeFirst();
410 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), expectedSelection);
411 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), initialSelection);
412 }
413
414 // Change the model by inserting or removing items.
415 switch (changeType) {
416 case InsertItems:
417 m_selectionManager->itemsInserted(changedItems);
418 break;
419 case RemoveItems:
420 m_selectionManager->itemsRemoved(changedItems);
421 break;
422 case EndAnchoredSelection:
423 m_selectionManager->endAnchoredSelection();
424 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
425 break;
426 case NoChange:
427 break;
428 }
429
430 QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
431 QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
432 if (finalSelection == expectedSelection) {
433 QCOMPARE(spySelectionChanged.count(), 0);
434 }
435 else {
436 QCOMPARE(spySelectionChanged.count(), 1);
437 QList<QVariant> arguments = spySelectionChanged.takeFirst();
438 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), finalSelection);
439 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), expectedSelection);
440 }
441
442 // Finally, clear the selection
443 m_selectionManager->clearSelection();
444 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
445 QVERIFY(!m_selectionManager->hasSelection());
446 if (finalSelection.isEmpty()) {
447 // Selection has been empty already
448 QCOMPARE(spySelectionChanged.count(), 0);
449 }
450 else {
451 QCOMPARE(spySelectionChanged.count(), 1);
452 QList<QVariant> arguments = spySelectionChanged.takeFirst();
453 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(0)), QSet<int>());
454 QCOMPARE(qvariant_cast<QSet<int> >(arguments.at(1)), finalSelection);
455 }
456 }
457
458 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
459
460 #include "kitemlistselectionmanagertest.moc"