Debian Codegraph @

tests/auto/serviceframework/qserviceinterfacedescriptor/tst_qserviceinterfacedescriptor.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module 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 //TESTED_COMPONENT=src/serviceframework
30
31 #include <QtTest/QtTest>
32 #include <QtCore>
33 #ifndef QT_NO_DEBUG_STREAM
34 #include <QDebug>
35 #endif
36 #include <qserviceinterfacedescriptor.h>
37 #include <private/qserviceinterfacedescriptor_p.h>
38
39 QT_USE_NAMESPACE
40 class tst_QServiceInterfaceDescriptor: public QObject
41 {
42 Q_OBJECT
43
44 private slots:
45 void initTestCase();
46 void cleanupTestCase();
47 void comparison();
48 #ifndef QT_NO_DATASTREAM
49 void testStreamOperators();
50 #endif
51 #ifndef QT_NO_DEBUG_STREAM
52 void testDebugStream();
53 #endif
54 void destructor();
55 };
56
57 void tst_QServiceInterfaceDescriptor::initTestCase()
58 {
59 }
60
61 void tst_QServiceInterfaceDescriptor::comparison()
62 {
63 QServiceInterfaceDescriptor desc;
64 QVERIFY(desc.majorVersion() == -1);
65 QVERIFY(desc.minorVersion() == -1);
66 QVERIFY(desc.serviceName().isEmpty());
67 QVERIFY(desc.interfaceName().isEmpty());
68 QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
69 QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::Location).isValid());
70 QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
71 QVERIFY(!desc.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
72 QVERIFY(desc.scope() == QService::UserScope);
73 QVERIFY(!desc.isValid());
74
75 QServiceInterfaceDescriptor copy(desc);
76 QVERIFY(copy.majorVersion() == -1);
77 QVERIFY(copy.minorVersion() == -1);
78 QVERIFY(copy.serviceName().isEmpty());
79 QVERIFY(copy.interfaceName().isEmpty());
80 QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Capabilities).isValid());
81 QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::Location).isValid());
82 QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::InterfaceDescription).isValid());
83 QVERIFY(!copy.attribute(QServiceInterfaceDescriptor::ServiceDescription).isValid());
84 QVERIFY(copy.scope() == QService::UserScope);
85 QVERIFY(!copy.isValid());
86
87 QVERIFY(desc == copy);
88
89 QServiceInterfaceDescriptor valid;
90 QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
91 QServiceInterfaceDescriptorPrivate::setPrivate(&valid, d);
92 d->serviceName = "name";
93 d->interfaceName = "interface";
94 d->major = 3;
95 d->minor = 1;
96 d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("mydescription"));
97 d->customAttributes.insert(QString("ckey"), QString("cvalue"));
98 d->scope = QService::SystemScope;
99
100 QCOMPARE(valid.interfaceName(), QString("interface"));
101 QCOMPARE(valid.serviceName(), QString("name"));
102 QCOMPARE(valid.majorVersion(), 3);
103 QCOMPARE(valid.minorVersion(), 1);
104 QCOMPARE(valid.customAttribute("ckey"), QString("cvalue"));
105 QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
106 QCOMPARE(valid.attribute(QServiceInterfaceDescriptor::Location).toString(), QString(""));
107 QCOMPARE(valid.scope(), QService::SystemScope);
108 QVERIFY(valid.isValid());
109
110 QVERIFY(valid != desc);
111 QVERIFY(desc != valid);
112
113 //test copy constructor
114 QServiceInterfaceDescriptor validCopy(valid);
115 QVERIFY(valid==validCopy);
116 QVERIFY(validCopy==valid);
117
118 QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
119 QVERIFY(valid!=validCopy);
120 QVERIFY(validCopy!=valid);
121
122 QCOMPARE(validCopy.interfaceName(), QString("interface"));
123 QCOMPARE(validCopy.serviceName(), QString("name"));
124 QCOMPARE(validCopy.majorVersion(), 3);
125 QCOMPARE(validCopy.minorVersion(), 1);
126 QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue"));
127 QCOMPARE(validCopy.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
128 QCOMPARE(validCopy.customAttribute("ckey"),QString("cvalue"));
129 QCOMPARE(validCopy.scope(), QService::SystemScope);
130 QVERIFY(validCopy.isValid());
131
132 //test assignment operator
133 QServiceInterfaceDescriptor validCopy2 = valid;
134 QVERIFY(valid==validCopy2);
135 QVERIFY(validCopy2==valid);
136
137 QServiceInterfaceDescriptorPrivate::getPrivate(&validCopy2)->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue2"));
138 QVERIFY(valid!=validCopy2);
139 QVERIFY(validCopy2!=valid);
140
141 QCOMPARE(validCopy2.interfaceName(), QString("interface"));
142 QCOMPARE(validCopy2.serviceName(), QString("name"));
143 QCOMPARE(validCopy2.majorVersion(), 3);
144 QCOMPARE(validCopy2.minorVersion(), 1);
145 QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::Location).toString(), QString("myValue2"));
146 QCOMPARE(validCopy2.customAttribute("ckey"),QString("cvalue"));
147 QCOMPARE(validCopy2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString(), QString("mydescription"));
148 QCOMPARE(validCopy2.scope(), QService::SystemScope);
149 QVERIFY(validCopy2.isValid());
150
151 //test customAttributes
152 d->customAttributes.insert(QString("ckey"), QString("cvalue"));
153 d->customAttributes.insert(QString("ckey1"), QString("cvalue1"));
154 d->customAttributes.insert(QString("ckey2"), QString("cvalue2"));
155 QStringList customAttributes = valid.customAttributes();
156 QVERIFY(customAttributes.contains("ckey"));
157 QVERIFY(customAttributes.contains("ckey1"));
158 QVERIFY(customAttributes.contains("ckey2"));
159 QCOMPARE(customAttributes.count(), 3);
160 }
161
162 #ifndef QT_NO_DATASTREAM
163 void tst_QServiceInterfaceDescriptor::testStreamOperators()
164 {
165 QByteArray byteArray;
166 QBuffer buffer(&byteArray);
167 buffer.open(QIODevice::ReadWrite);
168 QDataStream stream(&buffer);
169
170
171 QServiceInterfaceDescriptor empty;
172 QVERIFY(!empty.isValid());
173
174 //stream invalid into invalid
175 QServiceInterfaceDescriptor invalid;
176 QVERIFY(!invalid.isValid());
177 QVERIFY(invalid == empty);
178 buffer.seek(0);
179 stream << empty;
180 buffer.seek(0);
181 stream >> invalid;
182 QVERIFY(invalid == empty);
183
184 //stream invalid into valid
185 QServiceInterfaceDescriptor valid;
186 QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
187 QServiceInterfaceDescriptorPrivate::setPrivate(&valid, d);
188 d->serviceName = "name";
189 d->interfaceName = "interface";
190 d->major = 3;
191 d->minor = 1;
192 d->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
193 d->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val1" << "val2");
194 d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the service description"));
195 d->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the interface description"));
196 d->customAttributes.insert(QString("key1"), QString("value1"));
197 d->customAttributes.insert(QString("abcd"), QString("efgh"));
198 d->customAttributes.insert(QString("empty"), QString(""));
199 d->scope = QService::SystemScope;
200 QVERIFY(valid.isValid());
201 QServiceInterfaceDescriptor validref = valid;
202 QVERIFY(validref == valid);
203 QVERIFY(!(validref!=valid));
204 QVERIFY(empty!=validref);
205
206 buffer.seek(0);
207 stream << empty;
208 buffer.seek(0);
209 stream >> validref;
210 QVERIFY(empty == validref);
211 QVERIFY(!(empty!=validref));
212 QVERIFY(validref != valid);
213
214 //stream valid into invalid
215 QServiceInterfaceDescriptor invalid2;
216 QVERIFY(!invalid2.isValid());
217 validref = valid;
218 QVERIFY(validref == valid);
219 QVERIFY(validref != invalid2);
220
221 buffer.seek(0);
222 stream << validref;
223 buffer.seek(0);
224 stream >> invalid2;
225 QVERIFY(invalid2 == validref);
226 QVERIFY(!(invalid2 != validref));
227 QVERIFY(invalid2.isValid());
228 QVERIFY(invalid2.interfaceName() == QString("interface"));
229 QVERIFY(invalid2.serviceName() == QString("name"));
230 QVERIFY(invalid2.majorVersion() == 3);
231 QVERIFY(invalid2.minorVersion() == 1);
232 QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::Location).toString() == QString("myValue"));
233 QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList() == (QStringList() << "val1" << "val2"));
234 QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString() == QString("This is the service description"));
235 QVERIFY(invalid2.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString() == QString("This is the interface description"));
236 QCOMPARE(invalid2.customAttribute("key1"), QString("value1"));
237 QCOMPARE(invalid2.customAttribute("abcd"), QString("efgh"));
238 QCOMPARE(invalid2.customAttribute("notvalid"), QString());
239 QVERIFY(invalid2.customAttribute("notvalid").isEmpty());
240 QVERIFY(invalid2.customAttribute("notvalid").isNull());
241 QCOMPARE(invalid2.customAttribute("empty"), QString(""));
242 QVERIFY(invalid2.customAttribute("empty").isEmpty());
243 QVERIFY(!invalid2.customAttribute("empty").isNull());
244 QCOMPARE(invalid2.scope(), QService::SystemScope);
245
246 //stream valid into valid
247 QServiceInterfaceDescriptor valid2;
248 QServiceInterfaceDescriptorPrivate *d2 = new QServiceInterfaceDescriptorPrivate();
249 QServiceInterfaceDescriptorPrivate::setPrivate(&valid2, d2);
250 d2->serviceName = "name2";
251 d2->interfaceName = "interface2";
252 d2->major = 5;
253 d2->minor = 6;
254 d2->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue1"));
255 d2->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val3" << "val4");
256 d2->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the second service description"));
257 d2->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the second interface description"));
258 d2->customAttributes.insert(QString("key1"), QString("value2"));
259 d2->customAttributes.insert(QString("abcd1"), QString("efgh"));
260 d2->customAttributes.insert(QString("empty"), QString(""));
261 d2->scope = QService::UserScope;
262 QVERIFY(valid2.isValid());
263 QCOMPARE(valid2.customAttribute("key1"), QString("value2"));
264 QCOMPARE(valid2.customAttribute("abcd1"), QString("efgh"));
265 QCOMPARE(valid2.customAttribute("abcd"), QString());
266 QVERIFY(valid2.customAttribute("abcd").isEmpty());
267 QVERIFY(valid2.customAttribute("abcd").isNull());
268 QCOMPARE(valid2.customAttribute("empty"), QString(""));
269 QVERIFY(valid2.customAttribute("empty").isEmpty());
270 QVERIFY(!valid2.customAttribute("empty").isNull());
271
272
273 QVERIFY(valid2 != valid);
274 QVERIFY(!(valid2 == valid));
275
276 buffer.seek(0);
277 stream << valid;
278 buffer.seek(0);
279 stream >> valid2;
280 QVERIFY(valid2 == valid);
281 QVERIFY(!(valid2 != valid));
282 QVERIFY(valid2.isValid());
283 QVERIFY(valid2.interfaceName() == QString("interface"));
284 QVERIFY(valid2.serviceName() == QString("name"));
285 QVERIFY(valid2.majorVersion() == 3);
286 QVERIFY(valid2.minorVersion() == 1);
287 QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::Location).toString() == QString("myValue"));
288 QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::Capabilities).toStringList() == (QStringList() << "val1" << "val2"));
289 QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::ServiceDescription).toString() == QString("This is the service description"));
290 QVERIFY(valid2.attribute(QServiceInterfaceDescriptor::InterfaceDescription).toString() == QString("This is the interface description"));
291 QCOMPARE(valid2.customAttribute("key1"), QString("value1"));
292 QCOMPARE(valid2.customAttribute("abcd"), QString("efgh"));
293 QCOMPARE(valid2.customAttribute("notvalid"), QString());
294 QVERIFY(valid2.customAttribute("notvalid").isEmpty());
295 QVERIFY(valid2.customAttribute("notvalid").isNull());
296 QCOMPARE(valid2.customAttribute("empty"), QString(""));
297 QVERIFY(valid2.customAttribute("empty").isEmpty());
298 QVERIFY(!valid2.customAttribute("empty").isNull());
299 QCOMPARE(valid2.customAttribute("abcd1"), QString());
300 QCOMPARE(valid2.scope(), QService::SystemScope);
301 }
302 #endif //QT_NO_DATASTREAM
303
304 #ifndef QT_NO_DEBUG_STREAM
305 static QString msg;
306 static QtMsgType type;
307
308 static void customMsgHandler(QtMsgType t, const QMessageLogContext &, const QString &m)
309 {
310 msg = m;
311 type = t;
312
313 }
314
315 void tst_QServiceInterfaceDescriptor::testDebugStream()
316 {
317 QServiceInterfaceDescriptor valid2;
318 QServiceInterfaceDescriptorPrivate *d2 = new QServiceInterfaceDescriptorPrivate();
319 QServiceInterfaceDescriptorPrivate::setPrivate(&valid2, d2);
320 d2->serviceName = "name2";
321 d2->interfaceName = "interface2";
322 d2->major = 5;
323 d2->minor = 6;
324 d2->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue1"));
325 d2->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val3" << "val4");
326 d2->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the second service description"));
327 d2->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the second interface description"));
328 QVERIFY(valid2.isValid());
329
330 QServiceInterfaceDescriptor invalid;
331
332 qInstallMessageHandler(customMsgHandler);
333 qDebug() << valid2 << invalid;
334 QCOMPARE(type, QtDebugMsg);
335 QCOMPARE(msg,QString::fromLatin1("QServiceInterfaceDescriptor(service=\"name2\", interface=\"interface2 5.6\") QServiceInterfaceDescriptor(invalid)"));
336 qInstallMessageHandler(0);
337 }
338 #endif
339
340 void tst_QServiceInterfaceDescriptor::destructor()
341 {
342 //test destructor if descriptor is invalid
343 QServiceInterfaceDescriptor* invalid = new QServiceInterfaceDescriptor();
344 delete invalid;
345
346 //test destructor if descriptor is valid
347 QServiceInterfaceDescriptor* valid = new QServiceInterfaceDescriptor();
348 QServiceInterfaceDescriptorPrivate *d = new QServiceInterfaceDescriptorPrivate();
349 QServiceInterfaceDescriptorPrivate::setPrivate(valid, d);
350 d->serviceName = "name";
351 d->interfaceName = "interface";
352 d->major = 3;
353 d->minor = 1;
354 d->attributes.insert(QServiceInterfaceDescriptor::Location, QString("myValue"));
355 d->attributes.insert(QServiceInterfaceDescriptor::Capabilities, QStringList() << "val1" << "val2");
356 d->attributes.insert(QServiceInterfaceDescriptor::ServiceDescription, QString("This is the service description"));
357 d->attributes.insert(QServiceInterfaceDescriptor::InterfaceDescription, QString("This is the interface description"));
358 d->customAttributes.insert("ckey", "cvalue");
359 QVERIFY(valid->isValid());
360 delete valid;
361 }
362
363 void tst_QServiceInterfaceDescriptor::cleanupTestCase()
364 {
365 }
366
367 QTEST_MAIN(tst_QServiceInterfaceDescriptor)
368 #include "tst_qserviceinterfacedescriptor.moc"