]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Ported tests away from KRandomSequence, QTest::kWaitForSignal and KTempDir.
[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 "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"
26 #include "testdir.h"
27
28 #include <KConfigGroup>
29 #include <KGlobalSettings>
30
31 #include <QTest>
32 #include <QGraphicsSceneMouseEvent>
33 #include <QSignalSpy>
34
35 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout);
36 Q_DECLARE_METATYPE(Qt::Orientation);
37 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior);
38 Q_DECLARE_METATYPE(KItemSet);
39
40 class KItemListControllerTest : public QObject
41 {
42 Q_OBJECT
43
44 private slots:
45 void initTestCase();
46 void cleanupTestCase();
47
48 void init();
49 void cleanup();
50
51 void testKeyboardNavigation_data();
52 void testKeyboardNavigation();
53 void testMouseClickActivation();
54
55 private:
56 /**
57 * Make sure that the number of columns in the view is equal to \a count
58 * by changing the geometry of the container.
59 */
60 void adjustGeometryForColumnCount(int count);
61
62 private:
63 KFileItemListView* m_view;
64 KItemListController* m_controller;
65 KItemListSelectionManager* m_selectionManager;
66 KFileItemModel* m_model;
67 TestDir* m_testDir;
68 KItemListContainer* m_container;
69 };
70
71 /**
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.
75 */
76 void KItemListControllerTest::initTestCase()
77 {
78 qRegisterMetaType<KItemSet>("KItemSet");
79
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();
88
89 QStringList files;
90 files
91 << "a1" << "a2" << "a3"
92 << "b1"
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";
96
97 m_testDir->createFiles(files);
98 m_model->loadDirectory(m_testDir->url());
99 QSignalSpy spyDirectoryLoadingCompleted(m_model, SIGNAL(directoryLoadingCompleted()));
100 QVERIFY(spyDirectoryLoadingCompleted.wait());
101
102 m_container->show();
103 QTest::qWaitForWindowShown(m_container);
104 }
105
106 void KItemListControllerTest::cleanupTestCase()
107 {
108 delete m_container;
109 m_container = 0;
110
111 delete m_testDir;
112 m_testDir = 0;
113 }
114
115 /** Before each test, the current item, selection, and item size are reset to the defaults. */
116 void KItemListControllerTest::init()
117 {
118 m_selectionManager->setCurrentItem(0);
119 QCOMPARE(m_selectionManager->currentItem(), 0);
120
121 m_selectionManager->clearSelection();
122 QVERIFY(!m_selectionManager->hasSelection());
123
124 const QSizeF itemSize(50, 50);
125 m_view->setItemSize(itemSize);
126 QCOMPARE(m_view->itemSize(), itemSize);
127 }
128
129 void KItemListControllerTest::cleanup()
130 {
131 }
132
133 /**
134 * \class KeyPress is a small helper struct that represents a key press event,
135 * including the key and the keyboard modifiers.
136 */
137 struct KeyPress {
138
139 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier) :
140 m_key(key),
141 m_modifier(modifier)
142 {}
143
144 Qt::Key m_key;
145 Qt::KeyboardModifiers m_modifier;
146 };
147
148 /**
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
153 * key press.
154 */
155 struct ViewState {
156
157 ViewState(int current, const KItemSet selection, bool activated = false) :
158 m_current(current),
159 m_selection(selection),
160 m_activated(activated)
161 {}
162
163 int m_current;
164 KItemSet m_selection;
165 bool m_activated;
166 };
167
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>);
171
172 /**
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.
179 */
180 void KItemListControllerTest::testKeyboardNavigation_data()
181 {
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");
188
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";
197
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";
206
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";
213
214 foreach (const KFileItemListView::ItemLayout& layout, layoutList) {
215 // The following settings depend on the layout.
216 // Note that 'columns' are actually 'rows' in
217 // Compact layout.
218 Qt::Orientation scrollOrientation;
219 QList<int> columnCountList;
220 Qt::Key nextItemKey;
221 Qt::Key previousItemKey;
222 Qt::Key nextRowKey;
223 Qt::Key previousRowKey;
224
225 switch (layout) {
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;
233 break;
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;
241 break;
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;
249 break;
250 }
251
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;
256
257 // First, key presses which should have the same effect
258 // for any layout and any number of columns.
259 testList
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()));
288
289 // Next, we test combinations of key presses which only work for a
290 // particular number of columns and either enabled or disabled grouping.
291
292 // One column.
293 if (columnCount == 1) {
294 testList
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));
301 }
302
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.
307
308 if (columnCount == 3 && !groupingEnabled) {
309 // 3 columns, no grouping:
310 //
311 // a1 a2 a3 | 0 1 2
312 // b1 c1 c2 | 3 4 5
313 // c3 c4 c5 | 6 7 8
314 // d1 d2 d3 | 9 10 11
315 // d4 e1 e2 | 12 13 14
316 // e3 e4 e5 | 15 16 17
317 // e6 e7 | 18 19
318 testList
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));
339 }
340
341 if (columnCount == 5 && !groupingEnabled) {
342 // 5 columns, no grouping:
343 //
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
348 testList
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));
360 }
361
362 if (columnCount == 3 && groupingEnabled) {
363 // 3 columns, with grouping:
364 //
365 // a1 a2 a3 | 0 1 2
366 // b1 | 3
367 // c1 c2 c3 | 4 5 6
368 // c4 c5 | 7 8
369 // d1 d2 d3 | 9 10 11
370 // d4 | 12
371 // e1 e2 e3 | 13 14 15
372 // e4 e5 e6 | 16 17 18
373 // e7 | 19
374 testList
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));
388 }
389
390 if (columnCount == 5 && groupingEnabled) {
391 // 5 columns, with grouping:
392 //
393 // a1 a2 a3 | 0 1 2
394 // b1 | 3
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
398 // e6 e7 | 18 19
399 testList
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));
413 }
414
415 const QString testName =
416 layoutNames[layout] + ", " +
417 QString("%1 columns, ").arg(columnCount) +
418 selectionBehaviorNames[selectionBehavior] + ", " +
419 groupingEnabledNames[groupingEnabled];
420
421 const QByteArray testNameAscii = testName.toAscii();
422
423 QTest::newRow(testNameAscii.data())
424 << layout
425 << scrollOrientation
426 << columnCount
427 << selectionBehavior
428 << groupingEnabled
429 << testList;
430 }
431 }
432 }
433 }
434 }
435
436 /**
437 * This function sets the view's properties according to the data provided by
438 * KItemListControllerTest::testKeyboardNavigation_data().
439 *
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.
442 */
443 void KItemListControllerTest::testKeyboardNavigation()
444 {
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);
451
452 m_view->setItemLayout(layout);
453 QCOMPARE(m_view->itemLayout(), layout);
454
455 m_view->setScrollOrientation(scrollOrientation);
456 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
457
458 m_controller->setSelectionBehavior(selectionBehavior);
459 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
460
461 m_model->setGroupedSorting(groupingEnabled);
462 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
463
464 adjustGeometryForColumnCount(columnCount);
465 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
466
467 QSignalSpy spySingleItemActivated(m_controller, SIGNAL(itemActivated(int)));
468 QSignalSpy spyMultipleItemsActivated(m_controller, SIGNAL(itemsActivated(KItemSet)));
469
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;
477
478 QTest::keyClick(m_container, key, modifier);
479
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;
485 }
486
487 if (activated) {
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());
496 } else {
497 QVERIFY(spySingleItemActivated.isEmpty());
498 QVERIFY(!spyMultipleItemsActivated.isEmpty());
499 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
500 }
501 break;
502 }
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());
511 break;
512 }
513 }
514 }
515 }
516
517 void KItemListControllerTest::testMouseClickActivation()
518 {
519 m_view->setItemLayout(KFileItemListView::IconsLayout);
520
521 // Make sure that we have a large window, such that
522 // the items are visible and clickable.
523 adjustGeometryForColumnCount(5);
524
525 // Make sure that the first item is visible in the view.
526 m_view->setScrollOffset(0);
527 QCOMPARE(m_view->firstVisibleIndex(), 0);
528
529 const QPointF pos = m_view->itemContextRect(0).center();
530
531 // Save the "single click" setting.
532 const bool restoreKGlobalSettingsSingleClick = KGlobalSettings::singleClick();
533
534 KConfig config("kcminputrc");
535 KConfigGroup group = config.group("KDE");
536
537 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
538 mousePressEvent.setPos(pos);
539 mousePressEvent.setButton(Qt::LeftButton);
540 mousePressEvent.setButtons(Qt::LeftButton);
541
542 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
543 mouseReleaseEvent.setPos(pos);
544 mouseReleaseEvent.setButton(Qt::LeftButton);
545 mouseReleaseEvent.setButtons(Qt::NoButton);
546
547 QSignalSpy spyItemActivated(m_controller, SIGNAL(itemActivated(int)));
548
549 // Default setting: single click activation.
550 group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
551 config.sync();
552 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
553
554 int iterations = 0;
555 const int maxIterations = 20;
556 while (!KGlobalSettings::singleClick() && iterations < maxIterations) {
557 QTest::qWait(50);
558 ++iterations;
559 }
560
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!");
564 }
565
566 m_view->event(&mousePressEvent);
567 m_view->event(&mouseReleaseEvent);
568 QCOMPARE(spyItemActivated.count(), 1);
569 spyItemActivated.clear();
570
571 // Set the global setting to "double click activation".
572 group.writeEntry("SingleClick", false, KConfig::Persistent|KConfig::Global);
573 config.sync();
574 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
575
576 iterations = 0;
577 while (KGlobalSettings::singleClick() && iterations < maxIterations) {
578 QTest::qWait(50);
579 ++iterations;
580 }
581
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!");
585 }
586
587 m_view->event(&mousePressEvent);
588 m_view->event(&mouseReleaseEvent);
589 QCOMPARE(spyItemActivated.count(), 0);
590 spyItemActivated.clear();
591
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();
598
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();
605
606 // Set the global setting back to "single click activation".
607 group.writeEntry("SingleClick", true, KConfig::Persistent|KConfig::Global);
608 config.sync();
609 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
610
611 iterations = 0;
612 while (!KGlobalSettings::singleClick() && iterations < maxIterations) {
613 QTest::qWait(50);
614 ++iterations;
615 }
616
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!");
620 }
621
622 m_view->event(&mousePressEvent);
623 m_view->event(&mouseReleaseEvent);
624 QCOMPARE(spyItemActivated.count(), 1);
625 spyItemActivated.clear();
626
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();
633
634 // Restore previous settings.
635 m_controller->setSingleClickActivationEnforced(true);
636 group.writeEntry("SingleClick", restoreKGlobalSettingsSingleClick, KConfig::Persistent|KConfig::Global);
637 config.sync();
638 KGlobalSettings::self()->emitChange(KGlobalSettings::SettingsChanged, KGlobalSettings::SETTINGS_MOUSE);
639
640 iterations = 0;
641 while (KGlobalSettings::singleClick() != restoreKGlobalSettingsSingleClick && iterations < maxIterations) {
642 QTest::qWait(50);
643 ++iterations;
644 }
645
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!");
649 }
650 }
651
652 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
653 {
654 const QSize size = m_view->itemSize().toSize();
655
656 QRect rect = m_container->geometry();
657 rect.setSize(size * count);
658 m_container->setGeometry(rect);
659
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);
665 }
666 }
667
668 QTEST_MAIN(KItemListControllerTest)
669
670 #include "kitemlistcontrollertest.moc"