1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the test suite of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "viewtestutil.h"
44
#include <QtQuick/QQuickView>
46
#include <QtTest/QTest>
48
QQuickView *QQuickViewTestUtil::createView()
50
QQuickView *window = new QQuickView(0);
51
window->setGeometry(0,0,240,320);
56
void QQuickViewTestUtil::flick(QQuickView *window, const QPoint &from, const QPoint &to, int duration)
58
const int pointCount = 5;
59
QPoint diff = to - from;
61
// send press, five equally spaced moves, and release.
62
QTest::mousePress(window, Qt::LeftButton, 0, from);
64
for (int i = 0; i < pointCount; ++i)
65
QTest::mouseMove(window, from + (i+1)*diff/pointCount, duration / pointCount);
67
QTest::mouseRelease(window, Qt::LeftButton, 0, to);
71
QList<int> QQuickViewTestUtil::adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count)
74
for (int i=0; i<indexes.count(); i++) {
84
QList<int> QQuickViewTestUtil::adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count)
87
for (int i=0; i<indexes.count(); i++) {
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
105
QList<int> QQuickViewTestUtil::adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count)
108
for (int i=0; i<indexes.count(); i++) {
109
int num = indexes[i];
117
QQuickViewTestUtil::QaimModel::QaimModel(QObject *parent)
118
: QAbstractListModel(parent)
120
QHash<int, QByteArray> roles;
121
roles[Name] = "name";
122
roles[Number] = "number";
126
int QQuickViewTestUtil::QaimModel::rowCount(const QModelIndex &parent) const
132
QVariant QQuickViewTestUtil::QaimModel::data(const QModelIndex &index, int role) const
136
rv = list.at(index.row()).first;
137
else if (role == Number)
138
rv = list.at(index.row()).second;
143
int QQuickViewTestUtil::QaimModel::count() const
148
QString QQuickViewTestUtil::QaimModel::name(int index) const
150
return list.at(index).first;
153
QString QQuickViewTestUtil::QaimModel::number(int index) const
155
return list.at(index).second;
158
void QQuickViewTestUtil::QaimModel::addItem(const QString &name, const QString &number)
160
emit beginInsertRows(QModelIndex(), list.count(), list.count());
161
list.append(QPair<QString,QString>(name, number));
162
emit endInsertRows();
165
void QQuickViewTestUtil::QaimModel::addItems(const QList<QPair<QString, QString> > &items)
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();
173
void QQuickViewTestUtil::QaimModel::insertItem(int index, const QString &name, const QString &number)
175
emit beginInsertRows(QModelIndex(), index, index);
176
list.insert(index, QPair<QString,QString>(name, number));
177
emit endInsertRows();
180
void QQuickViewTestUtil::QaimModel::insertItems(int index, const QList<QPair<QString, QString> > &items)
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();
188
void QQuickViewTestUtil::QaimModel::removeItem(int index)
190
emit beginRemoveRows(QModelIndex(), index, index);
191
list.removeAt(index);
192
emit endRemoveRows();
195
void QQuickViewTestUtil::QaimModel::removeItems(int index, int count)
197
emit beginRemoveRows(QModelIndex(), index, index+count-1);
199
list.removeAt(index);
200
emit endRemoveRows();
203
void QQuickViewTestUtil::QaimModel::moveItem(int from, int to)
205
emit beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
210
void QQuickViewTestUtil::QaimModel::moveItems(int from, int to, int count)
212
emit beginMoveRows(QModelIndex(), from, from+count-1, QModelIndex(), to > from ? to+count : to);
213
qquickmodelviewstestutil_move(from, to, count, &list);
217
void QQuickViewTestUtil::QaimModel::modifyItem(int idx, const QString &name, const QString &number)
219
list[idx] = QPair<QString,QString>(name, number);
220
emit dataChanged(index(idx,0), index(idx,0));
223
void QQuickViewTestUtil::QaimModel::clear()
225
int count = list.count();
227
beginRemoveRows(QModelIndex(), 0, count-1);
233
void QQuickViewTestUtil::QaimModel::reset()
235
emit beginResetModel();
236
emit endResetModel();
239
void QQuickViewTestUtil::QaimModel::resetItems(const QList<QPair<QString, QString> > &items)
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));
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));
259
QQuickViewTestUtil::ListRange::ListRange()
264
QQuickViewTestUtil::ListRange::ListRange(const ListRange &other)
267
indexes = other.indexes;
270
QQuickViewTestUtil::ListRange::ListRange(int start, int end)
273
for (int i=start; i<=end; i++)
277
QQuickViewTestUtil::ListRange::~ListRange()
281
QQuickViewTestUtil::ListRange QQuickViewTestUtil::ListRange::operator+(const ListRange &other) const
286
a.indexes.append(other.indexes);
290
bool QQuickViewTestUtil::ListRange::operator==(const ListRange &other) const
292
return indexes.toSet() == other.indexes.toSet();
295
bool QQuickViewTestUtil::ListRange::operator!=(const ListRange &other) const
297
return !(*this == other);
300
bool QQuickViewTestUtil::ListRange::isValid() const
305
int QQuickViewTestUtil::ListRange::count() const
307
return indexes.count();
310
QList<QPair<QString,QString> > QQuickViewTestUtil::ListRange::getModelDataValues(const QaimModel &model)
312
QList<QPair<QString,QString> > data;
315
for (int i=0; i<indexes.count(); i++)
316
data.append(qMakePair(model.name(indexes[i]), model.number(indexes[i])));