Debian Codegraph @

tests/auto/dbus/qdbusmetatype/tst_qdbusmetatype.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QTest>
5 #include <QCoreApplication>
6 #include <QMetaType>
7 #include <QDBusArgument>
8 #include <QDBusMetaType>
9
10 class tst_QDBusMetaType: public QObject
11 {
12 Q_OBJECT
13 public:
14 int intStringMap;
15 int stringStringMap;
16 int stringStruct1Map;
17
18 private slots:
19 void initTestCase();
20 void staticTypes_data();
21 void staticTypes();
22 void dynamicTypes_data();
23 void dynamicTypes();
24 void invalidTypes_data();
25 void invalidTypes();
26 };
27
28 using StringPair = std::pair<QString, QString>;
29
30 struct Struct1 { }; // (s)
31 struct Struct2 { }; // (sos)
32 struct Struct3 { }; // (sas)
33 struct Struct4 // (ssa(ss)sayasx)
34 {
35 QString m1;
36 QString m2;
37 QList<StringPair> m3;
38 QString m4;
39 QByteArray m5;
40 QStringList m6;
41 qlonglong m7;
42 };
43 struct Struct5 // a{sa{sv}} - non-standard outer struct is used as a local
44 { // container, see marshalling operator below.
45 QVariantMap m1;
46 QVariantMap m2;
47 QVariantMap m3;
48 };
49 struct Struct6 // av - non-standard outer struct is used as a local container,
50 { // see marshalling operator below.
51 QVariant v1;
52 QVariant v2;
53 QVariant v3;
54 };
55
56
57 struct Invalid0 { }; // empty
58 struct Invalid1 { }; // s
59 struct Invalid2 { }; // o
60 struct Invalid3 { }; // as
61 struct Invalid4 { }; // ay
62 struct Invalid5 { }; // ii
63 struct Invalid6 { }; // <invalid>
64 struct Invalid7 { }; // (<invalid>)
65
66 Q_DECLARE_METATYPE(Struct1)
67 Q_DECLARE_METATYPE(Struct2)
68 Q_DECLARE_METATYPE(Struct3)
69 Q_DECLARE_METATYPE(Struct4)
70 Q_DECLARE_METATYPE(StringPair)
71 Q_DECLARE_METATYPE(Struct5)
72 Q_DECLARE_METATYPE(Struct6)
73
74 Q_DECLARE_METATYPE(Invalid0)
75 Q_DECLARE_METATYPE(Invalid1)
76 Q_DECLARE_METATYPE(Invalid2)
77 Q_DECLARE_METATYPE(Invalid3)
78 Q_DECLARE_METATYPE(Invalid4)
79 Q_DECLARE_METATYPE(Invalid5)
80 Q_DECLARE_METATYPE(Invalid6)
81 Q_DECLARE_METATYPE(Invalid7)
82
83 typedef QMap<int, QString> IntStringMap;
84 typedef QMap<QString, QString> StringStringMap;
85 typedef QMap<QString, Struct1> StringStruct1Map;
86
87 QT_BEGIN_NAMESPACE
88 QDBusArgument &operator<<(QDBusArgument &arg, const Struct1 &)
89 {
90 arg.beginStructure();
91 arg << QString();
92 arg.endStructure();
93 return arg;
94 }
95
96 QDBusArgument &operator<<(QDBusArgument &arg, const Struct2 &)
97 {
98 arg.beginStructure();
99 arg << QString() << QDBusObjectPath() << QString();
100 arg.endStructure();
101 return arg;
102 }
103
104 QDBusArgument &operator<<(QDBusArgument &arg, const Struct3 &)
105 {
106 arg.beginStructure();
107 arg << QString() << QStringList();
108 arg.endStructure();
109 return arg;
110 }
111
112 QDBusArgument &operator<<(QDBusArgument &arg, const StringPair &s)
113 {
114 arg.beginStructure();
115 arg << s.first << s.second;
116 arg.endStructure();
117 return arg;
118 }
119
120 QDBusArgument &operator<<(QDBusArgument &arg, const Struct4 &s)
121 {
122 arg.beginStructure();
123 arg << s.m1 << s.m2 << s.m3 << s.m4 << s.m5 << s.m6 << s.m7;
124 arg.endStructure();
125 return arg;
126 }
127
128 QDBusArgument &operator<<(QDBusArgument &arg, const Struct5 &s)
129 {
130 arg.beginMap(qMetaTypeId<QString>(), qMetaTypeId<QVariantMap>());
131
132 arg.beginMapEntry();
133 arg << QStringLiteral("map1") << s.m1;
134 arg.endMapEntry();
135
136 arg.beginMapEntry();
137 arg << QStringLiteral("map2") << s.m2;
138 arg.endMapEntry();
139
140 arg.beginMapEntry();
141 arg << QStringLiteral("map3") << s.m3;
142 arg.endMapEntry();
143
144 arg.endMap();
145 return arg;
146 }
147
148 QDBusArgument &operator<<(QDBusArgument &arg, const Struct6 &s)
149 {
150 arg.beginArray(qMetaTypeId<QDBusVariant>());
151 arg << QDBusVariant(s.v1) << QDBusVariant(s.v2) << QDBusVariant(s.v3);
152 arg.endArray();
153 return arg;
154 }
155
156 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid0 &)
157 {
158 return arg;
159 }
160
161 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid1 &)
162 {
163 arg << QString();
164 return arg;
165 }
166
167 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid2 &)
168 {
169 arg << QDBusObjectPath();
170 return arg;
171 }
172
173 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid3 &)
174 {
175 arg << QStringList();
176 return arg;
177 }
178
179 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid4 &)
180 {
181 arg << QByteArray();
182 return arg;
183 }
184
185 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid5 &)
186 {
187 arg << 1 << 2;
188 return arg;
189 }
190
191 // no Invalid6
192
193 QDBusArgument &operator<<(QDBusArgument &arg, const Invalid7 &)
194 {
195 arg.beginStructure();
196 arg << Invalid0();
197 arg.endStructure();
198 return arg;
199 }
200
201 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct1 &)
202 { return arg; }
203 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct2 &)
204 { return arg; }
205 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct3 &)
206 { return arg; }
207 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct4 &)
208 { return arg; }
209 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct5 &)
210 { return arg; }
211 const QDBusArgument &operator>>(const QDBusArgument &arg, Struct6 &)
212 { return arg; }
213 const QDBusArgument &operator>>(const QDBusArgument &arg, StringPair &)
214 { return arg; }
215 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid0 &)
216 { return arg; }
217 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid1 &)
218 { return arg; }
219 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid2 &)
220 { return arg; }
221 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid3 &)
222 { return arg; }
223 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid4 &)
224 { return arg; }
225 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid5 &)
226 { return arg; }
227 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid6 &)
228 { return arg; }
229 const QDBusArgument &operator>>(const QDBusArgument &arg, Invalid7 &)
230 { return arg; }
231 QT_END_NAMESPACE
232
233 void tst_QDBusMetaType::initTestCase()
234 {
235 qDBusRegisterMetaType<Struct1>();
236 qDBusRegisterMetaType<Struct2>();
237 qDBusRegisterMetaType<Struct3>();
238 qDBusRegisterMetaType<Struct4>();
239 qDBusRegisterMetaType<Struct5>();
240 qDBusRegisterMetaType<Struct6>();
241 qDBusRegisterMetaType<StringPair>();
242
243 qDBusRegisterMetaType<QList<Struct1> >();
244 qDBusRegisterMetaType<QList<Struct2> >();
245 qDBusRegisterMetaType<QList<Struct3> >();
246 qDBusRegisterMetaType<QList<Struct4> >();
247
248 #ifdef Q_CC_GNU_ONLY
249 // GCC has a defect/extension (depending on your point of view) that allows
250 // a template class with defaulted template parameters to match a Template
251 // Template Parameter (TTP) with fewer template arguments. The call below
252 // tries to use the template<template <typename> class Container, ...>
253 // template functions qdbusargument.h
254 qDBusRegisterMetaType<std::vector<Struct1> >();
255 #endif
256
257 qDBusRegisterMetaType<Invalid0>();
258 qDBusRegisterMetaType<Invalid1>();
259 qDBusRegisterMetaType<Invalid2>();
260 qDBusRegisterMetaType<Invalid3>();
261 qDBusRegisterMetaType<Invalid4>();
262 qDBusRegisterMetaType<Invalid5>();
263 // don't register Invalid6
264 qDBusRegisterMetaType<Invalid7>();
265
266 qDBusRegisterMetaType<QList<Invalid0> >();
267
268 intStringMap = qDBusRegisterMetaType<QMap<int, QString> >().id();
269 stringStringMap = qDBusRegisterMetaType<QMap<QString, QString> >().id();
270 stringStruct1Map = qDBusRegisterMetaType<QMap<QString, Struct1> >().id();
271 }
272
273 void tst_QDBusMetaType::staticTypes_data()
274 {
275 QTest::addColumn<int>("typeId");
276 QTest::addColumn<QString>("signature");
277
278 QTest::newRow("uchar") << int(QMetaType::UChar) << "y";
279 QTest::newRow("bool") << int(QMetaType::Bool) << "b";
280 QTest::newRow("short") << int(QMetaType::Short) << "n";
281 QTest::newRow("ushort") << int(QMetaType::UShort) << "q";
282 QTest::newRow("int") << int(QMetaType::Int) << "i";
283 QTest::newRow("uint") << int(QMetaType::UInt) << "u";
284 QTest::newRow("qlonglong") << int(QMetaType::LongLong) << "x";
285 QTest::newRow("qulonglong") << int(QMetaType::ULongLong) << "t";
286 QTest::newRow("double") << int(QMetaType::Double) << "d";
287 QTest::newRow("QString") << int(QMetaType::QString) << "s";
288 QTest::newRow("QDBusObjectPath") << qMetaTypeId<QDBusObjectPath>() << "o";
289 QTest::newRow("QDBusSignature") << qMetaTypeId<QDBusSignature>() << "g";
290 QTest::newRow("QDBusVariant") << qMetaTypeId<QDBusVariant>() << "v";
291
292 QTest::newRow("QByteArray") << int(QMetaType::QByteArray) << "ay";
293 QTest::newRow("QStringList") << int(QMetaType::QStringList) << "as";
294 }
295
296 void tst_QDBusMetaType::dynamicTypes_data()
297 {
298 QTest::addColumn<int>("typeId");
299 QTest::addColumn<QString>("signature");
300
301 QTest::newRow("QVariantList") << int(QMetaType::QVariantList) << "av";
302 QTest::newRow("QVariantMap") << int(QMetaType::QVariantMap) << "a{sv}";
303 QTest::newRow("QDate") << int(QMetaType::QDate) << "(iii)";
304 QTest::newRow("QTime") << int(QMetaType::QTime) << "(iiii)";
305 QTest::newRow("QDateTime") << int(QMetaType::QDateTime) << "((iii)(iiii)i)";
306 QTest::newRow("QRect") << int(QMetaType::QRect) << "(iiii)";
307 QTest::newRow("QRectF") << int(QMetaType::QRectF) << "(dddd)";
308 QTest::newRow("QSize") << int(QMetaType::QSize) << "(ii)";
309 QTest::newRow("QSizeF") << int(QMetaType::QSizeF) << "(dd)";
310 QTest::newRow("QPoint") << int(QMetaType::QPoint) << "(ii)";
311 QTest::newRow("QPointF") << int(QMetaType::QPointF) << "(dd)";
312 QTest::newRow("QLine") << int(QMetaType::QLine) << "((ii)(ii))";
313 QTest::newRow("QLineF") << int(QMetaType::QLineF) << "((dd)(dd))";
314
315 QTest::newRow("Struct1") << qMetaTypeId<Struct1>() << "(s)";
316 QTest::newRow("QList<Struct1>") << qMetaTypeId<QList<Struct1> >() << "a(s)";
317 #ifdef Q_CC_GNU_ONLY
318 QTest::newRow("std::vector<Struct1>") << qMetaTypeId<std::vector<Struct1> >() << "a(s)";
319 #endif
320
321 QTest::newRow("Struct2") << qMetaTypeId<Struct2>() << "(sos)";
322 QTest::newRow("QList<Struct2>") << qMetaTypeId<QList<Struct2>>() << "a(sos)";
323
324 QTest::newRow("QList<Struct3>") << qMetaTypeId<QList<Struct3>>() << "a(sas)";
325 QTest::newRow("Struct3") << qMetaTypeId<Struct3>() << "(sas)";
326
327 QTest::newRow("Struct4") << qMetaTypeId<Struct4>() << "(ssa(ss)sayasx)";
328 QTest::newRow("QList<Struct4>") << qMetaTypeId<QList<Struct4>>() << "a(ssa(ss)sayasx)";
329
330 QTest::newRow("Struct5") << qMetaTypeId<Struct5>() << "a{sa{sv}}";
331
332 QTest::newRow("Struct6") << qMetaTypeId<Struct6>() << "av";
333
334 QTest::newRow("QMap<int,QString>") << intStringMap << "a{is}";
335 QTest::newRow("QMap<QString,QString>") << stringStringMap << "a{ss}";
336 QTest::newRow("QMap<QString,Struct1>") << stringStruct1Map << "a{s(s)}";
337 }
338
339 void tst_QDBusMetaType::staticTypes()
340 {
341 QFETCH(int, typeId);
342
343 QString result = QDBusMetaType::typeToSignature(QMetaType(typeId));
344 QTEST(result, "signature");
345 }
346
347 void tst_QDBusMetaType::dynamicTypes()
348 {
349 // same test
350 staticTypes();
351 }
352
353 void tst_QDBusMetaType::invalidTypes_data()
354 {
355 QTest::addColumn<int>("typeId");
356 QTest::addColumn<QString>("signature");
357
358 QTest::newRow("Invalid0") << qMetaTypeId<Invalid0>() << "";
359 QTest::newRow("Invalid1") << qMetaTypeId<Invalid1>() << "";
360 QTest::newRow("Invalid2") << qMetaTypeId<Invalid2>() << "";
361 QTest::newRow("Invalid3") << qMetaTypeId<Invalid3>() << "";
362 QTest::newRow("Invalid4") << qMetaTypeId<Invalid4>() << "";
363 QTest::newRow("Invalid5") << qMetaTypeId<Invalid5>() << "";
364 QTest::newRow("Invalid6") << qMetaTypeId<Invalid6>() << "";
365 QTest::newRow("Invalid7") << qMetaTypeId<Invalid7>() << "";
366
367 QTest::newRow("QList<Invalid0>") << qMetaTypeId<QList<Invalid0>>() << "";
368
369 QTest::newRow("long") << int(QMetaType::Long) << "";
370 QTest::newRow("void*") << int(QMetaType::VoidStar) << "";
371 }
372
373 void tst_QDBusMetaType::invalidTypes()
374 {
375 // same test
376 if (qstrcmp(QTest::currentDataTag(), "Invalid0") == 0)
377 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid0' produces invalid D-Bus signature '<empty>' (Did you forget to call beginStructure() ?)");
378 else if (qstrcmp(QTest::currentDataTag(), "Invalid1") == 0)
379 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid1' attempts to redefine basic D-Bus type 's' (QString) (Did you forget to call beginStructure() ?)");
380 else if (qstrcmp(QTest::currentDataTag(), "Invalid2") == 0)
381 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid2' attempts to redefine basic D-Bus type 'o' (QDBusObjectPath) (Did you forget to call beginStructure() ?)");
382 else if (qstrcmp(QTest::currentDataTag(), "Invalid3") == 0)
383 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid3' attempts to redefine basic D-Bus type 'as' (QStringList) (Did you forget to call beginStructure() ?)");
384 else if (qstrcmp(QTest::currentDataTag(), "Invalid4") == 0)
385 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid4' attempts to redefine basic D-Bus type 'ay' (QByteArray) (Did you forget to call beginStructure() ?)");
386 else if (qstrcmp(QTest::currentDataTag(), "Invalid5") == 0)
387 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid5' produces invalid D-Bus signature 'ii' (Did you forget to call beginStructure() ?)");
388 else if (qstrcmp(QTest::currentDataTag(), "Invalid7") == 0)
389 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'Invalid7' produces invalid D-Bus signature '()' (Did you forget to call beginStructure() ?)");
390 else if (qstrcmp(QTest::currentDataTag(), "QList<Invalid0>") == 0)
391 QTest::ignoreMessage(QtWarningMsg, "QDBusMarshaller: type 'QList<Invalid0>' produces invalid D-Bus signature 'a' (Did you forget to call beginStructure() ?)");
392
393 staticTypes();
394 staticTypes(); // run twice: the error messages should be printed once only
395 }
396
397 QTEST_MAIN(tst_QDBusMetaType)
398
399 #include "tst_qdbusmetatype.moc"