]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Encapsulate the creation and handling of the directory lister, the model and proxy...
[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/dolphinsortfilterproxymodel.h"
28 #include "views/zoomlevelinfo.h"
29
30 #include <qtestmouse.h>
31 #include <qtestkeyboard.h>
32
33 class DolphinDetailsViewTest : public TestBase
34 {
35 Q_OBJECT
36
37 private slots:
38
39 void init() {
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);
45 m_view->show();
46 QTest::qWaitForWindowShown(m_view);
47 }
48
49 void cleanup() {
50 m_view->hide();
51 cleanupTestDir();
52 }
53
54 void testExpandedUrls();
55
56 void bug217447_shiftArrowSelection();
57 void bug234600_overlappingIconsWhenZooming();
58 void bug257401_longFilenamesKeyboardNavigation();
59
60 private:
61
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);
65 }
66
67 DolphinDetailsView* m_detailsView;
68 };
69
70 /**
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.
74 */
75
76 void DolphinDetailsViewTest::testExpandedUrls()
77 {
78 m_detailsView->setFoldersExpandable(true);
79
80 QStringList files;
81 QStringList subFolderNames;
82 subFolderNames << "a" << "b" << "c";
83
84 foreach(const QString& level1, subFolderNames) {
85 foreach(const QString& level2, subFolderNames) {
86 foreach(const QString& level3, subFolderNames) {
87 files << level1 + "/" + level2 + "/" + level3 + "/testfile";
88 }
89 }
90 }
91
92 createFiles(files);
93 reloadViewAndWait();
94
95 // We start with an empty set of expanded URLs.
96 QSet<KUrl> expectedExpandedUrls;
97 QCOMPARE(m_detailsView->expandedUrls(), expectedExpandedUrls);
98
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&)));
103
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";
107
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);
113
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()) {
117 QTest::qWait(10);
118 }
119 spyFinishedPathLoading.takeFirst();
120 }
121
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";
125
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);
131 }
132 }
133
134 /**
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
138 *
139 * https://bugs.kde.org/show_bug.cgi?id=217447
140 *
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.
148 *
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).
152 */
153
154 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
155 {
156 for (int i = 0; i < 100; i++) {
157 createFile(QString("%1").arg(i));
158 }
159 reloadViewAndWait();
160
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);
165
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);
175
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);
181
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));
186
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.
189
190 int current = 1;
191
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);
196
197 selectedIndexes = selectionModel->selectedIndexes();
198 QCOMPARE(selectedIndexes.count(), current + 1);
199 QVERIFY(selectedIndexes.contains(currentIndex));
200
201 current++;
202 }
203 }
204
205 /**
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
209 * may overlap, see
210 *
211 * https://bugs.kde.org/show_bug.cgi?id=234600
212 */
213
214 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
215 {
216 QStringList files;
217 files << "a" << "b" << "c" << "d";
218
219 createFiles(files);
220 reloadViewAndWait();
221
222 QModelIndex index0 = m_detailsView->model()->index(0, 0);
223 m_detailsView->setCurrentIndex(index0);
224 QCOMPARE(m_detailsView->currentIndex(), index0);
225
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);
231
232 QModelIndex index1 = m_detailsView->model()->index(1, 0);
233 m_detailsView->setCurrentIndex(index1);
234 QCOMPARE(m_detailsView->currentIndex(), index1);
235
236 // Increase the zoom level successively to the maximum.
237 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
238 zoomLevel++;
239 m_view->setZoomLevel(zoomLevel);
240 QCOMPARE(m_view->zoomLevel(), zoomLevel);
241
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));
244 }
245
246 m_view->setZoomLevel(zoomLevelBackup);
247 }
248
249 /**
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
253 *
254 * https://bugs.kde.org/show_bug.cgi?id=257401
255 *
256 * This test checks that the visualRect of an item is never wider than the "Name" column.
257 */
258
259 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
260 QString name;
261 for (int i = 0; i < 20; i++) {
262 name += "mmmmmmmmmm";
263 createFile(name);
264 }
265 reloadViewAndWait();
266
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));
272
273 QItemSelectionModel* selectionModel = m_detailsView->selectionModel();
274 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
275 QCOMPARE(selectedIndexes.count(), 1);
276 QVERIFY(selectedIndexes.contains(index0));
277
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));
288 }
289 }
290
291 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
292
293 #include "dolphindetailsviewtest.moc"