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>
37 class DolphinDetailsViewTest
: public TestBase
43 void bug217447_shiftArrowSelection();
44 void bug234600_overlappingIconsWhenZooming();
49 * When the first item in the view is active and Shift is held while the "arrow down"
50 * key is pressed repeatedly, the selection should grow by one item for each key press.
51 * A change in Qt 4.6 revealed a bug in DolphinDetailsView which broke this, see
53 * https://bugs.kde.org/show_bug.cgi?id=217447
55 * The problem was that DolphinDetailsView, which uses not the full width of the "Name"
56 * column for an item, but only the width of the actual file name, did not reimplement
57 * QTreeView::visualRect(). This caused item selection to fail because QAbstractItemView
58 * uses the center of the visualRect of an item internally. If the width of the file name
59 * is less than half the width of the "Name" column, the center of an item's visualRect
60 * was therefore outside the space that DolphinDetailsView actually assigned to the
61 * item, and this led to unexpected deselection of items.
63 * TODO: To make the test more reliable, one could adjust the width of the "Name"
64 * column before the test in order to really make sure that the column is more than twice
65 * as wide as the space actually occupied by the file names (this triggers the bug).
68 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
70 for (int i
= 0; i
< 100; i
++) {
71 createFile(QString("%1").arg(i
));
74 m_view
->setMode(DolphinView::DetailsView
);
75 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
77 m_view
->resize(1000, 400);
79 QTest::qWaitForWindowShown(m_view
);
82 // Select the first item
83 QModelIndex index0
= detailsView
->model()->index(0, 0);
84 detailsView
->setCurrentIndex(index0
);
85 QCOMPARE(detailsView
->currentIndex(), index0
);
87 // Before we test Shift-selection, we verify that the root cause is fixed a bit more
88 // directly: we check that passing the corners or the center of an item's visualRect
89 // to itemAt() returns the item (and not an invalid model index).
90 QRect rect
= detailsView
->visualRect(index0
);
91 QCOMPARE(detailsView
->indexAt(rect
.center()), index0
);
92 QCOMPARE(detailsView
->indexAt(rect
.topLeft()), index0
);
93 QCOMPARE(detailsView
->indexAt(rect
.topRight()), index0
);
94 QCOMPARE(detailsView
->indexAt(rect
.bottomLeft()), index0
);
95 QCOMPARE(detailsView
->indexAt(rect
.bottomRight()), index0
);
97 // Another way to test this is to Ctrl-click the center of the visualRect.
98 // The selection state of the item should be toggled.
99 detailsView
->clearSelection();
100 QItemSelectionModel
* selectionModel
= detailsView
->selectionModel();
101 QCOMPARE(selectionModel
->selectedIndexes().count(), 0);
103 QTest::mouseClick(detailsView
->viewport(), Qt::LeftButton
, Qt::ControlModifier
, rect
.center());
104 QModelIndexList selectedIndexes
= selectionModel
->selectedIndexes();
105 QCOMPARE(selectedIndexes
.count(), 1);
106 QVERIFY(selectedIndexes
.contains(index0
));
108 // Now we go down item by item using Shift+Down. In each step, we check that the current item
109 // is added to the selection and that the size of the selection grows by one.
113 while (current
< 100) {
114 QTest::keyClick(detailsView
->viewport(), Qt::Key_Down
, Qt::ShiftModifier
);
115 QModelIndex currentIndex
= detailsView
->model()->index(current
, 0);
116 QCOMPARE(detailsView
->currentIndex(), currentIndex
);
118 selectedIndexes
= selectionModel
->selectedIndexes();
119 QCOMPARE(selectedIndexes
.count(), current
+ 1);
120 QVERIFY(selectedIndexes
.contains(currentIndex
));
130 * When the icon size is changed, we have to make sure that the maximumSize given
131 * to KFileItemDelegate for rendering each item is updated correctly. If this is not
132 * done, the visualRects are clipped by the incorrect maximum size, and the icons
135 * https://bugs.kde.org/show_bug.cgi?id=234600
138 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
141 files
<< "a" << "b" << "c" << "d";
144 m_view
->setMode(DolphinView::DetailsView
);
145 DolphinDetailsView
* detailsView
= qobject_cast
<DolphinDetailsView
*>(itemView());
146 QVERIFY(detailsView
);
147 m_view
->resize(400, 400);
149 QTest::qWaitForWindowShown(m_view
);
152 QModelIndex index0
= detailsView
->model()->index(0, 0);
153 detailsView
->setCurrentIndex(index0
);
154 QCOMPARE(detailsView
->currentIndex(), index0
);
156 // Setting the zoom level to the minimum value and triggering DolphinDetailsView::currentChanged(...)
157 // should make sure that the bug is triggered.
158 int zoomLevel
= ZoomLevelInfo::minimumLevel();
159 m_view
->setZoomLevel(zoomLevel
);
161 QModelIndex index1
= detailsView
->model()->index(1, 0);
162 detailsView
->setCurrentIndex(index1
);
163 QCOMPARE(detailsView
->currentIndex(), index1
);
165 // Increase the zoom level successively to the maximum.
166 while(zoomLevel
< ZoomLevelInfo::maximumLevel()) {
168 m_view
->setZoomLevel(zoomLevel
);
170 //Check for each zoom level that the height of each item is at least the icon size.
171 QVERIFY(detailsView
->visualRect(index1
).height() >= ZoomLevelInfo::iconSizeForZoomLevel(zoomLevel
));
178 QTEST_KDEMAIN(DolphinDetailsViewTest
, GUI
)
180 #include "dolphindetailsviewtest.moc"