]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/kitemlistcontrollertest.cpp
4cb1256e33d80ad62a54c371d4a68cb41da96b0f
[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_Escape), ViewState(15, KItemSet()))
330 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
331 << qMakePair(KeyPress(Qt::Key_E), ViewState(14, KItemSet() << 14))
332 << qMakePair(KeyPress(Qt::Key_E), ViewState(15, KItemSet() << 15))
333 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(15, KItemSet()))
334 << qMakePair(KeyPress(Qt::Key_E), ViewState(13, KItemSet() << 13))
335 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0))
336 << qMakePair(KeyPress(Qt::Key_Escape), ViewState(0, KItemSet()));
337
338 // Next, we test combinations of key presses which only work for a
339 // particular number of columns and either enabled or disabled grouping.
340
341 // One column.
342 if (columnCount == 1) {
343 testList
344 << qMakePair(KeyPress(nextRowKey), ViewState(1, KItemSet() << 1))
345 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(2, KItemSet() << 1 << 2))
346 << qMakePair(KeyPress(nextRowKey, Qt::ControlModifier), ViewState(3, KItemSet() << 1 << 2))
347 << qMakePair(KeyPress(previousRowKey), ViewState(2, KItemSet() << 2))
348 << qMakePair(KeyPress(previousItemKey), ViewState(1, KItemSet() << 1))
349 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
350 }
351
352 // Multiple columns: we test both 3 and 5 columns with grouping
353 // enabled or disabled. For each case, the layout of the items
354 // in the view is shown (both using file names and indices) to
355 // make it easier to understand what the tests do.
356
357 if (columnCount == 3 && !groupingEnabled) {
358 // 3 columns, no grouping:
359 //
360 // a1 a2 a3 | 0 1 2
361 // b1 c1 c2 | 3 4 5
362 // c3 c4 c5 | 6 7 8
363 // d1 d2 d3 | 9 10 11
364 // d4 e1 e2 | 12 13 14
365 // e3 e4 e5 | 15 16 17
366 // e6 e7 | 18 19
367 testList
368 << 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
398 << qMakePair(KeyPress(nextRowKey), ViewState(5, KItemSet() << 5))
399 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(6, KItemSet() << 5))
400 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
401 << qMakePair(KeyPress(nextItemKey), ViewState(12, KItemSet() << 12))
402 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(17, KItemSet() << 12 << 13 << 14 << 15 << 16 << 17))
403 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
404 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(7, KItemSet() << 7 << 8 << 9 << 10 << 11 << 12))
405 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(12, KItemSet() << 12))
406 << qMakePair(KeyPress(Qt::Key_End, Qt::ControlModifier), ViewState(19, KItemSet() << 12))
407 << qMakePair(KeyPress(previousRowKey), ViewState(14, KItemSet() << 14))
408 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
409 }
410
411 if (columnCount == 3 && groupingEnabled) {
412 // 3 columns, with grouping:
413 //
414 // a1 a2 a3 | 0 1 2
415 // b1 | 3
416 // c1 c2 c3 | 4 5 6
417 // c4 c5 | 7 8
418 // d1 d2 d3 | 9 10 11
419 // d4 | 12
420 // e1 e2 e3 | 13 14 15
421 // e4 e5 e6 | 16 17 18
422 // e7 | 19
423 testList
424 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
425 << qMakePair(KeyPress(nextItemKey), ViewState(2, KItemSet() << 2))
426 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 2 << 3))
427 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(6, KItemSet() << 2 << 3 << 4 << 5 << 6))
428 << qMakePair(KeyPress(nextRowKey), ViewState(8, KItemSet() << 8))
429 << qMakePair(KeyPress(nextRowKey), ViewState(11, KItemSet() << 11))
430 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(12, KItemSet() << 11))
431 << qMakePair(KeyPress(nextRowKey), ViewState(13, KItemSet() << 13))
432 << qMakePair(KeyPress(nextRowKey), ViewState(16, KItemSet() << 16))
433 << qMakePair(KeyPress(nextItemKey), ViewState(17, KItemSet() << 17))
434 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
435 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
436 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
437 }
438
439 if (columnCount == 5 && groupingEnabled) {
440 // 5 columns, with grouping:
441 //
442 // a1 a2 a3 | 0 1 2
443 // b1 | 3
444 // c1 c2 c3 c4 c5 | 4 5 6 7 8
445 // d1 d2 d3 d4 | 9 10 11 12
446 // e1 e2 e3 e4 e5 | 13 14 15 16 17
447 // e6 e7 | 18 19
448 testList
449 << qMakePair(KeyPress(nextItemKey), ViewState(1, KItemSet() << 1))
450 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(3, KItemSet() << 1 << 2 << 3))
451 << qMakePair(KeyPress(nextRowKey, Qt::ShiftModifier), ViewState(5, KItemSet() << 1 << 2 << 3 << 4 << 5))
452 << qMakePair(KeyPress(nextItemKey), ViewState(6, KItemSet() << 6))
453 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(7, KItemSet() << 6))
454 << qMakePair(KeyPress(nextItemKey, Qt::ControlModifier), ViewState(8, KItemSet() << 6))
455 << qMakePair(KeyPress(nextRowKey), ViewState(12, KItemSet() << 12))
456 << qMakePair(KeyPress(nextRowKey), ViewState(17, KItemSet() << 17))
457 << qMakePair(KeyPress(nextRowKey), ViewState(19, KItemSet() << 19))
458 << qMakePair(KeyPress(previousRowKey), ViewState(17, KItemSet() << 17))
459 << qMakePair(KeyPress(Qt::Key_End, Qt::ShiftModifier), ViewState(19, KItemSet() << 17 << 18 << 19))
460 << qMakePair(KeyPress(previousRowKey, Qt::ShiftModifier), ViewState(14, KItemSet() << 14 << 15 << 16 << 17))
461 << qMakePair(KeyPress(Qt::Key_Home), ViewState(0, KItemSet() << 0));
462 }
463
464 const QString testName =
465 layoutNames[layout] + ", " +
466 QString("%1 columns, ").arg(columnCount) +
467 selectionBehaviorNames[selectionBehavior] + ", " +
468 groupingEnabledNames[groupingEnabled];
469
470 const QByteArray testNameAscii = testName.toLatin1();
471
472 QTest::newRow(testNameAscii.data())
473 << layout
474 << scrollOrientation
475 << columnCount
476 << selectionBehavior
477 << groupingEnabled
478 << testList;
479 }
480 }
481 }
482 }
483 }
484
485 /**
486 * This function sets the view's properties according to the data provided by
487 * KItemListControllerTest::testKeyboardNavigation_data().
488 *
489 * The list \a testList contains pairs of key presses, which are sent to the
490 * container, and expected view states, which are verified then.
491 */
492 void KItemListControllerTest::testKeyboardNavigation()
493 {
494 QFETCH(KFileItemListView::ItemLayout, layout);
495 QFETCH(Qt::Orientation, scrollOrientation);
496 QFETCH(int, columnCount);
497 QFETCH(KItemListController::SelectionBehavior, selectionBehavior);
498 QFETCH(bool, groupingEnabled);
499 QFETCH(QList<keyPressViewStatePair>, testList);
500
501 m_view->setItemLayout(layout);
502 QCOMPARE(m_view->itemLayout(), layout);
503
504 m_view->setScrollOrientation(scrollOrientation);
505 QCOMPARE(m_view->scrollOrientation(), scrollOrientation);
506
507 m_controller->setSelectionBehavior(selectionBehavior);
508 QCOMPARE(m_controller->selectionBehavior(), selectionBehavior);
509
510 m_model->setGroupedSorting(groupingEnabled);
511 QCOMPARE(m_model->groupedSorting(), groupingEnabled);
512
513 adjustGeometryForColumnCount(columnCount);
514 QCOMPARE(m_view->m_layouter->m_columnCount, columnCount);
515
516 QSignalSpy spySingleItemActivated(m_controller, &KItemListController::itemActivated);
517 QSignalSpy spyMultipleItemsActivated(m_controller, &KItemListController::itemsActivated);
518
519 while (!testList.isEmpty()) {
520 const QPair<KeyPress, ViewState> test = testList.takeFirst();
521 const Qt::Key key = test.first.m_key;
522 const Qt::KeyboardModifiers modifier = test.first.m_modifier;
523 const int current = test.second.m_current;
524 const KItemSet selection = test.second.m_selection;
525 const bool activated = test.second.m_activated;
526
527 QTest::keyClick(m_container, key, modifier);
528
529 QCOMPARE(m_selectionManager->currentItem(), current);
530 switch (selectionBehavior) {
531 case KItemListController::NoSelection: QVERIFY(m_selectionManager->selectedItems().isEmpty()); break;
532 case KItemListController::SingleSelection: QCOMPARE(m_selectionManager->selectedItems(), KItemSet() << current); break;
533 case KItemListController::MultiSelection: QCOMPARE(m_selectionManager->selectedItems(), selection); break;
534 }
535
536 if (activated) {
537 switch (selectionBehavior) {
538 case KItemListController::MultiSelection:
539 if (!selection.isEmpty()) {
540 // The selected items should be activated.
541 if (selection.count() == 1) {
542 QVERIFY(!spySingleItemActivated.isEmpty());
543 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), selection.first());
544 QVERIFY(spyMultipleItemsActivated.isEmpty());
545 } else {
546 QVERIFY(spySingleItemActivated.isEmpty());
547 QVERIFY(!spyMultipleItemsActivated.isEmpty());
548 QCOMPARE(qvariant_cast<KItemSet>(spyMultipleItemsActivated.takeFirst().at(0)), selection);
549 }
550 break;
551 }
552 // No items are selected. Therefore, the current item should be activated.
553 // This is handled by falling through to the NoSelection/SingleSelection case.
554 Q_FALLTHROUGH();
555 case KItemListController::NoSelection:
556 case KItemListController::SingleSelection:
557 // In NoSelection and SingleSelection mode, the current item should be activated.
558 QVERIFY(!spySingleItemActivated.isEmpty());
559 QCOMPARE(qvariant_cast<int>(spySingleItemActivated.takeFirst().at(0)), current);
560 QVERIFY(spyMultipleItemsActivated.isEmpty());
561 break;
562 }
563 }
564 }
565 }
566
567 void KItemListControllerTest::testMouseClickActivation()
568 {
569 m_view->setItemLayout(KFileItemListView::IconsLayout);
570
571 // Make sure that we have a large window, such that
572 // the items are visible and clickable.
573 adjustGeometryForColumnCount(5);
574
575 // Make sure that the first item is visible in the view.
576 m_view->setScrollOffset(0);
577 QCOMPARE(m_view->firstVisibleIndex(), 0);
578
579 const QPointF pos = m_view->itemContextRect(0).center();
580
581 // Save the "single click" setting.
582 const bool restoreSettingsSingleClick = m_testStyle->activateItemOnSingleClick();
583
584 QGraphicsSceneMouseEvent mousePressEvent(QEvent::GraphicsSceneMousePress);
585 mousePressEvent.setPos(pos);
586 mousePressEvent.setButton(Qt::LeftButton);
587 mousePressEvent.setButtons(Qt::LeftButton);
588
589 QGraphicsSceneMouseEvent mouseReleaseEvent(QEvent::GraphicsSceneMouseRelease);
590 mouseReleaseEvent.setPos(pos);
591 mouseReleaseEvent.setButton(Qt::LeftButton);
592 mouseReleaseEvent.setButtons(Qt::NoButton);
593
594 QSignalSpy spyItemActivated(m_controller, &KItemListController::itemActivated);
595
596 // Default setting: single click activation.
597 m_testStyle->setActivateItemOnSingleClick(true);
598 m_view->event(&mousePressEvent);
599 m_view->event(&mouseReleaseEvent);
600 QCOMPARE(spyItemActivated.count(), 1);
601 spyItemActivated.clear();
602
603 // Set the global setting to "double click activation".
604 m_testStyle->setActivateItemOnSingleClick(false);
605 m_view->event(&mousePressEvent);
606 m_view->event(&mouseReleaseEvent);
607 QCOMPARE(spyItemActivated.count(), 0);
608 spyItemActivated.clear();
609
610 // Enforce single click activation in the controller.
611 m_controller->setSingleClickActivationEnforced(true);
612 m_view->event(&mousePressEvent);
613 m_view->event(&mouseReleaseEvent);
614 QCOMPARE(spyItemActivated.count(), 1);
615 spyItemActivated.clear();
616
617 // Do not enforce single click activation in the controller.
618 m_controller->setSingleClickActivationEnforced(false);
619 m_view->event(&mousePressEvent);
620 m_view->event(&mouseReleaseEvent);
621 QCOMPARE(spyItemActivated.count(), 0);
622 spyItemActivated.clear();
623
624 // Set the global setting back to "single click activation".
625 m_testStyle->setActivateItemOnSingleClick(true);
626 m_view->event(&mousePressEvent);
627 m_view->event(&mouseReleaseEvent);
628 QCOMPARE(spyItemActivated.count(), 1);
629 spyItemActivated.clear();
630
631 // Enforce single click activation in the controller.
632 m_controller->setSingleClickActivationEnforced(true);
633 m_view->event(&mousePressEvent);
634 m_view->event(&mouseReleaseEvent);
635 QCOMPARE(spyItemActivated.count(), 1);
636 spyItemActivated.clear();
637
638 // Restore previous settings.
639 m_controller->setSingleClickActivationEnforced(true);
640 m_testStyle->setActivateItemOnSingleClick(restoreSettingsSingleClick);
641 }
642
643 void KItemListControllerTest::adjustGeometryForColumnCount(int count)
644 {
645 const QSize size = m_view->itemSize().toSize();
646
647 QRect rect = m_container->geometry();
648 rect.setSize(size * count);
649 m_container->setGeometry(rect);
650
651 // Increase the size of the container until the correct column count is reached.
652 while (m_view->m_layouter->m_columnCount < count) {
653 rect = m_container->geometry();
654 rect.setSize(rect.size() + size);
655 m_container->setGeometry(rect);
656 }
657 }
658
659 QTEST_MAIN(KItemListControllerTest)
660
661 #include "kitemlistcontrollertest.moc"