]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphindetailsviewtest.cpp
Add unit test for bug 201459.
[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 bug217447_shiftArrowSelection();
44 void bug234600_overlappingIconsWhenZooming();
45
46 };
47
48 /**
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
52 *
53 * https://bugs.kde.org/show_bug.cgi?id=217447
54 *
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.
62 *
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).
66 */
67
68 void DolphinDetailsViewTest::bug217447_shiftArrowSelection()
69 {
70 for (int i = 0; i < 100; i++) {
71 createFile(QString("%1").arg(i));
72 }
73
74 m_view->setMode(DolphinView::DetailsView);
75 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
76 QVERIFY(detailsView);
77 m_view->resize(1000, 400);
78 m_view->show();
79 QTest::qWaitForWindowShown(m_view);
80 reloadViewAndWait();
81
82 // Select the first item
83 QModelIndex index0 = detailsView->model()->index(0, 0);
84 detailsView->setCurrentIndex(index0);
85 QCOMPARE(detailsView->currentIndex(), index0);
86
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);
96
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);
102
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));
107
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.
110
111 int current = 1;
112
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);
117
118 selectedIndexes = selectionModel->selectedIndexes();
119 QCOMPARE(selectedIndexes.count(), current + 1);
120 QVERIFY(selectedIndexes.contains(currentIndex));
121
122 current++;
123 }
124
125 m_view->hide();
126 cleanupTestDir();
127 }
128
129 /**
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
133 * may overlap, see
134 *
135 * https://bugs.kde.org/show_bug.cgi?id=234600
136 */
137
138 void DolphinDetailsViewTest::bug234600_overlappingIconsWhenZooming()
139 {
140 QStringList files;
141 files << "a" << "b" << "c" << "d";
142 createFiles(files);
143
144 m_view->setMode(DolphinView::DetailsView);
145 DolphinDetailsView* detailsView = qobject_cast<DolphinDetailsView*>(itemView());
146 QVERIFY(detailsView);
147 m_view->resize(400, 400);
148 m_view->show();
149 QTest::qWaitForWindowShown(m_view);
150 reloadViewAndWait();
151
152 QModelIndex index0 = detailsView->model()->index(0, 0);
153 detailsView->setCurrentIndex(index0);
154 QCOMPARE(detailsView->currentIndex(), index0);
155
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);
160
161 QModelIndex index1 = detailsView->model()->index(1, 0);
162 detailsView->setCurrentIndex(index1);
163 QCOMPARE(detailsView->currentIndex(), index1);
164
165 // Increase the zoom level successively to the maximum.
166 while(zoomLevel < ZoomLevelInfo::maximumLevel()) {
167 zoomLevel++;
168 m_view->setZoomLevel(zoomLevel);
169
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));
172 }
173
174 m_view->hide();
175 cleanupTestDir();
176 }
177
178 QTEST_KDEMAIN(DolphinDetailsViewTest, GUI)
179
180 #include "dolphindetailsviewtest.moc"