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 Q_DECL_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
, SIGNAL(directoryLoadingCompleted()));
143 QVERIFY(spyDirectoryLoadingCompleted
.wait());
146 QTest::qWaitForWindowExposed(m_container
);
149 void KItemListControllerTest::cleanupTestCase()
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_Home
), ViewState(0, KItemSet() << 0))
330 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
332 // Next, we test combinations of key presses which only work for a
333 // particular number of columns and either enabled or disabled grouping.
336 if (columnCount
== 1) {
338 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
339 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
340 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
341 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
342 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
343 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
346 // Multiple columns: we test both 3 and 5 columns with grouping
347 // enabled or disabled. For each case, the layout of the items
348 // in the view is shown (both using file names and indices) to
349 // make it easier to understand what the tests do.
351 if (columnCount
== 3 && !groupingEnabled
) {
352 // 3 columns, no grouping:
357 // d1 d2 d3 | 9 10 11
358 // d4 e1 e2 | 12 13 14
359 // e3 e4 e5 | 15 16 17
362 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
363 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
364 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
365 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
366 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
367 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
368 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
369 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
370 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
371 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
372 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
373 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
374 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
375 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
376 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
377 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
378 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
379 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
380 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
381 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
384 if (columnCount
== 5 && !groupingEnabled
) {
385 // 5 columns, no grouping:
387 // a1 a2 a3 b1 c1 | 0 1 2 3 4
388 // c2 c3 c4 c5 d1 | 5 6 7 8 9
389 // d2 d3 d4 e1 e2 | 10 11 12 13 14
390 // e3 e4 e5 e6 e7 | 15 16 17 18 19
392 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
393 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
394 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
395 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
396 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
397 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
398 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
399 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
400 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
401 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
402 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
405 if (columnCount
== 3 && groupingEnabled
) {
406 // 3 columns, with grouping:
412 // d1 d2 d3 | 9 10 11
414 // e1 e2 e3 | 13 14 15
415 // e4 e5 e6 | 16 17 18
418 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
419 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
420 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
421 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
422 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
423 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
424 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
425 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
426 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
427 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
428 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
429 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
430 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
433 if (columnCount
== 5 && groupingEnabled
) {
434 // 5 columns, with grouping:
438 // c1 c2 c3 c4 c5 | 4 5 6 7 8
439 // d1 d2 d3 d4 | 9 10 11 12
440 // e1 e2 e3 e4 e5 | 13 14 15 16 17
443 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
444 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
445 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
446 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
447 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
448 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
449 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
450 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
451 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
452 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
453 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
454 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
455 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
458 const QString testName
=
459 layoutNames
[layout
] + ", " +
460 QString("%1 columns, ").arg(columnCount
) +
461 selectionBehaviorNames
[selectionBehavior
] + ", " +
462 groupingEnabledNames
[groupingEnabled
];
464 const QByteArray testNameAscii
= testName
.toLatin1();
466 QTest::newRow(testNameAscii
.data())
480 * This function sets the view's properties according to the data provided by
481 * KItemListControllerTest::testKeyboardNavigation_data().
483 * The list \a testList contains pairs of key presses, which are sent to the
484 * container, and expected view states, which are verified then.
486 void KItemListControllerTest::testKeyboardNavigation()
488 QFETCH(KFileItemListView::ItemLayout
, layout
);
489 QFETCH(Qt::Orientation
, scrollOrientation
);
490 QFETCH(int, columnCount
);
491 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
492 QFETCH(bool, groupingEnabled
);
493 QFETCH(QList
<keyPressViewStatePair
>, testList
);
495 m_view
->setItemLayout(layout
);
496 QCOMPARE(m_view
->itemLayout(), layout
);
498 m_view
->setScrollOrientation(scrollOrientation
);
499 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
501 m_controller
->setSelectionBehavior(selectionBehavior
);
502 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
504 m_model
->setGroupedSorting(groupingEnabled
);
505 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
507 adjustGeometryForColumnCount(columnCount
);
508 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
510 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
511 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(KItemSet
)));
513 while (!testList
.isEmpty()) {
514 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
515 const Qt::Key key
= test
.first
.m_key
;
516 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
517 const int current
= test
.second
.m_current
;
518 const KItemSet selection
= test
.second
.m_selection
;
519 const bool activated
= test
.second
.m_activated
;
521 QTest::keyClick(m_container
, key
, modifier
);
523 QCOMPARE(m_selectionManager
->currentItem(), current
);
524 switch (selectionBehavior
) {
525 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
526 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
527 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
531 switch (selectionBehavior
) {
532 case KItemListController::MultiSelection
:
533 if (!selection
.isEmpty()) {
534 // The selected items should be activated.
535 if (selection
.count() == 1) {
536 QVERIFY(!spySingleItemActivated
.isEmpty());
537 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
538 QVERIFY(spyMultipleItemsActivated
.isEmpty());
540 QVERIFY(spySingleItemActivated
.isEmpty());
541 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
542 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
546 // No items are selected. Therefore, the current item should be activated.
547 // This is handled by falling through to the NoSelection/SingleSelection case.
548 case KItemListController::NoSelection
:
549 case KItemListController::SingleSelection
:
550 // In NoSelection and SingleSelection mode, the current item should be activated.
551 QVERIFY(!spySingleItemActivated
.isEmpty());
552 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
553 QVERIFY(spyMultipleItemsActivated
.isEmpty());
560 void KItemListControllerTest::testMouseClickActivation()
562 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
564 // Make sure that we have a large window, such that
565 // the items are visible and clickable.
566 adjustGeometryForColumnCount(5);
568 // Make sure that the first item is visible in the view.
569 m_view
->setScrollOffset(0);
570 QCOMPARE(m_view
->firstVisibleIndex(), 0);
572 const QPointF pos
= m_view
->itemContextRect(0).center();
574 // Save the "single click" setting.
575 const bool restoreSettingsSingleClick
= m_testStyle
->activateItemOnSingleClick();
577 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
578 mousePressEvent
.setPos(pos
);
579 mousePressEvent
.setButton(Qt::LeftButton
);
580 mousePressEvent
.setButtons(Qt::LeftButton
);
582 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
583 mouseReleaseEvent
.setPos(pos
);
584 mouseReleaseEvent
.setButton(Qt::LeftButton
);
585 mouseReleaseEvent
.setButtons(Qt::NoButton
);
587 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
589 // Default setting: single click activation.
590 m_testStyle
->setActivateItemOnSingleClick(true);
591 m_view
->event(&mousePressEvent
);
592 m_view
->event(&mouseReleaseEvent
);
593 QCOMPARE(spyItemActivated
.count(), 1);
594 spyItemActivated
.clear();
596 // Set the global setting to "double click activation".
597 m_testStyle
->setActivateItemOnSingleClick(false);
598 m_view
->event(&mousePressEvent
);
599 m_view
->event(&mouseReleaseEvent
);
600 QCOMPARE(spyItemActivated
.count(), 0);
601 spyItemActivated
.clear();
603 // Enforce single click activation in the controller.
604 m_controller
->setSingleClickActivationEnforced(true);
605 m_view
->event(&mousePressEvent
);
606 m_view
->event(&mouseReleaseEvent
);
607 QCOMPARE(spyItemActivated
.count(), 1);
608 spyItemActivated
.clear();
610 // Do not enforce single click activation in the controller.
611 m_controller
->setSingleClickActivationEnforced(false);
612 m_view
->event(&mousePressEvent
);
613 m_view
->event(&mouseReleaseEvent
);
614 QCOMPARE(spyItemActivated
.count(), 0);
615 spyItemActivated
.clear();
617 // Set the global setting back to "single click activation".
618 m_testStyle
->setActivateItemOnSingleClick(true);
619 m_view
->event(&mousePressEvent
);
620 m_view
->event(&mouseReleaseEvent
);
621 QCOMPARE(spyItemActivated
.count(), 1);
622 spyItemActivated
.clear();
624 // Enforce single click activation in the controller.
625 m_controller
->setSingleClickActivationEnforced(true);
626 m_view
->event(&mousePressEvent
);
627 m_view
->event(&mouseReleaseEvent
);
628 QCOMPARE(spyItemActivated
.count(), 1);
629 spyItemActivated
.clear();
631 // Restore previous settings.
632 m_controller
->setSingleClickActivationEnforced(true);
633 m_testStyle
->setActivateItemOnSingleClick(restoreSettingsSingleClick
);
636 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
638 const QSize size
= m_view
->itemSize().toSize();
640 QRect rect
= m_container
->geometry();
641 rect
.setSize(size
* count
);
642 m_container
->setGeometry(rect
);
644 // Increase the size of the container until the correct column count is reached.
645 while (m_view
->m_layouter
->m_columnCount
< count
) {
646 rect
= m_container
->geometry();
647 rect
.setSize(rect
.size() + size
);
648 m_container
->setGeometry(rect
);
652 QTEST_MAIN(KItemListControllerTest
)
654 #include "kitemlistcontrollertest.moc"