]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Merge remote-tracking branch 'origin/KDE/4.10'
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
1 /***************************************************************************
2 * Copyright (C) 2012 by Frank Reininghaus <frank78ac@googlemail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include <qtest_kde.h>
21 #include <qtestmouse.h>
22 #include <qtestkeyboard.h>
23
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"
30 #include "testdir.h"
31
32 #include <KConfigGroup>
33 #include <KGlobalSettings>
34
35 #include <QGraphicsSceneMouseEvent>
36
37 namespace {
38 const int DefaultTimeout = 2000;
39 };
40
41 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout);
42 Q_DECLARE_METATYPE(Qt::Orientation);
43 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior);
44 Q_DECLARE_METATYPE(QSet<int>);
45
46 class KItemListControllerTest : public QObject
47 {
48 Q_OBJECT
49
50 private slots:
51 void initTestCase();
52 void cleanupTestCase();
53
54 void init();
55 void cleanup();
56
57 void testKeyboardNavigation_data();
58 void testKeyboardNavigation();
59 void testMouseClickActivation();
60
61 private:
62 /**
63 * Make sure that the number of columns in the view is equal to \a count
64 * by changing the geometry of the container.
65 */
66 void adjustGeometryForColumnCount(int count);
67
68 private:
69 KFileItemListView* m_view;
70 KItemListController* m_controller;
71 KItemListSelectionManager* m_selectionManager;
72 KFileItemModel* m_model;
73 TestDir* m_testDir;
74 KItemListContainer* m_container;
75 };
76
77 /**
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.
81 */
82 void KItemListControllerTest::initTestCase()
83 {
84 qRegisterMetaType<QSet<int> >("QSet<int>");
85
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();
94
95 QStringList files;
96 files
97 << "a1" << "a2" << "a3"
98 << "b1"
99 << "c1" << "c2" << "c3" << "c4" << "c5"
100 << "d1" << "d2" << "d3" << "d4"
101 << "e1" << "e2" << "e3" << "e4" << "e5" << "e6" << "e7";
102
103 m_testDir->createFiles(files);
104 m_model->loadDirectory(m_testDir->url());
105 QVERIFY(QTest::kWaitForSignal(m_model, SIGNAL(directoryLoadingCompleted()), DefaultTimeout));
106
107 m_container->show();
108 QTest::qWaitForWindowShown(m_container);
109 }
110
111 void KItemListControllerTest::cleanupTestCase()
112 {
113 delete m_container;
114 m_container = 0;
115
116 delete m_testDir;
117 m_testDir = 0;
118 }
119
120 /** Before each test, the current item, selection, and item size are reset to the defaults. */
121 void KItemListControllerTest::init()
122 {
123 m_selectionManager->setCurrentItem(0);
124 QCOMPARE(m_selectionManager->currentItem(), 0);
125
126 m_selectionManager->clearSelection();
127 QVERIFY(!m_selectionManager->hasSelection());
128
129 const QSizeF itemSize(50, 50);
130 m_view->setItemSize(itemSize);
131 QCOMPARE(m_view->itemSize(), itemSize);
132 }
133
134 void KItemListControllerTest::cleanup()
135 {
136 }
137
138 /**
139 * \class KeyPress is a small helper struct that represents a key press event,
140 * including the key and the keyboard modifiers.
141 */
142 struct KeyPress {
143
144 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier) :
145 m_key(key),
146 m_modifier(modifier)
147 {}
148
149 Qt::Key m_key;
150 Qt::KeyboardModifiers m_modifier;
151 };
152
153 /**
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
158 * key press.
159 */
160 struct ViewState {
161
162 ViewState(int current, const QSet<int> selection, bool activated = false) :
163 m_current(current),
164 m_selection(selection),
165 m_activated(activated)
166 {}
167
168 int m_current;
169 QSet<int> m_selection;
170 bool m_activated;
171 };
172
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>);
176
177 /**
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.
184 */
185 void KItemListControllerTest::testKeyboardNavigation_data()
186 {
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");
193
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";
202
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";
211
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";
218
219 foreach (const KFileItemListView::ItemLayout& layout, layoutList) {
220 // The following settings depend on the layout.
221 // Note that 'columns' are actually 'rows' in
222 // Compact layout.
223 Qt::Orientation scrollOrientation;
224 QList<int> columnCountList;
225 Qt::Key nextItemKey;
226 Qt::Key previousItemKey;
227 Qt::Key nextRowKey;
228 Qt::Key previousRowKey;
229
230 switch (layout) {
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;
238 break;
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;
246 break;
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;
254 break;
255 }
256
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;
261
262 // First, key presses which should have the same effect
263 // for any layout and any number of columns.
264 testList
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
287 // Next, we test combinations of key presses which only work for a
288 // particular number of columns and either enabled or disabled grouping.
289
290 // One column.
291 if (columnCount == 1) {
292 testList
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));
299 }
300
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.
305
306 if (columnCount == 3 && !groupingEnabled) {
307 // 3 columns, no grouping:
308 //
309 // a1 a2 a3 | 0 1 2
310 // b1 c1 c2 | 3 4 5
311 // c3 c4 c5 | 6 7 8
312 // d1 d2 d3 | 9 10 11
313 // d4 e1 e2 | 12 13 14
314 // e3 e4 e5 | 15 16 17
315 // e6 e7 | 18 19
316 testList
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));
337 }
338
339 if (columnCount == 5 && !groupingEnabled) {
340 // 5 columns, no grouping:
341 //
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
346 testList
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));
358 }
359
360 if (columnCount == 3 && groupingEnabled) {
361 // 3 columns, with grouping:
362 //
363 // a1 a2 a3 | 0 1 2
364 // b1 | 3
365 // c1 c2 c3 | 4 5 6
366 // c4 c5 | 7 8
367 // d1 d2 d3 | 9 10 11
368 // d4 | 12
369 // e1 e2 e3 | 13 14 15
370 // e4 e5 e6 | 16 17 18
371 // e7 | 19
372 testList
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));
386 }
387
388 if (columnCount == 5 && groupingEnabled) {
389 // 5 columns, with grouping:
390 //
391 // a1 a2 a3 | 0 1 2
392 // b1 | 3
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
396 // e6 e7 | 18 19
397 testList
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));
411 }
412
413 const QString testName =
414 layoutNames[layout] + ", " +
415 QString("%1 columns, ").arg(columnCount) +
416 selectionBehaviorNames[selectionBehavior] + ", " +
417 groupingEnabledNames[groupingEnabled];
418
419 const QByteArray testNameAscii = testName.toAscii();
420
421 QTest::newRow(testNameAscii.data())
422 << layout
423 << scrollOrientation
424 << columnCount
425 << selectionBehavior
426 << groupingEnabled
427 << testList;
428 }
429 }
430 }
431 }
432 }
433
434 /**
435 * This function sets the view's properties according to the data provided by
436 * KItemListControllerTest::testKeyboardNavigation_data().
437 *
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.
440 */
441 void KItemListControllerTest::testKeyboardNavigation()
442 {
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);
449
450 m_view->setItemLayout(layout);
451 QCOMPARE(m_view->itemLayout(), layout);
452
453 m_view->setScrollOrientation(scrollOrientation);
454 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
455
456 m_controller->setSelectionBehavior(selectionBehavior);
457 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
458
459 m_model->setGroupedSorting(groupingEnabled);
460 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
461
462 adjustGeometryForColumnCount(columnCount);
463 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
464
465 QSignalSpy spySingleItemActivated(m_controller, SIGNAL(itemActivated(int)));
466 QSignalSpy spyMultipleItemsActivated(m_controller, SIGNAL(itemsActivated(QSet<int>)));
467
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;
475
476 QTest::keyClick(m_container, key, modifier);
477
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;
483 }
484
485 if (activated) {
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());
494 } else {
495 QVERIFY(spySingleItemActivated.isEmpty());
496 QVERIFY(!spyMultipleItemsActivated.isEmpty());
497 QCOMPARE(qvariant_cast<QSet<int> >(spyMultipleItemsActivated.takeFirst().at(0)), selection);
498 }
499 break;
500 }
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());
509 break;
510 }
511 }
512 }
513 }
514
515 void KItemListControllerTest::testMouseClickActivation()
516 {
517 m_view->setItemLayout(KFileItemListView::IconsLayout);
518
519 // Make sure that we have a large window, such that
520 // the items are visible and clickable.
521 adjustGeometryForColumnCount(5);
522
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) {
527 QTest::qWait(50);
528 }
529
530 const QPointF pos = m_view->itemContextRect(0).center();
531
532 // Save the "single click" setting.
533 const bool restoreKGlobalSettingsSingleClick = KGlobalSettings::singleClick();
534
535 KConfig config("kcminputrc");
536 KConfigGroup group = config.group("KDE");
537
538 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
539 mousePressEvent.setPos(pos);
540 mousePressEvent.setButton(Qt::LeftButton);
541 mousePressEvent.setButtons(Qt::LeftButton);
542
543 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
544 mouseReleaseEvent.setPos(pos);
545 mouseReleaseEvent.setButton(Qt::LeftButton);
546 mouseReleaseEvent.setButtons(Qt::NoButton);
547
548 QSignalSpy spyItemActivated(m_controller, SIGNAL(itemActivated(int)));
549
550 // Default setting: single click activation.
551 group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
552 config.sync();
553 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
554 while (!KGlobalSettings::singleClick()) {
555 QTest::qWait(50);
556 }
557 m_view->event(&mousePressEvent);
558 m_view->event(&mouseReleaseEvent);
559 QCOMPARE(spyItemActivated.count(), 1);
560 spyItemActivated.clear();
561
562 // Set the global setting to "double click activation".
563 group.writeEntry("SingleClick", false, KConfig::Persistent|KConfig::Global);
564 config.sync();
565 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
566 while (KGlobalSettings::singleClick()) {
567 QTest::qWait(50);
568 }
569 m_view->event(&mousePressEvent);
570 m_view->event(&mouseReleaseEvent);
571 QCOMPARE(spyItemActivated.count(), 0);
572 spyItemActivated.clear();
573
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();
580
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();
587
588 // Set the global setting back to "single click activation".
589 group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
590 config.sync();
591 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
592 while (!KGlobalSettings::singleClick()) {
593 QTest::qWait(50);
594 }
595 m_view->event(&mousePressEvent);
596 m_view->event(&mouseReleaseEvent);
597 QCOMPARE(spyItemActivated.count(), 1);
598 spyItemActivated.clear();
599
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();
606
607 // Restore previous settings.
608 m_controller->setSingleClickActivationEnforced(true);
609 group.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick, KConfig::Persistent|KConfig::Global);
610 config.sync();
611 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
612 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick) {
613 QTest::qWait(50);
614 }
615 }
616
617 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
618 {
619 const QSize size = m_view->itemSize().toSize();
620
621 QRect rect = m_container->geometry();
622 rect.setSize(size * count);
623 m_container->setGeometry(rect);
624
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);
630 }
631 }
632
633 QTEST_KDEMAIN(KItemListControllerTest, GUI)
634
635 #include "kitemlistcontrollertest.moc"