1 /*
2     Copyright (C) 2010 George Kiagiadakis <kiagiadakis.george@gmail.com>
3     Copyright (C) 2010 Collabora Ltd.
4       @author George Kiagiadakis <george.kiagiadakis@collabora.co.uk>
5 
6     This library is free software; you can redistribute it and/or modify
7     it under the terms of the GNU Lesser General Public License as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU Lesser General Public License for more details.
15 
16     You should have received a copy of the GNU Lesser General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "qgsttest.h"
20 #include <QGst/Object>
21 #include <QGst/Message>
22 #include <QGst/Pipeline>
23 #include <QGst/ElementFactory>
24 #include <QGst/UriHandler>
25 #include <QGst/StreamVolume>
26 
27 class RefPointerTest : public QGstTest
28 {
29     Q_OBJECT
30 private Q_SLOTS:
31     void refTest1();
32     void refTest2();
33     void dynamicCastTest();
34     void dynamicCastDownObjectTest();
35     void dynamicCastUpObjectTest();
36     void dynamicCastObjectToIfaceTest();
37     void dynamicCastIfaceToObjectTest();
38     void cppWrappersTest();
39     void messageDynamicCastTest();
40     void equalityTest();
41 };
42 
refTest1()43 void RefPointerTest::refTest1()
44 {
45     GstElement *element = gst_bin_new(NULL);
46     GstObject *bin1 = GST_OBJECT(element);
47     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(element), 1);
48 
49     GstObject *bin2 = GST_OBJECT(gst_object_ref_sink(bin1));
50     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin2), 1);
51     QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin2, false);
52     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin2), 1);
53 }
54 
refTest2()55 void RefPointerTest::refTest2()
56 {
57     GstObject *bin = GST_OBJECT(gst_object_ref_sink(GST_OBJECT(gst_bin_new(NULL))));
58 
59     {
60         QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
61         QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 2);
62         {
63             QGst::ObjectPtr object2 = object;
64             QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 3);
65         }
66     }
67     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 1);
68     gst_object_unref(bin);
69 }
70 
dynamicCastTest()71 void RefPointerTest::dynamicCastTest()
72 {
73     GstObject *bin = GST_OBJECT(gst_object_ref_sink(GST_OBJECT(gst_bin_new(NULL))));
74 
75     {
76         QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
77         QVERIFY(!object.dynamicCast<QGlib::Object>().isNull());
78     }
79 
80     {
81         QGlib::ObjectPtr object = QGlib::ObjectPtr::wrap(G_OBJECT(bin));
82         QVERIFY(!object.dynamicCast<QGst::Object>().isNull());
83     }
84 
85     gst_object_unref(bin);
86 }
87 
dynamicCastDownObjectTest()88 void RefPointerTest::dynamicCastDownObjectTest()
89 {
90     GstObject *bin = GST_OBJECT(gst_object_ref_sink(gst_bin_new(NULL)));
91 
92     {
93         QGlib::ObjectPtr object = QGlib::ObjectPtr::wrap(G_OBJECT(bin));
94         QVERIFY(!object.dynamicCast<QGst::Object>().isNull());
95         QVERIFY(!object.dynamicCast<QGst::Bin>().isNull());
96         QVERIFY(object.dynamicCast<QGst::Pipeline>().isNull());
97     }
98 
99     gst_object_unref(bin);
100 }
101 
dynamicCastUpObjectTest()102 void RefPointerTest::dynamicCastUpObjectTest()
103 {
104     GstBin *bin = GST_BIN(gst_object_ref_sink(gst_bin_new(NULL)));
105 
106     {
107         QGst::BinPtr object = QGst::BinPtr::wrap(bin);
108         QVERIFY(!object.dynamicCast<QGst::Element>().isNull());
109         QVERIFY(!object.dynamicCast<QGlib::Object>().isNull());
110         QVERIFY(!object.dynamicCast<QGst::ChildProxy>().isNull());
111     }
112 
113     gst_object_unref(bin);
114 }
115 
dynamicCastObjectToIfaceTest()116 void RefPointerTest::dynamicCastObjectToIfaceTest()
117 {
118     QGst::ElementPtr e = QGst::ElementFactory::make("fakesrc");
119     QGst::UriHandlerPtr u = e.dynamicCast<QGst::UriHandler>();
120     QVERIFY(u.isNull());
121 
122     e = QGst::ElementFactory::make("filesrc");
123     u = e.dynamicCast<QGst::UriHandler>();
124     QVERIFY(!u.isNull());
125 }
126 
dynamicCastIfaceToObjectTest()127 void RefPointerTest::dynamicCastIfaceToObjectTest()
128 {
129     GstElement *e = gst_element_factory_make("filesrc", NULL);
130     gst_object_ref_sink(e);
131 
132     QGst::UriHandlerPtr u = QGst::UriHandlerPtr::wrap(GST_URI_HANDLER(e), false);
133     QVERIFY(!u.isNull());
134     QVERIFY(!u.dynamicCast<QGst::Element>().isNull());
135 }
136 
cppWrappersTest()137 void RefPointerTest::cppWrappersTest()
138 {
139     QGst::ElementPtr e = QGst::ElementFactory::make("playbin");
140     QVERIFY(!e.isNull());
141 
142     {
143         QGst::PipelinePtr pipeline = e.dynamicCast<QGst::Pipeline>();
144         QVERIFY(!pipeline.isNull());
145         //the C++ wrappers must be the same
146         QCOMPARE(static_cast<QGlib::RefCountedObject*>(pipeline.operator->()),
147                  static_cast<QGlib::RefCountedObject*>(e.operator->()));
148     }
149 
150     {
151         QGst::ChildProxyPtr proxy = e.dynamicCast<QGst::ChildProxy>();
152         QVERIFY(!proxy.isNull());
153         //the C++ wrappers must be the same
154         QCOMPARE(static_cast<QGlib::RefCountedObject*>(proxy.operator->()),
155                  static_cast<QGlib::RefCountedObject*>(e.operator->()));
156     }
157 
158     { //new wrap() should give the same C++ instance
159         GstElement *gobj = e;
160         QGst::ElementPtr e2 = QGst::ElementPtr::wrap(gobj);
161         QCOMPARE(static_cast<QGlib::RefCountedObject*>(e2.operator->()),
162                  static_cast<QGlib::RefCountedObject*>(e.operator->()));
163     }
164 
165     {
166         QGst::StreamVolumePtr sv = e.dynamicCast<QGst::StreamVolume>();
167         QVERIFY(!sv.isNull());
168         //now the C++ wrapper must not be the same, since Pipeline does not inherit StreamVolume
169         QVERIFY(static_cast<QGlib::RefCountedObject*>(sv.operator->())
170                 != static_cast<QGlib::RefCountedObject*>(e.operator->()));
171     }
172 
173     {
174         QGst::MessagePtr msg = QGst::ApplicationMessage::create(e);
175         QGst::MessagePtr msg2 = msg;
176         QCOMPARE(static_cast<QGlib::RefCountedObject*>(msg.operator->()),
177                  static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
178         QVERIFY(msg2 == msg);
179 
180         QGst::MessagePtr msg3 = QGst::MessagePtr::wrap(msg2);
181         QVERIFY(static_cast<QGlib::RefCountedObject*>(msg3.operator->())
182                 != static_cast<QGlib::RefCountedObject*>(msg2.operator->()));
183         QVERIFY(msg3 == msg2);
184     }
185 }
186 
messageDynamicCastTest()187 void RefPointerTest::messageDynamicCastTest()
188 {
189     QGst::BinPtr bin = QGst::Bin::create();
190     QGst::MessagePtr msg = QGst::ApplicationMessage::create(bin);
191     QVERIFY(!msg.isNull());
192     QVERIFY(!msg.dynamicCast<QGst::ApplicationMessage>().isNull());
193     QVERIFY(msg.dynamicCast<QGst::EosMessage>().isNull());
194 }
195 
equalityTest()196 void RefPointerTest::equalityTest()
197 {
198     QGst::BinPtr bin = QGst::Bin::create();
199     QGst::ElementPtr element = bin;
200     QVERIFY(element == bin);
201     QVERIFY(bin == element);
202     QVERIFY(bin == bin);
203 
204     GstElement *e = element;
205     QVERIFY(e == element);
206     QVERIFY(element == e);
207     QVERIFY(bin == e);
208     QVERIFY(e == bin);
209 
210     e++;
211     QVERIFY(e != element);
212     QVERIFY(element != e);
213     QVERIFY(bin != e);
214     QVERIFY(e != bin);
215 
216     e = NULL;
217     QVERIFY(e != element);
218     QVERIFY(element != e);
219     QVERIFY(bin != e);
220     QVERIFY(e != bin);
221 
222     element.clear();
223     QVERIFY(element != bin);
224     QVERIFY(bin != element);
225     QVERIFY(e == element);
226     QVERIFY(element == e);
227 
228     bin.clear();
229     QVERIFY(element == bin);
230     QVERIFY(bin == element);
231     QVERIFY(bin == bin);
232     QVERIFY(bin == e);
233     QVERIFY(e == bin);
234 }
235 
236 QTEST_APPLESS_MAIN(RefPointerTest)
237 
238 #include "moc_qgsttest.cpp"
239 #include "refpointertest.moc"
240