1
/****************************************************************************
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/
6
** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "viewtestutil.h"
44
#include <QtQuick/QQuickView>
46
#include <QtTest/QTest>
49
static void qquickmodelviewstestutil_move(int from, int to, int n, T *items)
52
// Only move forwards - flip if backwards moving
62
typename T::ConstIterator it=items->begin(); it += from+n;
63
for (; i<to-from; ++i,++it)
66
it=items->begin(); it += from;
69
typename T::ConstIterator f=replaced.begin();
70
typename T::Iterator t=items->begin(); t += from;
71
for (; f != replaced.end(); ++f, ++t)
75
QQuickView *QQuickViewTestUtil::createView()
77
QQuickView *canvas = new QQuickView(0);
78
canvas->setGeometry(0,0,240,320);
83
void QQuickViewTestUtil::flick(QQuickView *canvas, const QPoint &from, const QPoint &to, int duration)
85
const int pointCount = 5;
86
QPoint diff = to - from;
88
// send press, five equally spaced moves, and release.
89
QTest::mousePress(canvas, Qt::LeftButton, 0, from);
91
for (int i = 0; i < pointCount; ++i)
92
QTest::mouseMove(canvas, from + (i+1)*diff/pointCount, duration / pointCount);
94
QTest::mouseRelease(canvas, Qt::LeftButton, 0, to);
98
QList<int> QQuickViewTestUtil::adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count)
101
for (int i=0; i<indexes.count(); i++) {
102
int num = indexes[i];
111
QList<int> QQuickViewTestUtil::adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count)
114
for (int i=0; i<indexes.count(); i++) {
115
int num = indexes[i];
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
132
QList<int> QQuickViewTestUtil::adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count)
135
for (int i=0; i<indexes.count(); i++) {
136
int num = indexes[i];
145
QQuickViewTestUtil::QmlListModel::QmlListModel(QObject *parent)
146
: QListModelInterface(parent)
150
QQuickViewTestUtil::QmlListModel::~QmlListModel()
154
QString QQuickViewTestUtil::QmlListModel::name(int index) const
156
return list.at(index).first;
159
QString QQuickViewTestUtil::QmlListModel::number(int index) const
161
return list.at(index).second;
164
int QQuickViewTestUtil::QmlListModel::count() const
169
QList<int> QQuickViewTestUtil::QmlListModel::roles() const
171
return QList<int>() << Name << Number;
174
QString QQuickViewTestUtil::QmlListModel::toString(int role) const
186
QVariant QQuickViewTestUtil::QmlListModel::data(int index, int role) const
189
return list.at(index).first;
191
return list.at(index).second;
195
QHash<int, QVariant> QQuickViewTestUtil::QmlListModel::data(int index, const QList<int> &roles) const
197
QHash<int,QVariant> returnHash;
199
for (int i = 0; i < roles.size(); ++i) {
200
int role = roles.at(i);
204
info = list.at(index).first;
207
info = list.at(index).second;
212
returnHash.insert(role, info);
217
void QQuickViewTestUtil::QmlListModel::addItem(const QString &name, const QString &number)
219
list.append(QPair<QString,QString>(name, number));
220
emit itemsInserted(list.count()-1, 1);
223
void QQuickViewTestUtil::QmlListModel::insertItem(int index, const QString &name, const QString &number)
225
list.insert(index, QPair<QString,QString>(name, number));
226
emit itemsInserted(index, 1);
229
void QQuickViewTestUtil::QmlListModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
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());
236
void QQuickViewTestUtil::QmlListModel::removeItem(int index)
238
list.removeAt(index);
239
emit itemsRemoved(index, 1);
242
void QQuickViewTestUtil::QmlListModel::removeItems(int index, int count)
246
list.removeAt(index);
247
emit itemsRemoved(index, count);
250
void QQuickViewTestUtil::QmlListModel::moveItem(int from, int to)
253
emit itemsMoved(from, to, 1);
256
void QQuickViewTestUtil::QmlListModel::moveItems(int from, int to, int count)
258
qquickmodelviewstestutil_move(from, to, count, &list);
259
emit itemsMoved(from, to, count);
262
void QQuickViewTestUtil::QmlListModel::modifyItem(int index, const QString &name, const QString &number)
264
list[index] = QPair<QString,QString>(name, number);
265
emit itemsChanged(index, 1, roles());
268
void QQuickViewTestUtil::QmlListModel::clear() {
269
int count = list.count();
271
emit itemsRemoved(0, count);
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));
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));
286
QQuickViewTestUtil::QaimModel::QaimModel(QObject *parent)
287
: QAbstractListModel(parent)
289
QHash<int, QByteArray> roles;
290
roles[Name] = "name";
291
roles[Number] = "number";
295
int QQuickViewTestUtil::QaimModel::rowCount(const QModelIndex &parent) const
301
QVariant QQuickViewTestUtil::QaimModel::data(const QModelIndex &index, int role) const
305
rv = list.at(index.row()).first;
306
else if (role == Number)
307
rv = list.at(index.row()).second;
312
int QQuickViewTestUtil::QaimModel::count() const
317
QString QQuickViewTestUtil::QaimModel::name(int index) const
319
return list.at(index).first;
322
QString QQuickViewTestUtil::QaimModel::number(int index) const
324
return list.at(index).second;
327
void QQuickViewTestUtil::QaimModel::addItem(const QString &name, const QString &number)
329
emit beginInsertRows(QModelIndex(), list.count(), list.count());
330
list.append(QPair<QString,QString>(name, number));
331
emit endInsertRows();
334
void QQuickViewTestUtil::QaimModel::addItems(const QList<QPair<QString, QString> > &items)
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();
342
void QQuickViewTestUtil::QaimModel::insertItem(int index, const QString &name, const QString &number)
344
emit beginInsertRows(QModelIndex(), index, index);
345
list.insert(index, QPair<QString,QString>(name, number));
346
emit endInsertRows();
349
void QQuickViewTestUtil::QaimModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
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();
357
void QQuickViewTestUtil::QaimModel::removeItem(int index)
359
emit beginRemoveRows(QModelIndex(), index, index);
360
list.removeAt(index);
361
emit endRemoveRows();
364
void QQuickViewTestUtil::QaimModel::removeItems(int index, int count)
366
emit beginRemoveRows(QModelIndex(), index, index+count-1);
368
list.removeAt(index);
369
emit endRemoveRows();
372
void QQuickViewTestUtil::QaimModel::moveItem(int from, int to)
374
emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
379
void QQuickViewTestUtil::QaimModel::moveItems(int from, int to, int count)
381
emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
382
qquickmodelviewstestutil_move(from, to, count, &list);
386
void QQuickViewTestUtil::QaimModel::modifyItem(int idx, const QString &name, const QString &number)
388
list[idx] = QPair<QString,QString>(name, number);
389
emit dataChanged(index(idx,0), index(idx,0));
392
void QQuickViewTestUtil::QaimModel::clear()
394
int count = list.count();
395
emit beginRemoveRows(QModelIndex(), 0, count-1);
397
emit endRemoveRows();
400
void QQuickViewTestUtil::QaimModel::reset()
402
emit beginResetModel();
403
emit endResetModel();
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));
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));
419
QQuickViewTestUtil::ListRange::ListRange()
424
QQuickViewTestUtil::ListRange::ListRange(const ListRange &other)
427
indexes = other.indexes;
430
QQuickViewTestUtil::ListRange::ListRange(int start, int end)
433
for (int i=start; i<=end; i++)
437
QQuickViewTestUtil::ListRange::~ListRange()
441
QQuickViewTestUtil::ListRange QQuickViewTestUtil::ListRange::operator+(const ListRange &other) const
446
a.indexes.append(other.indexes);
450
bool QQuickViewTestUtil::ListRange::operator==(const ListRange &other) const
452
return indexes.toSet() == other.indexes.toSet();
455
bool QQuickViewTestUtil::ListRange::operator!=(const ListRange &other) const
457
return !(*this == other);
460
bool QQuickViewTestUtil::ListRange::isValid() const
465
int QQuickViewTestUtil::ListRange::count() const
467
return indexes.count();
470
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QmlListModel &model)
472
QList<QPair<QString,QString> > data;
475
for (int i=0; i<indexes.count(); i++)
476
data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));
480
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QaimModel &model)
482
QList<QPair<QString,QString> > data;
485
for (int i=0; i<indexes.count(); i++)
486
data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));