1/****************************************************************************
2**
3** Copyright (C) 2016 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com>
4** Copyright (C) 2019 Menlo Systems GmbH, author Arno Rehn <a.rehn@menlosystems.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtWebChannel module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:GPL-EXCEPT$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3 as published by the Free Software
21** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#ifndef TST_WEBCHANNEL_H
31#define TST_WEBCHANNEL_H
32
33#include <QObject>
34#include <QVariant>
35#include <QVector>
36#include <QJsonValue>
37#include <QJsonObject>
38#include <QJsonArray>
39
40#include <QtWebChannel/QWebChannelAbstractTransport>
41
42struct TestStruct
43{
44 TestStruct(int foo = 0, int bar = 0)
45 : foo(foo)
46 , bar(bar)
47 {}
48 int foo;
49 int bar;
50};
51Q_DECLARE_METATYPE(TestStruct)
52using TestStructVector = std::vector<TestStruct>;
53Q_DECLARE_METATYPE(TestStructVector)
54
55QT_BEGIN_NAMESPACE
56
57class DummyTransport : public QWebChannelAbstractTransport
58{
59 Q_OBJECT
60public:
61 explicit DummyTransport(QObject *parent = nullptr)
62 : QWebChannelAbstractTransport(parent)
63 {}
64 ~DummyTransport() {};
65
66 void emitMessageReceived(const QJsonObject &message)
67 {
68 emit messageReceived(message, transport: this);
69 }
70
71 QVector<QJsonObject> messagesSent() const
72 {
73 return mMessagesSent;
74 }
75
76public slots:
77 void sendMessage(const QJsonObject &message) override
78 {
79 mMessagesSent.push_back(t: message);
80 }
81private:
82 QVector<QJsonObject> mMessagesSent;
83};
84
85class TestObject : public QObject
86{
87 Q_OBJECT
88 Q_ENUMS(Foo)
89
90 Q_PROPERTY(Foo foo READ foo CONSTANT)
91 Q_PROPERTY(int asdf READ asdf NOTIFY asdfChanged)
92 Q_PROPERTY(QString bar READ bar NOTIFY theBarHasChanged)
93 Q_PROPERTY(QObject * objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectPropertyChanged)
94 Q_PROPERTY(TestObject * returnedObject READ returnedObject WRITE setReturnedObject NOTIFY returnedObjectChanged)
95 Q_PROPERTY(QString prop READ prop WRITE setProp NOTIFY propChanged)
96
97public:
98 explicit TestObject(QObject *parent = 0)
99 : QObject(parent)
100 , mObjectProperty(0)
101 , mReturnedObject(Q_NULLPTR)
102 { }
103
104 enum Foo {
105 Bar,
106 Asdf
107 };
108
109 enum TestFlag : quint16 {
110 FirstFlag = 0x1,
111 SecondFlag = 0x2
112 };
113 Q_DECLARE_FLAGS(TestFlags, TestFlag)
114 Q_FLAG(TestFlags)
115
116 Foo foo() const {return Bar;}
117 int asdf() const {return 42;}
118 QString bar() const {return QString();}
119
120 QObject *objectProperty() const
121 {
122 return mObjectProperty;
123 }
124
125 TestObject *returnedObject() const
126 {
127 return mReturnedObject;
128 }
129
130 QString prop() const
131 {
132 return mProp;
133 }
134
135 Q_INVOKABLE void method1() {}
136
137protected:
138 Q_INVOKABLE void method2() {}
139
140private:
141 Q_INVOKABLE void method3() {}
142
143signals:
144 void sig1();
145 void sig2(const QString&);
146 void asdfChanged();
147 void theBarHasChanged();
148 void objectPropertyChanged();
149 void returnedObjectChanged();
150 void propChanged(const QString&);
151 void replay();
152 void overloadSignal(int);
153 void overloadSignal(float);
154
155public slots:
156 void slot1() {}
157 void slot2(const QString&) {}
158
159 void setReturnedObject(TestObject *obj)
160 {
161 mReturnedObject = obj;
162 emit returnedObjectChanged();
163 }
164
165 void setObjectProperty(QObject *object)
166 {
167 mObjectProperty = object;
168 emit objectPropertyChanged();
169 }
170
171 void setProp(const QString&prop) {emit propChanged(mProp=prop);}
172 void fire() {emit replay();}
173
174 double overload(double d) { return d + 1; }
175 int overload(int i) { return i * 2; }
176 QObject *overload(QObject *object) { return object; }
177 QString overload(const QString &str) { return str.toUpper(); }
178 QString overload(const QString &str, int i) { return str.toUpper() + QString::number(i + 1); }
179 QString overload(const QJsonArray &v) { return QString::number(v[1].toInt()) + v[0].toString(); }
180
181protected slots:
182 void slot3() {}
183
184private slots:
185 void slot4() {}
186
187public:
188 QObject *mObjectProperty;
189 TestObject *mReturnedObject;
190 QString mProp;
191};
192
193Q_DECLARE_OPERATORS_FOR_FLAGS(TestObject::TestFlags)
194
195class BenchObject : public QObject
196{
197 Q_OBJECT
198
199 Q_PROPERTY(int p0 MEMBER m_p0 NOTIFY p0Changed)
200 Q_PROPERTY(int p1 MEMBER m_p1 NOTIFY p1Changed)
201 Q_PROPERTY(int p2 MEMBER m_p2 NOTIFY p2Changed)
202 Q_PROPERTY(int p3 MEMBER m_p3 NOTIFY p3Changed)
203 Q_PROPERTY(int p4 MEMBER m_p4 NOTIFY p4Changed)
204 Q_PROPERTY(int p5 MEMBER m_p5 NOTIFY p5Changed)
205 Q_PROPERTY(int p6 MEMBER m_p6 NOTIFY p6Changed)
206 Q_PROPERTY(int p7 MEMBER m_p7 NOTIFY p7Changed)
207 Q_PROPERTY(int p8 MEMBER m_p8 NOTIFY p8Changed)
208 Q_PROPERTY(int p9 MEMBER m_p9 NOTIFY p9Changed)
209public:
210 explicit BenchObject(QObject *parent = 0)
211 : QObject(parent)
212 , m_p0(0)
213 , m_p1(0)
214 , m_p2(0)
215 , m_p3(0)
216 , m_p4(0)
217 , m_p5(0)
218 , m_p6(0)
219 , m_p7(0)
220 , m_p8(0)
221 , m_p9(0)
222 { }
223
224 void change()
225 {
226 m_p0++;
227 m_p1++;
228 m_p2++;
229 m_p3++;
230 m_p4++;
231 m_p5++;
232 m_p6++;
233 m_p7++;
234 m_p8++;
235 m_p9++;
236 emit p0Changed(m_p0);
237 emit p1Changed(m_p1);
238 emit p2Changed(m_p2);
239 emit p3Changed(m_p3);
240 emit p4Changed(m_p4);
241 emit p5Changed(m_p5);
242 emit p6Changed(m_p6);
243 emit p7Changed(m_p7);
244 emit p8Changed(m_p8);
245 emit p9Changed(m_p9);
246 }
247
248signals:
249 void s0();
250 void s1();
251 void s2();
252 void s3();
253 void s4();
254 void s5();
255 void s6();
256 void s7();
257 void s8();
258 void s9();
259
260 void p0Changed(int);
261 void p1Changed(int);
262 void p2Changed(int);
263 void p3Changed(int);
264 void p4Changed(int);
265 void p5Changed(int);
266 void p6Changed(int);
267 void p7Changed(int);
268 void p8Changed(int);
269 void p9Changed(int);
270
271public slots:
272 void m0(){};
273 void m1(){};
274 void m2(){};
275 void m3(){};
276 void m4(){};
277 void m5(){};
278 void m6(){};
279 void m7(){};
280 void m8(){};
281 void m9(){};
282
283private:
284 int m_p0, m_p1, m_p2, m_p3, m_p4, m_p5, m_p6, m_p7, m_p8, m_p9;
285};
286
287class TestWebChannel : public QObject
288{
289 Q_OBJECT
290
291 Q_PROPERTY(int lastInt READ readInt WRITE setInt NOTIFY lastIntChanged);
292 Q_PROPERTY(bool lastBool READ readBool WRITE setBool NOTIFY lastBoolChanged);
293 Q_PROPERTY(double lastDouble READ readDouble WRITE setDouble NOTIFY lastDoubleChanged);
294 Q_PROPERTY(QVariant lastVariant READ readVariant WRITE setVariant NOTIFY lastVariantChanged);
295 Q_PROPERTY(QJsonValue lastJsonValue READ readJsonValue WRITE setJsonValue NOTIFY lastJsonValueChanged);
296 Q_PROPERTY(QJsonObject lastJsonObject READ readJsonObject WRITE setJsonObject NOTIFY lastJsonObjectChanged);
297 Q_PROPERTY(QJsonArray lastJsonArray READ readJsonArray WRITE setJsonArray NOTIFY lastJsonArrayChanged);
298public:
299 explicit TestWebChannel(QObject *parent = 0);
300 virtual ~TestWebChannel();
301
302public slots:
303 int readInt() const;
304 void setInt(int i);
305 bool readBool() const;
306 void setBool(bool b);
307 double readDouble() const;
308 void setDouble(double d);
309 QVariant readVariant() const;
310 void setVariant(const QVariant &v);
311 QJsonValue readJsonValue() const;
312 void setJsonValue(const QJsonValue &v);
313 QJsonObject readJsonObject() const;
314 void setJsonObject(const QJsonObject &v);
315 QJsonArray readJsonArray() const;
316 void setJsonArray(const QJsonArray &v);
317
318 int readOverload(int i);
319 QString readOverload(const QString &arg);
320 QString readOverload(const QString &arg, int i);
321
322signals:
323 void lastIntChanged();
324 void lastBoolChanged();
325 void lastDoubleChanged();
326 void lastVariantChanged();
327 void lastJsonValueChanged();
328 void lastJsonObjectChanged();
329 void lastJsonArrayChanged();
330
331private slots:
332 void testRegisterObjects();
333 void testDeregisterObjects();
334 void testDeregisterObjectAtStart();
335 void testInfoForObject();
336 void testInvokeMethodConversion();
337 void testFunctionOverloading();
338 void testSetPropertyConversion();
339 void testInvokeMethodOverloadResolution();
340 void testDisconnect();
341 void testWrapRegisteredObject();
342 void testUnwrapObject();
343 void testTransportWrapObjectProperties();
344 void testRemoveUnusedTransports();
345 void testPassWrappedObjectBack();
346 void testWrapValues();
347 void testWrapObjectWithMultipleTransports();
348 void testJsonToVariant();
349 void testInfiniteRecursion();
350 void testAsyncObject();
351 void testDeletionDuringMethodInvocation_data();
352 void testDeletionDuringMethodInvocation();
353
354 void benchClassInfo();
355 void benchInitializeClients();
356 void benchPropertyUpdates();
357 void benchRegisterObjects();
358 void benchRemoveTransport();
359
360 void qtbug46548_overriddenProperties();
361 void qtbug62388_wrapObjectMultipleTransports();
362
363private:
364 DummyTransport *m_dummyTransport;
365
366 int m_lastInt;
367 bool m_lastBool;
368 double m_lastDouble;
369 QVariant m_lastVariant;
370 QJsonValue m_lastJsonValue;
371 QJsonObject m_lastJsonObject;
372 QJsonArray m_lastJsonArray;
373};
374
375QT_END_NAMESPACE
376
377Q_DECLARE_METATYPE(TestObject::Foo)
378
379#endif // TST_WEBCHANNEL_H
380

source code of qtwebchannel/tests/auto/webchannel/tst_webchannel.h