]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Always use the 'Select' mode for anchored selections
[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
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 // Set anchor item and check that the selection manager emits the anchorChanged(int,int) signal correctly.
108 m_selectionManager->setAnchorItem(3);
109 QCOMPARE(m_selectionManager->anchorItem(), 3);
110 QCOMPARE(spyAnchor.count(), 1);
111 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 3);
112 spyAnchor.takeFirst();
113
114 m_selectionManager->setAnchorItem(5);
115 QCOMPARE(m_selectionManager->anchorItem(), 5);
116 QCOMPARE(spyAnchor.count(), 1);
117 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
118 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 3);
119 spyAnchor.takeFirst();
120
121 // Items between current and anchor should be selected now
122 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 4 << 5);
123 QVERIFY(m_selectionManager->hasSelection());
124
125 // Change current item again and check the selection
126 m_selectionManager->setCurrentItem(2);
127 QCOMPARE(m_selectionManager->currentItem(), 2);
128 QCOMPARE(spyCurrent.count(), 1);
129 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
130 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 4);
131 spyCurrent.takeFirst();
132
133 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
134 QVERIFY(m_selectionManager->hasSelection());
135
136 // Inserting items should update current item and anchor item.
137 m_selectionManager->itemsInserted(KItemRangeList() <<
138 KItemRange(0, 1) <<
139 KItemRange(2, 2) <<
140 KItemRange(6, 3));
141
142 QCOMPARE(m_selectionManager->currentItem(), 5);
143 QCOMPARE(spyCurrent.count(), 1);
144 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
145 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
146 spyCurrent.takeFirst();
147
148 QCOMPARE(m_selectionManager->anchorItem(), 8);
149 QCOMPARE(spyAnchor.count(), 1);
150 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
151 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
152 spyAnchor.takeFirst();
153
154 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 5 << 6 << 7 << 8);
155 QVERIFY(m_selectionManager->hasSelection());
156
157 // Removing items should update current item and anchor item.
158 m_selectionManager->itemsRemoved(KItemRangeList() <<
159 KItemRange(0, 2) <<
160 KItemRange(2, 1) <<
161 KItemRange(9, 2));
162
163 QCOMPARE(m_selectionManager->currentItem(), 2);
164 QCOMPARE(spyCurrent.count(), 1);
165 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
166 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
167 spyCurrent.takeFirst();
168
169 QCOMPARE(m_selectionManager->anchorItem(), 5);
170 QCOMPARE(spyAnchor.count(), 1);
171 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
172 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
173 spyAnchor.takeFirst();
174
175 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << 2 << 3 << 4 << 5);
176 QVERIFY(m_selectionManager->hasSelection());
177
178 // Verify that clearSelection() also clears the anchored selection.
179 m_selectionManager->clearSelection();
180 QCOMPARE(m_selectionManager->selectedItems(), QSet<int>());
181 QVERIFY(!m_selectionManager->hasSelection());
182 }
183
184 void KItemListSelectionManagerTest::testSetSelected_data()
185 {
186 QTest::addColumn<int>("index");
187 QTest::addColumn<int>("count");
188 QTest::addColumn<int>("expectedSelectionCount");
189
190 QTest::newRow("Select all") << 0 << 100 << 100;
191 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
192 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
193 QTest::newRow("Too small index") << -1 << 100 << 0;
194 QTest::newRow("Too large index") << 100 << 100 << 0;
195 QTest::newRow("Too large count") << 0 << 100000 << 100;
196 QTest::newRow("Too small count") << 0 << 0 << 0;
197 }
198
199 void KItemListSelectionManagerTest::testSetSelected()
200 {
201 QFETCH(int, index);
202 QFETCH(int, count);
203 QFETCH(int, expectedSelectionCount);
204 m_selectionManager->setSelected(index, count);
205 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
206 }
207
208 void KItemListSelectionManagerTest::testItemsInserted()
209 {
210 // Select items 10 to 12
211 m_selectionManager->setSelected(10, 3);
212 QSet<int> selectedItems = m_selectionManager->selectedItems();
213 QVERIFY(selectedItems.contains(10));
214 QVERIFY(selectedItems.contains(11));
215 QVERIFY(selectedItems.contains(12));
216
217 // Insert items 0 to 4 -> selection must be 15 to 17
218 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 5));
219 selectedItems = m_selectionManager->selectedItems();
220 QVERIFY(selectedItems.contains(15));
221 QVERIFY(selectedItems.contains(16));
222 QVERIFY(selectedItems.contains(17));
223
224 // Insert 3 items between the selections
225 m_selectionManager->itemsInserted(KItemRangeList() <<
226 KItemRange(15, 1) <<
227 KItemRange(16, 1) <<
228 KItemRange(17, 1));
229 selectedItems = m_selectionManager->selectedItems();
230 QVERIFY(selectedItems.contains(16));
231 QVERIFY(selectedItems.contains(18));
232 QVERIFY(selectedItems.contains(20));
233 }
234
235 void KItemListSelectionManagerTest::testItemsRemoved()
236 {
237 // Select items 10 to 15
238 m_selectionManager->setSelected(10, 6);
239 QSet<int> selectedItems = m_selectionManager->selectedItems();
240 for (int i = 10; i <= 15; ++i) {
241 QVERIFY(selectedItems.contains(i));
242 }
243
244 // Remove items 0 to 4 -> selection must be 5 to 10
245 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
246 selectedItems = m_selectionManager->selectedItems();
247 for (int i = 5; i <= 10; ++i) {
248 QVERIFY(selectedItems.contains(i));
249 }
250
251 // Remove the items 6 , 8 and 10
252 m_selectionManager->itemsRemoved(KItemRangeList() <<
253 KItemRange(6, 1) <<
254 KItemRange(8, 1) <<
255 KItemRange(10, 1));
256 selectedItems = m_selectionManager->selectedItems();
257 QCOMPARE(selectedItems.count(), 3);
258 QVERIFY(selectedItems.contains(5));
259 QVERIFY(selectedItems.contains(6));
260 QVERIFY(selectedItems.contains(7));
261 }
262
263 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
264
265 #include "kitemlistselectionmanagertest.moc"