~saviq/ubuntu/saucy/qtdeclarative-opensource-src/add-qtquick-delegate-range

« back to all changes in this revision

Viewing changes to tests/auto/quick/shared/viewtestutil.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 14:17:19 UTC
  • Revision ID: package-import@ubuntu.com-20130205141719-qqeyml8wslpyez52
Tags: upstream-5.0.1
ImportĀ upstreamĀ versionĀ 5.0.1

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 "viewtestutil.h"
 
43
 
 
44
#include <QtQuick/QQuickView>
 
45
 
 
46
#include <QtTest/QTest>
 
47
 
 
48
QQuickView *QQuickViewTestUtil::createView()
 
49
{
 
50
    QQuickView *window = new QQuickView(0);
 
51
    window->setGeometry(0,0,240,320);
 
52
 
 
53
    return window;
 
54
}
 
55
 
 
56
void QQuickViewTestUtil::flick(QQuickView *window, const QPoint &from, const QPoint &to, int duration)
 
57
{
 
58
    const int pointCount = 5;
 
59
    QPoint diff = to - from;
 
60
 
 
61
    // send press, five equally spaced moves, and release.
 
62
    QTest::mousePress(window, Qt::LeftButton, 0, from);
 
63
 
 
64
    for (int i = 0; i < pointCount; ++i)
 
65
        QTest::mouseMove(window, from + (i+1)*diff/pointCount, duration / pointCount);
 
66
 
 
67
    QTest::mouseRelease(window, Qt::LeftButton, 0, to);
 
68
    QTest::qWait(50);
 
69
}
 
70
 
 
71
QList<int> QQuickViewTestUtil::adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count)
 
72
{
 
73
    QList<int> result;
 
74
    for (int i=0; i<indexes.count(); i++) {
 
75
        int num = indexes[i];
 
76
        if (num >= index) {
 
77
            num += count;
 
78
        }
 
79
        result << num;
 
80
    }
 
81
    return result;
 
82
}
 
83
 
 
84
QList<int> QQuickViewTestUtil::adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count)
 
85
{
 
86
    QList<int> result;
 
87
    for (int i=0; i<indexes.count(); i++) {
 
88
        int num = indexes[i];
 
89
        if (from < to) {
 
90
            if (num >= from && num < from + count)
 
91
                num += (to - from); // target
 
92
            else if (num >= from && num < to + count)
 
93
                num -= count;   // displaced
 
94
        } else if (from > to) {
 
95
            if (num >= from && num < from + count)
 
96
                num -= (from - to);  // target
 
97
            else if (num >= to && num < from + count)
 
98
                num += count;   // displaced
 
99
        }
 
100
        result << num;
 
101
    }
 
102
    return result;
 
103
}
 
104
 
 
105
QList<int> QQuickViewTestUtil::adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count)
 
106
{
 
107
    QList<int> result;
 
108
    for (int i=0; i<indexes.count(); i++) {
 
109
        int num = indexes[i];
 
110
        if (num >= index)
 
111
            num -= count;
 
112
        result << num;
 
113
    }
 
114
    return result;
 
115
}
 
116
 
 
117
QQuickViewTestUtil::QaimModel::QaimModel(QObject *parent)
 
118
    : QAbstractListModel(parent)
 
119
{
 
120
    QHash<int, QByteArray> roles;
 
121
    roles[Name] = "name";
 
122
    roles[Number] = "number";
 
123
    setRoleNames(roles);
 
124
}
 
125
 
 
126
int QQuickViewTestUtil::QaimModel::rowCount(const QModelIndex &parent) const
 
127
{
 
128
    Q_UNUSED(parent);
 
129
    return list.count();
 
130
}
 
131
 
 
132
QVariant QQuickViewTestUtil::QaimModel::data(const QModelIndex &index, int role) const
 
133
{
 
134
    QVariant rv;
 
135
    if (role == Name)
 
136
        rv = list.at(index.row()).first;
 
137
    else if (role == Number)
 
138
        rv = list.at(index.row()).second;
 
139
 
 
140
    return rv;
 
141
}
 
142
 
 
143
int QQuickViewTestUtil::QaimModel::count() const
 
144
{
 
145
    return rowCount();
 
146
}
 
147
 
 
148
QString QQuickViewTestUtil::QaimModel::name(int index) const
 
149
{
 
150
    return list.at(index).first;
 
151
}
 
152
 
 
153
QString QQuickViewTestUtil::QaimModel::number(int index) const
 
154
{
 
155
    return list.at(index).second;
 
156
}
 
157
 
 
158
void QQuickViewTestUtil::QaimModel::addItem(const QString &name, const QString &number)
 
159
{
 
160
    emit beginInsertRows(QModelIndex(), list.count(), list.count());
 
161
    list.append(QPair<QString,QString>(name, number));
 
162
    emit endInsertRows();
 
163
}
 
164
 
 
165
void QQuickViewTestUtil::QaimModel::addItems(const QList<QPair<QString, QString> > &items)
 
166
{
 
167
    emit beginInsertRows(QModelIndex(), list.count(), list.count()+items.count()-1);
 
168
    for (int i=0; i<items.count(); i++)
 
169
        list.append(QPair<QString,QString>(items[i].first, items[i].second));
 
170
    emit endInsertRows();
 
171
}
 
172
 
 
173
void QQuickViewTestUtil::QaimModel::insertItem(int index, const QString &name, const QString &number)
 
174
{
 
175
    emit beginInsertRows(QModelIndex(), index, index);
 
176
    list.insert(index, QPair<QString,QString>(name, number));
 
177
    emit endInsertRows();
 
178
}
 
179
 
 
180
void QQuickViewTestUtil::QaimModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
 
181
{
 
182
    emit beginInsertRows(QModelIndex(), index, index+items.count()-1);
 
183
    for (int i=0; i<items.count(); i++)
 
184
        list.insert(index + i, QPair<QString,QString>(items[i].first, items[i].second));
 
185
    emit endInsertRows();
 
186
}
 
187
 
 
188
void QQuickViewTestUtil::QaimModel::removeItem(int index)
 
189
{
 
190
    emit beginRemoveRows(QModelIndex(), index, index);
 
191
    list.removeAt(index);
 
192
    emit endRemoveRows();
 
193
}
 
194
 
 
195
void QQuickViewTestUtil::QaimModel::removeItems(int index, int count)
 
196
{
 
197
    emit beginRemoveRows(QModelIndex(), index, index+count-1);
 
198
    while (count--)
 
199
        list.removeAt(index);
 
200
    emit endRemoveRows();
 
201
}
 
202
 
 
203
void QQuickViewTestUtil::QaimModel::moveItem(int from, int to)
 
204
{
 
205
    emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
 
206
    list.move(from, to);
 
207
    emit endMoveRows();
 
208
}
 
209
 
 
210
void QQuickViewTestUtil::QaimModel::moveItems(int from, int to, int count)
 
211
{
 
212
    emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
 
213
    qquickmodelviewstestutil_move(from, to, count, &list);
 
214
    emit endMoveRows();
 
215
}
 
216
 
 
217
void QQuickViewTestUtil::QaimModel::modifyItem(int idx, const QString &name, const QString &number)
 
218
{
 
219
    list[idx] = QPair<QString,QString>(name, number);
 
220
    emit dataChanged(index(idx,0), index(idx,0));
 
221
}
 
222
 
 
223
void QQuickViewTestUtil::QaimModel::clear()
 
224
{
 
225
    int count = list.count();
 
226
    if (count > 0) {
 
227
        beginRemoveRows(QModelIndex(), 0, count-1);
 
228
        list.clear();
 
229
        endRemoveRows();
 
230
    }
 
231
}
 
232
 
 
233
void QQuickViewTestUtil::QaimModel::reset()
 
234
{
 
235
    emit beginResetModel();
 
236
    emit endResetModel();
 
237
}
 
238
 
 
239
void QQuickViewTestUtil::QaimModel::resetItems(const QList<QPair<QString, QString> > &items)
 
240
{
 
241
    beginResetModel();
 
242
    list = items;
 
243
    endResetModel();
 
244
}
 
245
 
 
246
void QQuickViewTestUtil::QaimModel::matchAgainst(const QList<QPair<QString, QString> > &other, const QString &error1, const QString &error2) {
 
247
    for (int i=0; i<other.count(); i++) {
 
248
        QVERIFY2(list.contains(other[i]),
 
249
                 QTest::toString(other[i].first + " " + other[i].second + " " + error1));
 
250
    }
 
251
    for (int i=0; i<list.count(); i++) {
 
252
        QVERIFY2(other.contains(list[i]),
 
253
                 QTest::toString(list[i].first + " " + list[i].second + " " + error2));
 
254
    }
 
255
}
 
256
 
 
257
 
 
258
 
 
259
QQuickViewTestUtil::ListRange::ListRange()
 
260
    : valid(false)
 
261
{
 
262
}
 
263
 
 
264
QQuickViewTestUtil::ListRange::ListRange(const ListRange &other)
 
265
    : valid(other.valid)
 
266
{
 
267
    indexes = other.indexes;
 
268
}
 
269
 
 
270
QQuickViewTestUtil::ListRange::ListRange(int start, int end)
 
271
    : valid(true)
 
272
{
 
273
    for (int i=start; i<=end; i++)
 
274
        indexes << i;
 
275
}
 
276
 
 
277
QQuickViewTestUtil::ListRange::~ListRange()
 
278
{
 
279
}
 
280
 
 
281
QQuickViewTestUtil::ListRange QQuickViewTestUtil::ListRange::operator+(const ListRange &other) const
 
282
{
 
283
    if (other == *this)
 
284
        return *this;
 
285
    ListRange a(*this);
 
286
    a.indexes.append(other.indexes);
 
287
    return a;
 
288
}
 
289
 
 
290
bool QQuickViewTestUtil::ListRange::operator==(const ListRange &other) const
 
291
{
 
292
    return indexes.toSet() == other.indexes.toSet();
 
293
}
 
294
 
 
295
bool QQuickViewTestUtil::ListRange::operator!=(const ListRange &other) const
 
296
{
 
297
    return !(*this == other);
 
298
}
 
299
 
 
300
bool QQuickViewTestUtil::ListRange::isValid() const
 
301
{
 
302
    return valid;
 
303
}
 
304
 
 
305
int QQuickViewTestUtil::ListRange::count() const
 
306
{
 
307
    return indexes.count();
 
308
}
 
309
 
 
310
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QaimModel &model)
 
311
{
 
312
    QList<QPair<QString,QString> > data;
 
313
    if (!valid)
 
314
        return data;
 
315
    for (int i=0; i<indexes.count(); i++)
 
316
        data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));
 
317
    return data;
 
318
}
 
319