]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Clazy fix
[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/kitemlistcontroller.h"
8 #include "kitemviews/kfileitemlistview.h"
9 #include "kitemviews/kfileitemmodel.h"
10 #include "kitemviews/kitemlistcontainer.h"
11 #include "kitemviews/kitemlistselectionmanager.h"
12 #include "kitemviews/private/kitemlistviewlayouter.h"
13 #include "testdir.h"
14
15 #include <QGraphicsSceneMouseEvent>
16 #include <QProxyStyle>
17 #include <QSignalSpy>
18 #include <QStandardPaths>
19 #include <QTest>
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, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
47 {
48 switch (hint) {
49 case QStyle::SH_ItemView_ActivateItemOnSingleClick:
50 return (int)activateItemOnSingleClick();
51 default:
52 return QProxyStyle::styleHint(hint, option, widget, returnData);
53 }
54 }
55
56 private:
57 bool m_activateItemOnSingleClick;
58 };
59
60 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout)
61 Q_DECLARE_METATYPE(Qt::Orientation)
62 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior)
63 Q_DECLARE_METATYPE(KItemSet)
64
65 class KItemListControllerTest : public QObject
66 {
67 Q_OBJECT
68
69 private Q_SLOTS:
70 void initTestCase();
71 void cleanupTestCase();
72
73 void init();
74 void cleanup();
75
76 void testKeyboardNavigationMultiSelection_data();
77 void testKeyboardNavigationMultiSelection();
78 void testKeyboardNavigationSingleSelectionNoSelection_data();
79 void testKeyboardNavigationSingleSelectionNoSelection();
80 void testMouseClickActivation();
81
82 private:
83 /**
84 * Make sure that the number of columns in the view is equal to \a count
85 * by changing the geometry of the container.
86 */
87 void adjustGeometryForColumnCount(int count);
88
89 private:
90 KFileItemListView *m_view;
91 KItemListController *m_controller;
92 KItemListSelectionManager *m_selectionManager;
93 KFileItemModel *m_model;
94 TestDir *m_testDir;
95 KItemListContainer *m_container;
96 KItemListControllerTestStyle *m_testStyle;
97 };
98
99 /**
100 * This function initializes the member objects, creates the temporary files, and
101 * shows the view on the screen on startup. This could also be done before every
102 * single test, but this would make the time needed to run the test much larger.
103 */
104 void KItemListControllerTest::initTestCase()
105 {
106 QStandardPaths::setTestModeEnabled(true);
107 qRegisterMetaType<KItemSet>("KItemSet");
108
109 m_testDir = new TestDir();
110 m_model = new KFileItemModel();
111 m_view = new KFileItemListView();
112 m_controller = new KItemListController(m_model, m_view, this);
113 m_container = new KItemListContainer(m_controller);
114 #ifndef QT_NO_ACCESSIBILITY
115 m_view->setAccessibleParentsObject(m_container);
116 #endif
117 m_controller = m_container->controller();
118 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
119 m_selectionManager = m_controller->selectionManager();
120 m_testStyle = new KItemListControllerTestStyle(m_view->style());
121 m_view->setStyle(m_testStyle);
122
123 QStringList files;
124 files << "a1"
125 << "a2"
126 << "a3"
127 << "b1"
128 << "c1"
129 << "c2"
130 << "c3"
131 << "c4"
132 << "c5"
133 << "d1"
134 << "d2"
135 << "d3"
136 << "d4"
137 << "e"
138 << "e 2"
139 << "e 3"
140 << "e 4"
141 << "e 5"
142 << "e 6"
143 << "e 7";
144
145 m_testDir->createFiles(files);
146 m_model->loadDirectory(m_testDir->url());
147 QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
148 QVERIFY(spyDirectoryLoadingCompleted.wait());
149
150 m_container->show();
151 QVERIFY(QTest::qWaitForWindowExposed(m_container));
152 }
153
154 void KItemListControllerTest::cleanupTestCase()
155 {
156 delete m_container;
157 m_container = nullptr;
158
159 delete m_testDir;
160 m_testDir = nullptr;
161 }
162
163 /** Before each test, the current item, selection, and item size are reset to the defaults. */
164 void KItemListControllerTest::init()
165 {
166 m_selectionManager->setCurrentItem(0);
167 QCOMPARE(m_selectionManager->currentItem(), 0);
168
169 m_selectionManager->clearSelection();
170 QVERIFY(!m_selectionManager->hasSelection());
171
172 const QSizeF itemSize(50, 50);
173 m_view->setItemSize(itemSize);
174 QCOMPARE(m_view->itemSize(), itemSize);
175
176 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
177 QCOMPARE(m_controller->selectionBehavior(), KItemListController::MultiSelection);
178 }
179
180 void KItemListControllerTest::cleanup()
181 {
182 m_controller->setSelectionModeEnabled(false);
183 }
184
185 /**
186 * \class KeyPress is a small helper struct that represents a key press event,
187 * including the key and the keyboard modifiers.
188 */
189 struct KeyPress {
190 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier)
191 : m_key(key)
192 , m_modifier(modifier)
193 {
194 }
195
196 Qt::Key m_key;
197 Qt::KeyboardModifiers m_modifier;
198 };
199
200 /**
201 * \class ViewState is a small helper struct that represents a certain state
202 * of the view, including the current item, the selected items in MultiSelection
203 * mode (in the other modes, the selection is either empty or equal to the
204 * current item), and the information whether items were activated by the last
205 * key press.
206 */
207 struct ViewState {
208 ViewState(int current, const KItemSet &selection, bool activated = false)
209 : m_current(current)
210 , m_selection(selection)
211 , m_activated(activated)
212 {
213 }
214
215 int m_current;
216 KItemSet m_selection;
217 bool m_activated;
218 };
219
220 // We have to define a typedef for the pair in order to make the test compile.
221 typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
222 Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
223
224 /**
225 * This function tests all possible combinations of view layouts, layout direction,
226 * and enabled/disabled groupings for different column counts, and
227 * provides a list of key presses and the states that the view should be in
228 * after the key press event.
229 */
230 void KItemListControllerTest::testKeyboardNavigationMultiSelection_data()
231 {
232 QTest::addColumn<KFileItemListView::ItemLayout>("layout");
233 QTest::addColumn<Qt::Orientation>("scrollOrientation");
234 QTest::addColumn<int>("columnCount");
235 QTest::addColumn<bool>("groupingEnabled");
236 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
237 QTest::addColumn<bool>(
238 "selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
239 QTest::addColumn<QList<QPair<KeyPress, ViewState>>>("testList");
240
241 QList<KFileItemListView::ItemLayout> layoutList;
242 QHash<KFileItemListView::ItemLayout, QString> layoutNames;
243 layoutList.append(KFileItemListView::IconsLayout);
244 layoutNames[KFileItemListView::IconsLayout] = "Icons";
245 layoutList.append(KFileItemListView::CompactLayout);
246 layoutNames[KFileItemListView::CompactLayout] = "Compact";
247 layoutList.append(KFileItemListView::DetailsLayout);
248 layoutNames[KFileItemListView::DetailsLayout] = "Details";
249
250 QList<bool> groupingEnabledList;
251 QHash<bool, QString> groupingEnabledNames;
252 groupingEnabledList.append(false);
253 groupingEnabledNames[false] = "ungrouped";
254 groupingEnabledList.append(true);
255 groupingEnabledNames[true] = "grouping enabled";
256
257 QList<Qt::LayoutDirection> layoutDirectionList;
258 QHash<Qt::LayoutDirection, QString> layoutDirectionNames;
259 layoutDirectionList.append(Qt::LeftToRight);
260 layoutDirectionNames[Qt::LeftToRight] = "Left-to-Right LayoutDirection";
261 layoutDirectionList.append(Qt::RightToLeft);
262 layoutDirectionNames[Qt::RightToLeft] = "Right-to-Left LayoutDirection";
263
264 bool selectionModeEnabled = false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
265 // We only enable it for a few separate tests at the end.
266
267 for (const KFileItemListView::ItemLayout &layout : layoutList) {
268 // The following settings depend on the layout.
269 // Note that 'columns' are actually 'rows' in
270 // Compact layout.
271 Qt::Orientation scrollOrientation;
272 QList<int> columnCountList;
273 Qt::Key nextItemKey = Qt::Key_Right;
274 Qt::Key previousItemKey = Qt::Key_Right;
275 Qt::Key nextRowKey = Qt::Key_Right;
276 Qt::Key previousRowKey = Qt::Key_Right;
277
278 switch (layout) {
279 case KFileItemListView::IconsLayout:
280 scrollOrientation = Qt::Vertical;
281 columnCountList << 1 << 3 << 5;
282 nextItemKey = Qt::Key_Right;
283 previousItemKey = Qt::Key_Left;
284 nextRowKey = Qt::Key_Down;
285 previousRowKey = Qt::Key_Up;
286 break;
287 case KFileItemListView::CompactLayout:
288 scrollOrientation = Qt::Horizontal;
289 columnCountList << 1 << 3 << 5;
290 nextItemKey = Qt::Key_Down;
291 previousItemKey = Qt::Key_Up;
292 nextRowKey = Qt::Key_Right;
293 previousRowKey = Qt::Key_Left;
294 break;
295 case KFileItemListView::DetailsLayout:
296 scrollOrientation = Qt::Vertical;
297 columnCountList << 1;
298 nextItemKey = Qt::Key_Down;
299 previousItemKey = Qt::Key_Up;
300 nextRowKey = Qt::Key_Down;
301 previousRowKey = Qt::Key_Up;
302 break;
303 }
304 for (auto layoutDirection : std::as_const(layoutDirectionList)) {
305 if (layoutDirection == Qt::RightToLeft) {
306 switch (layout) {
307 case KFileItemListView::IconsLayout:
308 std::swap(nextItemKey, previousItemKey);
309 break;
310 case KFileItemListView::CompactLayout:
311 std::swap(nextRowKey, previousRowKey);
312 break;
313 default:
314 break;
315 }
316 }
317 for (int columnCount : std::as_const(columnCountList)) {
318 for (bool groupingEnabled : std::as_const(groupingEnabledList)) {
319 QList<QPair<KeyPress, ViewState>> testList;
320
321 // First, key presses which should have the same effect
322 // for any layout and any number of columns.
323 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
324 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
325 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
326 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
327 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
328 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
329 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
330 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
331 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
332 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
333 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
334 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
335 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
336 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
337 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
338 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
339 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
340 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
341 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
342 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
343 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
344 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
345 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
346 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
347 << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
348 << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
349 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
350 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
351 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
352 << qMakePair(KeyPress(previousItemKey), ViewState(13, KItemSet() << 13))
353 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
354 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(14, KItemSet()))
355 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
356 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
357 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
358
359 // Next, we test combinations of key presses which only work for a
360 // particular number of columns and either enabled or disabled grouping.
361
362 // One column.
363 if (columnCount == 1) {
364 testList << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
365 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
366 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
367 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
368 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
369 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
370 }
371
372 // Multiple columns: we test both 3 and 5 columns with grouping
373 // enabled or disabled. For each case, the layout of the items
374 // in the view is shown (both using file names and indices) to
375 // make it easier to understand what the tests do.
376
377 if (columnCount == 3 && !groupingEnabled) {
378 // 3 columns, no grouping:
379 //
380 // a1 a2 a3 | 0 1 2
381 // b1 c1 c2 | 3 4 5
382 // c3 c4 c5 | 6 7 8
383 // d1 d2 d3 | 9 10 11
384 // d4 e1 e2 | 12 13 14
385 // e3 e4 e5 | 15 16 17
386 // e6 e7 | 18 19
387 testList << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
388 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
389 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
390 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
391 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
392 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
393 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
394 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
395 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
396 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
397 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
398 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
399 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
400 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
401 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
402 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
403 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
404 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
405 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
406 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
407 }
408
409 if (columnCount == 5 && !groupingEnabled) {
410 // 5 columns, no grouping:
411 //
412 // a1 a2 a3 b1 c1 | 0 1 2 3 4
413 // c2 c3 c4 c5 d1 | 5 6 7 8 9
414 // d2 d3 d4 e1 e2 | 10 11 12 13 14
415 // e3 e4 e5 e6 e7 | 15 16 17 18 19
416 testList << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
417 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
418 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
419 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
420 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
421 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
422 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
423 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
424 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
425 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
426 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
427 }
428
429 if (columnCount == 3 && groupingEnabled) {
430 // 3 columns, with grouping:
431 //
432 // a1 a2 a3 | 0 1 2
433 // b1 | 3
434 // c1 c2 c3 | 4 5 6
435 // c4 c5 | 7 8
436 // d1 d2 d3 | 9 10 11
437 // d4 | 12
438 // e1 e2 e3 | 13 14 15
439 // e4 e5 e6 | 16 17 18
440 // e7 | 19
441 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
442 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
443 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
444 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
445 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
446 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
447 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
448 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
449 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
450 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
451 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
452 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
453 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
454 }
455
456 if (columnCount == 5 && groupingEnabled) {
457 // 5 columns, with grouping:
458 //
459 // a1 a2 a3 | 0 1 2
460 // b1 | 3
461 // c1 c2 c3 c4 c5 | 4 5 6 7 8
462 // d1 d2 d3 d4 | 9 10 11 12
463 // e1 e2 e3 e4 e5 | 13 14 15 16 17
464 // e6 e7 | 18 19
465 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
466 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
467 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
468 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
469 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
470 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
471 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
472 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
473 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
474 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
475 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
476 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
477 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
478 }
479
480 const QString testName = layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
481 + groupingEnabledNames[groupingEnabled] + ", " + layoutDirectionNames[layoutDirection];
482
483 const QByteArray testNameAscii = testName.toLatin1();
484
485 QTest::newRow(testNameAscii.data())
486 << layout << scrollOrientation << columnCount << groupingEnabled << layoutDirection << selectionModeEnabled << testList;
487 }
488 }
489 }
490 }
491
492 /**
493 * Selection mode tests
494 * We only test for the default icon view mode with typical scrollOrientation, selectionBehaviour, no grouping, left-to-right layoutDirection because none
495 * of this should affect selection mode and special-casing selection mode within the above test would make the above code even more complex than it already
496 * is.
497 */
498 selectionModeEnabled = true;
499 const KFileItemListView::ItemLayout layout = KFileItemListView::IconsLayout;
500 const Qt::Orientation scrollOrientation = Qt::Vertical;
501 const int columnCount = 3;
502 const Qt::Key nextItemKey = Qt::Key_Right;
503 const Qt::Key previousItemKey = Qt::Key_Left;
504 const Qt::Key nextRowKey = Qt::Key_Down;
505 const Qt::Key previousRowKey = Qt::Key_Up;
506
507 const Qt::LayoutDirection layoutDirection = Qt::LeftToRight;
508 const bool groupingEnabled = false;
509
510 QList<QPair<KeyPress, ViewState>> testList;
511
512 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet())) // In selection mode nothing is selected simply by moving with arrow keys.
513 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1)) // Pressing Return toggles the selection but does not activate.
514 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet())) // Pressing Enter toggles the selection but does not activate.
515 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet()))
516 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3)) // Shift+Arrow key still selects in selection mode.
517 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2))
518 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2 << 3))
519 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2)) // Shift+Left and then Shift+Right cancel each other out.
520 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2))
521 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 4))
522 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 2 << 4))
523 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4))
524 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
525 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3 << 4))
526 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
527 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 0 << 1 << 2 << 3 << 4))
528 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
529 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
530 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
531 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
532 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
533 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
534 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // Space toggles selection in selection mode.
535 << qMakePair(KeyPress(Qt::Key_D), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // No selection change by type-ahead.
536 << qMakePair(KeyPress(Qt::Key_Space), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // Space is not added to type-ahead.
537 << qMakePair(KeyPress(Qt::Key_4), ViewState(12, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // No selection change by type-ahead.
538 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
539
540 // The following tests assume a columnCount of three and no grouping enabled.
541 << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
542 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
543 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
544 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
545 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
546 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
547 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
548 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
549 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 9 << 18 << 19))
550 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
551 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
552 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
553 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
554 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
555 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
556 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
557 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
558 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
559 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
560 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19));
561
562 const QString testName = "Selection Mode: " + layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
563 + groupingEnabledNames[groupingEnabled] + ", " + layoutDirectionNames[layoutDirection];
564
565 const QByteArray testNameAscii = testName.toLatin1();
566
567 QTest::newRow(testNameAscii.data()) << layout << scrollOrientation << columnCount << groupingEnabled << layoutDirection << selectionModeEnabled << testList;
568 }
569
570 /**
571 * This function sets the view's properties according to the data provided.
572 *
573 * The list \a testList contains pairs of key presses, which are sent to the
574 * container, and expected view states, which are verified then.
575 */
576 void KItemListControllerTest::testKeyboardNavigationMultiSelection()
577 {
578 QFETCH(KFileItemListView::ItemLayout, layout);
579 QFETCH(Qt::Orientation, scrollOrientation);
580 QFETCH(int, columnCount);
581 QFETCH(bool, groupingEnabled);
582 QFETCH(Qt::LayoutDirection, layoutDirection);
583 QFETCH(bool, selectionModeEnabled);
584 QFETCH(QList<keyPressViewStatePair>, testList);
585
586 QApplication::setLayoutDirection(layoutDirection);
587 m_view->setLayoutDirection(layoutDirection);
588
589 m_view->setItemLayout(layout);
590 QCOMPARE(m_view->itemLayout(), layout);
591
592 m_view->setScrollOrientation(scrollOrientation);
593 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
594
595 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
596 QCOMPARE(m_controller->selectionBehavior(), KItemListController::MultiSelection);
597
598 m_model->setGroupedSorting(groupingEnabled);
599 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
600
601 m_controller->setSelectionModeEnabled(selectionModeEnabled);
602 QCOMPARE(m_controller->selectionMode(), selectionModeEnabled);
603
604 adjustGeometryForColumnCount(columnCount);
605 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
606
607 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
608 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
609
610 int rowCount = 0;
611 while (!testList.isEmpty()) {
612 ++rowCount;
613 const QPair<KeyPress, ViewState> test = testList.takeFirst();
614 const Qt::Key key = test.first.m_key;
615 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
616 const int current = test.second.m_current;
617 const KItemSet selection = test.second.m_selection;
618 const bool activated = test.second.m_activated;
619
620 QTest::keyClick(m_container, key, modifier);
621
622 QVERIFY2(
623 m_selectionManager->currentItem() == current,
624 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
625 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationMultiSelection_data().")
626 .arg(m_selectionManager->currentItem())
627 .arg(current)
628 .arg(QKeySequence(key).toString())
629 .arg(rowCount)));
630 QCOMPARE(m_selectionManager->selectedItems(), selection);
631
632 if (activated) {
633 if (!selection.isEmpty()) {
634 // The selected items should be activated.
635 if (selection.count() == 1) {
636 QVERIFY(!spySingleItemActivated.isEmpty());
637 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
638 QVERIFY(spyMultipleItemsActivated.isEmpty());
639 } else {
640 QVERIFY(spySingleItemActivated.isEmpty());
641 QVERIFY(!spyMultipleItemsActivated.isEmpty());
642 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
643 }
644 } else {
645 QVERIFY(!spySingleItemActivated.isEmpty());
646 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
647 QVERIFY(spyMultipleItemsActivated.isEmpty());
648 }
649 }
650 }
651 }
652
653 /**
654 * This function tests all possible combinations of view layouts, layout direction,
655 * and enabled/disabled groupings for different column counts, and
656 * provides a list of key presses and the states that the view should be in
657 * after the key press event.
658 */
659 void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data()
660 {
661 QTest::addColumn<KFileItemListView::ItemLayout>("layout");
662 QTest::addColumn<Qt::Orientation>("scrollOrientation");
663 QTest::addColumn<int>("columnCount");
664 QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior"); // Defines how many items can be selected at the same time.
665 QTest::addColumn<bool>("groupingEnabled");
666 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
667 QTest::addColumn<bool>(
668 "selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
669 QTest::addColumn<QList<QPair<KeyPress, ViewState>>>("testList");
670
671 QList<KFileItemListView::ItemLayout> layoutList;
672 QHash<KFileItemListView::ItemLayout, QString> layoutNames;
673 layoutList.append(KFileItemListView::IconsLayout);
674 layoutNames[KFileItemListView::IconsLayout] = "Icons";
675 layoutList.append(KFileItemListView::CompactLayout);
676 layoutNames[KFileItemListView::CompactLayout] = "Compact";
677 layoutList.append(KFileItemListView::DetailsLayout);
678 layoutNames[KFileItemListView::DetailsLayout] = "Details";
679
680 QList<KItemListController::SelectionBehavior> selectionBehaviorList;
681 QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
682 selectionBehaviorList.append(KItemListController::NoSelection);
683 selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
684 selectionBehaviorList.append(KItemListController::SingleSelection);
685 selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
686
687 QList<bool> groupingEnabledList;
688 QHash<bool, QString> groupingEnabledNames;
689 groupingEnabledList.append(false);
690 groupingEnabledNames[false] = "ungrouped";
691 groupingEnabledList.append(true);
692 groupingEnabledNames[true] = "grouping enabled";
693
694 QList<Qt::LayoutDirection> layoutDirectionList;
695 QHash<Qt::LayoutDirection, QString> layoutDirectionNames;
696 layoutDirectionList.append(Qt::LeftToRight);
697 layoutDirectionNames[Qt::LeftToRight] = "Left-to-Right LayoutDirection";
698 layoutDirectionList.append(Qt::RightToLeft);
699 layoutDirectionNames[Qt::RightToLeft] = "Right-to-Left LayoutDirection";
700
701 bool selectionModeEnabled = false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
702 // We only enable it for a few separate tests at the end.
703
704 for (const KFileItemListView::ItemLayout &layout : layoutList) {
705 // The following settings depend on the layout.
706 // Note that 'columns' are actually 'rows' in
707 // Compact layout.
708 Qt::Orientation scrollOrientation;
709 QList<int> columnCountList;
710 Qt::Key nextItemKey = Qt::Key_Right;
711 Qt::Key previousItemKey = Qt::Key_Right;
712 Qt::Key nextRowKey = Qt::Key_Right;
713 Qt::Key previousRowKey = Qt::Key_Right;
714
715 switch (layout) {
716 case KFileItemListView::IconsLayout:
717 scrollOrientation = Qt::Vertical;
718 columnCountList << 1 << 3 << 5;
719 nextItemKey = Qt::Key_Right;
720 previousItemKey = Qt::Key_Left;
721 nextRowKey = Qt::Key_Down;
722 previousRowKey = Qt::Key_Up;
723 break;
724 case KFileItemListView::CompactLayout:
725 scrollOrientation = Qt::Horizontal;
726 columnCountList << 1 << 3 << 5;
727 nextItemKey = Qt::Key_Down;
728 previousItemKey = Qt::Key_Up;
729 nextRowKey = Qt::Key_Right;
730 previousRowKey = Qt::Key_Left;
731 break;
732 case KFileItemListView::DetailsLayout:
733 scrollOrientation = Qt::Vertical;
734 columnCountList << 1;
735 nextItemKey = Qt::Key_Down;
736 previousItemKey = Qt::Key_Up;
737 nextRowKey = Qt::Key_Down;
738 previousRowKey = Qt::Key_Up;
739 break;
740 }
741 for (auto layoutDirection : std::as_const(layoutDirectionList)) {
742 if (layoutDirection == Qt::RightToLeft) {
743 switch (layout) {
744 case KFileItemListView::IconsLayout:
745 std::swap(nextItemKey, previousItemKey);
746 break;
747 case KFileItemListView::CompactLayout:
748 std::swap(nextRowKey, previousRowKey);
749 break;
750 default:
751 break;
752 }
753 }
754 for (int columnCount : std::as_const(columnCountList)) {
755 for (const KItemListController::SelectionBehavior &selectionBehavior : std::as_const(selectionBehaviorList)) {
756 for (bool groupingEnabled : std::as_const(groupingEnabledList)) {
757 QList<QPair<KeyPress, ViewState>> testList;
758
759 // First, key presses which should have the same effect
760 // for any layout and any number of columns.
761 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
762 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
763 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
764 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
765 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
766 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
767 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
768 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
769 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
770 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
771 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
772 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
773 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
774 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
775 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
776 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
777 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
778 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
779 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
780 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
781 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
782 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
783 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
784 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
785 << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
786 << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
787 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
788 << qMakePair(KeyPress(Qt::Key_E), ViewState(16, KItemSet() << 16))
789 << qMakePair(KeyPress(Qt::Key_E), ViewState(17, KItemSet() << 17))
790 << qMakePair(KeyPress(previousItemKey), ViewState(16, KItemSet() << 16))
791 << qMakePair(KeyPress(Qt::Key_E), ViewState(17, KItemSet() << 17))
792 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(17, KItemSet()))
793 << qMakePair(KeyPress(Qt::Key_E), ViewState(18, KItemSet() << 18))
794 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
795 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
796
797 // Next, we test combinations of key presses which only work for a
798 // particular number of columns and either enabled or disabled grouping.
799
800 // One column.
801 if (columnCount == 1) {
802 testList << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
803 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
804 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
805 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
806 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
807 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
808 }
809
810 // Multiple columns: we test both 3 and 5 columns with grouping
811 // enabled or disabled. For each case, the layout of the items
812 // in the view is shown (both using file names and indices) to
813 // make it easier to understand what the tests do.
814
815 if (columnCount == 3 && !groupingEnabled) {
816 // 3 columns, no grouping:
817 //
818 // a1 a2 a3 | 0 1 2
819 // b1 c1 c2 | 3 4 5
820 // c3 c4 c5 | 6 7 8
821 // d1 d2 d3 | 9 10 11
822 // d4 e1 e2 | 12 13 14
823 // e3 e4 e5 | 15 16 17
824 // e6 e7 | 18 19
825 testList << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
826 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
827 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
828 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
829 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
830 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
831 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
832 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
833 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
834 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
835 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
836 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
837 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
838 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
839 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
840 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
841 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
842 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
843 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
844 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
845 }
846
847 if (columnCount == 5 && !groupingEnabled) {
848 // 5 columns, no grouping:
849 //
850 // a1 a2 a3 b1 c1 | 0 1 2 3 4
851 // c2 c3 c4 c5 d1 | 5 6 7 8 9
852 // d2 d3 d4 e1 e2 | 10 11 12 13 14
853 // e3 e4 e5 e6 e7 | 15 16 17 18 19
854 testList << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
855 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
856 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
857 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
858 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
859 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
860 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
861 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
862 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
863 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
864 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
865 }
866
867 if (columnCount == 3 && groupingEnabled) {
868 // 3 columns, with grouping:
869 //
870 // a1 a2 a3 | 0 1 2
871 // b1 | 3
872 // c1 c2 c3 | 4 5 6
873 // c4 c5 | 7 8
874 // d1 d2 d3 | 9 10 11
875 // d4 | 12
876 // e1 e2 e3 | 13 14 15
877 // e4 e5 e6 | 16 17 18
878 // e7 | 19
879 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
880 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
881 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
882 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
883 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
884 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
885 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
886 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
887 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
888 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
889 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
890 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
891 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
892 }
893
894 if (columnCount == 5 && groupingEnabled) {
895 // 5 columns, with grouping:
896 //
897 // a1 a2 a3 | 0 1 2
898 // b1 | 3
899 // c1 c2 c3 c4 c5 | 4 5 6 7 8
900 // d1 d2 d3 d4 | 9 10 11 12
901 // e1 e2 e3 e4 e5 | 13 14 15 16 17
902 // e6 e7 | 18 19
903 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
904 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
905 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
906 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
907 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
908 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
909 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
910 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
911 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
912 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
913 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
914 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
915 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
916 }
917
918 const QString testName = layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
919 + selectionBehaviorNames[selectionBehavior] + ", " + groupingEnabledNames[groupingEnabled] + ", "
920 + layoutDirectionNames[layoutDirection];
921
922 const QByteArray testNameAscii = testName.toLatin1();
923
924 QTest::newRow(testNameAscii.data()) << layout << scrollOrientation << columnCount << selectionBehavior << groupingEnabled
925 << layoutDirection << selectionModeEnabled << testList;
926 }
927 }
928 }
929 }
930 }
931 }
932
933 /**
934 * This function sets the view's properties according to the data provided.
935 *
936 * The list \a testList contains pairs of key presses, which are sent to the
937 * container, and expected view states, which are verified then.
938 */
939 void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection()
940 {
941 QFETCH(KFileItemListView::ItemLayout, layout);
942 QFETCH(Qt::Orientation, scrollOrientation);
943 QFETCH(int, columnCount);
944 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
945 QFETCH(bool, groupingEnabled);
946 QFETCH(Qt::LayoutDirection, layoutDirection);
947 QFETCH(bool, selectionModeEnabled);
948 QFETCH(QList<keyPressViewStatePair>, testList);
949
950 QApplication::setLayoutDirection(layoutDirection);
951 m_view->setLayoutDirection(layoutDirection);
952
953 m_view->setItemLayout(layout);
954 QCOMPARE(m_view->itemLayout(), layout);
955
956 m_view->setScrollOrientation(scrollOrientation);
957 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
958
959 m_controller->setSelectionBehavior(selectionBehavior);
960 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
961
962 m_model->setGroupedSorting(groupingEnabled);
963 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
964
965 m_controller->setSelectionModeEnabled(selectionModeEnabled);
966 QCOMPARE(m_controller->selectionMode(), selectionModeEnabled);
967
968 adjustGeometryForColumnCount(columnCount);
969 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
970
971 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
972 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
973
974 int rowCount = 0;
975 while (!testList.isEmpty()) {
976 ++rowCount;
977 const QPair<KeyPress, ViewState> test = testList.takeFirst();
978 const Qt::Key key = test.first.m_key;
979 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
980 const int current = test.second.m_current;
981 const KItemSet selection = test.second.m_selection;
982 const bool activated = test.second.m_activated;
983
984 QTest::keyClick(m_container, key, modifier);
985
986 QVERIFY2(
987 m_selectionManager->currentItem() == current,
988 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
989 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data().")
990 .arg(m_selectionManager->currentItem())
991 .arg(current)
992 .arg(QKeySequence(key).toString())
993 .arg(rowCount)));
994 switch (selectionBehavior) {
995 case KItemListController::NoSelection:
996 QVERIFY(m_selectionManager->selectedItems().isEmpty());
997 break;
998 case KItemListController::SingleSelection:
999 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current);
1000 break;
1001 default:
1002 Q_UNREACHABLE();
1003 }
1004
1005 if (activated) {
1006 switch (selectionBehavior) {
1007 case KItemListController::NoSelection:
1008 case KItemListController::SingleSelection:
1009 // In NoSelection and SingleSelection mode, the current item should be activated.
1010 QVERIFY(!spySingleItemActivated.isEmpty());
1011 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
1012 QVERIFY(spyMultipleItemsActivated.isEmpty());
1013 break;
1014 default:
1015 Q_UNREACHABLE();
1016 }
1017 }
1018 }
1019 }
1020
1021 void KItemListControllerTest::testMouseClickActivation()
1022 {
1023 m_view->setItemLayout(KFileItemListView::IconsLayout);
1024
1025 // Make sure that we have a large window, such that
1026 // the items are visible and clickable.
1027 adjustGeometryForColumnCount(5);
1028
1029 // Make sure that the first item is visible in the view.
1030 m_view->setScrollOffset(0);
1031 QCOMPARE(m_view->firstVisibleIndex(), 0);
1032
1033 const QPointF pos = m_view->itemContextRect(0).center();
1034
1035 // Save the "single click" setting.
1036 const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
1037
1038 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
1039 mousePressEvent.setPos(pos);
1040 mousePressEvent.setButton(Qt::LeftButton);
1041 mousePressEvent.setButtons(Qt::LeftButton);
1042
1043 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
1044 mouseReleaseEvent.setPos(pos);
1045 mouseReleaseEvent.setButton(Qt::LeftButton);
1046 mouseReleaseEvent.setButtons(Qt::NoButton);
1047
1048 QGraphicsSceneMouseEvent mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
1049 mouseDoubleClickEvent.setPos(pos);
1050 mouseDoubleClickEvent.setButton(Qt::LeftButton);
1051 mouseDoubleClickEvent.setButtons(Qt::LeftButton);
1052
1053 QGraphicsSceneMouseEvent mouseRightPressEvent(QEvent::GraphicsSceneMousePress);
1054 mouseRightPressEvent.setPos(pos);
1055 mouseRightPressEvent.setButton(Qt::RightButton);
1056 mouseRightPressEvent.setButtons(Qt::RightButton);
1057
1058 QGraphicsSceneMouseEvent mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease);
1059 mouseRightReleaseEvent.setPos(pos);
1060 mouseRightReleaseEvent.setButton(Qt::RightButton);
1061 mouseRightReleaseEvent.setButtons(Qt::NoButton);
1062
1063 QGraphicsSceneMouseEvent mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
1064 mouseRightDoubleClickEvent.setPos(pos);
1065 mouseRightDoubleClickEvent.setButton(Qt::RightButton);
1066 mouseRightDoubleClickEvent.setButtons(Qt::RightButton);
1067
1068 QGraphicsSceneMouseEvent mouseBackPressEvent(QEvent::GraphicsSceneMousePress);
1069 mouseBackPressEvent.setPos(pos);
1070 mouseBackPressEvent.setButton(Qt::BackButton);
1071 mouseBackPressEvent.setButtons(Qt::BackButton);
1072
1073 QGraphicsSceneMouseEvent mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease);
1074 mouseBackReleaseEvent.setPos(pos);
1075 mouseBackReleaseEvent.setButton(Qt::BackButton);
1076 mouseBackReleaseEvent.setButtons(Qt::NoButton);
1077
1078 QGraphicsSceneMouseEvent mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
1079 mouseBackDoubleClickEvent.setPos(pos);
1080 mouseBackDoubleClickEvent.setButton(Qt::BackButton);
1081 mouseBackDoubleClickEvent.setButtons(Qt::BackButton);
1082
1083 QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
1084
1085 // Default setting: single click activation.
1086 m_testStyle->setActivateItemOnSingleClick(true);
1087 m_view->event(&mousePressEvent);
1088 m_view->event(&mouseReleaseEvent);
1089 QCOMPARE(spyItemActivated.count(), 1);
1090 spyItemActivated.clear();
1091 QVERIFY2(!m_view->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
1092
1093 // Set the global setting to "double click activation".
1094 m_testStyle->setActivateItemOnSingleClick(false);
1095 m_view->event(&mousePressEvent);
1096 m_view->event(&mouseReleaseEvent);
1097 QCOMPARE(spyItemActivated.count(), 0);
1098 spyItemActivated.clear();
1099 QVERIFY(m_view->controller()->selectionManager()->hasSelection());
1100
1101 // emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
1102 m_view->event(&mousePressEvent);
1103 m_view->event(&mouseReleaseEvent);
1104 m_view->event(&mouseDoubleClickEvent);
1105 m_view->event(&mouseReleaseEvent);
1106 QCOMPARE(spyItemActivated.count(), 1);
1107 spyItemActivated.clear();
1108 QVERIFY2(!m_view->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
1109
1110 // right mouse button should not trigger activation
1111 m_view->event(&mouseRightPressEvent);
1112 m_view->event(&mouseRightReleaseEvent);
1113 m_view->event(&mouseRightDoubleClickEvent);
1114 m_view->event(&mouseRightReleaseEvent);
1115 QCOMPARE(spyItemActivated.count(), 0);
1116
1117 // back mouse button should not trigger activation
1118 m_view->event(&mouseBackPressEvent);
1119 m_view->event(&mouseBackReleaseEvent);
1120 m_view->event(&mouseBackDoubleClickEvent);
1121 m_view->event(&mouseBackReleaseEvent);
1122 QCOMPARE(spyItemActivated.count(), 0);
1123
1124 // Enforce single click activation in the controller.
1125 m_controller->setSingleClickActivationEnforced(true);
1126 m_view->event(&mousePressEvent);
1127 m_view->event(&mouseReleaseEvent);
1128 QCOMPARE(spyItemActivated.count(), 1);
1129 spyItemActivated.clear();
1130 constexpr const char *reasonWhySelectionShouldPersist = "An item was selected before this mouse click. The click should not have cleared this selection.";
1131 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
1132
1133 // Do not enforce single click activation in the controller.
1134 m_controller->setSingleClickActivationEnforced(false);
1135 m_view->event(&mousePressEvent);
1136 m_view->event(&mouseReleaseEvent);
1137 QCOMPARE(spyItemActivated.count(), 0);
1138 spyItemActivated.clear();
1139 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
1140
1141 // Set the global setting back to "single click activation".
1142 m_testStyle->setActivateItemOnSingleClick(true);
1143 m_view->event(&mousePressEvent);
1144 m_view->event(&mouseReleaseEvent);
1145 QCOMPARE(spyItemActivated.count(), 1);
1146 spyItemActivated.clear();
1147 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
1148
1149 // Enforce single click activation in the controller.
1150 m_controller->setSingleClickActivationEnforced(true);
1151 m_view->event(&mousePressEvent);
1152 m_view->event(&mouseReleaseEvent);
1153 QCOMPARE(spyItemActivated.count(), 1);
1154 spyItemActivated.clear();
1155 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
1156
1157 // Restore previous settings.
1158 m_controller->setSingleClickActivationEnforced(true);
1159 m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
1160 }
1161
1162 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
1163 {
1164 const QSize size = m_view->itemSize().toSize();
1165
1166 QRect rect = m_container->geometry();
1167 rect.setSize(size * count);
1168 m_container->setGeometry(rect);
1169
1170 // Increase the size of the container until the correct column count is reached.
1171 while (m_view->m_layouter->m_columnCount < count) {
1172 rect = m_container->geometry();
1173 rect.setSize(rect.size() + size);
1174 m_container->setGeometry(rect);
1175 }
1176 }
1177
1178 QTEST_MAIN(KItemListControllerTest)
1179
1180 #include "kitemlistcontrollertest.moc"