]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Add function TestBase::waitForFinishedPathLoading()
[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 view->setMode(DolphinView::DetailsView);
57 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView(view));
58 detailsView->setFoldersExpandable(true);
59 view->resize(400, 400);
60 view->show();
61 QTest::qWaitForWindowShown(view);
62 reloadViewAndWait(view);
63 return detailsView;
64 }
65
66 QModelIndex proxyModelIndexForUrl(const DolphinView* view, const KUrl& url) const {
67 const QModelIndex index = view->m_viewAccessor.m_dolphinModel->indexForUrl(url);
68 return view->m_viewAccessor.m_proxyModel->mapFromSource(index);
69 }
70 };
71
72 /**
73 * This test verifies that DolphinDetailsView::expandedUrls() returns the right set of URLs.
74 * The test creates a folder hierarchy: 3 folders (a, b, c) contain 3 subfolders (also named a, b, c) each.
75 * Each of those contains 3 further subfolders of the same name.
76 */
77
78 void DolphinDetailsViewTest::testExpandedUrls()
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 TestDir dir;
93 dir.createFiles(files);
94 DolphinView view(dir.url(), 0);
95 DolphinDetailsView* detailsView = initView(&view);
96
97 // We start with an empty set of expanded URLs.
98 QSet<KUrl> expectedExpandedUrls;
99 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
100
101 // Expand URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
102 QStringList itemsToExpand;
103 itemsToExpand << "b" << "b/a" << "b/a/c" << "b/c" << "c";
104
105 foreach(const QString& item, itemsToExpand) {
106 KUrl url(dir.name() + item);
107 detailsView->expand(proxyModelIndexForUrl(&view, url));
108 expectedExpandedUrls += url;
109 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
110
111 // Before we proceed, we have to make sure that the view has finished
112 // loading the contents of the expanded folder.
113 QVERIFY(waitForFinishedPathLoading(&view));
114 }
115
116 // Collapse URLs one by one and verify the result of DolphinDetailsView::expandedUrls()
117 QStringList itemsToCollapse;
118 itemsToCollapse << "b/c" << "b/a/c" << "c" << "b/a" << "b";
119
120 foreach(const QString& item, itemsToCollapse) {
121 KUrl url(dir.name() + item);
122 detailsView->collapse(proxyModelIndexForUrl(&view, url));
123 expectedExpandedUrls -= url;
124 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
125 }
126 }
127
128 /**
129 * When the first item in the view is active and Shift is held while the "arrow down"
130 * key is pressed repeatedly, the selection should grow by one item for each key press.
131 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
132 *
133 * https://bugs.kde.org/show_bug.cgi?id=217447
134 *
135 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
136 * column for an item, but only the width of the actual file name, did not reimplement
137 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
138 * uses the center of the visualRect of an item internally. If the width of the file name
139 * is less than half the width of the "Name" column, the center of an item's visualRect
140 * was therefore outside the space that DolphinDetailsView actually assigned to the
141 * item, and this led to unexpected deselection of items.
142 *
143 * TODO: To make the test more reliable, one could adjust the width of the "Name"
144 * column before the test in order to really make sure that the column is more than twice
145 * as wide as the space actually occupied by the file names (this triggers the bug).
146 */
147
148 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
149 {
150 TestDir dir;
151 for (int i = 0; i < 100; i++) {
152 dir.createFile(QString("%1").arg(i));
153 }
154 DolphinView view(dir.url(), 0);
155 DolphinDetailsView* detailsView = initView(&view);
156
157 // Select the first item
158 QModelIndex index0 = detailsView->model()->index(0, 0);
159 detailsView->setCurrentIndex(index0);
160 QCOMPARE(detailsView->currentIndex(), index0);
161
162 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
163 // directly: we check that passing the corners or the center of an item's visualRect
164 // to itemAt() returns the item (and not an invalid model index).
165 QRect rect = detailsView->visualRect(index0);
166 QCOMPARE(detailsView->indexAt(rect.center()), index0);
167 QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
168 QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
169 QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
170 QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
171
172 // Another way to test this is to Ctrl-click the center of the visualRect.
173 // The selection state of the item should be toggled.
174 detailsView->clearSelection();
175 QItemSelectionModel* selectionModel = detailsView->selectionModel();
176 QCOMPARE(selectionModel->selectedIndexes().count(), 0);
177
178 QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
179 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
180 QCOMPARE(selectedIndexes.count(), 1);
181 QVERIFY(selectedIndexes.contains(index0));
182
183 // Now we go down item by item using Shift+Down. In each step, we check that the current item
184 // is added to the selection and that the size of the selection grows by one.
185
186 int current = 1;
187
188 while (current < 100) {
189 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
190 QModelIndex currentIndex = detailsView->model()->index(current, 0);
191 QCOMPARE(detailsView->currentIndex(), currentIndex);
192
193 selectedIndexes = selectionModel->selectedIndexes();
194 QCOMPARE(selectedIndexes.count(), current + 1);
195 QVERIFY(selectedIndexes.contains(currentIndex));
196
197 current++;
198 }
199 }
200
201 /**
202 * When the icon size is changed, we have to make sure that the maximumSize given
203 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
204 * done, the visualRects are clipped by the incorrect maximum size, and the icons
205 * may overlap, see
206 *
207 * https://bugs.kde.org/show_bug.cgi?id=234600
208 */
209
210 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
211 {
212 QStringList files;
213 files << "a" << "b" << "c" << "d";
214
215 TestDir dir;
216 dir.createFiles(files);
217 DolphinView view(dir.url(), 0);
218 DolphinDetailsView* detailsView = initView(&view);
219
220 QModelIndex index0 = detailsView->model()->index(0, 0);
221 detailsView->setCurrentIndex(index0);
222 QCOMPARE(detailsView->currentIndex(), index0);
223
224 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
225 // should make sure that the bug is triggered.
226 int zoomLevelBackup = view.zoomLevel();
227 int zoomLevel = ZoomLevelInfo::minimumLevel();
228 view.setZoomLevel(zoomLevel);
229
230 QModelIndex index1 = detailsView->model()->index(1, 0);
231 detailsView->setCurrentIndex(index1);
232 QCOMPARE(detailsView->currentIndex(), index1);
233
234 // Increase the zoom level successively to the maximum.
235 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
236 zoomLevel++;
237 view.setZoomLevel(zoomLevel);
238 QCOMPARE(view.zoomLevel(), zoomLevel);
239
240 //Check for each zoom level that the height of each item is at least the icon size.
241 QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
242 }
243
244 view.setZoomLevel(zoomLevelBackup);
245 }
246
247 /**
248 * The width of the visualRect of an item is usually replaced by the width of the file name.
249 * However, if the file name is wider then the view's name column, this leads to problems with
250 * keyboard navigation if files with very long names are present in the current folder, see
251 *
252 * https://bugs.kde.org/show_bug.cgi?id=257401
253 *
254 * This test checks that the visualRect of an item is never wider than the "Name" column.
255 */
256
257 void DolphinDetailsViewTest::bug257401_longFilenamesKeyboardNavigation() {
258 TestDir dir;
259 QString name;
260 for (int i = 0; i < 20; i++) {
261 name += "mmmmmmmmmm";
262 dir.createFile(name);
263 }
264 DolphinView view(dir.url(), 0);
265 DolphinDetailsView* detailsView = initView(&view);
266
267 // Select the first item
268 QModelIndex index0 = detailsView->model()->index(0, 0);
269 detailsView->setCurrentIndex(index0);
270 QCOMPARE(detailsView->currentIndex(), index0);
271 QVERIFY(detailsView->visualRect(index0).width() < detailsView->columnWidth(DolphinModel::Name));
272
273 QItemSelectionModel* selectionModel = 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(detailsView->viewport(), Qt::Key_Down, Qt::NoModifier);
282 QModelIndex currentIndex = detailsView->model()->index(i + 1, 0);
283 QCOMPARE(detailsView->currentIndex(), currentIndex);
284 QVERIFY(detailsView->visualRect(currentIndex).width() <= 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"