]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistselectionmanagertest.cpp
Update the anchor item when items are added or removed
[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 // Set current item and check that the selection manager emits the currentChanged(int,int) signal correctly.
98 m_selectionManager->setCurrentItem(4);
99 QCOMPARE(m_selectionManager->currentItem(), 4);
100 QCOMPARE(spyCurrent.count(), 1);
101 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 4);
102 spyCurrent.takeFirst();
103
104 m_selectionManager->setCurrentItem(2);
105 QCOMPARE(m_selectionManager->currentItem(), 2);
106 QCOMPARE(spyCurrent.count(), 1);
107 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
108 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 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 // Inserting items should update current item and anchor item.
126 m_selectionManager->itemsInserted(KItemRangeList() <<
127 KItemRange(0, 1) <<
128 KItemRange(2, 2) <<
129 KItemRange(6, 3));
130
131 QCOMPARE(m_selectionManager->currentItem(), 5);
132 QCOMPARE(spyCurrent.count(), 1);
133 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 5);
134 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 2);
135 spyCurrent.takeFirst();
136
137 QCOMPARE(m_selectionManager->anchorItem(), 8);
138 QCOMPARE(spyAnchor.count(), 1);
139 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 8);
140 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 5);
141 spyAnchor.takeFirst();
142
143 // Removing items should update current item and anchor item.
144 m_selectionManager->itemsRemoved(KItemRangeList() <<
145 KItemRange(0, 2) <<
146 KItemRange(2, 1) <<
147 KItemRange(9, 2));
148
149 QCOMPARE(m_selectionManager->currentItem(), 2);
150 QCOMPARE(spyCurrent.count(), 1);
151 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(0)), 2);
152 QCOMPARE(qvariant_cast<int>(spyCurrent.at(0).at(1)), 5);
153 spyCurrent.takeFirst();
154
155 QCOMPARE(m_selectionManager->anchorItem(), 5);
156 QCOMPARE(spyAnchor.count(), 1);
157 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(0)), 5);
158 QCOMPARE(qvariant_cast<int>(spyAnchor.at(0).at(1)), 8);
159 spyAnchor.takeFirst();
160 }
161
162 void KItemListSelectionManagerTest::testSetSelected_data()
163 {
164 QTest::addColumn<int>("index");
165 QTest::addColumn<int>("count");
166 QTest::addColumn<int>("expectedSelectionCount");
167
168 QTest::newRow("Select all") << 0 << 100 << 100;
169 QTest::newRow("Sub selection 15 items") << 20 << 15 << 15;
170 QTest::newRow("Sub selection 1 item") << 20 << 1 << 1;
171 QTest::newRow("Too small index") << -1 << 100 << 0;
172 QTest::newRow("Too large index") << 100 << 100 << 0;
173 QTest::newRow("Too large count") << 0 << 100000 << 100;
174 QTest::newRow("Too small count") << 0 << 0 << 0;
175 }
176
177 void KItemListSelectionManagerTest::testSetSelected()
178 {
179 QFETCH(int, index);
180 QFETCH(int, count);
181 QFETCH(int, expectedSelectionCount);
182 m_selectionManager->setSelected(index, count);
183 QCOMPARE(m_selectionManager->selectedItems().count(), expectedSelectionCount);
184 }
185
186 void KItemListSelectionManagerTest::testItemsInserted()
187 {
188 // Select items 10 to 12
189 m_selectionManager->setSelected(10, 3);
190 QSet<int> selectedItems = m_selectionManager->selectedItems();
191 QVERIFY(selectedItems.contains(10));
192 QVERIFY(selectedItems.contains(11));
193 QVERIFY(selectedItems.contains(12));
194
195 // Insert items 0 to 4 -> selection must be 15 to 17
196 m_selectionManager->itemsInserted(KItemRangeList() << KItemRange(0, 5));
197 selectedItems = m_selectionManager->selectedItems();
198 QVERIFY(selectedItems.contains(15));
199 QVERIFY(selectedItems.contains(16));
200 QVERIFY(selectedItems.contains(17));
201
202 // Insert 3 items between the selections
203 m_selectionManager->itemsInserted(KItemRangeList() <<
204 KItemRange(15, 1) <<
205 KItemRange(16, 1) <<
206 KItemRange(17, 1));
207 selectedItems = m_selectionManager->selectedItems();
208 QVERIFY(selectedItems.contains(16));
209 QVERIFY(selectedItems.contains(18));
210 QVERIFY(selectedItems.contains(20));
211 }
212
213 void KItemListSelectionManagerTest::testItemsRemoved()
214 {
215 // Select items 10 to 15
216 m_selectionManager->setSelected(10, 6);
217 QSet<int> selectedItems = m_selectionManager->selectedItems();
218 for (int i = 10; i <= 15; ++i) {
219 QVERIFY(selectedItems.contains(i));
220 }
221
222 // Remove items 0 to 4 -> selection must be 5 to 10
223 m_selectionManager->itemsRemoved(KItemRangeList() << KItemRange(0, 5));
224 selectedItems = m_selectionManager->selectedItems();
225 for (int i = 5; i <= 10; ++i) {
226 QVERIFY(selectedItems.contains(i));
227 }
228
229 // Remove the items 6 , 8 and 10
230 m_selectionManager->itemsRemoved(KItemRangeList() <<
231 KItemRange(6, 1) <<
232 KItemRange(8, 1) <<
233 KItemRange(10, 1));
234 selectedItems = m_selectionManager->selectedItems();
235 QCOMPARE(selectedItems.count(), 3);
236 QVERIFY(selectedItems.contains(5));
237 QVERIFY(selectedItems.contains(6));
238 QVERIFY(selectedItems.contains(7));
239 }
240
241 QTEST_KDEMAIN(KItemListSelectionManagerTest, NoGUI)
242
243 #include "kitemlistselectionmanagertest.moc"