1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef QQUICKVIEWTESTUTIL_H |
30 | #define QQUICKVIEWTESTUTIL_H |
31 | |
32 | #include <QtQuick/QQuickItem> |
33 | #include <QtQml/QQmlExpression> |
34 | #include <QtCore/QAbstractListModel> |
35 | |
36 | QT_FORWARD_DECLARE_CLASS(QQuickView) |
37 | QT_FORWARD_DECLARE_CLASS(QQuickItemViewPrivate) |
38 | QT_FORWARD_DECLARE_CLASS(FxViewItem) |
39 | |
40 | QT_BEGIN_NAMESPACE |
41 | |
42 | namespace QQuickViewTestUtil |
43 | { |
44 | QQuickView *createView(); |
45 | |
46 | void flick(QQuickView *window, const QPoint &from, const QPoint &to, int duration); |
47 | void centerOnScreen(QQuickView *window, const QSize &size); |
48 | void centerOnScreen(QQuickView *window); |
49 | void moveMouseAway(QQuickView *window); |
50 | void moveAndPress(QQuickView *window, const QPoint &position); |
51 | void moveAndRelease(QQuickView *window, const QPoint &position); |
52 | |
53 | QList<int> adjustIndexesForAddDisplaced(const QList<int> &indexes, int index, int count); |
54 | QList<int> adjustIndexesForMove(const QList<int> &indexes, int from, int to, int count); |
55 | QList<int> adjustIndexesForRemoveDisplaced(const QList<int> &indexes, int index, int count); |
56 | |
57 | struct ListChange { |
58 | enum { Inserted, Removed, Moved, SetCurrent, SetContentY, Polish } type; |
59 | int index; |
60 | int count; |
61 | int to; // Move |
62 | qreal pos; // setContentY |
63 | |
64 | static ListChange insert(int index, int count = 1) { ListChange c = { .type: Inserted, .index: index, .count: count, .to: -1, .pos: 0.0 }; return c; } |
65 | static ListChange remove(int index, int count = 1) { ListChange c = { .type: Removed, .index: index, .count: count, .to: -1, .pos: 0.0 }; return c; } |
66 | static ListChange move(int index, int to, int count) { ListChange c = { .type: Moved, .index: index, .count: count, .to: to, .pos: 0.0 }; return c; } |
67 | static ListChange setCurrent(int index) { ListChange c = { .type: SetCurrent, .index: index, .count: -1, .to: -1, .pos: 0.0 }; return c; } |
68 | static ListChange setContentY(qreal pos) { ListChange c = { .type: SetContentY, .index: -1, .count: -1, .to: -1, .pos: pos }; return c; } |
69 | static ListChange polish() { ListChange c = { .type: Polish, .index: -1, .count: -1, .to: -1, .pos: 0.0 }; return c; } |
70 | }; |
71 | |
72 | class QaimModel : public QAbstractListModel |
73 | { |
74 | Q_OBJECT |
75 | public: |
76 | enum Roles { Name = Qt::UserRole+1, Number = Qt::UserRole+2 }; |
77 | |
78 | QaimModel(QObject *parent=0); |
79 | |
80 | int rowCount(const QModelIndex &parent=QModelIndex()) const; |
81 | int columnCount(const QModelIndex &parent=QModelIndex()) const; |
82 | QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const; |
83 | QHash<int,QByteArray> roleNames() const; |
84 | |
85 | int count() const; |
86 | QString name(int index) const; |
87 | QString number(int index) const; |
88 | |
89 | Q_INVOKABLE void addItem(const QString &name, const QString &number); |
90 | void addItems(const QList<QPair<QString, QString> > &items); |
91 | void insertItem(int index, const QString &name, const QString &number); |
92 | void insertItems(int index, const QList<QPair<QString, QString> > &items); |
93 | |
94 | Q_INVOKABLE void removeItem(int index); |
95 | void removeItems(int index, int count); |
96 | |
97 | void moveItem(int from, int to); |
98 | void moveItems(int from, int to, int count); |
99 | |
100 | void modifyItem(int idx, const QString &name, const QString &number); |
101 | |
102 | void clear(); |
103 | void reset(); |
104 | void resetItems(const QList<QPair<QString, QString> > &items); |
105 | |
106 | void matchAgainst(const QList<QPair<QString, QString> > &other, const QString &error1, const QString &error2); |
107 | |
108 | using QAbstractListModel::dataChanged; |
109 | |
110 | int columns = 1; |
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 | class StressTestModel : public QAbstractListModel |
166 | { |
167 | Q_OBJECT |
168 | |
169 | public: |
170 | |
171 | StressTestModel(); |
172 | |
173 | int rowCount(const QModelIndex &) const; |
174 | QVariant data(const QModelIndex &, int) const; |
175 | |
176 | public Q_SLOTS: |
177 | void updateModel(); |
178 | |
179 | private: |
180 | int m_rowCount; |
181 | }; |
182 | |
183 | bool testVisibleItems(const QQuickItemViewPrivate *priv, bool *nonUnique, FxViewItem **failItem, int *expectedIdx); |
184 | } |
185 | |
186 | namespace QQuickTouchUtils { |
187 | void flush(QQuickWindow *window); |
188 | } |
189 | |
190 | namespace QQuickTest { |
191 | bool initView(QQuickView &v, const QUrl &url, bool moveMouseOut, QByteArray *errorMessage); |
192 | } |
193 | |
194 | QT_END_NAMESPACE |
195 | |
196 | Q_DECLARE_METATYPE(QQuickViewTestUtil::QaimModel*) |
197 | Q_DECLARE_METATYPE(QQuickViewTestUtil::ListChange) |
198 | Q_DECLARE_METATYPE(QList<QQuickViewTestUtil::ListChange>) |
199 | Q_DECLARE_METATYPE(QQuickViewTestUtil::ListRange) |
200 | |
201 | |
202 | #endif // QQUICKVIEWTESTUTIL_H |
203 | |