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