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

source code of qtsystems/tests/auto/serviceframework/qserviceinterfacedescriptor/tst_qserviceinterfacedescriptor.cpp