1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2011 by Frank Reininghaus <frank78ac@googlemail.com> *
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. *
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. *
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 ***************************************************************************/
21 #include <qtest_kde.h>
23 #include "kitemviews/kitemmodelbase.h"
24 #include "kitemviews/kitemlistselectionmanager.h"
26 class DummyModel
: public KItemModelBase
30 virtual int count() const;
31 virtual QHash
<QByteArray
, QVariant
> data(int index
) const;
34 DummyModel::DummyModel() :
39 int DummyModel::count() const
44 QHash
<QByteArray
, QVariant
> DummyModel::data(int index
) const
47 return QHash
<QByteArray
, QVariant
>();
52 class KItemListSelectionManagerTest
: public QObject
60 void testConstructor();
62 void testCurrentItemAnchorItem();
63 void testSetSelected_data();
64 void testSetSelected();
65 void testItemsInserted();
66 void testItemsRemoved();
67 void testAnchoredSelection();
70 KItemListSelectionManager
* m_selectionManager
;
73 void KItemListSelectionManagerTest::init()
75 m_selectionManager
= new KItemListSelectionManager();
76 m_selectionManager
->setModel(new DummyModel());
79 void KItemListSelectionManagerTest::cleanup()
81 delete m_selectionManager
->model();
82 delete m_selectionManager
;
83 m_selectionManager
= 0;
86 void KItemListSelectionManagerTest::testConstructor()
88 QVERIFY(!m_selectionManager
->hasSelection());
89 QCOMPARE(m_selectionManager
->selectedItems().count(), 0);
90 QCOMPARE(m_selectionManager
->currentItem(), 0);
91 QCOMPARE(m_selectionManager
->anchorItem(), -1);
94 void KItemListSelectionManagerTest::testCurrentItemAnchorItem()
96 QSignalSpy
spyCurrent(m_selectionManager
, SIGNAL(currentChanged(int,int)));
97 QSignalSpy
spyAnchor(m_selectionManager
, SIGNAL(anchorChanged(int,int)));
99 m_selectionManager
->setAnchoredSelectionActive(true);
100 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
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();
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();
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();
123 // Items between current and anchor should be selected now
124 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
125 QVERIFY(m_selectionManager
->hasSelection());
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();
135 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
136 QVERIFY(m_selectionManager
->hasSelection());
138 // Inserting items should update current item and anchor item.
139 m_selectionManager
->itemsInserted(KItemRangeList() <<
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();
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();
156 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8);
157 QVERIFY(m_selectionManager
->hasSelection());
159 // Removing items should update current item and anchor item.
160 m_selectionManager
->itemsRemoved(KItemRangeList() <<
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();
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();
177 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 2 << 3 << 4 << 5);
178 QVERIFY(m_selectionManager
->hasSelection());
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());
186 void KItemListSelectionManagerTest::testSetSelected_data()
188 QTest::addColumn
<int>("index");
189 QTest::addColumn
<int>("count");
190 QTest::addColumn
<int>("expectedSelectionCount");
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;
201 void KItemListSelectionManagerTest::testSetSelected()
205 QFETCH(int, expectedSelectionCount
);
206 m_selectionManager
->setSelected(index
, count
);
207 QCOMPARE(m_selectionManager
->selectedItems().count(), expectedSelectionCount
);
210 void KItemListSelectionManagerTest::testItemsInserted()
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));
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));
226 // Insert 3 items between the selections
227 m_selectionManager
->itemsInserted(KItemRangeList() <<
231 selectedItems
= m_selectionManager
->selectedItems();
232 QVERIFY(selectedItems
.contains(16));
233 QVERIFY(selectedItems
.contains(18));
234 QVERIFY(selectedItems
.contains(20));
237 void KItemListSelectionManagerTest::testItemsRemoved()
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
));
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
));
253 // Remove the items 6 , 8 and 10
254 m_selectionManager
->itemsRemoved(KItemRangeList() <<
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));
265 void KItemListSelectionManagerTest::testAnchoredSelection()
267 m_selectionManager
->beginAnchoredSelection(5);
268 QVERIFY(m_selectionManager
->isAnchoredSelectionActive());
269 QCOMPARE(m_selectionManager
->anchorItem(), 5);
271 m_selectionManager
->setCurrentItem(6);
272 QCOMPARE(m_selectionManager
->currentItem(), 6);
273 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6);
275 m_selectionManager
->setCurrentItem(4);
276 QCOMPARE(m_selectionManager
->currentItem(), 4);
277 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 4 << 5);
279 m_selectionManager
->setCurrentItem(7);
280 QCOMPARE(m_selectionManager
->currentItem(), 7);
281 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7);
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);
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);
293 m_selectionManager
->setCurrentItem(6);
294 QCOMPARE(m_selectionManager
->currentItem(), 6);
295 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 8 << 9);
297 m_selectionManager
->setCurrentItem(10);
298 QCOMPARE(m_selectionManager
->currentItem(), 10);
299 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
301 m_selectionManager
->endAnchoredSelection();
302 QVERIFY(!m_selectionManager
->isAnchoredSelectionActive());
303 QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << 5 << 6 << 7 << 9 << 10);
306 QTEST_KDEMAIN(KItemListSelectionManagerTest
, NoGUI
)
308 #include "kitemlistselectionmanagertest.moc"