Add option (enabled by default) to omit the sender from a slot's arguments. In most...
[qtgstreamer:qtgstreamer.git] / tests / signalstest.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 <QGlib/Signal>
19 #include <QGst/Pipeline>
20
21 class SignalsTest : public QGstTest
22 {
23     Q_OBJECT
24 private:
25    void closureTestClosure(const QGst::ObjectPtr & obj, QGst::ObjectPtr parentObj);
26    void emitTestClosure(const QGlib::ObjectPtr & instance, const QGlib::ParamSpecPtr & param);
27 private Q_SLOTS:
28    void closureTest();
29    void queryTest();
30    void emitTest();
31 };
32
33 static bool closureCalled = false;
34
35 void SignalsTest::closureTestClosure(const QGst::ObjectPtr & obj, QGst::ObjectPtr parentObj)
36 {
37     qDebug() << "closureTestClosure called";
38     QCOMPARE(obj->property("name").get<QString>(), QString("mybin"));
39     QCOMPARE(parentObj->property("name").get<QString>(), QString("mypipeline"));
40     closureCalled = true;
41 }
42
43 void SignalsTest::closureTest()
44 {
45     QGst::PipelinePtr pipeline = QGst::Pipeline::create("mypipeline");
46     QGst::BinPtr bin = QGst::Bin::create("mybin");
47
48     closureCalled = false;
49     QGlib::Signal::connect(bin, "parent-set", this,
50                            &SignalsTest::closureTestClosure, QGlib::Signal::PassSender);
51     bin->setParent(pipeline);
52     QCOMPARE(closureCalled, true);
53 }
54
55 void SignalsTest::queryTest()
56 {
57     //void user_function(GObject *gobject, GParamSpec *pspec, gpointer user_data)
58     //Run First / No Recursion / Has Details / Action / No Hooks
59     QGlib::Signal s = QGlib::Signal::lookup("notify", QGlib::GetType<QGlib::Object>());
60     QVERIFY(s.isValid());
61
62     QCOMPARE(s.name(), QString("notify"));
63     QCOMPARE(s.instanceType(), QGlib::GetType<QGlib::Object>());
64
65     QCOMPARE(s.flags(), QGlib::Signal::RunFirst | QGlib::Signal::NoRecurse |
66                         QGlib::Signal::Detailed | QGlib::Signal::Action | QGlib::Signal::NoHooks);
67     QCOMPARE(s.returnType(), QGlib::Type(QGlib::Type::None));
68
69     QList<QGlib::Type> paramTypes = s.paramTypes();
70     QCOMPARE(paramTypes.size(), 1);
71     QCOMPARE(paramTypes[0], QGlib::GetType<QGlib::ParamSpec>());
72 }
73
74 void SignalsTest::emitTestClosure(const QGlib::ObjectPtr & instance, const QGlib::ParamSpecPtr & param)
75 {
76     qDebug() << "emitTestClosure called";
77     QCOMPARE(instance->property("name").get<QString>(), QString("mybin"));
78     QCOMPARE(param->name(), QString("name"));
79     closureCalled = true;
80 }
81
82 void SignalsTest::emitTest()
83 {
84     QGst::BinPtr bin = QGst::Bin::create("mybin");
85     QGlib::SignalHandler handler = QGlib::Signal::connect(bin, "notify::name",
86                                                           this, &SignalsTest::emitTestClosure,
87                                                           QGlib::Signal::PassSender);
88
89     QVERIFY(handler.isConnected());
90
91     closureCalled = false;
92     QGlib::Signal::emit<void>(bin, "notify::name", bin->findProperty("name"));
93     QCOMPARE(closureCalled, true);
94
95     //calling with wrong return value. should show error message but *call* the signal
96     //and return default constructed value for int
97     closureCalled = false;
98     int r = QGlib::Signal::emit<int>(bin, "notify::name", bin->findProperty("name"));
99     QCOMPARE(r, int());
100     QCOMPARE(closureCalled, true);
101
102     //calling with wrong number of arguments. should show error message and *not call* the signal
103     closureCalled = false;
104     QGlib::Signal::emit<void>(bin, "notify::name");
105     QCOMPARE(closureCalled, false);
106
107     //calling wrong signal. will return default constructed value for int
108     closureCalled = false;
109     r = QGlib::Signal::emit<int>(bin, "foobar");
110     QCOMPARE(r, int());
111     QCOMPARE(closureCalled, false);
112
113     handler.disconnect();
114     QVERIFY(!handler.isConnected());
115
116     closureCalled = false;
117     QGlib::Signal::emit<void>(bin, "notify::name", bin->findProperty("name"));
118     QCOMPARE(closureCalled, false);
119 }
120
121 QTEST_APPLESS_MAIN(SignalsTest)
122
123 #include "moc_qgsttest.cpp"
124 #include "signalstest.moc"