~loic.molinari/+junk/qtdeclarative-shadereffectsource-changes

« back to all changes in this revision

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

  • Committer: Loïc Molinari
  • Date: 2012-04-21 17:59:51 UTC
  • Revision ID: loic.molinari@canonical.com-20120421175951-bqx68caaf5zrp76l
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** GNU Lesser General Public License Usage
 
10
** This file may be used under the terms of the GNU Lesser General Public
 
11
** License version 2.1 as published by the Free Software Foundation and
 
12
** appearing in the file LICENSE.LGPL included in the packaging of this
 
13
** file. Please review the following information to ensure the GNU Lesser
 
14
** General Public License version 2.1 requirements will be met:
 
15
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
16
**
 
17
** In addition, as a special exception, Nokia gives you certain additional
 
18
** rights. These rights are described in the Nokia Qt LGPL Exception
 
19
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
20
**
 
21
** GNU General Public License Usage
 
22
** Alternatively, this file may be used under the terms of the GNU General
 
23
** Public License version 3.0 as published by the Free Software Foundation
 
24
** and appearing in the file LICENSE.GPL included in the packaging of this
 
25
** file. Please review the following information to ensure the GNU General
 
26
** Public License version 3.0 requirements will be met:
 
27
** http://www.gnu.org/copyleft/gpl.html.
 
28
**
 
29
** Other Usage
 
30
** Alternatively, this file may be used in accordance with the terms and
 
31
** conditions contained in a signed written agreement between you and Nokia.
 
32
**
 
33
**
 
34
**
 
35
**
 
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
template<typename T>
 
49
static void qquickmodelviewstestutil_move(int from, int to, int n, T *items)
 
50
{
 
51
    if (from > to) {
 
52
        // Only move forwards - flip if backwards moving
 
53
        int tfrom = from;
 
54
        int tto = to;
 
55
        from = tto;
 
56
        to = tto+n;
 
57
        n = tfrom-tto;
 
58
    }
 
59
 
 
60
    T replaced;
 
61
    int i=0;
 
62
    typename T::ConstIterator it=items->begin(); it += from+n;
 
63
    for (; i<to-from; ++i,++it)
 
64
        replaced.append(*it);
 
65
    i=0;
 
66
    it=items->begin(); it += from;
 
67
    for (; i<n; ++i,++it)
 
68
        replaced.append(*it);
 
69
    typename T::ConstIterator f=replaced.begin();
 
70
    typename T::Iterator t=items->begin(); t += from;
 
71
    for (; f != replaced.end(); ++f, ++t)
 
72
        *t = *f;
 
73
}
 
74
 
 
75
QQuickView *QQuickViewTestUtil::createView()
 
76
{
 
77
    QQuickView *canvas = new QQuickView(0);
 
78
    canvas->setGeometry(0,0,240,320);
 
79
 
 
80
    return canvas;
 
81
}
 
82
 
 
83
void QQuickViewTestUtil::flick(QQuickView *canvas, const QPoint &from, const QPoint &to, int duration)
 
84
{
 
85
    const int pointCount = 5;
 
86
    QPoint diff = to - from;
 
87
 
 
88
    // send press, five equally spaced moves, and release.
 
89
    QTest::mousePress(canvas, Qt::LeftButton, 0, from);
 
90
 
 
91
    for (int i = 0; i < pointCount; ++i)
 
92
        QTest::mouseMove(canvas, from + (i+1)*diff/pointCount, duration / pointCount);
 
93
 
 
94
    QTest::mouseRelease(canvas, Qt::LeftButton, 0, to);
 
95
    QTest::qWait(50);
 
96
}
 
97
 
 
98
QList<int> QQuickViewTestUtil::adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count)
 
99
{
 
100
    QList<int> result;
 
101
    for (int i=0; i<indexes.count(); i++) {
 
102
        int num = indexes[i];
 
103
        if (num >= index) {
 
104
            num += count;
 
105
        }
 
106
        result << num;
 
107
    }
 
108
    return result;
 
109
}
 
110
 
 
111
QList<int> QQuickViewTestUtil::adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count)
 
112
{
 
113
    QList<int> result;
 
114
    for (int i=0; i<indexes.count(); i++) {
 
115
        int num = indexes[i];
 
116
        if (from < to) {
 
117
            if (num >= from && num < from + count)
 
118
                num += (to - from); // target
 
119
            else if (num >= from && num < to + count)
 
120
                num -= count;   // displaced
 
121
        } else if (from > to) {
 
122
            if (num >= from && num < from + count)
 
123
                num -= (from - to);  // target
 
124
            else if (num >= to && num < from + count)
 
125
                num += count;   // displaced
 
126
        }
 
127
        result << num;
 
128
    }
 
129
    return result;
 
130
}
 
131
 
 
132
QList<int> QQuickViewTestUtil::adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count)
 
133
{
 
134
    QList<int> result;
 
135
    for (int i=0; i<indexes.count(); i++) {
 
136
        int num = indexes[i];
 
137
        if (num >= index)
 
138
            num -= count;
 
139
        result << num;
 
140
    }
 
141
    return result;
 
142
}
 
143
 
 
144
 
 
145
QQuickViewTestUtil::QmlListModel::QmlListModel(QObject *parent)
 
146
    : QListModelInterface(parent)
 
147
{
 
148
}
 
149
 
 
150
QQuickViewTestUtil::QmlListModel::~QmlListModel()
 
151
{
 
152
}
 
153
 
 
154
QString QQuickViewTestUtil::QmlListModel::name(int index) const
 
155
{
 
156
    return list.at(index).first;
 
157
}
 
158
 
 
159
QString QQuickViewTestUtil::QmlListModel::number(int index) const
 
160
{
 
161
    return list.at(index).second;
 
162
}
 
163
 
 
164
int QQuickViewTestUtil::QmlListModel::count() const
 
165
{
 
166
    return list.count();
 
167
}
 
168
 
 
169
QList<int> QQuickViewTestUtil::QmlListModel::roles() const
 
170
{
 
171
    return QList<int>() << Name << Number;
 
172
}
 
173
 
 
174
QString QQuickViewTestUtil::QmlListModel::toString(int role) const
 
175
{
 
176
    switch (role) {
 
177
    case Name:
 
178
        return "name";
 
179
    case Number:
 
180
        return "number";
 
181
    default:
 
182
        return "";
 
183
    }
 
184
}
 
185
 
 
186
QVariant QQuickViewTestUtil::QmlListModel::data(int index, int role) const
 
187
{
 
188
    if (role==0)
 
189
        return list.at(index).first;
 
190
    if (role==1)
 
191
        return list.at(index).second;
 
192
    return QVariant();
 
193
}
 
194
 
 
195
QHash<int, QVariant> QQuickViewTestUtil::QmlListModel::data(int index, const QList<int> &roles) const
 
196
{
 
197
    QHash<int,QVariant> returnHash;
 
198
 
 
199
    for (int i = 0; i < roles.size(); ++i) {
 
200
        int role = roles.at(i);
 
201
        QVariant info;
 
202
        switch (role) {
 
203
        case Name:
 
204
            info = list.at(index).first;
 
205
            break;
 
206
        case Number:
 
207
            info = list.at(index).second;
 
208
            break;
 
209
        default:
 
210
            break;
 
211
        }
 
212
        returnHash.insert(role, info);
 
213
    }
 
214
    return returnHash;
 
215
}
 
216
 
 
217
void QQuickViewTestUtil::QmlListModel::addItem(const QString &name, const QString &number)
 
218
{
 
219
    list.append(QPair<QString,QString>(name, number));
 
220
    emit itemsInserted(list.count()-1, 1);
 
221
}
 
222
 
 
223
void QQuickViewTestUtil::QmlListModel::insertItem(int index, const QString &name, const QString &number)
 
224
{
 
225
    list.insert(index, QPair<QString,QString>(name, number));
 
226
    emit itemsInserted(index, 1);
 
227
}
 
228
 
 
229
void QQuickViewTestUtil::QmlListModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
 
230
{
 
231
    for (int i=0; i<items.count(); i++)
 
232
        list.insert(index + i, QPair<QString,QString>(items[i].first, items[i].second));
 
233
    emit itemsInserted(index, items.count());
 
234
}
 
235
 
 
236
void QQuickViewTestUtil::QmlListModel::removeItem(int index)
 
237
{
 
238
    list.removeAt(index);
 
239
    emit itemsRemoved(index, 1);
 
240
}
 
241
 
 
242
void QQuickViewTestUtil::QmlListModel::removeItems(int index, int count)
 
243
{
 
244
    int c = count;
 
245
    while (c--)
 
246
        list.removeAt(index);
 
247
    emit itemsRemoved(index, count);
 
248
}
 
249
 
 
250
void QQuickViewTestUtil::QmlListModel::moveItem(int from, int to)
 
251
{
 
252
    list.move(from, to);
 
253
    emit itemsMoved(from, to, 1);
 
254
}
 
255
 
 
256
void QQuickViewTestUtil::QmlListModel::moveItems(int from, int to, int count)
 
257
{
 
258
    qquickmodelviewstestutil_move(from, to, count, &list);
 
259
    emit itemsMoved(from, to, count);
 
260
}
 
261
 
 
262
void QQuickViewTestUtil::QmlListModel::modifyItem(int index, const QString &name, const QString &number)
 
263
{
 
264
    list[index] = QPair<QString,QString>(name, number);
 
265
    emit itemsChanged(index, 1, roles());
 
266
}
 
267
 
 
268
void QQuickViewTestUtil::QmlListModel::clear() {
 
269
    int count = list.count();
 
270
    list.clear();
 
271
    emit itemsRemoved(0, count);
 
272
}
 
273
 
 
274
void QQuickViewTestUtil::QmlListModel::matchAgainst(const QList<QPair<QString, QString> > &other, const QString &error1, const QString &error2) {
 
275
    for (int i=0; i<other.count(); i++) {
 
276
        QVERIFY2(list.contains(other[i]),
 
277
                 QTest::toString(other[i].first + " " + other[i].second + " " + error1));
 
278
    }
 
279
    for (int i=0; i<list.count(); i++) {
 
280
        QVERIFY2(other.contains(list[i]),
 
281
                 QTest::toString(list[i].first + " " + list[i].second + " " + error2));
 
282
    }
 
283
}
 
284
 
 
285
 
 
286
QQuickViewTestUtil::QaimModel::QaimModel(QObject *parent)
 
287
    : QAbstractListModel(parent)
 
288
{
 
289
    QHash<int, QByteArray> roles;
 
290
    roles[Name] = "name";
 
291
    roles[Number] = "number";
 
292
    setRoleNames(roles);
 
293
}
 
294
 
 
295
int QQuickViewTestUtil::QaimModel::rowCount(const QModelIndex &parent) const
 
296
{
 
297
    Q_UNUSED(parent);
 
298
    return list.count();
 
299
}
 
300
 
 
301
QVariant QQuickViewTestUtil::QaimModel::data(const QModelIndex &index, int role) const
 
302
{
 
303
    QVariant rv;
 
304
    if (role == Name)
 
305
        rv = list.at(index.row()).first;
 
306
    else if (role == Number)
 
307
        rv = list.at(index.row()).second;
 
308
 
 
309
    return rv;
 
310
}
 
311
 
 
312
int QQuickViewTestUtil::QaimModel::count() const
 
313
{
 
314
    return rowCount();
 
315
}
 
316
 
 
317
QString QQuickViewTestUtil::QaimModel::name(int index) const
 
318
{
 
319
    return list.at(index).first;
 
320
}
 
321
 
 
322
QString QQuickViewTestUtil::QaimModel::number(int index) const
 
323
{
 
324
    return list.at(index).second;
 
325
}
 
326
 
 
327
void QQuickViewTestUtil::QaimModel::addItem(const QString &name, const QString &number)
 
328
{
 
329
    emit beginInsertRows(QModelIndex(), list.count(), list.count());
 
330
    list.append(QPair<QString,QString>(name, number));
 
331
    emit endInsertRows();
 
332
}
 
333
 
 
334
void QQuickViewTestUtil::QaimModel::addItems(const QList<QPair<QString, QString> > &items)
 
335
{
 
336
    emit beginInsertRows(QModelIndex(), list.count(), list.count()+items.count()-1);
 
337
    for (int i=0; i<items.count(); i++)
 
338
        list.append(QPair<QString,QString>(items[i].first, items[i].second));
 
339
    emit endInsertRows();
 
340
}
 
341
 
 
342
void QQuickViewTestUtil::QaimModel::insertItem(int index, const QString &name, const QString &number)
 
343
{
 
344
    emit beginInsertRows(QModelIndex(), index, index);
 
345
    list.insert(index, QPair<QString,QString>(name, number));
 
346
    emit endInsertRows();
 
347
}
 
348
 
 
349
void QQuickViewTestUtil::QaimModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
 
350
{
 
351
    emit beginInsertRows(QModelIndex(), index, index+items.count()-1);
 
352
    for (int i=0; i<items.count(); i++)
 
353
        list.insert(index + i, QPair<QString,QString>(items[i].first, items[i].second));
 
354
    emit endInsertRows();
 
355
}
 
356
 
 
357
void QQuickViewTestUtil::QaimModel::removeItem(int index)
 
358
{
 
359
    emit beginRemoveRows(QModelIndex(), index, index);
 
360
    list.removeAt(index);
 
361
    emit endRemoveRows();
 
362
}
 
363
 
 
364
void QQuickViewTestUtil::QaimModel::removeItems(int index, int count)
 
365
{
 
366
    emit beginRemoveRows(QModelIndex(), index, index+count-1);
 
367
    while (count--)
 
368
        list.removeAt(index);
 
369
    emit endRemoveRows();
 
370
}
 
371
 
 
372
void QQuickViewTestUtil::QaimModel::moveItem(int from, int to)
 
373
{
 
374
    emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
 
375
    list.move(from, to);
 
376
    emit endMoveRows();
 
377
}
 
378
 
 
379
void QQuickViewTestUtil::QaimModel::moveItems(int from, int to, int count)
 
380
{
 
381
    emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
 
382
    qquickmodelviewstestutil_move(from, to, count, &list);
 
383
    emit endMoveRows();
 
384
}
 
385
 
 
386
void QQuickViewTestUtil::QaimModel::modifyItem(int idx, const QString &name, const QString &number)
 
387
{
 
388
    list[idx] = QPair<QString,QString>(name, number);
 
389
    emit dataChanged(index(idx,0), index(idx,0));
 
390
}
 
391
 
 
392
void QQuickViewTestUtil::QaimModel::clear()
 
393
{
 
394
    int count = list.count();
 
395
    emit beginRemoveRows(QModelIndex(), 0, count-1);
 
396
    list.clear();
 
397
    emit endRemoveRows();
 
398
}
 
399
 
 
400
void QQuickViewTestUtil::QaimModel::reset()
 
401
{
 
402
    emit beginResetModel();
 
403
    emit endResetModel();
 
404
}
 
405
 
 
406
void QQuickViewTestUtil::QaimModel::matchAgainst(const QList<QPair<QString, QString> > &other, const QString &error1, const QString &error2) {
 
407
    for (int i=0; i<other.count(); i++) {
 
408
        QVERIFY2(list.contains(other[i]),
 
409
                 QTest::toString(other[i].first + " " + other[i].second + " " + error1));
 
410
    }
 
411
    for (int i=0; i<list.count(); i++) {
 
412
        QVERIFY2(other.contains(list[i]),
 
413
                 QTest::toString(list[i].first + " " + list[i].second + " " + error2));
 
414
    }
 
415
}
 
416
 
 
417
 
 
418
 
 
419
QQuickViewTestUtil::ListRange::ListRange()
 
420
    : valid(false)
 
421
{
 
422
}
 
423
 
 
424
QQuickViewTestUtil::ListRange::ListRange(const ListRange &other)
 
425
    : valid(other.valid)
 
426
{
 
427
    indexes = other.indexes;
 
428
}
 
429
 
 
430
QQuickViewTestUtil::ListRange::ListRange(int start, int end)
 
431
    : valid(true)
 
432
{
 
433
    for (int i=start; i<=end; i++)
 
434
        indexes << i;
 
435
}
 
436
 
 
437
QQuickViewTestUtil::ListRange::~ListRange()
 
438
{
 
439
}
 
440
 
 
441
QQuickViewTestUtil::ListRange QQuickViewTestUtil::ListRange::operator+(const ListRange &other) const
 
442
{
 
443
    if (other == *this)
 
444
        return *this;
 
445
    ListRange a(*this);
 
446
    a.indexes.append(other.indexes);
 
447
    return a;
 
448
}
 
449
 
 
450
bool QQuickViewTestUtil::ListRange::operator==(const ListRange &other) const
 
451
{
 
452
    return indexes.toSet() == other.indexes.toSet();
 
453
}
 
454
 
 
455
bool QQuickViewTestUtil::ListRange::operator!=(const ListRange &other) const
 
456
{
 
457
    return !(*this == other);
 
458
}
 
459
 
 
460
bool QQuickViewTestUtil::ListRange::isValid() const
 
461
{
 
462
    return valid;
 
463
}
 
464
 
 
465
int QQuickViewTestUtil::ListRange::count() const
 
466
{
 
467
    return indexes.count();
 
468
}
 
469
 
 
470
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QmlListModel &model)
 
471
{
 
472
    QList<QPair<QString,QString> > data;
 
473
    if (!valid)
 
474
        return data;
 
475
    for (int i=0; i<indexes.count(); i++)
 
476
        data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));
 
477
    return data;
 
478
}
 
479
 
 
480
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QaimModel &model)
 
481
{
 
482
    QList<QPair<QString,QString> > data;
 
483
    if (!valid)
 
484
        return data;
 
485
    for (int i=0; i<indexes.count(); i++)
 
486
        data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));
 
487
    return data;
 
488
}
 
489