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 ***************************************************************************/
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>
39 const int DefaultTimeout
= 2000;
42 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout
);
43 Q_DECLARE_METATYPE(Qt::Orientation
);
44 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior
);
45 Q_DECLARE_METATYPE(KItemSet
);
47 class KItemListControllerTest
: public QObject
53 void cleanupTestCase();
58 void testKeyboardNavigation_data();
59 void testKeyboardNavigation();
60 void testMouseClickActivation();
64 * Make sure that the number of columns in the view is equal to \a count
65 * by changing the geometry of the container.
67 void adjustGeometryForColumnCount(int count
);
70 KFileItemListView
* m_view
;
71 KItemListController
* m_controller
;
72 KItemListSelectionManager
* m_selectionManager
;
73 KFileItemModel
* m_model
;
75 KItemListContainer
* m_container
;
79 * This function initializes the member objects, creates the temporary files, and
80 * shows the view on the screen on startup. This could also be done before every
81 * single test, but this would make the time needed to run the test much larger.
83 void KItemListControllerTest::initTestCase()
85 qRegisterMetaType
<KItemSet
>("KItemSet");
87 m_testDir
= new TestDir();
88 m_model
= new KFileItemModel();
89 m_view
= new KFileItemListView();
90 m_controller
= new KItemListController(m_model
, m_view
, this);
91 m_container
= new KItemListContainer(m_controller
);
92 m_controller
= m_container
->controller();
93 m_controller
->setSelectionBehavior(KItemListController::MultiSelection
);
94 m_selectionManager
= m_controller
->selectionManager();
98 << "a1" << "a2" << "a3"
100 << "c1" << "c2" << "c3" << "c4" << "c5"
101 << "d1" << "d2" << "d3" << "d4"
102 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
104 m_testDir
->createFiles(files
);
105 m_model
->loadDirectory(m_testDir
->url());
106 QSignalSpy
spyDirectoryLoadingCompleted(m_model
, SIGNAL(directoryLoadingCompleted()));
107 QVERIFY(spyDirectoryLoadingCompleted
.wait(DefaultTimeout
));
110 QTest::qWaitForWindowShown(m_container
);
113 void KItemListControllerTest::cleanupTestCase()
122 /** Before each test, the current item, selection, and item size are reset to the defaults. */
123 void KItemListControllerTest::init()
125 m_selectionManager
->setCurrentItem(0);
126 QCOMPARE(m_selectionManager
->currentItem(), 0);
128 m_selectionManager
->clearSelection();
129 QVERIFY(!m_selectionManager
->hasSelection());
131 const QSizeF
itemSize(50, 50);
132 m_view
->setItemSize(itemSize
);
133 QCOMPARE(m_view
->itemSize(), itemSize
);
136 void KItemListControllerTest::cleanup()
141 * \class KeyPress is a small helper struct that represents a key press event,
142 * including the key and the keyboard modifiers.
146 KeyPress(Qt::Key key
, Qt::KeyboardModifiers modifier
= Qt::NoModifier
) :
152 Qt::KeyboardModifiers m_modifier
;
156 * \class ViewState is a small helper struct that represents a certain state
157 * of the view, including the current item, the selected items in MultiSelection
158 * mode (in the other modes, the selection is either empty or equal to the
159 * current item), and the information whether items were activated by the last
164 ViewState(int current
, const KItemSet selection
, bool activated
= false) :
166 m_selection(selection
),
167 m_activated(activated
)
171 KItemSet m_selection
;
175 // We have to define a typedef for the pair in order to make the test compile.
176 typedef QPair
<KeyPress
, ViewState
> keyPressViewStatePair
;
177 Q_DECLARE_METATYPE(QList
<keyPressViewStatePair
>);
180 * This function provides the data for the actual test function
181 * KItemListControllerTest::testKeyboardNavigation().
182 * It tests all possible combinations of view layouts, selection behaviors,
183 * and enabled/disabled groupings for different column counts, and
184 * provides a list of key presses and the states that the view should be in
185 * after the key press event.
187 void KItemListControllerTest::testKeyboardNavigation_data()
189 QTest::addColumn
<KFileItemListView::ItemLayout
>("layout");
190 QTest::addColumn
<Qt::Orientation
>("scrollOrientation");
191 QTest::addColumn
<int>("columnCount");
192 QTest::addColumn
<KItemListController::SelectionBehavior
>("selectionBehavior");
193 QTest::addColumn
<bool>("groupingEnabled");
194 QTest::addColumn
<QList
<QPair
<KeyPress
, ViewState
> > >("testList");
196 QList
<KFileItemListView::ItemLayout
> layoutList
;
197 QHash
<KFileItemListView::ItemLayout
, QString
> layoutNames
;
198 layoutList
.append(KFileItemListView::IconsLayout
);
199 layoutNames
[KFileItemListView::IconsLayout
] = "Icons";
200 layoutList
.append(KFileItemListView::CompactLayout
);
201 layoutNames
[KFileItemListView::CompactLayout
] = "Compact";
202 layoutList
.append(KFileItemListView::DetailsLayout
);
203 layoutNames
[KFileItemListView::DetailsLayout
] = "Details";
205 QList
<KItemListController::SelectionBehavior
> selectionBehaviorList
;
206 QHash
<KItemListController::SelectionBehavior
, QString
> selectionBehaviorNames
;
207 selectionBehaviorList
.append(KItemListController::NoSelection
);
208 selectionBehaviorNames
[KItemListController::NoSelection
] = "NoSelection";
209 selectionBehaviorList
.append(KItemListController::SingleSelection
);
210 selectionBehaviorNames
[KItemListController::SingleSelection
] = "SingleSelection";
211 selectionBehaviorList
.append(KItemListController::MultiSelection
);
212 selectionBehaviorNames
[KItemListController::MultiSelection
] = "MultiSelection";
214 QList
<bool> groupingEnabledList
;
215 QHash
<bool, QString
> groupingEnabledNames
;
216 groupingEnabledList
.append(false);
217 groupingEnabledNames
[false] = "ungrouped";
218 groupingEnabledList
.append(true);
219 groupingEnabledNames
[true] = "grouping enabled";
221 foreach (const KFileItemListView::ItemLayout
& layout
, layoutList
) {
222 // The following settings depend on the layout.
223 // Note that 'columns' are actually 'rows' in
225 Qt::Orientation scrollOrientation
;
226 QList
<int> columnCountList
;
228 Qt::Key previousItemKey
;
230 Qt::Key previousRowKey
;
233 case KFileItemListView::IconsLayout
:
234 scrollOrientation
= Qt::Vertical
;
235 columnCountList
<< 1 << 3 << 5;
236 nextItemKey
= Qt::Key_Right
;
237 previousItemKey
= Qt::Key_Left
;
238 nextRowKey
= Qt::Key_Down
;
239 previousRowKey
= Qt::Key_Up
;
241 case KFileItemListView::CompactLayout
:
242 scrollOrientation
= Qt::Horizontal
;
243 columnCountList
<< 1 << 3 << 5;
244 nextItemKey
= Qt::Key_Down
;
245 previousItemKey
= Qt::Key_Up
;
246 nextRowKey
= Qt::Key_Right
;
247 previousRowKey
= Qt::Key_Left
;
249 case KFileItemListView::DetailsLayout
:
250 scrollOrientation
= Qt::Vertical
;
251 columnCountList
<< 1;
252 nextItemKey
= Qt::Key_Down
;
253 previousItemKey
= Qt::Key_Up
;
254 nextRowKey
= Qt::Key_Down
;
255 previousRowKey
= Qt::Key_Up
;
259 foreach (int columnCount
, columnCountList
) {
260 foreach (const KItemListController::SelectionBehavior
& selectionBehavior
, selectionBehaviorList
) {
261 foreach (bool groupingEnabled
, groupingEnabledList
) { // krazy:exclude=foreach
262 QList
<QPair
<KeyPress
, ViewState
> > testList
;
264 // First, key presses which should have the same effect
265 // for any layout and any number of columns.
267 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
268 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(1, KItemSet() << 1, true))
269 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(1, KItemSet() << 1, true))
270 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
271 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
272 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(3, KItemSet() << 2 << 3, true))
273 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 2))
274 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
275 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 2 << 3))
276 << qMakePair(KeyPress(Qt::Key_Return
), ViewState(4, KItemSet() << 2 << 3, true))
277 << qMakePair(KeyPress(previousItemKey
), ViewState(3, KItemSet() << 3))
278 << qMakePair(KeyPress(Qt::Key_Home
, Qt::ShiftModifier
), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
279 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
280 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 2 << 3))
281 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
282 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
283 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(18, KItemSet() << 18 << 19))
284 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
285 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
286 << qMakePair(KeyPress(Qt::Key_Enter
), ViewState(0, KItemSet(), true))
287 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet() << 0))
288 << qMakePair(KeyPress(Qt::Key_Space
, Qt::ControlModifier
), ViewState(0, KItemSet()))
289 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(0, KItemSet() << 0))
290 << qMakePair(KeyPress(Qt::Key_E
), ViewState(13, KItemSet() << 13))
291 << qMakePair(KeyPress(Qt::Key_Space
), ViewState(14, KItemSet() << 14))
292 << qMakePair(KeyPress(Qt::Key_3
), ViewState(15, KItemSet() << 15))
293 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0))
294 << qMakePair(KeyPress(Qt::Key_Escape
), ViewState(0, KItemSet()));
296 // Next, we test combinations of key presses which only work for a
297 // particular number of columns and either enabled or disabled grouping.
300 if (columnCount
== 1) {
302 << qMakePair(KeyPress(nextRowKey
), ViewState(1, KItemSet() << 1))
303 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(2, KItemSet() << 1 << 2))
304 << qMakePair(KeyPress(nextRowKey
, Qt::ControlModifier
), ViewState(3, KItemSet() << 1 << 2))
305 << qMakePair(KeyPress(previousRowKey
), ViewState(2, KItemSet() << 2))
306 << qMakePair(KeyPress(previousItemKey
), ViewState(1, KItemSet() << 1))
307 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
310 // Multiple columns: we test both 3 and 5 columns with grouping
311 // enabled or disabled. For each case, the layout of the items
312 // in the view is shown (both using file names and indices) to
313 // make it easier to understand what the tests do.
315 if (columnCount
== 3 && !groupingEnabled
) {
316 // 3 columns, no grouping:
321 // d1 d2 d3 | 9 10 11
322 // d4 e1 e2 | 12 13 14
323 // e3 e4 e5 | 15 16 17
326 << qMakePair(KeyPress(nextRowKey
), ViewState(3, KItemSet() << 3))
327 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(4, KItemSet() << 3))
328 << qMakePair(KeyPress(nextRowKey
), ViewState(7, KItemSet() << 7))
329 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
330 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(9, KItemSet() << 7 << 8 << 9))
331 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(8, KItemSet() << 7 << 8))
332 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
333 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
334 << qMakePair(KeyPress(previousItemKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 5 << 6 << 7))
335 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 6 << 7))
336 << qMakePair(KeyPress(nextItemKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7))
337 << qMakePair(KeyPress(nextRowKey
), ViewState(10, KItemSet() << 10))
338 << qMakePair(KeyPress(nextItemKey
), ViewState(11, KItemSet() << 11))
339 << qMakePair(KeyPress(nextRowKey
), ViewState(14, KItemSet() << 14))
340 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
341 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
342 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
343 << qMakePair(KeyPress(Qt::Key_End
), ViewState(19, KItemSet() << 19))
344 << qMakePair(KeyPress(previousRowKey
), ViewState(16, KItemSet() << 16))
345 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
348 if (columnCount
== 5 && !groupingEnabled
) {
349 // 5 columns, no grouping:
351 // a1 a2 a3 b1 c1 | 0 1 2 3 4
352 // c2 c3 c4 c5 d1 | 5 6 7 8 9
353 // d2 d3 d4 e1 e2 | 10 11 12 13 14
354 // e3 e4 e5 e6 e7 | 15 16 17 18 19
356 << qMakePair(KeyPress(nextRowKey
), ViewState(5, KItemSet() << 5))
357 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(6, KItemSet() << 5))
358 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
359 << qMakePair(KeyPress(nextItemKey
), ViewState(12, KItemSet() << 12))
360 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
361 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
362 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
363 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(12, KItemSet() << 12))
364 << qMakePair(KeyPress(Qt::Key_End
, Qt::ControlModifier
), ViewState(19, KItemSet() << 12))
365 << qMakePair(KeyPress(previousRowKey
), ViewState(14, KItemSet() << 14))
366 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
369 if (columnCount
== 3 && groupingEnabled
) {
370 // 3 columns, with grouping:
376 // d1 d2 d3 | 9 10 11
378 // e1 e2 e3 | 13 14 15
379 // e4 e5 e6 | 16 17 18
382 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
383 << qMakePair(KeyPress(nextItemKey
), ViewState(2, KItemSet() << 2))
384 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 2 << 3))
385 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
386 << qMakePair(KeyPress(nextRowKey
), ViewState(8, KItemSet() << 8))
387 << qMakePair(KeyPress(nextRowKey
), ViewState(11, KItemSet() << 11))
388 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(12, KItemSet() << 11))
389 << qMakePair(KeyPress(nextRowKey
), ViewState(13, KItemSet() << 13))
390 << qMakePair(KeyPress(nextRowKey
), ViewState(16, KItemSet() << 16))
391 << qMakePair(KeyPress(nextItemKey
), ViewState(17, KItemSet() << 17))
392 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
393 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
394 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
397 if (columnCount
== 5 && groupingEnabled
) {
398 // 5 columns, with grouping:
402 // c1 c2 c3 c4 c5 | 4 5 6 7 8
403 // d1 d2 d3 d4 | 9 10 11 12
404 // e1 e2 e3 e4 e5 | 13 14 15 16 17
407 << qMakePair(KeyPress(nextItemKey
), ViewState(1, KItemSet() << 1))
408 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(3, KItemSet() << 1 << 2 << 3))
409 << qMakePair(KeyPress(nextRowKey
, Qt::ShiftModifier
), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
410 << qMakePair(KeyPress(nextItemKey
), ViewState(6, KItemSet() << 6))
411 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(7, KItemSet() << 6))
412 << qMakePair(KeyPress(nextItemKey
, Qt::ControlModifier
), ViewState(8, KItemSet() << 6))
413 << qMakePair(KeyPress(nextRowKey
), ViewState(12, KItemSet() << 12))
414 << qMakePair(KeyPress(nextRowKey
), ViewState(17, KItemSet() << 17))
415 << qMakePair(KeyPress(nextRowKey
), ViewState(19, KItemSet() << 19))
416 << qMakePair(KeyPress(previousRowKey
), ViewState(17, KItemSet() << 17))
417 << qMakePair(KeyPress(Qt::Key_End
, Qt::ShiftModifier
), ViewState(19, KItemSet() << 17 << 18 << 19))
418 << qMakePair(KeyPress(previousRowKey
, Qt::ShiftModifier
), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
419 << qMakePair(KeyPress(Qt::Key_Home
), ViewState(0, KItemSet() << 0));
422 const QString testName
=
423 layoutNames
[layout
] + ", " +
424 QString("%1 columns, ").arg(columnCount
) +
425 selectionBehaviorNames
[selectionBehavior
] + ", " +
426 groupingEnabledNames
[groupingEnabled
];
428 const QByteArray testNameAscii
= testName
.toAscii();
430 QTest::newRow(testNameAscii
.data())
444 * This function sets the view's properties according to the data provided by
445 * KItemListControllerTest::testKeyboardNavigation_data().
447 * The list \a testList contains pairs of key presses, which are sent to the
448 * container, and expected view states, which are verified then.
450 void KItemListControllerTest::testKeyboardNavigation()
452 QFETCH(KFileItemListView::ItemLayout
, layout
);
453 QFETCH(Qt::Orientation
, scrollOrientation
);
454 QFETCH(int, columnCount
);
455 QFETCH(KItemListController::SelectionBehavior
, selectionBehavior
);
456 QFETCH(bool, groupingEnabled
);
457 QFETCH(QList
<keyPressViewStatePair
>, testList
);
459 m_view
->setItemLayout(layout
);
460 QCOMPARE(m_view
->itemLayout(), layout
);
462 m_view
->setScrollOrientation(scrollOrientation
);
463 QCOMPARE(m_view
->scrollOrientation(), scrollOrientation
);
465 m_controller
->setSelectionBehavior(selectionBehavior
);
466 QCOMPARE(m_controller
->selectionBehavior(), selectionBehavior
);
468 m_model
->setGroupedSorting(groupingEnabled
);
469 QCOMPARE(m_model
->groupedSorting(), groupingEnabled
);
471 adjustGeometryForColumnCount(columnCount
);
472 QCOMPARE(m_view
->m_layouter
->m_columnCount
, columnCount
);
474 QSignalSpy
spySingleItemActivated(m_controller
, SIGNAL(itemActivated(int)));
475 QSignalSpy
spyMultipleItemsActivated(m_controller
, SIGNAL(itemsActivated(KItemSet
)));
477 while (!testList
.isEmpty()) {
478 const QPair
<KeyPress
, ViewState
> test
= testList
.takeFirst();
479 const Qt::Key key
= test
.first
.m_key
;
480 const Qt::KeyboardModifiers modifier
= test
.first
.m_modifier
;
481 const int current
= test
.second
.m_current
;
482 const KItemSet selection
= test
.second
.m_selection
;
483 const bool activated
= test
.second
.m_activated
;
485 QTest::keyClick(m_container
, key
, modifier
);
487 QCOMPARE(m_selectionManager
->currentItem(), current
);
488 switch (selectionBehavior
) {
489 case KItemListController::NoSelection
: QVERIFY(m_selectionManager
->selectedItems().isEmpty()); break;
490 case KItemListController::SingleSelection
: QCOMPARE(m_selectionManager
->selectedItems(), KItemSet() << current
); break;
491 case KItemListController::MultiSelection
: QCOMPARE(m_selectionManager
->selectedItems(), selection
); break;
495 switch (selectionBehavior
) {
496 case KItemListController::MultiSelection
:
497 if (!selection
.isEmpty()) {
498 // The selected items should be activated.
499 if (selection
.count() == 1) {
500 QVERIFY(!spySingleItemActivated
.isEmpty());
501 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), selection
.first());
502 QVERIFY(spyMultipleItemsActivated
.isEmpty());
504 QVERIFY(spySingleItemActivated
.isEmpty());
505 QVERIFY(!spyMultipleItemsActivated
.isEmpty());
506 QCOMPARE(qvariant_cast
<KItemSet
>(spyMultipleItemsActivated
.takeFirst().at(0)), selection
);
510 // No items are selected. Therefore, the current item should be activated.
511 // This is handled by falling through to the NoSelection/SingleSelection case.
512 case KItemListController::NoSelection
:
513 case KItemListController::SingleSelection
:
514 // In NoSelection and SingleSelection mode, the current item should be activated.
515 QVERIFY(!spySingleItemActivated
.isEmpty());
516 QCOMPARE(qvariant_cast
<int>(spySingleItemActivated
.takeFirst().at(0)), current
);
517 QVERIFY(spyMultipleItemsActivated
.isEmpty());
524 void KItemListControllerTest::testMouseClickActivation()
526 m_view
->setItemLayout(KFileItemListView::IconsLayout
);
528 // Make sure that we have a large window, such that
529 // the items are visible and clickable.
530 adjustGeometryForColumnCount(5);
532 // Make sure that the first item is visible in the view.
533 m_view
->setScrollOffset(0);
534 QCOMPARE(m_view
->firstVisibleIndex(), 0);
536 const QPointF pos
= m_view
->itemContextRect(0).center();
538 // Save the "single click" setting.
539 const bool restoreKGlobalSettingsSingleClick
= KGlobalSettings::singleClick();
541 KConfig
config("kcminputrc");
542 KConfigGroup group
= config
.group("KDE");
544 QGraphicsSceneMouseEvent
mousePressEvent(QEvent::GraphicsSceneMousePress
);
545 mousePressEvent
.setPos(pos
);
546 mousePressEvent
.setButton(Qt::LeftButton
);
547 mousePressEvent
.setButtons(Qt::LeftButton
);
549 QGraphicsSceneMouseEvent
mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease
);
550 mouseReleaseEvent
.setPos(pos
);
551 mouseReleaseEvent
.setButton(Qt::LeftButton
);
552 mouseReleaseEvent
.setButtons(Qt::NoButton
);
554 QSignalSpy
spyItemActivated(m_controller
, SIGNAL(itemActivated(int)));
556 // Default setting: single click activation.
557 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
559 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
562 const int maxIterations
= 20;
563 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
568 if (!KGlobalSettings::singleClick()) {
569 // TODO: Try to find a way to make sure that changing the global setting works.
570 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
573 m_view
->event(&mousePressEvent
);
574 m_view
->event(&mouseReleaseEvent
);
575 QCOMPARE(spyItemActivated
.count(), 1);
576 spyItemActivated
.clear();
578 // Set the global setting to "double click activation".
579 group
.writeEntry("SingleClick", false, KConfig::Persistent
|KConfig::Global
);
581 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
584 while (KGlobalSettings::singleClick() && iterations
< maxIterations
) {
589 if (KGlobalSettings::singleClick()) {
590 // TODO: Try to find a way to make sure that changing the global setting works.
591 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
594 m_view
->event(&mousePressEvent
);
595 m_view
->event(&mouseReleaseEvent
);
596 QCOMPARE(spyItemActivated
.count(), 0);
597 spyItemActivated
.clear();
599 // Enforce single click activation in the controller.
600 m_controller
->setSingleClickActivationEnforced(true);
601 m_view
->event(&mousePressEvent
);
602 m_view
->event(&mouseReleaseEvent
);
603 QCOMPARE(spyItemActivated
.count(), 1);
604 spyItemActivated
.clear();
606 // Do not enforce single click activation in the controller.
607 m_controller
->setSingleClickActivationEnforced(false);
608 m_view
->event(&mousePressEvent
);
609 m_view
->event(&mouseReleaseEvent
);
610 QCOMPARE(spyItemActivated
.count(), 0);
611 spyItemActivated
.clear();
613 // Set the global setting back to "single click activation".
614 group
.writeEntry("SingleClick", true, KConfig::Persistent
|KConfig::Global
);
616 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
619 while (!KGlobalSettings::singleClick() && iterations
< maxIterations
) {
624 if (!KGlobalSettings::singleClick()) {
625 // TODO: Try to find a way to make sure that changing the global setting works.
626 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
629 m_view
->event(&mousePressEvent
);
630 m_view
->event(&mouseReleaseEvent
);
631 QCOMPARE(spyItemActivated
.count(), 1);
632 spyItemActivated
.clear();
634 // Enforce single click activation in the controller.
635 m_controller
->setSingleClickActivationEnforced(true);
636 m_view
->event(&mousePressEvent
);
637 m_view
->event(&mouseReleaseEvent
);
638 QCOMPARE(spyItemActivated
.count(), 1);
639 spyItemActivated
.clear();
641 // Restore previous settings.
642 m_controller
->setSingleClickActivationEnforced(true);
643 group
.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick
, KConfig::Persistent
|KConfig::Global
);
645 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged
, KGlobalSettings::SETTINGS_MOUSE
);
648 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
&& iterations
< maxIterations
) {
653 if (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick
) {
654 // TODO: Try to find a way to make sure that changing the global setting works.
655 QSKIP("Failed to change the KGlobalSettings::singleClick() setting!", SkipSingle
);
659 void KItemListControllerTest::adjustGeometryForColumnCount(int count
)
661 const QSize size
= m_view
->itemSize().toSize();
663 QRect rect
= m_container
->geometry();
664 rect
.setSize(size
* count
);
665 m_container
->setGeometry(rect
);
667 // Increase the size of the container until the correct column count is reached.
668 while (m_view
->m_layouter
->m_columnCount
< count
) {
669 rect
= m_container
->geometry();
670 rect
.setSize(rect
.size() + size
);
671 m_container
->setGeometry(rect
);
675 QTEST_MAIN(KItemListControllerTest
)
677 #include "kitemlistcontrollertest.moc"