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/dolphinsortfilterproxymodel.h"
28 #include "views/zoomlevelinfo.h"
30 #include <qtestmouse.h>
31 #include <qtestkeyboard.h>
33 class DolphinDetailsViewTest
: public TestBase
40 m_view
->setMode(DolphinView::DetailsView
);
41 m_detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
42 QVERIFY(m_detailsView
);
43 m_detailsView
->setFoldersExpandable(true);
44 m_view
->resize(400, 400);
46 QTest::qWaitForWindowShown(m_view
);
54 void testExpandedUrls();
56 void bug217447_shiftArrowSelection();
57 void bug234600_overlappingIconsWhenZooming();
58 void bug257401_longFilenamesKeyboardNavigation();
62 QModelIndex
proxyModelIndexForUrl(const KUrl
& url
) const {
63 const QModelIndex index
= m_view
->m_viewAccessor
.m_dolphinModel
->indexForUrl(url
);
64 return m_view
->m_viewAccessor
.m_proxyModel
->mapFromSource(index
);
67 DolphinDetailsView
* m_detailsView
;
71 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
72 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
73 * Each of those contains 3 further subfolders of the same name.
76 void DolphinDetailsViewTest::testExpandedUrls()
78 m_detailsView
->setFoldersExpandable(true);
81 QStringList subFolderNames
;
82 subFolderNames
<< "a" << "b" << "c";
84 foreach(const QString
& level1
, subFolderNames
) {
85 foreach(const QString
& level2
, subFolderNames
) {
86 foreach(const QString
& level3
, subFolderNames
) {
87 files
<< level1
+ "/" + level2
+ "/" + level3
+ "/testfile";
95 // We start with an empty set of expanded URLs.
96 QSet
<KUrl
> expectedExpandedUrls
;
97 QCOMPARE(m_detailsView
->expandedUrls(), expectedExpandedUrls
);
99 // Every time we expand a folder, we have to wait until the view has finished loading
100 // its contents before we can expand further subfolders. We keep track of the reloading
101 // using a signal spy.
102 QSignalSpy
spyFinishedPathLoading(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)));
104 // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
105 QStringList itemsToExpand
;
106 itemsToExpand
<< "b" << "b/a" << "b/a/c" << "b/c" << "c";
108 foreach(const QString
& item
, itemsToExpand
) {
109 KUrl
url(m_path
+ item
);
110 m_detailsView
->expand(proxyModelIndexForUrl(url
));
111 expectedExpandedUrls
+= url
;
112 QCOMPARE(m_detailsView
->expandedUrls(), expectedExpandedUrls
);
114 // Before we proceed, we have to make sure that the view has finished
115 // loading the contents of the expanded folder.
116 while (spyFinishedPathLoading
.isEmpty()) {
119 spyFinishedPathLoading
.takeFirst();
122 // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
123 QStringList itemsToCollapse
;
124 itemsToCollapse
<< "b/c" << "b/a/c" << "c" << "b/a" << "b";
126 foreach(const QString
& item
, itemsToCollapse
) {
127 KUrl
url(m_path
+ item
);
128 m_detailsView
->collapse(proxyModelIndexForUrl(url
));
129 expectedExpandedUrls
-= url
;
130 QCOMPARE(m_detailsView
->expandedUrls(), expectedExpandedUrls
);
135 * When the first item in the view is active and Shift is held while the "arrow down"
136 * key is pressed repeatedly, the selection should grow by one item for each key press.
137 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
139 * https://bugs.kde.org/show_bug.cgi?id=217447
141 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
142 * column for an item, but only the width of the actual file name, did not reimplement
143 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
144 * uses the center of the visualRect of an item internally. If the width of the file name
145 * is less than half the width of the "Name" column, the center of an item's visualRect
146 * was therefore outside the space that DolphinDetailsView actually assigned to the
147 * item, and this led to unexpected deselection of items.
149 * TODO: To make the test more reliable, one could adjust the width of the "Name"
150 * column before the test in order to really make sure that the column is more than twice
151 * as wide as the space actually occupied by the file names (this triggers the bug).
154 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
156 for (int i
= 0; i
< 100; i
++) {
157 createFile(QString("%1").arg(i
));
161 // Select the first item
162 QModelIndex index0
= m_detailsView
->model()->index(0, 0);
163 m_detailsView
->setCurrentIndex(index0
);
164 QCOMPARE(m_detailsView
->currentIndex(), index0
);
166 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
167 // directly: we check that passing the corners or the center of an item's visualRect
168 // to itemAt() returns the item (and not an invalid model index).
169 QRect rect
= m_detailsView
->visualRect(index0
);
170 QCOMPARE(m_detailsView
->indexAt(rect
.center()), index0
);
171 QCOMPARE(m_detailsView
->indexAt(rect
.topLeft()), index0
);
172 QCOMPARE(m_detailsView
->indexAt(rect
.topRight()), index0
);
173 QCOMPARE(m_detailsView
->indexAt(rect
.bottomLeft()), index0
);
174 QCOMPARE(m_detailsView
->indexAt(rect
.bottomRight()), index0
);
176 // Another way to test this is to Ctrl-click the center of the visualRect.
177 // The selection state of the item should be toggled.
178 m_detailsView
->clearSelection();
179 QItemSelectionModel
* selectionModel
= m_detailsView
->selectionModel();
180 QCOMPARE(selectionModel
->selectedIndexes().count(), 0);
182 QTest::mouseClick(m_detailsView
->viewport(), Qt::LeftButton
, Qt::ControlModifier
, rect
.center());
183 QModelIndexList selectedIndexes
= selectionModel
->selectedIndexes();
184 QCOMPARE(selectedIndexes
.count(), 1);
185 QVERIFY(selectedIndexes
.contains(index0
));
187 // Now we go down item by item using Shift+Down. In each step, we check that the current item
188 // is added to the selection and that the size of the selection grows by one.
192 while (current
< 100) {
193 QTest::keyClick(m_detailsView
->viewport(), Qt::Key_Down
, Qt::ShiftModifier
);
194 QModelIndex currentIndex
= m_detailsView
->model()->index(current
, 0);
195 QCOMPARE(m_detailsView
->currentIndex(), currentIndex
);
197 selectedIndexes
= selectionModel
->selectedIndexes();
198 QCOMPARE(selectedIndexes
.count(), current
+ 1);
199 QVERIFY(selectedIndexes
.contains(currentIndex
));
206 * When the icon size is changed, we have to make sure that the maximumSize given
207 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
208 * done, the visualRects are clipped by the incorrect maximum size, and the icons
211 * https://bugs.kde.org/show_bug.cgi?id=234600
214 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
217 files
<< "a" << "b" << "c" << "d";
222 QModelIndex index0
= m_detailsView
->model()->index(0, 0);
223 m_detailsView
->setCurrentIndex(index0
);
224 QCOMPARE(m_detailsView
->currentIndex(), index0
);
226 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
227 // should make sure that the bug is triggered.
228 int zoomLevelBackup
= m_view
->zoomLevel();
229 int zoomLevel
= ZoomLevelInfo::minimumLevel();
230 m_view
->setZoomLevel(zoomLevel
);
232 QModelIndex index1
= m_detailsView
->model()->index(1, 0);
233 m_detailsView
->setCurrentIndex(index1
);
234 QCOMPARE(m_detailsView
->currentIndex(), index1
);
236 // Increase the zoom level successively to the maximum.
237 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
239 m_view
->setZoomLevel(zoomLevel
);
240 QCOMPARE(m_view
->zoomLevel(), zoomLevel
);
242 //Check for each zoom level that the height of each item is at least the icon size.
243 QVERIFY(m_detailsView
->visualRect(index1
).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
));
246 m_view
->setZoomLevel(zoomLevelBackup
);
250 * The width of the visualRect of an item is usually replaced by the width of the file name.
251 * However, if the file name is wider then the view's name column, this leads to problems with
252 * keyboard navigation if files with very long names are present in the current folder, see
254 * https://bugs.kde.org/show_bug.cgi?id=257401
256 * This test checks that the visualRect of an item is never wider than the "Name" column.
259 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
261 for (int i
= 0; i
< 20; i
++) {
262 name
+= "mmmmmmmmmm";
267 // Select the first item
268 QModelIndex index0
= m_detailsView
->model()->index(0, 0);
269 m_detailsView
->setCurrentIndex(index0
);
270 QCOMPARE(m_detailsView
->currentIndex(), index0
);
271 QVERIFY(m_detailsView
->visualRect(index0
).width() < m_detailsView
->columnWidth(DolphinModel::Name
));
273 QItemSelectionModel
* selectionModel
= m_detailsView
->selectionModel();
274 QModelIndexList selectedIndexes
= selectionModel
->selectedIndexes();
275 QCOMPARE(selectedIndexes
.count(), 1);
276 QVERIFY(selectedIndexes
.contains(index0
));
278 // Move down successively using the "Down" key and check that current item
279 // and selection are as expected.
280 for (int i
= 0; i
< 19; i
++) {
281 QTest::keyClick(m_detailsView
->viewport(), Qt::Key_Down
, Qt::NoModifier
);
282 QModelIndex currentIndex
= m_detailsView
->model()->index(i
+ 1, 0);
283 QCOMPARE(m_detailsView
->currentIndex(), currentIndex
);
284 QVERIFY(m_detailsView
->visualRect(currentIndex
).width() <= m_detailsView
->columnWidth(DolphinModel::Name
));
285 selectedIndexes
= selectionModel
->selectedIndexes();
286 QCOMPARE(selectedIndexes
.count(), 1);
287 QVERIFY(selectedIndexes
.contains(currentIndex
));
291 QTEST_KDEMAIN(DolphinDetailsViewTest
, GUI
)
293 #include "dolphindetailsviewtest.moc"