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 testKeyboardNavigationMultiSelection_data();
77 void testKeyboardNavigationMultiSelection();
78 void testKeyboardNavigationSingleSelectionNoSelection_data();
79 void testKeyboardNavigationSingleSelectionNoSelection();
80 void testMouseClickActivation();
84 * Make sure that the number of columns in the view is equal to \a count
85 * by changing the geometry of the container.
87 void adjustGeometryForColumnCount(int count
);
90 KFileItemListView
*m_view
;
91 KItemListController
*m_controller
;
92 KItemListSelectionManager
*m_selectionManager
;
93 KFileItemModel
*m_model
;
95 KItemListContainer
*m_container
;
96 KItemListControllerTestStyle
*m_testStyle
;
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.
104 void KItemListControllerTest::initTestCase()
106 QStandardPaths::setTestModeEnabled(true);
107 qRegisterMetaType
<KItemSet
>("KItemSet");
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
);
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
);
145 m_testDir
->createFiles(files
);
146 m_model
->loadDirectory(m_testDir
->url());
147 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, &KFileItemModel::directoryLoadingCompleted
);
148 QVERIFY(spyDirectoryLoadingCompleted
.wait());
151 QVERIFY(QTest::qWaitForWindowExposed(m_container
));
154 void KItemListControllerTest::cleanupTestCase()
157 m_container
= nullptr;
163 /** Before each test, the current item, selection, and item size are reset to the defaults. */
164 void KItemListControllerTest::init()
166 m_selectionManager
->setCurrentItem(0);
167 QCOMPARE(m_selectionManager
->currentItem(), 0);
169 m_selectionManager
->clearSelection();
170 QVERIFY(!m_selectionManager
->hasSelection());
172 const QSizeF
itemSize(50, 50);
173 m_view
->setItemSize(itemSize
);
174 QCOMPARE(m_view
->itemSize(), itemSize
);
176 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
177 QCOMPARE(m_controller
->selectionBehavior(), KItemListController::MultiSelection
);
180 void KItemListControllerTest::cleanup()
182 m_controller
->setSelectionModeEnabled(false);
186 * \class KeyPress is a small helper struct that represents a key press event,
187 * including the key and the keyboard modifiers.
190 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
)
192 , m_modifier(modifier
)
197 Qt::KeyboardModifiers m_modifier
;
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
208 ViewState(int current
, const KItemSet
&selection
, bool activated
= false)
210 , m_selection(selection
)
211 , m_activated(activated
)
216 KItemSet m_selection
;
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
>)
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.
230 void KItemListControllerTest::testKeyboardNavigationMultiSelection_data()
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");
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";
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";
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";
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.
267 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
268 // The following settings depend on the layout.
269 // Note that 'columns' are actually 'rows' in
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
;
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
;
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
;
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
;
304 for (auto layoutDirection
: std::as_const(layoutDirectionList
)) {
305 if (layoutDirection
== Qt::RightToLeft
) {
307 case KFileItemListView::IconsLayout
:
308 std::swap(nextItemKey
, previousItemKey
);
310 case KFileItemListView::CompactLayout
:
311 std::swap(nextRowKey
, previousRowKey
);
317 for (int columnCount
: std::as_const(columnCountList
)) {
318 for (bool groupingEnabled
: std::as_const(groupingEnabledList
)) {
319 QList
<QPair
<KeyPress
, ViewState
>> testList
;
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()));
359 // Next, we test combinations of key presses which only work for a
360 // particular number of columns and either enabled or disabled grouping.
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));
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.
377 if (columnCount
== 3 && !groupingEnabled
) {
378 // 3 columns, no grouping:
383 // d1 d2 d3 | 9 10 11
384 // d4 e1 e2 | 12 13 14
385 // e3 e4 e5 | 15 16 17
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));
409 if (columnCount
== 5 && !groupingEnabled
) {
410 // 5 columns, no grouping:
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));
429 if (columnCount
== 3 && groupingEnabled
) {
430 // 3 columns, with grouping:
436 // d1 d2 d3 | 9 10 11
438 // e1 e2 e3 | 13 14 15
439 // e4 e5 e6 | 16 17 18
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));
456 if (columnCount
== 5 && groupingEnabled
) {
457 // 5 columns, with grouping:
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
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));
480 const QString testName
= layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
481 + groupingEnabledNames
[groupingEnabled
] + ", " + layoutDirectionNames
[layoutDirection
];
483 const QByteArray testNameAscii
= testName
.toLatin1();
485 QTest::newRow(testNameAscii
.data())
486 << layout
<< scrollOrientation
<< columnCount
<< groupingEnabled
<< layoutDirection
<< selectionModeEnabled
<< testList
;
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
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
;
507 const Qt::LayoutDirection layoutDirection
= Qt::LeftToRight
;
508 const KItemListController::SelectionBehavior
&selectionBehavior
= KItemListController::MultiSelection
;
509 const bool groupingEnabled
= false;
511 QList
<QPair
<KeyPress
, ViewState
>> testList
;
513 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet())) // In selection mode nothing is selected simply by moving with arrow keys.
514 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1)) // Pressing Return toggles the selection but does not activate.
515 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet())) // Pressing Enter toggles the selection but does not activate.
516 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet()))
517 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3)) // Shift+Arrow key still selects in selection mode.
518 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2))
519 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2 << 3))
520 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2)) // Shift+Left and then Shift+Right cancel each other out.
521 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2))
522 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 4))
523 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 2 << 4))
524 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4))
525 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
526 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3 << 4))
527 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3 << 4))
528 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 0 << 1 << 2 << 3 << 4))
529 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
530 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
531 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
532 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
533 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19))
534 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3 << 4 << 18 << 19))
535 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // Space toggles selection in selection mode.
536 << qMakePair(KeyPress(Qt::Key_D
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 18 << 19)) // No selection change by type-ahead.
537 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // Space is not added to type-ahead.
538 << qMakePair(KeyPress(Qt::Key_4
), ViewState(12, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19)) // No selection change by type-ahead.
539 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
541 // The following tests assume a columnCount of three and no grouping enabled.
542 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
543 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
544 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
545 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
546 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
547 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 1 << 2 << 3 << 4 << 7 << 8 << 9 << 18 << 19))
548 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
549 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
550 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 9 << 18 << 19))
551 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 1 << 2 << 3 << 4 << 6 << 7 << 9 << 18 << 19))
552 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
553 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
554 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
555 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
556 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
557 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
558 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
559 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
560 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19))
561 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 1 << 2 << 3 << 4 << 9 << 18 << 19));
563 const QString testName
= "Selection Mode: " + layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
564 + groupingEnabledNames
[groupingEnabled
] + ", " + layoutDirectionNames
[layoutDirection
];
566 const QByteArray testNameAscii
= testName
.toLatin1();
568 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< groupingEnabled
<< layoutDirection
<< selectionModeEnabled
<< testList
;
572 * This function sets the view's properties according to the data provided.
574 * The list \a testList contains pairs of key presses, which are sent to the
575 * container, and expected view states, which are verified then.
577 void KItemListControllerTest::testKeyboardNavigationMultiSelection()
579 QFETCH(KFileItemListView::ItemLayout
, layout
);
580 QFETCH(Qt::Orientation
, scrollOrientation
);
581 QFETCH(int, columnCount
);
582 QFETCH(bool, groupingEnabled
);
583 QFETCH(Qt::LayoutDirection
, layoutDirection
);
584 QFETCH(bool, selectionModeEnabled
);
585 QFETCH(QList
<keyPressViewStatePair
>, testList
);
587 QApplication::setLayoutDirection(layoutDirection
);
588 m_view
->setLayoutDirection(layoutDirection
);
590 m_view
->setItemLayout(layout
);
591 QCOMPARE(m_view
->itemLayout(), layout
);
593 m_view
->setScrollOrientation(scrollOrientation
);
594 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
596 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
597 QCOMPARE(m_controller
->selectionBehavior(), KItemListController::MultiSelection
);
599 m_model
->setGroupedSorting(groupingEnabled
);
600 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
602 m_controller
->setSelectionModeEnabled(selectionModeEnabled
);
603 QCOMPARE(m_controller
->selectionMode(), selectionModeEnabled
);
605 adjustGeometryForColumnCount(columnCount
);
606 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
608 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
609 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
612 while (!testList
.isEmpty()) {
614 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
615 const Qt::Key key
= test
.first
.m_key
;
616 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
617 const int current
= test
.second
.m_current
;
618 const KItemSet selection
= test
.second
.m_selection
;
619 const bool activated
= test
.second
.m_activated
;
621 QTest::keyClick(m_container
, key
, modifier
);
624 m_selectionManager
->currentItem() == current
,
625 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
626 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationMultiSelection_data().")
627 .arg(m_selectionManager
->currentItem())
629 .arg(QKeySequence(key
).toString())
631 QCOMPARE(m_selectionManager
->selectedItems(), selection
);
634 if (!selection
.isEmpty()) {
635 // The selected items should be activated.
636 if (selection
.count() == 1) {
637 QVERIFY(!spySingleItemActivated
.isEmpty());
638 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
639 QVERIFY(spyMultipleItemsActivated
.isEmpty());
641 QVERIFY(spySingleItemActivated
.isEmpty());
642 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
643 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
646 QVERIFY(!spySingleItemActivated
.isEmpty());
647 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
648 QVERIFY(spyMultipleItemsActivated
.isEmpty());
655 * This function tests all possible combinations of view layouts, layout direction,
656 * and enabled/disabled groupings for different column counts, and
657 * provides a list of key presses and the states that the view should be in
658 * after the key press event.
660 void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data()
662 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
663 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
664 QTest::addColumn
<int>("columnCount");
665 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior"); // Defines how many items can be selected at the same time.
666 QTest::addColumn
<bool>("groupingEnabled");
667 QTest::addColumn
<Qt::LayoutDirection
>("layoutDirection");
668 QTest::addColumn
<bool>(
669 "selectionModeEnabled"); // Don't confuse this with "selectionBehaviour". This is about changing controls for users to help multi-selecting.
670 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
>>>("testList");
672 QList
<KFileItemListView::ItemLayout
> layoutList
;
673 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
674 layoutList
.append(KFileItemListView::IconsLayout
);
675 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
676 layoutList
.append(KFileItemListView::CompactLayout
);
677 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
678 layoutList
.append(KFileItemListView::DetailsLayout
);
679 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
681 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
682 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
683 selectionBehaviorList
.append(KItemListController::NoSelection
);
684 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
685 selectionBehaviorList
.append(KItemListController::SingleSelection
);
686 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
688 QList
<bool> groupingEnabledList
;
689 QHash
<bool, QString
> groupingEnabledNames
;
690 groupingEnabledList
.append(false);
691 groupingEnabledNames
[false] = "ungrouped";
692 groupingEnabledList
.append(true);
693 groupingEnabledNames
[true] = "grouping enabled";
695 QList
<Qt::LayoutDirection
> layoutDirectionList
;
696 QHash
<Qt::LayoutDirection
, QString
> layoutDirectionNames
;
697 layoutDirectionList
.append(Qt::LeftToRight
);
698 layoutDirectionNames
[Qt::LeftToRight
] = "Left-to-Right LayoutDirection";
699 layoutDirectionList
.append(Qt::RightToLeft
);
700 layoutDirectionNames
[Qt::RightToLeft
] = "Right-to-Left LayoutDirection";
702 bool selectionModeEnabled
= false; // For most tests this is kept disabled because it is not really affected by all the other test conditions.
703 // We only enable it for a few separate tests at the end.
705 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
706 // The following settings depend on the layout.
707 // Note that 'columns' are actually 'rows' in
709 Qt::Orientation scrollOrientation
;
710 QList
<int> columnCountList
;
711 Qt::Key nextItemKey
= Qt::Key_Right
;
712 Qt::Key previousItemKey
= Qt::Key_Right
;
713 Qt::Key nextRowKey
= Qt::Key_Right
;
714 Qt::Key previousRowKey
= Qt::Key_Right
;
717 case KFileItemListView::IconsLayout
:
718 scrollOrientation
= Qt::Vertical
;
719 columnCountList
<< 1 << 3 << 5;
720 nextItemKey
= Qt::Key_Right
;
721 previousItemKey
= Qt::Key_Left
;
722 nextRowKey
= Qt::Key_Down
;
723 previousRowKey
= Qt::Key_Up
;
725 case KFileItemListView::CompactLayout
:
726 scrollOrientation
= Qt::Horizontal
;
727 columnCountList
<< 1 << 3 << 5;
728 nextItemKey
= Qt::Key_Down
;
729 previousItemKey
= Qt::Key_Up
;
730 nextRowKey
= Qt::Key_Right
;
731 previousRowKey
= Qt::Key_Left
;
733 case KFileItemListView::DetailsLayout
:
734 scrollOrientation
= Qt::Vertical
;
735 columnCountList
<< 1;
736 nextItemKey
= Qt::Key_Down
;
737 previousItemKey
= Qt::Key_Up
;
738 nextRowKey
= Qt::Key_Down
;
739 previousRowKey
= Qt::Key_Up
;
742 for (auto layoutDirection
: std::as_const(layoutDirectionList
)) {
743 if (layoutDirection
== Qt::RightToLeft
) {
745 case KFileItemListView::IconsLayout
:
746 std::swap(nextItemKey
, previousItemKey
);
748 case KFileItemListView::CompactLayout
:
749 std::swap(nextRowKey
, previousRowKey
);
755 for (int columnCount
: std::as_const(columnCountList
)) {
756 for (const KItemListController::SelectionBehavior
&selectionBehavior
: std::as_const(selectionBehaviorList
)) {
757 for (bool groupingEnabled
: std::as_const(groupingEnabledList
)) {
758 QList
<QPair
<KeyPress
, ViewState
>> testList
;
760 // First, key presses which should have the same effect
761 // for any layout and any number of columns.
762 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
763 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
764 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
765 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
766 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
767 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
768 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
769 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
770 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
771 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
772 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
773 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
774 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
775 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
776 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
777 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
778 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
779 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
780 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
781 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
782 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
783 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
784 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
785 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
786 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
787 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
788 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
789 << qMakePair(KeyPress(Qt::Key_E
), ViewState(16, KItemSet() << 16))
790 << qMakePair(KeyPress(Qt::Key_E
), ViewState(17, KItemSet() << 17))
791 << qMakePair(KeyPress(previousItemKey
), ViewState(16, KItemSet() << 16))
792 << qMakePair(KeyPress(Qt::Key_E
), ViewState(17, KItemSet() << 17))
793 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(17, KItemSet()))
794 << qMakePair(KeyPress(Qt::Key_E
), ViewState(18, KItemSet() << 18))
795 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
796 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
798 // Next, we test combinations of key presses which only work for a
799 // particular number of columns and either enabled or disabled grouping.
802 if (columnCount
== 1) {
803 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
804 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
805 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
806 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
807 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
808 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
811 // Multiple columns: we test both 3 and 5 columns with grouping
812 // enabled or disabled. For each case, the layout of the items
813 // in the view is shown (both using file names and indices) to
814 // make it easier to understand what the tests do.
816 if (columnCount
== 3 && !groupingEnabled
) {
817 // 3 columns, no grouping:
822 // d1 d2 d3 | 9 10 11
823 // d4 e1 e2 | 12 13 14
824 // e3 e4 e5 | 15 16 17
826 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
827 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
828 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
829 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
830 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
831 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
832 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
833 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
834 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
835 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
836 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
837 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
838 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
839 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
840 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
841 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
842 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
843 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
844 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
845 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
848 if (columnCount
== 5 && !groupingEnabled
) {
849 // 5 columns, no grouping:
851 // a1 a2 a3 b1 c1 | 0 1 2 3 4
852 // c2 c3 c4 c5 d1 | 5 6 7 8 9
853 // d2 d3 d4 e1 e2 | 10 11 12 13 14
854 // e3 e4 e5 e6 e7 | 15 16 17 18 19
855 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
856 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
857 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
858 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
859 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
860 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
861 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
862 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
863 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
864 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
865 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
868 if (columnCount
== 3 && groupingEnabled
) {
869 // 3 columns, with grouping:
875 // d1 d2 d3 | 9 10 11
877 // e1 e2 e3 | 13 14 15
878 // e4 e5 e6 | 16 17 18
880 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
881 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
882 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
883 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
884 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
885 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
886 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
887 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
888 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
889 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
890 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
891 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
892 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
895 if (columnCount
== 5 && groupingEnabled
) {
896 // 5 columns, with grouping:
900 // c1 c2 c3 c4 c5 | 4 5 6 7 8
901 // d1 d2 d3 d4 | 9 10 11 12
902 // e1 e2 e3 e4 e5 | 13 14 15 16 17
904 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
905 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
906 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
907 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
908 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
909 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
910 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
911 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
912 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
913 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
914 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
915 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
916 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
919 const QString testName
= layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
920 + selectionBehaviorNames
[selectionBehavior
] + ", " + groupingEnabledNames
[groupingEnabled
] + ", "
921 + layoutDirectionNames
[layoutDirection
];
923 const QByteArray testNameAscii
= testName
.toLatin1();
925 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
926 << layoutDirection
<< selectionModeEnabled
<< testList
;
935 * This function sets the view's properties according to the data provided.
937 * The list \a testList contains pairs of key presses, which are sent to the
938 * container, and expected view states, which are verified then.
940 void KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection()
942 QFETCH(KFileItemListView::ItemLayout
, layout
);
943 QFETCH(Qt::Orientation
, scrollOrientation
);
944 QFETCH(int, columnCount
);
945 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
946 QFETCH(bool, groupingEnabled
);
947 QFETCH(Qt::LayoutDirection
, layoutDirection
);
948 QFETCH(bool, selectionModeEnabled
);
949 QFETCH(QList
<keyPressViewStatePair
>, testList
);
951 QApplication::setLayoutDirection(layoutDirection
);
952 m_view
->setLayoutDirection(layoutDirection
);
954 m_view
->setItemLayout(layout
);
955 QCOMPARE(m_view
->itemLayout(), layout
);
957 m_view
->setScrollOrientation(scrollOrientation
);
958 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
960 m_controller
->setSelectionBehavior(selectionBehavior
);
961 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
963 m_model
->setGroupedSorting(groupingEnabled
);
964 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
966 m_controller
->setSelectionModeEnabled(selectionModeEnabled
);
967 QCOMPARE(m_controller
->selectionMode(), selectionModeEnabled
);
969 adjustGeometryForColumnCount(columnCount
);
970 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
972 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
973 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
976 while (!testList
.isEmpty()) {
978 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
979 const Qt::Key key
= test
.first
.m_key
;
980 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
981 const int current
= test
.second
.m_current
;
982 const KItemSet selection
= test
.second
.m_selection
;
983 const bool activated
= test
.second
.m_activated
;
985 QTest::keyClick(m_container
, key
, modifier
);
988 m_selectionManager
->currentItem() == current
,
989 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
990 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigationSingleSelectionNoSelection_data().")
991 .arg(m_selectionManager
->currentItem())
993 .arg(QKeySequence(key
).toString())
995 switch (selectionBehavior
) {
996 case KItemListController::NoSelection
:
997 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
999 case KItemListController::SingleSelection
:
1000 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
);
1007 switch (selectionBehavior
) {
1008 case KItemListController::NoSelection
:
1009 case KItemListController::SingleSelection
:
1010 // In NoSelection and SingleSelection mode, the current item should be activated.
1011 QVERIFY(!spySingleItemActivated
.isEmpty());
1012 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
1013 QVERIFY(spyMultipleItemsActivated
.isEmpty());
1022 void KItemListControllerTest::testMouseClickActivation()
1024 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
1026 // Make sure that we have a large window, such that
1027 // the items are visible and clickable.
1028 adjustGeometryForColumnCount(5);
1030 // Make sure that the first item is visible in the view.
1031 m_view
->setScrollOffset(0);
1032 QCOMPARE(m_view
->firstVisibleIndex(), 0);
1034 const QPointF pos
= m_view
->itemContextRect(0).center();
1036 // Save the "single click" setting.
1037 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
1039 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
1040 mousePressEvent
.setPos(pos
);
1041 mousePressEvent
.setButton(Qt::LeftButton
);
1042 mousePressEvent
.setButtons(Qt::LeftButton
);
1044 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
1045 mouseReleaseEvent
.setPos(pos
);
1046 mouseReleaseEvent
.setButton(Qt::LeftButton
);
1047 mouseReleaseEvent
.setButtons(Qt::NoButton
);
1049 QGraphicsSceneMouseEvent
mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
1050 mouseDoubleClickEvent
.setPos(pos
);
1051 mouseDoubleClickEvent
.setButton(Qt::LeftButton
);
1052 mouseDoubleClickEvent
.setButtons(Qt::LeftButton
);
1054 QGraphicsSceneMouseEvent
mouseRightPressEvent(QEvent::GraphicsSceneMousePress
);
1055 mouseRightPressEvent
.setPos(pos
);
1056 mouseRightPressEvent
.setButton(Qt::RightButton
);
1057 mouseRightPressEvent
.setButtons(Qt::RightButton
);
1059 QGraphicsSceneMouseEvent
mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
1060 mouseRightReleaseEvent
.setPos(pos
);
1061 mouseRightReleaseEvent
.setButton(Qt::RightButton
);
1062 mouseRightReleaseEvent
.setButtons(Qt::NoButton
);
1064 QGraphicsSceneMouseEvent
mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
1065 mouseRightDoubleClickEvent
.setPos(pos
);
1066 mouseRightDoubleClickEvent
.setButton(Qt::RightButton
);
1067 mouseRightDoubleClickEvent
.setButtons(Qt::RightButton
);
1069 QGraphicsSceneMouseEvent
mouseBackPressEvent(QEvent::GraphicsSceneMousePress
);
1070 mouseBackPressEvent
.setPos(pos
);
1071 mouseBackPressEvent
.setButton(Qt::BackButton
);
1072 mouseBackPressEvent
.setButtons(Qt::BackButton
);
1074 QGraphicsSceneMouseEvent
mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
1075 mouseBackReleaseEvent
.setPos(pos
);
1076 mouseBackReleaseEvent
.setButton(Qt::BackButton
);
1077 mouseBackReleaseEvent
.setButtons(Qt::NoButton
);
1079 QGraphicsSceneMouseEvent
mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
1080 mouseBackDoubleClickEvent
.setPos(pos
);
1081 mouseBackDoubleClickEvent
.setButton(Qt::BackButton
);
1082 mouseBackDoubleClickEvent
.setButtons(Qt::BackButton
);
1084 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
1086 // Default setting: single click activation.
1087 m_testStyle
->setActivateItemOnSingleClick(true);
1088 m_view
->event(&mousePressEvent
);
1089 m_view
->event(&mouseReleaseEvent
);
1090 QCOMPARE(spyItemActivated
.count(), 1);
1091 spyItemActivated
.clear();
1092 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
1094 // Set the global setting to "double click activation".
1095 m_testStyle
->setActivateItemOnSingleClick(false);
1096 m_view
->event(&mousePressEvent
);
1097 m_view
->event(&mouseReleaseEvent
);
1098 QCOMPARE(spyItemActivated
.count(), 0);
1099 spyItemActivated
.clear();
1100 QVERIFY(m_view
->controller()->selectionManager()->hasSelection());
1102 // emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
1103 m_view
->event(&mousePressEvent
);
1104 m_view
->event(&mouseReleaseEvent
);
1105 m_view
->event(&mouseDoubleClickEvent
);
1106 m_view
->event(&mouseReleaseEvent
);
1107 QCOMPARE(spyItemActivated
.count(), 1);
1108 spyItemActivated
.clear();
1109 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
1111 // right mouse button should not trigger activation
1112 m_view
->event(&mouseRightPressEvent
);
1113 m_view
->event(&mouseRightReleaseEvent
);
1114 m_view
->event(&mouseRightDoubleClickEvent
);
1115 m_view
->event(&mouseRightReleaseEvent
);
1116 QCOMPARE(spyItemActivated
.count(), 0);
1118 // back mouse button should not trigger activation
1119 m_view
->event(&mouseBackPressEvent
);
1120 m_view
->event(&mouseBackReleaseEvent
);
1121 m_view
->event(&mouseBackDoubleClickEvent
);
1122 m_view
->event(&mouseBackReleaseEvent
);
1123 QCOMPARE(spyItemActivated
.count(), 0);
1125 // Enforce single click activation in the controller.
1126 m_controller
->setSingleClickActivationEnforced(true);
1127 m_view
->event(&mousePressEvent
);
1128 m_view
->event(&mouseReleaseEvent
);
1129 QCOMPARE(spyItemActivated
.count(), 1);
1130 spyItemActivated
.clear();
1131 constexpr const char *reasonWhySelectionShouldPersist
= "An item was selected before this mouse click. The click should not have cleared this selection.";
1132 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
1134 // Do not enforce single click activation in the controller.
1135 m_controller
->setSingleClickActivationEnforced(false);
1136 m_view
->event(&mousePressEvent
);
1137 m_view
->event(&mouseReleaseEvent
);
1138 QCOMPARE(spyItemActivated
.count(), 0);
1139 spyItemActivated
.clear();
1140 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
1142 // Set the global setting back to "single click activation".
1143 m_testStyle
->setActivateItemOnSingleClick(true);
1144 m_view
->event(&mousePressEvent
);
1145 m_view
->event(&mouseReleaseEvent
);
1146 QCOMPARE(spyItemActivated
.count(), 1);
1147 spyItemActivated
.clear();
1148 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
1150 // Enforce single click activation in the controller.
1151 m_controller
->setSingleClickActivationEnforced(true);
1152 m_view
->event(&mousePressEvent
);
1153 m_view
->event(&mouseReleaseEvent
);
1154 QCOMPARE(spyItemActivated
.count(), 1);
1155 spyItemActivated
.clear();
1156 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
1158 // Restore previous settings.
1159 m_controller
->setSingleClickActivationEnforced(true);
1160 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
1163 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
1165 const QSize size
= m_view
->itemSize().toSize();
1167 QRect rect
= m_container
->geometry();
1168 rect
.setSize(size
* count
);
1169 m_container
->setGeometry(rect
);
1171 // Increase the size of the container until the correct column count is reached.
1172 while (m_view
->m_layouter
->m_columnCount
< count
) {
1173 rect
= m_container
->geometry();
1174 rect
.setSize(rect
.size() + size
);
1175 m_container
->setGeometry(rect
);
1179 QTEST_MAIN(KItemListControllerTest
)
1181 #include "kitemlistcontrollertest.moc"