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