Update to 5.0.0-beta1
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlpropertycache / tst_qqmlpropertycache.cpp
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 <qtest.h>
43 #include <private/qqmlpropertycache_p.h>
44 #include <QtQml/qqmlengine.h>
45 #include "../../shared/util.h"
46
47 class tst_qqmlpropertycache : public QObject
48 {
49     Q_OBJECT
50 public:
51     tst_qqmlpropertycache() {}
52
53 private slots:
54     void properties();
55     void propertiesDerived();
56     void methods();
57     void methodsDerived();
58     void signalHandlers();
59     void signalHandlersDerived();
60
61 private:
62     QQmlEngine engine;
63 };
64
65 class BaseObject : public QObject
66 {
67     Q_OBJECT
68     Q_PROPERTY(int propertyA READ propertyA NOTIFY propertyAChanged)
69     Q_PROPERTY(QString propertyB READ propertyB NOTIFY propertyBChanged)
70 public:
71     BaseObject(QObject *parent = 0) : QObject(parent) {}
72
73     int propertyA() const { return 0; }
74     QString propertyB() const { return QString(); }
75
76 public Q_SLOTS:
77     void slotA() {}
78
79 Q_SIGNALS:
80     void propertyAChanged();
81     void propertyBChanged();
82     void signalA();
83 };
84
85 class DerivedObject : public BaseObject
86 {
87     Q_OBJECT
88     Q_PROPERTY(int propertyC READ propertyC NOTIFY propertyCChanged)
89     Q_PROPERTY(QString propertyD READ propertyD NOTIFY propertyDChanged)
90 public:
91     DerivedObject(QObject *parent = 0) : BaseObject(parent) {}
92
93     int propertyC() const { return 0; }
94     QString propertyD() const { return QString(); }
95
96 public Q_SLOTS:
97     void slotB() {}
98
99 Q_SIGNALS:
100     void propertyCChanged();
101     void propertyDChanged();
102     void signalB();
103 };
104
105 QQmlPropertyData *cacheProperty(QQmlPropertyCache *cache, const char *name)
106 {
107     return cache->property(QLatin1String(name), 0, 0);
108 }
109
110 void tst_qqmlpropertycache::properties()
111 {
112     QQmlEngine engine;
113     DerivedObject object;
114     const QMetaObject *metaObject = object.metaObject();
115
116     QQmlRefPointer<QQmlPropertyCache> cache(new QQmlPropertyCache(&engine, metaObject));
117     QQmlPropertyData *data;
118
119     QVERIFY(data = cacheProperty(cache, "propertyA"));
120     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyA"));
121
122     QVERIFY(data = cacheProperty(cache, "propertyB"));
123     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyB"));
124
125     QVERIFY(data = cacheProperty(cache, "propertyC"));
126     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyC"));
127
128     QVERIFY(data = cacheProperty(cache, "propertyD"));
129     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyD"));
130 }
131
132 void tst_qqmlpropertycache::propertiesDerived()
133 {
134     QQmlEngine engine;
135     DerivedObject object;
136     const QMetaObject *metaObject = object.metaObject();
137
138     QQmlRefPointer<QQmlPropertyCache> parentCache(new QQmlPropertyCache(&engine, &BaseObject::staticMetaObject));
139     QQmlRefPointer<QQmlPropertyCache> cache(parentCache->copyAndAppend(&engine, object.metaObject()));
140     QQmlPropertyData *data;
141
142     QVERIFY(data = cacheProperty(cache, "propertyA"));
143     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyA"));
144
145     QVERIFY(data = cacheProperty(cache, "propertyB"));
146     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyB"));
147
148     QVERIFY(data = cacheProperty(cache, "propertyC"));
149     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyC"));
150
151     QVERIFY(data = cacheProperty(cache, "propertyD"));
152     QCOMPARE(data->coreIndex, metaObject->indexOfProperty("propertyD"));
153 }
154
155 void tst_qqmlpropertycache::methods()
156 {
157     QQmlEngine engine;
158     DerivedObject object;
159     const QMetaObject *metaObject = object.metaObject();
160
161     QQmlRefPointer<QQmlPropertyCache> cache(new QQmlPropertyCache(&engine, metaObject));
162     QQmlPropertyData *data;
163
164     QVERIFY(data = cacheProperty(cache, "slotA"));
165     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("slotA()"));
166
167     QVERIFY(data = cacheProperty(cache, "slotB"));
168     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("slotB()"));
169
170     QVERIFY(data = cacheProperty(cache, "signalA"));
171     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalA()"));
172
173     QVERIFY(data = cacheProperty(cache, "signalB"));
174     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalB()"));
175
176     QVERIFY(data = cacheProperty(cache, "propertyAChanged"));
177     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyAChanged()"));
178
179     QVERIFY(data = cacheProperty(cache, "propertyBChanged"));
180     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyBChanged()"));
181
182     QVERIFY(data = cacheProperty(cache, "propertyCChanged"));
183     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyCChanged()"));
184
185     QVERIFY(data = cacheProperty(cache, "propertyDChanged"));
186     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyDChanged()"));
187 }
188
189 void tst_qqmlpropertycache::methodsDerived()
190 {
191     QQmlEngine engine;
192     DerivedObject object;
193     const QMetaObject *metaObject = object.metaObject();
194
195     QQmlRefPointer<QQmlPropertyCache> parentCache(new QQmlPropertyCache(&engine, &BaseObject::staticMetaObject));
196     QQmlRefPointer<QQmlPropertyCache> cache(parentCache->copyAndAppend(&engine, object.metaObject()));
197     QQmlPropertyData *data;
198
199     QVERIFY(data = cacheProperty(cache, "slotA"));
200     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("slotA()"));
201
202     QVERIFY(data = cacheProperty(cache, "slotB"));
203     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("slotB()"));
204
205     QVERIFY(data = cacheProperty(cache, "signalA"));
206     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalA()"));
207
208     QVERIFY(data = cacheProperty(cache, "signalB"));
209     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalB()"));
210
211     QVERIFY(data = cacheProperty(cache, "propertyAChanged"));
212     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyAChanged()"));
213
214     QVERIFY(data = cacheProperty(cache, "propertyBChanged"));
215     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyBChanged()"));
216
217     QVERIFY(data = cacheProperty(cache, "propertyCChanged"));
218     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyCChanged()"));
219
220     QVERIFY(data = cacheProperty(cache, "propertyDChanged"));
221     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyDChanged()"));
222 }
223
224 void tst_qqmlpropertycache::signalHandlers()
225 {
226     QQmlEngine engine;
227     DerivedObject object;
228     const QMetaObject *metaObject = object.metaObject();
229
230     QQmlRefPointer<QQmlPropertyCache> cache(new QQmlPropertyCache(&engine, metaObject));
231     QQmlPropertyData *data;
232
233     QVERIFY(data = cacheProperty(cache, "onSignalA"));
234     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalA()"));
235
236     QVERIFY(data = cacheProperty(cache, "onSignalB"));
237     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalB()"));
238
239     QVERIFY(data = cacheProperty(cache, "onPropertyAChanged"));
240     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyAChanged()"));
241
242     QVERIFY(data = cacheProperty(cache, "onPropertyBChanged"));
243     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyBChanged()"));
244
245     QVERIFY(data = cacheProperty(cache, "onPropertyCChanged"));
246     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyCChanged()"));
247
248     QVERIFY(data = cacheProperty(cache, "onPropertyDChanged"));
249     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyDChanged()"));
250 }
251
252 void tst_qqmlpropertycache::signalHandlersDerived()
253 {
254     QQmlEngine engine;
255     DerivedObject object;
256     const QMetaObject *metaObject = object.metaObject();
257
258     QQmlRefPointer<QQmlPropertyCache> parentCache(new QQmlPropertyCache(&engine, &BaseObject::staticMetaObject));
259     QQmlRefPointer<QQmlPropertyCache> cache(parentCache->copyAndAppend(&engine, object.metaObject()));
260     QQmlPropertyData *data;
261
262     QVERIFY(data = cacheProperty(cache, "onSignalA"));
263     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalA()"));
264
265     QVERIFY(data = cacheProperty(cache, "onSignalB"));
266     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("signalB()"));
267
268     QVERIFY(data = cacheProperty(cache, "onPropertyAChanged"));
269     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyAChanged()"));
270
271     QVERIFY(data = cacheProperty(cache, "onPropertyBChanged"));
272     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyBChanged()"));
273
274     QVERIFY(data = cacheProperty(cache, "onPropertyCChanged"));
275     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyCChanged()"));
276
277     QVERIFY(data = cacheProperty(cache, "onPropertyDChanged"));
278     QCOMPARE(data->coreIndex, metaObject->indexOfMethod("propertyDChanged()"));
279 }
280
281 QTEST_MAIN(tst_qqmlpropertycache)
282
283 #include "tst_qqmlpropertycache.moc"