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()
180 * \class KeyPress is a small helper struct that represents a key press event,
181 * including the key and the keyboard modifiers.
184 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
)
186 , m_modifier(modifier
)
191 Qt::KeyboardModifiers m_modifier
;
195 * \class ViewState is a small helper struct that represents a certain state
196 * of the view, including the current item, the selected items in MultiSelection
197 * mode (in the other modes, the selection is either empty or equal to the
198 * current item), and the information whether items were activated by the last
202 ViewState(int current
, const KItemSet
&selection
, bool activated
= false)
204 , m_selection(selection
)
205 , m_activated(activated
)
210 KItemSet m_selection
;
214 // We have to define a typedef for the pair in order to make the test compile.
215 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
216 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>)
219 * This function provides the data for the actual test function
220 * KItemListControllerTest::testKeyboardNavigation().
221 * It tests all possible combinations of view layouts, selection behaviors,
222 * and enabled/disabled groupings for different column counts, and
223 * provides a list of key presses and the states that the view should be in
224 * after the key press event.
226 void KItemListControllerTest::testKeyboardNavigation_data()
228 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
229 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
230 QTest::addColumn
<int>("columnCount");
231 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
232 QTest::addColumn
<bool>("groupingEnabled");
233 QTest::addColumn
<Qt::LayoutDirection
>("layoutDirection");
234 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
>>>("testList");
236 QList
<KFileItemListView::ItemLayout
> layoutList
;
237 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
238 layoutList
.append(KFileItemListView::IconsLayout
);
239 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
240 layoutList
.append(KFileItemListView::CompactLayout
);
241 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
242 layoutList
.append(KFileItemListView::DetailsLayout
);
243 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
245 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
246 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
247 selectionBehaviorList
.append(KItemListController::NoSelection
);
248 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
249 selectionBehaviorList
.append(KItemListController::SingleSelection
);
250 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
251 selectionBehaviorList
.append(KItemListController::MultiSelection
);
252 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
254 QList
<bool> groupingEnabledList
;
255 QHash
<bool, QString
> groupingEnabledNames
;
256 groupingEnabledList
.append(false);
257 groupingEnabledNames
[false] = "ungrouped";
258 groupingEnabledList
.append(true);
259 groupingEnabledNames
[true] = "grouping enabled";
261 QList
<Qt::LayoutDirection
> layoutDirectionList
;
262 QHash
<Qt::LayoutDirection
, QString
> layoutDirectionNames
;
263 layoutDirectionList
.append(Qt::LeftToRight
);
264 layoutDirectionNames
[Qt::LeftToRight
] = "Left-to-Right LayoutDirection";
265 layoutDirectionList
.append(Qt::RightToLeft
);
266 layoutDirectionNames
[Qt::RightToLeft
] = "Right-to-Left LayoutDirection";
268 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
269 // The following settings depend on the layout.
270 // Note that 'columns' are actually 'rows' in
272 Qt::Orientation scrollOrientation
;
273 QList
<int> columnCountList
;
275 Qt::Key previousItemKey
;
277 Qt::Key previousRowKey
;
280 case KFileItemListView::IconsLayout
:
281 scrollOrientation
= Qt::Vertical
;
282 columnCountList
<< 1 << 3 << 5;
283 nextItemKey
= Qt::Key_Right
;
284 previousItemKey
= Qt::Key_Left
;
285 nextRowKey
= Qt::Key_Down
;
286 previousRowKey
= Qt::Key_Up
;
288 case KFileItemListView::CompactLayout
:
289 scrollOrientation
= Qt::Horizontal
;
290 columnCountList
<< 1 << 3 << 5;
291 nextItemKey
= Qt::Key_Down
;
292 previousItemKey
= Qt::Key_Up
;
293 nextRowKey
= Qt::Key_Right
;
294 previousRowKey
= Qt::Key_Left
;
296 case KFileItemListView::DetailsLayout
:
297 scrollOrientation
= Qt::Vertical
;
298 columnCountList
<< 1;
299 nextItemKey
= Qt::Key_Down
;
300 previousItemKey
= Qt::Key_Up
;
301 nextRowKey
= Qt::Key_Down
;
302 previousRowKey
= Qt::Key_Up
;
305 for (auto layoutDirection
: std::as_const(layoutDirectionList
)) {
306 if (layoutDirection
== Qt::RightToLeft
) {
308 case KFileItemListView::IconsLayout
:
309 std::swap(nextItemKey
, previousItemKey
);
311 case KFileItemListView::CompactLayout
:
312 std::swap(nextRowKey
, previousRowKey
);
318 for (int columnCount
: std::as_const(columnCountList
)) {
319 for (const KItemListController::SelectionBehavior
&selectionBehavior
: std::as_const(selectionBehaviorList
)) {
320 for (bool groupingEnabled
: std::as_const(groupingEnabledList
)) {
321 QList
<QPair
<KeyPress
, ViewState
>> testList
;
323 // First, key presses which should have the same effect
324 // for any layout and any number of columns.
325 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
326 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
327 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
328 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
329 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
330 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
331 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
332 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
333 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
334 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
335 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
336 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
337 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
338 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
339 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
340 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
341 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
342 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
343 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
344 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
345 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
346 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
347 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
348 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
349 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
350 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
351 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
352 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
353 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
354 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
355 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
356 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
357 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
358 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
360 // Next, we test combinations of key presses which only work for a
361 // particular number of columns and either enabled or disabled grouping.
364 if (columnCount
== 1) {
365 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
366 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
367 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
368 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
369 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
370 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
373 // Multiple columns: we test both 3 and 5 columns with grouping
374 // enabled or disabled. For each case, the layout of the items
375 // in the view is shown (both using file names and indices) to
376 // make it easier to understand what the tests do.
378 if (columnCount
== 3 && !groupingEnabled
) {
379 // 3 columns, no grouping:
384 // d1 d2 d3 | 9 10 11
385 // d4 e1 e2 | 12 13 14
386 // e3 e4 e5 | 15 16 17
388 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
389 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
390 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
391 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
392 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
393 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
394 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
395 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
396 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
397 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
398 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
399 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
400 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
401 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
402 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
403 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
404 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
405 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
406 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
407 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
410 if (columnCount
== 5 && !groupingEnabled
) {
411 // 5 columns, no grouping:
413 // a1 a2 a3 b1 c1 | 0 1 2 3 4
414 // c2 c3 c4 c5 d1 | 5 6 7 8 9
415 // d2 d3 d4 e1 e2 | 10 11 12 13 14
416 // e3 e4 e5 e6 e7 | 15 16 17 18 19
417 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
418 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
419 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
420 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
421 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
422 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
423 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
424 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
425 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
426 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
427 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
430 if (columnCount
== 3 && groupingEnabled
) {
431 // 3 columns, with grouping:
437 // d1 d2 d3 | 9 10 11
439 // e1 e2 e3 | 13 14 15
440 // e4 e5 e6 | 16 17 18
442 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
443 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
444 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
445 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
446 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
447 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
448 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
449 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
450 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
451 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
452 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
453 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
454 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
457 if (columnCount
== 5 && groupingEnabled
) {
458 // 5 columns, with grouping:
462 // c1 c2 c3 c4 c5 | 4 5 6 7 8
463 // d1 d2 d3 d4 | 9 10 11 12
464 // e1 e2 e3 e4 e5 | 13 14 15 16 17
466 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
467 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
468 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
469 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
470 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
471 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
472 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
473 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
474 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
475 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
476 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
477 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
478 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
481 const QString testName
= layoutNames
[layout
] + ", " + QStringLiteral("%1 columns, ").arg(columnCount
)
482 + selectionBehaviorNames
[selectionBehavior
] + ", " + groupingEnabledNames
[groupingEnabled
] + ", "
483 + layoutDirectionNames
[layoutDirection
];
485 const QByteArray testNameAscii
= testName
.toLatin1();
487 QTest::newRow(testNameAscii
.data())
488 << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
<< layoutDirection
<< testList
;
497 * This function sets the view's properties according to the data provided by
498 * KItemListControllerTest::testKeyboardNavigation_data().
500 * The list \a testList contains pairs of key presses, which are sent to the
501 * container, and expected view states, which are verified then.
503 void KItemListControllerTest::testKeyboardNavigation()
505 QFETCH(KFileItemListView::ItemLayout
, layout
);
506 QFETCH(Qt::Orientation
, scrollOrientation
);
507 QFETCH(int, columnCount
);
508 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
509 QFETCH(bool, groupingEnabled
);
510 QFETCH(Qt::LayoutDirection
, layoutDirection
);
511 QFETCH(QList
<keyPressViewStatePair
>, testList
);
513 QApplication::setLayoutDirection(layoutDirection
);
514 m_view
->setLayoutDirection(layoutDirection
);
516 m_view
->setItemLayout(layout
);
517 QCOMPARE(m_view
->itemLayout(), layout
);
519 m_view
->setScrollOrientation(scrollOrientation
);
520 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
522 m_controller
->setSelectionBehavior(selectionBehavior
);
523 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
525 m_model
->setGroupedSorting(groupingEnabled
);
526 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
528 adjustGeometryForColumnCount(columnCount
);
529 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
531 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
532 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
535 while (!testList
.isEmpty()) {
537 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
538 const Qt::Key key
= test
.first
.m_key
;
539 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
540 const int current
= test
.second
.m_current
;
541 const KItemSet selection
= test
.second
.m_selection
;
542 const bool activated
= test
.second
.m_activated
;
544 QTest::keyClick(m_container
, key
, modifier
);
547 m_selectionManager
->currentItem() == current
,
548 qPrintable(QStringLiteral("currentItem() returns index %1 but %2 would be expected. Before this, key \"%3\" was pressed. This test case is defined "
549 "in row %4 of the testList from KItemListControllerTest::testKeyboardNavigation_data().")
550 .arg(m_selectionManager
->currentItem())
552 .arg(QKeySequence(key
).toString())
554 switch (selectionBehavior
) {
555 case KItemListController::NoSelection
:
556 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
558 case KItemListController::SingleSelection
:
559 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
);
561 case KItemListController::MultiSelection
:
562 QCOMPARE(m_selectionManager
->selectedItems(), selection
);
567 switch (selectionBehavior
) {
568 case KItemListController::MultiSelection
:
569 if (!selection
.isEmpty()) {
570 // The selected items should be activated.
571 if (selection
.count() == 1) {
572 QVERIFY(!spySingleItemActivated
.isEmpty());
573 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
574 QVERIFY(spyMultipleItemsActivated
.isEmpty());
576 QVERIFY(spySingleItemActivated
.isEmpty());
577 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
578 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
582 // No items are selected. Therefore, the current item should be activated.
583 // This is handled by falling through to the NoSelection/SingleSelection case.
585 case KItemListController::NoSelection
:
586 case KItemListController::SingleSelection
:
587 // In NoSelection and SingleSelection mode, the current item should be activated.
588 QVERIFY(!spySingleItemActivated
.isEmpty());
589 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
590 QVERIFY(spyMultipleItemsActivated
.isEmpty());
597 void KItemListControllerTest::testMouseClickActivation()
599 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
601 // Make sure that we have a large window, such that
602 // the items are visible and clickable.
603 adjustGeometryForColumnCount(5);
605 // Make sure that the first item is visible in the view.
606 m_view
->setScrollOffset(0);
607 QCOMPARE(m_view
->firstVisibleIndex(), 0);
609 const QPointF pos
= m_view
->itemContextRect(0).center();
611 // Save the "single click" setting.
612 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
614 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
615 mousePressEvent
.setPos(pos
);
616 mousePressEvent
.setButton(Qt::LeftButton
);
617 mousePressEvent
.setButtons(Qt::LeftButton
);
619 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
620 mouseReleaseEvent
.setPos(pos
);
621 mouseReleaseEvent
.setButton(Qt::LeftButton
);
622 mouseReleaseEvent
.setButtons(Qt::NoButton
);
624 QGraphicsSceneMouseEvent
mouseDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
625 mouseDoubleClickEvent
.setPos(pos
);
626 mouseDoubleClickEvent
.setButton(Qt::LeftButton
);
627 mouseDoubleClickEvent
.setButtons(Qt::LeftButton
);
629 QGraphicsSceneMouseEvent
mouseRightPressEvent(QEvent::GraphicsSceneMousePress
);
630 mouseRightPressEvent
.setPos(pos
);
631 mouseRightPressEvent
.setButton(Qt::RightButton
);
632 mouseRightPressEvent
.setButtons(Qt::RightButton
);
634 QGraphicsSceneMouseEvent
mouseRightReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
635 mouseRightReleaseEvent
.setPos(pos
);
636 mouseRightReleaseEvent
.setButton(Qt::RightButton
);
637 mouseRightReleaseEvent
.setButtons(Qt::NoButton
);
639 QGraphicsSceneMouseEvent
mouseRightDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
640 mouseRightDoubleClickEvent
.setPos(pos
);
641 mouseRightDoubleClickEvent
.setButton(Qt::RightButton
);
642 mouseRightDoubleClickEvent
.setButtons(Qt::RightButton
);
644 QGraphicsSceneMouseEvent
mouseBackPressEvent(QEvent::GraphicsSceneMousePress
);
645 mouseBackPressEvent
.setPos(pos
);
646 mouseBackPressEvent
.setButton(Qt::BackButton
);
647 mouseBackPressEvent
.setButtons(Qt::BackButton
);
649 QGraphicsSceneMouseEvent
mouseBackReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
650 mouseBackReleaseEvent
.setPos(pos
);
651 mouseBackReleaseEvent
.setButton(Qt::BackButton
);
652 mouseBackReleaseEvent
.setButtons(Qt::NoButton
);
654 QGraphicsSceneMouseEvent
mouseBackDoubleClickEvent(QEvent::GraphicsSceneMouseDoubleClick
);
655 mouseBackDoubleClickEvent
.setPos(pos
);
656 mouseBackDoubleClickEvent
.setButton(Qt::BackButton
);
657 mouseBackDoubleClickEvent
.setButtons(Qt::BackButton
);
659 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
661 // Default setting: single click activation.
662 m_testStyle
->setActivateItemOnSingleClick(true);
663 m_view
->event(&mousePressEvent
);
664 m_view
->event(&mouseReleaseEvent
);
665 QCOMPARE(spyItemActivated
.count(), 1);
666 spyItemActivated
.clear();
667 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
669 // Set the global setting to "double click activation".
670 m_testStyle
->setActivateItemOnSingleClick(false);
671 m_view
->event(&mousePressEvent
);
672 m_view
->event(&mouseReleaseEvent
);
673 QCOMPARE(spyItemActivated
.count(), 0);
674 spyItemActivated
.clear();
675 QVERIFY(m_view
->controller()->selectionManager()->hasSelection());
677 // emulation of double click according to https://doc.qt.io/qt-6/qgraphicsscene.html#mouseDoubleClickEvent
678 m_view
->event(&mousePressEvent
);
679 m_view
->event(&mouseReleaseEvent
);
680 m_view
->event(&mouseDoubleClickEvent
);
681 m_view
->event(&mouseReleaseEvent
);
682 QCOMPARE(spyItemActivated
.count(), 1);
683 spyItemActivated
.clear();
684 QVERIFY2(!m_view
->controller()->selectionManager()->hasSelection(), "An item should not be implicitly selected during activation. @see bug 424723");
686 // right mouse button should not trigger activation
687 m_view
->event(&mouseRightPressEvent
);
688 m_view
->event(&mouseRightReleaseEvent
);
689 m_view
->event(&mouseRightDoubleClickEvent
);
690 m_view
->event(&mouseRightReleaseEvent
);
691 QCOMPARE(spyItemActivated
.count(), 0);
693 // back mouse button should not trigger activation
694 m_view
->event(&mouseBackPressEvent
);
695 m_view
->event(&mouseBackReleaseEvent
);
696 m_view
->event(&mouseBackDoubleClickEvent
);
697 m_view
->event(&mouseBackReleaseEvent
);
698 QCOMPARE(spyItemActivated
.count(), 0);
700 // Enforce single click activation in the controller.
701 m_controller
->setSingleClickActivationEnforced(true);
702 m_view
->event(&mousePressEvent
);
703 m_view
->event(&mouseReleaseEvent
);
704 QCOMPARE(spyItemActivated
.count(), 1);
705 spyItemActivated
.clear();
706 constexpr const char *reasonWhySelectionShouldPersist
= "An item was selected before this mouse click. The click should not have cleared this selection.";
707 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
709 // Do not enforce single click activation in the controller.
710 m_controller
->setSingleClickActivationEnforced(false);
711 m_view
->event(&mousePressEvent
);
712 m_view
->event(&mouseReleaseEvent
);
713 QCOMPARE(spyItemActivated
.count(), 0);
714 spyItemActivated
.clear();
715 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
717 // Set the global setting back to "single click activation".
718 m_testStyle
->setActivateItemOnSingleClick(true);
719 m_view
->event(&mousePressEvent
);
720 m_view
->event(&mouseReleaseEvent
);
721 QCOMPARE(spyItemActivated
.count(), 1);
722 spyItemActivated
.clear();
723 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
725 // Enforce single click activation in the controller.
726 m_controller
->setSingleClickActivationEnforced(true);
727 m_view
->event(&mousePressEvent
);
728 m_view
->event(&mouseReleaseEvent
);
729 QCOMPARE(spyItemActivated
.count(), 1);
730 spyItemActivated
.clear();
731 QVERIFY2(m_view
->controller()->selectionManager()->hasSelection(), reasonWhySelectionShouldPersist
);
733 // Restore previous settings.
734 m_controller
->setSingleClickActivationEnforced(true);
735 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
738 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
740 const QSize size
= m_view
->itemSize().toSize();
742 QRect rect
= m_container
->geometry();
743 rect
.setSize(size
* count
);
744 m_container
->setGeometry(rect
);
746 // Increase the size of the container until the correct column count is reached.
747 while (m_view
->m_layouter
->m_columnCount
< count
) {
748 rect
= m_container
->geometry();
749 rect
.setSize(rect
.size() + size
);
750 m_container
->setGeometry(rect
);
754 QTEST_MAIN(KItemListControllerTest
)
756 #include "kitemlistcontrollertest.moc"