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"
28 #include <KConfigGroup>
29 #include <KGlobalSettings>
32 #include <QGraphicsSceneMouseEvent>
35 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
);
36 Q_DECLARE_METATYPE(Qt::Orientation
);
37 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
);
38 Q_DECLARE_METATYPE(KItemSet
);
40 class KItemListControllerTest
: public QObject
46 void cleanupTestCase();
51 void testKeyboardNavigation_data();
52 void testKeyboardNavigation();
53 void testMouseClickActivation();
57 * Make sure that the number of columns in the view is equal to \a count
58 * by changing the geometry of the container.
60 void adjustGeometryForColumnCount(int count
);
63 KFileItemListView
* m_view
;
64 KItemListController
* m_controller
;
65 KItemListSelectionManager
* m_selectionManager
;
66 KFileItemModel
* m_model
;
68 KItemListContainer
* m_container
;
72 * This function initializes the member objects, creates the temporary files, and
73 * shows the view on the screen on startup. This could also be done before every
74 * single test, but this would make the time needed to run the test much larger.
76 void KItemListControllerTest::initTestCase()
78 qRegisterMetaType
<KItemSet
>("KItemSet");
80 m_testDir
= new TestDir();
81 m_model
= new KFileItemModel();
82 m_view
= new KFileItemListView();
83 m_controller
= new KItemListController(m_model
, m_view
, this);
84 m_container
= new KItemListContainer(m_controller
);
85 m_controller
= m_container
->controller();
86 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
87 m_selectionManager
= m_controller
->selectionManager();
91 << "a1" << "a2" << "a3"
93 << "c1" << "c2" << "c3" << "c4" << "c5"
94 << "d1" << "d2" << "d3" << "d4"
95 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
97 m_testDir
->createFiles(files
);
98 m_model
->loadDirectory(m_testDir
->url());
99 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, SIGNAL(directoryLoadingCompleted()));
100 QVERIFY(spyDirectoryLoadingCompleted
.wait());
103 QTest::qWaitForWindowShown(m_container
);
106 void KItemListControllerTest::cleanupTestCase()
115 /** Before each test, the current item, selection, and item size are reset to the defaults. */
116 void KItemListControllerTest::init()
118 m_selectionManager
->setCurrentItem(0);
119 QCOMPARE(m_selectionManager
->currentItem(), 0);
121 m_selectionManager
->clearSelection();
122 QVERIFY(!m_selectionManager
->hasSelection());
124 const QSizeF
itemSize(50, 50);
125 m_view
->setItemSize(itemSize
);
126 QCOMPARE(m_view
->itemSize(), itemSize
);
129 void KItemListControllerTest::cleanup()
134 * \class KeyPress is a small helper struct that represents a key press event,
135 * including the key and the keyboard modifiers.
139 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
145 Qt::KeyboardModifiers m_modifier
;
149 * \class ViewState is a small helper struct that represents a certain state
150 * of the view, including the current item, the selected items in MultiSelection
151 * mode (in the other modes, the selection is either empty or equal to the
152 * current item), and the information whether items were activated by the last
157 ViewState(int current
, const KItemSet selection
, bool activated
= false) :
159 m_selection(selection
),
160 m_activated(activated
)
164 KItemSet m_selection
;
168 // We have to define a typedef for the pair in order to make the test compile.
169 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
170 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>);
173 * This function provides the data for the actual test function
174 * KItemListControllerTest::testKeyboardNavigation().
175 * It tests all possible combinations of view layouts, selection behaviors,
176 * and enabled/disabled groupings for different column counts, and
177 * provides a list of key presses and the states that the view should be in
178 * after the key press event.
180 void KItemListControllerTest::testKeyboardNavigation_data()
182 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
183 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
184 QTest::addColumn
<int>("columnCount");
185 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
186 QTest::addColumn
<bool>("groupingEnabled");
187 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
189 QList
<KFileItemListView::ItemLayout
> layoutList
;
190 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
191 layoutList
.append(KFileItemListView::IconsLayout
);
192 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
193 layoutList
.append(KFileItemListView::CompactLayout
);
194 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
195 layoutList
.append(KFileItemListView::DetailsLayout
);
196 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
198 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
199 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
200 selectionBehaviorList
.append(KItemListController::NoSelection
);
201 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
202 selectionBehaviorList
.append(KItemListController::SingleSelection
);
203 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
204 selectionBehaviorList
.append(KItemListController::MultiSelection
);
205 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
207 QList
<bool> groupingEnabledList
;
208 QHash
<bool, QString
> groupingEnabledNames
;
209 groupingEnabledList
.append(false);
210 groupingEnabledNames
[false] = "ungrouped";
211 groupingEnabledList
.append(true);
212 groupingEnabledNames
[true] = "grouping enabled";
214 foreach (const KFileItemListView::ItemLayout
& layout
, layoutList
) {
215 // The following settings depend on the layout.
216 // Note that 'columns' are actually 'rows' in
218 Qt::Orientation scrollOrientation
;
219 QList
<int> columnCountList
;
221 Qt::Key previousItemKey
;
223 Qt::Key previousRowKey
;
226 case KFileItemListView::IconsLayout
:
227 scrollOrientation
= Qt::Vertical
;
228 columnCountList
<< 1 << 3 << 5;
229 nextItemKey
= Qt::Key_Right
;
230 previousItemKey
= Qt::Key_Left
;
231 nextRowKey
= Qt::Key_Down
;
232 previousRowKey
= Qt::Key_Up
;
234 case KFileItemListView::CompactLayout
:
235 scrollOrientation
= Qt::Horizontal
;
236 columnCountList
<< 1 << 3 << 5;
237 nextItemKey
= Qt::Key_Down
;
238 previousItemKey
= Qt::Key_Up
;
239 nextRowKey
= Qt::Key_Right
;
240 previousRowKey
= Qt::Key_Left
;
242 case KFileItemListView::DetailsLayout
:
243 scrollOrientation
= Qt::Vertical
;
244 columnCountList
<< 1;
245 nextItemKey
= Qt::Key_Down
;
246 previousItemKey
= Qt::Key_Up
;
247 nextRowKey
= Qt::Key_Down
;
248 previousRowKey
= Qt::Key_Up
;
252 foreach (int columnCount
, columnCountList
) {
253 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
254 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
255 QList
<QPair
<KeyPress
, ViewState
> > testList
;
257 // First, key presses which should have the same effect
258 // for any layout and any number of columns.
260 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
261 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
262 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
263 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
264 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
265 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
266 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
267 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
268 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
269 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
270 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
271 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
272 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
273 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
274 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
275 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
276 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
277 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
278 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
279 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
280 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
281 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
282 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
283 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
284 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
285 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
286 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
287 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
289 // Next, we test combinations of key presses which only work for a
290 // particular number of columns and either enabled or disabled grouping.
293 if (columnCount
== 1) {
295 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
296 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
297 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
298 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
299 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
300 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
303 // Multiple columns: we test both 3 and 5 columns with grouping
304 // enabled or disabled. For each case, the layout of the items
305 // in the view is shown (both using file names and indices) to
306 // make it easier to understand what the tests do.
308 if (columnCount
== 3 && !groupingEnabled
) {
309 // 3 columns, no grouping:
314 // d1 d2 d3 | 9 10 11
315 // d4 e1 e2 | 12 13 14
316 // e3 e4 e5 | 15 16 17
319 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
320 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
321 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
322 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
323 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
324 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
325 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
326 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
327 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
328 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
329 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
330 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
331 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
332 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
333 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
334 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
335 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
336 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
337 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
338 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
341 if (columnCount
== 5 && !groupingEnabled
) {
342 // 5 columns, no grouping:
344 // a1 a2 a3 b1 c1 | 0 1 2 3 4
345 // c2 c3 c4 c5 d1 | 5 6 7 8 9
346 // d2 d3 d4 e1 e2 | 10 11 12 13 14
347 // e3 e4 e5 e6 e7 | 15 16 17 18 19
349 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
350 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
351 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
352 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
353 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
354 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
355 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
356 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
357 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
358 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
359 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
362 if (columnCount
== 3 && groupingEnabled
) {
363 // 3 columns, with grouping:
369 // d1 d2 d3 | 9 10 11
371 // e1 e2 e3 | 13 14 15
372 // e4 e5 e6 | 16 17 18
375 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
376 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
377 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
378 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
379 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
380 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
381 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
382 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
383 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
384 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
385 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
386 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
387 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
390 if (columnCount
== 5 && groupingEnabled
) {
391 // 5 columns, with grouping:
395 // c1 c2 c3 c4 c5 | 4 5 6 7 8
396 // d1 d2 d3 d4 | 9 10 11 12
397 // e1 e2 e3 e4 e5 | 13 14 15 16 17
400 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
401 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
402 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
403 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
404 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
405 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
406 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
407 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
408 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
409 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
410 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
411 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
412 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
415 const QString testName
=
416 layoutNames
[layout
] + ", " +
417 QString("%1 columns, ").arg(columnCount
) +
418 selectionBehaviorNames
[selectionBehavior
] + ", " +
419 groupingEnabledNames
[groupingEnabled
];
421 const QByteArray testNameAscii
= testName
.toAscii();
423 QTest::newRow(testNameAscii
.data())
437 * This function sets the view's properties according to the data provided by
438 * KItemListControllerTest::testKeyboardNavigation_data().
440 * The list \a testList contains pairs of key presses, which are sent to the
441 * container, and expected view states, which are verified then.
443 void KItemListControllerTest::testKeyboardNavigation()
445 QFETCH(KFileItemListView::ItemLayout
, layout
);
446 QFETCH(Qt::Orientation
, scrollOrientation
);
447 QFETCH(int, columnCount
);
448 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
449 QFETCH(bool, groupingEnabled
);
450 QFETCH(QList
<keyPressViewStatePair
>, testList
);
452 m_view
->setItemLayout(layout
);
453 QCOMPARE(m_view
->itemLayout(), layout
);
455 m_view
->setScrollOrientation(scrollOrientation
);
456 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
458 m_controller
->setSelectionBehavior(selectionBehavior
);
459 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
461 m_model
->setGroupedSorting(groupingEnabled
);
462 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
464 adjustGeometryForColumnCount(columnCount
);
465 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
467 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
468 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(KItemSet
)));
470 while (!testList
.isEmpty()) {
471 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
472 const Qt::Key key
= test
.first
.m_key
;
473 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
474 const int current
= test
.second
.m_current
;
475 const KItemSet selection
= test
.second
.m_selection
;
476 const bool activated
= test
.second
.m_activated
;
478 QTest::keyClick(m_container
, key
, modifier
);
480 QCOMPARE(m_selectionManager
->currentItem(), current
);
481 switch (selectionBehavior
) {
482 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
483 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
484 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
488 switch (selectionBehavior
) {
489 case KItemListController::MultiSelection
:
490 if (!selection
.isEmpty()) {
491 // The selected items should be activated.
492 if (selection
.count() == 1) {
493 QVERIFY(!spySingleItemActivated
.isEmpty());
494 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
495 QVERIFY(spyMultipleItemsActivated
.isEmpty());
497 QVERIFY(spySingleItemActivated
.isEmpty());
498 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
499 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
503 // No items are selected. Therefore, the current item should be activated.
504 // This is handled by falling through to the NoSelection/SingleSelection case.
505 case KItemListController::NoSelection
:
506 case KItemListController::SingleSelection
:
507 // In NoSelection and SingleSelection mode, the current item should be activated.
508 QVERIFY(!spySingleItemActivated
.isEmpty());
509 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
510 QVERIFY(spyMultipleItemsActivated
.isEmpty());
517 void KItemListControllerTest::testMouseClickActivation()
519 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
521 // Make sure that we have a large window, such that
522 // the items are visible and clickable.
523 adjustGeometryForColumnCount(5);
525 // Make sure that the first item is visible in the view.
526 m_view
->setScrollOffset(0);
527 QCOMPARE(m_view
->firstVisibleIndex(), 0);
529 const QPointF pos
= m_view
->itemContextRect(0).center();
531 // Save the "single click" setting.
532 const bool restoreKGlobalSettingsSingleClick
= KGlobalSettings::singleClick();
534 KConfig
config("kcminputrc");
535 KConfigGroup group
= config
.group("KDE");
537 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
538 mousePressEvent
.setPos(pos
);
539 mousePressEvent
.setButton(Qt::LeftButton
);
540 mousePressEvent
.setButtons(Qt::LeftButton
);
542 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
543 mouseReleaseEvent
.setPos(pos
);
544 mouseReleaseEvent
.setButton(Qt::LeftButton
);
545 mouseReleaseEvent
.setButtons(Qt::NoButton
);
547 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
549 // Default setting: single click activation.
550 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
552 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
555 const int maxIterations
= 20;
556 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
561 if (!KGlobalSettings::singleClick()) {
562 // TODO: Try to find a way to make sure that changing the global setting works.
563 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
566 m_view
->event(&mousePressEvent
);
567 m_view
->event(&mouseReleaseEvent
);
568 QCOMPARE(spyItemActivated
.count(), 1);
569 spyItemActivated
.clear();
571 // Set the global setting to "double click activation".
572 group
.writeEntry("SingleClick", false, KConfig::Persistent
|KConfig::Global
);
574 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
577 while (KGlobalSettings::singleClick() && iterations
< maxIterations
) {
582 if (KGlobalSettings::singleClick()) {
583 // TODO: Try to find a way to make sure that changing the global setting works.
584 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
587 m_view
->event(&mousePressEvent
);
588 m_view
->event(&mouseReleaseEvent
);
589 QCOMPARE(spyItemActivated
.count(), 0);
590 spyItemActivated
.clear();
592 // Enforce single click activation in the controller.
593 m_controller
->setSingleClickActivationEnforced(true);
594 m_view
->event(&mousePressEvent
);
595 m_view
->event(&mouseReleaseEvent
);
596 QCOMPARE(spyItemActivated
.count(), 1);
597 spyItemActivated
.clear();
599 // Do not enforce single click activation in the controller.
600 m_controller
->setSingleClickActivationEnforced(false);
601 m_view
->event(&mousePressEvent
);
602 m_view
->event(&mouseReleaseEvent
);
603 QCOMPARE(spyItemActivated
.count(), 0);
604 spyItemActivated
.clear();
606 // Set the global setting back to "single click activation".
607 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
609 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
612 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
617 if (!KGlobalSettings::singleClick()) {
618 // TODO: Try to find a way to make sure that changing the global setting works.
619 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
622 m_view
->event(&mousePressEvent
);
623 m_view
->event(&mouseReleaseEvent
);
624 QCOMPARE(spyItemActivated
.count(), 1);
625 spyItemActivated
.clear();
627 // Enforce single click activation in the controller.
628 m_controller
->setSingleClickActivationEnforced(true);
629 m_view
->event(&mousePressEvent
);
630 m_view
->event(&mouseReleaseEvent
);
631 QCOMPARE(spyItemActivated
.count(), 1);
632 spyItemActivated
.clear();
634 // Restore previous settings.
635 m_controller
->setSingleClickActivationEnforced(true);
636 group
.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick
, KConfig::Persistent
|KConfig::Global
);
638 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
641 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
&& iterations
< maxIterations
) {
646 if (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
) {
647 // TODO: Try to find a way to make sure that changing the global setting works.
648 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!");
652 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
654 const QSize size
= m_view
->itemSize().toSize();
656 QRect rect
= m_container
->geometry();
657 rect
.setSize(size
* count
);
658 m_container
->setGeometry(rect
);
660 // Increase the size of the container until the correct column count is reached.
661 while (m_view
->m_layouter
->m_columnCount
< count
) {
662 rect
= m_container
->geometry();
663 rect
.setSize(rect
.size() + size
);
664 m_container
->setGeometry(rect
);
668 QTEST_MAIN(KItemListControllerTest
)
670 #include "kitemlistcontrollertest.moc"