1 /***************************************************************************
2 * Copyright (C) 2010 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 <qtest_kde.h>
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"
33 #include <QtCore/QDir>
34 #include <qtestmouse.h>
35 #include <qtestkeyboard.h>
37 class DolphinDetailsViewTest
: public TestBase
43 void testExpandedUrls();
45 void bug217447_shiftArrowSelection();
46 void bug234600_overlappingIconsWhenZooming();
47 void bug257401_longFilenamesKeyboardNavigation();
51 QModelIndex
proxyModelIndexForUrl(const KUrl
& url
) const {
52 const QModelIndex index
= m_dolphinModel
->indexForUrl(url
);
53 return m_proxyModel
->mapFromSource(index
);
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.
63 void DolphinDetailsViewTest::testExpandedUrls()
66 QStringList subFolderNames
;
67 subFolderNames
<< "a" << "b" << "c";
69 foreach(const QString
& level1
, subFolderNames
) {
70 foreach(const QString
& level2
, subFolderNames
) {
71 foreach(const QString
& level3
, subFolderNames
) {
72 files
<< level1
+ "/" + level2
+ "/" + level3
+ "/testfile";
79 m_view
->setMode(DolphinView::DetailsView
);
80 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
82 detailsView
->setFoldersExpandable(true);
83 m_view
->resize(400, 400);
85 QTest::qWaitForWindowShown(m_view
);
88 // We start with an empty set of expanded URLs.
89 QSet
<KUrl
> expectedExpandedUrls
;
90 QCOMPARE(detailsView
->expandedUrls(), expectedExpandedUrls
);
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
&)));
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";
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
);
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()) {
112 spyFinishedPathLoading
.takeFirst();
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";
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
);
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
135 * https://bugs.kde.org/show_bug.cgi?id=217447
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.
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).
150 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
152 for (int i
= 0; i
< 100; i
++) {
153 createFile(QString("%1").arg(i
));
156 m_view
->setMode(DolphinView::DetailsView
);
157 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
158 QVERIFY(detailsView
);
159 m_view
->resize(1000, 400);
161 QTest::qWaitForWindowShown(m_view
);
164 // Select the first item
165 QModelIndex index0
= detailsView
->model()->index(0, 0);
166 detailsView
->setCurrentIndex(index0
);
167 QCOMPARE(detailsView
->currentIndex(), index0
);
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
);
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);
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
));
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.
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
);
200 selectedIndexes
= selectionModel
->selectedIndexes();
201 QCOMPARE(selectedIndexes
.count(), current
+ 1);
202 QVERIFY(selectedIndexes
.contains(currentIndex
));
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
217 * https://bugs.kde.org/show_bug.cgi?id=234600
220 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
223 files
<< "a" << "b" << "c" << "d";
226 m_view
->setMode(DolphinView::DetailsView
);
227 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
228 QVERIFY(detailsView
);
229 m_view
->resize(400, 400);
231 QTest::qWaitForWindowShown(m_view
);
234 QModelIndex index0
= detailsView
->model()->index(0, 0);
235 detailsView
->setCurrentIndex(index0
);
236 QCOMPARE(detailsView
->currentIndex(), index0
);
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
);
243 QModelIndex index1
= detailsView
->model()->index(1, 0);
244 detailsView
->setCurrentIndex(index1
);
245 QCOMPARE(detailsView
->currentIndex(), index1
);
247 // Increase the zoom level successively to the maximum.
248 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
250 m_view
->setZoomLevel(zoomLevel
);
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
));
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
265 * https://bugs.kde.org/show_bug.cgi?id=257401
267 * This test checks that the visualRect of an item is never wider than the "Name" column.
270 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
272 for (int i
= 0; i
< 20; i
++) {
273 name
+= "mmmmmmmmmm";
277 m_view
->setMode(DolphinView::DetailsView
);
278 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
279 QVERIFY(detailsView
);
280 m_view
->resize(400, 400);
282 QTest::qWaitForWindowShown(m_view
);
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
));
291 QItemSelectionModel
* selectionModel
= detailsView
->selectionModel();
292 QModelIndexList selectedIndexes
= selectionModel
->selectedIndexes();
293 QCOMPARE(selectedIndexes
.count(), 1);
294 QVERIFY(selectedIndexes
.contains(index0
));
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
));
312 QTEST_KDEMAIN(DolphinDetailsViewTest
, GUI
)
314 #include "dolphindetailsviewtest.moc"