]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Test signal emission on selection changes
[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 ModelChangeType {
315 NoChange,
316 InsertItems,
317 RemoveItems
318 };
319 }
320
321 Q_DECLARE_METATYPE(QSet<int>);
322 Q_DECLARE_METATYPE(ModelChangeType);
323 Q_DECLARE_METATYPE(KItemRangeList);
324
325 void KItemListSelectionManagerTest::testChangeSelection_data()
326 {
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");
334
335 QTest::newRow("No change")
336 << (QSet<int>() << 5 << 6)
337 << 2 << 3
338 << (QSet<int>() << 2 << 3 << 5 << 6)
339 << NoChange << KItemRangeList()
340 << (QSet<int>() << 2 << 3 << 5 << 6);
341
342 QTest::newRow("Insert Items")
343 << (QSet<int>() << 5 << 6)
344 << 2 << 3
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);
348
349 QTest::newRow("Remove Items")
350 << (QSet<int>() << 5 << 6)
351 << 2 << 3
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);
355 }
356
357 void KItemListSelectionManagerTest::testChangeSelection()
358 {
359 QFETCH(QSet<int>, initialSelection);
360 QFETCH(int, anchor);
361 QFETCH(int, current);
362 QFETCH(QSet<int> , expectedSelection);
363 QFETCH(ModelChangeType, changeType);
364 QFETCH(KItemRangeList, changedItems);
365 QFETCH(QSet<int> , finalSelection);
366
367 QSignalSpy spySelectionChanged(m_selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)));
368
369 // Initial selection should be empty
370 QVERIFY(!m_selectionManager->hasSelection());
371 QVERIFY(m_selectionManager->selectedItems().isEmpty());
372
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);
379 }
380 else {
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>());
386 }
387
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);
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 NoChange:
415 break;
416 }
417
418 QCOMPARE(m_selectionManager->selectedItems(), finalSelection);
419 QCOMPARE(m_selectionManager->hasSelection(), !finalSelection.isEmpty());
420 if (finalSelection == expectedSelection) {
421 QCOMPARE(spySelectionChanged.count(), 0);
422 }
423 else {
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);
428 }
429
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);
437 }
438 else {
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);
443 }
444 }
445
446 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
447
448 #include "kitemlistselectionmanagertest.moc"