]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Use the inactive text color for all columns except the name column. This indicates...
[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/dolphindirlister.h"
28 #include "views/dolphinsortfilterproxymodel.h"
29 #include "views/zoomlevelinfo.h"
30
31 #include <KTempDir>
32
33 #include <QtCore/QDir>
34 #include <qtestmouse.h>
35 #include <qtestkeyboard.h>
36
37 class DolphinDetailsViewTest : public TestBase
38 {
39 Q_OBJECT
40
41 private slots:
42
43 void testExpandedUrls();
44
45 void bug217447_shiftArrowSelection();
46 void bug234600_overlappingIconsWhenZooming();
47
48 private:
49
50 QModelIndex proxyModelIndexForUrl(const KUrl& url) const {
51 const QModelIndex index = m_dolphinModel->indexForUrl(url);
52 return m_proxyModel->mapFromSource(index);
53 }
54 };
55
56 /**
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.
60 */
61
62 void DolphinDetailsViewTest::testExpandedUrls()
63 {
64 QStringList files;
65 QStringList subFolderNames;
66 subFolderNames << "a" << "b" << "c";
67
68 foreach(const QString& level1, subFolderNames) {
69 foreach(const QString& level2, subFolderNames) {
70 foreach(const QString& level3, subFolderNames) {
71 files << level1 + "/" + level2 + "/" + level3 + "/testfile";
72 }
73 }
74 }
75
76 createFiles(files);
77
78 m_view->setMode(DolphinView::DetailsView);
79 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
80 QVERIFY(detailsView);
81 detailsView->setFoldersExpandable(true);
82 m_view->resize(400, 400);
83 m_view->show();
84 QTest::qWaitForWindowShown(m_view);
85 reloadViewAndWait();
86
87 // We start with an empty set of expanded URLs.
88 QSet<KUrl> expectedExpandedUrls;
89 QCOMPARE(detailsView->expandedUrls(), expectedExpandedUrls);
90
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&)));
95
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";
99
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);
105
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()) {
109 QTest::qWait(10);
110 }
111 spyFinishedPathLoading.takeFirst();
112 }
113
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";
117
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);
123 }
124
125 m_view->hide();
126 cleanupTestDir();
127 }
128
129 /**
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
133 *
134 * https://bugs.kde.org/show_bug.cgi?id=217447
135 *
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.
143 *
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).
147 */
148
149 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
150 {
151 for (int i = 0; i < 100; i++) {
152 createFile(QString("%1").arg(i));
153 }
154
155 m_view->setMode(DolphinView::DetailsView);
156 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
157 QVERIFY(detailsView);
158 m_view->resize(1000, 400);
159 m_view->show();
160 QTest::qWaitForWindowShown(m_view);
161 reloadViewAndWait();
162
163 // Select the first item
164 QModelIndex index0 = detailsView->model()->index(0, 0);
165 detailsView->setCurrentIndex(index0);
166 QCOMPARE(detailsView->currentIndex(), index0);
167
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);
177
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);
183
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));
188
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.
191
192 int current = 1;
193
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);
198
199 selectedIndexes = selectionModel->selectedIndexes();
200 QCOMPARE(selectedIndexes.count(), current + 1);
201 QVERIFY(selectedIndexes.contains(currentIndex));
202
203 current++;
204 }
205
206 m_view->hide();
207 cleanupTestDir();
208 }
209
210 /**
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
214 * may overlap, see
215 *
216 * https://bugs.kde.org/show_bug.cgi?id=234600
217 */
218
219 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
220 {
221 QStringList files;
222 files << "a" << "b" << "c" << "d";
223 createFiles(files);
224
225 m_view->setMode(DolphinView::DetailsView);
226 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
227 QVERIFY(detailsView);
228 m_view->resize(400, 400);
229 m_view->show();
230 QTest::qWaitForWindowShown(m_view);
231 reloadViewAndWait();
232
233 QModelIndex index0 = detailsView->model()->index(0, 0);
234 detailsView->setCurrentIndex(index0);
235 QCOMPARE(detailsView->currentIndex(), index0);
236
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);
241
242 QModelIndex index1 = detailsView->model()->index(1, 0);
243 detailsView->setCurrentIndex(index1);
244 QCOMPARE(detailsView->currentIndex(), index1);
245
246 // Increase the zoom level successively to the maximum.
247 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
248 zoomLevel++;
249 m_view->setZoomLevel(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 m_view->hide();
256 cleanupTestDir();
257 }
258
259 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
260
261 #include "dolphindetailsviewtest.moc"