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