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