]>
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"
31 #include <qtestmouse.h>
32 #include <qtestkeyboard.h>
34 DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
35 // Need to register KFileItemList for use with QSignalSpy
36 qRegisterMetaType
<KFileItemList
>("KFileItemList");
39 void DolphinViewTest_AllViewModes::init() {
40 m_view
->setMode(mode());
41 QVERIFY(verifyCorrectViewMode());
42 m_view
->resize(200, 300);
44 QTest::qWaitForWindowShown(m_view
);
47 void DolphinViewTest_AllViewModes::cleanup() {
53 * testSelection() checks the basic selection funtionality of DolphinView, including:
56 * selectedItemsCount()
62 * and the signal selectionChanged(const KFileItemList& selection)
65 Q_DECLARE_METATYPE(KFileItemList
)
67 void DolphinViewTest_AllViewModes::testSelection() {
68 const int totalItems
= 50;
70 for (int i
= 0; i
< totalItems
; i
++) {
71 createFile(QString("%1").arg(i
));
75 // Start with an empty selection
76 m_view
->clearSelection();
78 QCOMPARE(m_view
->selectedItems().count(), 0);
79 QCOMPARE(m_view
->selectedItemsCount(), 0);
80 QVERIFY(!m_view
->hasSelection());
82 // First some simple tests where either all or no items are selected
84 verifySelectedItemsCount(totalItems
);
86 m_view
->invertSelection();
87 verifySelectedItemsCount(0);
89 m_view
->invertSelection();
90 verifySelectedItemsCount(totalItems
);
92 m_view
->clearSelection();
93 verifySelectedItemsCount(0);
95 // Now we select individual items using mouse clicks
96 QModelIndex index
= itemView()->model()->index(2, 0);
97 itemView()->scrollTo(index
);
98 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
99 verifySelectedItemsCount(1);
101 index
= itemView()->model()->index(45, 0);
102 itemView()->scrollTo(index
);
103 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
104 verifySelectedItemsCount(2);
106 index
= itemView()->model()->index(48, 0);
107 itemView()->scrollTo(index
);
108 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ShiftModifier
, itemView()->visualRect(index
).center());
109 verifySelectedItemsCount(5);
113 * Check that setting the directory view properties works.
116 void DolphinViewTest_AllViewModes::testViewPropertySettings()
118 // Create some files with different sizes and modification times to check the different sorting options
119 QDateTime now
= QDateTime::currentDateTime();
121 createFile("a", "A file", now
.addDays(-3));
122 createFile("b", "A larger file", now
.addDays(0));
123 createDir("c", now
.addDays(-2));
124 createFile("d", "The largest file in this directory", now
.addDays(-1));
125 createFile("e", "An even larger file", now
.addDays(-4));
130 // First set all settings to the default.
131 m_view
->setSorting(DolphinView::SortByName
);
132 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
134 m_view
->setSortOrder(Qt::AscendingOrder
);
135 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
137 m_view
->setSortFoldersFirst(true);
138 QVERIFY(m_view
->sortFoldersFirst());
140 m_view
->setShowPreview(false);
141 QVERIFY(!m_view
->showPreview());
143 m_view
->setShowHiddenFiles(false);
144 QVERIFY(!m_view
->showHiddenFiles());
146 if (mode() == DolphinView::ColumnView
) {
147 // If the columns view is used, the view is empty before calling qApp->sendPostedEvents.
148 // It seems that some event needs to be processed to see the items in the view.
149 // TODO: Find out why this is needed for the columns view, but not for the other view modes.
150 qApp
->sendPostedEvents();
153 /** Check that the sort order is correct for different kinds of settings */
155 // Sort by Name, ascending
156 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
157 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
158 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "d" << "e");
160 // Sort by Name, descending
161 m_view
->setSortOrder(Qt::DescendingOrder
);
162 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
163 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
164 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "d" << "b" << "a");
166 // Sort by Size, descending
167 m_view
->setSorting(DolphinView::SortBySize
);
168 QCOMPARE(m_view
->sorting(), DolphinView::SortBySize
);
169 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
170 QCOMPARE(viewItems(), QStringList() << "c" << "d" << "e" << "b" << "a");
172 // Sort by Size, ascending
173 m_view
->setSortOrder(Qt::AscendingOrder
);
174 QCOMPARE(m_view
->sorting(), DolphinView::SortBySize
);
175 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
176 QCOMPARE(viewItems(), QStringList() << "c" << "a" << "b" << "e" << "d");
178 // Sort by Date, ascending
179 m_view
->setSorting(DolphinView::SortByDate
);
180 QCOMPARE(m_view
->sorting(), DolphinView::SortByDate
);
181 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
182 QCOMPARE(viewItems(), QStringList() << "c" << "e" << "a" << "d" << "b");
184 // Sort by Date, descending
185 m_view
->setSortOrder(Qt::DescendingOrder
);
186 QCOMPARE(m_view
->sorting(), DolphinView::SortByDate
);
187 QCOMPARE(m_view
->sortOrder(), Qt::DescendingOrder
);
188 QCOMPARE(viewItems(), QStringList() << "c" << "b" << "d" << "a" << "e");
190 // Disable "Sort Folders First"
191 m_view
->setSortFoldersFirst(false);
192 QVERIFY(!m_view
->sortFoldersFirst());
193 QCOMPARE(viewItems(), QStringList()<< "b" << "d" << "c" << "a" << "e");
195 // Try again with Sort by Name, ascending
196 m_view
->setSorting(DolphinView::SortByName
);
197 m_view
->setSortOrder(Qt::AscendingOrder
);
198 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
199 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
200 QCOMPARE(viewItems(), QStringList() << "a" << "b" << "c" << "d" << "e");
202 // Show hidden files. This triggers the dir lister
203 // -> we have to wait until loading the hidden files is finished
204 m_view
->setShowHiddenFiles(true);
205 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)), 2000));
206 QVERIFY(m_view
->showHiddenFiles());
207 QCOMPARE(viewItems(), QStringList() << ".f" << "a" << "b" << "c" << "d" << "e");
210 m_view
->setShowPreview(true);
211 QVERIFY(m_view
->showPreview());
213 // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
217 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
219 * A view may never grab the keyboard focus itself and must respect the focus-state
220 * when switching the view mode.
223 void DolphinViewTest_AllViewModes::testKeyboardFocus()
225 const DolphinView::Mode mode
= m_view
->mode();
227 QVERIFY(!m_view
->hasFocus());
228 for (int i
= 0; i
<= DolphinView::MaxModeEnum
; ++i
) {
229 m_view
->setMode(static_cast<DolphinView::Mode
>(i
));
230 QVERIFY(!m_view
->hasFocus());
233 m_view
->setMode(mode
);
237 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
238 * signal is received and checks that the selection state of the view is as expected.
241 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount
) const
243 QSignalSpy
spySelectionChanged(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)));
244 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)), 500));
246 QCOMPARE(m_view
->selectedItems().count(), itemsCount
);
247 QCOMPARE(m_view
->selectedItemsCount(), itemsCount
);
248 QCOMPARE(spySelectionChanged
.count(), 1);
249 QCOMPARE(qvariant_cast
<KFileItemList
>(spySelectionChanged
.at(0).at(0)).count(), itemsCount
);
250 if (itemsCount
!= 0) {
251 QVERIFY(m_view
->hasSelection());
254 QVERIFY(!m_view
->hasSelection());
258 #include "dolphinviewtest_allviewmodes.moc"