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
<QList
<QPair
<KeyPress
, ViewState
>>>("testList");
235 QList
<KFileItemListView::ItemLayout
> layoutList
;
236 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
237 layoutList
.append(KFileItemListView::IconsLayout
);
238 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
239 layoutList
.append(KFileItemListView::CompactLayout
);
240 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
241 layoutList
.append(KFileItemListView::DetailsLayout
);
242 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
244 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
245 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
246 selectionBehaviorList
.append(KItemListController::NoSelection
);
247 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
248 selectionBehaviorList
.append(KItemListController::SingleSelection
);
249 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
250 selectionBehaviorList
.append(KItemListController::MultiSelection
);
251 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
253 QList
<bool> groupingEnabledList
;
254 QHash
<bool, QString
> groupingEnabledNames
;
255 groupingEnabledList
.append(false);
256 groupingEnabledNames
[false] = "ungrouped";
257 groupingEnabledList
.append(true);
258 groupingEnabledNames
[true] = "grouping enabled";
260 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
261 // The following settings depend on the layout.
262 // Note that 'columns' are actually 'rows' in
264 Qt::Orientation scrollOrientation
;
265 QList
<int> columnCountList
;
267 Qt::Key previousItemKey
;
269 Qt::Key previousRowKey
;
272 case KFileItemListView::IconsLayout
:
273 scrollOrientation
= Qt::Vertical
;
274 columnCountList
<< 1 << 3 << 5;
275 nextItemKey
= Qt::Key_Right
;
276 previousItemKey
= Qt::Key_Left
;
277 nextRowKey
= Qt::Key_Down
;
278 previousRowKey
= Qt::Key_Up
;
280 case KFileItemListView::CompactLayout
:
281 scrollOrientation
= Qt::Horizontal
;
282 columnCountList
<< 1 << 3 << 5;
283 nextItemKey
= Qt::Key_Down
;
284 previousItemKey
= Qt::Key_Up
;
285 nextRowKey
= Qt::Key_Right
;
286 previousRowKey
= Qt::Key_Left
;
288 case KFileItemListView::DetailsLayout
:
289 scrollOrientation
= Qt::Vertical
;
290 columnCountList
<< 1;
291 nextItemKey
= Qt::Key_Down
;
292 previousItemKey
= Qt::Key_Up
;
293 nextRowKey
= Qt::Key_Down
;
294 previousRowKey
= Qt::Key_Up
;
298 for (int columnCount
: std::as_const(columnCountList
)) {
299 for (const KItemListController::SelectionBehavior
&selectionBehavior
: std::as_const(selectionBehaviorList
)) {
300 for (bool groupingEnabled
: std::as_const(groupingEnabledList
)) {
301 QList
<QPair
<KeyPress
, ViewState
>> testList
;
303 // First, key presses which should have the same effect
304 // for any layout and any number of columns.
305 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
306 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
307 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
308 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
309 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
310 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
311 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
312 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
313 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
314 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
315 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
316 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
317 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
318 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
319 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
320 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
321 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
322 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
323 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
324 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
325 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
326 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
327 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
328 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
329 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
330 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
331 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
332 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
333 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
334 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
335 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
336 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
337 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
338 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
340 // Next, we test combinations of key presses which only work for a
341 // particular number of columns and either enabled or disabled grouping.
344 if (columnCount
== 1) {
345 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
346 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
347 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
348 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
349 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
350 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
353 // Multiple columns: we test both 3 and 5 columns with grouping
354 // enabled or disabled. For each case, the layout of the items
355 // in the view is shown (both using file names and indices) to
356 // make it easier to understand what the tests do.
358 if (columnCount
== 3 && !groupingEnabled
) {
359 // 3 columns, no grouping:
364 // d1 d2 d3 | 9 10 11
365 // d4 e1 e2 | 12 13 14
366 // e3 e4 e5 | 15 16 17
368 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
369 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
370 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
371 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
372 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
373 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
374 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
375 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
376 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
377 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
378 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
379 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
380 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
381 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
382 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
383 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
384 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
385 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
386 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
387 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
390 if (columnCount
== 5 && !groupingEnabled
) {
391 // 5 columns, no grouping:
393 // a1 a2 a3 b1 c1 | 0 1 2 3 4
394 // c2 c3 c4 c5 d1 | 5 6 7 8 9
395 // d2 d3 d4 e1 e2 | 10 11 12 13 14
396 // e3 e4 e5 e6 e7 | 15 16 17 18 19
397 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
398 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
399 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
400 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
401 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
402 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
403 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
404 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
405 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
406 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
407 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
410 if (columnCount
== 3 && groupingEnabled
) {
411 // 3 columns, with grouping:
417 // d1 d2 d3 | 9 10 11
419 // e1 e2 e3 | 13 14 15
420 // e4 e5 e6 | 16 17 18
422 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
423 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
424 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
425 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
426 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
427 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
428 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
429 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
430 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
431 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
432 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
433 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
434 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
437 if (columnCount
== 5 && groupingEnabled
) {
438 // 5 columns, with grouping:
442 // c1 c2 c3 c4 c5 | 4 5 6 7 8
443 // d1 d2 d3 d4 | 9 10 11 12
444 // e1 e2 e3 e4 e5 | 13 14 15 16 17
446 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
447 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
448 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
449 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
450 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
451 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
452 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
453 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
454 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
455 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
456 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
457 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
458 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
461 const QString testName
= layoutNames
[layout
] + ", " + QString("%1 columns, ").arg(columnCount
) + selectionBehaviorNames
[selectionBehavior
]
462 + ", " + groupingEnabledNames
[groupingEnabled
];
464 const QByteArray testNameAscii
= testName
.toLatin1();
466 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
<< testList
;
474 * This function sets the view's properties according to the data provided by
475 * KItemListControllerTest::testKeyboardNavigation_data().
477 * The list \a testList contains pairs of key presses, which are sent to the
478 * container, and expected view states, which are verified then.
480 void KItemListControllerTest::testKeyboardNavigation()
482 QFETCH(KFileItemListView::ItemLayout
, layout
);
483 QFETCH(Qt::Orientation
, scrollOrientation
);
484 QFETCH(int, columnCount
);
485 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
486 QFETCH(bool, groupingEnabled
);
487 QFETCH(QList
<keyPressViewStatePair
>, testList
);
489 m_view
->setItemLayout(layout
);
490 QCOMPARE(m_view
->itemLayout(), layout
);
492 m_view
->setScrollOrientation(scrollOrientation
);
493 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
495 m_controller
->setSelectionBehavior(selectionBehavior
);
496 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
498 m_model
->setGroupedSorting(groupingEnabled
);
499 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
501 adjustGeometryForColumnCount(columnCount
);
502 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
504 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
505 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
507 while (!testList
.isEmpty()) {
508 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
509 const Qt::Key key
= test
.first
.m_key
;
510 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
511 const int current
= test
.second
.m_current
;
512 const KItemSet selection
= test
.second
.m_selection
;
513 const bool activated
= test
.second
.m_activated
;
515 QTest::keyClick(m_container
, key
, modifier
);
517 QCOMPARE(m_selectionManager
->currentItem(), current
);
518 switch (selectionBehavior
) {
519 case KItemListController::NoSelection
:
520 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
522 case KItemListController::SingleSelection
:
523 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
);
525 case KItemListController::MultiSelection
:
526 QCOMPARE(m_selectionManager
->selectedItems(), selection
);
531 switch (selectionBehavior
) {
532 case KItemListController::MultiSelection
:
533 if (!selection
.isEmpty()) {
534 // The selected items should be activated.
535 if (selection
.count() == 1) {
536 QVERIFY(!spySingleItemActivated
.isEmpty());
537 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
538 QVERIFY(spyMultipleItemsActivated
.isEmpty());
540 QVERIFY(spySingleItemActivated
.isEmpty());
541 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
542 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
546 // No items are selected. Therefore, the current item should be activated.
547 // This is handled by falling through to the NoSelection/SingleSelection case.
549 case KItemListController::NoSelection
:
550 case KItemListController::SingleSelection
:
551 // In NoSelection and SingleSelection mode, the current item should be activated.
552 QVERIFY(!spySingleItemActivated
.isEmpty());
553 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
554 QVERIFY(spyMultipleItemsActivated
.isEmpty());
561 void KItemListControllerTest::testMouseClickActivation()
563 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
565 // Make sure that we have a large window, such that
566 // the items are visible and clickable.
567 adjustGeometryForColumnCount(5);
569 // Make sure that the first item is visible in the view.
570 m_view
->setScrollOffset(0);
571 QCOMPARE(m_view
->firstVisibleIndex(), 0);
573 const QPointF pos
= m_view
->itemContextRect(0).center();
575 // Save the "single click" setting.
576 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
578 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
579 mousePressEvent
.setPos(pos
);
580 mousePressEvent
.setButton(Qt::LeftButton
);
581 mousePressEvent
.setButtons(Qt::LeftButton
);
583 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
584 mouseReleaseEvent
.setPos(pos
);
585 mouseReleaseEvent
.setButton(Qt::LeftButton
);
586 mouseReleaseEvent
.setButtons(Qt::NoButton
);
588 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
590 // Default setting: single click activation.
591 m_testStyle
->setActivateItemOnSingleClick(true);
592 m_view
->event(&mousePressEvent
);
593 m_view
->event(&mouseReleaseEvent
);
594 QCOMPARE(spyItemActivated
.count(), 1);
595 spyItemActivated
.clear();
597 // Set the global setting to "double click activation".
598 m_testStyle
->setActivateItemOnSingleClick(false);
599 m_view
->event(&mousePressEvent
);
600 m_view
->event(&mouseReleaseEvent
);
601 QCOMPARE(spyItemActivated
.count(), 0);
602 spyItemActivated
.clear();
604 // Enforce single click activation in the controller.
605 m_controller
->setSingleClickActivationEnforced(true);
606 m_view
->event(&mousePressEvent
);
607 m_view
->event(&mouseReleaseEvent
);
608 QCOMPARE(spyItemActivated
.count(), 1);
609 spyItemActivated
.clear();
611 // Do not enforce single click activation in the controller.
612 m_controller
->setSingleClickActivationEnforced(false);
613 m_view
->event(&mousePressEvent
);
614 m_view
->event(&mouseReleaseEvent
);
615 QCOMPARE(spyItemActivated
.count(), 0);
616 spyItemActivated
.clear();
618 // Set the global setting back to "single click activation".
619 m_testStyle
->setActivateItemOnSingleClick(true);
620 m_view
->event(&mousePressEvent
);
621 m_view
->event(&mouseReleaseEvent
);
622 QCOMPARE(spyItemActivated
.count(), 1);
623 spyItemActivated
.clear();
625 // Enforce single click activation in the controller.
626 m_controller
->setSingleClickActivationEnforced(true);
627 m_view
->event(&mousePressEvent
);
628 m_view
->event(&mouseReleaseEvent
);
629 QCOMPARE(spyItemActivated
.count(), 1);
630 spyItemActivated
.clear();
632 // Restore previous settings.
633 m_controller
->setSingleClickActivationEnforced(true);
634 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
637 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
639 const QSize size
= m_view
->itemSize().toSize();
641 QRect rect
= m_container
->geometry();
642 rect
.setSize(size
* count
);
643 m_container
->setGeometry(rect
);
645 // Increase the size of the container until the correct column count is reached.
646 while (m_view
->m_layouter
->m_columnCount
< count
) {
647 rect
= m_container
->geometry();
648 rect
.setSize(rect
.size() + size
);
649 m_container
->setGeometry(rect
);
653 QTEST_MAIN(KItemListControllerTest
)
655 #include "kitemlistcontrollertest.moc"