]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphinviewtest_allviewmodes.cpp
Add a unit test that checks the DolphinView functionality that is
[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 "dolphinviewtest_allviewmodes.h"
21
22 #include <qtest_kde.h>
23
24 #include "testbase.h"
25
26 #include "views/dolphinview.h"
27 #include "views/dolphinmodel.h"
28 #include "views/dolphindirlister.h"
29 #include "views/dolphinsortfilterproxymodel.h"
30
31 #include <qtestmouse.h>
32 #include <qtestkeyboard.h>
33
34 DolphinViewTest_AllViewModes::DolphinViewTest_AllViewModes() {
35 // Need to register KFileItemList for use with QSignalSpy
36 qRegisterMetaType<KFileItemList>("KFileItemList");
37 }
38
39 void DolphinViewTest_AllViewModes::init() {
40 m_view->setMode(mode());
41 QVERIFY(verifyCorrectViewMode());
42 m_view->resize(200, 300);
43 m_view->show();
44 QTest::qWaitForWindowShown(m_view);
45 }
46
47 void DolphinViewTest_AllViewModes::cleanup() {
48 m_view->hide();
49 cleanupTestDir();
50 }
51
52 /**
53 * testSelection() checks the basic selection funtionality of DolphinView, including:
54 *
55 * selectedItems()
56 * selectedItemsCount()
57 * selectAll()
58 * invertSelection()
59 * clearSelection()
60 * hasSelection()
61 *
62 * and the signal selectionChanged(const KFileItemList& selection)
63 */
64
65 Q_DECLARE_METATYPE(KFileItemList)
66
67 void DolphinViewTest_AllViewModes::testSelection() {
68 const int totalItems = 50;
69
70 for (int i = 0; i < totalItems; i++) {
71 createFile(QString("%1").arg(i));
72 }
73 reloadViewAndWait();
74
75 // Start with an empty selection
76 m_view->clearSelection();
77
78 QCOMPARE(m_view->selectedItems().count(), 0);
79 QCOMPARE(m_view->selectedItemsCount(), 0);
80 QVERIFY(!m_view->hasSelection());
81
82 // First some simple tests where either all or no items are selected
83 m_view->selectAll();
84 verifySelectedItemsCount(totalItems);
85
86 m_view->invertSelection();
87 verifySelectedItemsCount(0);
88
89 m_view->invertSelection();
90 verifySelectedItemsCount(totalItems);
91
92 m_view->clearSelection();
93 verifySelectedItemsCount(0);
94
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);
100
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);
105
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);
110 }
111
112 /**
113 * Check that setting the directory view properties works.
114 */
115
116 void DolphinViewTest_AllViewModes::testViewPropertySettings()
117 {
118 // Create some files with different sizes and modification times to check the different sorting options
119 QDateTime now = QDateTime::currentDateTime();
120
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));
126 createFile(".f");
127
128 reloadViewAndWait();
129
130 // First set all settings to the default.
131 m_view->setSorting(DolphinView::SortByName);
132 QCOMPARE(m_view->sorting(), DolphinView::SortByName);
133
134 m_view->setSortOrder(Qt::AscendingOrder);
135 QCOMPARE(m_view->sortOrder(), Qt::AscendingOrder);
136
137 m_view->setSortFoldersFirst(true);
138 QVERIFY(m_view->sortFoldersFirst());
139
140 m_view->setShowPreview(false);
141 QVERIFY(!m_view->showPreview());
142
143 m_view->setShowHiddenFiles(false);
144 QVERIFY(!m_view->showHiddenFiles());
145
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();
151 }
152
153 /** Check that the sort order is correct for different kinds of settings */
154
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");
159
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");
165
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");
171
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");
177
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");
183
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");
189
190 // Disable "Sort Folders First"
191 m_view->setSortFoldersFirst(false);
192 QVERIFY(!m_view->sortFoldersFirst());
193 QCOMPARE(viewItems(), QStringList()<< "b" << "d" << "c" << "a" << "e");
194
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");
201
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");
208
209 // Previews
210 m_view->setShowPreview(true);
211 QVERIFY(m_view->showPreview());
212
213 // TODO: Check that the view properties are restored correctly when changing the folder and then going back.
214 }
215
216 /**
217 * testKeyboardFocus() checks whether a view grabs the keyboard focus.
218 *
219 * A view may never grab the keyboard focus itself and must respect the focus-state
220 * when switching the view mode.
221 */
222
223 void DolphinViewTest_AllViewModes::testKeyboardFocus()
224 {
225 const DolphinView::Mode mode = m_view->mode();
226
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());
231 }
232
233 m_view->setMode(mode);
234 }
235
236 /**
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.
239 */
240
241 void DolphinViewTest_AllViewModes::verifySelectedItemsCount(int itemsCount) const
242 {
243 QSignalSpy spySelectionChanged(m_view, SIGNAL(selectionChanged(const KFileItemList&)));
244 QVERIFY(QTest::kWaitForSignal(m_view, SIGNAL(selectionChanged(const KFileItemList&)), 500));
245
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());
252 }
253 else {
254 QVERIFY(!m_view->hasSelection());
255 }
256 }
257
258 #include "dolphinviewtest_allviewmodes.moc"