]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
Revert "Revert "Disable Hidden Files Last sort by default""
[dolphin.git] / src / tests / kitemlistcontrollertest.cpp
1 /*
2 * SPDX-FileCopyrightText: 2012 Frank Reininghaus <frank78ac@googlemail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "kitemviews/kitemlistcontainer.h"
8 #include "kitemviews/kfileitemlistview.h"
9 #include "kitemviews/kfileitemmodel.h"
10 #include "kitemviews/kitemlistcontroller.h"
11 #include "kitemviews/kitemlistselectionmanager.h"
12 #include "kitemviews/private/kitemlistviewlayouter.h"
13 #include "testdir.h"
14
15 #include <QTest>
16 #include <QGraphicsSceneMouseEvent>
17 #include <QSignalSpy>
18 #include <QProxyStyle>
19 #include <QStandardPaths>
20
21 /**
22 * \class KItemListControllerTestStyle is a proxy style for testing the
23 * KItemListController with different style hint options, e.g. single/double
24 * click activation.
25 */
26 class KItemListControllerTestStyle : public QProxyStyle
27 {
28 Q_OBJECT
29 public:
30 KItemListControllerTestStyle(QStyle* style) :
31 QProxyStyle(style),
32 m_activateItemOnSingleClick((bool)style->styleHint(SH_ItemView_ActivateItemOnSingleClick))
33 {
34 }
35
36 void setActivateItemOnSingleClick(bool activateItemOnSingleClick)
37 {
38 m_activateItemOnSingleClick = activateItemOnSingleClick;
39 }
40
41 bool activateItemOnSingleClick() const
42 {
43 return m_activateItemOnSingleClick;
44 }
45
46 int styleHint(StyleHint hint,
47 const QStyleOption* option = nullptr,
48 const QWidget* widget = nullptr,
49 QStyleHintReturn* returnData = nullptr) const override
50 {
51 switch (hint) {
52 case QStyle::SH_ItemView_ActivateItemOnSingleClick:
53 return (int)activateItemOnSingleClick();
54 default:
55 return QProxyStyle::styleHint(hint, option, widget, returnData);
56 }
57 }
58
59 private:
60 bool m_activateItemOnSingleClick;
61 };
62
63 Q_DECLARE_METATYPE(KFileItemListView::ItemLayout)
64 Q_DECLARE_METATYPE(Qt::Orientation)
65 Q_DECLARE_METATYPE(KItemListController::SelectionBehavior)
66 Q_DECLARE_METATYPE(KItemSet)
67
68 class KItemListControllerTest : public QObject
69 {
70 Q_OBJECT
71
72 private Q_SLOTS:
73 void initTestCase();
74 void cleanupTestCase();
75
76 void init();
77 void cleanup();
78
79 void testKeyboardNavigation_data();
80 void testKeyboardNavigation();
81 void testMouseClickActivation();
82
83 private:
84 /**
85 * Make sure that the number of columns in the view is equal to \a count
86 * by changing the geometry of the container.
87 */
88 void adjustGeometryForColumnCount(int count);
89
90 private:
91 KFileItemListView* m_view;
92 KItemListController* m_controller;
93 KItemListSelectionManager* m_selectionManager;
94 KFileItemModel* m_model;
95 TestDir* m_testDir;
96 KItemListContainer* m_container;
97 KItemListControllerTestStyle* m_testStyle;
98 };
99
100 /**
101 * This function initializes the member objects, creates the temporary files, and
102 * shows the view on the screen on startup. This could also be done before every
103 * single test, but this would make the time needed to run the test much larger.
104 */
105 void KItemListControllerTest::initTestCase()
106 {
107 QStandardPaths::setTestModeEnabled(true);
108 qRegisterMetaType<KItemSet>("KItemSet");
109
110 m_testDir = new TestDir();
111 m_model = new KFileItemModel();
112 m_view = new KFileItemListView();
113 m_controller = new KItemListController(m_model, m_view, this);
114 m_container = new KItemListContainer(m_controller);
115 m_controller = m_container->controller();
116 m_controller->setSelectionBehavior(KItemListController::MultiSelection);
117 m_selectionManager = m_controller->selectionManager();
118 m_testStyle = new KItemListControllerTestStyle(m_view->style());
119 m_view->setStyle(m_testStyle);
120
121 QStringList files;
122 files
123 << "a1" << "a2" << "a3"
124 << "b1"
125 << "c1" << "c2" << "c3" << "c4" << "c5"
126 << "d1" << "d2" << "d3" << "d4"
127 << "e" << "e 2" << "e 3" << "e 4" << "e 5" << "e 6" << "e 7";
128
129 m_testDir->createFiles(files);
130 m_model->loadDirectory(m_testDir->url());
131 QSignalSpy spyDirectoryLoadingCompleted(m_model, &KFileItemModel::directoryLoadingCompleted);
132 QVERIFY(spyDirectoryLoadingCompleted.wait());
133
134 m_container->show();
135 QVERIFY(QTest::qWaitForWindowExposed(m_container));
136 }
137
138 void KItemListControllerTest::cleanupTestCase()
139 {
140 delete m_container;
141 m_container = nullptr;
142
143 delete m_testDir;
144 m_testDir = nullptr;
145 }
146
147 /** Before each test, the current item, selection, and item size are reset to the defaults. */
148 void KItemListControllerTest::init()
149 {
150 m_selectionManager->setCurrentItem(0);
151 QCOMPARE(m_selectionManager->currentItem(), 0);
152
153 m_selectionManager->clearSelection();
154 QVERIFY(!m_selectionManager->hasSelection());
155
156 const QSizeF itemSize(50, 50);
157 m_view->setItemSize(itemSize);
158 QCOMPARE(m_view->itemSize(), itemSize);
159 }
160
161 void KItemListControllerTest::cleanup()
162 {
163 }
164
165 /**
166 * \class KeyPress is a small helper struct that represents a key press event,
167 * including the key and the keyboard modifiers.
168 */
169 struct KeyPress {
170
171 KeyPress(Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier) :
172 m_key(key),
173 m_modifier(modifier)
174 {}
175
176 Qt::Key m_key;
177 Qt::KeyboardModifiers m_modifier;
178 };
179
180 /**
181 * \class ViewState is a small helper struct that represents a certain state
182 * of the view, including the current item, the selected items in MultiSelection
183 * mode (in the other modes, the selection is either empty or equal to the
184 * current item), and the information whether items were activated by the last
185 * key press.
186 */
187 struct ViewState {
188
189 ViewState(int current, const KItemSet &selection, bool activated = false) :
190 m_current(current),
191 m_selection(selection),
192 m_activated(activated)
193 {}
194
195 int m_current;
196 KItemSet m_selection;
197 bool m_activated;
198 };
199
200 // We have to define a typedef for the pair in order to make the test compile.
201 typedef QPair<KeyPress, ViewState> keyPressViewStatePair;
202 Q_DECLARE_METATYPE(QList<keyPressViewStatePair>)
203
204 /**
205 * This function provides the data for the actual test function
206 * KItemListControllerTest::testKeyboardNavigation().
207 * It tests all possible combinations of view layouts, selection behaviors,
208 * and enabled/disabled groupings for different column counts, and
209 * provides a list of key presses and the states that the view should be in
210 * after the key press event.
211 */
212 void KItemListControllerTest::testKeyboardNavigation_data()
213 {
214 QTest::addColumn<KFileItemListView::ItemLayout>("layout");
215 QTest::addColumn<Qt::Orientation>("scrollOrientation");
216 QTest::addColumn<int>("columnCount");
217 QTest::addColumn<KItemListController::SelectionBehavior>("selectionBehavior");
218 QTest::addColumn<bool>("groupingEnabled");
219 QTest::addColumn<QList<QPair<KeyPress, ViewState> > >("testList");
220
221 QList<KFileItemListView::ItemLayout> layoutList;
222 QHash<KFileItemListView::ItemLayout, QString> layoutNames;
223 layoutList.append(KFileItemListView::IconsLayout);
224 layoutNames[KFileItemListView::IconsLayout] = "Icons";
225 layoutList.append(KFileItemListView::CompactLayout);
226 layoutNames[KFileItemListView::CompactLayout] = "Compact";
227 layoutList.append(KFileItemListView::DetailsLayout);
228 layoutNames[KFileItemListView::DetailsLayout] = "Details";
229
230 QList<KItemListController::SelectionBehavior> selectionBehaviorList;
231 QHash<KItemListController::SelectionBehavior, QString> selectionBehaviorNames;
232 selectionBehaviorList.append(KItemListController::NoSelection);
233 selectionBehaviorNames[KItemListController::NoSelection] = "NoSelection";
234 selectionBehaviorList.append(KItemListController::SingleSelection);
235 selectionBehaviorNames[KItemListController::SingleSelection] = "SingleSelection";
236 selectionBehaviorList.append(KItemListController::MultiSelection);
237 selectionBehaviorNames[KItemListController::MultiSelection] = "MultiSelection";
238
239 QList<bool> groupingEnabledList;
240 QHash<bool, QString> groupingEnabledNames;
241 groupingEnabledList.append(false);
242 groupingEnabledNames[false] = "ungrouped";
243 groupingEnabledList.append(true);
244 groupingEnabledNames[true] = "grouping enabled";
245
246 for (const KFileItemListView::ItemLayout& layout : layoutList) {
247 // The following settings depend on the layout.
248 // Note that 'columns' are actually 'rows' in
249 // Compact layout.
250 Qt::Orientation scrollOrientation;
251 QList<int> columnCountList;
252 Qt::Key nextItemKey;
253 Qt::Key previousItemKey;
254 Qt::Key nextRowKey;
255 Qt::Key previousRowKey;
256
257 switch (layout) {
258 case KFileItemListView::IconsLayout:
259 scrollOrientation = Qt::Vertical;
260 columnCountList << 1 << 3 << 5;
261 nextItemKey = Qt::Key_Right;
262 previousItemKey = Qt::Key_Left;
263 nextRowKey = Qt::Key_Down;
264 previousRowKey = Qt::Key_Up;
265 break;
266 case KFileItemListView::CompactLayout:
267 scrollOrientation = Qt::Horizontal;
268 columnCountList << 1 << 3 << 5;
269 nextItemKey = Qt::Key_Down;
270 previousItemKey = Qt::Key_Up;
271 nextRowKey = Qt::Key_Right;
272 previousRowKey = Qt::Key_Left;
273 break;
274 case KFileItemListView::DetailsLayout:
275 scrollOrientation = Qt::Vertical;
276 columnCountList << 1;
277 nextItemKey = Qt::Key_Down;
278 previousItemKey = Qt::Key_Up;
279 nextRowKey = Qt::Key_Down;
280 previousRowKey = Qt::Key_Up;
281 break;
282 }
283
284 for (int columnCount : qAsConst(columnCountList)) {
285 for (const KItemListController::SelectionBehavior& selectionBehavior : qAsConst(selectionBehaviorList)) {
286 for (bool groupingEnabled : qAsConst(groupingEnabledList)) {
287 QList<QPair<KeyPress, ViewState> > testList;
288
289 // First, key presses which should have the same effect
290 // for any layout and any number of columns.
291 testList
292 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
293 << qMakePair(KeyPress(Qt::Key_Return), ViewState(1, KItemSet() << 1, true))
294 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(1, KItemSet() << 1, true))
295 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
296 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
297 << qMakePair(KeyPress(Qt::Key_Return), ViewState(3, KItemSet() << 2 << 3, true))
298 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 2))
299 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
300 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 2 << 3))
301 << qMakePair(KeyPress(Qt::Key_Return), ViewState(4, KItemSet() << 2 << 3, true))
302 << qMakePair(KeyPress(previousItemKey), ViewState(3, KItemSet() << 3))
303 << qMakePair(KeyPress(Qt::Key_Home, Qt::ShiftModifier), ViewState(0, KItemSet() << 0 << 1 << 2 << 3))
304 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
305 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 2 << 3))
306 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(1, KItemSet() << 0 << 1 << 2 << 3))
307 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
308 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(18, KItemSet() << 18 << 19))
309 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
310 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
311 << qMakePair(KeyPress(Qt::Key_Enter), ViewState(0, KItemSet(), true))
312 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet() << 0))
313 << qMakePair(KeyPress(Qt::Key_Space, Qt::ControlModifier), ViewState(0, KItemSet()))
314 << qMakePair(KeyPress(Qt::Key_Space), ViewState(0, KItemSet() << 0))
315 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
316 << qMakePair(KeyPress(Qt::Key_Space), ViewState(14, KItemSet() << 14))
317 << qMakePair(KeyPress(Qt::Key_3), ViewState(15, KItemSet() << 15))
318 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
319 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
320 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
321 << qMakePair(KeyPress(Qt::Key_E), ViewState(15, KItemSet() << 15))
322 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
323 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
324 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
325 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
326
327 // Next, we test combinations of key presses which only work for a
328 // particular number of columns and either enabled or disabled grouping.
329
330 // One column.
331 if (columnCount == 1) {
332 testList
333 << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
334 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
335 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
336 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
337 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
338 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
339 }
340
341 // Multiple columns: we test both 3 and 5 columns with grouping
342 // enabled or disabled. For each case, the layout of the items
343 // in the view is shown (both using file names and indices) to
344 // make it easier to understand what the tests do.
345
346 if (columnCount == 3 && !groupingEnabled) {
347 // 3 columns, no grouping:
348 //
349 // a1 a2 a3 | 0 1 2
350 // b1 c1 c2 | 3 4 5
351 // c3 c4 c5 | 6 7 8
352 // d1 d2 d3 | 9 10 11
353 // d4 e1 e2 | 12 13 14
354 // e3 e4 e5 | 15 16 17
355 // e6 e7 | 18 19
356 testList
357 << qMakePair(KeyPress(nextRowKey), ViewState(3, KItemSet() << 3))
358 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(4, KItemSet() << 3))
359 << qMakePair(KeyPress(nextRowKey), ViewState(7, KItemSet() << 7))
360 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
361 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(9, KItemSet() << 7 << 8 << 9))
362 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(8, KItemSet() << 7 << 8))
363 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
364 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
365 << qMakePair(KeyPress(previousItemKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 5 << 6 << 7))
366 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 6 << 7))
367 << qMakePair(KeyPress(nextItemKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7))
368 << qMakePair(KeyPress(nextRowKey), ViewState(10, KItemSet() << 10))
369 << qMakePair(KeyPress(nextItemKey), ViewState(11, KItemSet() << 11))
370 << qMakePair(KeyPress(nextRowKey), ViewState(14, KItemSet() << 14))
371 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
372 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
373 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
374 << qMakePair(KeyPress(Qt::Key_End), ViewState(19, KItemSet() << 19))
375 << qMakePair(KeyPress(previousRowKey), ViewState(16, KItemSet() << 16))
376 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
377 }
378
379 if (columnCount == 5 && !groupingEnabled) {
380 // 5 columns, no grouping:
381 //
382 // a1 a2 a3 b1 c1 | 0 1 2 3 4
383 // c2 c3 c4 c5 d1 | 5 6 7 8 9
384 // d2 d3 d4 e1 e2 | 10 11 12 13 14
385 // e3 e4 e5 e6 e7 | 15 16 17 18 19
386 testList
387 << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
388 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
389 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
390 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
391 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
392 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
393 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
394 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
395 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
396 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
397 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
398 }
399
400 if (columnCount == 3 && groupingEnabled) {
401 // 3 columns, with grouping:
402 //
403 // a1 a2 a3 | 0 1 2
404 // b1 | 3
405 // c1 c2 c3 | 4 5 6
406 // c4 c5 | 7 8
407 // d1 d2 d3 | 9 10 11
408 // d4 | 12
409 // e1 e2 e3 | 13 14 15
410 // e4 e5 e6 | 16 17 18
411 // e7 | 19
412 testList
413 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
414 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
415 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
416 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
417 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
418 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
419 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
420 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
421 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
422 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
423 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
424 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
425 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
426 }
427
428 if (columnCount == 5 && groupingEnabled) {
429 // 5 columns, with grouping:
430 //
431 // a1 a2 a3 | 0 1 2
432 // b1 | 3
433 // c1 c2 c3 c4 c5 | 4 5 6 7 8
434 // d1 d2 d3 d4 | 9 10 11 12
435 // e1 e2 e3 e4 e5 | 13 14 15 16 17
436 // e6 e7 | 18 19
437 testList
438 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
439 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
440 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
441 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
442 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
443 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
444 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
445 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
446 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
447 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
448 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
449 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
450 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
451 }
452
453 const QString testName =
454 layoutNames[layout] + ", " +
455 QString("%1 columns, ").arg(columnCount) +
456 selectionBehaviorNames[selectionBehavior] + ", " +
457 groupingEnabledNames[groupingEnabled];
458
459 const QByteArray testNameAscii = testName.toLatin1();
460
461 QTest::newRow(testNameAscii.data())
462 << layout
463 << scrollOrientation
464 << columnCount
465 << selectionBehavior
466 << groupingEnabled
467 << testList;
468 }
469 }
470 }
471 }
472 }
473
474 /**
475 * This function sets the view's properties according to the data provided by
476 * KItemListControllerTest::testKeyboardNavigation_data().
477 *
478 * The list \a testList contains pairs of key presses, which are sent to the
479 * container, and expected view states, which are verified then.
480 */
481 void KItemListControllerTest::testKeyboardNavigation()
482 {
483 QFETCH(KFileItemListView::ItemLayout, layout);
484 QFETCH(Qt::Orientation, scrollOrientation);
485 QFETCH(int, columnCount);
486 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
487 QFETCH(bool, groupingEnabled);
488 QFETCH(QList<keyPressViewStatePair>, testList);
489
490 m_view->setItemLayout(layout);
491 QCOMPARE(m_view->itemLayout(), layout);
492
493 m_view->setScrollOrientation(scrollOrientation);
494 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
495
496 m_controller->setSelectionBehavior(selectionBehavior);
497 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
498
499 m_model->setGroupedSorting(groupingEnabled);
500 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
501
502 adjustGeometryForColumnCount(columnCount);
503 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
504
505 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
506 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
507
508 while (!testList.isEmpty()) {
509 const QPair<KeyPress, ViewState> test = testList.takeFirst();
510 const Qt::Key key = test.first.m_key;
511 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
512 const int current = test.second.m_current;
513 const KItemSet selection = test.second.m_selection;
514 const bool activated = test.second.m_activated;
515
516 QTest::keyClick(m_container, key, modifier);
517
518 QCOMPARE(m_selectionManager->currentItem(), current);
519 switch (selectionBehavior) {
520 case KItemListController::NoSelection: QVERIFY(m_selectionManager->selectedItems().isEmpty()); break;
521 case KItemListController::SingleSelection: QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current); break;
522 case KItemListController::MultiSelection: QCOMPARE(m_selectionManager->selectedItems(), selection); break;
523 }
524
525 if (activated) {
526 switch (selectionBehavior) {
527 case KItemListController::MultiSelection:
528 if (!selection.isEmpty()) {
529 // The selected items should be activated.
530 if (selection.count() == 1) {
531 QVERIFY(!spySingleItemActivated.isEmpty());
532 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
533 QVERIFY(spyMultipleItemsActivated.isEmpty());
534 } else {
535 QVERIFY(spySingleItemActivated.isEmpty());
536 QVERIFY(!spyMultipleItemsActivated.isEmpty());
537 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
538 }
539 break;
540 }
541 // No items are selected. Therefore, the current item should be activated.
542 // This is handled by falling through to the NoSelection/SingleSelection case.
543 Q_FALLTHROUGH();
544 case KItemListController::NoSelection:
545 case KItemListController::SingleSelection:
546 // In NoSelection and SingleSelection mode, the current item should be activated.
547 QVERIFY(!spySingleItemActivated.isEmpty());
548 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
549 QVERIFY(spyMultipleItemsActivated.isEmpty());
550 break;
551 }
552 }
553 }
554 }
555
556 void KItemListControllerTest::testMouseClickActivation()
557 {
558 m_view->setItemLayout(KFileItemListView::IconsLayout);
559
560 // Make sure that we have a large window, such that
561 // the items are visible and clickable.
562 adjustGeometryForColumnCount(5);
563
564 // Make sure that the first item is visible in the view.
565 m_view->setScrollOffset(0);
566 QCOMPARE(m_view->firstVisibleIndex(), 0);
567
568 const QPointF pos = m_view->itemContextRect(0).center();
569
570 // Save the "single click" setting.
571 const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
572
573 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
574 mousePressEvent.setPos(pos);
575 mousePressEvent.setButton(Qt::LeftButton);
576 mousePressEvent.setButtons(Qt::LeftButton);
577
578 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
579 mouseReleaseEvent.setPos(pos);
580 mouseReleaseEvent.setButton(Qt::LeftButton);
581 mouseReleaseEvent.setButtons(Qt::NoButton);
582
583 QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
584
585 // Default setting: single click activation.
586 m_testStyle->setActivateItemOnSingleClick(true);
587 m_view->event(&mousePressEvent);
588 m_view->event(&mouseReleaseEvent);
589 QCOMPARE(spyItemActivated.count(), 1);
590 spyItemActivated.clear();
591
592 // Set the global setting to "double click activation".
593 m_testStyle->setActivateItemOnSingleClick(false);
594 m_view->event(&mousePressEvent);
595 m_view->event(&mouseReleaseEvent);
596 QCOMPARE(spyItemActivated.count(), 0);
597 spyItemActivated.clear();
598
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();
605
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();
612
613 // Set the global setting back to "single click activation".
614 m_testStyle->setActivateItemOnSingleClick(true);
615 m_view->event(&mousePressEvent);
616 m_view->event(&mouseReleaseEvent);
617 QCOMPARE(spyItemActivated.count(), 1);
618 spyItemActivated.clear();
619
620 // Enforce single click activation in the controller.
621 m_controller->setSingleClickActivationEnforced(true);
622 m_view->event(&mousePressEvent);
623 m_view->event(&mouseReleaseEvent);
624 QCOMPARE(spyItemActivated.count(), 1);
625 spyItemActivated.clear();
626
627 // Restore previous settings.
628 m_controller->setSingleClickActivationEnforced(true);
629 m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
630 }
631
632 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
633 {
634 const QSize size = m_view->itemSize().toSize();
635
636 QRect rect = m_container->geometry();
637 rect.setSize(size * count);
638 m_container->setGeometry(rect);
639
640 // Increase the size of the container until the correct column count is reached.
641 while (m_view->m_layouter->m_columnCount < count) {
642 rect = m_container->geometry();
643 rect.setSize(rect.size() + size);
644 m_container->setGeometry(rect);
645 }
646 }
647
648 QTEST_MAIN(KItemListControllerTest)
649
650 #include "kitemlistcontrollertest.moc"