2 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kitemviews/kitemlistcontainer.h"
8 #include "kitemviews/kfileitemlistview.h"
9 #include "kitemviews/kfileitemmodel.h"
10 #include "kitemviews/kitemlistcontroller.h"
11 #include "kitemviews/kitemlistselectionmanager.h"
12 #include "kitemviews/private/kitemlistviewlayouter.h"
16 #include <QGraphicsSceneMouseEvent>
18 #include <QProxyStyle>
19 #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
,
47 const QStyleOption
* option
= nullptr,
48 const QWidget
* widget
= nullptr,
49 QStyleHintReturn
* returnData
= nullptr) const override
52 case QStyle::SH_ItemView_ActivateItemOnSingleClick
:
53 return (int)activateItemOnSingleClick();
55 return QProxyStyle::styleHint(hint
, option
, widget
, returnData
);
60 bool m_activateItemOnSingleClick
;
63 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
)
64 Q_DECLARE_METATYPE(Qt::Orientation
)
65 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
)
66 Q_DECLARE_METATYPE(KItemSet
)
68 class KItemListControllerTest
: public QObject
74 void cleanupTestCase();
79 void testKeyboardNavigation_data();
80 void testKeyboardNavigation();
81 void testMouseClickActivation();
85 * Make sure that the number of columns in the view is equal to \a count
86 * by changing the geometry of the container.
88 void adjustGeometryForColumnCount(int count
);
91 KFileItemListView
* m_view
;
92 KItemListController
* m_controller
;
93 KItemListSelectionManager
* m_selectionManager
;
94 KFileItemModel
* m_model
;
96 KItemListContainer
* m_container
;
97 KItemListControllerTestStyle
* m_testStyle
;
101 * This function initializes the member objects, creates the temporary files, and
102 * shows the view on the screen on startup. This could also be done before every
103 * single test, but this would make the time needed to run the test much larger.
105 void KItemListControllerTest::initTestCase()
107 QStandardPaths::setTestModeEnabled(true);
108 qRegisterMetaType
<KItemSet
>("KItemSet");
110 m_testDir
= new TestDir();
111 m_model
= new KFileItemModel();
112 m_view
= new KFileItemListView();
113 m_controller
= new KItemListController(m_model
, m_view
, this);
114 m_container
= new KItemListContainer(m_controller
);
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
);
123 << "a1" << "a2" << "a3"
125 << "c1" << "c2" << "c3" << "c4" << "c5"
126 << "d1" << "d2" << "d3" << "d4"
127 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
129 m_testDir
->createFiles(files
);
130 m_model
->loadDirectory(m_testDir
->url());
131 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, &KFileItemModel::directoryLoadingCompleted
);
132 QVERIFY(spyDirectoryLoadingCompleted
.wait());
135 QVERIFY(QTest::qWaitForWindowExposed(m_container
));
138 void KItemListControllerTest::cleanupTestCase()
141 m_container
= nullptr;
147 /** Before each test, the current item, selection, and item size are reset to the defaults. */
148 void KItemListControllerTest::init()
150 m_selectionManager
->setCurrentItem(0);
151 QCOMPARE(m_selectionManager
->currentItem(), 0);
153 m_selectionManager
->clearSelection();
154 QVERIFY(!m_selectionManager
->hasSelection());
156 const QSizeF
itemSize(50, 50);
157 m_view
->setItemSize(itemSize
);
158 QCOMPARE(m_view
->itemSize(), itemSize
);
161 void KItemListControllerTest::cleanup()
166 * \class KeyPress is a small helper struct that represents a key press event,
167 * including the key and the keyboard modifiers.
171 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
177 Qt::KeyboardModifiers m_modifier
;
181 * \class ViewState is a small helper struct that represents a certain state
182 * of the view, including the current item, the selected items in MultiSelection
183 * mode (in the other modes, the selection is either empty or equal to the
184 * current item), and the information whether items were activated by the last
189 ViewState(int current
, const KItemSet
&selection
, bool activated
= false) :
191 m_selection(selection
),
192 m_activated(activated
)
196 KItemSet m_selection
;
200 // We have to define a typedef for the pair in order to make the test compile.
201 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
202 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>)
205 * This function provides the data for the actual test function
206 * KItemListControllerTest::testKeyboardNavigation().
207 * It tests all possible combinations of view layouts, selection behaviors,
208 * and enabled/disabled groupings for different column counts, and
209 * provides a list of key presses and the states that the view should be in
210 * after the key press event.
212 void KItemListControllerTest::testKeyboardNavigation_data()
214 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
215 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
216 QTest::addColumn
<int>("columnCount");
217 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
218 QTest::addColumn
<bool>("groupingEnabled");
219 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
221 QList
<KFileItemListView::ItemLayout
> layoutList
;
222 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
223 layoutList
.append(KFileItemListView::IconsLayout
);
224 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
225 layoutList
.append(KFileItemListView::CompactLayout
);
226 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
227 layoutList
.append(KFileItemListView::DetailsLayout
);
228 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
230 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
231 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
232 selectionBehaviorList
.append(KItemListController::NoSelection
);
233 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
234 selectionBehaviorList
.append(KItemListController::SingleSelection
);
235 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
236 selectionBehaviorList
.append(KItemListController::MultiSelection
);
237 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
239 QList
<bool> groupingEnabledList
;
240 QHash
<bool, QString
> groupingEnabledNames
;
241 groupingEnabledList
.append(false);
242 groupingEnabledNames
[false] = "ungrouped";
243 groupingEnabledList
.append(true);
244 groupingEnabledNames
[true] = "grouping enabled";
246 for (const KFileItemListView::ItemLayout
& layout
: layoutList
) {
247 // The following settings depend on the layout.
248 // Note that 'columns' are actually 'rows' in
250 Qt::Orientation scrollOrientation
;
251 QList
<int> columnCountList
;
253 Qt::Key previousItemKey
;
255 Qt::Key previousRowKey
;
258 case KFileItemListView::IconsLayout
:
259 scrollOrientation
= Qt::Vertical
;
260 columnCountList
<< 1 << 3 << 5;
261 nextItemKey
= Qt::Key_Right
;
262 previousItemKey
= Qt::Key_Left
;
263 nextRowKey
= Qt::Key_Down
;
264 previousRowKey
= Qt::Key_Up
;
266 case KFileItemListView::CompactLayout
:
267 scrollOrientation
= Qt::Horizontal
;
268 columnCountList
<< 1 << 3 << 5;
269 nextItemKey
= Qt::Key_Down
;
270 previousItemKey
= Qt::Key_Up
;
271 nextRowKey
= Qt::Key_Right
;
272 previousRowKey
= Qt::Key_Left
;
274 case KFileItemListView::DetailsLayout
:
275 scrollOrientation
= Qt::Vertical
;
276 columnCountList
<< 1;
277 nextItemKey
= Qt::Key_Down
;
278 previousItemKey
= Qt::Key_Up
;
279 nextRowKey
= Qt::Key_Down
;
280 previousRowKey
= Qt::Key_Up
;
284 for (int columnCount
: qAsConst(columnCountList
)) {
285 for (const KItemListController::SelectionBehavior
& selectionBehavior
: qAsConst(selectionBehaviorList
)) {
286 for (bool groupingEnabled
: qAsConst(groupingEnabledList
)) {
287 QList
<QPair
<KeyPress
, ViewState
> > testList
;
289 // First, key presses which should have the same effect
290 // for any layout and any number of columns.
292 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
293 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
294 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
295 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
296 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
297 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
298 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
299 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
300 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
301 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
302 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
303 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
304 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
305 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
306 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
307 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
308 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
309 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
310 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
311 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
312 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
313 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
314 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet())) // This used to select, but we are now using it to trigger either
315 // selection mode or "QuickLook". Ctrl+Space still works for selecting as expected.
316 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
317 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
318 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
319 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
320 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
321 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
322 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
323 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
324 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
325 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
326 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
328 // Next, we test combinations of key presses which only work for a
329 // particular number of columns and either enabled or disabled grouping.
332 if (columnCount
== 1) {
334 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
335 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
336 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
337 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
338 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
339 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
342 // Multiple columns: we test both 3 and 5 columns with grouping
343 // enabled or disabled. For each case, the layout of the items
344 // in the view is shown (both using file names and indices) to
345 // make it easier to understand what the tests do.
347 if (columnCount
== 3 && !groupingEnabled
) {
348 // 3 columns, no grouping:
353 // d1 d2 d3 | 9 10 11
354 // d4 e1 e2 | 12 13 14
355 // e3 e4 e5 | 15 16 17
358 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
359 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
360 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
361 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
362 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
363 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
364 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
365 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
366 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
367 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
368 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
369 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
370 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
371 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
372 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
373 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
374 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
375 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
376 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
377 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
380 if (columnCount
== 5 && !groupingEnabled
) {
381 // 5 columns, no grouping:
383 // a1 a2 a3 b1 c1 | 0 1 2 3 4
384 // c2 c3 c4 c5 d1 | 5 6 7 8 9
385 // d2 d3 d4 e1 e2 | 10 11 12 13 14
386 // e3 e4 e5 e6 e7 | 15 16 17 18 19
388 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
389 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
390 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
391 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
392 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
393 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
394 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
395 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
396 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
397 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
398 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
401 if (columnCount
== 3 && groupingEnabled
) {
402 // 3 columns, with grouping:
408 // d1 d2 d3 | 9 10 11
410 // e1 e2 e3 | 13 14 15
411 // e4 e5 e6 | 16 17 18
414 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
415 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
416 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
417 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
418 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
419 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
420 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
421 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
422 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
423 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
424 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
425 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
426 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
429 if (columnCount
== 5 && groupingEnabled
) {
430 // 5 columns, with grouping:
434 // c1 c2 c3 c4 c5 | 4 5 6 7 8
435 // d1 d2 d3 d4 | 9 10 11 12
436 // e1 e2 e3 e4 e5 | 13 14 15 16 17
439 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
440 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
441 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
442 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
443 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
444 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
445 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
446 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
447 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
448 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
449 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
450 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
451 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
454 const QString testName
=
455 layoutNames
[layout
] + ", " +
456 QString("%1 columns, ").arg(columnCount
) +
457 selectionBehaviorNames
[selectionBehavior
] + ", " +
458 groupingEnabledNames
[groupingEnabled
];
460 const QByteArray testNameAscii
= testName
.toLatin1();
462 QTest::newRow(testNameAscii
.data())
476 * This function sets the view's properties according to the data provided by
477 * KItemListControllerTest::testKeyboardNavigation_data().
479 * The list \a testList contains pairs of key presses, which are sent to the
480 * container, and expected view states, which are verified then.
482 void KItemListControllerTest::testKeyboardNavigation()
484 QFETCH(KFileItemListView::ItemLayout
, layout
);
485 QFETCH(Qt::Orientation
, scrollOrientation
);
486 QFETCH(int, columnCount
);
487 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
488 QFETCH(bool, groupingEnabled
);
489 QFETCH(QList
<keyPressViewStatePair
>, testList
);
491 m_view
->setItemLayout(layout
);
492 QCOMPARE(m_view
->itemLayout(), layout
);
494 m_view
->setScrollOrientation(scrollOrientation
);
495 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
497 m_controller
->setSelectionBehavior(selectionBehavior
);
498 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
500 m_model
->setGroupedSorting(groupingEnabled
);
501 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
503 adjustGeometryForColumnCount(columnCount
);
504 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
506 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
507 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
509 while (!testList
.isEmpty()) {
510 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
511 const Qt::Key key
= test
.first
.m_key
;
512 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
513 const int current
= test
.second
.m_current
;
514 const KItemSet selection
= test
.second
.m_selection
;
515 const bool activated
= test
.second
.m_activated
;
517 QTest::keyClick(m_container
, key
, modifier
);
519 QCOMPARE(m_selectionManager
->currentItem(), current
);
520 switch (selectionBehavior
) {
521 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
522 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
523 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
527 switch (selectionBehavior
) {
528 case KItemListController::MultiSelection
:
529 if (!selection
.isEmpty()) {
530 // The selected items should be activated.
531 if (selection
.count() == 1) {
532 QVERIFY(!spySingleItemActivated
.isEmpty());
533 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
534 QVERIFY(spyMultipleItemsActivated
.isEmpty());
536 QVERIFY(spySingleItemActivated
.isEmpty());
537 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
538 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
542 // No items are selected. Therefore, the current item should be activated.
543 // This is handled by falling through to the NoSelection/SingleSelection case.
545 case KItemListController::NoSelection
:
546 case KItemListController::SingleSelection
:
547 // In NoSelection and SingleSelection mode, the current item should be activated.
548 QVERIFY(!spySingleItemActivated
.isEmpty());
549 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
550 QVERIFY(spyMultipleItemsActivated
.isEmpty());
557 void KItemListControllerTest::testMouseClickActivation()
559 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
561 // Make sure that we have a large window, such that
562 // the items are visible and clickable.
563 adjustGeometryForColumnCount(5);
565 // Make sure that the first item is visible in the view.
566 m_view
->setScrollOffset(0);
567 QCOMPARE(m_view
->firstVisibleIndex(), 0);
569 const QPointF pos
= m_view
->itemContextRect(0).center();
571 // Save the "single click" setting.
572 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
574 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
575 mousePressEvent
.setPos(pos
);
576 mousePressEvent
.setButton(Qt::LeftButton
);
577 mousePressEvent
.setButtons(Qt::LeftButton
);
579 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
580 mouseReleaseEvent
.setPos(pos
);
581 mouseReleaseEvent
.setButton(Qt::LeftButton
);
582 mouseReleaseEvent
.setButtons(Qt::NoButton
);
584 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
586 // Default setting: single click activation.
587 m_testStyle
->setActivateItemOnSingleClick(true);
588 m_view
->event(&mousePressEvent
);
589 m_view
->event(&mouseReleaseEvent
);
590 QCOMPARE(spyItemActivated
.count(), 1);
591 spyItemActivated
.clear();
593 // Set the global setting to "double click activation".
594 m_testStyle
->setActivateItemOnSingleClick(false);
595 m_view
->event(&mousePressEvent
);
596 m_view
->event(&mouseReleaseEvent
);
597 QCOMPARE(spyItemActivated
.count(), 0);
598 spyItemActivated
.clear();
600 // Enforce single click activation in the controller.
601 m_controller
->setSingleClickActivationEnforced(true);
602 m_view
->event(&mousePressEvent
);
603 m_view
->event(&mouseReleaseEvent
);
604 QCOMPARE(spyItemActivated
.count(), 1);
605 spyItemActivated
.clear();
607 // Do not enforce single click activation in the controller.
608 m_controller
->setSingleClickActivationEnforced(false);
609 m_view
->event(&mousePressEvent
);
610 m_view
->event(&mouseReleaseEvent
);
611 QCOMPARE(spyItemActivated
.count(), 0);
612 spyItemActivated
.clear();
614 // Set the global setting back to "single click activation".
615 m_testStyle
->setActivateItemOnSingleClick(true);
616 m_view
->event(&mousePressEvent
);
617 m_view
->event(&mouseReleaseEvent
);
618 QCOMPARE(spyItemActivated
.count(), 1);
619 spyItemActivated
.clear();
621 // Enforce single click activation in the controller.
622 m_controller
->setSingleClickActivationEnforced(true);
623 m_view
->event(&mousePressEvent
);
624 m_view
->event(&mouseReleaseEvent
);
625 QCOMPARE(spyItemActivated
.count(), 1);
626 spyItemActivated
.clear();
628 // Restore previous settings.
629 m_controller
->setSingleClickActivationEnforced(true);
630 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
633 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
635 const QSize size
= m_view
->itemSize().toSize();
637 QRect rect
= m_container
->geometry();
638 rect
.setSize(size
* count
);
639 m_container
->setGeometry(rect
);
641 // Increase the size of the container until the correct column count is reached.
642 while (m_view
->m_layouter
->m_columnCount
< count
) {
643 rect
= m_container
->geometry();
644 rect
.setSize(rect
.size() + size
);
645 m_container
->setGeometry(rect
);
649 QTEST_MAIN(KItemListControllerTest
)
651 #include "kitemlistcontrollertest.moc"