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