]>
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 if (mode() == DolphinView::ColumnView
) {
41 // In Columns View mode, we need to create a new DolphinView after each
42 // test to make the file-related tests after the first one pass.
43 // TODO: Try to find out if there is a hidden bug in the Columns View that causes this.
45 m_view
= new DolphinView(KUrl(m_path
), 0);
47 m_view
->setMode(mode());
48 QVERIFY(verifyCorrectViewMode());
49 m_view
->resize(200, 300);
51 QTest::qWaitForWindowShown(m_view
);
54 void DolphinViewTest_AllViewModes::cleanup() {
60 * testSelection() checks the basic selection funtionality of DolphinView, including:
63 * selectedItemsCount()
69 * and the signal selectionChanged(const KFileItemList& selection)
72 Q_DECLARE_METATYPE(KFileItemList
)
74 void DolphinViewTest_AllViewModes::testSelection() {
75 const int totalItems
= 50;
77 for (int i
= 0; i
< totalItems
; i
++) {
78 createFile(QString("%1").arg(i
));
82 // Start with an empty selection
83 m_view
->clearSelection();
85 QCOMPARE(m_view
->selectedItems().count(), 0);
86 QCOMPARE(m_view
->selectedItemsCount(), 0);
87 QVERIFY(!m_view
->hasSelection());
89 // First some simple tests where either all or no items are selected
91 verifySelectedItemsCount(totalItems
);
93 m_view
->invertSelection();
94 verifySelectedItemsCount(0);
96 m_view
->invertSelection();
97 verifySelectedItemsCount(totalItems
);
99 m_view
->clearSelection();
100 verifySelectedItemsCount(0);
102 // Now we select individual items using mouse clicks
103 QModelIndex index
= itemView()->model()->index(2, 0);
104 itemView()->scrollTo(index
);
105 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
106 verifySelectedItemsCount(1);
108 index
= itemView()->model()->index(45, 0);
109 itemView()->scrollTo(index
);
110 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ControlModifier
, itemView()->visualRect(index
).center());
111 verifySelectedItemsCount(2);
113 index
= itemView()->model()->index(48, 0);
114 itemView()->scrollTo(index
);
115 QTest::mouseClick(itemView()->viewport(), Qt::LeftButton
, Qt::ShiftModifier
, itemView()->visualRect(index
).center());
116 verifySelectedItemsCount(5);
120 * Check that setting the directory view properties works.
123 void DolphinViewTest_AllViewModes::testViewPropertySettings()
125 // Create some files with different sizes and modification times to check the different sorting options
126 QDateTime now
= QDateTime::currentDateTime();
128 createFile("a", "A file", now
.addDays(-3));
129 createFile("b", "A larger file", now
.addDays(0));
130 createDir("c", now
.addDays(-2));
131 createFile("d", "The largest file in this directory", now
.addDays(-1));
132 createFile("e", "An even larger file", now
.addDays(-4));
137 // First set all settings to the default.
138 m_view
->setSorting(DolphinView::SortByName
);
139 QCOMPARE(m_view
->sorting(), DolphinView::SortByName
);
141 m_view
->setSortOrder(Qt::AscendingOrder
);
142 QCOMPARE(m_view
->sortOrder(), Qt::AscendingOrder
);
144 m_view
->setSortFoldersFirst(true);
145 QVERIFY(m_view
->sortFoldersFirst());
147 m_view
->setShowPreview(false);
148 QVERIFY(!m_view
->showPreview());
150 m_view
->setShowHiddenFiles(false);
151 QVERIFY(!m_view
->showHiddenFiles());
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, see
222 * https://bugs.kde.org/show_bug.cgi?id=261147
225 void DolphinViewTest_AllViewModes::testKeyboardFocus()
227 const DolphinView::Mode mode
= m_view
->mode();
229 // Move keyboard focus to another widget. To see that this is needed, run only this test,
230 // i.e., pass 'testKeyboardFocus' as a parameter on the command line.
233 QTest::qWaitForWindowShown(&widget
);
236 QVERIFY(!m_view
->hasFocus());
238 // Switch view modes and verify that the view does not get the focus back
239 for (int i
= 0; i
<= DolphinView::MaxModeEnum
; ++i
) {
240 m_view
->setMode(static_cast<DolphinView::Mode
>(i
));
241 QVERIFY(!m_view
->hasFocus());
244 m_view
->setMode(mode
);
248 * verifySelectedItemsCount(int) waits until the DolphinView's selectionChanged(const KFileItemList&)
249 * signal is received and checks that the selection state of the view is as expected.
252 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount
) const
254 QSignalSpy
spySelectionChanged(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)));
255 QVERIFY(QTest::kWaitForSignal(m_view
, SIGNAL(selectionChanged(const KFileItemList
&)), 500));
257 QCOMPARE(m_view
->selectedItems().count(), itemsCount
);
258 QCOMPARE(m_view
->selectedItemsCount(), itemsCount
);
259 QCOMPARE(spySelectionChanged
.count(), 1);
260 QCOMPARE(qvariant_cast
<KFileItemList
>(spySelectionChanged
.at(0).at(0)).count(), itemsCount
);
261 if (itemsCount
!= 0) {
262 QVERIFY(m_view
->hasSelection());
265 QVERIFY(!m_view
->hasSelection());
269 #include "dolphinviewtest_allviewmodes.moc"