]> cloud.milkyroute.net Git - dolphin.git/blob - src/tests/dolphintreeviewtest.cpp
Add a first unit test for Dolphin, which verifies that the fix for one
[dolphin.git] / src / tests / dolphintreeviewtest.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 "views/dolphintreeview.h"
23
24 #include <qtestkeyboard.h>
25 #include <QtGui/QStringListModel>
26
27 class DolphinTreeViewTest : public QObject
28 {
29 Q_OBJECT
30
31 private slots:
32
33 void bug218114_visualRegionForSelection();
34
35 };
36
37 /**
38 * TestView is a simple view class derived from DolphinTreeView.
39 * It makes sure that the visualRect for each index contains only the item text as
40 * returned by QAbstractItemModel::data(...) for the role Qt::DisplayRole.
41 *
42 * We have to check that DolphinTreeView handles the case of visualRects with different widths
43 * correctly because this is the case in DolphinDetailsView which is derived from DolphinTreeView.
44 */
45
46 class TestView : public DolphinTreeView
47 {
48 Q_OBJECT
49
50 public:
51
52 TestView(QWidget* parent = 0) : DolphinTreeView(parent) {};
53 ~TestView() {};
54
55 QRect visualRect(const QModelIndex& index) const {
56 QRect rect = DolphinTreeView::visualRect(index);
57
58 const QStyleOptionViewItem option = viewOptions();
59 const QFontMetrics fontMetrics(option.font);
60 int width = option.decorationSize.width() + fontMetrics.width(model()->data(index).toString());
61
62 rect.setWidth(width);
63 return rect;
64 }
65
66 };
67
68 /**
69 * QTreeView assumes implicitly that the width of each item's visualRect is the same. This leads to painting
70 * problems in Dolphin if items with different widths are in one QItemSelectionRange, see
71 *
72 * https://bugs.kde.org/show_bug.cgi?id=218114
73 *
74 * To fix this, DolphinTreeView has a custom implementation of visualRegionForSelection(). The following
75 * unit test checks that.
76 */
77
78 void DolphinTreeViewTest::bug218114_visualRegionForSelection()
79 {
80 QStringList items;
81 items << "a" << "an item with a long name" << "a";
82 QStringListModel model(items);
83
84 QModelIndex index0 = model.index(0, 0);
85 QModelIndex index1 = model.index(1, 0);
86 QModelIndex index2 = model.index(2, 0);
87
88 TestView view;
89 view.setModel(&model);
90 view.setSelectionMode(QAbstractItemView::ExtendedSelection);
91 view.resize(400, 400);
92 view.show();
93 QTest::qWaitForWindowShown(&view);
94
95 // First check that the width of index1 is larger than that of index0 and index2 (this triggers the bug).
96
97 QVERIFY(view.visualRect(index0).width() < view.visualRect(index1).width());
98 QVERIFY(view.visualRect(index2).width() < view.visualRect(index1).width());
99
100 // Select all items in one go.
101
102 view.selectAll();
103 const QItemSelection selection = view.selectionModel()->selection();
104 QCOMPARE(selection.count(), 1);
105 QCOMPARE(selection.indexes().count(), 3);
106
107 // Verify that the visualRegionForSelection contains all visualRects.
108 // We do this indirectly using QRegion::boundingRect() because
109 // QRegion::contains(const QRect&) returns true even if the QRect is not
110 // entirely inside the QRegion.
111
112 const QRegion region = view.visualRegionForSelection(selection);
113 const QRect boundingRect = region.boundingRect();
114
115 QVERIFY(boundingRect.contains(view.visualRect(index0)));
116 QVERIFY(boundingRect.contains(view.visualRect(index1)));
117 QVERIFY(boundingRect.contains(view.visualRect(index2)));
118 }
119
120 QTEST_KDEMAIN(DolphinTreeViewTest, GUI)
121
122 #include "dolphintreeviewtest.moc"