]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
31b4a57cdde64e630b0decad2e1de0b1b6f5cf3e
[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 #include "testdir.h"
24
25 #include "views/dolphindetailsview.h"
26 #include "views/dolphinview.h"
27 #include "views/dolphinmodel.h"
28 #include "views/dolphinsortfilterproxymodel.h"
29 #include "views/zoomlevelinfo.h"
30
31 #include <qtestmouse.h>
32 #include <qtestkeyboard.h>
33
34 class DolphinDetailsViewTest : public TestBase
35 {
36 Q_OBJECT
37
38 private slots:
39
40 void testExpandedUrls();
41
42 void bug217447_shiftArrowSelection();
43 void bug234600_overlappingIconsWhenZooming();
44 void bug257401_longFilenamesKeyboardNavigation();
45
46 private:
47
48 /**
49 * initView(DolphinView*) sets the correct view mode, shows the view on the screen, and waits
50 * until loading the folder in the view is finished.
51 *
52 * Many unit tests need access to the internal DolphinDetailsView in DolphinView.
53 * Therefore, a pointer to the details view is returned by initView(DolphinView*).
54 */
55 DolphinDetailsView* initView(DolphinView* view) const {
56 QSignalSpy spyFinishedPathLoading(view, SIGNAL(finishedPathLoading(const KUrl&)));
57 view->setMode(DolphinView::DetailsView);
58 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view));
59 Q_ASSERT(detailsView);
60 detailsView->setFoldersExpandable(true);
61 view->resize(400, 400);
62 view->show();
63 QTest::qWaitForWindowShown(view);
64
65 // If the DolphinView's finishedPathLoading(const KUrl&) signal has not been received yet,
66 // we have to wait a bit more.
67 // The reason why the if-statement is needed here is that the signal might have been emitted
68 // while we were waiting in QTest::qWaitForWindowShown(view)
69 // -> waitForFinishedPathLoading(view) would fail in that case.
70 if (spyFinishedPathLoading.isEmpty()) {
71 waitForFinishedPathLoading(view);
72 }
73
74 return detailsView;
75 }
76
77 QModelIndex proxyModelIndexForUrl(const DolphinView* view, const KUrl& url) const {
78 const QModelIndex index = view->m_viewAccessor.m_dolphinModel->indexForUrl(url);
79 return view->m_viewAccessor.m_proxyModel->mapFromSource(index);
80 }
81 };
82
83 /**
84 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
85 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
86 * Each of those contains 3 further subfolders of the same name.
87 */
88
89 void DolphinDetailsViewTest::testExpandedUrls()
90 {
91 QStringList files;
92 QStringList subFolderNames;
93 subFolderNames << "a" << "b" << "c";
94
95 foreach(const QString& level1, subFolderNames) {
96 foreach(const QString& level2, subFolderNames) {
97 foreach(const QString& level3, subFolderNames) {
98 files << level1 + "/" + level2 + "/" + level3 + "/testfile";
99 }
100 }
101 }
102
103 TestDir dir;
104 dir.createFiles(files);
105 DolphinView view(dir.url(), 0);
106 DolphinDetailsView* detailsView = initView(&view);
107
108 // We start with an empty set of expanded URLs.
109 QSet<KUrl> expectedExpandedUrls;
110 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
111
112 // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
113 QStringList itemsToExpand;
114 itemsToExpand << "b" << "b/a" << "b/a/c" << "b/c" << "c";
115
116 foreach(const QString& item, itemsToExpand) {
117 KUrl url(dir.name() + item);
118 detailsView->expand(proxyModelIndexForUrl(&view, url));
119 expectedExpandedUrls += url;
120 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
121
122 // Before we proceed, we have to make sure that the view has finished
123 // loading the contents of the expanded folder.
124 waitForFinishedPathLoading(&view);
125 }
126
127 // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
128 QStringList itemsToCollapse;
129 itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b";
130
131 foreach(const QString& item, itemsToCollapse) {
132 KUrl url(dir.name() + item);
133 detailsView->collapse(proxyModelIndexForUrl(&view, url));
134 expectedExpandedUrls -= url;
135 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
136 }
137 }
138
139 /**
140 * When the first item in the view is active and Shift is held while the "arrow down"
141 * key is pressed repeatedly, the selection should grow by one item for each key press.
142 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
143 *
144 * https://bugs.kde.org/show_bug.cgi?id=217447
145 *
146 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
147 * column for an item, but only the width of the actual file name, did not reimplement
148 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
149 * uses the center of the visualRect of an item internally. If the width of the file name
150 * is less than half the width of the "Name" column, the center of an item's visualRect
151 * was therefore outside the space that DolphinDetailsView actually assigned to the
152 * item, and this led to unexpected deselection of items.
153 *
154 * TODO: To make the test more reliable, one could adjust the width of the "Name"
155 * column before the test in order to really make sure that the column is more than twice
156 * as wide as the space actually occupied by the file names (this triggers the bug).
157 */
158
159 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
160 {
161 TestDir dir;
162 for (int i = 0; i < 100; i++) {
163 dir.createFile(QString("%1").arg(i));
164 }
165 DolphinView view(dir.url(), 0);
166 DolphinDetailsView* detailsView = initView(&view);
167
168 // Select the first item
169 QModelIndex index0 = detailsView->model()->index(0, 0);
170 detailsView->setCurrentIndex(index0);
171 QCOMPARE(detailsView->currentIndex(), index0);
172
173 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
174 // directly: we check that passing the corners or the center of an item's visualRect
175 // to itemAt() returns the item (and not an invalid model index).
176 QRect rect = detailsView->visualRect(index0);
177 QCOMPARE(detailsView->indexAt(rect.center()), index0);
178 QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
179 QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
180 QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
181 QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
182
183 // Another way to test this is to Ctrl-click the center of the visualRect.
184 // The selection state of the item should be toggled.
185 detailsView->clearSelection();
186 QItemSelectionModel* selectionModel = detailsView->selectionModel();
187 QCOMPARE(selectionModel->selectedIndexes().count(), 0);
188
189 QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
190 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
191 QCOMPARE(selectedIndexes.count(), 1);
192 QVERIFY(selectedIndexes.contains(index0));
193
194 // Now we go down item by item using Shift+Down. In each step, we check that the current item
195 // is added to the selection and that the size of the selection grows by one.
196
197 int current = 1;
198
199 while (current < 100) {
200 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
201 QModelIndex currentIndex = detailsView->model()->index(current, 0);
202 QCOMPARE(detailsView->currentIndex(), currentIndex);
203
204 selectedIndexes = selectionModel->selectedIndexes();
205 QCOMPARE(selectedIndexes.count(), current + 1);
206 QVERIFY(selectedIndexes.contains(currentIndex));
207
208 current++;
209 }
210 }
211
212 /**
213 * When the icon size is changed, we have to make sure that the maximumSize given
214 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
215 * done, the visualRects are clipped by the incorrect maximum size, and the icons
216 * may overlap, see
217 *
218 * https://bugs.kde.org/show_bug.cgi?id=234600
219 */
220
221 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
222 {
223 QStringList files;
224 files << "a" << "b" << "c" << "d";
225
226 TestDir dir;
227 dir.createFiles(files);
228 DolphinView view(dir.url(), 0);
229 DolphinDetailsView* detailsView = initView(&view);
230
231 QModelIndex index0 = detailsView->model()->index(0, 0);
232 detailsView->setCurrentIndex(index0);
233 QCOMPARE(detailsView->currentIndex(), index0);
234
235 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
236 // should make sure that the bug is triggered.
237 int zoomLevelBackup = view.zoomLevel();
238 int zoomLevel = ZoomLevelInfo::minimumLevel();
239 view.setZoomLevel(zoomLevel);
240
241 QModelIndex index1 = detailsView->model()->index(1, 0);
242 detailsView->setCurrentIndex(index1);
243 QCOMPARE(detailsView->currentIndex(), index1);
244
245 // Increase the zoom level successively to the maximum.
246 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
247 zoomLevel++;
248 view.setZoomLevel(zoomLevel);
249 QCOMPARE(view.zoomLevel(), zoomLevel);
250
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));
253 }
254
255 view.setZoomLevel(zoomLevelBackup);
256 }
257
258 /**
259 * The width of the visualRect of an item is usually replaced by the width of the file name.
260 * However, if the file name is wider then the view's name column, this leads to problems with
261 * keyboard navigation if files with very long names are present in the current folder, see
262 *
263 * https://bugs.kde.org/show_bug.cgi?id=257401
264 *
265 * This test checks that the visualRect of an item is never wider than the "Name" column.
266 */
267
268 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
269 TestDir dir;
270 QString name;
271 for (int i = 0; i < 20; i++) {
272 name += "mmmmmmmmmm";
273 dir.createFile(name);
274 }
275 DolphinView view(dir.url(), 0);
276 DolphinDetailsView* detailsView = initView(&view);
277
278 // Select the first item
279 QModelIndex index0 = detailsView->model()->index(0, 0);
280 detailsView->setCurrentIndex(index0);
281 QCOMPARE(detailsView->currentIndex(), index0);
282 QVERIFY(detailsView->visualRect(index0).width() < detailsView->columnWidth(DolphinModel::Name));
283
284 QItemSelectionModel* selectionModel = detailsView->selectionModel();
285 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
286 QCOMPARE(selectedIndexes.count(), 1);
287 QVERIFY(selectedIndexes.contains(index0));
288
289 // Move down successively using the "Down" key and check that current item
290 // and selection are as expected.
291 for (int i = 0; i < 19; i++) {
292 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
293 QModelIndex currentIndex = detailsView->model()->index(i + 1, 0);
294 QCOMPARE(detailsView->currentIndex(), currentIndex);
295 QVERIFY(detailsView->visualRect(currentIndex).width() <= detailsView->columnWidth(DolphinModel::Name));
296 selectedIndexes = selectionModel->selectedIndexes();
297 QCOMPARE(selectedIndexes.count(), 1);
298 QVERIFY(selectedIndexes.contains(currentIndex));
299 }
300 }
301
302 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
303
304 #include "dolphindetailsviewtest.moc"