~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/benchmarks/gui/itemviews/qtableview/tst_qtableview.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <qtest.h>
 
43
#include <QDebug>
 
44
#include <QTableView>
 
45
#include <QImage>
 
46
#include <QPainter>
 
47
 
 
48
class QtTestTableModel: public QAbstractTableModel
 
49
{
 
50
    Q_OBJECT
 
51
 
 
52
 
 
53
public:
 
54
    QtTestTableModel(int rows = 0, int columns = 0, QObject *parent = 0)
 
55
        : QAbstractTableModel(parent),
 
56
          row_count(rows),
 
57
          column_count(columns) {}
 
58
 
 
59
    int rowCount(const QModelIndex& = QModelIndex()) const { return row_count; }
 
60
    int columnCount(const QModelIndex& = QModelIndex()) const { return column_count; }
 
61
    bool isEditable(const QModelIndex &) const { return true; }
 
62
 
 
63
    QVariant data(const QModelIndex &idx, int role) const
 
64
    {
 
65
        if (!idx.isValid() || idx.row() >= row_count || idx.column() >= column_count) {
 
66
            qWarning() << "Invalid modelIndex [%d,%d,%p]" << idx;
 
67
            return QVariant();
 
68
        }
 
69
 
 
70
        if (role == Qt::DisplayRole || role == Qt::EditRole)
 
71
            return QString("[%1,%2,%3]").arg(idx.row()).arg(idx.column()).arg(0);
 
72
 
 
73
        return QVariant();
 
74
    }
 
75
 
 
76
    bool insertRows(int start, int count, const QModelIndex &parent = QModelIndex())
 
77
    {
 
78
        if (start < 0 || start > row_count)
 
79
            return false;
 
80
 
 
81
        beginInsertRows(parent, start, start + count - 1);
 
82
        row_count += count;
 
83
        endInsertRows();
 
84
        return true;
 
85
    }
 
86
 
 
87
    bool removeRows(int start, int count, const QModelIndex &parent = QModelIndex())
 
88
    {
 
89
        if (start < 0 || start >= row_count || row_count < count)
 
90
            return false;
 
91
 
 
92
        beginRemoveRows(parent, start, start + count - 1);
 
93
        row_count -= count;
 
94
        endRemoveRows();
 
95
        return true;
 
96
    }
 
97
 
 
98
    bool insertColumns(int start, int count, const QModelIndex &parent = QModelIndex())
 
99
    {
 
100
        if (start < 0 || start > column_count)
 
101
            return false;
 
102
 
 
103
        beginInsertColumns(parent, start, start + count - 1);
 
104
        column_count += count;
 
105
        endInsertColumns();
 
106
        return true;
 
107
    }
 
108
 
 
109
    bool removeColumns(int start, int count, const QModelIndex &parent = QModelIndex())
 
110
    {
 
111
        if (start < 0 || start >= column_count || column_count < count)
 
112
            return false;
 
113
 
 
114
        beginRemoveColumns(parent, start, start + count - 1);
 
115
        column_count -= count;
 
116
        endRemoveColumns();
 
117
        return true;
 
118
    }
 
119
 
 
120
    int row_count;
 
121
    int column_count;
 
122
};
 
123
 
 
124
 
 
125
 
 
126
 
 
127
class tst_QTableView : public QObject
 
128
{
 
129
    Q_OBJECT
 
130
 
 
131
public:
 
132
    tst_QTableView();
 
133
    virtual ~tst_QTableView();
 
134
 
 
135
public slots:
 
136
    void init();
 
137
    void cleanup();
 
138
 
 
139
private slots:
 
140
    void spanInit();
 
141
    void spanDraw();
 
142
    void spanSelectColumn();
 
143
    void spanSelectAll();
 
144
    void rowInsertion_data();
 
145
    void rowInsertion();
 
146
    void rowRemoval_data();
 
147
    void rowRemoval();
 
148
    void columnInsertion_data();
 
149
    void columnInsertion();
 
150
    void columnRemoval_data();
 
151
    void columnRemoval();
 
152
private:
 
153
    static inline void spanInit_helper(QTableView *);
 
154
};
 
155
 
 
156
tst_QTableView::tst_QTableView()
 
157
{
 
158
}
 
159
 
 
160
tst_QTableView::~tst_QTableView()
 
161
{
 
162
}
 
163
 
 
164
void tst_QTableView::init()
 
165
{
 
166
}
 
167
 
 
168
void tst_QTableView::cleanup()
 
169
{
 
170
}
 
171
 
 
172
void tst_QTableView::spanInit_helper(QTableView *view)
 
173
{
 
174
    for (int i=0; i < 40; i++) {
 
175
        view->setSpan(1+i%2, 1+4*i, 1+i%3, 2);
 
176
    }
 
177
    
 
178
    for (int i=1; i < 40; i++) {
 
179
        view->setSpan(6 + i*7, 4, 4, 50);
 
180
    }
 
181
}
 
182
 
 
183
void tst_QTableView::spanInit()
 
184
{
 
185
    QtTestTableModel model(500, 500);
 
186
    QTableView v;
 
187
    v.setModel(&model);
 
188
    
 
189
    QBENCHMARK {
 
190
        spanInit_helper(&v);
 
191
    }
 
192
}
 
193
 
 
194
void tst_QTableView::spanDraw()
 
195
{
 
196
    QtTestTableModel model(500, 500);
 
197
    QTableView v;
 
198
    v.setModel(&model);
 
199
    
 
200
    spanInit_helper(&v);
 
201
    v.show();
 
202
    v.resize(500,500);
 
203
    QTest::qWait(30);
 
204
 
 
205
    QImage image(500, 500, QImage::Format_ARGB32_Premultiplied);
 
206
    QPainter painter(&image);
 
207
    QBENCHMARK {
 
208
        v.render(&painter);
 
209
    }
 
210
}
 
211
 
 
212
void tst_QTableView::spanSelectAll()
 
213
{
 
214
    QtTestTableModel model(500, 500);
 
215
    QTableView v;
 
216
    v.setModel(&model);
 
217
    
 
218
    spanInit_helper(&v);
 
219
    v.show();
 
220
    QTest::qWait(30);
 
221
    
 
222
    QBENCHMARK {
 
223
        v.selectAll();
 
224
    }
 
225
}
 
226
 
 
227
void tst_QTableView::spanSelectColumn()
 
228
{
 
229
    QtTestTableModel model(500, 500);
 
230
    QTableView v;
 
231
    v.setModel(&model);
 
232
    
 
233
    spanInit_helper(&v);
 
234
    v.show();
 
235
    QTest::qWait(30);
 
236
    
 
237
    QBENCHMARK {
 
238
        v.selectColumn(22);
 
239
    }
 
240
}
 
241
 
 
242
typedef QVector<QRect> SpanList;
 
243
Q_DECLARE_METATYPE(SpanList)
 
244
 
 
245
void spansData()
 
246
{
 
247
    QTest::addColumn<SpanList>("spans");
 
248
 
 
249
    QTest::newRow("Without spans")
 
250
        << SpanList();
 
251
 
 
252
    QTest::newRow("With spans")
 
253
            << (SpanList()
 
254
                  << QRect(0, 1, 1, 2)
 
255
                  << QRect(1, 2, 1, 2)
 
256
                  << QRect(2, 2, 1, 5)
 
257
                  << QRect(2, 8, 1, 2)
 
258
                  << QRect(3, 4, 1, 2)
 
259
                  << QRect(4, 4, 1, 4)
 
260
                  << QRect(5, 6, 1, 3)
 
261
                  << QRect(6, 7, 1, 3));
 
262
}
 
263
 
 
264
void tst_QTableView::rowInsertion_data()
 
265
{
 
266
    spansData();
 
267
}
 
268
 
 
269
void tst_QTableView::rowInsertion()
 
270
{
 
271
    QFETCH(SpanList, spans);
 
272
 
 
273
    QtTestTableModel model(10, 10);
 
274
    QTableView view;
 
275
    view.setModel(&model);
 
276
 
 
277
    foreach (QRect span, spans)
 
278
        view.setSpan(span.top(), span.left(), span.height(), span.width());
 
279
    view.show();
 
280
    QTest::qWait(50);
 
281
 
 
282
    QBENCHMARK_ONCE {
 
283
        view.model()->insertRows(0, 2);
 
284
        view.model()->insertRows(5, 2);
 
285
        view.model()->insertRows(8, 2);
 
286
        view.model()->insertRows(12, 2);
 
287
    }
 
288
}
 
289
 
 
290
void tst_QTableView::rowRemoval_data()
 
291
{
 
292
    spansData();
 
293
}
 
294
 
 
295
void tst_QTableView::rowRemoval()
 
296
{
 
297
    QFETCH(SpanList, spans);
 
298
 
 
299
    QtTestTableModel model(10, 10);
 
300
    QTableView view;
 
301
    view.setModel(&model);
 
302
 
 
303
    foreach (QRect span, spans)
 
304
        view.setSpan(span.top(), span.left(), span.height(), span.width());
 
305
    view.show();
 
306
    QTest::qWait(50);
 
307
 
 
308
    QBENCHMARK_ONCE {
 
309
        view.model()->removeRows(3, 3);
 
310
    }
 
311
}
 
312
 
 
313
void tst_QTableView::columnInsertion_data()
 
314
{
 
315
    spansData();
 
316
}
 
317
 
 
318
void tst_QTableView::columnInsertion()
 
319
{
 
320
    QFETCH(SpanList, spans);
 
321
 
 
322
    QtTestTableModel model(10, 10);
 
323
    QTableView view;
 
324
    view.setModel(&model);
 
325
 
 
326
    // Same set as for rowInsertion, just swapping columns and rows.
 
327
    foreach (QRect span, spans)
 
328
        view.setSpan(span.left(), span.top(), span.width(), span.height());
 
329
    view.show();
 
330
    QTest::qWait(50);
 
331
 
 
332
    QBENCHMARK_ONCE {
 
333
        view.model()->insertColumns(0, 2);
 
334
        view.model()->insertColumns(5, 2);
 
335
        view.model()->insertColumns(8, 2);
 
336
        view.model()->insertColumns(12, 2);
 
337
    }
 
338
}
 
339
 
 
340
void tst_QTableView::columnRemoval_data()
 
341
{
 
342
    spansData();
 
343
}
 
344
 
 
345
void tst_QTableView::columnRemoval()
 
346
{
 
347
    QFETCH(SpanList, spans);
 
348
 
 
349
    QtTestTableModel model(10, 10);
 
350
    QTableView view;
 
351
    view.setModel(&model);
 
352
 
 
353
    // Same set as for rowRemoval, just swapping columns and rows.
 
354
    foreach (QRect span, spans)
 
355
        view.setSpan(span.left(), span.top(), span.width(), span.height());
 
356
    view.show();
 
357
    QTest::qWait(50);
 
358
 
 
359
    QBENCHMARK_ONCE {
 
360
        view.model()->removeColumns(3, 3);
 
361
    }
 
362
}
 
363
 
 
364
QTEST_MAIN(tst_QTableView)
 
365
#include "tst_qtableview.moc"