2
* Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
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 Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
* Boston, MA 02110-1301 USA
22
#include <edittreeview.h>
23
#include <qstandarditemmodel.h>
25
class tst_EditTreeView : public QObject
31
void cleanupTestCase();
36
void edittreeview_data();
39
void keyPressEvent_data();
44
void removeSelected_data();
45
void removeSelected();
48
// Subclass that exposes the protected functions.
49
class SubEditTreeView : public EditTreeView
54
QStandardItemModel *model = new QStandardItemModel(this);
55
QStandardItem *parentItem = model->invisibleRootItem();
56
for (int i = 0; i < 4; ++i) {
57
QStandardItem *item = 0;
58
for (int j = 0; j < 4; ++j) {
59
item = new QStandardItem(QString("item %0").arg(j));
60
parentItem->appendRow(item);
69
// This will be called before the first test function is executed.
70
// It is only called once.
71
void tst_EditTreeView::initTestCase()
75
// This will be called after the last test function is executed.
76
// It is only called once.
77
void tst_EditTreeView::cleanupTestCase()
81
// This will be called before each test function is executed.
82
void tst_EditTreeView::init()
86
// This will be called after every test function.
87
void tst_EditTreeView::cleanup()
91
void tst_EditTreeView::edittreeview_data()
95
void tst_EditTreeView::edittreeview()
98
QTest::keyClick(&view, Qt::Key_Space);
99
QTest::keyClick(&view, Qt::Key_Delete);
101
view.removeSelected();
104
Q_DECLARE_METATYPE(Qt::Key)
105
void tst_EditTreeView::keyPressEvent_data()
107
QTest::addColumn<Qt::Key>("key");
108
QTest::newRow("space") << Qt::Key_Space;
109
QTest::newRow("delete") << Qt::Key_Delete;
112
// public void keyPressEvent(QKeyEvent *event)
113
void tst_EditTreeView::keyPressEvent()
115
QFETCH(Qt::Key, key);
117
SubEditTreeView view;
120
QModelIndex idx = view.model()->index(0, 0);
121
view.selectionModel()->select(idx, QItemSelectionModel::SelectCurrent);
122
int oldCount = view.model()->rowCount();
123
QTest::keyClick(&view, key);
124
if (key == Qt::Key_Delete)
125
QCOMPARE(oldCount - 1, view.model()->rowCount());
127
QCOMPARE(oldCount, view.model()->rowCount());
130
// public void removeAll()
131
void tst_EditTreeView::removeAll()
133
SubEditTreeView view;
136
QCOMPARE(view.model()->rowCount(), 0);
139
typedef QList<int> IntList;
140
Q_DECLARE_METATYPE(IntList)
141
void tst_EditTreeView::removeSelected_data()
143
QTest::addColumn<int>("selectParent");
144
QTest::addColumn<IntList>("select");
145
QTest::addColumn<bool>("upParent");
146
QTest::addColumn<int>("isSelected");
148
QTest::newRow("first in list") << 3 << (IntList() << 0) << false << 0;
149
QTest::newRow("last in list") << 3 << (IntList() << 3) << false << 2;
150
QTest::newRow("all in list") << 3 << (IntList() << 0 << 1 << 2 << 3) << true << 3;
153
// public void removeSelected()
154
void tst_EditTreeView::removeSelected()
156
QFETCH(int, selectParent);
157
QFETCH(IntList, select);
158
QFETCH(bool, upParent);
159
QFETCH(int, isSelected);
161
SubEditTreeView view;
164
QAbstractItemModel *model = view.model();
166
QModelIndex parent = model->index(selectParent, 0);
167
while (!select.isEmpty()) {
168
QModelIndex idx = model->index(select.takeLast(), 0, parent);
169
view.selectionModel()->select(idx, QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
170
view.setCurrentIndex(idx);
171
view.removeSelected();
175
parent = parent.parent();
177
QModelIndex idx = model->index(isSelected, 0, parent);
179
QCOMPARE(view.currentIndex(), idx);
180
QCOMPARE(view.selectionModel()->selectedRows().count(), 1);
181
QCOMPARE(view.selectionModel()->selectedRows().first(), idx);
184
QTEST_MAIN(tst_EditTreeView)
185
#include "tst_edittreeview.moc"