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 << "e1" << "e2" << "e3" << "e4" << "e5" << "e6" << "e7";
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));
287 // Next, we test combinations of key presses which only work for a
288 // particular number of columns and either enabled or disabled grouping.
291 if (columnCount
== 1) {
293 << qMakePair(KeyPress(nextRowKey
), ViewState(1, QSet
<int>() << 1))
294 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, QSet
<int>() << 1 << 2))
295 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, QSet
<int>() << 1 << 2))
296 << qMakePair(KeyPress(previousRowKey
), ViewState(2, QSet
<int>() << 2))
297 << qMakePair(KeyPress(previousItemKey
), ViewState(1, QSet
<int>() << 1))
298 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
301 // Multiple columns: we test both 3 and 5 columns with grouping
302 // enabled or disabled. For each case, the layout of the items
303 // in the view is shown (both using file names and indices) to
304 // make it easier to understand what the tests do.
306 if (columnCount
== 3 && !groupingEnabled
) {
307 // 3 columns, no grouping:
312 // d1 d2 d3 | 9 10 11
313 // d4 e1 e2 | 12 13 14
314 // e3 e4 e5 | 15 16 17
317 << qMakePair(KeyPress(nextRowKey
), ViewState(3, QSet
<int>() << 3))
318 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, QSet
<int>() << 3))
319 << qMakePair(KeyPress(nextRowKey
), ViewState(7, QSet
<int>() << 7))
320 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, QSet
<int>() << 7 << 8))
321 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, QSet
<int>() << 7 << 8 << 9))
322 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, QSet
<int>() << 7 << 8))
323 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7))
324 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 6 << 7))
325 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, QSet
<int>() << 5 << 6 << 7))
326 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 6 << 7))
327 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7))
328 << qMakePair(KeyPress(nextRowKey
), ViewState(10, QSet
<int>() << 10))
329 << qMakePair(KeyPress(nextItemKey
), ViewState(11, QSet
<int>() << 11))
330 << qMakePair(KeyPress(nextRowKey
), ViewState(14, QSet
<int>() << 14))
331 << qMakePair(KeyPress(nextRowKey
), ViewState(17, QSet
<int>() << 17))
332 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
333 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
334 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, QSet
<int>() << 19))
335 << qMakePair(KeyPress(previousRowKey
), ViewState(16, QSet
<int>() << 16))
336 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
339 if (columnCount
== 5 && !groupingEnabled
) {
340 // 5 columns, no grouping:
342 // a1 a2 a3 b1 c1 | 0 1 2 3 4
343 // c2 c3 c4 c5 d1 | 5 6 7 8 9
344 // d2 d3 d4 e1 e2 | 10 11 12 13 14
345 // e3 e4 e5 e6 e7 | 15 16 17 18 19
347 << qMakePair(KeyPress(nextRowKey
), ViewState(5, QSet
<int>() << 5))
348 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, QSet
<int>() << 5))
349 << qMakePair(KeyPress(nextRowKey
), ViewState(11, QSet
<int>() << 11))
350 << qMakePair(KeyPress(nextItemKey
), ViewState(12, QSet
<int>() << 12))
351 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, QSet
<int>() << 12 << 13 << 14 << 15 << 16 << 17))
352 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, QSet
<int>() << 12))
353 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, QSet
<int>() << 7 << 8 << 9 << 10 << 11 << 12))
354 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, QSet
<int>() << 12))
355 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, QSet
<int>() << 12))
356 << qMakePair(KeyPress(previousRowKey
), ViewState(14, QSet
<int>() << 14))
357 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
360 if (columnCount
== 3 && groupingEnabled
) {
361 // 3 columns, with grouping:
367 // d1 d2 d3 | 9 10 11
369 // e1 e2 e3 | 13 14 15
370 // e4 e5 e6 | 16 17 18
373 << qMakePair(KeyPress(nextItemKey
), ViewState(1, QSet
<int>() << 1))
374 << qMakePair(KeyPress(nextItemKey
), ViewState(2, QSet
<int>() << 2))
375 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 2 << 3))
376 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, QSet
<int>() << 2 << 3 << 4 << 5 << 6))
377 << qMakePair(KeyPress(nextRowKey
), ViewState(8, QSet
<int>() << 8))
378 << qMakePair(KeyPress(nextRowKey
), ViewState(11, QSet
<int>() << 11))
379 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, QSet
<int>() << 11))
380 << qMakePair(KeyPress(nextRowKey
), ViewState(13, QSet
<int>() << 13))
381 << qMakePair(KeyPress(nextRowKey
), ViewState(16, QSet
<int>() << 16))
382 << qMakePair(KeyPress(nextItemKey
), ViewState(17, QSet
<int>() << 17))
383 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
384 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
385 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
388 if (columnCount
== 5 && groupingEnabled
) {
389 // 5 columns, with grouping:
393 // c1 c2 c3 c4 c5 | 4 5 6 7 8
394 // d1 d2 d3 d4 | 9 10 11 12
395 // e1 e2 e3 e4 e5 | 13 14 15 16 17
398 << qMakePair(KeyPress(nextItemKey
), ViewState(1, QSet
<int>() << 1))
399 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, QSet
<int>() << 1 << 2 << 3))
400 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, QSet
<int>() << 1 << 2 << 3 << 4 << 5))
401 << qMakePair(KeyPress(nextItemKey
), ViewState(6, QSet
<int>() << 6))
402 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, QSet
<int>() << 6))
403 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, QSet
<int>() << 6))
404 << qMakePair(KeyPress(nextRowKey
), ViewState(12, QSet
<int>() << 12))
405 << qMakePair(KeyPress(nextRowKey
), ViewState(17, QSet
<int>() << 17))
406 << qMakePair(KeyPress(nextRowKey
), ViewState(19, QSet
<int>() << 19))
407 << qMakePair(KeyPress(previousRowKey
), ViewState(17, QSet
<int>() << 17))
408 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, QSet
<int>() << 17 << 18 << 19))
409 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, QSet
<int>() << 14 << 15 << 16 << 17))
410 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, QSet
<int>() << 0));
413 const QString testName
=
414 layoutNames
[layout
] + ", " +
415 QString("%1 columns, ").arg(columnCount
) +
416 selectionBehaviorNames
[selectionBehavior
] + ", " +
417 groupingEnabledNames
[groupingEnabled
];
419 const QByteArray testNameAscii
= testName
.toAscii();
421 QTest::newRow(testNameAscii
.data())
435 * This function sets the view's properties according to the data provided by
436 * KItemListControllerTest::testKeyboardNavigation_data().
438 * The list \a testList contains pairs of key presses, which are sent to the
439 * container, and expected view states, which are verified then.
441 void KItemListControllerTest::testKeyboardNavigation()
443 QFETCH(KFileItemListView::ItemLayout
, layout
);
444 QFETCH(Qt::Orientation
, scrollOrientation
);
445 QFETCH(int, columnCount
);
446 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
447 QFETCH(bool, groupingEnabled
);
448 QFETCH(QList
<keyPressViewStatePair
>, testList
);
450 m_view
->setItemLayout(layout
);
451 QCOMPARE(m_view
->itemLayout(), layout
);
453 m_view
->setScrollOrientation(scrollOrientation
);
454 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
456 m_controller
->setSelectionBehavior(selectionBehavior
);
457 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
459 m_model
->setGroupedSorting(groupingEnabled
);
460 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
462 adjustGeometryForColumnCount(columnCount
);
463 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
465 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
466 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(QSet
<int>)));
468 while (!testList
.isEmpty()) {
469 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
470 const Qt::Key key
= test
.first
.m_key
;
471 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
472 const int current
= test
.second
.m_current
;
473 const QSet
<int> selection
= test
.second
.m_selection
;
474 const bool activated
= test
.second
.m_activated
;
476 QTest::keyClick(m_container
, key
, modifier
);
478 QCOMPARE(m_selectionManager
->currentItem(), current
);
479 switch (selectionBehavior
) {
480 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
481 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), QSet
<int>() << current
); break;
482 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
486 switch (selectionBehavior
) {
487 case KItemListController::MultiSelection
:
488 if (!selection
.isEmpty()) {
489 // The selected items should be activated.
490 if (selection
.count() == 1) {
491 QVERIFY(!spySingleItemActivated
.isEmpty());
492 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.toList().at(0));
493 QVERIFY(spyMultipleItemsActivated
.isEmpty());
495 QVERIFY(spySingleItemActivated
.isEmpty());
496 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
497 QCOMPARE(qvariant_cast
<QSet
<int> >(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
501 // No items are selected. Therefore, the current item should be activated.
502 // This is handled by falling through to the NoSelection/SingleSelection case.
503 case KItemListController::NoSelection
:
504 case KItemListController::SingleSelection
:
505 // In NoSelection and SingleSelection mode, the current item should be activated.
506 QVERIFY(!spySingleItemActivated
.isEmpty());
507 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
508 QVERIFY(spyMultipleItemsActivated
.isEmpty());
515 void KItemListControllerTest::testMouseClickActivation()
517 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
519 // Make sure that we have a large window, such that
520 // the items are visible and clickable.
521 adjustGeometryForColumnCount(5);
523 // Make sure that the first item is visible in the view.
524 QTest::keyClick(m_container
, Qt::Key_End
, Qt::NoModifier
);
525 QTest::keyClick(m_container
, Qt::Key_Home
, Qt::NoModifier
);
526 while (m_view
->firstVisibleIndex() > 0) {
530 const QPointF pos
= m_view
->itemContextRect(0).center();
532 // Save the "single click" setting.
533 const bool restoreKGlobalSettingsSingleClick
= KGlobalSettings::singleClick();
535 KConfig
config("kcminputrc");
536 KConfigGroup group
= config
.group("KDE");
538 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
539 mousePressEvent
.setPos(pos
);
540 mousePressEvent
.setButton(Qt::LeftButton
);
541 mousePressEvent
.setButtons(Qt::LeftButton
);
543 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
544 mouseReleaseEvent
.setPos(pos
);
545 mouseReleaseEvent
.setButton(Qt::LeftButton
);
546 mouseReleaseEvent
.setButtons(Qt::NoButton
);
548 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
550 // Default setting: single click activation.
551 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
553 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
554 while (!KGlobalSettings::singleClick()) {
557 m_view
->event(&mousePressEvent
);
558 m_view
->event(&mouseReleaseEvent
);
559 QCOMPARE(spyItemActivated
.count(), 1);
560 spyItemActivated
.clear();
562 // Set the global setting to "double click activation".
563 group
.writeEntry("SingleClick", false, KConfig::Persistent
|KConfig::Global
);
565 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
566 while (KGlobalSettings::singleClick()) {
569 m_view
->event(&mousePressEvent
);
570 m_view
->event(&mouseReleaseEvent
);
571 QCOMPARE(spyItemActivated
.count(), 0);
572 spyItemActivated
.clear();
574 // Enforce single click activation in the controller.
575 m_controller
->setSingleClickActivationEnforced(true);
576 m_view
->event(&mousePressEvent
);
577 m_view
->event(&mouseReleaseEvent
);
578 QCOMPARE(spyItemActivated
.count(), 1);
579 spyItemActivated
.clear();
581 // Do not enforce single click activation in the controller.
582 m_controller
->setSingleClickActivationEnforced(false);
583 m_view
->event(&mousePressEvent
);
584 m_view
->event(&mouseReleaseEvent
);
585 QCOMPARE(spyItemActivated
.count(), 0);
586 spyItemActivated
.clear();
588 // Set the global setting back to "single click activation".
589 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
591 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
592 while (!KGlobalSettings::singleClick()) {
595 m_view
->event(&mousePressEvent
);
596 m_view
->event(&mouseReleaseEvent
);
597 QCOMPARE(spyItemActivated
.count(), 1);
598 spyItemActivated
.clear();
600 // Enforce single click activation in the controller.
601 m_controller
->setSingleClickActivationEnforced(true);
602 m_view
->event(&mousePressEvent
);
603 m_view
->event(&mouseReleaseEvent
);
604 QCOMPARE(spyItemActivated
.count(), 1);
605 spyItemActivated
.clear();
607 // Restore previous settings.
608 m_controller
->setSingleClickActivationEnforced(true);
609 group
.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick
, KConfig::Persistent
|KConfig::Global
);
611 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
612 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
) {
617 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
619 const QSize size
= m_view
->itemSize().toSize();
621 QRect rect
= m_container
->geometry();
622 rect
.setSize(size
* count
);
623 m_container
->setGeometry(rect
);
625 // Increase the size of the container until the correct column count is reached.
626 while (m_view
->m_layouter
->m_columnCount
< count
) {
627 rect
= m_container
->geometry();
628 rect
.setSize(rect
.size() + size
);
629 m_container
->setGeometry(rect
);
633 QTEST_KDEMAIN(KItemListControllerTest
, GUI
)
635 #include "kitemlistcontrollertest.moc"