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();
50 QModelIndex
proxyModelIndexForUrl(const KUrl
& url
) const {
51 const QModelIndex index
= m_dolphinModel
->indexForUrl(url
);
52 return m_proxyModel
->mapFromSource(index
);
57 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
58 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also names a, b, c) each.
59 * Each of those contains 3 further subfolders of the same name.
62 void DolphinDetailsViewTest::testExpandedUrls()
65 QStringList subFolderNames
;
66 subFolderNames
<< "a" << "b" << "c";
68 foreach(const QString
& level1
, subFolderNames
) {
69 foreach(const QString
& level2
, subFolderNames
) {
70 foreach(const QString
& level3
, subFolderNames
) {
71 files
<< level1
+ "/" + level2
+ "/" + level3
+ "/testfile";
78 m_view
->setMode(DolphinView::DetailsView
);
79 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
81 detailsView
->setFoldersExpandable(true);
82 m_view
->resize(400, 400);
84 QTest::qWaitForWindowShown(m_view
);
87 // We start with an empty set of expanded URLs.
88 QSet
<KUrl
> expectedExpandedUrls
;
89 QCOMPARE(detailsView
->expandedUrls(), expectedExpandedUrls
);
91 // Every time we expand a folder, we have to wait until the view has finished loading
92 // its contents before we can expand further subfolders. We keep track of the reloading
93 // using a signal spy.
94 QSignalSpy
spyFinishedPathLoading(m_view
, SIGNAL(finishedPathLoading(const KUrl
&)));
96 // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
97 QStringList itemsToExpand
;
98 itemsToExpand
<< "b" << "b/a" << "b/a/c" << "b/c" << "c";
100 foreach(const QString
& item
, itemsToExpand
) {
101 KUrl
url(m_path
+ item
);
102 detailsView
->expand(proxyModelIndexForUrl(url
));
103 expectedExpandedUrls
+= url
;
104 QCOMPARE(detailsView
->expandedUrls(), expectedExpandedUrls
);
106 // Before we proceed, we have to make sure that the view has finished
107 // loading the contents of the expanded folder.
108 while (spyFinishedPathLoading
.isEmpty()) {
111 spyFinishedPathLoading
.takeFirst();
114 // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
115 QStringList itemsToCollapse
;
116 itemsToCollapse
<< "b/c" << "b/a/c" << "c" << "b/a" << "b";
118 foreach(const QString
& item
, itemsToCollapse
) {
119 KUrl
url(m_path
+ item
);
120 detailsView
->collapse(proxyModelIndexForUrl(url
));
121 expectedExpandedUrls
-= url
;
122 QCOMPARE(detailsView
->expandedUrls(), expectedExpandedUrls
);
130 * When the first item in the view is active and Shift is held while the "arrow down"
131 * key is pressed repeatedly, the selection should grow by one item for each key press.
132 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
134 * https://bugs.kde.org/show_bug.cgi?id=217447
136 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
137 * column for an item, but only the width of the actual file name, did not reimplement
138 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
139 * uses the center of the visualRect of an item internally. If the width of the file name
140 * is less than half the width of the "Name" column, the center of an item's visualRect
141 * was therefore outside the space that DolphinDetailsView actually assigned to the
142 * item, and this led to unexpected deselection of items.
144 * TODO: To make the test more reliable, one could adjust the width of the "Name"
145 * column before the test in order to really make sure that the column is more than twice
146 * as wide as the space actually occupied by the file names (this triggers the bug).
149 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
151 for (int i
= 0; i
< 100; i
++) {
152 createFile(QString("%1").arg(i
));
155 m_view
->setMode(DolphinView::DetailsView
);
156 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
157 QVERIFY(detailsView
);
158 m_view
->resize(1000, 400);
160 QTest::qWaitForWindowShown(m_view
);
163 // Select the first item
164 QModelIndex index0
= detailsView
->model()->index(0, 0);
165 detailsView
->setCurrentIndex(index0
);
166 QCOMPARE(detailsView
->currentIndex(), index0
);
168 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
169 // directly: we check that passing the corners or the center of an item's visualRect
170 // to itemAt() returns the item (and not an invalid model index).
171 QRect rect
= detailsView
->visualRect(index0
);
172 QCOMPARE(detailsView
->indexAt(rect
.center()), index0
);
173 QCOMPARE(detailsView
->indexAt(rect
.topLeft()), index0
);
174 QCOMPARE(detailsView
->indexAt(rect
.topRight()), index0
);
175 QCOMPARE(detailsView
->indexAt(rect
.bottomLeft()), index0
);
176 QCOMPARE(detailsView
->indexAt(rect
.bottomRight()), index0
);
178 // Another way to test this is to Ctrl-click the center of the visualRect.
179 // The selection state of the item should be toggled.
180 detailsView
->clearSelection();
181 QItemSelectionModel
* selectionModel
= detailsView
->selectionModel();
182 QCOMPARE(selectionModel
->selectedIndexes().count(), 0);
184 QTest::mouseClick(detailsView
->viewport(), Qt::LeftButton
, Qt::ControlModifier
, rect
.center());
185 QModelIndexList selectedIndexes
= selectionModel
->selectedIndexes();
186 QCOMPARE(selectedIndexes
.count(), 1);
187 QVERIFY(selectedIndexes
.contains(index0
));
189 // Now we go down item by item using Shift+Down. In each step, we check that the current item
190 // is added to the selection and that the size of the selection grows by one.
194 while (current
< 100) {
195 QTest::keyClick(detailsView
->viewport(), Qt::Key_Down
, Qt::ShiftModifier
);
196 QModelIndex currentIndex
= detailsView
->model()->index(current
, 0);
197 QCOMPARE(detailsView
->currentIndex(), currentIndex
);
199 selectedIndexes
= selectionModel
->selectedIndexes();
200 QCOMPARE(selectedIndexes
.count(), current
+ 1);
201 QVERIFY(selectedIndexes
.contains(currentIndex
));
211 * When the icon size is changed, we have to make sure that the maximumSize given
212 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
213 * done, the visualRects are clipped by the incorrect maximum size, and the icons
216 * https://bugs.kde.org/show_bug.cgi?id=234600
219 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
222 files
<< "a" << "b" << "c" << "d";
225 m_view
->setMode(DolphinView::DetailsView
);
226 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
227 QVERIFY(detailsView
);
228 m_view
->resize(400, 400);
230 QTest::qWaitForWindowShown(m_view
);
233 QModelIndex index0
= detailsView
->model()->index(0, 0);
234 detailsView
->setCurrentIndex(index0
);
235 QCOMPARE(detailsView
->currentIndex(), index0
);
237 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
238 // should make sure that the bug is triggered.
239 int zoomLevel
= ZoomLevelInfo::minimumLevel();
240 m_view
->setZoomLevel(zoomLevel
);
242 QModelIndex index1
= detailsView
->model()->index(1, 0);
243 detailsView
->setCurrentIndex(index1
);
244 QCOMPARE(detailsView
->currentIndex(), index1
);
246 // Increase the zoom level successively to the maximum.
247 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
249 m_view
->setZoomLevel(zoomLevel
);
251 //Check for each zoom level that the height of each item is at least the icon size.
252 QVERIFY(detailsView
->visualRect(index1
).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
));
259 QTEST_KDEMAIN(DolphinDetailsViewTest
, GUI
)
261 #include "dolphindetailsviewtest.moc"