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