]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Dolphin unit test refactoring
[dolphin.git] / src / tests / dolphindetailsviewtest.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 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 <qtest_kde.h>
21
22 #include "testbase.h"
23 #include "testdir.h"
24
25 #include "views/dolphindetailsview.h"
26 #include "views/dolphinview.h"
27 #include "views/dolphinmodel.h"
28 #include "views/dolphinsortfilterproxymodel.h"
29 #include "views/zoomlevelinfo.h"
30
31 #include <qtestmouse.h>
32 #include <qtestkeyboard.h>
33
34 class DolphinDetailsViewTest : public TestBase
35 {
36 Q_OBJECT
37
38 private slots:
39
40 void testExpandedUrls();
41
42 void bug217447_shiftArrowSelection();
43 void bug234600_overlappingIconsWhenZooming();
44 void bug257401_longFilenamesKeyboardNavigation();
45
46 private:
47
48 /**
49 * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits
50 * until loading the folder in the view is finished.
51 *
52 * Many unit tests need access to the internal DolphinDetailsView in DolphinView.
53 * Therefore, a pointer to the details view is returned by initView(DolphinView*).
54 */
55 DolphinDetailsView* initView(DolphinView* view) const {
56 view->setMode(DolphinView::DetailsView);
57 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view));
58 detailsView->setFoldersExpandable(true);
59 view->resize(400, 400);
60 view->show();
61 QTest::qWaitForWindowShown(view);
62 reloadViewAndWait(view);
63 return detailsView;
64 }
65
66 QModelIndex proxyModelIndexForUrl(const DolphinView* view, const KUrl& url) const {
67 const QModelIndex index = view->m_viewAccessor.m_dolphinModel->indexForUrl(url);
68 return view->m_viewAccessor.m_proxyModel->mapFromSource(index);
69 }
70 };
71
72 /**
73 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
74 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
75 * Each of those contains 3 further subfolders of the same name.
76 */
77
78 void DolphinDetailsViewTest::testExpandedUrls()
79 {
80 QStringList files;
81 QStringList subFolderNames;
82 subFolderNames << "a" << "b" << "c";
83
84 foreach(const QString& level1, subFolderNames) {
85 foreach(const QString& level2, subFolderNames) {
86 foreach(const QString& level3, subFolderNames) {
87 files << level1 + "/" + level2 + "/" + level3 + "/testfile";
88 }
89 }
90 }
91
92 TestDir dir;
93 dir.createFiles(files);
94 DolphinView view(dir.url(), 0);
95 DolphinDetailsView* detailsView = initView(&view);
96
97 // We start with an empty set of expanded URLs.
98 QSet<KUrl> expectedExpandedUrls;
99 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
100
101 // Every time we expand a folder, we have to wait until the view has finished loading
102 // its contents before we can expand further subfolders. We keep track of the reloading
103 // using a signal spy.
104 QSignalSpy spyFinishedPathLoading(&view, SIGNAL(finishedPathLoading(const KUrl&)));
105
106 // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
107 QStringList itemsToExpand;
108 itemsToExpand << "b" << "b/a" << "b/a/c" << "b/c" << "c";
109
110 foreach(const QString& item, itemsToExpand) {
111 KUrl url(dir.name() + item);
112 detailsView->expand(proxyModelIndexForUrl(&view, url));
113 expectedExpandedUrls += url;
114 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
115
116 // Before we proceed, we have to make sure that the view has finished
117 // loading the contents of the expanded folder.
118 while (spyFinishedPathLoading.isEmpty()) {
119 QTest::qWait(10);
120 }
121 spyFinishedPathLoading.takeFirst();
122 }
123
124 // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
125 QStringList itemsToCollapse;
126 itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b";
127
128 foreach(const QString& item, itemsToCollapse) {
129 KUrl url(dir.name() + item);
130 detailsView->collapse(proxyModelIndexForUrl(&view, url));
131 expectedExpandedUrls -= url;
132 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
133 }
134 }
135
136 /**
137 * When the first item in the view is active and Shift is held while the "arrow down"
138 * key is pressed repeatedly, the selection should grow by one item for each key press.
139 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
140 *
141 * https://bugs.kde.org/show_bug.cgi?id=217447
142 *
143 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
144 * column for an item, but only the width of the actual file name, did not reimplement
145 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
146 * uses the center of the visualRect of an item internally. If the width of the file name
147 * is less than half the width of the "Name" column, the center of an item's visualRect
148 * was therefore outside the space that DolphinDetailsView actually assigned to the
149 * item, and this led to unexpected deselection of items.
150 *
151 * TODO: To make the test more reliable, one could adjust the width of the "Name"
152 * column before the test in order to really make sure that the column is more than twice
153 * as wide as the space actually occupied by the file names (this triggers the bug).
154 */
155
156 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
157 {
158 TestDir dir;
159 for (int i = 0; i < 100; i++) {
160 dir.createFile(QString("%1").arg(i));
161 }
162 DolphinView view(dir.url(), 0);
163 DolphinDetailsView* detailsView = initView(&view);
164
165 // Select the first item
166 QModelIndex index0 = detailsView->model()->index(0, 0);
167 detailsView->setCurrentIndex(index0);
168 QCOMPARE(detailsView->currentIndex(), index0);
169
170 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
171 // directly: we check that passing the corners or the center of an item's visualRect
172 // to itemAt() returns the item (and not an invalid model index).
173 QRect rect = detailsView->visualRect(index0);
174 QCOMPARE(detailsView->indexAt(rect.center()), index0);
175 QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
176 QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
177 QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
178 QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
179
180 // Another way to test this is to Ctrl-click the center of the visualRect.
181 // The selection state of the item should be toggled.
182 detailsView->clearSelection();
183 QItemSelectionModel* selectionModel = detailsView->selectionModel();
184 QCOMPARE(selectionModel->selectedIndexes().count(), 0);
185
186 QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
187 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
188 QCOMPARE(selectedIndexes.count(), 1);
189 QVERIFY(selectedIndexes.contains(index0));
190
191 // Now we go down item by item using Shift+Down. In each step, we check that the current item
192 // is added to the selection and that the size of the selection grows by one.
193
194 int current = 1;
195
196 while (current < 100) {
197 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
198 QModelIndex currentIndex = detailsView->model()->index(current, 0);
199 QCOMPARE(detailsView->currentIndex(), currentIndex);
200
201 selectedIndexes = selectionModel->selectedIndexes();
202 QCOMPARE(selectedIndexes.count(), current + 1);
203 QVERIFY(selectedIndexes.contains(currentIndex));
204
205 current++;
206 }
207 }
208
209 /**
210 * When the icon size is changed, we have to make sure that the maximumSize given
211 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
212 * done, the visualRects are clipped by the incorrect maximum size, and the icons
213 * may overlap, see
214 *
215 * https://bugs.kde.org/show_bug.cgi?id=234600
216 */
217
218 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
219 {
220 QStringList files;
221 files << "a" << "b" << "c" << "d";
222
223 TestDir dir;
224 dir.createFiles(files);
225 DolphinView view(dir.url(), 0);
226 DolphinDetailsView* detailsView = initView(&view);
227
228 QModelIndex index0 = detailsView->model()->index(0, 0);
229 detailsView->setCurrentIndex(index0);
230 QCOMPARE(detailsView->currentIndex(), index0);
231
232 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
233 // should make sure that the bug is triggered.
234 int zoomLevelBackup = view.zoomLevel();
235 int zoomLevel = ZoomLevelInfo::minimumLevel();
236 view.setZoomLevel(zoomLevel);
237
238 QModelIndex index1 = detailsView->model()->index(1, 0);
239 detailsView->setCurrentIndex(index1);
240 QCOMPARE(detailsView->currentIndex(), index1);
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 //Check for each zoom level that the height of each item is at least the icon size.
249 QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
250 }
251
252 view.setZoomLevel(zoomLevelBackup);
253 }
254
255 /**
256 * The width of the visualRect of an item is usually replaced by the width of the file name.
257 * However, if the file name is wider then the view's name column, this leads to problems with
258 * keyboard navigation if files with very long names are present in the current folder, see
259 *
260 * https://bugs.kde.org/show_bug.cgi?id=257401
261 *
262 * This test checks that the visualRect of an item is never wider than the "Name" column.
263 */
264
265 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
266 TestDir dir;
267 QString name;
268 for (int i = 0; i < 20; i++) {
269 name += "mmmmmmmmmm";
270 dir.createFile(name);
271 }
272 DolphinView view(dir.url(), 0);
273 DolphinDetailsView* detailsView = initView(&view);
274
275 // Select the first item
276 QModelIndex index0 = detailsView->model()->index(0, 0);
277 detailsView->setCurrentIndex(index0);
278 QCOMPARE(detailsView->currentIndex(), index0);
279 QVERIFY(detailsView->visualRect(index0).width() < detailsView->columnWidth(DolphinModel::Name));
280
281 QItemSelectionModel* selectionModel = detailsView->selectionModel();
282 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
283 QCOMPARE(selectedIndexes.count(), 1);
284 QVERIFY(selectedIndexes.contains(index0));
285
286 // Move down successively using the "Down" key and check that current item
287 // and selection are as expected.
288 for (int i = 0; i < 19; i++) {
289 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
290 QModelIndex currentIndex = detailsView->model()->index(i + 1, 0);
291 QCOMPARE(detailsView->currentIndex(), currentIndex);
292 QVERIFY(detailsView->visualRect(currentIndex).width() <= detailsView->columnWidth(DolphinModel::Name));
293 selectedIndexes = selectionModel->selectedIndexes();
294 QCOMPARE(selectedIndexes.count(), 1);
295 QVERIFY(selectedIndexes.contains(currentIndex));
296 }
297 }
298
299 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
300
301 #include "dolphindetailsviewtest.moc"