]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
refactor: replace QString() with QStringLiteral() for better performance
[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 testKeyboardNavigation_data();
77 void testKeyboardNavigation();
78 void testMouseClickActivation();
79
80 private:
81 /**
82 * Make sure that the number of columns in the view is equal to \a count
83 * by changing the geometry of the container.
84 */
85 void adjustGeometryForColumnCount(int count);
86
87 private:
88 KFileItemListView *m_view;
89 KItemListController *m_controller;
90 KItemListSelectionManager *m_selectionManager;
91 KFileItemModel *m_model;
92 TestDir *m_testDir;
93 KItemListContainer *m_container;
94 KItemListControllerTestStyle *m_testStyle;
95 };
96
97 /**
98 * This function initializes the member objects, creates the temporary files, and
99 * shows the view on the screen on startup. This could also be done before every
100 * single test, but this would make the time needed to run the test much larger.
101 */
102 void KItemListControllerTest::initTestCase()
103 {
104 QStandardPaths::setTestModeEnabled(true);
105 qRegisterMetaType<KItemSet>("KItemSet");
106
107 m_testDir = new TestDir();
108 m_model = new KFileItemModel();
109 m_view = new KFileItemListView();
110 m_controller = new KItemListController(m_model, m_view, this);
111 m_container = new KItemListContainer(m_controller);
112 #ifndef QT_NO_ACCESSIBILITY
113 m_view->setAccessibleParentsObject(m_container);
114 #endif
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 << "a1"
123 << "a2"
124 << "a3"
125 << "b1"
126 << "c1"
127 << "c2"
128 << "c3"
129 << "c4"
130 << "c5"
131 << "d1"
132 << "d2"
133 << "d3"
134 << "d4"
135 << "e"
136 << "e 2"
137 << "e 3"
138 << "e 4"
139 << "e 5"
140 << "e 6"
141 << "e 7";
142
143 m_testDir->createFiles(files);
144 m_model->loadDirectory(m_testDir->url());
145 QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
146 QVERIFY(spyDirectoryLoadingCompleted.wait());
147
148 m_container->show();
149 QVERIFY(QTest::qWaitForWindowExposed(m_container));
150 }
151
152 void KItemListControllerTest::cleanupTestCase()
153 {
154 delete m_container;
155 m_container = nullptr;
156
157 delete m_testDir;
158 m_testDir = nullptr;
159 }
160
161 /** Before each test, the current item, selection, and item size are reset to the defaults. */
162 void KItemListControllerTest::init()
163 {
164 m_selectionManager->setCurrentItem(0);
165 QCOMPARE(m_selectionManager->currentItem(), 0);
166
167 m_selectionManager->clearSelection();
168 QVERIFY(!m_selectionManager->hasSelection());
169
170 const QSizeF itemSize(50, 50);
171 m_view->setItemSize(itemSize);
172 QCOMPARE(m_view->itemSize(), itemSize);
173 }
174
175 void KItemListControllerTest::cleanup()
176 {
177 }
178
179 /**
180 * \class KeyPress is a small helper struct that represents a key press event,
181 * including the key and the keyboard modifiers.
182 */
183 struct KeyPress {
184 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier)
185 : m_key(key)
186 , m_modifier(modifier)
187 {
188 }
189
190 Qt::Key m_key;
191 Qt::KeyboardModifiers m_modifier;
192 };
193
194 /**
195 * \class ViewState is a small helper struct that represents a certain state
196 * of the view, including the current item, the selected items in MultiSelection
197 * mode (in the other modes, the selection is either empty or equal to the
198 * current item), and the information whether items were activated by the last
199 * key press.
200 */
201 struct ViewState {
202 ViewState(int current, const KItemSet &selection, bool activated = false)
203 : m_current(current)
204 , m_selection(selection)
205 , m_activated(activated)
206 {
207 }
208
209 int m_current;
210 KItemSet m_selection;
211 bool m_activated;
212 };
213
214 // We have to define a typedef for the pair in order to make the test compile.
215 typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
216 Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
217
218 /**
219 * This function provides the data for the actual test function
220 * KItemListControllerTest::testKeyboardNavigation().
221 * It tests all possible combinations of view layouts, selection behaviors,
222 * and enabled/disabled groupings for different column counts, and
223 * provides a list of key presses and the states that the view should be in
224 * after the key press event.
225 */
226 void KItemListControllerTest::testKeyboardNavigation_data()
227 {
228 QTest::addColumn<KFileItemListView::ItemLayout>("layout");
229 QTest::addColumn<Qt::Orientation>("scrollOrientation");
230 QTest::addColumn<int>("columnCount");
231 QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior");
232 QTest::addColumn<bool>("groupingEnabled");
233 QTest::addColumn<Qt::LayoutDirection>("layoutDirection");
234 QTest::addColumn<QList<QPair<KeyPress, ViewState>>>("testList");
235
236 QList<KFileItemListView::ItemLayout> layoutList;
237 QHash<KFileItemListView::ItemLayout, QString> layoutNames;
238 layoutList.append(KFileItemListView::IconsLayout);
239 layoutNames[KFileItemListView::IconsLayout] = "Icons";
240 layoutList.append(KFileItemListView::CompactLayout);
241 layoutNames[KFileItemListView::CompactLayout] = "Compact";
242 layoutList.append(KFileItemListView::DetailsLayout);
243 layoutNames[KFileItemListView::DetailsLayout] = "Details";
244
245 QList<KItemListController::SelectionBehavior> selectionBehaviorList;
246 QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
247 selectionBehaviorList.append(KItemListController::NoSelection);
248 selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
249 selectionBehaviorList.append(KItemListController::SingleSelection);
250 selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
251 selectionBehaviorList.append(KItemListController::MultiSelection);
252 selectionBehaviorNames[KItemListController::MultiSelection] = "MultiSelection";
253
254 QList<bool> groupingEnabledList;
255 QHash<bool, QString> groupingEnabledNames;
256 groupingEnabledList.append(false);
257 groupingEnabledNames[false] = "ungrouped";
258 groupingEnabledList.append(true);
259 groupingEnabledNames[true] = "grouping enabled";
260
261 QList<Qt::LayoutDirection> layoutDirectionList;
262 QHash<Qt::LayoutDirection, QString> layoutDirectionNames;
263 layoutDirectionList.append(Qt::LeftToRight);
264 layoutDirectionNames[Qt::LeftToRight] = "Left-to-Right LayoutDirection";
265 layoutDirectionList.append(Qt::RightToLeft);
266 layoutDirectionNames[Qt::RightToLeft] = "Right-to-Left LayoutDirection";
267
268 for (const KFileItemListView::ItemLayout &layout : layoutList) {
269 // The following settings depend on the layout.
270 // Note that 'columns' are actually 'rows' in
271 // Compact layout.
272 Qt::Orientation scrollOrientation;
273 QList<int> columnCountList;
274 Qt::Key nextItemKey;
275 Qt::Key previousItemKey;
276 Qt::Key nextRowKey;
277 Qt::Key previousRowKey;
278
279 switch (layout) {
280 case KFileItemListView::IconsLayout:
281 scrollOrientation = Qt::Vertical;
282 columnCountList << 1 << 3 << 5;
283 nextItemKey = Qt::Key_Right;
284 previousItemKey = Qt::Key_Left;
285 nextRowKey = Qt::Key_Down;
286 previousRowKey = Qt::Key_Up;
287 break;
288 case KFileItemListView::CompactLayout:
289 scrollOrientation = Qt::Horizontal;
290 columnCountList << 1 << 3 << 5;
291 nextItemKey = Qt::Key_Down;
292 previousItemKey = Qt::Key_Up;
293 nextRowKey = Qt::Key_Right;
294 previousRowKey = Qt::Key_Left;
295 break;
296 case KFileItemListView::DetailsLayout:
297 scrollOrientation = Qt::Vertical;
298 columnCountList << 1;
299 nextItemKey = Qt::Key_Down;
300 previousItemKey = Qt::Key_Up;
301 nextRowKey = Qt::Key_Down;
302 previousRowKey = Qt::Key_Up;
303 break;
304 }
305 for (auto layoutDirection : std::as_const(layoutDirectionList)) {
306 if (layoutDirection == Qt::RightToLeft) {
307 switch (layout) {
308 case KFileItemListView::IconsLayout:
309 std::swap(nextItemKey, previousItemKey);
310 break;
311 case KFileItemListView::CompactLayout:
312 std::swap(nextRowKey, previousRowKey);
313 break;
314 default:
315 break;
316 }
317 }
318 for (int columnCount : std::as_const(columnCountList)) {
319 for (const KItemListController::SelectionBehavior &selectionBehavior : std::as_const(selectionBehaviorList)) {
320 for (bool groupingEnabled : std::as_const(groupingEnabledList)) {
321 QList<QPair<KeyPress, ViewState>> testList;
322
323 // First, key presses which should have the same effect
324 // for any layout and any number of columns.
325 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
326 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
327 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
328 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
329 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
330 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
331 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
332 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
333 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
334 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
335 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
336 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
337 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
338 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
339 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
340 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
341 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
342 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
343 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
344 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
345 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
346 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
347 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
348 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
349 << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
350 << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
351 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
352 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
353 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
354 << qMakePair(KeyPress(Qt::Key_E), ViewState(15, KItemSet() << 15))
355 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
356 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
357 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
358 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
359
360 // Next, we test combinations of key presses which only work for a
361 // particular number of columns and either enabled or disabled grouping.
362
363 // One column.
364 if (columnCount == 1) {
365 testList << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
366 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
367 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
368 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
369 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
370 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
371 }
372
373 // Multiple columns: we test both 3 and 5 columns with grouping
374 // enabled or disabled. For each case, the layout of the items
375 // in the view is shown (both using file names and indices) to
376 // make it easier to understand what the tests do.
377
378 if (columnCount == 3 && !groupingEnabled) {
379 // 3 columns, no grouping:
380 //
381 // a1 a2 a3 | 0 1 2
382 // b1 c1 c2 | 3 4 5
383 // c3 c4 c5 | 6 7 8
384 // d1 d2 d3 | 9 10 11
385 // d4 e1 e2 | 12 13 14
386 // e3 e4 e5 | 15 16 17
387 // e6 e7 | 18 19
388 testList << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
389 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
390 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
391 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
392 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
393 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
394 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
395 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
396 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
397 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
398 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
399 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
400 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
401 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
402 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
403 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
404 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
405 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
406 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
407 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
408 }
409
410 if (columnCount == 5 && !groupingEnabled) {
411 // 5 columns, no grouping:
412 //
413 // a1 a2 a3 b1 c1 | 0 1 2 3 4
414 // c2 c3 c4 c5 d1 | 5 6 7 8 9
415 // d2 d3 d4 e1 e2 | 10 11 12 13 14
416 // e3 e4 e5 e6 e7 | 15 16 17 18 19
417 testList << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
418 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
419 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
420 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
421 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
422 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
423 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
424 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
425 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
426 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
427 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
428 }
429
430 if (columnCount == 3 && groupingEnabled) {
431 // 3 columns, with grouping:
432 //
433 // a1 a2 a3 | 0 1 2
434 // b1 | 3
435 // c1 c2 c3 | 4 5 6
436 // c4 c5 | 7 8
437 // d1 d2 d3 | 9 10 11
438 // d4 | 12
439 // e1 e2 e3 | 13 14 15
440 // e4 e5 e6 | 16 17 18
441 // e7 | 19
442 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
443 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
444 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
445 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
446 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
447 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
448 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
449 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
450 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
451 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
452 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
453 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
454 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
455 }
456
457 if (columnCount == 5 && groupingEnabled) {
458 // 5 columns, with grouping:
459 //
460 // a1 a2 a3 | 0 1 2
461 // b1 | 3
462 // c1 c2 c3 c4 c5 | 4 5 6 7 8
463 // d1 d2 d3 d4 | 9 10 11 12
464 // e1 e2 e3 e4 e5 | 13 14 15 16 17
465 // e6 e7 | 18 19
466 testList << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
467 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
468 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
469 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
470 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
471 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
472 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
473 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
474 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
475 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
476 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
477 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
478 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
479 }
480
481 const QString testName = layoutNames[layout] + ", " + QStringLiteral("%1 columns, ").arg(columnCount)
482 + selectionBehaviorNames[selectionBehavior] + ", " + groupingEnabledNames[groupingEnabled] + ", "
483 + layoutDirectionNames[layoutDirection];
484
485 const QByteArray testNameAscii = testName.toLatin1();
486
487 QTest::newRow(testNameAscii.data())
488 << layout << scrollOrientation << columnCount << selectionBehavior << groupingEnabled << layoutDirection << testList;
489 }
490 }
491 }
492 }
493 }
494 }
495
496 /**
497 * This function sets the view's properties according to the data provided by
498 * KItemListControllerTest::testKeyboardNavigation_data().
499 *
500 * The list \a testList contains pairs of key presses, which are sent to the
501 * container, and expected view states, which are verified then.
502 */
503 void KItemListControllerTest::testKeyboardNavigation()
504 {
505 QFETCH(KFileItemListView::ItemLayout, layout);
506 QFETCH(Qt::Orientation, scrollOrientation);
507 QFETCH(int, columnCount);
508 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
509 QFETCH(bool, groupingEnabled);
510 QFETCH(Qt::LayoutDirection, layoutDirection);
511 QFETCH(QList<keyPressViewStatePair>, testList);
512
513 QApplication::setLayoutDirection(layoutDirection);
514 m_view->setLayoutDirection(layoutDirection);
515
516 m_view->setItemLayout(layout);
517 QCOMPARE(m_view->itemLayout(), layout);
518
519 m_view->setScrollOrientation(scrollOrientation);
520 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
521
522 m_controller->setSelectionBehavior(selectionBehavior);
523 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
524
525 m_model->setGroupedSorting(groupingEnabled);
526 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
527
528 adjustGeometryForColumnCount(columnCount);
529 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
530
531 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
532 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
533
534 int rowCount = 0;
535 while (!testList.isEmpty()) {
536 ++rowCount;
537 const QPair<KeyPress, ViewState> test = testList.takeFirst();
538 const Qt::Key key = test.first.m_key;
539 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
540 const int current = test.second.m_current;
541 const KItemSet selection = test.second.m_selection;
542 const bool activated = test.second.m_activated;
543
544 QTest::keyClick(m_container, key, modifier);
545
546 QVERIFY2(
547 m_selectionManager->currentItem() == current,
548 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
549 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigation_data().")
550 .arg(m_selectionManager->currentItem())
551 .arg(current)
552 .arg(QKeySequence(key).toString())
553 .arg(rowCount)));
554 switch (selectionBehavior) {
555 case KItemListController::NoSelection:
556 QVERIFY(m_selectionManager->selectedItems().isEmpty());
557 break;
558 case KItemListController::SingleSelection:
559 QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current);
560 break;
561 case KItemListController::MultiSelection:
562 QCOMPARE(m_selectionManager->selectedItems(), selection);
563 break;
564 }
565
566 if (activated) {
567 switch (selectionBehavior) {
568 case KItemListController::MultiSelection:
569 if (!selection.isEmpty()) {
570 // The selected items should be activated.
571 if (selection.count() == 1) {
572 QVERIFY(!spySingleItemActivated.isEmpty());
573 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
574 QVERIFY(spyMultipleItemsActivated.isEmpty());
575 } else {
576 QVERIFY(spySingleItemActivated.isEmpty());
577 QVERIFY(!spyMultipleItemsActivated.isEmpty());
578 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
579 }
580 break;
581 }
582 // No items are selected. Therefore, the current item should be activated.
583 // This is handled by falling through to the NoSelection/SingleSelection case.
584 Q_FALLTHROUGH();
585 case KItemListController::NoSelection:
586 case KItemListController::SingleSelection:
587 // In NoSelection and SingleSelection mode, the current item should be activated.
588 QVERIFY(!spySingleItemActivated.isEmpty());
589 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
590 QVERIFY(spyMultipleItemsActivated.isEmpty());
591 break;
592 }
593 }
594 }
595 }
596
597 void KItemListControllerTest::testMouseClickActivation()
598 {
599 m_view->setItemLayout(KFileItemListView::IconsLayout);
600
601 // Make sure that we have a large window, such that
602 // the items are visible and clickable.
603 adjustGeometryForColumnCount(5);
604
605 // Make sure that the first item is visible in the view.
606 m_view->setScrollOffset(0);
607 QCOMPARE(m_view->firstVisibleIndex(), 0);
608
609 const QPointF pos = m_view->itemContextRect(0).center();
610
611 // Save the "single click" setting.
612 const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
613
614 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
615 mousePressEvent.setPos(pos);
616 mousePressEvent.setButton(Qt::LeftButton);
617 mousePressEvent.setButtons(Qt::LeftButton);
618
619 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
620 mouseReleaseEvent.setPos(pos);
621 mouseReleaseEvent.setButton(Qt::LeftButton);
622 mouseReleaseEvent.setButtons(Qt::NoButton);
623
624 QGraphicsSceneMouseEvent mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
625 mouseDoubleClickEvent.setPos(pos);
626 mouseDoubleClickEvent.setButton(Qt::LeftButton);
627 mouseDoubleClickEvent.setButtons(Qt::LeftButton);
628
629 QGraphicsSceneMouseEvent mouseRightPressEvent(QEvent::GraphicsSceneMousePress);
630 mouseRightPressEvent.setPos(pos);
631 mouseRightPressEvent.setButton(Qt::RightButton);
632 mouseRightPressEvent.setButtons(Qt::RightButton);
633
634 QGraphicsSceneMouseEvent mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease);
635 mouseRightReleaseEvent.setPos(pos);
636 mouseRightReleaseEvent.setButton(Qt::RightButton);
637 mouseRightReleaseEvent.setButtons(Qt::NoButton);
638
639 QGraphicsSceneMouseEvent mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
640 mouseRightDoubleClickEvent.setPos(pos);
641 mouseRightDoubleClickEvent.setButton(Qt::RightButton);
642 mouseRightDoubleClickEvent.setButtons(Qt::RightButton);
643
644 QGraphicsSceneMouseEvent mouseBackPressEvent(QEvent::GraphicsSceneMousePress);
645 mouseBackPressEvent.setPos(pos);
646 mouseBackPressEvent.setButton(Qt::BackButton);
647 mouseBackPressEvent.setButtons(Qt::BackButton);
648
649 QGraphicsSceneMouseEvent mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease);
650 mouseBackReleaseEvent.setPos(pos);
651 mouseBackReleaseEvent.setButton(Qt::BackButton);
652 mouseBackReleaseEvent.setButtons(Qt::NoButton);
653
654 QGraphicsSceneMouseEvent mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick);
655 mouseBackDoubleClickEvent.setPos(pos);
656 mouseBackDoubleClickEvent.setButton(Qt::BackButton);
657 mouseBackDoubleClickEvent.setButtons(Qt::BackButton);
658
659 QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
660
661 // Default setting: single click activation.
662 m_testStyle->setActivateItemOnSingleClick(true);
663 m_view->event(&mousePressEvent);
664 m_view->event(&mouseReleaseEvent);
665 QCOMPARE(spyItemActivated.count(), 1);
666 spyItemActivated.clear();
667 QVERIFY2(!m_view->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
668
669 // Set the global setting to "double click activation".
670 m_testStyle->setActivateItemOnSingleClick(false);
671 m_view->event(&mousePressEvent);
672 m_view->event(&mouseReleaseEvent);
673 QCOMPARE(spyItemActivated.count(), 0);
674 spyItemActivated.clear();
675 QVERIFY(m_view->controller()->selectionManager()->hasSelection());
676
677 // emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
678 m_view->event(&mousePressEvent);
679 m_view->event(&mouseReleaseEvent);
680 m_view->event(&mouseDoubleClickEvent);
681 m_view->event(&mouseReleaseEvent);
682 QCOMPARE(spyItemActivated.count(), 1);
683 spyItemActivated.clear();
684 QVERIFY2(!m_view->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
685
686 // right mouse button should not trigger activation
687 m_view->event(&mouseRightPressEvent);
688 m_view->event(&mouseRightReleaseEvent);
689 m_view->event(&mouseRightDoubleClickEvent);
690 m_view->event(&mouseRightReleaseEvent);
691 QCOMPARE(spyItemActivated.count(), 0);
692
693 // back mouse button should not trigger activation
694 m_view->event(&mouseBackPressEvent);
695 m_view->event(&mouseBackReleaseEvent);
696 m_view->event(&mouseBackDoubleClickEvent);
697 m_view->event(&mouseBackReleaseEvent);
698 QCOMPARE(spyItemActivated.count(), 0);
699
700 // Enforce single click activation in the controller.
701 m_controller->setSingleClickActivationEnforced(true);
702 m_view->event(&mousePressEvent);
703 m_view->event(&mouseReleaseEvent);
704 QCOMPARE(spyItemActivated.count(), 1);
705 spyItemActivated.clear();
706 constexpr const char *reasonWhySelectionShouldPersist = "An item was selected before this mouse click. The click should not have cleared this selection.";
707 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
708
709 // Do not enforce single click activation in the controller.
710 m_controller->setSingleClickActivationEnforced(false);
711 m_view->event(&mousePressEvent);
712 m_view->event(&mouseReleaseEvent);
713 QCOMPARE(spyItemActivated.count(), 0);
714 spyItemActivated.clear();
715 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
716
717 // Set the global setting back to "single click activation".
718 m_testStyle->setActivateItemOnSingleClick(true);
719 m_view->event(&mousePressEvent);
720 m_view->event(&mouseReleaseEvent);
721 QCOMPARE(spyItemActivated.count(), 1);
722 spyItemActivated.clear();
723 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
724
725 // Enforce single click activation in the controller.
726 m_controller->setSingleClickActivationEnforced(true);
727 m_view->event(&mousePressEvent);
728 m_view->event(&mouseReleaseEvent);
729 QCOMPARE(spyItemActivated.count(), 1);
730 spyItemActivated.clear();
731 QVERIFY2(m_view->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist);
732
733 // Restore previous settings.
734 m_controller->setSingleClickActivationEnforced(true);
735 m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
736 }
737
738 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
739 {
740 const QSize size = m_view->itemSize().toSize();
741
742 QRect rect = m_container->geometry();
743 rect.setSize(size * count);
744 m_container->setGeometry(rect);
745
746 // Increase the size of the container until the correct column count is reached.
747 while (m_view->m_layouter->m_columnCount < count) {
748 rect = m_container->geometry();
749 rect.setSize(rect.size() + size);
750 m_container->setGeometry(rect);
751 }
752 }
753
754 QTEST_MAIN(KItemListControllerTest)
755
756 #include "kitemlistcontrollertest.moc"