~ubuntu-branches/ubuntu/quantal/arora/quantal

« back to all changes in this revision

Viewing changes to autotests/utils/edittreeview/tst_edittreeview.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Roderick B. Greening
  • Date: 2009-10-01 16:08:58 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20091001160858-h1wnt3ddyzl07nih
Tags: 0.10.0-0ubuntu1
* New upstream release 
* Remove patches
  - kubuntu_01_google_lucky.diff - Open Search now used upstream
  - kubuntu_02_default_bookmarks.diff - bookmarks fixed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2008 Benjamin C. Meyer <ben@meyerhome.net>
 
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 Free Software
 
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include <qtest.h>
 
21
 
 
22
#include <edittreeview.h>
 
23
#include <qstandarditemmodel.h>
 
24
 
 
25
class tst_EditTreeView : public QObject
 
26
{
 
27
    Q_OBJECT
 
28
 
 
29
public slots:
 
30
    void initTestCase();
 
31
    void cleanupTestCase();
 
32
    void init();
 
33
    void cleanup();
 
34
 
 
35
private slots:
 
36
    void edittreeview_data();
 
37
    void edittreeview();
 
38
 
 
39
    void keyPressEvent_data();
 
40
    void keyPressEvent();
 
41
 
 
42
    void removeAll();
 
43
 
 
44
    void removeSelected_data();
 
45
    void removeSelected();
 
46
};
 
47
 
 
48
// Subclass that exposes the protected functions.
 
49
class SubEditTreeView : public EditTreeView
 
50
{
 
51
 
 
52
public:
 
53
    void addModel() {
 
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);
 
61
            }
 
62
            parentItem = item;
 
63
        }
 
64
        setModel(model);
 
65
    }
 
66
 
 
67
};
 
68
 
 
69
// This will be called before the first test function is executed.
 
70
// It is only called once.
 
71
void tst_EditTreeView::initTestCase()
 
72
{
 
73
}
 
74
 
 
75
// This will be called after the last test function is executed.
 
76
// It is only called once.
 
77
void tst_EditTreeView::cleanupTestCase()
 
78
{
 
79
}
 
80
 
 
81
// This will be called before each test function is executed.
 
82
void tst_EditTreeView::init()
 
83
{
 
84
}
 
85
 
 
86
// This will be called after every test function.
 
87
void tst_EditTreeView::cleanup()
 
88
{
 
89
}
 
90
 
 
91
void tst_EditTreeView::edittreeview_data()
 
92
{
 
93
}
 
94
 
 
95
void tst_EditTreeView::edittreeview()
 
96
{
 
97
    SubEditTreeView view;
 
98
    QTest::keyClick(&view, Qt::Key_Space);
 
99
    QTest::keyClick(&view, Qt::Key_Delete);
 
100
    view.removeAll();
 
101
    view.removeSelected();
 
102
}
 
103
 
 
104
Q_DECLARE_METATYPE(Qt::Key)
 
105
void tst_EditTreeView::keyPressEvent_data()
 
106
{
 
107
    QTest::addColumn<Qt::Key>("key");
 
108
    QTest::newRow("space") << Qt::Key_Space;
 
109
    QTest::newRow("delete") << Qt::Key_Delete;
 
110
}
 
111
 
 
112
// public void keyPressEvent(QKeyEvent *event)
 
113
void tst_EditTreeView::keyPressEvent()
 
114
{
 
115
    QFETCH(Qt::Key, key);
 
116
 
 
117
    SubEditTreeView view;
 
118
    view.addModel();
 
119
 
 
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());
 
126
    else
 
127
        QCOMPARE(oldCount, view.model()->rowCount());
 
128
}
 
129
 
 
130
// public void removeAll()
 
131
void tst_EditTreeView::removeAll()
 
132
{
 
133
    SubEditTreeView view;
 
134
    view.addModel();
 
135
    view.removeAll();
 
136
    QCOMPARE(view.model()->rowCount(), 0);
 
137
}
 
138
 
 
139
typedef QList<int> IntList;
 
140
Q_DECLARE_METATYPE(IntList)
 
141
void tst_EditTreeView::removeSelected_data()
 
142
{
 
143
    QTest::addColumn<int>("selectParent");
 
144
    QTest::addColumn<IntList>("select");
 
145
    QTest::addColumn<bool>("upParent");
 
146
    QTest::addColumn<int>("isSelected");
 
147
 
 
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;
 
151
}
 
152
 
 
153
// public void removeSelected()
 
154
void tst_EditTreeView::removeSelected()
 
155
{
 
156
    QFETCH(int, selectParent);
 
157
    QFETCH(IntList, select);
 
158
    QFETCH(bool, upParent);
 
159
    QFETCH(int, isSelected);
 
160
 
 
161
    SubEditTreeView view;
 
162
    view.show();
 
163
    view.addModel();
 
164
    QAbstractItemModel *model = view.model();
 
165
    view.expandAll();
 
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();
 
172
    }
 
173
 
 
174
    if (upParent)
 
175
        parent = parent.parent();
 
176
 
 
177
    QModelIndex idx = model->index(isSelected, 0, parent);
 
178
 
 
179
    QCOMPARE(view.currentIndex(), idx);
 
180
    QCOMPARE(view.selectionModel()->selectedRows().count(), 1);
 
181
    QCOMPARE(view.selectionModel()->selectedRows().first(), idx);
 
182
}
 
183
 
 
184
QTEST_MAIN(tst_EditTreeView)
 
185
#include "tst_edittreeview.moc"
 
186