1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite 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 | #include <QtTest/QtTest> |
30 | |
31 | #include "test1.h" |
32 | |
33 | #include <QtDBus/QDBusConnection> |
34 | |
35 | // in qdbusxmlgenerator.cpp |
36 | QT_BEGIN_NAMESPACE |
37 | extern Q_DBUS_EXPORT QString qDBusGenerateMetaObjectXml(QString interface, |
38 | const QMetaObject *mo, |
39 | const QMetaObject *base, |
40 | int flags); |
41 | QT_END_NAMESPACE |
42 | |
43 | static QString (const QString &input) |
44 | { |
45 | return |
46 | "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n<node>" |
47 | + (input.isEmpty() ? QString() : QString("\n " + input.trimmed())) |
48 | + "\n</node>\n" ; |
49 | } |
50 | |
51 | class tst_qdbuscpp2xml : public QObject |
52 | { |
53 | Q_OBJECT |
54 | |
55 | private slots: |
56 | void qdbuscpp2xml_data(); |
57 | void qdbuscpp2xml(); |
58 | |
59 | void initTestCase(); |
60 | void cleanupTestCase(); |
61 | |
62 | private: |
63 | QHash<QString, QObject*> m_tests; |
64 | }; |
65 | |
66 | void tst_qdbuscpp2xml::initTestCase() |
67 | { |
68 | m_tests.insert(akey: "test1" , avalue: new Test1); |
69 | } |
70 | |
71 | void tst_qdbuscpp2xml::cleanupTestCase() |
72 | { |
73 | qDeleteAll(c: m_tests); |
74 | } |
75 | |
76 | void tst_qdbuscpp2xml::qdbuscpp2xml_data() |
77 | { |
78 | QTest::addColumn<QString>(name: "inputfile" ); |
79 | QTest::addColumn<int>(name: "flags" ); |
80 | |
81 | QBitArray doneFlags(QDBusConnection::ExportAllContents + 1); |
82 | for (int flag = 0x10; flag < QDBusConnection::ExportScriptableContents; flag += 0x10) { |
83 | QTest::newRow(dataTag: "xmlgenerator-" + QByteArray::number(flag)) << "test1" << flag; |
84 | doneFlags.setBit(flag); |
85 | for (int mask = QDBusConnection::ExportAllSlots; mask <= QDBusConnection::ExportAllContents; mask += 0x110) { |
86 | int flags = flag | mask; |
87 | if (doneFlags.testBit(i: flags)) |
88 | continue; |
89 | QTest::newRow(dataTag: "xmlgenerator-" + QByteArray::number(flags)) << "test1" << flags; |
90 | doneFlags.setBit(flags); |
91 | } |
92 | } |
93 | } |
94 | |
95 | void tst_qdbuscpp2xml::qdbuscpp2xml() |
96 | { |
97 | QFETCH(QString, inputfile); |
98 | QFETCH(int, flags); |
99 | |
100 | // qdbuscpp2xml considers these equivalent |
101 | if (flags & QDBusConnection::ExportScriptableSlots) |
102 | flags |= QDBusConnection::ExportScriptableInvokables; |
103 | if (flags & QDBusConnection::ExportNonScriptableSlots) |
104 | flags |= QDBusConnection::ExportNonScriptableInvokables; |
105 | |
106 | if (flags & QDBusConnection::ExportScriptableInvokables) |
107 | flags |= QDBusConnection::ExportScriptableSlots; |
108 | if (flags & QDBusConnection::ExportNonScriptableInvokables) |
109 | flags |= QDBusConnection::ExportNonScriptableSlots; |
110 | |
111 | QStringList options; |
112 | if (flags & QDBusConnection::ExportScriptableProperties) { |
113 | if (flags & QDBusConnection::ExportNonScriptableProperties) |
114 | options << "-P" ; |
115 | else |
116 | options << "-p" ; |
117 | } |
118 | if (flags & QDBusConnection::ExportScriptableSignals) { |
119 | if (flags & QDBusConnection::ExportNonScriptableSignals) |
120 | options << "-S" ; |
121 | else |
122 | options << "-s" ; |
123 | } |
124 | if (flags & QDBusConnection::ExportScriptableSlots) { |
125 | if (flags & QDBusConnection::ExportNonScriptableSlots) |
126 | options << "-M" ; |
127 | else |
128 | options << "-m" ; |
129 | } |
130 | |
131 | // Launch |
132 | const QString binpath = QLibraryInfo::location(QLibraryInfo::BinariesPath); |
133 | const QString command = binpath + QLatin1String("/qdbuscpp2xml" ); |
134 | QProcess process; |
135 | process.start(program: command, arguments: QStringList() << options << (QFINDTESTDATA(inputfile + QStringLiteral(".h" )))); |
136 | if (!process.waitForFinished()) { |
137 | const QString path = QString::fromLocal8Bit(str: qgetenv(varName: "PATH" )); |
138 | QString message = QString::fromLatin1(str: "'%1' could not be found when run from '%2'. Path: '%3' " ). |
139 | arg(args: command, args: QDir::currentPath(), args: path); |
140 | QFAIL(qPrintable(message)); |
141 | } |
142 | const QChar cr = QLatin1Char('\r'); |
143 | const QString err = QString::fromLocal8Bit(str: process.readAllStandardError()).remove(c: cr); |
144 | const QString out = QString::fromLatin1(str: process.readAllStandardOutput()).remove(c: cr); |
145 | |
146 | if (!err.isEmpty()) { |
147 | qDebug() << "UNEXPECTED STDERR CONTENTS: " << err; |
148 | QFAIL("UNEXPECTED STDERR CONTENTS" ); |
149 | } |
150 | |
151 | const QChar nl = QLatin1Char('\n'); |
152 | const QStringList actualLines = out.split(sep: nl); |
153 | |
154 | QObject *testObject = m_tests.value(akey: inputfile); |
155 | |
156 | if (flags == 0) |
157 | flags = QDBusConnection::ExportScriptableContents |
158 | | QDBusConnection::ExportNonScriptableContents; |
159 | |
160 | QString expected = qDBusGenerateMetaObjectXml(interface: QString(), mo: testObject->metaObject(), base: &QObject::staticMetaObject, flags); |
161 | |
162 | expected = addXmlHeader(input: expected); |
163 | |
164 | QCOMPARE(out, expected); |
165 | } |
166 | |
167 | QTEST_APPLESS_MAIN(tst_qdbuscpp2xml) |
168 | |
169 | #include "tst_qdbuscpp2xml.moc" |
170 | |