]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Test which items are activated when pressing Enter or Return
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.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 #include <qtestmouse.h>
22 #include <qtestkeyboard.h>
23
24 #include <KDirLister>
25 #include "kitemviews/kitemlistcontainer.h"
26 #include "kitemviews/kfileitemlistview.h"
27 #include "kitemviews/kfileitemmodel.h"
28 #include "kitemviews/kitemlistcontroller.h"
29 #include "kitemviews/kitemlistselectionmanager.h"
30 #include "kitemviews/kitemlistviewlayouter_p.h"
31 #include "testdir.h"
32
33 namespace {
34 const int DefaultTimeout = 2000;
35 };
36
37 Q_DECLARE_METATYPE(KFileItemListView::Layout);
38 Q_DECLARE_METATYPE(Qt::Orientation);
39 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior);
40 Q_DECLARE_METATYPE(QSet<int>);
41
42 class KItemListControllerTest : public QObject
43 {
44 Q_OBJECT
45
46 private slots:
47 void initTestCase();
48 void cleanupTestCase();
49
50 void init();
51 void cleanup();
52
53 void testKeyboardNavigation_data();
54 void testKeyboardNavigation();
55
56 private:
57 /**
58 * Make sure that the number of columns in the view is equal to \a count
59 * by changing the geometry of the container.
60 */
61 void adjustGeometryForColumnCount(int count);
62
63 private:
64 KFileItemListView* m_view;
65 KItemListController* m_controller;
66 KItemListSelectionManager* m_selectionManager;
67 KFileItemModel* m_model;
68 KDirLister* m_dirLister;
69 TestDir* m_testDir;
70 KItemListContainer* m_container;
71 };
72
73 /**
74 * This function initializes the member objects, creates the temporary files, and
75 * shows the view on the screen on startup. This could also be done before every
76 * single test, but this would make the time needed to run the test much larger.
77 */
78 void KItemListControllerTest::initTestCase()
79 {
80 qRegisterMetaType<QSet<int> >("QSet<int>");
81
82 m_testDir = new TestDir();
83 m_dirLister = new KDirLister();
84 m_model = new KFileItemModel(m_dirLister);
85 m_container = new KItemListContainer();
86 m_controller = m_container->controller();
87 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
88 m_selectionManager = m_controller->selectionManager();
89
90 m_view = new KFileItemListView();
91 m_controller->setView(m_view);
92 m_controller->setModel(m_model);
93
94 QStringList files;
95 files
96 << "a1" << "a2" << "a3"
97 << "b1"
98 << "c1" << "c2" << "c3" << "c4" << "c5"
99 << "d1" << "d2" << "d3" << "d4"
100 << "e1" << "e2" << "e3" << "e4" << "e5" << "e6" << "e7";
101
102 m_testDir->createFiles(files);
103 m_dirLister->openUrl(m_testDir->url());
104 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(loadingCompleted()), DefaultTimeout));
105
106 m_container->show();
107 QTest::qWaitForWindowShown(m_container);
108 }
109
110 void KItemListControllerTest::cleanupTestCase()
111 {
112 delete m_view;
113 m_view = 0;
114
115 delete m_container;
116 m_container = 0;
117 m_controller = 0;
118
119 delete m_model;
120 m_model = 0;
121
122 delete m_dirLister;
123 m_dirLister = 0;
124
125 delete m_testDir;
126 m_testDir = 0;
127 }
128
129 /** Before each test, the current item, selection, and item size are reset to the defaults. */
130 void KItemListControllerTest::init()
131 {
132 m_selectionManager->setCurrentItem(0);
133 QCOMPARE(m_selectionManager->currentItem(), 0);
134
135 m_selectionManager->clearSelection();
136 QVERIFY(!m_selectionManager->hasSelection());
137
138 const QSizeF itemSize(50, 50);
139 m_view->setItemSize(itemSize);
140 QCOMPARE(m_view->itemSize(), itemSize);
141 }
142
143 void KItemListControllerTest::cleanup()
144 {
145 }
146
147 /**
148 * \class KeyPress is a small helper struct that represents a key press event,
149 * including the key and the keyboard modifiers.
150 */
151 struct KeyPress {
152
153 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier) :
154 m_key(key),
155 m_modifier(modifier)
156 {}
157
158 Qt::Key m_key;
159 Qt::KeyboardModifiers m_modifier;
160 };
161
162 /**
163 * \class ViewState is a small helper struct that represents a certain state
164 * of the view, including the current item, the selected items in MultiSelection
165 * mode (in the other modes, the selection is either empty or equal to the
166 * current item), and the information whether items were activated by the last
167 * key press.
168 */
169 struct ViewState {
170
171 ViewState(int current, const QSet<int> selection, bool activated = false) :
172 m_current(current),
173 m_selection(selection),
174 m_activated(activated)
175 {}
176
177 int m_current;
178 QSet<int> m_selection;
179 bool m_activated;
180 };
181
182 // We have to define a typedef for the pair in order to make the test compile.
183 typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
184 Q_DECLARE_METATYPE(QList<keyPressViewStatePair>);
185
186 /**
187 * This function provides the data for the actual test function
188 * KItemListControllerTest::testKeyboardNavigation().
189 * It tests all possible combinations of view layouts, selection behaviors,
190 * and enabled/disabled groupings for different column counts, and
191 * provides a list of key presses and the states that the view should be in
192 * after the key press event.
193 */
194 void KItemListControllerTest::testKeyboardNavigation_data()
195 {
196 QTest::addColumn<KFileItemListView::Layout>("layout");
197 QTest::addColumn<Qt::Orientation>("scrollOrientation");
198 QTest::addColumn<int>("columnCount");
199 QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior");
200 QTest::addColumn<bool>("groupingEnabled");
201 QTest::addColumn<QList<QPair<KeyPress, ViewState> > >("testList");
202
203 QList<KFileItemListView::Layout> layoutList;
204 QHash<KFileItemListView::Layout, QString> layoutNames;
205 layoutList.append(KFileItemListView::IconsLayout);
206 layoutNames[KFileItemListView::IconsLayout] = "Icons";
207 layoutList.append(KFileItemListView::CompactLayout);
208 layoutNames[KFileItemListView::CompactLayout] = "Compact";
209 layoutList.append(KFileItemListView::DetailsLayout);
210 layoutNames[KFileItemListView::DetailsLayout] = "Details";
211
212 QList<KItemListController::SelectionBehavior> selectionBehaviorList;
213 QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
214 selectionBehaviorList.append(KItemListController::NoSelection);
215 selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
216 selectionBehaviorList.append(KItemListController::SingleSelection);
217 selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
218 selectionBehaviorList.append(KItemListController::MultiSelection);
219 selectionBehaviorNames[KItemListController::MultiSelection] = "MultiSelection";
220
221 QList<bool> groupingEnabledList;
222 QHash<bool, QString> groupingEnabledNames;
223 groupingEnabledList.append(false);
224 groupingEnabledNames[false] = "ungrouped";
225 groupingEnabledList.append(true);
226 groupingEnabledNames[true] = "grouping enabled";
227
228 foreach (KFileItemListView::Layout layout, layoutList) {
229 // The following settings depend on the layout.
230 // Note that 'columns' are actually 'rows' in
231 // Compact layout.
232 Qt::Orientation scrollOrientation;
233 QList<int> columnCountList;
234 Qt::Key nextItemKey;
235 Qt::Key previousItemKey;
236 Qt::Key nextRowKey;
237 Qt::Key previousRowKey;
238
239 switch (layout) {
240 case KFileItemListView::IconsLayout:
241 scrollOrientation = Qt::Vertical;
242 columnCountList << 1 << 3 << 5;
243 nextItemKey = Qt::Key_Right;
244 previousItemKey = Qt::Key_Left;
245 nextRowKey = Qt::Key_Down;
246 previousRowKey = Qt::Key_Up;
247 break;
248 case KFileItemListView::CompactLayout:
249 scrollOrientation = Qt::Horizontal;
250 columnCountList << 1 << 3 << 5;
251 nextItemKey = Qt::Key_Down;
252 previousItemKey = Qt::Key_Up;
253 nextRowKey = Qt::Key_Right;
254 previousRowKey = Qt::Key_Left;
255 break;
256 case KFileItemListView::DetailsLayout:
257 scrollOrientation = Qt::Vertical;
258 columnCountList << 1;
259 nextItemKey = Qt::Key_Down;
260 previousItemKey = Qt::Key_Up;
261 nextRowKey = Qt::Key_Down;
262 previousRowKey = Qt::Key_Up;
263 break;
264 }
265
266 foreach (int columnCount, columnCountList) {
267 foreach (KItemListController::SelectionBehavior selectionBehavior, selectionBehaviorList) {
268 foreach (bool groupingEnabled, groupingEnabledList) {
269 QList<QPair<KeyPress, ViewState> > testList;
270
271 // First, key presses which should have the same effect
272 // for any layout and any number of columns.
273 testList
274 << qMakePair(KeyPress(nextItemKey), ViewState(1, QSet<int>() << 1))
275 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, QSet<int>() << 1, true))
276 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, QSet<int>() << 1, true))
277 << qMakePair(KeyPress(nextItemKey), ViewState(2, QSet<int>() << 2))
278 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, QSet<int>() << 2 << 3))
279 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, QSet<int>() << 2 << 3, true))
280 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, QSet<int>() << 2))
281 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, QSet<int>() << 2 << 3))
282 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, QSet<int>() << 2 << 3))
283 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, QSet<int>() << 2 << 3, true))
284 << qMakePair(KeyPress(previousItemKey), ViewState(3, QSet<int>() << 3))
285 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, QSet<int>() << 0 << 1 << 2 << 3))
286 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, QSet<int>() << 0 << 1 << 2 << 3))
287 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, QSet<int>() << 0 << 2 << 3))
288 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, QSet<int>() << 0 << 1 << 2 << 3))
289 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, QSet<int>() << 19))
290 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, QSet<int>() << 18 << 19))
291 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0))
292 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, QSet<int>()))
293 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, QSet<int>(), true))
294 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, QSet<int>() << 0));
295
296 // Next, we test combinations of key presses which only work for a
297 // particular number of columns and either enabled or disabled grouping.
298
299 // One column.
300 if (columnCount == 1) {
301 testList
302 << qMakePair(KeyPress(nextRowKey), ViewState(1, QSet<int>() << 1))
303 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, QSet<int>() << 1 << 2))
304 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, QSet<int>() << 1 << 2))
305 << qMakePair(KeyPress(previousRowKey), ViewState(2, QSet<int>() << 2))
306 << qMakePair(KeyPress(previousItemKey), ViewState(1, QSet<int>() << 1))
307 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0));
308 }
309
310 // Multiple columns: we test both 3 and 5 columns with grouping
311 // enabled or disabled. For each case, the layout of the items
312 // in the view is shown (both using file names and indices) to
313 // make it easier to understand what the tests do.
314
315 if (columnCount == 3 && !groupingEnabled) {
316 // 3 columns, no grouping:
317 //
318 // a1 a2 a3 | 0 1 2
319 // b1 c1 c2 | 3 4 5
320 // c3 c4 c5 | 6 7 8
321 // d1 d2 d3 | 9 10 11
322 // d4 e1 e2 | 12 13 14
323 // e3 e4 e5 | 15 16 17
324 // e6 e7 | 18 19
325 testList
326 << qMakePair(KeyPress(nextRowKey), ViewState(3, QSet<int>() << 3))
327 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, QSet<int>() << 3))
328 << qMakePair(KeyPress(nextRowKey), ViewState(7, QSet<int>() << 7))
329 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, QSet<int>() << 7 << 8))
330 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, QSet<int>() << 7 << 8 << 9))
331 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, QSet<int>() << 7 << 8))
332 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, QSet<int>() << 7))
333 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, QSet<int>() << 6 << 7))
334 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, QSet<int>() << 5 << 6 << 7))
335 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, QSet<int>() << 6 << 7))
336 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, QSet<int>() << 7))
337 << qMakePair(KeyPress(nextRowKey), ViewState(10, QSet<int>() << 10))
338 << qMakePair(KeyPress(nextItemKey), ViewState(11, QSet<int>() << 11))
339 << qMakePair(KeyPress(nextRowKey), ViewState(14, QSet<int>() << 14))
340 << qMakePair(KeyPress(nextRowKey), ViewState(17, QSet<int>() << 17))
341 << qMakePair(KeyPress(nextRowKey), ViewState(19, QSet<int>() << 19))
342 << qMakePair(KeyPress(previousRowKey), ViewState(17, QSet<int>() << 17))
343 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, QSet<int>() << 19))
344 << qMakePair(KeyPress(previousRowKey), ViewState(16, QSet<int>() << 16))
345 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0));
346 }
347
348 if (columnCount == 5 && !groupingEnabled) {
349 // 5 columns, no grouping:
350 //
351 // a1 a2 a3 b1 c1 | 0 1 2 3 4
352 // c2 c3 c4 c5 d1 | 5 6 7 8 9
353 // d2 d3 d4 e1 e2 | 10 11 12 13 14
354 // e3 e4 e5 e6 e7 | 15 16 17 18 19
355 testList
356 << qMakePair(KeyPress(nextRowKey), ViewState(5, QSet<int>() << 5))
357 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, QSet<int>() << 5))
358 << qMakePair(KeyPress(nextRowKey), ViewState(11, QSet<int>() << 11))
359 << qMakePair(KeyPress(nextItemKey), ViewState(12, QSet<int>() << 12))
360 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, QSet<int>() << 12 << 13 << 14 << 15 << 16 << 17))
361 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, QSet<int>() << 12))
362 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, QSet<int>() << 7 << 8 << 9 << 10 << 11 << 12))
363 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, QSet<int>() << 12))
364 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, QSet<int>() << 12))
365 << qMakePair(KeyPress(previousRowKey), ViewState(14, QSet<int>() << 14))
366 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0));
367 }
368
369 if (columnCount == 3 && groupingEnabled) {
370 // 3 columns, with grouping:
371 //
372 // a1 a2 a3 | 0 1 2
373 // b1 | 3
374 // c1 c2 c3 | 4 5 6
375 // c4 c5 | 7 8
376 // d1 d2 d3 | 9 10 11
377 // d4 | 12
378 // e1 e2 e3 | 13 14 15
379 // e4 e5 e6 | 16 17 18
380 // e7 | 19
381 testList
382 << qMakePair(KeyPress(nextItemKey), ViewState(1, QSet<int>() << 1))
383 << qMakePair(KeyPress(nextItemKey), ViewState(2, QSet<int>() << 2))
384 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, QSet<int>() << 2 << 3))
385 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, QSet<int>() << 2 << 3 << 4 << 5 << 6))
386 << qMakePair(KeyPress(nextRowKey), ViewState(8, QSet<int>() << 8))
387 << qMakePair(KeyPress(nextRowKey), ViewState(11, QSet<int>() << 11))
388 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, QSet<int>() << 11))
389 << qMakePair(KeyPress(nextRowKey), ViewState(13, QSet<int>() << 13))
390 << qMakePair(KeyPress(nextRowKey), ViewState(16, QSet<int>() << 16))
391 << qMakePair(KeyPress(nextItemKey), ViewState(17, QSet<int>() << 17))
392 << qMakePair(KeyPress(nextRowKey), ViewState(19, QSet<int>() << 19))
393 << qMakePair(KeyPress(previousRowKey), ViewState(17, QSet<int>() << 17))
394 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0));
395 }
396
397 if (columnCount == 5 && groupingEnabled) {
398 // 5 columns, with grouping:
399 //
400 // a1 a2 a3 | 0 1 2
401 // b1 | 3
402 // c1 c2 c3 c4 c5 | 4 5 6 7 8
403 // d1 d2 d3 d4 | 9 10 11 12
404 // e1 e2 e3 e4 e5 | 13 14 15 16 17
405 // e6 e7 | 18 19
406 testList
407 << qMakePair(KeyPress(nextItemKey), ViewState(1, QSet<int>() << 1))
408 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, QSet<int>() << 1 << 2 << 3))
409 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, QSet<int>() << 1 << 2 << 3 << 4 << 5))
410 << qMakePair(KeyPress(nextItemKey), ViewState(6, QSet<int>() << 6))
411 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, QSet<int>() << 6))
412 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, QSet<int>() << 6))
413 << qMakePair(KeyPress(nextRowKey), ViewState(12, QSet<int>() << 12))
414 << qMakePair(KeyPress(nextRowKey), ViewState(17, QSet<int>() << 17))
415 << qMakePair(KeyPress(nextRowKey), ViewState(19, QSet<int>() << 19))
416 << qMakePair(KeyPress(previousRowKey), ViewState(17, QSet<int>() << 17))
417 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, QSet<int>() << 17 << 18 << 19))
418 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, QSet<int>() << 14 << 15 << 16 << 17))
419 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, QSet<int>() << 0));
420 }
421
422 const QString testName =
423 layoutNames[layout] + ", " +
424 QString("%1 columns, ").arg(columnCount) +
425 selectionBehaviorNames[selectionBehavior] + ", " +
426 groupingEnabledNames[groupingEnabled];
427
428 const QByteArray testNameAscii = testName.toAscii();
429
430 QTest::newRow(testNameAscii.data())
431 << layout
432 << scrollOrientation
433 << columnCount
434 << selectionBehavior
435 << groupingEnabled
436 << testList;
437 }
438 }
439 }
440 }
441 }
442
443 /**
444 * This function sets the view's properties according to the data provided by
445 * KItemListControllerTest::testKeyboardNavigation_data().
446 *
447 * The list \a testList contains pairs of key presses, which are sent to the
448 * container, and expected view states, which are verified then.
449 */
450 void KItemListControllerTest::testKeyboardNavigation()
451 {
452 QFETCH(KFileItemListView::Layout, layout);
453 QFETCH(Qt::Orientation, scrollOrientation);
454 QFETCH(int, columnCount);
455 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
456 QFETCH(bool, groupingEnabled);
457 QFETCH(QList<keyPressViewStatePair>, testList);
458
459 m_view->setItemLayout(layout);
460 QCOMPARE(m_view->itemLayout(), layout);
461
462 m_view->setScrollOrientation(scrollOrientation);
463 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
464
465 m_controller->setSelectionBehavior(selectionBehavior);
466 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
467
468 m_model->setGroupedSorting(groupingEnabled);
469 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
470
471 adjustGeometryForColumnCount(columnCount);
472 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
473
474 QSignalSpy spySingleItemActivated(m_controller, SIGNAL(itemActivated(int)));
475 QSignalSpy spyMultipleItemsActivated(m_controller, SIGNAL(itemsActivated(QSet<int>)));
476
477 while (!testList.isEmpty()) {
478 const QPair<KeyPress, ViewState> test = testList.takeFirst();
479 const Qt::Key key = test.first.m_key;
480 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
481 const int current = test.second.m_current;
482 const QSet<int> selection = test.second.m_selection;
483 const bool activated = test.second.m_activated;
484
485 QTest::keyClick(m_container, key, modifier);
486
487 QCOMPARE(m_selectionManager->currentItem(), current);
488 switch (selectionBehavior) {
489 case KItemListController::NoSelection: QVERIFY(m_selectionManager->selectedItems().isEmpty()); break;
490 case KItemListController::SingleSelection: QCOMPARE(m_selectionManager->selectedItems(), QSet<int>() << current); break;
491 case KItemListController::MultiSelection: QCOMPARE(m_selectionManager->selectedItems(), selection); break;
492 }
493
494 if (activated) {
495 switch (selectionBehavior) {
496 case KItemListController::MultiSelection:
497 if (!selection.isEmpty()) {
498 // The selected items should be activated.
499 if (selection.count() == 1) {
500 QVERIFY(!spySingleItemActivated.isEmpty());
501 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.toList().at(0));
502 QVERIFY(spyMultipleItemsActivated.isEmpty());
503 } else {
504 QVERIFY(spySingleItemActivated.isEmpty());
505 QVERIFY(!spyMultipleItemsActivated.isEmpty());
506 QCOMPARE(qvariant_cast<QSet<int> >(spyMultipleItemsActivated.takeFirst().at(0)), selection);
507 }
508 break;
509 }
510 // No items are selected. Therefore, the current item should be activated.
511 // This is handled by falling through to the NoSelection/SingleSelection case.
512 case KItemListController::NoSelection:
513 case KItemListController::SingleSelection:
514 // In NoSelection and SingleSelection mode, the current item should be activated.
515 QVERIFY(!spySingleItemActivated.isEmpty());
516 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
517 QVERIFY(spyMultipleItemsActivated.isEmpty());
518 break;
519 }
520 }
521 }
522 }
523
524 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
525 {
526 const QSize size = m_view->itemSize().toSize();
527
528 QRect rect = m_container->geometry();
529 rect.setSize(size * count);
530 m_container->setGeometry(rect);
531
532 // Increase the size of the container until the correct column count is reached.
533 while (m_view->m_layouter->m_columnCount < count) {
534 rect = m_container->geometry();
535 rect.setSize(rect.size() + size);
536 m_container->setGeometry(rect);
537 }
538 }
539
540 QTEST_KDEMAIN(KItemListControllerTest, GUI)
541
542 #include "kitemlistcontrollertest.moc"