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
41 KItemListControllerTestStyle(QStyle
* style
) :
43 m_activateItemOnSingleClick((bool)style
->styleHint(SH_ItemView_ActivateItemOnSingleClick
))
47 void setActivateItemOnSingleClick(bool activateItemOnSingleClick
)
49 m_activateItemOnSingleClick
= activateItemOnSingleClick
;
52 bool activateItemOnSingleClick() const
54 return m_activateItemOnSingleClick
;
57 int styleHint(StyleHint hint
,
58 const QStyleOption
* option
= nullptr,
59 const QWidget
* widget
= nullptr,
60 QStyleHintReturn
* returnData
= nullptr) const Q_DECL_OVERRIDE
63 case QStyle::SH_ItemView_ActivateItemOnSingleClick
:
64 return (int)activateItemOnSingleClick();
66 return QProxyStyle::styleHint(hint
, option
, widget
, returnData
);
71 bool m_activateItemOnSingleClick
;
74 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
);
75 Q_DECLARE_METATYPE(Qt::Orientation
);
76 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
);
77 Q_DECLARE_METATYPE(KItemSet
);
79 class KItemListControllerTest
: public QObject
85 void cleanupTestCase();
90 void testKeyboardNavigation_data();
91 void testKeyboardNavigation();
92 void testMouseClickActivation();
96 * Make sure that the number of columns in the view is equal to \a count
97 * by changing the geometry of the container.
99 void adjustGeometryForColumnCount(int count
);
102 KFileItemListView
* m_view
;
103 KItemListController
* m_controller
;
104 KItemListSelectionManager
* m_selectionManager
;
105 KFileItemModel
* m_model
;
107 KItemListContainer
* m_container
;
108 KItemListControllerTestStyle
* m_testStyle
;
112 * This function initializes the member objects, creates the temporary files, and
113 * shows the view on the screen on startup. This could also be done before every
114 * single test, but this would make the time needed to run the test much larger.
116 void KItemListControllerTest::initTestCase()
118 qRegisterMetaType
<KItemSet
>("KItemSet");
120 m_testDir
= new TestDir();
121 m_model
= new KFileItemModel();
122 m_view
= new KFileItemListView();
123 m_controller
= new KItemListController(m_model
, m_view
, this);
124 m_container
= new KItemListContainer(m_controller
);
125 m_controller
= m_container
->controller();
126 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
127 m_selectionManager
= m_controller
->selectionManager();
128 m_testStyle
= new KItemListControllerTestStyle(m_view
->style());
129 m_view
->setStyle(m_testStyle
);
133 << "a1" << "a2" << "a3"
135 << "c1" << "c2" << "c3" << "c4" << "c5"
136 << "d1" << "d2" << "d3" << "d4"
137 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
139 m_testDir
->createFiles(files
);
140 m_model
->loadDirectory(m_testDir
->url());
141 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, SIGNAL(directoryLoadingCompleted()));
142 QVERIFY(spyDirectoryLoadingCompleted
.wait());
145 QTest::qWaitForWindowExposed(m_container
);
148 void KItemListControllerTest::cleanupTestCase()
157 /** Before each test, the current item, selection, and item size are reset to the defaults. */
158 void KItemListControllerTest::init()
160 m_selectionManager
->setCurrentItem(0);
161 QCOMPARE(m_selectionManager
->currentItem(), 0);
163 m_selectionManager
->clearSelection();
164 QVERIFY(!m_selectionManager
->hasSelection());
166 const QSizeF
itemSize(50, 50);
167 m_view
->setItemSize(itemSize
);
168 QCOMPARE(m_view
->itemSize(), itemSize
);
171 void KItemListControllerTest::cleanup()
176 * \class KeyPress is a small helper struct that represents a key press event,
177 * including the key and the keyboard modifiers.
181 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
187 Qt::KeyboardModifiers m_modifier
;
191 * \class ViewState is a small helper struct that represents a certain state
192 * of the view, including the current item, the selected items in MultiSelection
193 * mode (in the other modes, the selection is either empty or equal to the
194 * current item), and the information whether items were activated by the last
199 ViewState(int current
, const KItemSet selection
, bool activated
= false) :
201 m_selection(selection
),
202 m_activated(activated
)
206 KItemSet m_selection
;
210 // We have to define a typedef for the pair in order to make the test compile.
211 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
212 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>);
215 * This function provides the data for the actual test function
216 * KItemListControllerTest::testKeyboardNavigation().
217 * It tests all possible combinations of view layouts, selection behaviors,
218 * and enabled/disabled groupings for different column counts, and
219 * provides a list of key presses and the states that the view should be in
220 * after the key press event.
222 void KItemListControllerTest::testKeyboardNavigation_data()
224 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
225 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
226 QTest::addColumn
<int>("columnCount");
227 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
228 QTest::addColumn
<bool>("groupingEnabled");
229 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
231 QList
<KFileItemListView::ItemLayout
> layoutList
;
232 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
233 layoutList
.append(KFileItemListView::IconsLayout
);
234 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
235 layoutList
.append(KFileItemListView::CompactLayout
);
236 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
237 layoutList
.append(KFileItemListView::DetailsLayout
);
238 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
240 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
241 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
242 selectionBehaviorList
.append(KItemListController::NoSelection
);
243 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
244 selectionBehaviorList
.append(KItemListController::SingleSelection
);
245 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
246 selectionBehaviorList
.append(KItemListController::MultiSelection
);
247 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
249 QList
<bool> groupingEnabledList
;
250 QHash
<bool, QString
> groupingEnabledNames
;
251 groupingEnabledList
.append(false);
252 groupingEnabledNames
[false] = "ungrouped";
253 groupingEnabledList
.append(true);
254 groupingEnabledNames
[true] = "grouping enabled";
256 foreach (const KFileItemListView::ItemLayout
& layout
, layoutList
) {
257 // The following settings depend on the layout.
258 // Note that 'columns' are actually 'rows' in
260 Qt::Orientation scrollOrientation
;
261 QList
<int> columnCountList
;
263 Qt::Key previousItemKey
;
265 Qt::Key previousRowKey
;
268 case KFileItemListView::IconsLayout
:
269 scrollOrientation
= Qt::Vertical
;
270 columnCountList
<< 1 << 3 << 5;
271 nextItemKey
= Qt::Key_Right
;
272 previousItemKey
= Qt::Key_Left
;
273 nextRowKey
= Qt::Key_Down
;
274 previousRowKey
= Qt::Key_Up
;
276 case KFileItemListView::CompactLayout
:
277 scrollOrientation
= Qt::Horizontal
;
278 columnCountList
<< 1 << 3 << 5;
279 nextItemKey
= Qt::Key_Down
;
280 previousItemKey
= Qt::Key_Up
;
281 nextRowKey
= Qt::Key_Right
;
282 previousRowKey
= Qt::Key_Left
;
284 case KFileItemListView::DetailsLayout
:
285 scrollOrientation
= Qt::Vertical
;
286 columnCountList
<< 1;
287 nextItemKey
= Qt::Key_Down
;
288 previousItemKey
= Qt::Key_Up
;
289 nextRowKey
= Qt::Key_Down
;
290 previousRowKey
= Qt::Key_Up
;
294 foreach (int columnCount
, columnCountList
) {
295 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
296 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
297 QList
<QPair
<KeyPress
, ViewState
> > testList
;
299 // First, key presses which should have the same effect
300 // for any layout and any number of columns.
302 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
303 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
304 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
305 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
306 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
307 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
308 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
309 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
310 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
311 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
312 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
313 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
314 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
315 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
316 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
317 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
318 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
319 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
320 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
321 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
322 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
323 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
324 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
325 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
326 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
327 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
328 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
329 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
331 // Next, we test combinations of key presses which only work for a
332 // particular number of columns and either enabled or disabled grouping.
335 if (columnCount
== 1) {
337 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
338 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
339 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
340 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
341 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
342 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
345 // Multiple columns: we test both 3 and 5 columns with grouping
346 // enabled or disabled. For each case, the layout of the items
347 // in the view is shown (both using file names and indices) to
348 // make it easier to understand what the tests do.
350 if (columnCount
== 3 && !groupingEnabled
) {
351 // 3 columns, no grouping:
356 // d1 d2 d3 | 9 10 11
357 // d4 e1 e2 | 12 13 14
358 // e3 e4 e5 | 15 16 17
361 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
362 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
363 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
364 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
365 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
366 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
367 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
368 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
369 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
370 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
371 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
372 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
373 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
374 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
375 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
376 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
377 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
378 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
379 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
380 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
383 if (columnCount
== 5 && !groupingEnabled
) {
384 // 5 columns, no grouping:
386 // a1 a2 a3 b1 c1 | 0 1 2 3 4
387 // c2 c3 c4 c5 d1 | 5 6 7 8 9
388 // d2 d3 d4 e1 e2 | 10 11 12 13 14
389 // e3 e4 e5 e6 e7 | 15 16 17 18 19
391 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
392 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
393 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
394 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
395 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
396 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
397 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
398 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
399 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
400 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
401 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
404 if (columnCount
== 3 && groupingEnabled
) {
405 // 3 columns, with grouping:
411 // d1 d2 d3 | 9 10 11
413 // e1 e2 e3 | 13 14 15
414 // e4 e5 e6 | 16 17 18
417 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
418 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
419 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
420 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
421 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
422 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
423 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
424 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
425 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
426 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
427 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
428 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
429 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
432 if (columnCount
== 5 && groupingEnabled
) {
433 // 5 columns, with grouping:
437 // c1 c2 c3 c4 c5 | 4 5 6 7 8
438 // d1 d2 d3 d4 | 9 10 11 12
439 // e1 e2 e3 e4 e5 | 13 14 15 16 17
442 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
443 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
444 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
445 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
446 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
447 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
448 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
449 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
450 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
451 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
452 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
453 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
454 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
457 const QString testName
=
458 layoutNames
[layout
] + ", " +
459 QString("%1 columns, ").arg(columnCount
) +
460 selectionBehaviorNames
[selectionBehavior
] + ", " +
461 groupingEnabledNames
[groupingEnabled
];
463 const QByteArray testNameAscii
= testName
.toLatin1();
465 QTest::newRow(testNameAscii
.data())
479 * This function sets the view's properties according to the data provided by
480 * KItemListControllerTest::testKeyboardNavigation_data().
482 * The list \a testList contains pairs of key presses, which are sent to the
483 * container, and expected view states, which are verified then.
485 void KItemListControllerTest::testKeyboardNavigation()
487 QFETCH(KFileItemListView::ItemLayout
, layout
);
488 QFETCH(Qt::Orientation
, scrollOrientation
);
489 QFETCH(int, columnCount
);
490 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
491 QFETCH(bool, groupingEnabled
);
492 QFETCH(QList
<keyPressViewStatePair
>, testList
);
494 m_view
->setItemLayout(layout
);
495 QCOMPARE(m_view
->itemLayout(), layout
);
497 m_view
->setScrollOrientation(scrollOrientation
);
498 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
500 m_controller
->setSelectionBehavior(selectionBehavior
);
501 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
503 m_model
->setGroupedSorting(groupingEnabled
);
504 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
506 adjustGeometryForColumnCount(columnCount
);
507 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
509 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
510 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(KItemSet
)));
512 while (!testList
.isEmpty()) {
513 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
514 const Qt::Key key
= test
.first
.m_key
;
515 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
516 const int current
= test
.second
.m_current
;
517 const KItemSet selection
= test
.second
.m_selection
;
518 const bool activated
= test
.second
.m_activated
;
520 QTest::keyClick(m_container
, key
, modifier
);
522 QCOMPARE(m_selectionManager
->currentItem(), current
);
523 switch (selectionBehavior
) {
524 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
525 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
526 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
530 switch (selectionBehavior
) {
531 case KItemListController::MultiSelection
:
532 if (!selection
.isEmpty()) {
533 // The selected items should be activated.
534 if (selection
.count() == 1) {
535 QVERIFY(!spySingleItemActivated
.isEmpty());
536 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
537 QVERIFY(spyMultipleItemsActivated
.isEmpty());
539 QVERIFY(spySingleItemActivated
.isEmpty());
540 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
541 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
545 // No items are selected. Therefore, the current item should be activated.
546 // This is handled by falling through to the NoSelection/SingleSelection case.
547 case KItemListController::NoSelection
:
548 case KItemListController::SingleSelection
:
549 // In NoSelection and SingleSelection mode, the current item should be activated.
550 QVERIFY(!spySingleItemActivated
.isEmpty());
551 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
552 QVERIFY(spyMultipleItemsActivated
.isEmpty());
559 void KItemListControllerTest::testMouseClickActivation()
561 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
563 // Make sure that we have a large window, such that
564 // the items are visible and clickable.
565 adjustGeometryForColumnCount(5);
567 // Make sure that the first item is visible in the view.
568 m_view
->setScrollOffset(0);
569 QCOMPARE(m_view
->firstVisibleIndex(), 0);
571 const QPointF pos
= m_view
->itemContextRect(0).center();
573 // Save the "single click" setting.
574 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
576 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
577 mousePressEvent
.setPos(pos
);
578 mousePressEvent
.setButton(Qt::LeftButton
);
579 mousePressEvent
.setButtons(Qt::LeftButton
);
581 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
582 mouseReleaseEvent
.setPos(pos
);
583 mouseReleaseEvent
.setButton(Qt::LeftButton
);
584 mouseReleaseEvent
.setButtons(Qt::NoButton
);
586 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
588 // Default setting: single click activation.
589 m_testStyle
->setActivateItemOnSingleClick(true);
590 m_view
->event(&mousePressEvent
);
591 m_view
->event(&mouseReleaseEvent
);
592 QCOMPARE(spyItemActivated
.count(), 1);
593 spyItemActivated
.clear();
595 // Set the global setting to "double click activation".
596 m_testStyle
->setActivateItemOnSingleClick(false);
597 m_view
->event(&mousePressEvent
);
598 m_view
->event(&mouseReleaseEvent
);
599 QCOMPARE(spyItemActivated
.count(), 0);
600 spyItemActivated
.clear();
602 // Enforce single click activation in the controller.
603 m_controller
->setSingleClickActivationEnforced(true);
604 m_view
->event(&mousePressEvent
);
605 m_view
->event(&mouseReleaseEvent
);
606 QCOMPARE(spyItemActivated
.count(), 1);
607 spyItemActivated
.clear();
609 // Do not enforce single click activation in the controller.
610 m_controller
->setSingleClickActivationEnforced(false);
611 m_view
->event(&mousePressEvent
);
612 m_view
->event(&mouseReleaseEvent
);
613 QCOMPARE(spyItemActivated
.count(), 0);
614 spyItemActivated
.clear();
616 // Set the global setting back to "single click activation".
617 m_testStyle
->setActivateItemOnSingleClick(true);
618 m_view
->event(&mousePressEvent
);
619 m_view
->event(&mouseReleaseEvent
);
620 QCOMPARE(spyItemActivated
.count(), 1);
621 spyItemActivated
.clear();
623 // Enforce single click activation in the controller.
624 m_controller
->setSingleClickActivationEnforced(true);
625 m_view
->event(&mousePressEvent
);
626 m_view
->event(&mouseReleaseEvent
);
627 QCOMPARE(spyItemActivated
.count(), 1);
628 spyItemActivated
.clear();
630 // Restore previous settings.
631 m_controller
->setSingleClickActivationEnforced(true);
632 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
635 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
637 const QSize size
= m_view
->itemSize().toSize();
639 QRect rect
= m_container
->geometry();
640 rect
.setSize(size
* count
);
641 m_container
->setGeometry(rect
);
643 // Increase the size of the container until the correct column count is reached.
644 while (m_view
->m_layouter
->m_columnCount
< count
) {
645 rect
= m_container
->geometry();
646 rect
.setSize(rect
.size() + size
);
647 m_container
->setGeometry(rect
);
651 QTEST_MAIN(KItemListControllerTest
)
653 #include "kitemlistcontrollertest.moc"