]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Implement beginAnchoredSelection() and endAnchoredSelection().
[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
69 private:
70 KItemListSelectionManager* m_selectionManager;
71 };
72
73 void KItemListSelectionManagerTest::init()
74 {
75 m_selectionManager = new KItemListSelectionManager();
76 m_selectionManager->setModel(new DummyModel());
77 }
78
79 void KItemListSelectionManagerTest::cleanup()
80 {
81 delete m_selectionManager->model();
82 delete m_selectionManager;
83 m_selectionManager = 0;
84 }
85
86 void KItemListSelectionManagerTest::testConstructor()
87 {
88 QVERIFY(!m_selectionManager->hasSelection());
89 QCOMPARE(m_selectionManager->selectedItems().count(), 0);
90 QCOMPARE(m_selectionManager->currentItem(), 0);
91 QCOMPARE(m_selectionManager->anchorItem(), -1);
92 }
93
94 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
95 {
96 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
97 QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
98
99 m_selectionManager->setAnchoredSelectionActive(true);
100 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
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 // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
110 m_selectionManager->setAnchorItem(3);
111 QCOMPARE(m_selectionManager->anchorItem(), 3);
112 QCOMPARE(spyAnchor.count(), 1);
113 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 3);
114 spyAnchor.takeFirst();
115
116 m_selectionManager->setAnchorItem(5);
117 QCOMPARE(m_selectionManager->anchorItem(), 5);
118 QCOMPARE(spyAnchor.count(), 1);
119 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
120 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
121 spyAnchor.takeFirst();
122
123 // Items between current and anchor should be selected now
124 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
125 QVERIFY(m_selectionManager->hasSelection());
126
127 // Change current item again and check the selection
128 m_selectionManager->setCurrentItem(2);
129 QCOMPARE(m_selectionManager->currentItem(), 2);
130 QCOMPARE(spyCurrent.count(), 1);
131 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
132 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 4);
133 spyCurrent.takeFirst();
134
135 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
136 QVERIFY(m_selectionManager->hasSelection());
137
138 // Inserting items should update current item and anchor item.
139 m_selectionManager->itemsInserted(KItemRangeList() <<
140 KItemRange(0, 1) <<
141 KItemRange(2, 2) <<
142 KItemRange(6, 3));
143
144 QCOMPARE(m_selectionManager->currentItem(), 5);
145 QCOMPARE(spyCurrent.count(), 1);
146 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
147 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
148 spyCurrent.takeFirst();
149
150 QCOMPARE(m_selectionManager->anchorItem(), 8);
151 QCOMPARE(spyAnchor.count(), 1);
152 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
153 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
154 spyAnchor.takeFirst();
155
156 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
157 QVERIFY(m_selectionManager->hasSelection());
158
159 // Removing items should update current item and anchor item.
160 m_selectionManager->itemsRemoved(KItemRangeList() <<
161 KItemRange(0, 2) <<
162 KItemRange(2, 1) <<
163 KItemRange(9, 2));
164
165 QCOMPARE(m_selectionManager->currentItem(), 2);
166 QCOMPARE(spyCurrent.count(), 1);
167 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
168 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
169 spyCurrent.takeFirst();
170
171 QCOMPARE(m_selectionManager->anchorItem(), 5);
172 QCOMPARE(spyAnchor.count(), 1);
173 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
174 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
175 spyAnchor.takeFirst();
176
177 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
178 QVERIFY(m_selectionManager->hasSelection());
179
180 // Verify that clearSelection() also clears the anchored selection.
181 m_selectionManager->clearSelection();
182 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
183 QVERIFY(!m_selectionManager->hasSelection());
184 }
185
186 void KItemListSelectionManagerTest::testSetSelected_data()
187 {
188 QTest::addColumn<int>("index");
189 QTest::addColumn<int>("count");
190 QTest::addColumn<int>("expectedSelectionCount");
191
192 QTest::newRow("Select all") << 0 << 100 << 100;
193 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
194 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
195 QTest::newRow("Too small index") << -1 << 100 << 0;
196 QTest::newRow("Too large index") << 100 << 100 << 0;
197 QTest::newRow("Too large count") << 0 << 100000 << 100;
198 QTest::newRow("Too small count") << 0 << 0 << 0;
199 }
200
201 void KItemListSelectionManagerTest::testSetSelected()
202 {
203 QFETCH(int, index);
204 QFETCH(int, count);
205 QFETCH(int, expectedSelectionCount);
206 m_selectionManager->setSelected(index, count);
207 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
208 }
209
210 void KItemListSelectionManagerTest::testItemsInserted()
211 {
212 // Select items 10 to 12
213 m_selectionManager->setSelected(10, 3);
214 QSet<int> selectedItems = m_selectionManager->selectedItems();
215 QVERIFY(selectedItems.contains(10));
216 QVERIFY(selectedItems.contains(11));
217 QVERIFY(selectedItems.contains(12));
218
219 // Insert items 0 to 4 -> selection must be 15 to 17
220 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 5));
221 selectedItems = m_selectionManager->selectedItems();
222 QVERIFY(selectedItems.contains(15));
223 QVERIFY(selectedItems.contains(16));
224 QVERIFY(selectedItems.contains(17));
225
226 // Insert 3 items between the selections
227 m_selectionManager->itemsInserted(KItemRangeList() <<
228 KItemRange(15, 1) <<
229 KItemRange(16, 1) <<
230 KItemRange(17, 1));
231 selectedItems = m_selectionManager->selectedItems();
232 QVERIFY(selectedItems.contains(16));
233 QVERIFY(selectedItems.contains(18));
234 QVERIFY(selectedItems.contains(20));
235 }
236
237 void KItemListSelectionManagerTest::testItemsRemoved()
238 {
239 // Select items 10 to 15
240 m_selectionManager->setSelected(10, 6);
241 QSet<int> selectedItems = m_selectionManager->selectedItems();
242 for (int i = 10; i <= 15; ++i) {
243 QVERIFY(selectedItems.contains(i));
244 }
245
246 // Remove items 0 to 4 -> selection must be 5 to 10
247 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
248 selectedItems = m_selectionManager->selectedItems();
249 for (int i = 5; i <= 10; ++i) {
250 QVERIFY(selectedItems.contains(i));
251 }
252
253 // Remove the items 6 , 8 and 10
254 m_selectionManager->itemsRemoved(KItemRangeList() <<
255 KItemRange(6, 1) <<
256 KItemRange(8, 1) <<
257 KItemRange(10, 1));
258 selectedItems = m_selectionManager->selectedItems();
259 QCOMPARE(selectedItems.count(), 3);
260 QVERIFY(selectedItems.contains(5));
261 QVERIFY(selectedItems.contains(6));
262 QVERIFY(selectedItems.contains(7));
263 }
264
265 void KItemListSelectionManagerTest::testAnchoredSelection()
266 {
267 m_selectionManager->beginAnchoredSelection(5);
268 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
269 QCOMPARE(m_selectionManager->anchorItem(), 5);
270
271 m_selectionManager->setCurrentItem(6);
272 QCOMPARE(m_selectionManager->currentItem(), 6);
273 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6);
274
275 m_selectionManager->setCurrentItem(4);
276 QCOMPARE(m_selectionManager->currentItem(), 4);
277 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
278
279 m_selectionManager->setCurrentItem(7);
280 QCOMPARE(m_selectionManager->currentItem(), 7);
281 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
282
283 // Ending the anchored selection should not change the selected items.
284 m_selectionManager->endAnchoredSelection();
285 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
286 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7);
287
288 // Start a new anchored selection that overlaps the previous one
289 m_selectionManager->beginAnchoredSelection(9);
290 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
291 QCOMPARE(m_selectionManager->anchorItem(), 9);
292
293 m_selectionManager->setCurrentItem(6);
294 QCOMPARE(m_selectionManager->currentItem(), 6);
295 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8 << 9);
296
297 m_selectionManager->setCurrentItem(10);
298 QCOMPARE(m_selectionManager->currentItem(), 10);
299 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
300
301 m_selectionManager->endAnchoredSelection();
302 QVERIFY(!m_selectionManager->isAnchoredSelectionActive());
303 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 9 << 10);
304 }
305
306 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
307
308 #include "kitemlistselectionmanagertest.moc"