]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Adapt autotest to new expected "Space" key behaviour
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "kitemviews/kitemlistcontainer.h"
8 #include "kitemviews/kfileitemlistview.h"
9 #include "kitemviews/kfileitemmodel.h"
10 #include "kitemviews/kitemlistcontroller.h"
11 #include "kitemviews/kitemlistselectionmanager.h"
12 #include "kitemviews/private/kitemlistviewlayouter.h"
13 #include "testdir.h"
14
15 #include <QTest>
16 #include <QGraphicsSceneMouseEvent>
17 #include <QSignalSpy>
18 #include <QProxyStyle>
19 #include <QStandardPaths>
20
21 /**
22 * \class KItemListControllerTestStyle is a proxy style for testing the
23 * KItemListController with different style hint options, e.g. single/double
24 * click activation.
25 */
26 class KItemListControllerTestStyle : public QProxyStyle
27 {
28 Q_OBJECT
29 public:
30 KItemListControllerTestStyle(QStyle* style) :
31 QProxyStyle(style),
32 m_activateItemOnSingleClick((bool)style->styleHint(SH_ItemView_ActivateItemOnSingleClick))
33 {
34 }
35
36 void setActivateItemOnSingleClick(bool activateItemOnSingleClick)
37 {
38 m_activateItemOnSingleClick = activateItemOnSingleClick;
39 }
40
41 bool activateItemOnSingleClick() const
42 {
43 return m_activateItemOnSingleClick;
44 }
45
46 int styleHint(StyleHint hint,
47 const QStyleOption* option = nullptr,
48 const QWidget* widget = nullptr,
49 QStyleHintReturn* returnData = nullptr) const override
50 {
51 switch (hint) {
52 case QStyle::SH_ItemView_ActivateItemOnSingleClick:
53 return (int)activateItemOnSingleClick();
54 default:
55 return QProxyStyle::styleHint(hint, option, widget, returnData);
56 }
57 }
58
59 private:
60 bool m_activateItemOnSingleClick;
61 };
62
63 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout)
64 Q_DECLARE_METATYPE(Qt::Orientation)
65 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior)
66 Q_DECLARE_METATYPE(KItemSet)
67
68 class KItemListControllerTest : public QObject
69 {
70 Q_OBJECT
71
72 private Q_SLOTS:
73 void initTestCase();
74 void cleanupTestCase();
75
76 void init();
77 void cleanup();
78
79 void testKeyboardNavigation_data();
80 void testKeyboardNavigation();
81 void testMouseClickActivation();
82
83 private:
84 /**
85 * Make sure that the number of columns in the view is equal to \a count
86 * by changing the geometry of the container.
87 */
88 void adjustGeometryForColumnCount(int count);
89
90 private:
91 KFileItemListView* m_view;
92 KItemListController* m_controller;
93 KItemListSelectionManager* m_selectionManager;
94 KFileItemModel* m_model;
95 TestDir* m_testDir;
96 KItemListContainer* m_container;
97 KItemListControllerTestStyle* m_testStyle;
98 };
99
100 /**
101 * This function initializes the member objects, creates the temporary files, and
102 * shows the view on the screen on startup. This could also be done before every
103 * single test, but this would make the time needed to run the test much larger.
104 */
105 void KItemListControllerTest::initTestCase()
106 {
107 QStandardPaths::setTestModeEnabled(true);
108 qRegisterMetaType<KItemSet>("KItemSet");
109
110 m_testDir = new TestDir();
111 m_model = new KFileItemModel();
112 m_view = new KFileItemListView();
113 m_controller = new KItemListController(m_model, m_view, this);
114 m_container = new KItemListContainer(m_controller);
115 m_controller = m_container->controller();
116 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
117 m_selectionManager = m_controller->selectionManager();
118 m_testStyle = new KItemListControllerTestStyle(m_view->style());
119 m_view->setStyle(m_testStyle);
120
121 QStringList files;
122 files
123 << "a1" << "a2" << "a3"
124 << "b1"
125 << "c1" << "c2" << "c3" << "c4" << "c5"
126 << "d1" << "d2" << "d3" << "d4"
127 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
128
129 m_testDir->createFiles(files);
130 m_model->loadDirectory(m_testDir->url());
131 QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
132 QVERIFY(spyDirectoryLoadingCompleted.wait());
133
134 m_container->show();
135 QVERIFY(QTest::qWaitForWindowExposed(m_container));
136 }
137
138 void KItemListControllerTest::cleanupTestCase()
139 {
140 delete m_container;
141 m_container = nullptr;
142
143 delete m_testDir;
144 m_testDir = nullptr;
145 }
146
147 /** Before each test, the current item, selection, and item size are reset to the defaults. */
148 void KItemListControllerTest::init()
149 {
150 m_selectionManager->setCurrentItem(0);
151 QCOMPARE(m_selectionManager->currentItem(), 0);
152
153 m_selectionManager->clearSelection();
154 QVERIFY(!m_selectionManager->hasSelection());
155
156 const QSizeF itemSize(50, 50);
157 m_view->setItemSize(itemSize);
158 QCOMPARE(m_view->itemSize(), itemSize);
159 }
160
161 void KItemListControllerTest::cleanup()
162 {
163 }
164
165 /**
166 * \class KeyPress is a small helper struct that represents a key press event,
167 * including the key and the keyboard modifiers.
168 */
169 struct KeyPress {
170
171 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier) :
172 m_key(key),
173 m_modifier(modifier)
174 {}
175
176 Qt::Key m_key;
177 Qt::KeyboardModifiers m_modifier;
178 };
179
180 /**
181 * \class ViewState is a small helper struct that represents a certain state
182 * of the view, including the current item, the selected items in MultiSelection
183 * mode (in the other modes, the selection is either empty or equal to the
184 * current item), and the information whether items were activated by the last
185 * key press.
186 */
187 struct ViewState {
188
189 ViewState(int current, const KItemSet &selection, bool activated = false) :
190 m_current(current),
191 m_selection(selection),
192 m_activated(activated)
193 {}
194
195 int m_current;
196 KItemSet m_selection;
197 bool m_activated;
198 };
199
200 // We have to define a typedef for the pair in order to make the test compile.
201 typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
202 Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
203
204 /**
205 * This function provides the data for the actual test function
206 * KItemListControllerTest::testKeyboardNavigation().
207 * It tests all possible combinations of view layouts, selection behaviors,
208 * and enabled/disabled groupings for different column counts, and
209 * provides a list of key presses and the states that the view should be in
210 * after the key press event.
211 */
212 void KItemListControllerTest::testKeyboardNavigation_data()
213 {
214 QTest::addColumn<KFileItemListView::ItemLayout>("layout");
215 QTest::addColumn<Qt::Orientation>("scrollOrientation");
216 QTest::addColumn<int>("columnCount");
217 QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior");
218 QTest::addColumn<bool>("groupingEnabled");
219 QTest::addColumn<QList<QPair<KeyPress, ViewState> > >("testList");
220
221 QList<KFileItemListView::ItemLayout> layoutList;
222 QHash<KFileItemListView::ItemLayout, QString> layoutNames;
223 layoutList.append(KFileItemListView::IconsLayout);
224 layoutNames[KFileItemListView::IconsLayout] = "Icons";
225 layoutList.append(KFileItemListView::CompactLayout);
226 layoutNames[KFileItemListView::CompactLayout] = "Compact";
227 layoutList.append(KFileItemListView::DetailsLayout);
228 layoutNames[KFileItemListView::DetailsLayout] = "Details";
229
230 QList<KItemListController::SelectionBehavior> selectionBehaviorList;
231 QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
232 selectionBehaviorList.append(KItemListController::NoSelection);
233 selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
234 selectionBehaviorList.append(KItemListController::SingleSelection);
235 selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
236 selectionBehaviorList.append(KItemListController::MultiSelection);
237 selectionBehaviorNames[KItemListController::MultiSelection] = "MultiSelection";
238
239 QList<bool> groupingEnabledList;
240 QHash<bool, QString> groupingEnabledNames;
241 groupingEnabledList.append(false);
242 groupingEnabledNames[false] = "ungrouped";
243 groupingEnabledList.append(true);
244 groupingEnabledNames[true] = "grouping enabled";
245
246 for (const KFileItemListView::ItemLayout& layout : layoutList) {
247 // The following settings depend on the layout.
248 // Note that 'columns' are actually 'rows' in
249 // Compact layout.
250 Qt::Orientation scrollOrientation;
251 QList<int> columnCountList;
252 Qt::Key nextItemKey;
253 Qt::Key previousItemKey;
254 Qt::Key nextRowKey;
255 Qt::Key previousRowKey;
256
257 switch (layout) {
258 case KFileItemListView::IconsLayout:
259 scrollOrientation = Qt::Vertical;
260 columnCountList << 1 << 3 << 5;
261 nextItemKey = Qt::Key_Right;
262 previousItemKey = Qt::Key_Left;
263 nextRowKey = Qt::Key_Down;
264 previousRowKey = Qt::Key_Up;
265 break;
266 case KFileItemListView::CompactLayout:
267 scrollOrientation = Qt::Horizontal;
268 columnCountList << 1 << 3 << 5;
269 nextItemKey = Qt::Key_Down;
270 previousItemKey = Qt::Key_Up;
271 nextRowKey = Qt::Key_Right;
272 previousRowKey = Qt::Key_Left;
273 break;
274 case KFileItemListView::DetailsLayout:
275 scrollOrientation = Qt::Vertical;
276 columnCountList << 1;
277 nextItemKey = Qt::Key_Down;
278 previousItemKey = Qt::Key_Up;
279 nextRowKey = Qt::Key_Down;
280 previousRowKey = Qt::Key_Up;
281 break;
282 }
283
284 for (int columnCount : qAsConst(columnCountList)) {
285 for (const KItemListController::SelectionBehavior& selectionBehavior : qAsConst(selectionBehaviorList)) {
286 for (bool groupingEnabled : qAsConst(groupingEnabledList)) {
287 QList<QPair<KeyPress, ViewState> > testList;
288
289 // First, key presses which should have the same effect
290 // for any layout and any number of columns.
291 testList
292 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
293 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
294 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
295 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
296 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
297 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
298 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
299 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
300 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
301 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
302 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
303 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
304 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
305 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
306 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
307 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
308 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
309 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
310 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
311 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
312 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
313 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
314 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet())) // This used to select, but we are now using it to trigger either
315 // selection mode or "QuickLook". Ctrl+Space still works for selecting as expected.
316 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
317 << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
318 << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
319 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
320 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
321 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
322 << qMakePair(KeyPress(Qt::Key_E), ViewState(15, KItemSet() << 15))
323 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
324 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
325 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
326 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
327
328 // Next, we test combinations of key presses which only work for a
329 // particular number of columns and either enabled or disabled grouping.
330
331 // One column.
332 if (columnCount == 1) {
333 testList
334 << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
335 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
336 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
337 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
338 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
339 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
340 }
341
342 // Multiple columns: we test both 3 and 5 columns with grouping
343 // enabled or disabled. For each case, the layout of the items
344 // in the view is shown (both using file names and indices) to
345 // make it easier to understand what the tests do.
346
347 if (columnCount == 3 && !groupingEnabled) {
348 // 3 columns, no grouping:
349 //
350 // a1 a2 a3 | 0 1 2
351 // b1 c1 c2 | 3 4 5
352 // c3 c4 c5 | 6 7 8
353 // d1 d2 d3 | 9 10 11
354 // d4 e1 e2 | 12 13 14
355 // e3 e4 e5 | 15 16 17
356 // e6 e7 | 18 19
357 testList
358 << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
359 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
360 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
361 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
362 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
363 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
364 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
365 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
366 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
367 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
368 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
369 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
370 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
371 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
372 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
373 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
374 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
375 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
376 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
377 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
378 }
379
380 if (columnCount == 5 && !groupingEnabled) {
381 // 5 columns, no grouping:
382 //
383 // a1 a2 a3 b1 c1 | 0 1 2 3 4
384 // c2 c3 c4 c5 d1 | 5 6 7 8 9
385 // d2 d3 d4 e1 e2 | 10 11 12 13 14
386 // e3 e4 e5 e6 e7 | 15 16 17 18 19
387 testList
388 << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
389 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
390 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
391 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
392 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
393 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
394 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
395 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
396 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
397 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
398 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
399 }
400
401 if (columnCount == 3 && groupingEnabled) {
402 // 3 columns, with grouping:
403 //
404 // a1 a2 a3 | 0 1 2
405 // b1 | 3
406 // c1 c2 c3 | 4 5 6
407 // c4 c5 | 7 8
408 // d1 d2 d3 | 9 10 11
409 // d4 | 12
410 // e1 e2 e3 | 13 14 15
411 // e4 e5 e6 | 16 17 18
412 // e7 | 19
413 testList
414 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
415 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
416 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
417 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
418 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
419 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
420 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
421 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
422 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
423 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
424 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
425 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
426 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
427 }
428
429 if (columnCount == 5 && groupingEnabled) {
430 // 5 columns, with grouping:
431 //
432 // a1 a2 a3 | 0 1 2
433 // b1 | 3
434 // c1 c2 c3 c4 c5 | 4 5 6 7 8
435 // d1 d2 d3 d4 | 9 10 11 12
436 // e1 e2 e3 e4 e5 | 13 14 15 16 17
437 // e6 e7 | 18 19
438 testList
439 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
440 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
441 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
442 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
443 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
444 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
445 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
446 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
447 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
448 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
449 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
450 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
451 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
452 }
453
454 const QString testName =
455 layoutNames[layout] + ", " +
456 QString("%1 columns, ").arg(columnCount) +
457 selectionBehaviorNames[selectionBehavior] + ", " +
458 groupingEnabledNames[groupingEnabled];
459
460 const QByteArray testNameAscii = testName.toLatin1();
461
462 QTest::newRow(testNameAscii.data())
463 << layout
464 << scrollOrientation
465 << columnCount
466 << selectionBehavior
467 << groupingEnabled
468 << testList;
469 }
470 }
471 }
472 }
473 }
474
475 /**
476 * This function sets the view's properties according to the data provided by
477 * KItemListControllerTest::testKeyboardNavigation_data().
478 *
479 * The list \a testList contains pairs of key presses, which are sent to the
480 * container, and expected view states, which are verified then.
481 */
482 void KItemListControllerTest::testKeyboardNavigation()
483 {
484 QFETCH(KFileItemListView::ItemLayout, layout);
485 QFETCH(Qt::Orientation, scrollOrientation);
486 QFETCH(int, columnCount);
487 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
488 QFETCH(bool, groupingEnabled);
489 QFETCH(QList<keyPressViewStatePair>, testList);
490
491 m_view->setItemLayout(layout);
492 QCOMPARE(m_view->itemLayout(), layout);
493
494 m_view->setScrollOrientation(scrollOrientation);
495 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
496
497 m_controller->setSelectionBehavior(selectionBehavior);
498 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
499
500 m_model->setGroupedSorting(groupingEnabled);
501 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
502
503 adjustGeometryForColumnCount(columnCount);
504 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
505
506 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
507 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
508
509 while (!testList.isEmpty()) {
510 const QPair<KeyPress, ViewState> test = testList.takeFirst();
511 const Qt::Key key = test.first.m_key;
512 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
513 const int current = test.second.m_current;
514 const KItemSet selection = test.second.m_selection;
515 const bool activated = test.second.m_activated;
516
517 QTest::keyClick(m_container, key, modifier);
518
519 QCOMPARE(m_selectionManager->currentItem(), current);
520 switch (selectionBehavior) {
521 case KItemListController::NoSelection: QVERIFY(m_selectionManager->selectedItems().isEmpty()); break;
522 case KItemListController::SingleSelection: QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current); break;
523 case KItemListController::MultiSelection: QCOMPARE(m_selectionManager->selectedItems(), selection); break;
524 }
525
526 if (activated) {
527 switch (selectionBehavior) {
528 case KItemListController::MultiSelection:
529 if (!selection.isEmpty()) {
530 // The selected items should be activated.
531 if (selection.count() == 1) {
532 QVERIFY(!spySingleItemActivated.isEmpty());
533 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
534 QVERIFY(spyMultipleItemsActivated.isEmpty());
535 } else {
536 QVERIFY(spySingleItemActivated.isEmpty());
537 QVERIFY(!spyMultipleItemsActivated.isEmpty());
538 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
539 }
540 break;
541 }
542 // No items are selected. Therefore, the current item should be activated.
543 // This is handled by falling through to the NoSelection/SingleSelection case.
544 Q_FALLTHROUGH();
545 case KItemListController::NoSelection:
546 case KItemListController::SingleSelection:
547 // In NoSelection and SingleSelection mode, the current item should be activated.
548 QVERIFY(!spySingleItemActivated.isEmpty());
549 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
550 QVERIFY(spyMultipleItemsActivated.isEmpty());
551 break;
552 }
553 }
554 }
555 }
556
557 void KItemListControllerTest::testMouseClickActivation()
558 {
559 m_view->setItemLayout(KFileItemListView::IconsLayout);
560
561 // Make sure that we have a large window, such that
562 // the items are visible and clickable.
563 adjustGeometryForColumnCount(5);
564
565 // Make sure that the first item is visible in the view.
566 m_view->setScrollOffset(0);
567 QCOMPARE(m_view->firstVisibleIndex(), 0);
568
569 const QPointF pos = m_view->itemContextRect(0).center();
570
571 // Save the "single click" setting.
572 const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
573
574 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
575 mousePressEvent.setPos(pos);
576 mousePressEvent.setButton(Qt::LeftButton);
577 mousePressEvent.setButtons(Qt::LeftButton);
578
579 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
580 mouseReleaseEvent.setPos(pos);
581 mouseReleaseEvent.setButton(Qt::LeftButton);
582 mouseReleaseEvent.setButtons(Qt::NoButton);
583
584 QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
585
586 // Default setting: single click activation.
587 m_testStyle->setActivateItemOnSingleClick(true);
588 m_view->event(&mousePressEvent);
589 m_view->event(&mouseReleaseEvent);
590 QCOMPARE(spyItemActivated.count(), 1);
591 spyItemActivated.clear();
592
593 // Set the global setting to "double click activation".
594 m_testStyle->setActivateItemOnSingleClick(false);
595 m_view->event(&mousePressEvent);
596 m_view->event(&mouseReleaseEvent);
597 QCOMPARE(spyItemActivated.count(), 0);
598 spyItemActivated.clear();
599
600 // Enforce single click activation in the controller.
601 m_controller->setSingleClickActivationEnforced(true);
602 m_view->event(&mousePressEvent);
603 m_view->event(&mouseReleaseEvent);
604 QCOMPARE(spyItemActivated.count(), 1);
605 spyItemActivated.clear();
606
607 // Do not enforce single click activation in the controller.
608 m_controller->setSingleClickActivationEnforced(false);
609 m_view->event(&mousePressEvent);
610 m_view->event(&mouseReleaseEvent);
611 QCOMPARE(spyItemActivated.count(), 0);
612 spyItemActivated.clear();
613
614 // Set the global setting back to "single click activation".
615 m_testStyle->setActivateItemOnSingleClick(true);
616 m_view->event(&mousePressEvent);
617 m_view->event(&mouseReleaseEvent);
618 QCOMPARE(spyItemActivated.count(), 1);
619 spyItemActivated.clear();
620
621 // Enforce single click activation in the controller.
622 m_controller->setSingleClickActivationEnforced(true);
623 m_view->event(&mousePressEvent);
624 m_view->event(&mouseReleaseEvent);
625 QCOMPARE(spyItemActivated.count(), 1);
626 spyItemActivated.clear();
627
628 // Restore previous settings.
629 m_controller->setSingleClickActivationEnforced(true);
630 m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
631 }
632
633 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
634 {
635 const QSize size = m_view->itemSize().toSize();
636
637 QRect rect = m_container->geometry();
638 rect.setSize(size * count);
639 m_container->setGeometry(rect);
640
641 // Increase the size of the container until the correct column count is reached.
642 while (m_view->m_layouter->m_columnCount < count) {
643 rect = m_container->geometry();
644 rect.setSize(rect.size() + size);
645 m_container->setGeometry(rect);
646 }
647 }
648
649 QTEST_MAIN(KItemListControllerTest)
650
651 #include "kitemlistcontrollertest.moc"