1 /***************************************************************************
2 * Copyright (C) 2010 by Frank Reininghaus (frank78ac@googlemail.com) *
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. *
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. *
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 ***************************************************************************/
20 #include <qtest_kde.h>
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"
33 #include <QtCore/QDir>
34 #include <qtestmouse.h>
35 #include <qtestkeyboard.h>
39 class DolphinDetailsViewTest
: public TestBase
47 void bug217447_shiftArrowSelection();
48 void bug234600_overlappingIconsWhenZooming();
52 void DolphinDetailsViewTest::initTestCase()
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"));
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
64 * https://bugs.kde.org/show_bug.cgi?id=217447
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.
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).
79 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
81 for (int i
= 0; i
< 100; i
++) {
82 createFile(QString("%1").arg(i
));
85 m_view
->setMode(DolphinView::DetailsView
);
86 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
88 m_view
->resize(1000, 400);
90 QTest::qWaitForWindowShown(m_view
);
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
&)));
96 while (finished
.count() != 1) {
100 // Select the first item
101 QModelIndex index0
= detailsView
->model()->index(0, 0);
102 detailsView
->setCurrentIndex(index0
);
103 QCOMPARE(detailsView
->currentIndex(), index0
);
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
);
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);
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
));
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.
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
);
136 selectedIndexes
= selectionModel
->selectedIndexes();
137 QCOMPARE(selectedIndexes
.count(), current
+ 1);
138 QVERIFY(selectedIndexes
.contains(currentIndex
));
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
153 * https://bugs.kde.org/show_bug.cgi?id=234600
156 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
159 files
<< "a" << "b" << "c" << "d";
162 m_view
->setMode(DolphinView::DetailsView
);
163 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
164 QVERIFY(detailsView
);
165 m_view
->resize(400, 400);
167 QTest::qWaitForWindowShown(m_view
);
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
&)));
174 while (finished
.count() != 1) {
177 kDebug() << "...signal received, continuing";
179 QModelIndex index0
= detailsView
->model()->index(0, 0);
180 detailsView
->setCurrentIndex(index0
);
181 QCOMPARE(detailsView
->currentIndex(), index0
);
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
);
188 QModelIndex index1
= detailsView
->model()->index(1, 0);
189 detailsView
->setCurrentIndex(index1
);
190 QCOMPARE(detailsView
->currentIndex(), index1
);
192 kDebug() << "Now checking zoom levels...";
194 // Increase the zoom level successively to the maximum.
195 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
197 kDebug() << "Testing zoom level" << zoomLevel
;
198 m_view
->setZoomLevel(zoomLevel
);
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
));
206 kDebug() << "Cleaning up test directory...";
211 QTEST_KDEMAIN(DolphinDetailsViewTest
, GUI
)
213 #include "dolphindetailsviewtest.moc"