summaryrefslogtreecommitdiff
path: root/tests/auto/refpointertest.cpp
blob: 9a21e032a734e22e8638bd6dc8073406c38af781 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
    Copyright (C) 2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
    Copyright (C) 2010 Collabora Ltd.
      @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>

    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 program 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 program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "qgsttest.h"
#include <QGst/Object>
#include <QGst/Message>
#include <QGst/Pipeline>
#include <QGst/ElementFactory>
#include <QGst/UriHandler>
#include <QGst/StreamVolume>

class RefPointerTest : public QGstTest
{
    Q_OBJECT
private Q_SLOTS:
    void refTest1();
    void refTest2();
    void dynamicCastTest();
    void dynamicCastDownObjectTest();
    void dynamicCastUpObjectTest();
    void dynamicCastObjectToIfaceTest();
    void dynamicCastIfaceToObjectTest();
    void cppWrappersTest();
    void messageDynamicCastTest();
    void equalityTest();
};

void RefPointerTest::refTest1()
{
    GstElement *element = gst_bin_new(NULL);
    GstObject *bin1 = GST_OBJECT(element);
    QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(element), 1);

    GstObject *bin2 = GST_OBJECT(gst_object_ref_sink(bin1));
    QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin2), 1);
    QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin2, false);
    QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin2), 1);
}

void RefPointerTest::refTest2()
{
    GstObject *bin = GST_OBJECT(gst_object_ref_sink(GST_OBJECT(gst_bin_new(NULL))));

    {
        QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
        QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 2);
        {
            QGst::ObjectPtr object2 = object;
            QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 3);
        }
    }
    QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 1);
    gst_object_unref(bin);
}

void RefPointerTest::dynamicCastTest()
{
    GstObject *bin = GST_OBJECT(gst_object_ref_sink(GST_OBJECT(gst_bin_new(NULL))));

    {
        QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
        QVERIFY(!object.dynamicCast<QGlib::Object>().isNull());
    }

    {
        QGlib::ObjectPtr object = QGlib::ObjectPtr::wrap(G_OBJECT(bin));
        QVERIFY(!object.dynamicCast<QGst::Object>().isNull());
    }

    gst_object_unref(bin);
}

void RefPointerTest::dynamicCastDownObjectTest()
{
    GstObject *bin = GST_OBJECT(gst_object_ref_sink(gst_bin_new(NULL)));

    {
        QGlib::ObjectPtr object = QGlib::ObjectPtr::wrap(G_OBJECT(bin));
        QVERIFY(!object.dynamicCast<QGst::Object>().isNull());
        QVERIFY(!object.dynamicCast<QGst::Bin>().isNull());
        QVERIFY(object.dynamicCast<QGst::Pipeline>().isNull());
    }

    gst_object_unref(bin);
}

void RefPointerTest::dynamicCastUpObjectTest()
{
    GstBin *bin = GST_BIN(gst_object_ref_sink(gst_bin_new(NULL)));

    {
        QGst::BinPtr object = QGst::BinPtr::wrap(bin);
        QVERIFY(!object.dynamicCast<QGst::Element>().isNull());
        QVERIFY(!object.dynamicCast<QGlib::Object>().isNull());
        QVERIFY(!object.dynamicCast<QGst::ChildProxy>().isNull());
    }

    gst_object_unref(bin);
}

void RefPointerTest::dynamicCastObjectToIfaceTest()
{
    QGst::ElementPtr e = QGst::ElementFactory::make("fakesrc");
    QGst::UriHandlerPtr u = e.dynamicCast<QGst::UriHandler>();
    QVERIFY(u.isNull());

    e = QGst::ElementFactory::make("filesrc");
    u = e.dynamicCast<QGst::UriHandler>();
    QVERIFY(!u.isNull());
}

void RefPointerTest::dynamicCastIfaceToObjectTest()
{
    GstElement *e = gst_element_factory_make("filesrc", NULL);
    gst_object_ref_sink(e);

    QGst::UriHandlerPtr u = QGst::UriHandlerPtr::wrap(GST_URI_HANDLER(e), false);
    QVERIFY(!u.isNull());
    QVERIFY(!u.dynamicCast<QGst::Element>().isNull());
}

void RefPointerTest::cppWrappersTest()
{
    QGst::ElementPtr e = QGst::ElementFactory::make("playbin");
    QVERIFY(!e.isNull());

    {
        QGst::PipelinePtr pipeline = e.dynamicCast<QGst::Pipeline>();
        QVERIFY(!pipeline.isNull());
        //the C++ wrappers must be the same
        QCOMPARE(static_cast<QGlib::RefCountedObject*>(pipeline.operator->()),
                 static_cast<QGlib::RefCountedObject*>(e.operator->()));
    }

    {
        QGst::ChildProxyPtr proxy = e.dynamicCast<QGst::ChildProxy>();
        QVERIFY(!proxy.isNull());
        //the C++ wrappers must be the same
        QCOMPARE(static_cast<QGlib::RefCountedObject*>(proxy.operator->()),
                 static_cast<QGlib::RefCountedObject*>(e.operator->()));
    }

    { //new wrap() should give the same C++ instance
        GstElement *gobj = e;
        QGst::ElementPtr e2 = QGst::ElementPtr::wrap(gobj);
        QCOMPARE(static_cast<QGlib::RefCountedObject*>(e2.operator->()),
                 static_cast<QGlib::RefCountedObject*>(e.operator->()));
    }

    {
        QGst::StreamVolumePtr sv = e.dynamicCast<QGst::StreamVolume>();
        QVERIFY(!sv.isNull());
        //now the C++ wrapper must not be the same, since Pipeline does not inherit StreamVolume
        QVERIFY(static_cast<QGlib::RefCountedObject*>(sv.operator->())
                != static_cast<QGlib::RefCountedObject*>(e.operator->()));
    }

    {
        QGst::MessagePtr msg = QGst::ApplicationMessage::create(e);
        QGst::MessagePtr msg2 = msg;
        QCOMPARE(static_cast<QGlib::RefCountedObject*>(msg.operator->()),
                 static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
        QVERIFY(msg2 == msg);

        QGst::MessagePtr msg3 = QGst::MessagePtr::wrap(msg2);
        QVERIFY(static_cast<QGlib::RefCountedObject*>(msg3.operator->())
                != static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
        QVERIFY(msg3 == msg2);
    }

    {
        QGst::MessagePtr msg = QGst::ElementMessage::create(e);
        QGst::MessagePtr msg2 = msg;
        QCOMPARE(static_cast<QGlib::RefCountedObject*>(msg.operator->()),
                 static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
        QVERIFY(msg2 == msg);

        QGst::MessagePtr msg3 = QGst::MessagePtr::wrap(msg2);
        QVERIFY(static_cast<QGlib::RefCountedObject*>(msg3.operator->())
                != static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
        QVERIFY(msg3 == msg2);
    }
}

void RefPointerTest::messageDynamicCastTest()
{
    QGst::BinPtr bin = QGst::Bin::create();
    QGst::MessagePtr msg = QGst::ApplicationMessage::create(bin);
    QVERIFY(!msg.isNull());
    QVERIFY(!msg.dynamicCast<QGst::ApplicationMessage>().isNull());
    QVERIFY(msg.dynamicCast<QGst::EosMessage>().isNull());
}

void RefPointerTest::equalityTest()
{
    QGst::BinPtr bin = QGst::Bin::create();
    QGst::ElementPtr element = bin;
    QVERIFY(element == bin);
    QVERIFY(bin == element);
    QVERIFY(bin == bin);

    GstElement *e = element;
    QVERIFY(e == element);
    QVERIFY(element == e);
    QVERIFY(bin == e);
    QVERIFY(e == bin);

    e++;
    QVERIFY(e != element);
    QVERIFY(element != e);
    QVERIFY(bin != e);
    QVERIFY(e != bin);

    e = NULL;
    QVERIFY(e != element);
    QVERIFY(element != e);
    QVERIFY(bin != e);
    QVERIFY(e != bin);

    element.clear();
    QVERIFY(element != bin);
    QVERIFY(bin != element);
    QVERIFY(e == element);
    QVERIFY(element == e);

    bin.clear();
    QVERIFY(element == bin);
    QVERIFY(bin == element);
    QVERIFY(bin == bin);
    QVERIFY(bin == e);
    QVERIFY(e == bin);
}

QTEST_APPLESS_MAIN(RefPointerTest)

#include "moc_qgsttest.cpp"
#include "refpointertest.moc"