File indexing completed on 2024-04-28 04:44:10

0001 /**
0002  * Copyright (C)  2007  Brad Hards <bradh@frogmouth.net>
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  *
0008  * 1. Redistributions of source code must retain the above copyright
0009  *   notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *   notice, this list of conditions and the following disclaimer in the
0012  *   documentation and/or other materials provided with the distribution.
0013  *
0014  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0015  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0016  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0017  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0018  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0019  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0020  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0021  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0022  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0023  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0024  */
0025 
0026 #include <QtCrypto>
0027 #include <QtTest/QtTest>
0028 
0029 #include <limits>
0030 
0031 #ifdef QT_STATICPLUGIN
0032 #include "import_plugins.h"
0033 #endif
0034 
0035 class TestClass1 : public QObject
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040     TestClass1() {};
0041     TestClass1(const TestClass1 &)
0042         : QObject(nullptr) {};
0043 
0044 public Q_SLOTS:
0045     void    voidMethod() {};
0046     QString qstringMethod()
0047     {
0048         return QString();
0049     };
0050     bool boolMethod(const QString &)
0051     {
0052         return true;
0053     };
0054     QString returnArg(const QString &s)
0055     {
0056         return s;
0057     };
0058     QByteArray returnArg(const QByteArray &a)
0059     {
0060         return a;
0061     };
0062     QString returnRepeatArg(const QString &s)
0063     {
0064         return QString(s + s);
0065     };
0066     QString tenArgs(const QString &s, int, int, int, int, int, int, int, int, int)
0067     {
0068         return QString(s);
0069     };
0070     QString elevenArgs(const QString &s, int, int, int, int, int, int, int, int, int, int)
0071     {
0072         return QString(s);
0073     };
0074 };
0075 
0076 Q_DECLARE_METATYPE(TestClass1)
0077 
0078 class MetaTypeUnitTest : public QObject
0079 {
0080     Q_OBJECT
0081 
0082 private Q_SLOTS:
0083     void initTestCase();
0084     void cleanupTestCase();
0085     void returnTypeTest();
0086     void invokeMethodTest();
0087 
0088 private:
0089     QCA::Initializer *m_init;
0090 };
0091 
0092 void MetaTypeUnitTest::initTestCase()
0093 {
0094     m_init = new QCA::Initializer;
0095 }
0096 
0097 void MetaTypeUnitTest::cleanupTestCase()
0098 {
0099     QCA::unloadAllPlugins();
0100     delete m_init;
0101 }
0102 
0103 void MetaTypeUnitTest::returnTypeTest()
0104 {
0105     TestClass1        testClass1;
0106     QList<QByteArray> args;
0107 
0108 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0109     // returns a null type name because that is what void does...
0110     QCOMPARE(QMetaType::Void, QCA::methodReturnType(testClass1.metaObject(), QByteArray("voidMethod"), args));
0111     QCOMPARE(QMetaType::QString, QCA::methodReturnType(testClass1.metaObject(), QByteArray("qstringMethod"), args));
0112 
0113     // returns a null type, because args don't match
0114     QCOMPARE(QMetaType::UnknownType, QCA::methodReturnType(testClass1.metaObject(), QByteArray("boolMethod"), args));
0115 
0116     args << "QString";
0117     QCOMPARE(QMetaType::QString, QCA::methodReturnType(testClass1.metaObject(), QByteArray("returnArg"), args));
0118     QCOMPARE(QMetaType::Bool, QCA::methodReturnType(testClass1.metaObject(), QByteArray("boolMethod"), args));
0119     args.clear();
0120 
0121     args << "QByteArray";
0122     QCOMPARE(QMetaType::QByteArray, QCA::methodReturnType(testClass1.metaObject(), QByteArray("returnArg"), args));
0123     args.clear();
0124 
0125     args << "QString"
0126          << "int"
0127          << "int"
0128          << "int"
0129          << "int"
0130          << "int"
0131          << "int"
0132          << "int"
0133          << "int";
0134 
0135     // wrong number of arguments - has 9, needs 10
0136     QCOMPARE(QMetaType::UnknownType, QCA::methodReturnType(testClass1.metaObject(), QByteArray("tenArgs"), args));
0137 
0138     // match
0139     args << "int";
0140     QCOMPARE(QMetaType::QString, QCA::methodReturnType(testClass1.metaObject(), QByteArray("tenArgs"), args));
0141 
0142     args << "int";
0143     QCOMPARE(QMetaType::QString, QCA::methodReturnType(testClass1.metaObject(), QByteArray("elevenArgs"), args));
0144 #else
0145     // returns a null type name because that is what void does...
0146     QCOMPARE(QByteArray("void"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("voidMethod"), args));
0147     QCOMPARE(QByteArray("QString"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("qstringMethod"), args));
0148 
0149     // returns a null type, because args don't match
0150     QCOMPARE(QByteArray(""), QCA::methodReturnType(testClass1.metaObject(), QByteArray("boolMethod"), args));
0151 
0152     args << "QString";
0153     QCOMPARE(QByteArray("QString"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("returnArg"), args));
0154     QCOMPARE(QByteArray("bool"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("boolMethod"), args));
0155     args.clear();
0156 
0157     args << "QByteArray";
0158     QCOMPARE(QByteArray("QByteArray"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("returnArg"), args));
0159     args.clear();
0160 
0161     args << "QString"
0162          << "int"
0163          << "int"
0164          << "int"
0165          << "int"
0166          << "int"
0167          << "int"
0168          << "int"
0169          << "int";
0170 
0171     // wrong number of arguments - has 9, needs 10
0172     QCOMPARE(QByteArray(""), QCA::methodReturnType(testClass1.metaObject(), QByteArray("tenArgs"), args));
0173 
0174     // match
0175     args << "int";
0176     QCOMPARE(QByteArray("QString"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("tenArgs"), args));
0177 
0178     args << "int";
0179     QCOMPARE(QByteArray("QString"), QCA::methodReturnType(testClass1.metaObject(), QByteArray("elevenArgs"), args));
0180 #endif
0181 }
0182 
0183 void MetaTypeUnitTest::invokeMethodTest()
0184 {
0185     TestClass1  *testClass1 = new TestClass1;
0186     QVariantList args;
0187 
0188     bool ret;
0189     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("voidMethod"), args, nullptr);
0190     QVERIFY(ret);
0191 
0192     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("noSuchMethod"), args, nullptr);
0193     QVERIFY(ret == false);
0194 
0195     QVariant stringRes;
0196     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("qstringMethod"), args, &stringRes);
0197     QVERIFY(ret);
0198     QVERIFY(stringRes.isValid());
0199 
0200     QVariant result(false);
0201     QString  fakeArg;
0202     args << fakeArg;
0203     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("boolMethod"), args, &result);
0204     QVERIFY(ret);
0205     QCOMPARE(result.toBool(), true);
0206 
0207     result = QByteArray();
0208     args.clear();
0209     QByteArray myArray("array");
0210     args << myArray;
0211     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("returnArg"), args, &result);
0212     QVERIFY(ret);
0213     QCOMPARE(result.toByteArray(), myArray);
0214 
0215     result = QString();
0216     args.clear();
0217     QString myString = QStringLiteral("test words");
0218     args << myString;
0219     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("returnArg"), args, &result);
0220     QVERIFY(ret);
0221     QCOMPARE(result.toString(), myString);
0222 
0223     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("returnRepeatArg"), args, &result);
0224     QVERIFY(ret);
0225     QCOMPARE(result.toString(), QString(myString + myString));
0226 
0227     // 9 arguments - no matching method
0228     result = QStringLiteral("unchanged");
0229     args << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
0230     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("tenArgs"), args, &result);
0231     QVERIFY(ret == false);
0232     QCOMPARE(result.toString(), QStringLiteral("unchanged"));
0233 
0234     // 10 args
0235     args << 0;
0236     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("tenArgs"), args, &result);
0237     QVERIFY(ret);
0238     QCOMPARE(result.toString(), myString);
0239 
0240     // 11 args
0241     result = QStringLiteral("unchanged");
0242     args << 0;
0243     ret = QCA::invokeMethodWithVariants(testClass1, QByteArray("elevenArgs"), args, &result);
0244     QVERIFY(ret == false);
0245     QCOMPARE(result.toString(), QStringLiteral("unchanged"));
0246 
0247     delete testClass1;
0248 }
0249 
0250 QTEST_MAIN(MetaTypeUnitTest)
0251 
0252 #include "metatype.moc"