]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Initial support for anchored selections in the selection manager
[dolphin.git] / src / tests / kitemlistselectionmanagertest.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include <qtest_kde.h>
21
22 #include "kitemviews/kitemmodelbase.h"
23 #include "kitemviews/kitemlistselectionmanager.h"
24
25 class DummyModel : public KItemModelBase
26 {
27 public:
28 DummyModel();
29 virtual int count() const;
30 virtual QHash<QByteArray, QVariant> data(int index) const;
31 };
32
33 DummyModel::DummyModel() :
34 KItemModelBase()
35 {
36 }
37
38 int DummyModel::count() const
39 {
40 return 100;
41 }
42
43 QHash<QByteArray, QVariant> DummyModel::data(int index) const
44 {
45 Q_UNUSED(index);
46 return QHash<QByteArray, QVariant>();
47 }
48
49
50
51 class KItemListSelectionManagerTest : public QObject
52 {
53 Q_OBJECT
54
55 private slots:
56 void init();
57 void cleanup();
58
59 void testConstructor();
60
61 void testCurrentItemAnchorItem();
62 void testSetSelected_data();
63 void testSetSelected();
64 void testItemsInserted();
65 void testItemsRemoved();
66
67 private:
68 KItemListSelectionManager* m_selectionManager;
69 };
70
71 void KItemListSelectionManagerTest::init()
72 {
73 m_selectionManager = new KItemListSelectionManager();
74 m_selectionManager->setModel(new DummyModel());
75 }
76
77 void KItemListSelectionManagerTest::cleanup()
78 {
79 delete m_selectionManager->model();
80 delete m_selectionManager;
81 m_selectionManager = 0;
82 }
83
84 void KItemListSelectionManagerTest::testConstructor()
85 {
86 QVERIFY(!m_selectionManager->hasSelection());
87 QCOMPARE(m_selectionManager->selectedItems().count(), 0);
88 QCOMPARE(m_selectionManager->currentItem(), 0);
89 QCOMPARE(m_selectionManager->anchorItem(), -1);
90 }
91
92 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
93 {
94 QSignalSpy spyCurrent(m_selectionManager, SIGNAL(currentChanged(int,int)));
95 QSignalSpy spyAnchor(m_selectionManager, SIGNAL(anchorChanged(int,int)));
96
97 m_selectionManager->setAnchoredSelectionActive(true);
98 QVERIFY(m_selectionManager->isAnchoredSelectionActive());
99 m_selectionManager->setAnchoredSelectionMode(KItemListSelectionManager::Select);
100 QCOMPARE(m_selectionManager->anchoredSelectionMode(), KItemListSelectionManager::Select);
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 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
266
267 #include "kitemlistselectionmanagertest.moc"