1 /***************************************************************************
2 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kitemviews/kitemlistcontainer.h"
21 #include "kitemviews/kfileitemlistview.h"
22 #include "kitemviews/kfileitemmodel.h"
23 #include "kitemviews/kitemlistcontroller.h"
24 #include "kitemviews/kitemlistselectionmanager.h"
25 #include "kitemviews/private/kitemlistviewlayouter.h"
29 #include <QGraphicsSceneMouseEvent>
31 #include <QProxyStyle>
34 * \class KItemListControllerTestStyle is a proxy style for testing the
35 * KItemListController with different style hint options, e.g. single/double
38 class KItemListControllerTestStyle
: public QProxyStyle
42 KItemListControllerTestStyle(QStyle
* style
) :
44 m_activateItemOnSingleClick((bool)style
->styleHint(SH_ItemView_ActivateItemOnSingleClick
))
48 void setActivateItemOnSingleClick(bool activateItemOnSingleClick
)
50 m_activateItemOnSingleClick
= activateItemOnSingleClick
;
53 bool activateItemOnSingleClick() const
55 return m_activateItemOnSingleClick
;
58 int styleHint(StyleHint hint
,
59 const QStyleOption
* option
= nullptr,
60 const QWidget
* widget
= nullptr,
61 QStyleHintReturn
* returnData
= nullptr) const override
64 case QStyle::SH_ItemView_ActivateItemOnSingleClick
:
65 return (int)activateItemOnSingleClick();
67 return QProxyStyle::styleHint(hint
, option
, widget
, returnData
);
72 bool m_activateItemOnSingleClick
;
75 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
)
76 Q_DECLARE_METATYPE(Qt::Orientation
)
77 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
)
78 Q_DECLARE_METATYPE(KItemSet
)
80 class KItemListControllerTest
: public QObject
86 void cleanupTestCase();
91 void testKeyboardNavigation_data();
92 void testKeyboardNavigation();
93 void testMouseClickActivation();
97 * Make sure that the number of columns in the view is equal to \a count
98 * by changing the geometry of the container.
100 void adjustGeometryForColumnCount(int count
);
103 KFileItemListView
* m_view
;
104 KItemListController
* m_controller
;
105 KItemListSelectionManager
* m_selectionManager
;
106 KFileItemModel
* m_model
;
108 KItemListContainer
* m_container
;
109 KItemListControllerTestStyle
* m_testStyle
;
113 * This function initializes the member objects, creates the temporary files, and
114 * shows the view on the screen on startup. This could also be done before every
115 * single test, but this would make the time needed to run the test much larger.
117 void KItemListControllerTest::initTestCase()
119 qRegisterMetaType
<KItemSet
>("KItemSet");
121 m_testDir
= new TestDir();
122 m_model
= new KFileItemModel();
123 m_view
= new KFileItemListView();
124 m_controller
= new KItemListController(m_model
, m_view
, this);
125 m_container
= new KItemListContainer(m_controller
);
126 m_controller
= m_container
->controller();
127 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
128 m_selectionManager
= m_controller
->selectionManager();
129 m_testStyle
= new KItemListControllerTestStyle(m_view
->style());
130 m_view
->setStyle(m_testStyle
);
134 << "a1" << "a2" << "a3"
136 << "c1" << "c2" << "c3" << "c4" << "c5"
137 << "d1" << "d2" << "d3" << "d4"
138 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
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.
182 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
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
200 ViewState(int current
, const KItemSet
&selection
, bool activated
= false) :
202 m_selection(selection
),
203 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 foreach (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 foreach (int columnCount
, columnCountList
) {
296 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
297 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
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.
303 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
304 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
305 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
306 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
307 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
308 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
309 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
310 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
311 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
312 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
313 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
314 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
315 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
316 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
317 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
318 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
319 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
320 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
321 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
322 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
323 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
324 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
325 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
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) {
344 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
345 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
346 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
347 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
348 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
349 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
352 // Multiple columns: we test both 3 and 5 columns with grouping
353 // enabled or disabled. For each case, the layout of the items
354 // in the view is shown (both using file names and indices) to
355 // make it easier to understand what the tests do.
357 if (columnCount
== 3 && !groupingEnabled
) {
358 // 3 columns, no grouping:
363 // d1 d2 d3 | 9 10 11
364 // d4 e1 e2 | 12 13 14
365 // e3 e4 e5 | 15 16 17
368 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
369 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
370 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
371 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
372 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
373 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
374 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
375 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
376 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
377 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
378 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
379 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
380 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
381 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
382 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
383 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
384 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
385 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
386 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
387 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
390 if (columnCount
== 5 && !groupingEnabled
) {
391 // 5 columns, no grouping:
393 // a1 a2 a3 b1 c1 | 0 1 2 3 4
394 // c2 c3 c4 c5 d1 | 5 6 7 8 9
395 // d2 d3 d4 e1 e2 | 10 11 12 13 14
396 // e3 e4 e5 e6 e7 | 15 16 17 18 19
398 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
399 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
400 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
401 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
402 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
403 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
404 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
405 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
406 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
407 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
408 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
411 if (columnCount
== 3 && groupingEnabled
) {
412 // 3 columns, with grouping:
418 // d1 d2 d3 | 9 10 11
420 // e1 e2 e3 | 13 14 15
421 // e4 e5 e6 | 16 17 18
424 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
425 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
426 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
427 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
428 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
429 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
430 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
431 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
432 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
433 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
434 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
435 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
436 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
439 if (columnCount
== 5 && groupingEnabled
) {
440 // 5 columns, with grouping:
444 // c1 c2 c3 c4 c5 | 4 5 6 7 8
445 // d1 d2 d3 d4 | 9 10 11 12
446 // e1 e2 e3 e4 e5 | 13 14 15 16 17
449 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
450 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
451 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
452 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
453 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
454 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
455 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
456 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
457 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
458 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
459 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
460 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
461 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
464 const QString testName
=
465 layoutNames
[layout
] + ", " +
466 QString("%1 columns, ").arg(columnCount
) +
467 selectionBehaviorNames
[selectionBehavior
] + ", " +
468 groupingEnabledNames
[groupingEnabled
];
470 const QByteArray testNameAscii
= testName
.toLatin1();
472 QTest::newRow(testNameAscii
.data())
486 * This function sets the view's properties according to the data provided by
487 * KItemListControllerTest::testKeyboardNavigation_data().
489 * The list \a testList contains pairs of key presses, which are sent to the
490 * container, and expected view states, which are verified then.
492 void KItemListControllerTest::testKeyboardNavigation()
494 QFETCH(KFileItemListView::ItemLayout
, layout
);
495 QFETCH(Qt::Orientation
, scrollOrientation
);
496 QFETCH(int, columnCount
);
497 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
498 QFETCH(bool, groupingEnabled
);
499 QFETCH(QList
<keyPressViewStatePair
>, testList
);
501 m_view
->setItemLayout(layout
);
502 QCOMPARE(m_view
->itemLayout(), layout
);
504 m_view
->setScrollOrientation(scrollOrientation
);
505 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
507 m_controller
->setSelectionBehavior(selectionBehavior
);
508 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
510 m_model
->setGroupedSorting(groupingEnabled
);
511 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
513 adjustGeometryForColumnCount(columnCount
);
514 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
516 QSignalSpy
spySingleItemActivated(m_controller
, &KItemListController::itemActivated
);
517 QSignalSpy
spyMultipleItemsActivated(m_controller
, &KItemListController::itemsActivated
);
519 while (!testList
.isEmpty()) {
520 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
521 const Qt::Key key
= test
.first
.m_key
;
522 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
523 const int current
= test
.second
.m_current
;
524 const KItemSet selection
= test
.second
.m_selection
;
525 const bool activated
= test
.second
.m_activated
;
527 QTest::keyClick(m_container
, key
, modifier
);
529 QCOMPARE(m_selectionManager
->currentItem(), current
);
530 switch (selectionBehavior
) {
531 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
532 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
533 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
537 switch (selectionBehavior
) {
538 case KItemListController::MultiSelection
:
539 if (!selection
.isEmpty()) {
540 // The selected items should be activated.
541 if (selection
.count() == 1) {
542 QVERIFY(!spySingleItemActivated
.isEmpty());
543 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
544 QVERIFY(spyMultipleItemsActivated
.isEmpty());
546 QVERIFY(spySingleItemActivated
.isEmpty());
547 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
548 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
552 // No items are selected. Therefore, the current item should be activated.
553 // This is handled by falling through to the NoSelection/SingleSelection case.
555 case KItemListController::NoSelection
:
556 case KItemListController::SingleSelection
:
557 // In NoSelection and SingleSelection mode, the current item should be activated.
558 QVERIFY(!spySingleItemActivated
.isEmpty());
559 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
560 QVERIFY(spyMultipleItemsActivated
.isEmpty());
567 void KItemListControllerTest::testMouseClickActivation()
569 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
571 // Make sure that we have a large window, such that
572 // the items are visible and clickable.
573 adjustGeometryForColumnCount(5);
575 // Make sure that the first item is visible in the view.
576 m_view
->setScrollOffset(0);
577 QCOMPARE(m_view
->firstVisibleIndex(), 0);
579 const QPointF pos
= m_view
->itemContextRect(0).center();
581 // Save the "single click" setting.
582 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
584 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
585 mousePressEvent
.setPos(pos
);
586 mousePressEvent
.setButton(Qt::LeftButton
);
587 mousePressEvent
.setButtons(Qt::LeftButton
);
589 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
590 mouseReleaseEvent
.setPos(pos
);
591 mouseReleaseEvent
.setButton(Qt::LeftButton
);
592 mouseReleaseEvent
.setButtons(Qt::NoButton
);
594 QSignalSpy
spyItemActivated(m_controller
, &KItemListController::itemActivated
);
596 // Default setting: single click activation.
597 m_testStyle
->setActivateItemOnSingleClick(true);
598 m_view
->event(&mousePressEvent
);
599 m_view
->event(&mouseReleaseEvent
);
600 QCOMPARE(spyItemActivated
.count(), 1);
601 spyItemActivated
.clear();
603 // Set the global setting to "double click activation".
604 m_testStyle
->setActivateItemOnSingleClick(false);
605 m_view
->event(&mousePressEvent
);
606 m_view
->event(&mouseReleaseEvent
);
607 QCOMPARE(spyItemActivated
.count(), 0);
608 spyItemActivated
.clear();
610 // Enforce single click activation in the controller.
611 m_controller
->setSingleClickActivationEnforced(true);
612 m_view
->event(&mousePressEvent
);
613 m_view
->event(&mouseReleaseEvent
);
614 QCOMPARE(spyItemActivated
.count(), 1);
615 spyItemActivated
.clear();
617 // Do not enforce single click activation in the controller.
618 m_controller
->setSingleClickActivationEnforced(false);
619 m_view
->event(&mousePressEvent
);
620 m_view
->event(&mouseReleaseEvent
);
621 QCOMPARE(spyItemActivated
.count(), 0);
622 spyItemActivated
.clear();
624 // Set the global setting back to "single click activation".
625 m_testStyle
->setActivateItemOnSingleClick(true);
626 m_view
->event(&mousePressEvent
);
627 m_view
->event(&mouseReleaseEvent
);
628 QCOMPARE(spyItemActivated
.count(), 1);
629 spyItemActivated
.clear();
631 // Enforce single click activation in the controller.
632 m_controller
->setSingleClickActivationEnforced(true);
633 m_view
->event(&mousePressEvent
);
634 m_view
->event(&mouseReleaseEvent
);
635 QCOMPARE(spyItemActivated
.count(), 1);
636 spyItemActivated
.clear();
638 // Restore previous settings.
639 m_controller
->setSingleClickActivationEnforced(true);
640 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
643 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
645 const QSize size
= m_view
->itemSize().toSize();
647 QRect rect
= m_container
->geometry();
648 rect
.setSize(size
* count
);
649 m_container
->setGeometry(rect
);
651 // Increase the size of the container until the correct column count is reached.
652 while (m_view
->m_layouter
->m_columnCount
< count
) {
653 rect
= m_container
->geometry();
654 rect
.setSize(rect
.size() + size
);
655 m_container
->setGeometry(rect
);
659 QTEST_MAIN(KItemListControllerTest
)
661 #include "kitemlistcontrollertest.moc"