Add methods in RefPointer to allow casting RefPointers of derived classes to RefPoint...
[qtgstreamer:qtgstreamer.git] / tests / refpointertest.cpp
1 /*
2     Copyright (C) 2010  George Kiagiadakis <kiagiadakis.george@gmail.com>
3
4     This library is free software; you can redistribute it and/or modify
5     it under the terms of the GNU Lesser General Public License as published
6     by the Free Software Foundation; either version 2.1 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "qgsttest.h"
18 #include <QGst/Object>
19 #include <QGst/Bin>
20
21 class RefPointerTest : public QGstTest
22 {
23     Q_OBJECT
24 private slots:
25     void refTest1();
26     void refTest2();
27     void dynamicCastTest();
28     void implicitCastTest();
29 };
30
31 void RefPointerTest::refTest1()
32 {
33     GstObject *bin = GST_OBJECT(gst_object_ref(GST_OBJECT(gst_bin_new(NULL))));
34     gst_object_sink(bin);
35     QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin, false);
36     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 1);
37 }
38
39 void RefPointerTest::refTest2()
40 {
41     GstObject *bin = GST_OBJECT(gst_object_ref(GST_OBJECT(gst_bin_new(NULL))));
42     gst_object_sink(bin);
43     {
44         QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
45         QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 2);
46         {
47             QGst::ObjectPtr object2 = object;
48             QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 3);
49         }
50     }
51     QCOMPARE(GST_OBJECT_REFCOUNT_VALUE(bin), 1);
52     gst_object_unref(bin);
53 }
54
55 void RefPointerTest::dynamicCastTest()
56 {
57     GstObject *bin = GST_OBJECT(gst_object_ref(GST_OBJECT(gst_bin_new(NULL))));
58     gst_object_sink(bin);
59
60     {
61         QGst::ObjectPtr object = QGst::ObjectPtr::wrap(bin);
62         QVERIFY(!object.dynamicCast<QGlib::Object>().isNull());
63     }
64
65     {
66         QGlib::ObjectPtr object = QGlib::ObjectPtr::wrap(G_OBJECT(bin));
67         QVERIFY(!object.dynamicCast<QGst::Object>().isNull());
68     }
69
70     gst_object_unref(bin);
71 }
72
73 static void testMethod(const QGlib::ObjectPtr & obj)
74 {
75     QVERIFY(!obj.dynamicCast<QGst::Bin>().isNull());
76 };
77
78 void RefPointerTest::implicitCastTest()
79 {
80     //This is mostly a compilation test. If it compiles, it's fine, if it doesn't, there is a problem.
81     QGst::BinPtr bin = QGst::Bin::newBin();
82
83     {
84         //operator=()
85         QGst::ObjectPtr obj = bin;
86         QVERIFY(!obj.dynamicCast<QGst::Bin>().isNull());
87     }
88
89     {
90         //copy constructor
91         QGst::ObjectPtr obj(bin);
92         QVERIFY(!obj.dynamicCast<QGst::Bin>().isNull());
93     }
94
95     {
96         //implicit cast
97         testMethod(bin);
98     }
99
100 #if 0
101     {
102         QGst::ObjectPtr obj = bin;
103         QGst::BinPtr bin2 = obj; //should fail to compile
104     }
105 #endif
106 }
107
108 QTEST_APPLESS_MAIN(RefPointerTest)
109
110 #include "moc_qgsttest.cpp"
111 #include "refpointertest.moc"