2 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
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"
15 #include <QGraphicsSceneMouseEvent>
16 #include <QProxyStyle>
18 #include <QStandardPaths>
22 * \class KItemListControllerTestStyle is a proxy style for testing the
23 * KItemListController with different style hint options, e.g. single/double
26 class KItemListControllerTestStyle
: public QProxyStyle
30 KItemListControllerTestStyle(QStyle
*style
)
32 , m_activateItemOnSingleClick((bool)style
->styleHint(SH_ItemView_ActivateItemOnSingleClick
))
36 void setActivateItemOnSingleClick(bool activateItemOnSingleClick
)
38 m_activateItemOnSingleClick
= activateItemOnSingleClick
;
41 bool activateItemOnSingleClick() const
43 return m_activateItemOnSingleClick
;
46 int styleHint(StyleHint hint
, const QStyleOption
*option
= nullptr, const QWidget
*widget
= nullptr, QStyleHintReturn
*returnData
= nullptr) const override
49 case QStyle::SH_ItemView_ActivateItemOnSingleClick
:
50 return (int)activateItemOnSingleClick();
52 return QProxyStyle::styleHint(hint
, option
, widget
, returnData
);
57 bool m_activateItemOnSingleClick
;
60 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
)
61 Q_DECLARE_METATYPE(Qt::Orientation
)
62 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
)
63 Q_DECLARE_METATYPE(KItemSet
)
65 class KItemListControllerTest
: public QObject
71 void cleanupTestCase();
76 void testKeyboardNavigation_data();
77 void testKeyboardNavigation();
78 void testMouseClickActivation();
82 * Make sure that the number of columns in the view is equal to \a count
83 * by changing the geometry of the container.
85 void adjustGeometryForColumnCount(int count
);
88 KFileItemListView
*m_view
;
89 KItemListController
*m_controller
;
90 KItemListSelectionManager
*m_selectionManager
;
91 KFileItemModel
*m_model
;
93 KItemListContainer
*m_container
;
94 KItemListControllerTestStyle
*m_testStyle
;
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.
102 void KItemListControllerTest::initTestCase()
104 QStandardPaths::setTestModeEnabled(true);
105 qRegisterMetaType
<KItemSet
>("KItemSet");
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
);
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
);
143 m_testDir
->createFiles(files
);
144 m_model
->loadDirectory(m_testDir
->url());
145 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, &KFileItemModel::directoryLoadingCompleted
);
146 QVERIFY(spyDirectoryLoadingCompleted
.wait());
149 QVERIFY(QTest::qWaitForWindowExposed(m_container
));
152 void KItemListControllerTest::cleanupTestCase()
155 m_container
= nullptr;
161 /** Before each test, the current item, selection, and item size are reset to the defaults. */
162 void KItemListControllerTest::init()
164 m_selectionManager
->setCurrentItem(0);
165 QCOMPARE(m_selectionManager
->currentItem(), 0);
167 m_selectionManager
->clearSelection();
168 QVERIFY(!m_selectionManager
->hasSelection());
170 const QSizeF
itemSize(50, 50);
171 m_view
->setItemSize(itemSize
);
172 QCOMPARE(m_view
->itemSize(), itemSize
);
175 void KItemListControllerTest::cleanup()
177 m_controller
->setSelectionModeEnabled(false);
181 * \class KeyPress is a small helper struct that represents a key press event,
182 * including the key and the keyboard modifiers.
185 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
)
187 , m_modifier(modifier
)
192 Qt::KeyboardModifiers m_modifier
;
196 * \class ViewState is a small helper struct that represents a certain state
197 * of the view, including the current item, the selected items in MultiSelection
198 * mode (in the other modes, the selection is either empty or equal to the
199 * current item), and the information whether items were activated by the last
203 ViewState(int current
, const KItemSet
&selection
, bool activated
= false)
205 , m_selection(selection
)
206 , m_activated(activated
)
211 KItemSet m_selection
;
215 // We have to define a typedef for the pair in order to make the test compile.
216 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
217 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>)
220 * This function provides the data for the actual test function
221 * KItemListControllerTest::testKeyboardNavigation().
222 * It tests all possible combinations of view layouts, selection behaviors,
223 * and enabled/disabled groupings for different column counts, and
224 * provides a list of key presses and the states that the view should be in
225 * after the key press event.
227 void KItemListControllerTest::testKeyboardNavigation_data()
229 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
230 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
231 QTest::addColumn
<int>("columnCount");
232 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior"); // Defines how many items can be selected at the same time.
233 QTest::addColumn
<bool>("groupingEnabled");
234 QTest::addColumn
<Qt::LayoutDirection
>("layoutDirection");
235 QTest::addColumn
<bool>(
236 "selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
237 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
>>>("testList");
239 QList
<KFileItemListView::ItemLayout
> layoutList
;
240 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
241 layoutList
.append(KFileItemListView::IconsLayout
);
242 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
243 layoutList
.append(KFileItemListView::CompactLayout
);
244 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
245 layoutList
.append(KFileItemListView::DetailsLayout
);
246 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
248 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
249 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
250 selectionBehaviorList
.append(KItemListController::NoSelection
);
251 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
252 selectionBehaviorList
.append(KItemListController::SingleSelection
);
253 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
254 selectionBehaviorList
.append(KItemListController::MultiSelection
);
255 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
257 QList
<bool> groupingEnabledList
;
258 QHash
<bool, QString
> groupingEnabledNames
;
259 groupingEnabledList
.append(false);
260 groupingEnabledNames
[false] = "ungrouped";
261 groupingEnabledList
.append(true);
262 groupingEnabledNames
[true] = "grouping enabled";
264 QList
<Qt::LayoutDirection
> layoutDirectionList
;
265 QHash
<Qt::LayoutDirection
, QString
> layoutDirectionNames
;
266 layoutDirectionList
.append(Qt::LeftToRight
);
267 layoutDirectionNames
[Qt::LeftToRight
] = "Left-to-Right LayoutDirection";
268 layoutDirectionList
.append(Qt::RightToLeft
);
269 layoutDirectionNames
[Qt::RightToLeft
] = "Right-to-Left LayoutDirection";
271 bool selectionModeEnabled
= false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
272 // We only enable it for a few separate tests at the end.
274 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
275 // The following settings depend on the layout.
276 // Note that 'columns' are actually 'rows' in
278 Qt::Orientation scrollOrientation
;
279 QList
<int> columnCountList
;
281 Qt::Key previousItemKey
;
283 Qt::Key previousRowKey
;
286 case KFileItemListView::IconsLayout
:
287 scrollOrientation
= Qt::Vertical
;
288 columnCountList
<< 1 << 3 << 5;
289 nextItemKey
= Qt::Key_Right
;
290 previousItemKey
= Qt::Key_Left
;
291 nextRowKey
= Qt::Key_Down
;
292 previousRowKey
= Qt::Key_Up
;
294 case KFileItemListView::CompactLayout
:
295 scrollOrientation
= Qt::Horizontal
;
296 columnCountList
<< 1 << 3 << 5;
297 nextItemKey
= Qt::Key_Down
;
298 previousItemKey
= Qt::Key_Up
;
299 nextRowKey
= Qt::Key_Right
;
300 previousRowKey
= Qt::Key_Left
;
302 case KFileItemListView::DetailsLayout
:
303 scrollOrientation
= Qt::Vertical
;
304 columnCountList
<< 1;
305 nextItemKey
= Qt::Key_Down
;
306 previousItemKey
= Qt::Key_Up
;
307 nextRowKey
= Qt::Key_Down
;
308 previousRowKey
= Qt::Key_Up
;
311 for (auto layoutDirection
: std::as_const(layoutDirectionList
)) {
312 if (layoutDirection
== Qt::RightToLeft
) {
314 case KFileItemListView::IconsLayout
:
315 std::swap(nextItemKey
, previousItemKey
);
317 case KFileItemListView::CompactLayout
:
318 std::swap(nextRowKey
, previousRowKey
);
324 for (int columnCount
: std::as_const(columnCountList
)) {
325 for (const KItemListController::SelectionBehavior
&selectionBehavior
: std::as_const(selectionBehaviorList
)) {
326 for (bool groupingEnabled
: std::as_const(groupingEnabledList
)) {
327 QList
<QPair
<KeyPress
, ViewState
>> testList
;
329 // First, key presses which should have the same effect
330 // for any layout and any number of columns.
331 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
332 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
333 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
334 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
335 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
336 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
337 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
338 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
339 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
340 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
341 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
342 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
343 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
344 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
345 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
346 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
347 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
348 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
349 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
350 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
351 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
352 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
353 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
354 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
355 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
356 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
357 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
358 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
359 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
360 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
361 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
362 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
363 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
364 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
366 // Next, we test combinations of key presses which only work for a
367 // particular number of columns and either enabled or disabled grouping.
370 if (columnCount
== 1) {
371 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
372 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
373 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
374 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
375 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
376 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
379 // Multiple columns: we test both 3 and 5 columns with grouping
380 // enabled or disabled. For each case, the layout of the items
381 // in the view is shown (both using file names and indices) to
382 // make it easier to understand what the tests do.
384 if (columnCount
== 3 && !groupingEnabled
) {
385 // 3 columns, no grouping:
390 // d1 d2 d3 | 9 10 11
391 // d4 e1 e2 | 12 13 14
392 // e3 e4 e5 | 15 16 17
394 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
395 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
396 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
397 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
398 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
399 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
400 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
401 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
402 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
403 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
404 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
405 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
406 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
407 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
408 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
409 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
410 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
411 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
412 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
413 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
416 if (columnCount
== 5 && !groupingEnabled
) {
417 // 5 columns, no grouping:
419 // a1 a2 a3 b1 c1 | 0 1 2 3 4
420 // c2 c3 c4 c5 d1 | 5 6 7 8 9
421 // d2 d3 d4 e1 e2 | 10 11 12 13 14
422 // e3 e4 e5 e6 e7 | 15 16 17 18 19
423 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
424 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
425 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
426 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
427 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
428 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
429 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
430 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
431 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
432 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
433 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
436 if (columnCount
== 3 && groupingEnabled
) {
437 // 3 columns, with grouping:
443 // d1 d2 d3 | 9 10 11
445 // e1 e2 e3 | 13 14 15
446 // e4 e5 e6 | 16 17 18
448 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
449 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
450 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
451 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
452 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
453 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
454 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
455 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
456 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
457 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
458 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
459 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
460 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
463 if (columnCount
== 5 && groupingEnabled
) {
464 // 5 columns, with grouping:
468 // c1 c2 c3 c4 c5 | 4 5 6 7 8
469 // d1 d2 d3 d4 | 9 10 11 12
470 // e1 e2 e3 e4 e5 | 13 14 15 16 17
472 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
473 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
474 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
475 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
476 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
477 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
478 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
479 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
480 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
481 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
482 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
483 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
484 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
487 const QString testName
= layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
488 + selectionBehaviorNames
[selectionBehavior
] + ", " + groupingEnabledNames
[groupingEnabled
] + ", "
489 + layoutDirectionNames
[layoutDirection
];
491 const QByteArray testNameAscii
= testName
.toLatin1();
493 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
494 << layoutDirection
<< selectionModeEnabled
<< testList
;
502 * Selection mode tests
503 * We only test for the default icon view mode with typical scrollOrientation, selectionBehaviour, no grouping, left-to-right layoutDirection because none
504 * 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
507 selectionModeEnabled
= true;
508 const KFileItemListView::ItemLayout layout
= KFileItemListView::IconsLayout
;
509 const Qt::Orientation scrollOrientation
= Qt::Vertical
;
510 const int columnCount
= 3;
511 const Qt::Key nextItemKey
= Qt::Key_Right
;
512 const Qt::Key previousItemKey
= Qt::Key_Left
;
513 const Qt::Key nextRowKey
= Qt::Key_Down
;
514 const Qt::Key previousRowKey
= Qt::Key_Up
;
516 const Qt::LayoutDirection layoutDirection
= Qt::LeftToRight
;
517 const KItemListController::SelectionBehavior
&selectionBehavior
= KItemListController::MultiSelection
;
518 const bool groupingEnabled
= false;
520 QList
<QPair
<KeyPress
, ViewState
>> testList
;
522 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet())) // In selection mode nothing is selected simply by moving with arrow keys.
523 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1)) // Pressing Return toggles the selection but does not activate.
524 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet())) // Pressing Enter toggles the selection but does not activate.
525 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet()))
526 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3)) // Shift+Arrow key still selects in selection mode.
527 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2))
528 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2 << 3))
529 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2)) // Shift+Left and then Shift+Right cancel each other out.
530 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2))
531 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 4))
532 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 2 << 4))
533 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4))
534 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
535 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3 << 4))
536 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
537 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 0 << 1 << 2 << 3 << 4))
538 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
539 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
540 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
541 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
542 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
543 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
544 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // Space toggles selection in selection mode.
545 << qMakePair(KeyPress(Qt::Key_D
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // No selection change by type-ahead.
546 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // Space is not added to type-ahead.
547 << qMakePair(KeyPress(Qt::Key_4
), ViewState(12, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // No selection change by type-ahead.
548 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
550 // The following tests assume a columnCount of three and no grouping enabled.
551 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
552 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
553 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
554 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
555 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
556 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
557 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
558 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
559 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 9 << 18 << 19))
560 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
561 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
562 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
563 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
564 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
565 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
566 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
567 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
568 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
569 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
570 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19));
572 const QString testName
= "Selection Mode: " + layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
573 + selectionBehaviorNames
[selectionBehavior
] + ", " + groupingEnabledNames
[groupingEnabled
] + ", " + layoutDirectionNames
[layoutDirection
];
575 const QByteArray testNameAscii
= testName
.toLatin1();
577 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
<< layoutDirection
578 << selectionModeEnabled
<< testList
;
582 * This function sets the view's properties according to the data provided by
583 * KItemListControllerTest::testKeyboardNavigation_data().
585 * The list \a testList contains pairs of key presses, which are sent to the
586 * container, and expected view states, which are verified then.
588 void KItemListControllerTest::testKeyboardNavigation()
590 QFETCH(KFileItemListView::ItemLayout
, layout
);
591 QFETCH(Qt::Orientation
, scrollOrientation
);
592 QFETCH(int, columnCount
);
593 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
594 QFETCH(bool, groupingEnabled
);
595 QFETCH(Qt::LayoutDirection
, layoutDirection
);
596 QFETCH(bool, selectionModeEnabled
);
597 QFETCH(QList
<keyPressViewStatePair
>, testList
);
599 QApplication::setLayoutDirection(layoutDirection
);
600 m_view
->setLayoutDirection(layoutDirection
);
602 m_view
->setItemLayout(layout
);
603 QCOMPARE(m_view
->itemLayout(), layout
);
605 m_view
->setScrollOrientation(scrollOrientation
);
606 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
608 m_controller
->setSelectionBehavior(selectionBehavior
);
609 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
611 m_model
->setGroupedSorting(groupingEnabled
);
612 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
614 m_controller
->setSelectionModeEnabled(selectionModeEnabled
);
615 QCOMPARE(m_controller
->selectionMode(), selectionModeEnabled
);
617 adjustGeometryForColumnCount(columnCount
);
618 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
620 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
621 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
624 while (!testList
.isEmpty()) {
626 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
627 const Qt::Key key
= test
.first
.m_key
;
628 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
629 const int current
= test
.second
.m_current
;
630 const KItemSet selection
= test
.second
.m_selection
;
631 const bool activated
= test
.second
.m_activated
;
633 QTest::keyClick(m_container
, key
, modifier
);
636 m_selectionManager
->currentItem() == current
,
637 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
638 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigation_data().")
639 .arg(m_selectionManager
->currentItem())
641 .arg(QKeySequence(key
).toString())
643 switch (selectionBehavior
) {
644 case KItemListController::NoSelection
:
645 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
647 case KItemListController::SingleSelection
:
648 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
);
650 case KItemListController::MultiSelection
:
651 QCOMPARE(m_selectionManager
->selectedItems(), selection
);
656 switch (selectionBehavior
) {
657 case KItemListController::MultiSelection
:
658 if (!selection
.isEmpty()) {
659 // The selected items should be activated.
660 if (selection
.count() == 1) {
661 QVERIFY(!spySingleItemActivated
.isEmpty());
662 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
663 QVERIFY(spyMultipleItemsActivated
.isEmpty());
665 QVERIFY(spySingleItemActivated
.isEmpty());
666 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
667 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
671 // No items are selected. Therefore, the current item should be activated.
672 // This is handled by falling through to the NoSelection/SingleSelection case.
674 case KItemListController::NoSelection
:
675 case KItemListController::SingleSelection
:
676 // In NoSelection and SingleSelection mode, the current item should be activated.
677 QVERIFY(!spySingleItemActivated
.isEmpty());
678 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
679 QVERIFY(spyMultipleItemsActivated
.isEmpty());
686 void KItemListControllerTest::testMouseClickActivation()
688 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
690 // Make sure that we have a large window, such that
691 // the items are visible and clickable.
692 adjustGeometryForColumnCount(5);
694 // Make sure that the first item is visible in the view.
695 m_view
->setScrollOffset(0);
696 QCOMPARE(m_view
->firstVisibleIndex(), 0);
698 const QPointF pos
= m_view
->itemContextRect(0).center();
700 // Save the "single click" setting.
701 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
703 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
704 mousePressEvent
.setPos(pos
);
705 mousePressEvent
.setButton(Qt::LeftButton
);
706 mousePressEvent
.setButtons(Qt::LeftButton
);
708 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
709 mouseReleaseEvent
.setPos(pos
);
710 mouseReleaseEvent
.setButton(Qt::LeftButton
);
711 mouseReleaseEvent
.setButtons(Qt::NoButton
);
713 QGraphicsSceneMouseEvent
mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
714 mouseDoubleClickEvent
.setPos(pos
);
715 mouseDoubleClickEvent
.setButton(Qt::LeftButton
);
716 mouseDoubleClickEvent
.setButtons(Qt::LeftButton
);
718 QGraphicsSceneMouseEvent
mouseRightPressEvent(QEvent::GraphicsSceneMousePress
);
719 mouseRightPressEvent
.setPos(pos
);
720 mouseRightPressEvent
.setButton(Qt::RightButton
);
721 mouseRightPressEvent
.setButtons(Qt::RightButton
);
723 QGraphicsSceneMouseEvent
mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
724 mouseRightReleaseEvent
.setPos(pos
);
725 mouseRightReleaseEvent
.setButton(Qt::RightButton
);
726 mouseRightReleaseEvent
.setButtons(Qt::NoButton
);
728 QGraphicsSceneMouseEvent
mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
729 mouseRightDoubleClickEvent
.setPos(pos
);
730 mouseRightDoubleClickEvent
.setButton(Qt::RightButton
);
731 mouseRightDoubleClickEvent
.setButtons(Qt::RightButton
);
733 QGraphicsSceneMouseEvent
mouseBackPressEvent(QEvent::GraphicsSceneMousePress
);
734 mouseBackPressEvent
.setPos(pos
);
735 mouseBackPressEvent
.setButton(Qt::BackButton
);
736 mouseBackPressEvent
.setButtons(Qt::BackButton
);
738 QGraphicsSceneMouseEvent
mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
739 mouseBackReleaseEvent
.setPos(pos
);
740 mouseBackReleaseEvent
.setButton(Qt::BackButton
);
741 mouseBackReleaseEvent
.setButtons(Qt::NoButton
);
743 QGraphicsSceneMouseEvent
mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
744 mouseBackDoubleClickEvent
.setPos(pos
);
745 mouseBackDoubleClickEvent
.setButton(Qt::BackButton
);
746 mouseBackDoubleClickEvent
.setButtons(Qt::BackButton
);
748 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
750 // Default setting: single click activation.
751 m_testStyle
->setActivateItemOnSingleClick(true);
752 m_view
->event(&mousePressEvent
);
753 m_view
->event(&mouseReleaseEvent
);
754 QCOMPARE(spyItemActivated
.count(), 1);
755 spyItemActivated
.clear();
756 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
758 // Set the global setting to "double click activation".
759 m_testStyle
->setActivateItemOnSingleClick(false);
760 m_view
->event(&mousePressEvent
);
761 m_view
->event(&mouseReleaseEvent
);
762 QCOMPARE(spyItemActivated
.count(), 0);
763 spyItemActivated
.clear();
764 QVERIFY(m_view
->controller()->selectionManager()->hasSelection());
766 // emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
767 m_view
->event(&mousePressEvent
);
768 m_view
->event(&mouseReleaseEvent
);
769 m_view
->event(&mouseDoubleClickEvent
);
770 m_view
->event(&mouseReleaseEvent
);
771 QCOMPARE(spyItemActivated
.count(), 1);
772 spyItemActivated
.clear();
773 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
775 // right mouse button should not trigger activation
776 m_view
->event(&mouseRightPressEvent
);
777 m_view
->event(&mouseRightReleaseEvent
);
778 m_view
->event(&mouseRightDoubleClickEvent
);
779 m_view
->event(&mouseRightReleaseEvent
);
780 QCOMPARE(spyItemActivated
.count(), 0);
782 // back mouse button should not trigger activation
783 m_view
->event(&mouseBackPressEvent
);
784 m_view
->event(&mouseBackReleaseEvent
);
785 m_view
->event(&mouseBackDoubleClickEvent
);
786 m_view
->event(&mouseBackReleaseEvent
);
787 QCOMPARE(spyItemActivated
.count(), 0);
789 // Enforce single click activation in the controller.
790 m_controller
->setSingleClickActivationEnforced(true);
791 m_view
->event(&mousePressEvent
);
792 m_view
->event(&mouseReleaseEvent
);
793 QCOMPARE(spyItemActivated
.count(), 1);
794 spyItemActivated
.clear();
795 constexpr const char *reasonWhySelectionShouldPersist
= "An item was selected before this mouse click. The click should not have cleared this selection.";
796 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
798 // Do not enforce single click activation in the controller.
799 m_controller
->setSingleClickActivationEnforced(false);
800 m_view
->event(&mousePressEvent
);
801 m_view
->event(&mouseReleaseEvent
);
802 QCOMPARE(spyItemActivated
.count(), 0);
803 spyItemActivated
.clear();
804 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
806 // Set the global setting back to "single click activation".
807 m_testStyle
->setActivateItemOnSingleClick(true);
808 m_view
->event(&mousePressEvent
);
809 m_view
->event(&mouseReleaseEvent
);
810 QCOMPARE(spyItemActivated
.count(), 1);
811 spyItemActivated
.clear();
812 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
814 // Enforce single click activation in the controller.
815 m_controller
->setSingleClickActivationEnforced(true);
816 m_view
->event(&mousePressEvent
);
817 m_view
->event(&mouseReleaseEvent
);
818 QCOMPARE(spyItemActivated
.count(), 1);
819 spyItemActivated
.clear();
820 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
822 // Restore previous settings.
823 m_controller
->setSingleClickActivationEnforced(true);
824 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
827 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
829 const QSize size
= m_view
->itemSize().toSize();
831 QRect rect
= m_container
->geometry();
832 rect
.setSize(size
* count
);
833 m_container
->setGeometry(rect
);
835 // Increase the size of the container until the correct column count is reached.
836 while (m_view
->m_layouter
->m_columnCount
< count
) {
837 rect
= m_container
->geometry();
838 rect
.setSize(rect
.size() + size
);
839 m_container
->setGeometry(rect
);
843 QTEST_MAIN(KItemListControllerTest
)
845 #include "kitemlistcontrollertest.moc"