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 <qtest_kde.h>
21 #include <qtestmouse.h>
22 #include <qtestkeyboard.h>
24 #include "kitemviews/kitemlistcontainer.h"
25 #include "kitemviews/kfileitemlistview.h"
26 #include "kitemviews/kfileitemmodel.h"
27 #include "kitemviews/kitemlistcontroller.h"
28 #include "kitemviews/kitemlistselectionmanager.h"
29 #include "kitemviews/private/kitemlistviewlayouter.h"
32 #include <KConfigGroup>
33 #include <KGlobalSettings>
35 #include <QGraphicsSceneMouseEvent>
38 const int DefaultTimeout
= 2000;
41 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
);
42 Q_DECLARE_METATYPE(Qt::Orientation
);
43 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
);
44 Q_DECLARE_METATYPE(QSet
<int>);
46 class KItemListControllerTest
: public QObject
52 void cleanupTestCase();
57 void testKeyboardNavigation_data();
58 void testKeyboardNavigation();
59 void testMouseClickActivation();
63 * Make sure that the number of columns in the view is equal to \a count
64 * by changing the geometry of the container.
66 void adjustGeometryForColumnCount(int count
);
69 KFileItemListView
* m_view
;
70 KItemListController
* m_controller
;
71 KItemListSelectionManager
* m_selectionManager
;
72 KFileItemModel
* m_model
;
74 KItemListContainer
* m_container
;
78 * This function initializes the member objects, creates the temporary files, and
79 * shows the view on the screen on startup. This could also be done before every
80 * single test, but this would make the time needed to run the test much larger.
82 void KItemListControllerTest::initTestCase()
84 qRegisterMetaType
<QSet
<int> >("QSet<int>");
86 m_testDir
= new TestDir();
87 m_model
= new KFileItemModel();
88 m_view
= new KFileItemListView();
89 m_controller
= new KItemListController(m_model
, m_view
, this);
90 m_container
= new KItemListContainer(m_controller
);
91 m_controller
= m_container
->controller();
92 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
93 m_selectionManager
= m_controller
->selectionManager();
97 << "a1" << "a2" << "a3"
99 << "c1" << "c2" << "c3" << "c4" << "c5"
100 << "d1" << "d2" << "d3" << "d4"
101 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
103 m_testDir
->createFiles(files
);
104 m_model
->loadDirectory(m_testDir
->url());
105 QVERIFY(QTest::kWaitForSignal(m_model
, SIGNAL(directoryLoadingCompleted()), DefaultTimeout
));
108 QTest::qWaitForWindowShown(m_container
);
111 void KItemListControllerTest::cleanupTestCase()
120 /** Before each test, the current item, selection, and item size are reset to the defaults. */
121 void KItemListControllerTest::init()
123 m_selectionManager
->setCurrentItem(0);
124 QCOMPARE(m_selectionManager
->currentItem(), 0);
126 m_selectionManager
->clearSelection();
127 QVERIFY(!m_selectionManager
->hasSelection());
129 const QSizeF
itemSize(50, 50);
130 m_view
->setItemSize(itemSize
);
131 QCOMPARE(m_view
->itemSize(), itemSize
);
134 void KItemListControllerTest::cleanup()
139 * \class KeyPress is a small helper struct that represents a key press event,
140 * including the key and the keyboard modifiers.
144 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
150 Qt::KeyboardModifiers m_modifier
;
154 * \class ViewState is a small helper struct that represents a certain state
155 * of the view, including the current item, the selected items in MultiSelection
156 * mode (in the other modes, the selection is either empty or equal to the
157 * current item), and the information whether items were activated by the last
162 ViewState(int current
, const QSet
<int> selection
, bool activated
= false) :
164 m_selection(selection
),
165 m_activated(activated
)
169 QSet
<int> m_selection
;
173 // We have to define a typedef for the pair in order to make the test compile.
174 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
175 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>);
178 * This function provides the data for the actual test function
179 * KItemListControllerTest::testKeyboardNavigation().
180 * It tests all possible combinations of view layouts, selection behaviors,
181 * and enabled/disabled groupings for different column counts, and
182 * provides a list of key presses and the states that the view should be in
183 * after the key press event.
185 void KItemListControllerTest::testKeyboardNavigation_data()
187 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
188 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
189 QTest::addColumn
<int>("columnCount");
190 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
191 QTest::addColumn
<bool>("groupingEnabled");
192 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
194 QList
<KFileItemListView::ItemLayout
> layoutList
;
195 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
196 layoutList
.append(KFileItemListView::IconsLayout
);
197 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
198 layoutList
.append(KFileItemListView::CompactLayout
);
199 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
200 layoutList
.append(KFileItemListView::DetailsLayout
);
201 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
203 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
204 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
205 selectionBehaviorList
.append(KItemListController::NoSelection
);
206 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
207 selectionBehaviorList
.append(KItemListController::SingleSelection
);
208 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
209 selectionBehaviorList
.append(KItemListController::MultiSelection
);
210 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
212 QList
<bool> groupingEnabledList
;
213 QHash
<bool, QString
> groupingEnabledNames
;
214 groupingEnabledList
.append(false);
215 groupingEnabledNames
[false] = "ungrouped";
216 groupingEnabledList
.append(true);
217 groupingEnabledNames
[true] = "grouping enabled";
219 foreach (const KFileItemListView::ItemLayout
& layout
, layoutList
) {
220 // The following settings depend on the layout.
221 // Note that 'columns' are actually 'rows' in
223 Qt::Orientation scrollOrientation
;
224 QList
<int> columnCountList
;
226 Qt::Key previousItemKey
;
228 Qt::Key previousRowKey
;
231 case KFileItemListView::IconsLayout
:
232 scrollOrientation
= Qt::Vertical
;
233 columnCountList
<< 1 << 3 << 5;
234 nextItemKey
= Qt::Key_Right
;
235 previousItemKey
= Qt::Key_Left
;
236 nextRowKey
= Qt::Key_Down
;
237 previousRowKey
= Qt::Key_Up
;
239 case KFileItemListView::CompactLayout
:
240 scrollOrientation
= Qt::Horizontal
;
241 columnCountList
<< 1 << 3 << 5;
242 nextItemKey
= Qt::Key_Down
;
243 previousItemKey
= Qt::Key_Up
;
244 nextRowKey
= Qt::Key_Right
;
245 previousRowKey
= Qt::Key_Left
;
247 case KFileItemListView::DetailsLayout
:
248 scrollOrientation
= Qt::Vertical
;
249 columnCountList
<< 1;
250 nextItemKey
= Qt::Key_Down
;
251 previousItemKey
= Qt::Key_Up
;
252 nextRowKey
= Qt::Key_Down
;
253 previousRowKey
= Qt::Key_Up
;
257 foreach (int columnCount
, columnCountList
) {
258 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
259 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
260 QList
<QPair
<KeyPress
, ViewState
> > testList
;
262 // First, key presses which should have the same effect
263 // for any layout and any number of columns.
265 << qMakePair(KeyPress(nextItemKey
), ViewState(1, QSet
<int>() << 1))
266 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, QSet
<int>() << 1, true))
267 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, QSet
<int>() << 1, true))
268 << qMakePair(KeyPress(nextItemKey
), ViewState(2, QSet
<int>() << 2))
269 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 2 << 3))
270 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, QSet
<int>() << 2 << 3, true))
271 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, QSet
<int>() << 2))
272 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 2 << 3))
273 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, QSet
<int>() << 2 << 3))
274 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, QSet
<int>() << 2 << 3, true))
275 << qMakePair(KeyPress(previousItemKey
), ViewState(3, QSet
<int>() << 3))
276 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, QSet
<int>() << 0 << 1 << 2 << 3))
277 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, QSet
<int>() << 0 << 1 << 2 << 3))
278 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, QSet
<int>() << 0 << 2 << 3))
279 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, QSet
<int>() << 0 << 1 << 2 << 3))
280 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, QSet
<int>() << 19))
281 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, QSet
<int>() << 18 << 19))
282 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0))
283 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, QSet
<int>()))
284 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, QSet
<int>(), true))
285 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, QSet
<int>() << 0))
286 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, QSet
<int>()))
287 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, QSet
<int>() << 0))
288 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, QSet
<int>() << 13))
289 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, QSet
<int>() << 14))
290 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, QSet
<int>() << 15))
291 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0))
292 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, QSet
<int>()));
294 // Next, we test combinations of key presses which only work for a
295 // particular number of columns and either enabled or disabled grouping.
298 if (columnCount
== 1) {
300 << qMakePair(KeyPress(nextRowKey
), ViewState(1, QSet
<int>() << 1))
301 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, QSet
<int>() << 1 << 2))
302 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, QSet
<int>() << 1 << 2))
303 << qMakePair(KeyPress(previousRowKey
), ViewState(2, QSet
<int>() << 2))
304 << qMakePair(KeyPress(previousItemKey
), ViewState(1, QSet
<int>() << 1))
305 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
308 // Multiple columns: we test both 3 and 5 columns with grouping
309 // enabled or disabled. For each case, the layout of the items
310 // in the view is shown (both using file names and indices) to
311 // make it easier to understand what the tests do.
313 if (columnCount
== 3 && !groupingEnabled
) {
314 // 3 columns, no grouping:
319 // d1 d2 d3 | 9 10 11
320 // d4 e1 e2 | 12 13 14
321 // e3 e4 e5 | 15 16 17
324 << qMakePair(KeyPress(nextRowKey
), ViewState(3, QSet
<int>() << 3))
325 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, QSet
<int>() << 3))
326 << qMakePair(KeyPress(nextRowKey
), ViewState(7, QSet
<int>() << 7))
327 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, QSet
<int>() << 7 << 8))
328 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, QSet
<int>() << 7 << 8 << 9))
329 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, QSet
<int>() << 7 << 8))
330 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7))
331 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 6 << 7))
332 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, QSet
<int>() << 5 << 6 << 7))
333 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 6 << 7))
334 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7))
335 << qMakePair(KeyPress(nextRowKey
), ViewState(10, QSet
<int>() << 10))
336 << qMakePair(KeyPress(nextItemKey
), ViewState(11, QSet
<int>() << 11))
337 << qMakePair(KeyPress(nextRowKey
), ViewState(14, QSet
<int>() << 14))
338 << qMakePair(KeyPress(nextRowKey
), ViewState(17, QSet
<int>() << 17))
339 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
340 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
341 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, QSet
<int>() << 19))
342 << qMakePair(KeyPress(previousRowKey
), ViewState(16, QSet
<int>() << 16))
343 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
346 if (columnCount
== 5 && !groupingEnabled
) {
347 // 5 columns, no grouping:
349 // a1 a2 a3 b1 c1 | 0 1 2 3 4
350 // c2 c3 c4 c5 d1 | 5 6 7 8 9
351 // d2 d3 d4 e1 e2 | 10 11 12 13 14
352 // e3 e4 e5 e6 e7 | 15 16 17 18 19
354 << qMakePair(KeyPress(nextRowKey
), ViewState(5, QSet
<int>() << 5))
355 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, QSet
<int>() << 5))
356 << qMakePair(KeyPress(nextRowKey
), ViewState(11, QSet
<int>() << 11))
357 << qMakePair(KeyPress(nextItemKey
), ViewState(12, QSet
<int>() << 12))
358 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, QSet
<int>() << 12 << 13 << 14 << 15 << 16 << 17))
359 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, QSet
<int>() << 12))
360 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7 << 8 << 9 << 10 << 11 << 12))
361 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, QSet
<int>() << 12))
362 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, QSet
<int>() << 12))
363 << qMakePair(KeyPress(previousRowKey
), ViewState(14, QSet
<int>() << 14))
364 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
367 if (columnCount
== 3 && groupingEnabled
) {
368 // 3 columns, with grouping:
374 // d1 d2 d3 | 9 10 11
376 // e1 e2 e3 | 13 14 15
377 // e4 e5 e6 | 16 17 18
380 << qMakePair(KeyPress(nextItemKey
), ViewState(1, QSet
<int>() << 1))
381 << qMakePair(KeyPress(nextItemKey
), ViewState(2, QSet
<int>() << 2))
382 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 2 << 3))
383 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 2 << 3 << 4 << 5 << 6))
384 << qMakePair(KeyPress(nextRowKey
), ViewState(8, QSet
<int>() << 8))
385 << qMakePair(KeyPress(nextRowKey
), ViewState(11, QSet
<int>() << 11))
386 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, QSet
<int>() << 11))
387 << qMakePair(KeyPress(nextRowKey
), ViewState(13, QSet
<int>() << 13))
388 << qMakePair(KeyPress(nextRowKey
), ViewState(16, QSet
<int>() << 16))
389 << qMakePair(KeyPress(nextItemKey
), ViewState(17, QSet
<int>() << 17))
390 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
391 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
392 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
395 if (columnCount
== 5 && groupingEnabled
) {
396 // 5 columns, with grouping:
400 // c1 c2 c3 c4 c5 | 4 5 6 7 8
401 // d1 d2 d3 d4 | 9 10 11 12
402 // e1 e2 e3 e4 e5 | 13 14 15 16 17
405 << qMakePair(KeyPress(nextItemKey
), ViewState(1, QSet
<int>() << 1))
406 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 1 << 2 << 3))
407 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, QSet
<int>() << 1 << 2 << 3 << 4 << 5))
408 << qMakePair(KeyPress(nextItemKey
), ViewState(6, QSet
<int>() << 6))
409 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, QSet
<int>() << 6))
410 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, QSet
<int>() << 6))
411 << qMakePair(KeyPress(nextRowKey
), ViewState(12, QSet
<int>() << 12))
412 << qMakePair(KeyPress(nextRowKey
), ViewState(17, QSet
<int>() << 17))
413 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
414 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
415 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, QSet
<int>() << 17 << 18 << 19))
416 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, QSet
<int>() << 14 << 15 << 16 << 17))
417 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
420 const QString testName
=
421 layoutNames
[layout
] + ", " +
422 QString("%1 columns, ").arg(columnCount
) +
423 selectionBehaviorNames
[selectionBehavior
] + ", " +
424 groupingEnabledNames
[groupingEnabled
];
426 const QByteArray testNameAscii
= testName
.toAscii();
428 QTest::newRow(testNameAscii
.data())
442 * This function sets the view's properties according to the data provided by
443 * KItemListControllerTest::testKeyboardNavigation_data().
445 * The list \a testList contains pairs of key presses, which are sent to the
446 * container, and expected view states, which are verified then.
448 void KItemListControllerTest::testKeyboardNavigation()
450 QFETCH(KFileItemListView::ItemLayout
, layout
);
451 QFETCH(Qt::Orientation
, scrollOrientation
);
452 QFETCH(int, columnCount
);
453 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
454 QFETCH(bool, groupingEnabled
);
455 QFETCH(QList
<keyPressViewStatePair
>, testList
);
457 m_view
->setItemLayout(layout
);
458 QCOMPARE(m_view
->itemLayout(), layout
);
460 m_view
->setScrollOrientation(scrollOrientation
);
461 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
463 m_controller
->setSelectionBehavior(selectionBehavior
);
464 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
466 m_model
->setGroupedSorting(groupingEnabled
);
467 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
469 adjustGeometryForColumnCount(columnCount
);
470 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
472 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
473 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(QSet
<int>)));
475 while (!testList
.isEmpty()) {
476 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
477 const Qt::Key key
= test
.first
.m_key
;
478 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
479 const int current
= test
.second
.m_current
;
480 const QSet
<int> selection
= test
.second
.m_selection
;
481 const bool activated
= test
.second
.m_activated
;
483 QTest::keyClick(m_container
, key
, modifier
);
485 QCOMPARE(m_selectionManager
->currentItem(), current
);
486 switch (selectionBehavior
) {
487 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
488 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << current
); break;
489 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
493 switch (selectionBehavior
) {
494 case KItemListController::MultiSelection
:
495 if (!selection
.isEmpty()) {
496 // The selected items should be activated.
497 if (selection
.count() == 1) {
498 QVERIFY(!spySingleItemActivated
.isEmpty());
499 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.toList().at(0));
500 QVERIFY(spyMultipleItemsActivated
.isEmpty());
502 QVERIFY(spySingleItemActivated
.isEmpty());
503 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
504 QCOMPARE(qvariant_cast
<QSet
<int> >(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
508 // No items are selected. Therefore, the current item should be activated.
509 // This is handled by falling through to the NoSelection/SingleSelection case.
510 case KItemListController::NoSelection
:
511 case KItemListController::SingleSelection
:
512 // In NoSelection and SingleSelection mode, the current item should be activated.
513 QVERIFY(!spySingleItemActivated
.isEmpty());
514 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
515 QVERIFY(spyMultipleItemsActivated
.isEmpty());
522 void KItemListControllerTest::testMouseClickActivation()
524 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
526 // Make sure that we have a large window, such that
527 // the items are visible and clickable.
528 adjustGeometryForColumnCount(5);
530 // Make sure that the first item is visible in the view.
531 m_view
->setScrollOffset(0);
532 QCOMPARE(m_view
->firstVisibleIndex(), 0);
534 const QPointF pos
= m_view
->itemContextRect(0).center();
536 // Save the "single click" setting.
537 const bool restoreKGlobalSettingsSingleClick
= KGlobalSettings::singleClick();
539 KConfig
config("kcminputrc");
540 KConfigGroup group
= config
.group("KDE");
542 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
543 mousePressEvent
.setPos(pos
);
544 mousePressEvent
.setButton(Qt::LeftButton
);
545 mousePressEvent
.setButtons(Qt::LeftButton
);
547 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
548 mouseReleaseEvent
.setPos(pos
);
549 mouseReleaseEvent
.setButton(Qt::LeftButton
);
550 mouseReleaseEvent
.setButtons(Qt::NoButton
);
552 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
554 // Default setting: single click activation.
555 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
557 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
560 const int maxIterations
= 20;
561 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
566 if (!KGlobalSettings::singleClick()) {
567 // TODO: Try to find a way to make sure that changing the global setting works.
568 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
571 m_view
->event(&mousePressEvent
);
572 m_view
->event(&mouseReleaseEvent
);
573 QCOMPARE(spyItemActivated
.count(), 1);
574 spyItemActivated
.clear();
576 // Set the global setting to "double click activation".
577 group
.writeEntry("SingleClick", false, KConfig::Persistent
|KConfig::Global
);
579 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
582 while (KGlobalSettings::singleClick() && iterations
< maxIterations
) {
587 if (KGlobalSettings::singleClick()) {
588 // TODO: Try to find a way to make sure that changing the global setting works.
589 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
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 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
614 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
617 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
622 if (!KGlobalSettings::singleClick()) {
623 // TODO: Try to find a way to make sure that changing the global setting works.
624 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
627 m_view
->event(&mousePressEvent
);
628 m_view
->event(&mouseReleaseEvent
);
629 QCOMPARE(spyItemActivated
.count(), 1);
630 spyItemActivated
.clear();
632 // Enforce single click activation in the controller.
633 m_controller
->setSingleClickActivationEnforced(true);
634 m_view
->event(&mousePressEvent
);
635 m_view
->event(&mouseReleaseEvent
);
636 QCOMPARE(spyItemActivated
.count(), 1);
637 spyItemActivated
.clear();
639 // Restore previous settings.
640 m_controller
->setSingleClickActivationEnforced(true);
641 group
.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick
, KConfig::Persistent
|KConfig::Global
);
643 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
646 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
&& iterations
< maxIterations
) {
651 if (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
) {
652 // TODO: Try to find a way to make sure that changing the global setting works.
653 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
657 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
659 const QSize size
= m_view
->itemSize().toSize();
661 QRect rect
= m_container
->geometry();
662 rect
.setSize(size
* count
);
663 m_container
->setGeometry(rect
);
665 // Increase the size of the container until the correct column count is reached.
666 while (m_view
->m_layouter
->m_columnCount
< count
) {
667 rect
= m_container
->geometry();
668 rect
.setSize(rect
.size() + size
);
669 m_container
->setGeometry(rect
);
673 QTEST_KDEMAIN(KItemListControllerTest
, GUI
)
675 #include "kitemlistcontrollertest.moc"