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 m_controller
= m_container
->controller();
113 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
114 m_selectionManager
= m_controller
->selectionManager();
115 m_testStyle
= new KItemListControllerTestStyle(m_view
->style());
116 m_view
->setStyle(m_testStyle
);
140 m_testDir
->createFiles(files
);
141 m_model
->loadDirectory(m_testDir
->url());
142 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, &KFileItemModel::directoryLoadingCompleted
);
143 QVERIFY(spyDirectoryLoadingCompleted
.wait());
146 QVERIFY(QTest::qWaitForWindowExposed(m_container
));
149 void KItemListControllerTest::cleanupTestCase()
152 m_container
= nullptr;
158 /** Before each test, the current item, selection, and item size are reset to the defaults. */
159 void KItemListControllerTest::init()
161 m_selectionManager
->setCurrentItem(0);
162 QCOMPARE(m_selectionManager
->currentItem(), 0);
164 m_selectionManager
->clearSelection();
165 QVERIFY(!m_selectionManager
->hasSelection());
167 const QSizeF
itemSize(50, 50);
168 m_view
->setItemSize(itemSize
);
169 QCOMPARE(m_view
->itemSize(), itemSize
);
172 void KItemListControllerTest::cleanup()
177 * \class KeyPress is a small helper struct that represents a key press event,
178 * including the key and the keyboard modifiers.
181 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
)
183 , m_modifier(modifier
)
188 Qt::KeyboardModifiers m_modifier
;
192 * \class ViewState is a small helper struct that represents a certain state
193 * of the view, including the current item, the selected items in MultiSelection
194 * mode (in the other modes, the selection is either empty or equal to the
195 * current item), and the information whether items were activated by the last
199 ViewState(int current
, const KItemSet
&selection
, bool activated
= false)
201 , m_selection(selection
)
202 , m_activated(activated
)
207 KItemSet m_selection
;
211 // We have to define a typedef for the pair in order to make the test compile.
212 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
213 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>)
216 * This function provides the data for the actual test function
217 * KItemListControllerTest::testKeyboardNavigation().
218 * It tests all possible combinations of view layouts, selection behaviors,
219 * and enabled/disabled groupings for different column counts, and
220 * provides a list of key presses and the states that the view should be in
221 * after the key press event.
223 void KItemListControllerTest::testKeyboardNavigation_data()
225 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
226 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
227 QTest::addColumn
<int>("columnCount");
228 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
229 QTest::addColumn
<bool>("groupingEnabled");
230 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
>>>("testList");
232 QList
<KFileItemListView::ItemLayout
> layoutList
;
233 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
234 layoutList
.append(KFileItemListView::IconsLayout
);
235 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
236 layoutList
.append(KFileItemListView::CompactLayout
);
237 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
238 layoutList
.append(KFileItemListView::DetailsLayout
);
239 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
241 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
242 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
243 selectionBehaviorList
.append(KItemListController::NoSelection
);
244 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
245 selectionBehaviorList
.append(KItemListController::SingleSelection
);
246 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
247 selectionBehaviorList
.append(KItemListController::MultiSelection
);
248 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
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 for (const KFileItemListView::ItemLayout
&layout
: layoutList
) {
258 // The following settings depend on the layout.
259 // Note that 'columns' are actually 'rows' in
261 Qt::Orientation scrollOrientation
;
262 QList
<int> columnCountList
;
264 Qt::Key previousItemKey
;
266 Qt::Key previousRowKey
;
269 case KFileItemListView::IconsLayout
:
270 scrollOrientation
= Qt::Vertical
;
271 columnCountList
<< 1 << 3 << 5;
272 nextItemKey
= Qt::Key_Right
;
273 previousItemKey
= Qt::Key_Left
;
274 nextRowKey
= Qt::Key_Down
;
275 previousRowKey
= Qt::Key_Up
;
277 case KFileItemListView::CompactLayout
:
278 scrollOrientation
= Qt::Horizontal
;
279 columnCountList
<< 1 << 3 << 5;
280 nextItemKey
= Qt::Key_Down
;
281 previousItemKey
= Qt::Key_Up
;
282 nextRowKey
= Qt::Key_Right
;
283 previousRowKey
= Qt::Key_Left
;
285 case KFileItemListView::DetailsLayout
:
286 scrollOrientation
= Qt::Vertical
;
287 columnCountList
<< 1;
288 nextItemKey
= Qt::Key_Down
;
289 previousItemKey
= Qt::Key_Up
;
290 nextRowKey
= Qt::Key_Down
;
291 previousRowKey
= Qt::Key_Up
;
295 for (int columnCount
: qAsConst(columnCountList
)) {
296 for (const KItemListController::SelectionBehavior
&selectionBehavior
: qAsConst(selectionBehaviorList
)) {
297 for (bool groupingEnabled
: qAsConst(groupingEnabledList
)) {
298 QList
<QPair
<KeyPress
, ViewState
>> testList
;
300 // First, key presses which should have the same effect
301 // for any layout and any number of columns.
302 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
303 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
304 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
305 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
306 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
307 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
308 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
309 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
310 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
311 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
312 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
313 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
314 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
315 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
316 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
317 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
318 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
319 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
320 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
321 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
322 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
323 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
324 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet())) // This used to select, but we are now using it to trigger either
325 // selection mode or "QuickLook". Ctrl+Space still works for selecting as expected.
326 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
327 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
328 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
329 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
330 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
331 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
332 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
333 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
334 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
335 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
336 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
338 // Next, we test combinations of key presses which only work for a
339 // particular number of columns and either enabled or disabled grouping.
342 if (columnCount
== 1) {
343 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
344 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
345 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
346 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
347 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
348 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
351 // Multiple columns: we test both 3 and 5 columns with grouping
352 // enabled or disabled. For each case, the layout of the items
353 // in the view is shown (both using file names and indices) to
354 // make it easier to understand what the tests do.
356 if (columnCount
== 3 && !groupingEnabled
) {
357 // 3 columns, no grouping:
362 // d1 d2 d3 | 9 10 11
363 // d4 e1 e2 | 12 13 14
364 // e3 e4 e5 | 15 16 17
366 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
367 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
368 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
369 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
370 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
371 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
372 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
373 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
374 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
375 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
376 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
377 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
378 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
379 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
380 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
381 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
382 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
383 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
384 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
385 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
388 if (columnCount
== 5 && !groupingEnabled
) {
389 // 5 columns, no grouping:
391 // a1 a2 a3 b1 c1 | 0 1 2 3 4
392 // c2 c3 c4 c5 d1 | 5 6 7 8 9
393 // d2 d3 d4 e1 e2 | 10 11 12 13 14
394 // e3 e4 e5 e6 e7 | 15 16 17 18 19
395 testList
<< qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
396 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
397 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
398 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
399 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
400 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
401 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
402 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
403 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
404 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
405 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
408 if (columnCount
== 3 && groupingEnabled
) {
409 // 3 columns, with grouping:
415 // d1 d2 d3 | 9 10 11
417 // e1 e2 e3 | 13 14 15
418 // e4 e5 e6 | 16 17 18
420 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
421 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
422 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
423 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
424 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
425 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
426 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
427 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
428 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
429 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
430 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
431 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
432 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
435 if (columnCount
== 5 && groupingEnabled
) {
436 // 5 columns, with grouping:
440 // c1 c2 c3 c4 c5 | 4 5 6 7 8
441 // d1 d2 d3 d4 | 9 10 11 12
442 // e1 e2 e3 e4 e5 | 13 14 15 16 17
444 testList
<< qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
445 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
446 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
447 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
448 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
449 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
450 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
451 << qMakePair(KeyPress(nextRowKey
), 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_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
455 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
456 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
459 const QString testName
= layoutNames
[layout
] + ", " + QString("%1 columns, ").arg(columnCount
) + selectionBehaviorNames
[selectionBehavior
]
460 + ", " + groupingEnabledNames
[groupingEnabled
];
462 const QByteArray testNameAscii
= testName
.toLatin1();
464 QTest::newRow(testNameAscii
.data()) << layout
<< scrollOrientation
<< columnCount
<< selectionBehavior
<< groupingEnabled
<< testList
;
472 * This function sets the view's properties according to the data provided by
473 * KItemListControllerTest::testKeyboardNavigation_data().
475 * The list \a testList contains pairs of key presses, which are sent to the
476 * container, and expected view states, which are verified then.
478 void KItemListControllerTest::testKeyboardNavigation()
480 QFETCH(KFileItemListView::ItemLayout
, layout
);
481 QFETCH(Qt::Orientation
, scrollOrientation
);
482 QFETCH(int, columnCount
);
483 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
484 QFETCH(bool, groupingEnabled
);
485 QFETCH(QList
<keyPressViewStatePair
>, testList
);
487 m_view
->setItemLayout(layout
);
488 QCOMPARE(m_view
->itemLayout(), layout
);
490 m_view
->setScrollOrientation(scrollOrientation
);
491 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
493 m_controller
->setSelectionBehavior(selectionBehavior
);
494 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
496 m_model
->setGroupedSorting(groupingEnabled
);
497 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
499 adjustGeometryForColumnCount(columnCount
);
500 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
502 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
503 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
505 while (!testList
.isEmpty()) {
506 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
507 const Qt::Key key
= test
.first
.m_key
;
508 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
509 const int current
= test
.second
.m_current
;
510 const KItemSet selection
= test
.second
.m_selection
;
511 const bool activated
= test
.second
.m_activated
;
513 QTest::keyClick(m_container
, key
, modifier
);
515 QCOMPARE(m_selectionManager
->currentItem(), current
);
516 switch (selectionBehavior
) {
517 case KItemListController::NoSelection
:
518 QVERIFY(m_selectionManager
->selectedItems().isEmpty());
520 case KItemListController::SingleSelection
:
521 QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
);
523 case KItemListController::MultiSelection
:
524 QCOMPARE(m_selectionManager
->selectedItems(), selection
);
529 switch (selectionBehavior
) {
530 case KItemListController::MultiSelection
:
531 if (!selection
.isEmpty()) {
532 // The selected items should be activated.
533 if (selection
.count() == 1) {
534 QVERIFY(!spySingleItemActivated
.isEmpty());
535 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
536 QVERIFY(spyMultipleItemsActivated
.isEmpty());
538 QVERIFY(spySingleItemActivated
.isEmpty());
539 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
540 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
544 // No items are selected. Therefore, the current item should be activated.
545 // This is handled by falling through to the NoSelection/SingleSelection case.
547 case KItemListController::NoSelection
:
548 case KItemListController::SingleSelection
:
549 // In NoSelection and SingleSelection mode, the current item should be activated.
550 QVERIFY(!spySingleItemActivated
.isEmpty());
551 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
552 QVERIFY(spyMultipleItemsActivated
.isEmpty());
559 void KItemListControllerTest::testMouseClickActivation()
561 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
563 // Make sure that we have a large window, such that
564 // the items are visible and clickable.
565 adjustGeometryForColumnCount(5);
567 // Make sure that the first item is visible in the view.
568 m_view
->setScrollOffset(0);
569 QCOMPARE(m_view
->firstVisibleIndex(), 0);
571 const QPointF pos
= m_view
->itemContextRect(0).center();
573 // Save the "single click" setting.
574 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
576 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
577 mousePressEvent
.setPos(pos
);
578 mousePressEvent
.setButton(Qt::LeftButton
);
579 mousePressEvent
.setButtons(Qt::LeftButton
);
581 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
582 mouseReleaseEvent
.setPos(pos
);
583 mouseReleaseEvent
.setButton(Qt::LeftButton
);
584 mouseReleaseEvent
.setButtons(Qt::NoButton
);
586 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
588 // Default setting: single click activation.
589 m_testStyle
->setActivateItemOnSingleClick(true);
590 m_view
->event(&mousePressEvent
);
591 m_view
->event(&mouseReleaseEvent
);
592 QCOMPARE(spyItemActivated
.count(), 1);
593 spyItemActivated
.clear();
595 // Set the global setting to "double click activation".
596 m_testStyle
->setActivateItemOnSingleClick(false);
597 m_view
->event(&mousePressEvent
);
598 m_view
->event(&mouseReleaseEvent
);
599 QCOMPARE(spyItemActivated
.count(), 0);
600 spyItemActivated
.clear();
602 // Enforce single click activation in the controller.
603 m_controller
->setSingleClickActivationEnforced(true);
604 m_view
->event(&mousePressEvent
);
605 m_view
->event(&mouseReleaseEvent
);
606 QCOMPARE(spyItemActivated
.count(), 1);
607 spyItemActivated
.clear();
609 // Do not enforce single click activation in the controller.
610 m_controller
->setSingleClickActivationEnforced(false);
611 m_view
->event(&mousePressEvent
);
612 m_view
->event(&mouseReleaseEvent
);
613 QCOMPARE(spyItemActivated
.count(), 0);
614 spyItemActivated
.clear();
616 // Set the global setting back to "single click activation".
617 m_testStyle
->setActivateItemOnSingleClick(true);
618 m_view
->event(&mousePressEvent
);
619 m_view
->event(&mouseReleaseEvent
);
620 QCOMPARE(spyItemActivated
.count(), 1);
621 spyItemActivated
.clear();
623 // Enforce single click activation in the controller.
624 m_controller
->setSingleClickActivationEnforced(true);
625 m_view
->event(&mousePressEvent
);
626 m_view
->event(&mouseReleaseEvent
);
627 QCOMPARE(spyItemActivated
.count(), 1);
628 spyItemActivated
.clear();
630 // Restore previous settings.
631 m_controller
->setSingleClickActivationEnforced(true);
632 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
635 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
637 const QSize size
= m_view
->itemSize().toSize();
639 QRect rect
= m_container
->geometry();
640 rect
.setSize(size
* count
);
641 m_container
->setGeometry(rect
);
643 // Increase the size of the container until the correct column count is reached.
644 while (m_view
->m_layouter
->m_columnCount
< count
) {
645 rect
= m_container
->geometry();
646 rect
.setSize(rect
.size() + size
);
647 m_container
->setGeometry(rect
);
651 QTEST_MAIN(KItemListControllerTest
)
653 #include "kitemlistcontrollertest.moc"