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>
21 * \class KItemListControllerTestStyle is a proxy style for testing the
22 * KItemListController with different style hint options, e.g. single/double
25 class KItemListControllerTestStyle
: public QProxyStyle
29 KItemListControllerTestStyle(QStyle
* style
) :
31 m_activateItemOnSingleClick((bool)style
->styleHint(SH_ItemView_ActivateItemOnSingleClick
))
35 void setActivateItemOnSingleClick(bool activateItemOnSingleClick
)
37 m_activateItemOnSingleClick
= activateItemOnSingleClick
;
40 bool activateItemOnSingleClick() const
42 return m_activateItemOnSingleClick
;
45 int styleHint(StyleHint hint
,
46 const QStyleOption
* option
= nullptr,
47 const QWidget
* widget
= nullptr,
48 QStyleHintReturn
* returnData
= nullptr) const override
51 case QStyle::SH_ItemView_ActivateItemOnSingleClick
:
52 return (int)activateItemOnSingleClick();
54 return QProxyStyle::styleHint(hint
, option
, widget
, returnData
);
59 bool m_activateItemOnSingleClick
;
62 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
)
63 Q_DECLARE_METATYPE(Qt::Orientation
)
64 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
)
65 Q_DECLARE_METATYPE(KItemSet
)
67 class KItemListControllerTest
: public QObject
73 void cleanupTestCase();
78 void testKeyboardNavigation_data();
79 void testKeyboardNavigation();
80 void testMouseClickActivation();
84 * Make sure that the number of columns in the view is equal to \a count
85 * by changing the geometry of the container.
87 void adjustGeometryForColumnCount(int count
);
90 KFileItemListView
* m_view
;
91 KItemListController
* m_controller
;
92 KItemListSelectionManager
* m_selectionManager
;
93 KFileItemModel
* m_model
;
95 KItemListContainer
* m_container
;
96 KItemListControllerTestStyle
* m_testStyle
;
100 * This function initializes the member objects, creates the temporary files, and
101 * shows the view on the screen on startup. This could also be done before every
102 * single test, but this would make the time needed to run the test much larger.
104 void KItemListControllerTest::initTestCase()
106 qRegisterMetaType
<KItemSet
>("KItemSet");
108 m_testDir
= new TestDir();
109 m_model
= new KFileItemModel();
110 m_view
= new KFileItemListView();
111 m_controller
= new KItemListController(m_model
, m_view
, this);
112 m_container
= new KItemListContainer(m_controller
);
113 m_controller
= m_container
->controller();
114 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
115 m_selectionManager
= m_controller
->selectionManager();
116 m_testStyle
= new KItemListControllerTestStyle(m_view
->style());
117 m_view
->setStyle(m_testStyle
);
121 << "a1" << "a2" << "a3"
123 << "c1" << "c2" << "c3" << "c4" << "c5"
124 << "d1" << "d2" << "d3" << "d4"
125 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
127 m_testDir
->createFiles(files
);
128 m_model
->loadDirectory(m_testDir
->url());
129 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, &KFileItemModel::directoryLoadingCompleted
);
130 QVERIFY(spyDirectoryLoadingCompleted
.wait());
133 QVERIFY(QTest::qWaitForWindowExposed(m_container
));
136 void KItemListControllerTest::cleanupTestCase()
139 m_container
= nullptr;
145 /** Before each test, the current item, selection, and item size are reset to the defaults. */
146 void KItemListControllerTest::init()
148 m_selectionManager
->setCurrentItem(0);
149 QCOMPARE(m_selectionManager
->currentItem(), 0);
151 m_selectionManager
->clearSelection();
152 QVERIFY(!m_selectionManager
->hasSelection());
154 const QSizeF
itemSize(50, 50);
155 m_view
->setItemSize(itemSize
);
156 QCOMPARE(m_view
->itemSize(), itemSize
);
159 void KItemListControllerTest::cleanup()
164 * \class KeyPress is a small helper struct that represents a key press event,
165 * including the key and the keyboard modifiers.
169 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
175 Qt::KeyboardModifiers m_modifier
;
179 * \class ViewState is a small helper struct that represents a certain state
180 * of the view, including the current item, the selected items in MultiSelection
181 * mode (in the other modes, the selection is either empty or equal to the
182 * current item), and the information whether items were activated by the last
187 ViewState(int current
, const KItemSet
&selection
, bool activated
= false) :
189 m_selection(selection
),
190 m_activated(activated
)
194 KItemSet m_selection
;
198 // We have to define a typedef for the pair in order to make the test compile.
199 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
200 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>)
203 * This function provides the data for the actual test function
204 * KItemListControllerTest::testKeyboardNavigation().
205 * It tests all possible combinations of view layouts, selection behaviors,
206 * and enabled/disabled groupings for different column counts, and
207 * provides a list of key presses and the states that the view should be in
208 * after the key press event.
210 void KItemListControllerTest::testKeyboardNavigation_data()
212 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
213 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
214 QTest::addColumn
<int>("columnCount");
215 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
216 QTest::addColumn
<bool>("groupingEnabled");
217 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
219 QList
<KFileItemListView::ItemLayout
> layoutList
;
220 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
221 layoutList
.append(KFileItemListView::IconsLayout
);
222 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
223 layoutList
.append(KFileItemListView::CompactLayout
);
224 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
225 layoutList
.append(KFileItemListView::DetailsLayout
);
226 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
228 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
229 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
230 selectionBehaviorList
.append(KItemListController::NoSelection
);
231 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
232 selectionBehaviorList
.append(KItemListController::SingleSelection
);
233 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
234 selectionBehaviorList
.append(KItemListController::MultiSelection
);
235 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
237 QList
<bool> groupingEnabledList
;
238 QHash
<bool, QString
> groupingEnabledNames
;
239 groupingEnabledList
.append(false);
240 groupingEnabledNames
[false] = "ungrouped";
241 groupingEnabledList
.append(true);
242 groupingEnabledNames
[true] = "grouping enabled";
244 foreach (const KFileItemListView::ItemLayout
& layout
, layoutList
) {
245 // The following settings depend on the layout.
246 // Note that 'columns' are actually 'rows' in
248 Qt::Orientation scrollOrientation
;
249 QList
<int> columnCountList
;
251 Qt::Key previousItemKey
;
253 Qt::Key previousRowKey
;
256 case KFileItemListView::IconsLayout
:
257 scrollOrientation
= Qt::Vertical
;
258 columnCountList
<< 1 << 3 << 5;
259 nextItemKey
= Qt::Key_Right
;
260 previousItemKey
= Qt::Key_Left
;
261 nextRowKey
= Qt::Key_Down
;
262 previousRowKey
= Qt::Key_Up
;
264 case KFileItemListView::CompactLayout
:
265 scrollOrientation
= Qt::Horizontal
;
266 columnCountList
<< 1 << 3 << 5;
267 nextItemKey
= Qt::Key_Down
;
268 previousItemKey
= Qt::Key_Up
;
269 nextRowKey
= Qt::Key_Right
;
270 previousRowKey
= Qt::Key_Left
;
272 case KFileItemListView::DetailsLayout
:
273 scrollOrientation
= Qt::Vertical
;
274 columnCountList
<< 1;
275 nextItemKey
= Qt::Key_Down
;
276 previousItemKey
= Qt::Key_Up
;
277 nextRowKey
= Qt::Key_Down
;
278 previousRowKey
= Qt::Key_Up
;
282 foreach (int columnCount
, columnCountList
) {
283 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
284 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
285 QList
<QPair
<KeyPress
, ViewState
> > testList
;
287 // First, key presses which should have the same effect
288 // for any layout and any number of columns.
290 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
291 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
292 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
293 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
294 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
295 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
296 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
297 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
298 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
299 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
300 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
301 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
302 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
303 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
304 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
305 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
306 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
307 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
308 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
309 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
310 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
311 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
312 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
313 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
314 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
315 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
316 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
317 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
318 << qMakePair(KeyPress(Qt::Key_E
), ViewState(14, KItemSet() << 14))
319 << qMakePair(KeyPress(Qt::Key_E
), ViewState(15, KItemSet() << 15))
320 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(15, KItemSet()))
321 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
322 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
323 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
325 // Next, we test combinations of key presses which only work for a
326 // particular number of columns and either enabled or disabled grouping.
329 if (columnCount
== 1) {
331 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
332 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
333 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
334 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
335 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
336 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
339 // Multiple columns: we test both 3 and 5 columns with grouping
340 // enabled or disabled. For each case, the layout of the items
341 // in the view is shown (both using file names and indices) to
342 // make it easier to understand what the tests do.
344 if (columnCount
== 3 && !groupingEnabled
) {
345 // 3 columns, no grouping:
350 // d1 d2 d3 | 9 10 11
351 // d4 e1 e2 | 12 13 14
352 // e3 e4 e5 | 15 16 17
355 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
356 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
357 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
358 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
359 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
360 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
361 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
362 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
363 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
364 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
365 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
366 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
367 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
368 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
369 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
370 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
371 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
372 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
373 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
374 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
377 if (columnCount
== 5 && !groupingEnabled
) {
378 // 5 columns, no grouping:
380 // a1 a2 a3 b1 c1 | 0 1 2 3 4
381 // c2 c3 c4 c5 d1 | 5 6 7 8 9
382 // d2 d3 d4 e1 e2 | 10 11 12 13 14
383 // e3 e4 e5 e6 e7 | 15 16 17 18 19
385 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
386 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
387 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
388 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
389 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
390 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
391 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
392 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
393 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
394 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
395 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
398 if (columnCount
== 3 && groupingEnabled
) {
399 // 3 columns, with grouping:
405 // d1 d2 d3 | 9 10 11
407 // e1 e2 e3 | 13 14 15
408 // e4 e5 e6 | 16 17 18
411 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
412 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
413 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
414 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
415 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
416 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
417 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
418 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
419 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
420 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
421 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
422 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
423 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
426 if (columnCount
== 5 && groupingEnabled
) {
427 // 5 columns, with grouping:
431 // c1 c2 c3 c4 c5 | 4 5 6 7 8
432 // d1 d2 d3 d4 | 9 10 11 12
433 // e1 e2 e3 e4 e5 | 13 14 15 16 17
436 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
437 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
438 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
439 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
440 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
441 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
442 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
443 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
444 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
445 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
446 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
447 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
448 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
451 const QString testName
=
452 layoutNames
[layout
] + ", " +
453 QString("%1 columns, ").arg(columnCount
) +
454 selectionBehaviorNames
[selectionBehavior
] + ", " +
455 groupingEnabledNames
[groupingEnabled
];
457 const QByteArray testNameAscii
= testName
.toLatin1();
459 QTest::newRow(testNameAscii
.data())
473 * This function sets the view's properties according to the data provided by
474 * KItemListControllerTest::testKeyboardNavigation_data().
476 * The list \a testList contains pairs of key presses, which are sent to the
477 * container, and expected view states, which are verified then.
479 void KItemListControllerTest::testKeyboardNavigation()
481 QFETCH(KFileItemListView::ItemLayout
, layout
);
482 QFETCH(Qt::Orientation
, scrollOrientation
);
483 QFETCH(int, columnCount
);
484 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
485 QFETCH(bool, groupingEnabled
);
486 QFETCH(QList
<keyPressViewStatePair
>, testList
);
488 m_view
->setItemLayout(layout
);
489 QCOMPARE(m_view
->itemLayout(), layout
);
491 m_view
->setScrollOrientation(scrollOrientation
);
492 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
494 m_controller
->setSelectionBehavior(selectionBehavior
);
495 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
497 m_model
->setGroupedSorting(groupingEnabled
);
498 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
500 adjustGeometryForColumnCount(columnCount
);
501 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
503 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
504 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
506 while (!testList
.isEmpty()) {
507 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
508 const Qt::Key key
= test
.first
.m_key
;
509 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
510 const int current
= test
.second
.m_current
;
511 const KItemSet selection
= test
.second
.m_selection
;
512 const bool activated
= test
.second
.m_activated
;
514 QTest::keyClick(m_container
, key
, modifier
);
516 QCOMPARE(m_selectionManager
->currentItem(), current
);
517 switch (selectionBehavior
) {
518 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
519 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
520 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
524 switch (selectionBehavior
) {
525 case KItemListController::MultiSelection
:
526 if (!selection
.isEmpty()) {
527 // The selected items should be activated.
528 if (selection
.count() == 1) {
529 QVERIFY(!spySingleItemActivated
.isEmpty());
530 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
531 QVERIFY(spyMultipleItemsActivated
.isEmpty());
533 QVERIFY(spySingleItemActivated
.isEmpty());
534 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
535 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
539 // No items are selected. Therefore, the current item should be activated.
540 // This is handled by falling through to the NoSelection/SingleSelection case.
542 case KItemListController::NoSelection
:
543 case KItemListController::SingleSelection
:
544 // In NoSelection and SingleSelection mode, the current item should be activated.
545 QVERIFY(!spySingleItemActivated
.isEmpty());
546 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
547 QVERIFY(spyMultipleItemsActivated
.isEmpty());
554 void KItemListControllerTest::testMouseClickActivation()
556 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
558 // Make sure that we have a large window, such that
559 // the items are visible and clickable.
560 adjustGeometryForColumnCount(5);
562 // Make sure that the first item is visible in the view.
563 m_view
->setScrollOffset(0);
564 QCOMPARE(m_view
->firstVisibleIndex(), 0);
566 const QPointF pos
= m_view
->itemContextRect(0).center();
568 // Save the "single click" setting.
569 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
571 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
572 mousePressEvent
.setPos(pos
);
573 mousePressEvent
.setButton(Qt::LeftButton
);
574 mousePressEvent
.setButtons(Qt::LeftButton
);
576 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
577 mouseReleaseEvent
.setPos(pos
);
578 mouseReleaseEvent
.setButton(Qt::LeftButton
);
579 mouseReleaseEvent
.setButtons(Qt::NoButton
);
581 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
583 // Default setting: single click activation.
584 m_testStyle
->setActivateItemOnSingleClick(true);
585 m_view
->event(&mousePressEvent
);
586 m_view
->event(&mouseReleaseEvent
);
587 QCOMPARE(spyItemActivated
.count(), 1);
588 spyItemActivated
.clear();
590 // Set the global setting to "double click activation".
591 m_testStyle
->setActivateItemOnSingleClick(false);
592 m_view
->event(&mousePressEvent
);
593 m_view
->event(&mouseReleaseEvent
);
594 QCOMPARE(spyItemActivated
.count(), 0);
595 spyItemActivated
.clear();
597 // Enforce single click activation in the controller.
598 m_controller
->setSingleClickActivationEnforced(true);
599 m_view
->event(&mousePressEvent
);
600 m_view
->event(&mouseReleaseEvent
);
601 QCOMPARE(spyItemActivated
.count(), 1);
602 spyItemActivated
.clear();
604 // Do not enforce single click activation in the controller.
605 m_controller
->setSingleClickActivationEnforced(false);
606 m_view
->event(&mousePressEvent
);
607 m_view
->event(&mouseReleaseEvent
);
608 QCOMPARE(spyItemActivated
.count(), 0);
609 spyItemActivated
.clear();
611 // Set the global setting back to "single click activation".
612 m_testStyle
->setActivateItemOnSingleClick(true);
613 m_view
->event(&mousePressEvent
);
614 m_view
->event(&mouseReleaseEvent
);
615 QCOMPARE(spyItemActivated
.count(), 1);
616 spyItemActivated
.clear();
618 // Enforce single click activation in the controller.
619 m_controller
->setSingleClickActivationEnforced(true);
620 m_view
->event(&mousePressEvent
);
621 m_view
->event(&mouseReleaseEvent
);
622 QCOMPARE(spyItemActivated
.count(), 1);
623 spyItemActivated
.clear();
625 // Restore previous settings.
626 m_controller
->setSingleClickActivationEnforced(true);
627 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
630 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
632 const QSize size
= m_view
->itemSize().toSize();
634 QRect rect
= m_container
->geometry();
635 rect
.setSize(size
* count
);
636 m_container
->setGeometry(rect
);
638 // Increase the size of the container until the correct column count is reached.
639 while (m_view
->m_layouter
->m_columnCount
< count
) {
640 rect
= m_container
->geometry();
641 rect
.setSize(rect
.size() + size
);
642 m_container
->setGeometry(rect
);
646 QTEST_MAIN(KItemListControllerTest
)
648 #include "kitemlistcontrollertest.moc"