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