summaryrefslogtreecommitdiff
path: root/tests/tst_metaobject/tst_metaobjecttest.cpp
blob: 4ae89fef976554a038da0fc4017976d9f5ce5074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// QtQuickStreamer, QML bindings for the GStreamer multimedia framework
// Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  US
//
// Author: Mathias Hasselmann <mathias@taschenorakel.de>
//
#include "QuickStreamer/item.h"
#include "testutils/testcontext.h"

#include <gst/gst.h>

#include <QCoreApplication>
#include <QQmlParserStatus>
#include <QtTest>

using QQuickStreamer::Item;

class MetaObjectTest : public QObject, TestContext
{
    Q_OBJECT

    typedef QPair<int, QByteArray> NameValuePair;

public:
    MetaObjectTest()
        : TestContext(SRCDIR, TOP_SRCDIR, OUTDIR, TOP_OUTDIR)
    {
    }

private slots:
    void init()
    {
        gst_init(Q_NULLPTR, Q_NULLPTR);
        QVERIFY(Item::registerObjectClass(GST_TYPE_PIPELINE));
        QVERIFY(Item::registerElementFactory("audiotestsrc"));
        QVERIFY(Item::registerElementFactory("encodebin"));
    }

    void testClassName()
    {
        Item item(GST_OBJECT(gst_pipeline_new(Q_NULLPTR)));
        QCOMPARE(item.metaObject()->className(), "GstPipeline");
    }

    void testProperties_data()
    {
        QTest::addColumn<QByteArray>("factory");
        QTest::addColumn<QByteArray>("name");
        QTest::addColumn<QByteArray>("type");
        QTest::addColumn<bool>("readable");
        QTest::addColumn<bool>("writable");
        QTest::addColumn<bool>("resetable");
        QTest::addColumn<bool>("enumType");

        QTest::newRow("name")
                << QByteArrayLiteral("pipeline")
                << QByteArrayLiteral("name")
                << QByteArrayLiteral("QString")
                << true << true << true << false;

        QTest::newRow("children")
                << QByteArrayLiteral("pipeline")
                << QByteArrayLiteral("children")
                << QByteArrayLiteral("QQmlListProperty<GstElement>")
                << true << false << false << false;

        QTest::newRow("state")
                << QByteArrayLiteral("pipeline")
                << QByteArrayLiteral("state")
                << QByteArrayLiteral("GstState::Values")
                << true << true << true << true;

        QTest::newRow("wave")
                << QByteArrayLiteral("audiotestsrc")
                << QByteArrayLiteral("wave")
                << QByteArrayLiteral("GstAudioTestSrcWave::Values")
                << true << true << true << true;
    }

    void testProperties()
    {
        QFETCH(QByteArray, factory);
        QFETCH(QByteArray, name);
        QFETCH(QByteArray, type);
        QFETCH(bool, readable);
        QFETCH(bool, writable);
        QFETCH(bool, resetable);
        QFETCH(bool, enumType);

        Item item(GST_OBJECT(gst_element_factory_make(factory.constData(), Q_NULLPTR)));
        QVERIFY(item.target());

        const int propertyIndex = item.metaObject()->indexOfProperty(name.constData());

        QVERIFY(propertyIndex >= 0);

        const auto &property = item.metaObject()->property(propertyIndex);

        QCOMPARE(property.isReadable(), readable);
        QCOMPARE(property.isWritable(), writable);
        QCOMPARE(property.isResettable(), resetable);
        QCOMPARE(property.isEnumType(), enumType);
        QVERIFY(property.hasNotifySignal());
        QCOMPARE(property.typeName(), type.constData());
        QVERIFY(QMetaType::type(type.constData()) != 0);
        QCOMPARE(property.name(), name.constData());
    }

    void testQObjectCast()
    {
        Item item(GST_OBJECT(gst_pipeline_new(Q_NULLPTR)));
        QCOMPARE(qobject_cast<QQmlParserStatus *>(&item), &item);
    }

    void testEnumerators_data()
    {
        QTest::addColumn<QByteArray>("typeName");
        QTest::addColumn<bool>("isFlag");
        QTest::addColumn<QList<NameValuePair>>("values");

        QTest::newRow("enum")
                << QByteArrayLiteral("GstAudioTestSrcWave") << false
                << QList<NameValuePair>({
                                            qMakePair(0, QByteArrayLiteral("Sine")),
                                            qMakePair(1, QByteArrayLiteral("Square")),
                                            qMakePair(5, QByteArrayLiteral("WhiteNoise"))
                                        });

        QTest::newRow("state")
                << QByteArrayLiteral("GstState") << false
                << QList<NameValuePair>({
                                            qMakePair(0, QByteArrayLiteral("VoidPending")), // FIXME: explicitly rename to just "Pending"?
                                            qMakePair(1, QByteArrayLiteral("Null")),
                                            qMakePair(2, QByteArrayLiteral("Ready")),
                                            qMakePair(3, QByteArrayLiteral("Paused")),
                                            qMakePair(4, QByteArrayLiteral("Playing"))
                                        });

        QTest::newRow("flags")
                << QByteArrayLiteral("GstEncodeBinFlags") << true
                << QList<NameValuePair>({
                                            qMakePair(1, QByteArrayLiteral("NoAudioConversion")),
                                            qMakePair(2, QByteArrayLiteral("NoVideoConversion"))
                                        });
    }

    void testEnumerators()
    {
        QFETCH(QByteArray, typeName);
        QFETCH(bool, isFlag);
        QFETCH(QList<NameValuePair>, values);

        const int typeId = QMetaType::type(typeName.constData());
        QVERIFY(typeId > 0);

        auto *const metaObject = QMetaType::metaObjectForType(typeId);
        QVERIFY(metaObject != Q_NULLPTR);
        QCOMPARE(metaObject->enumeratorCount(), 1);

        const auto enumerator = metaObject->enumerator(0);
        QVERIFY(enumerator.isValid());
        QCOMPARE(enumerator.name(), "Values");
        QCOMPARE(enumerator.isFlag(), isFlag);

        for (const auto &p: values) {
            QCOMPARE(enumerator.valueToKey(p.first), p.second.constData());

            bool ok = false;
            QCOMPARE(enumerator.keyToValue(p.second, &ok), p.first);
            QVERIFY(ok);
        }
    }
};

QTEST_MAIN(MetaObjectTest)

#include "tst_metaobjecttest.moc"