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