]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Add unit test for bug 217447.
[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 #include "kdebug.h"
38
39 class DolphinDetailsViewTest : public TestBase
40 {
41 Q_OBJECT
42
43 private slots:
44
45 void initTestCase();
46
47 void bug217447_shiftArrowSelection();
48 void bug234600_overlappingIconsWhenZooming();
49
50 };
51
52 void DolphinDetailsViewTest::initTestCase()
53 {
54 // add time stamps to find origin of test failures due to timeout at
55 // http://my.cdash.org/index.php?project=kdebase&date=
56 qputenv("KDE_DEBUG_TIMESTAMP", QByteArray("1"));
57 }
58
59 /**
60 * When the first item in the view is active and Shift is held while the "arrow down"
61 * key is pressed repeatedly, the selection should grow by one item for each key press.
62 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
63 *
64 * https://bugs.kde.org/show_bug.cgi?id=217447
65 *
66 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
67 * column for an item, but only the width of the actual file name, did not reimplement
68 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
69 * uses the center of the visualRect of an item internally. If the width of the file name
70 * is less than half the width of the "Name" column, the center of an item's visualRect
71 * was therefore outside the space that DolphinDetailsView actually assigned to the
72 * item, and this led to unexpected deselection of items.
73 *
74 * TODO: To make the test more reliable, one could adjust the width of the "Name"
75 * column before the test in order to really make sure that the column is more than twice
76 * as wide as the space actually occupied by the file names (this triggers the bug).
77 */
78
79 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
80 {
81 for (int i = 0; i < 100; i++) {
82 createFile(QString("%1").arg(i));
83 }
84
85 m_view->setMode(DolphinView::DetailsView);
86 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
87 QVERIFY(detailsView);
88 m_view->resize(1000, 400);
89 m_view->show();
90 QTest::qWaitForWindowShown(m_view);
91
92 // We have to make sure that the view has loaded the directory before we start the test.
93 // TODO: This will be needed frequently. Maybe move to TestHelper.
94 QSignalSpy finished(m_view, SIGNAL(finishedPathLoading(const KUrl&)));
95 m_view->reload();
96 while (finished.count() != 1) {
97 QTest::qWait(50);
98 }
99
100 // Select the first item
101 QModelIndex index0 = detailsView->model()->index(0, 0);
102 detailsView->setCurrentIndex(index0);
103 QCOMPARE(detailsView->currentIndex(), index0);
104
105 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
106 // directly: we check that passing the corners or the center of an item's visualRect
107 // to itemAt() returns the item (and not an invalid model index).
108 QRect rect = detailsView->visualRect(index0);
109 QCOMPARE(detailsView->indexAt(rect.center()), index0);
110 QCOMPARE(detailsView->indexAt(rect.topLeft()), index0);
111 QCOMPARE(detailsView->indexAt(rect.topRight()), index0);
112 QCOMPARE(detailsView->indexAt(rect.bottomLeft()), index0);
113 QCOMPARE(detailsView->indexAt(rect.bottomRight()), index0);
114
115 // Another way to test this is to Ctrl-click the center of the visualRect.
116 // The selection state of the item should be toggled.
117 detailsView->clearSelection();
118 QItemSelectionModel* selectionModel = detailsView->selectionModel();
119 QCOMPARE(selectionModel->selectedIndexes().count(), 0);
120
121 QTest::mouseClick(detailsView->viewport(), Qt::LeftButton, Qt::ControlModifier, rect.center());
122 QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
123 QCOMPARE(selectedIndexes.count(), 1);
124 QVERIFY(selectedIndexes.contains(index0));
125
126 // Now we go down item by item using Shift+Down. In each step, we check that the current item
127 // is added to the selection and that the size of the selection grows by one.
128
129 int current = 1;
130
131 while (current < 100) {
132 QTest::keyClick(detailsView->viewport(), Qt::Key_Down, Qt::ShiftModifier);
133 QModelIndex currentIndex = detailsView->model()->index(current, 0);
134 QCOMPARE(detailsView->currentIndex(), currentIndex);
135
136 selectedIndexes = selectionModel->selectedIndexes();
137 QCOMPARE(selectedIndexes.count(), current + 1);
138 QVERIFY(selectedIndexes.contains(currentIndex));
139
140 current++;
141 }
142
143 m_view->hide();
144 cleanupTestDir();
145 }
146
147 /**
148 * When the icon size is changed, we have to make sure that the maximumSize given
149 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
150 * done, the visualRects are clipped by the incorrect maximum size, and the icons
151 * may overlap, see
152 *
153 * https://bugs.kde.org/show_bug.cgi?id=234600
154 */
155
156 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
157 {
158 QStringList files;
159 files << "a" << "b" << "c" << "d";
160 createFiles(files);
161
162 m_view->setMode(DolphinView::DetailsView);
163 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
164 QVERIFY(detailsView);
165 m_view->resize(400, 400);
166 m_view->show();
167 QTest::qWaitForWindowShown(m_view);
168
169 // We have to make sure that the view has loaded the directory before we start the test.
170 // TODO: This will be needed frequently. Maybe move to TestHelper.
171 kDebug() << "Reloading view and waiting for the finishedPathLoading(const KUrl&) signal...";
172 QSignalSpy finished(m_view, SIGNAL(finishedPathLoading(const KUrl&)));
173 m_view->reload();
174 while (finished.count() != 1) {
175 QTest::qWait(50);
176 }
177 kDebug() << "...signal received, continuing";
178
179 QModelIndex index0 = detailsView->model()->index(0, 0);
180 detailsView->setCurrentIndex(index0);
181 QCOMPARE(detailsView->currentIndex(), index0);
182
183 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
184 // should make sure that the bug is triggered.
185 int zoomLevel = ZoomLevelInfo::minimumLevel();
186 m_view->setZoomLevel(zoomLevel);
187
188 QModelIndex index1 = detailsView->model()->index(1, 0);
189 detailsView->setCurrentIndex(index1);
190 QCOMPARE(detailsView->currentIndex(), index1);
191
192 kDebug() << "Now checking zoom levels...";
193
194 // Increase the zoom level successively to the maximum.
195 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
196 zoomLevel++;
197 kDebug() << "Testing zoom level" << zoomLevel;
198 m_view->setZoomLevel(zoomLevel);
199
200 //Check for each zoom level that the height of each item is at least the icon size.
201 QVERIFY(detailsView->visualRect(index1).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel));
202 }
203
204 m_view->hide();
205
206 kDebug() << "Cleaning up test directory...";
207 cleanupTestDir();
208 kDebug() << "Done.";
209 }
210
211 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
212
213 #include "dolphindetailsviewtest.moc"