]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
cac4c710249ae2a677f0be506c0a553d80a36070
[dolphin.git] / src / tests / dolphinviewtest_allviewmodes.cpp
1 /****************************************************************************
2 * Copyright (C) 2010-2011 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 <kdebug.h>
21
22 #include "dolphinviewtest_allviewmodes.h"
23
24 #include <qtest_kde.h>
25
26 #include "testdir.h"
27
28 #include "views/dolphinview.h"
29 #include "views/dolphinmodel.h"
30 #include "views/dolphindirlister.h"
31 #include "views/dolphinsortfilterproxymodel.h"
32 #include "views/zoomlevelinfo.h"
33
34 #include <QScrollBar>
35
36 #include <qtestmouse.h>
37 #include <qtestkeyboard.h>
38
39 DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
40 // Need to register KFileItemList for use with QSignalSpy
41 qRegisterMetaType<KFileItemList>("KFileItemList");
42 }
43
44 /**
45 * testSelection() checks the basic selection functionality of DolphinView, including:
46 *
47 * selectedItems()
48 * selectedItemsCount()
49 * selectAll()
50 * invertSelection()
51 * clearSelection()
52 * hasSelection()
53 *
54 * and the signal selectionChanged(const KFileItemList& selection)
55 */
56
57 Q_DECLARE_METATYPE(KFileItemList)
58
59 void DolphinViewTest_AllViewModes::testSelection() {
60 TestDir dir;
61 const int totalItems = 50;
62 for (int i = 0; i < totalItems; i++) {
63 dir.createFile(QString("%1").arg(i));
64 }
65
66 DolphinView view(dir.url(), 0);
67 QAbstractItemView* itemView = initView(&view);
68
69 // Start with an empty selection
70 view.clearSelection();
71
72 QCOMPARE(view.selectedItems().count(), 0);
73 QCOMPARE(view.selectedItemsCount(), 0);
74 QVERIFY(!view.hasSelection());
75
76 // First some simple tests where either all or no items are selected
77 view.selectAll();
78 verifySelectedItemsCount(&view, totalItems);
79
80 view.invertSelection();
81 verifySelectedItemsCount(&view, 0);
82
83 view.invertSelection();
84 verifySelectedItemsCount(&view, totalItems);
85
86 view.clearSelection();
87 verifySelectedItemsCount(&view, 0);
88
89 // Now we select individual items using mouse clicks
90 QModelIndex index = itemView->model()->index(2, 0);
91 itemView->scrollTo(index);
92 QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView->visualRect(index).center());
93 verifySelectedItemsCount(&view, 1);
94
95 index = itemView->model()->index(totalItems - 5, 0);
96 itemView->scrollTo(index);
97 QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView->visualRect(index).center());
98 verifySelectedItemsCount(&view, 2);
99
100 index = itemView->model()->index(totalItems - 2, 0);
101 itemView->scrollTo(index);
102 QTest::mouseClick(itemView->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView->visualRect(index).center());
103 verifySelectedItemsCount(&view, 5);
104
105 view.invertSelection();
106 verifySelectedItemsCount(&view, totalItems - 5);
107
108 // Pressing Esc should clear the selection
109 QTest::keyClick(itemView->viewport(), Qt::Key_Escape);
110 verifySelectedItemsCount(&view, 0);
111 }
112
113 /**
114 * Check that setting the directory view properties works.
115 */
116
117 void DolphinViewTest_AllViewModes::testViewPropertySettings()
118 {
119 // Create some files with different sizes and modification times to check the different sorting options
120 QDateTime now = QDateTime::currentDateTime();
121
122 TestDir dir;
123 dir.createFile("a", "A file", now.addDays(-3));
124 dir.createFile("b", "A larger file", now.addDays(0));
125 dir.createDir("c", now.addDays(-2));
126 dir.createFile("d", "The largest file in this directory", now.addDays(-1));
127 dir.createFile("e", "An even larger file", now.addDays(-4));
128 dir.createFile(".f");
129
130 DolphinView view(dir.url(), 0);
131 initView(&view);
132
133 // First set all settings to the default.
134 view.setSorting(DolphinView::SortByName);
135 QCOMPARE(view.sorting(), DolphinView::SortByName);
136
137 view.setSortOrder(Qt::AscendingOrder);
138 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
139
140 view.setSortFoldersFirst(true);
141 QVERIFY(view.sortFoldersFirst());
142
143 view.setShowPreview(false);
144 QVERIFY(!view.showPreview());
145
146 if (view.showHiddenFiles()) {
147 // Changing the "hidden files" setting triggers the dir lister
148 // -> we have to wait until loading the hidden files is finished
149 view.setShowHiddenFiles(false);
150 waitForFinishedPathLoading(&view);
151 }
152 QVERIFY(!view.showHiddenFiles());
153
154 /** Check that the sort order is correct for different kinds of settings */
155
156 // Sort by Name, ascending
157 QCOMPARE(view.sorting(), DolphinView::SortByName);
158 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
159 QCOMPARE(viewItems(&view), QStringList() << "c" << "a" << "b" << "d" << "e");
160
161 // Sort by Name, descending
162 view.setSortOrder(Qt::DescendingOrder);
163 QCOMPARE(view.sorting(), DolphinView::SortByName);
164 QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
165 QCOMPARE(viewItems(&view), QStringList() << "c" << "e" << "d" << "b" << "a");
166
167 // Sort by Size, descending
168 view.setSorting(DolphinView::SortBySize);
169 QCOMPARE(view.sorting(), DolphinView::SortBySize);
170 QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
171 QCOMPARE(viewItems(&view), QStringList() << "c" << "d" << "e" << "b" << "a");
172
173 // Sort by Size, ascending
174 view.setSortOrder(Qt::AscendingOrder);
175 QCOMPARE(view.sorting(), DolphinView::SortBySize);
176 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
177 QCOMPARE(viewItems(&view), QStringList() << "c" << "a" << "b" << "e" << "d");
178
179 // Sort by Date, ascending
180 view.setSorting(DolphinView::SortByDate);
181 QCOMPARE(view.sorting(), DolphinView::SortByDate);
182 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
183 QCOMPARE(viewItems(&view), QStringList() << "c" << "e" << "a" << "d" << "b");
184
185 // Sort by Date, descending
186 view.setSortOrder(Qt::DescendingOrder);
187 QCOMPARE(view.sorting(), DolphinView::SortByDate);
188 QCOMPARE(view.sortOrder(), Qt::DescendingOrder);
189 QCOMPARE(viewItems(&view), QStringList() << "c" << "b" << "d" << "a" << "e");
190
191 // Disable "Sort Folders First"
192 view.setSortFoldersFirst(false);
193 QVERIFY(!view.sortFoldersFirst());
194 QCOMPARE(viewItems(&view), QStringList()<< "b" << "d" << "c" << "a" << "e");
195
196 // Try again with Sort by Name, ascending
197 view.setSorting(DolphinView::SortByName);
198 view.setSortOrder(Qt::AscendingOrder);
199 QCOMPARE(view.sorting(), DolphinView::SortByName);
200 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
201 QCOMPARE(viewItems(&view), QStringList() << "a" << "b" << "c" << "d" << "e");
202
203 // Show hidden files. This triggers the dir lister
204 // -> we have to wait until loading the hidden files is finished
205 view.setShowHiddenFiles(true);
206 waitForFinishedPathLoading(&view);
207 QVERIFY(view.showHiddenFiles());
208
209 // Depending on the settings, a .directory file might have been created.
210 // Remove it from the list to get consistent results.
211 QStringList result = viewItems(&view);
212 result.removeAll(".directory");
213 QCOMPARE(result, QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
214
215 // Previews
216 view.setShowPreview(true);
217 QVERIFY(view.showPreview());
218
219 // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
220 }
221
222 /**
223 * testZoomLevel() checks that setting the zoom level works, both using DolphinView's API and using Ctrl+mouse wheel.
224 */
225
226 void DolphinViewTest_AllViewModes::testZoomLevel()
227 {
228 TestDir dir;
229 dir.createFiles(QStringList() << "a" << "b");
230 DolphinView view(dir.url(), 0);
231 QAbstractItemView* itemView = initView(&view);
232
233 view.setShowPreview(false);
234 QVERIFY(!view.showPreview());
235
236 int zoomLevelBackup = view.zoomLevel();
237
238 int zoomLevel = ZoomLevelInfo::minimumLevel();
239 view.setZoomLevel(zoomLevel);
240 QCOMPARE(view.zoomLevel(), zoomLevel);
241
242 // Increase the zoom level successively to the maximum.
243 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
244 zoomLevel++;
245 view.setZoomLevel(zoomLevel);
246 QCOMPARE(view.zoomLevel(), zoomLevel);
247 }
248
249 // Try setting a zoom level larger than the maximum
250 view.setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
251 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
252
253 // Turn previews on and try setting a zoom level smaller than the minimum
254 view.setShowPreview(true);
255 QVERIFY(view.showPreview());
256 view.setZoomLevel(ZoomLevelInfo::minimumLevel() - 1);
257 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
258
259 // Turn previews off again and check that the zoom level is restored
260 view.setShowPreview(false);
261 QVERIFY(!view.showPreview());
262 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
263
264 // Change the zoom level using Ctrl+mouse wheel
265 QModelIndex index = itemView->model()->index(0, 0);
266 itemView->scrollTo(index);
267
268 while (view.zoomLevel() > ZoomLevelInfo::minimumLevel()) {
269 int oldZoomLevel = view.zoomLevel();
270 QWheelEvent wheelEvent(itemView->visualRect(index).center(), -1, Qt::NoButton, Qt::ControlModifier);
271 bool wheelEventReceived = qApp->notify(itemView->viewport(), &wheelEvent);
272 QVERIFY(wheelEventReceived);
273 QVERIFY(view.zoomLevel() < oldZoomLevel);
274 }
275 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
276
277 while (view.zoomLevel() < ZoomLevelInfo::maximumLevel()) {
278 int oldZoomLevel = view.zoomLevel();
279 QWheelEvent wheelEvent(itemView->visualRect(index).center(), 1, Qt::NoButton, Qt::ControlModifier);
280 bool wheelEventReceived = qApp->notify(itemView->viewport(), &wheelEvent);
281 QVERIFY(wheelEventReceived);
282 QVERIFY(view.zoomLevel() > oldZoomLevel);
283 }
284 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::maximumLevel());
285
286 // Turn previews on again and check that the zoom level is restored
287 view.setShowPreview(true);
288 QVERIFY(view.showPreview());
289 QCOMPARE(view.zoomLevel(), ZoomLevelInfo::minimumLevel());
290
291 // Restore the initial state
292 view.setZoomLevel(zoomLevelBackup);
293 view.setShowPreview(false);
294 view.setZoomLevel(zoomLevelBackup);
295 }
296
297 /**
298 * testSaveAndRestoreState() checks if saving and restoring the view state (current item, scroll position).
299 *
300 * Note that we call qApp->sendPostedEvents() every time the view's finishedPathLoading(const KUrl&) signal
301 * is received. The reason is that the scroll position is restored in the slot restoreContentsPosition(),
302 * which is been invoked using a queued connection in DolphinView::slotLoadingCompleted(). To make sure
303 * that this slot is really executed before we proceed, we have to empty the event queue using qApp->sendPostedEvents().
304 */
305
306 void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
307 {
308 const int totalItems = 50;
309 TestDir dir;
310 for (int i = 0; i < totalItems; i++) {
311 dir.createFile(QString("%1").arg(i));
312 }
313 dir.createDir("51");
314 DolphinView view(dir.url(), 0);
315 initView(&view);
316
317 // Set sorting settings to the default to make sure that the item positions are reproducible.
318 view.setSorting(DolphinView::SortByName);
319 QCOMPARE(view.sorting(), DolphinView::SortByName);
320 view.setSortOrder(Qt::AscendingOrder);
321 QCOMPARE(view.sortOrder(), Qt::AscendingOrder);
322
323 // Make sure that previews are off and that the icon size does not depend on the preview setting.
324 // This is needed for the test for bug 270437, see below.
325 view.setShowPreview(false);
326 int zoomLevel = view.zoomLevel();
327 view.setShowPreview(true);
328 view.setZoomLevel(zoomLevel);
329 view.setShowPreview(false);
330
331 // Select item 45
332 const QModelIndex index45 = itemView(&view)->model()->index(45, 0);
333 itemView(&view)->scrollTo(index45);
334 itemView(&view)->setCurrentIndex(index45);
335 const int scrollPosX = itemView(&view)->horizontalScrollBar()->value();
336 const int scrollPosY = itemView(&view)->verticalScrollBar()->value();
337
338 // Save the view state
339 QByteArray viewState;
340 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
341 view.saveState(saveStream);
342
343 // Change the URL
344 view.setUrl(KUrl(dir.name() + "51"));
345 waitForFinishedPathLoading(&view);
346 qApp->sendPostedEvents();
347
348 // Go back, but do not call DolphinView::restoreState()
349 view.setUrl(dir.url());
350 waitForFinishedPathLoading(&view);
351 qApp->sendPostedEvents();
352
353 // Verify that the view is scrolled to top-left corner and that item 45 is not the current item.
354 // Note that the vertical position of the columns view might not be zero -> skip that part
355 // of the check in this case.
356 QVERIFY(itemView(&view)->currentIndex() != index45);
357 QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), 0);
358 if (mode() != DolphinView::ColumnView) {
359 QCOMPARE(itemView(&view)->verticalScrollBar()->value(), 0);
360 }
361
362 // Change the URL again
363 view.setUrl(KUrl(dir.name() + "51"));
364 waitForFinishedPathLoading(&view);
365 qApp->sendPostedEvents();
366
367 // Check that the current item and scroll position are correct if DolphinView::restoreState()
368 // is called after the URL change
369 view.setUrl(dir.url());
370 QDataStream restoreStream(viewState);
371 view.restoreState(restoreStream);
372 waitForFinishedPathLoading(&view);
373 qApp->sendPostedEvents();
374
375 QCOMPARE(itemView(&view)->currentIndex(), index45);
376 QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
377 QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
378
379 /**
380 * Additionally, we verify the fix for the bug https://bugs.kde.org/show_bug.cgi?id=270437
381 * Actually, it's a bug in KFilePreviewGenerator, but it is easier to test it here.
382 */
383
384 // Turn previews on.
385 view.setShowPreview(true);
386 QVERIFY(view.showPreview());
387
388 // We have to process all events in the queue to make sure that previews are really on.
389 qApp->sendPostedEvents();
390
391 // Current item and scroll position should not change.
392 QCOMPARE(itemView(&view)->currentIndex(), index45);
393 QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
394 QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
395
396 // Turn previews off again. Before bug 270437, this triggered the dir lister's openUrl() method
397 // -> we check that by listening to the view's startedPathLoading() signal and wait until the loading is finished in that case.
398 QSignalSpy spy(&view, SIGNAL(startedPathLoading(const KUrl&)));
399 view.setShowPreview(false);
400 QVERIFY(!view.showPreview());
401 qApp->sendPostedEvents();
402 if (!spy.isEmpty()) {
403 // The dir lister reloads the directory. We wait until the loading is finished.
404 waitForFinishedPathLoading(&view);
405 }
406
407 // Current item and scroll position should not change.
408 QCOMPARE(itemView(&view)->currentIndex(), index45);
409 QCOMPARE(itemView(&view)->horizontalScrollBar()->value(), scrollPosX);
410 QCOMPARE(itemView(&view)->verticalScrollBar()->value(), scrollPosY);
411 }
412
413 /**
414 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
415 *
416 * A view may never grab the keyboard focus itself and must respect the focus-state
417 * when switching the view mode, see
418 *
419 * https://bugs.kde.org/show_bug.cgi?id=261147
420 */
421
422 void DolphinViewTest_AllViewModes::testKeyboardFocus()
423 {
424 TestDir dir;
425 dir.createFiles(QStringList() << "a" << "b");
426 DolphinView view(dir.url(), 0);
427 initView(&view);
428
429 // Move the keyboard focus to another widget.
430 QWidget widget;
431 widget.show();
432 QTest::qWaitForWindowShown(&widget);
433 widget.setFocus();
434
435 QVERIFY(!view.hasFocus());
436
437 // Switch view modes and verify that the view does not get the focus back
438 for (int i = 0; i <= DolphinView::MaxModeEnum; ++i) {
439 view.setMode(static_cast<DolphinView::Mode>(i));
440 QVERIFY(!view.hasFocus());
441 }
442 }
443
444 /**
445 * testCutCopyPaste() checks if cutting or copying items in one view and pasting
446 * them in another one works.
447 */
448
449 void DolphinViewTest_AllViewModes::testCutCopyPaste()
450 {
451 TestDir dir1;
452 dir1.createFiles(QStringList() << "a" << "b" << "c" << "d");
453 DolphinView view1(dir1.url(), 0);
454 QAbstractItemView* itemView1 = initView(&view1);
455
456 TestDir dir2;
457 dir2.createFiles(QStringList() << "1" << "2" << "3" << "4");
458 dir2.createDir("subfolder");
459 DolphinView view2(dir2.url(), 0);
460 QAbstractItemView* itemView2 = initView(&view2);
461
462 // Make sure that both views are sorted by name in ascending order
463 // TODO: Maybe that should be done in initView(), such all tests can rely on it...?
464 view1.setSorting(DolphinView::SortByName);
465 view1.setSortOrder(Qt::AscendingOrder);
466 view2.setSorting(DolphinView::SortByName);
467 view2.setSortOrder(Qt::AscendingOrder);
468 view2.setSortFoldersFirst(true);
469
470 QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
471 QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4");
472
473 /** Copy and paste */
474 // Select an item ("d") n view1, copy it and paste it in view2.
475 // Note that we have to wait for view2's finishedPathLoading() signal because the pasting is done in the background.
476 QModelIndex index = itemView1->model()->index(3, 0);
477 itemView1->scrollTo(index);
478 QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
479 verifySelectedItemsCount(&view1, 1);
480 QCOMPARE(selectedItems(&view1), QStringList() << "d");
481 view1.copySelectedItems();
482 view2.paste();
483 waitForFinishedPathLoading(&view2);
484 QCOMPARE(viewItems(&view1), QStringList() << "a" << "b" << "c" << "d");
485 QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "3" << "4" << "d");
486 // The pasted item should be selected
487 QCOMPARE(selectedItems(&view2), QStringList() << "d");
488
489 /** Cut and paste */
490 // Select two items ("3", "4") in view2, cut and paste in view1.
491 view2.clearSelection();
492 index = itemView2->model()->index(3, 0);
493 itemView2->scrollTo(index);
494 QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
495 verifySelectedItemsCount(&view2, 1);
496 index = itemView2->model()->index(4, 0);
497 itemView2->scrollTo(index);
498 QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ShiftModifier, itemView2->visualRect(index).center());
499 verifySelectedItemsCount(&view2, 2);
500 QCOMPARE(selectedItems(&view2), QStringList() << "3" << "4");
501 view2.cutSelectedItems();
502 // In view1, "d" is still selected
503 QCOMPARE(selectedItems(&view1), QStringList() << "d");
504 // Paste "3" and "4"
505 view1.paste();
506 waitForFinishedPathLoading(&view1);
507 // In principle, KIO could implement copy&paste such that the pasted items are already there, but the cut items
508 // have not been removed yet. Therefore, we check the number of items in view2 and also wait for that view's
509 // finishedPathLoading() signal if the cut items are still there.
510 if (viewItems(&view2).count() > 4) {
511 waitForFinishedPathLoading(&view2);
512 }
513 QCOMPARE(viewItems(&view1), QStringList() << "3" << "4" << "a" << "b" << "c" << "d");
514 QCOMPARE(viewItems(&view2), QStringList() << "subfolder" << "1" << "2" << "d");
515 // The pasted items ("3", "4") should be selected now, and the previous selection ("d") should be cleared.
516 QCOMPARE(selectedItems(&view1), QStringList() << "3" << "4");
517
518 /** Copy and paste into subfolder */
519 view1.clearSelection();
520 index = itemView1->model()->index(3, 0);
521 itemView1->scrollTo(index);
522 QTest::mouseClick(itemView1->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView1->visualRect(index).center());
523 verifySelectedItemsCount(&view1, 1);
524 QCOMPARE(selectedItems(&view1), QStringList() << "b");
525 view1.copySelectedItems();
526 // Now we use view1 to display the subfolder, which is still empty.
527 view1.setUrl(KUrl(dir2.name() + "subfolder"));
528 waitForFinishedPathLoading(&view1);
529 QCOMPARE(viewItems(&view1), QStringList());
530 // Select the subfolder.in view2
531 view2.clearSelection();
532 index = itemView2->model()->index(0, 0);
533 itemView2->scrollTo(index);
534 QTest::mouseClick(itemView2->viewport(), Qt::LeftButton, Qt::ControlModifier, itemView2->visualRect(index).center());
535 verifySelectedItemsCount(&view2, 1);
536 // Paste into the subfolder
537 view2.pasteIntoFolder();
538 waitForFinishedPathLoading(&view1);
539 QCOMPARE(viewItems(&view1), QStringList() << "b");
540 // The pasted items in view1 are *not* selected now (because the pasting was done indirectly using view2.pasteIntoFolder()).
541 }
542
543 // Private member functions which are used by the tests
544
545 /**
546 * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits until loading the
547 * folder in the view is finished.
548 *
549 * Many unit tests need access to DolphinView's internal item view (icons, details, or columns).
550 * Therefore, a pointer to the item view is returned by initView(DolphinView*).
551 */
552
553 QAbstractItemView* DolphinViewTest_AllViewModes::initView(DolphinView* view) const
554 {
555 QSignalSpy spyFinishedPathLoading(view, SIGNAL(finishedPathLoading(const KUrl&)));
556 view->setMode(mode());
557 Q_ASSERT(verifyCorrectViewMode(view));
558 view->resize(200, 300);
559 view->show();
560 QTest::qWaitForWindowShown(view);
561
562 // If the DolphinView's finishedPathLoading(const KUrl&) signal has not been received yet,
563 // we have to wait a bit more.
564 // The reason why the if-statement is needed here is that the signal might have been emitted
565 // while we were waiting in QTest::qWaitForWindowShown(view)
566 // -> waitForFinishedPathLoading(view) would fail in that case.
567 if (spyFinishedPathLoading.isEmpty()) {
568 waitForFinishedPathLoading(view);
569 }
570
571 return itemView(view);
572 }
573
574 /**
575 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
576 * signal is received and checks that the selection state of the view is as expected.
577 */
578
579 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(DolphinView* view, int itemsCount) const
580 {
581 QSignalSpy spySelectionChanged(view, SIGNAL(selectionChanged(const KFileItemList&)));
582 QVERIFY(QTest::kWaitForSignal(view, SIGNAL(selectionChanged(const KFileItemList&)), 2000));
583
584 QCOMPARE(view->selectedItems().count(), itemsCount);
585 QCOMPARE(view->selectedItemsCount(), itemsCount);
586 QCOMPARE(spySelectionChanged.count(), 1);
587 QCOMPARE(qvariant_cast<KFileItemList>(spySelectionChanged.at(0).at(0)).count(), itemsCount);
588 if (itemsCount) {
589 QVERIFY(view->hasSelection());
590 }
591 else {
592 QVERIFY(!view->hasSelection());
593 }
594 }
595
596 #include "dolphinviewtest_allviewmodes.moc"