Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / tests / auto / quick / shared / viewtestutil.h
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 #ifndef QQUICKVIEWTESTUTIL_H
43 #define QQUICKVIEWTESTUTIL_H
44
45 #include <QtQuick/QQuickItem>
46 #include <QtQml/QQmlExpression>
47 #include <QtCore/QAbstractListModel>
48
49 QT_FORWARD_DECLARE_CLASS(QQuickView)
50
51 namespace QQuickViewTestUtil
52 {
53     QQuickView *createView();
54
55     void flick(QQuickView *window, const QPoint &from, const QPoint &to, int duration);
56
57     QList<int> adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count);
58     QList<int> adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count);
59     QList<int> adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count);
60
61     struct ListChange {
62         enum { Inserted, Removed, Moved, SetCurrent, SetContentY, Polish } type;
63         int index;
64         int count;
65         int to;     // Move
66         qreal pos;  // setContentY
67
68         static ListChange insert(int index, int count = 1) { ListChange c = { Inserted, index, count, -1, 0.0 }; return c; }
69         static ListChange remove(int index, int count = 1) { ListChange c = { Removed, index, count, -1, 0.0 }; return c; }
70         static ListChange move(int index, int to, int count) { ListChange c = { Moved, index, count, to, 0.0 }; return c; }
71         static ListChange setCurrent(int index) { ListChange c = { SetCurrent, index, -1, -1, 0.0 }; return c; }
72         static ListChange setContentY(qreal pos) { ListChange c = { SetContentY, -1, -1, -1, pos }; return c; }
73         static ListChange polish() { ListChange c = { Polish, -1, -1, -1, 0.0 }; return c; }
74     };
75
76     class QaimModel : public QAbstractListModel
77     {
78         Q_OBJECT
79     public:
80         enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 };
81
82         QaimModel(QObject *parent=0);
83
84         int rowCount(const QModelIndex &parent=QModelIndex()) const;
85         QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
86
87         int count() const;
88         QString name(int index) const;
89         QString number(int index) const;
90
91         Q_INVOKABLE void addItem(const QString &name, const QString &number);
92         void addItems(const QList<QPair<QString, QString> > &items);
93         void insertItem(int index, const QString &name, const QString &number);
94         void insertItems(int index, const QList<QPair<QString, QString> > &items);
95
96         Q_INVOKABLE void removeItem(int index);
97         void removeItems(int index, int count);
98
99         void moveItem(int from, int to);
100         void moveItems(int from, int to, int count);
101
102         void modifyItem(int idx, const QString &name, const QString &number);
103
104         void clear();
105         void reset();
106         void resetItems(const QList<QPair<QString, QString> > &items);
107
108         void matchAgainst(const QList<QPair<QString, QString> > &other, const QString &error1, const QString &error2);
109
110         using QAbstractListModel::dataChanged;
111
112     private:
113         QList<QPair<QString,QString> > list;
114     };
115
116     class ListRange
117     {
118     public:
119         ListRange();
120         ListRange(const ListRange &other);
121         ListRange(int start, int end);
122
123         ~ListRange();
124
125         ListRange operator+(const ListRange &other) const;
126         bool operator==(const ListRange &other) const;
127         bool operator!=(const ListRange &other) const;
128
129         bool isValid() const;
130         int count() const;
131
132         QList<QPair<QString,QString> > getModelDataValues(const QaimModel &model);
133
134         QList<int> indexes;
135         bool valid;
136     };
137
138     template<typename T>
139     static void qquickmodelviewstestutil_move(int from, int to, int n, T *items)
140     {
141         if (from > to) {
142             // Only move forwards - flip if backwards moving
143             int tfrom = from;
144             int tto = to;
145             from = tto;
146             to = tto+n;
147             n = tfrom-tto;
148         }
149
150         T replaced;
151         int i=0;
152         typename T::ConstIterator it=items->begin(); it += from+n;
153         for (; i<to-from; ++i,++it)
154             replaced.append(*it);
155         i=0;
156         it=items->begin(); it += from;
157         for (; i<n; ++i,++it)
158             replaced.append(*it);
159         typename T::ConstIterator f=replaced.begin();
160         typename T::Iterator t=items->begin(); t += from;
161         for (; f != replaced.end(); ++f, ++t)
162             *t = *f;
163     }
164 }
165
166 Q_DECLARE_METATYPE(QQuickViewTestUtil::QaimModel*)
167 Q_DECLARE_METATYPE(QQuickViewTestUtil::ListChange)
168 Q_DECLARE_METATYPE(QList<QQuickViewTestUtil::ListChange>)
169 Q_DECLARE_METATYPE(QQuickViewTestUtil::ListRange)
170
171
172 #endif // QQUICKVIEWTESTUTIL_H