]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
1 /*****************************************************************************
2 * Copyright (C) 2010-2011 by Frank Reininghaus (frank78ac@googlemail.com) *
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. *
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. *
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 *****************************************************************************/
20 #include "dolphinviewtest_allviewmodes.h"
22 #include <qtest_kde.h>
26 #include "views/dolphinview.h"
27 #include "views/dolphinmodel.h"
28 #include "views/dolphindirlister.h"
29 #include "views/dolphinsortfilterproxymodel.h"
30 #include "views/zoomlevelinfo.h"
32 #include <QtGui/QScrollBar>
34 #include <qtestmouse.h>
35 #include <qtestkeyboard.h>
37 DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
38 // Need to register KFileItemList for use with QSignalSpy
39 qRegisterMetaType
<KFileItemList
>("KFileItemList");
42 void DolphinViewTest_AllViewModes::init() {
43 if (mode() == DolphinView::ColumnView
) {
44 // In Columns View mode, we need to create a new DolphinView after each
45 // test to make the file-related tests after the first one pass.
46 // TODO: Try to find out if there is a hidden bug in the Columns View that causes this.
48 m_view
= new DolphinView(KUrl(m_path
), 0);
50 m_view
->setMode(mode());
51 QVERIFY(verifyCorrectViewMode());
52 m_view
->resize(200, 300);
54 QTest::qWaitForWindowShown(m_view
);
57 void DolphinViewTest_AllViewModes::cleanup() {
63 * testSelection() checks the basic selection funtionality of DolphinView, including:
66 * selectedItemsCount()
72 * and the signal selectionChanged(const KFileItemList& selection)
75 Q_DECLARE_METATYPE(KFileItemList
)
77 void DolphinViewTest_AllViewModes::testSelection() {
78 const int totalItems
= 50;
80 for (int i
= 0; i
< totalItems
; i
++) {
81 createFile(QString("%1").arg(i
));
85 // Start with an empty selection
86 m_view
->clearSelection();
88 QCOMPARE(m_view
->selectedItems().count(), 0);
89 QCOMPARE(m_view
->selectedItemsCount(), 0);
90 QVERIFY(!m_view
->hasSelection());
92 // First some simple tests where either all or no items are selected
94 verifySelectedItemsCount(totalItems
);
96 m_view
->invertSelection();
97 verifySelectedItemsCount(0);
99 m_view
->invertSelection();
100 verifySelectedItemsCount(totalItems
);
102 m_view
->clearSelection();
103 verifySelectedItemsCount(0);
105 // Now we select individual items using mouse clicks
106 QModelIndex index
= itemView()->model()->index(2, 0);
107 itemView()->scrollTo(index
);
108 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
109 verifySelectedItemsCount(1);
111 index
= itemView()->model()->index(totalItems
- 5, 0);
112 itemView()->scrollTo(index
);
113 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
114 verifySelectedItemsCount(2);
116 index
= itemView()->model()->index(totalItems
- 2, 0);
117 itemView()->scrollTo(index
);
118 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ShiftModifier
, itemView()->visualRect(index
).center());
119 verifySelectedItemsCount(5);
121 m_view
->invertSelection();
122 verifySelectedItemsCount(totalItems
- 5);
124 // Pressing Esc should clear the selection
125 QTest::keyClick(itemView()->viewport(), Qt::Key_Escape
);
126 verifySelectedItemsCount(0);
130 * Check that setting the directory view properties works.
133 void DolphinViewTest_AllViewModes::testViewPropertySettings()
135 // Create some files with different sizes and modification times to check the different sorting options
136 QDateTime now
= QDateTime::currentDateTime();
138 createFile("a", "A file", now
.addDays(-3));
139 createFile("b", "A larger file", now
.addDays(0));
140 createDir("c", now
.addDays(-2));
141 createFile("d", "The largest file in this directory", now
.addDays(-1));
142 createFile("e", "An even larger file", now
.addDays(-4));
147 // First set all settings to the default.
148 m_view
->setSorting(DolphinView::SortByName
);
149 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
151 m_view
->setSortOrder(Qt::AscendingOrder
);
152 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
154 m_view
->setSortFoldersFirst(true);
155 QVERIFY(m_view
->sortFoldersFirst());
157 m_view
->setShowPreview(false);
158 QVERIFY(!m_view
->showPreview());
160 m_view
->setShowHiddenFiles(false);
161 QVERIFY(!m_view
->showHiddenFiles());
163 /** Check that the sort order is correct for different kinds of settings */
165 // Sort by Name, ascending
166 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
167 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
168 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "d" << "e");
170 // Sort by Name, descending
171 m_view
->setSortOrder(Qt::DescendingOrder
);
172 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
173 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
174 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "d" << "b" << "a");
176 // Sort by Size, descending
177 m_view
->setSorting(DolphinView::SortBySize
);
178 QCOMPARE(m_view
->sorting(), DolphinView::SortBySize
);
179 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
180 QCOMPARE(viewItems(), QStringList() << "c" << "d" << "e" << "b" << "a");
182 // Sort by Size, ascending
183 m_view
->setSortOrder(Qt::AscendingOrder
);
184 QCOMPARE(m_view
->sorting(), DolphinView::SortBySize
);
185 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
186 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "e" << "d");
188 // Sort by Date, ascending
189 m_view
->setSorting(DolphinView::SortByDate
);
190 QCOMPARE(m_view
->sorting(), DolphinView::SortByDate
);
191 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
192 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "a" << "d" << "b");
194 // Sort by Date, descending
195 m_view
->setSortOrder(Qt::DescendingOrder
);
196 QCOMPARE(m_view
->sorting(), DolphinView::SortByDate
);
197 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
198 QCOMPARE(viewItems(), QStringList() << "c" << "b" << "d" << "a" << "e");
200 // Disable "Sort Folders First"
201 m_view
->setSortFoldersFirst(false);
202 QVERIFY(!m_view
->sortFoldersFirst());
203 QCOMPARE(viewItems(), QStringList()<< "b" << "d" << "c" << "a" << "e");
205 // Try again with Sort by Name, ascending
206 m_view
->setSorting(DolphinView::SortByName
);
207 m_view
->setSortOrder(Qt::AscendingOrder
);
208 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
209 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
210 QCOMPARE(viewItems(), QStringList() << "a" << "b" << "c" << "d" << "e");
212 // Show hidden files. This triggers the dir lister
213 // -> we have to wait until loading the hidden files is finished
214 m_view
->setShowHiddenFiles(true);
215 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
216 QVERIFY(m_view
->showHiddenFiles());
217 QCOMPARE(viewItems(), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
220 m_view
->setShowPreview(true);
221 QVERIFY(m_view
->showPreview());
223 // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
227 * testZoomLevel() checks that setting the zoom level works, both using DolphinView's API and using Ctrl+mouse wheel.
230 void DolphinViewTest_AllViewModes::testZoomLevel()
232 createFiles(QStringList() << "a" << "b");
235 m_view
->setShowPreview(false);
236 QVERIFY(!m_view
->showPreview());
238 int zoomLevelBackup
= m_view
->zoomLevel();
240 int zoomLevel
= ZoomLevelInfo::minimumLevel();
241 m_view
->setZoomLevel(zoomLevel
);
242 QCOMPARE(m_view
->zoomLevel(), zoomLevel
);
244 // Increase the zoom level successively to the maximum.
245 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
247 m_view
->setZoomLevel(zoomLevel
);
248 QCOMPARE(m_view
->zoomLevel(), zoomLevel
);
251 // Try setting a zoom level larger than the maximum
252 m_view
->setZoomLevel(ZoomLevelInfo::maximumLevel() + 1);
253 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::maximumLevel());
255 // Turn previews on and try setting a zoom level smaller than the minimum
256 m_view
->setShowPreview(true);
257 QVERIFY(m_view
->showPreview());
258 m_view
->setZoomLevel(ZoomLevelInfo::minimumLevel() - 1);
259 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::minimumLevel());
261 // Turn previews off again and check that the zoom level is restored
262 m_view
->setShowPreview(false);
263 QVERIFY(!m_view
->showPreview());
264 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::maximumLevel());
266 // Change the zoom level using Ctrl+mouse wheel
267 QModelIndex index
= itemView()->model()->index(0, 0);
268 itemView()->scrollTo(index
);
270 while (m_view
->zoomLevel() > ZoomLevelInfo::minimumLevel()) {
271 int oldZoomLevel
= m_view
->zoomLevel();
272 QWheelEvent
wheelEvent(itemView()->visualRect(index
).center(), -1, Qt::NoButton
, Qt::ControlModifier
);
273 bool wheelEventReceived
= qApp
->notify(itemView()->viewport(), &wheelEvent
);
274 QVERIFY(wheelEventReceived
);
275 QVERIFY(m_view
->zoomLevel() < oldZoomLevel
);
277 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::minimumLevel());
279 while (m_view
->zoomLevel() < ZoomLevelInfo::maximumLevel()) {
280 int oldZoomLevel
= m_view
->zoomLevel();
281 QWheelEvent
wheelEvent(itemView()->visualRect(index
).center(), 1, Qt::NoButton
, Qt::ControlModifier
);
282 bool wheelEventReceived
= qApp
->notify(itemView()->viewport(), &wheelEvent
);
283 QVERIFY(wheelEventReceived
);
284 QVERIFY(m_view
->zoomLevel() > oldZoomLevel
);
286 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::maximumLevel());
288 // Turn previews on again and check that the zoom level is restored
289 m_view
->setShowPreview(true);
290 QVERIFY(m_view
->showPreview());
291 QCOMPARE(m_view
->zoomLevel(), ZoomLevelInfo::minimumLevel());
293 // Restore the initial state
294 m_view
->setZoomLevel(zoomLevelBackup
);
295 m_view
->setShowPreview(false);
296 m_view
->setZoomLevel(zoomLevelBackup
);
300 * testSaveAndRestoreState() checks if saving and restoring the view state (current item, scroll position).
302 * Note that we call qApp->sendPostedEvents() every time the view's finishedPathLoading(const KUrl&) signal
303 * is received. The reason is that the scroll position is restored in the slot restoreContentsPosition(),
304 * which is been invoked using a queued connection in DolphinView::slotLoadingCompleted(). To make sure
305 * that this slot is really executed before we proceed, we have to empty the event queue using qApp->sendPostedEvents().
308 void DolphinViewTest_AllViewModes::testSaveAndRestoreState()
310 const int totalItems
= 50;
312 for (int i
= 0; i
< totalItems
; i
++) {
313 createFile(QString("%1").arg(i
));
318 // Set sorting settings to the default to make sure that the item positions are reproducible.
319 m_view
->setSorting(DolphinView::SortByName
);
320 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
321 m_view
->setSortOrder(Qt::AscendingOrder
);
322 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
324 const QModelIndex index45
= itemView()->model()->index(45, 0);
325 itemView()->scrollTo(index45
);
326 itemView()->setCurrentIndex(index45
);
327 const int scrollPosX
= itemView()->horizontalScrollBar()->value();
328 const int scrollPosY
= itemView()->verticalScrollBar()->value();
330 // Save the view state
331 QByteArray viewState
;
332 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
333 m_view
->saveState(saveStream
);
335 // Change the URL and then go back
336 m_view
->setUrl(m_path
+ "/51");
337 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
338 qApp
->sendPostedEvents();
340 m_view
->setUrl(m_path
);
341 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
342 qApp
->sendPostedEvents();
344 // Verify that the view is scrolled to top and that item 45 is not the current item
345 QVERIFY(itemView()->currentIndex() != index45
);
346 QCOMPARE(itemView()->horizontalScrollBar()->value(), 0);
347 QCOMPARE(itemView()->verticalScrollBar()->value(), 0);
349 // Change the URL again
350 m_view
->setUrl(m_path
+ "/51");
351 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
352 qApp
->sendPostedEvents();
354 // Check that the current item and scroll position are correct if DolphinView::restoreState()
355 // is called after the URL change
356 m_view
->setUrl(m_path
);
357 QDataStream
restoreStream(viewState
);
358 m_view
->restoreState(restoreStream
);
359 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
360 qApp
->sendPostedEvents();
362 QCOMPARE(itemView()->currentIndex(), index45
);
363 QCOMPARE(itemView()->horizontalScrollBar()->value(), scrollPosX
);
364 QCOMPARE(itemView()->verticalScrollBar()->value(), scrollPosY
);
368 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
370 * A view may never grab the keyboard focus itself and must respect the focus-state
371 * when switching the view mode, see
373 * https://bugs.kde.org/show_bug.cgi?id=261147
376 void DolphinViewTest_AllViewModes::testKeyboardFocus()
378 const DolphinView::Mode mode
= m_view
->mode();
380 // Move keyboard focus to another widget. To see that this is needed, run only this test,
381 // i.e., pass 'testKeyboardFocus' as a parameter on the command line.
384 QTest::qWaitForWindowShown(&widget
);
387 QVERIFY(!m_view
->hasFocus());
389 // Switch view modes and verify that the view does not get the focus back
390 for (int i
= 0; i
<= DolphinView::MaxModeEnum
; ++i
) {
391 m_view
->setMode(static_cast<DolphinView::Mode
>(i
));
392 QVERIFY(!m_view
->hasFocus());
395 m_view
->setMode(mode
);
399 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
400 * signal is received and checks that the selection state of the view is as expected.
403 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount
) const
405 QSignalSpy
spySelectionChanged(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)));
406 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)), 500));
408 QCOMPARE(m_view
->selectedItems().count(), itemsCount
);
409 QCOMPARE(m_view
->selectedItemsCount(), itemsCount
);
410 QCOMPARE(spySelectionChanged
.count(), 1);
411 QCOMPARE(qvariant_cast
<KFileItemList
>(spySelectionChanged
.at(0).at(0)).count(), itemsCount
);
412 if (itemsCount
!= 0) {
413 QVERIFY(m_view
->hasSelection());
416 QVERIFY(!m_view
->hasSelection());
420 #include "dolphinviewtest_allviewmodes.moc"