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() << 0))
315 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
316 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
317 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
318 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
319 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
320 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
321 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
322 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
323 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
324 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
325 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
327 // Next, we test combinations of key presses which only work for a
328 // particular number of columns and either enabled or disabled grouping.
331 if (columnCount
== 1) {
333 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
334 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
335 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
336 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
337 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
338 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
341 // Multiple columns: we test both 3 and 5 columns with grouping
342 // enabled or disabled. For each case, the layout of the items
343 // in the view is shown (both using file names and indices) to
344 // make it easier to understand what the tests do.
346 if (columnCount
== 3 && !groupingEnabled
) {
347 // 3 columns, no grouping:
352 // d1 d2 d3 | 9 10 11
353 // d4 e1 e2 | 12 13 14
354 // e3 e4 e5 | 15 16 17
357 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
358 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
359 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
360 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
361 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
362 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
363 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
364 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
365 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
366 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
367 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
368 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
369 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
370 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
371 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
372 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
373 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
374 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
375 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
376 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
379 if (columnCount
== 5 && !groupingEnabled
) {
380 // 5 columns, no grouping:
382 // a1 a2 a3 b1 c1 | 0 1 2 3 4
383 // c2 c3 c4 c5 d1 | 5 6 7 8 9
384 // d2 d3 d4 e1 e2 | 10 11 12 13 14
385 // e3 e4 e5 e6 e7 | 15 16 17 18 19
387 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
388 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
389 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
390 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
391 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
392 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
393 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
394 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
395 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
396 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
397 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
400 if (columnCount
== 3 && groupingEnabled
) {
401 // 3 columns, with grouping:
407 // d1 d2 d3 | 9 10 11
409 // e1 e2 e3 | 13 14 15
410 // e4 e5 e6 | 16 17 18
413 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
414 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
415 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
416 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
417 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
418 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
419 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
420 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
421 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
422 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
423 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
424 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
425 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
428 if (columnCount
== 5 && groupingEnabled
) {
429 // 5 columns, with grouping:
433 // c1 c2 c3 c4 c5 | 4 5 6 7 8
434 // d1 d2 d3 d4 | 9 10 11 12
435 // e1 e2 e3 e4 e5 | 13 14 15 16 17
438 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
439 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
440 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
441 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
442 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
443 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
444 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
445 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
446 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
447 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
448 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
449 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
450 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
453 const QString testName
=
454 layoutNames
[layout
] + ", " +
455 QString("%1 columns, ").arg(columnCount
) +
456 selectionBehaviorNames
[selectionBehavior
] + ", " +
457 groupingEnabledNames
[groupingEnabled
];
459 const QByteArray testNameAscii
= testName
.toLatin1();
461 QTest::newRow(testNameAscii
.data())
475 * This function sets the view's properties according to the data provided by
476 * KItemListControllerTest::testKeyboardNavigation_data().
478 * The list \a testList contains pairs of key presses, which are sent to the
479 * container, and expected view states, which are verified then.
481 void KItemListControllerTest::testKeyboardNavigation()
483 QFETCH(KFileItemListView::ItemLayout
, layout
);
484 QFETCH(Qt::Orientation
, scrollOrientation
);
485 QFETCH(int, columnCount
);
486 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
487 QFETCH(bool, groupingEnabled
);
488 QFETCH(QList
<keyPressViewStatePair
>, testList
);
490 m_view
->setItemLayout(layout
);
491 QCOMPARE(m_view
->itemLayout(), layout
);
493 m_view
->setScrollOrientation(scrollOrientation
);
494 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
496 m_controller
->setSelectionBehavior(selectionBehavior
);
497 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
499 m_model
->setGroupedSorting(groupingEnabled
);
500 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
502 adjustGeometryForColumnCount(columnCount
);
503 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
505 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
506 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
508 while (!testList
.isEmpty()) {
509 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
510 const Qt::Key key
= test
.first
.m_key
;
511 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
512 const int current
= test
.second
.m_current
;
513 const KItemSet selection
= test
.second
.m_selection
;
514 const bool activated
= test
.second
.m_activated
;
516 QTest::keyClick(m_container
, key
, modifier
);
518 QCOMPARE(m_selectionManager
->currentItem(), current
);
519 switch (selectionBehavior
) {
520 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
521 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
522 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
526 switch (selectionBehavior
) {
527 case KItemListController::MultiSelection
:
528 if (!selection
.isEmpty()) {
529 // The selected items should be activated.
530 if (selection
.count() == 1) {
531 QVERIFY(!spySingleItemActivated
.isEmpty());
532 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
533 QVERIFY(spyMultipleItemsActivated
.isEmpty());
535 QVERIFY(spySingleItemActivated
.isEmpty());
536 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
537 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
541 // No items are selected. Therefore, the current item should be activated.
542 // This is handled by falling through to the NoSelection/SingleSelection case.
544 case KItemListController::NoSelection
:
545 case KItemListController::SingleSelection
:
546 // In NoSelection and SingleSelection mode, the current item should be activated.
547 QVERIFY(!spySingleItemActivated
.isEmpty());
548 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
549 QVERIFY(spyMultipleItemsActivated
.isEmpty());
556 void KItemListControllerTest::testMouseClickActivation()
558 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
560 // Make sure that we have a large window, such that
561 // the items are visible and clickable.
562 adjustGeometryForColumnCount(5);
564 // Make sure that the first item is visible in the view.
565 m_view
->setScrollOffset(0);
566 QCOMPARE(m_view
->firstVisibleIndex(), 0);
568 const QPointF pos
= m_view
->itemContextRect(0).center();
570 // Save the "single click" setting.
571 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
573 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
574 mousePressEvent
.setPos(pos
);
575 mousePressEvent
.setButton(Qt::LeftButton
);
576 mousePressEvent
.setButtons(Qt::LeftButton
);
578 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
579 mouseReleaseEvent
.setPos(pos
);
580 mouseReleaseEvent
.setButton(Qt::LeftButton
);
581 mouseReleaseEvent
.setButtons(Qt::NoButton
);
583 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
585 // Default setting: single click activation.
586 m_testStyle
->setActivateItemOnSingleClick(true);
587 m_view
->event(&mousePressEvent
);
588 m_view
->event(&mouseReleaseEvent
);
589 QCOMPARE(spyItemActivated
.count(), 1);
590 spyItemActivated
.clear();
592 // Set the global setting to "double click activation".
593 m_testStyle
->setActivateItemOnSingleClick(false);
594 m_view
->event(&mousePressEvent
);
595 m_view
->event(&mouseReleaseEvent
);
596 QCOMPARE(spyItemActivated
.count(), 0);
597 spyItemActivated
.clear();
599 // Enforce single click activation in the controller.
600 m_controller
->setSingleClickActivationEnforced(true);
601 m_view
->event(&mousePressEvent
);
602 m_view
->event(&mouseReleaseEvent
);
603 QCOMPARE(spyItemActivated
.count(), 1);
604 spyItemActivated
.clear();
606 // Do not enforce single click activation in the controller.
607 m_controller
->setSingleClickActivationEnforced(false);
608 m_view
->event(&mousePressEvent
);
609 m_view
->event(&mouseReleaseEvent
);
610 QCOMPARE(spyItemActivated
.count(), 0);
611 spyItemActivated
.clear();
613 // Set the global setting back to "single click activation".
614 m_testStyle
->setActivateItemOnSingleClick(true);
615 m_view
->event(&mousePressEvent
);
616 m_view
->event(&mouseReleaseEvent
);
617 QCOMPARE(spyItemActivated
.count(), 1);
618 spyItemActivated
.clear();
620 // Enforce single click activation in the controller.
621 m_controller
->setSingleClickActivationEnforced(true);
622 m_view
->event(&mousePressEvent
);
623 m_view
->event(&mouseReleaseEvent
);
624 QCOMPARE(spyItemActivated
.count(), 1);
625 spyItemActivated
.clear();
627 // Restore previous settings.
628 m_controller
->setSingleClickActivationEnforced(true);
629 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
632 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
634 const QSize size
= m_view
->itemSize().toSize();
636 QRect rect
= m_container
->geometry();
637 rect
.setSize(size
* count
);
638 m_container
->setGeometry(rect
);
640 // Increase the size of the container until the correct column count is reached.
641 while (m_view
->m_layouter
->m_columnCount
< count
) {
642 rect
= m_container
->geometry();
643 rect
.setSize(rect
.size() + size
);
644 m_container
->setGeometry(rect
);
648 QTEST_MAIN(KItemListControllerTest
)
650 #include "kitemlistcontrollertest.moc"