2012-07-28 05:08:29 -04:00
|
|
|
#include <glibmm/thread.h>
|
|
|
|
|
2011-03-04 12:35:39 -05:00
|
|
|
#include "signals_test.h"
|
|
|
|
#include "pbd/signals.h"
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
using namespace std;
|
|
|
|
|
2011-03-04 12:35:39 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (SignalsTest);
|
|
|
|
|
2012-05-16 19:11:16 -04:00
|
|
|
void
|
|
|
|
SignalsTest::setUp ()
|
|
|
|
{
|
|
|
|
if (!Glib::thread_supported ()) {
|
|
|
|
Glib::thread_init ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-04 12:35:39 -05:00
|
|
|
class Emitter {
|
|
|
|
public:
|
|
|
|
void emit () {
|
|
|
|
Fred ();
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2024-10-18 12:01:28 -04:00
|
|
|
PBD::Signal<void()> Fred;
|
2011-03-04 12:35:39 -05:00
|
|
|
};
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
static int N = 0;
|
|
|
|
|
2011-03-04 12:35:39 -05:00
|
|
|
void
|
|
|
|
receiver ()
|
|
|
|
{
|
2012-05-14 13:18:48 -04:00
|
|
|
++N;
|
|
|
|
}
|
2011-03-04 12:35:39 -05:00
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
void
|
|
|
|
SignalsTest::testEmission ()
|
|
|
|
{
|
|
|
|
Emitter* e = new Emitter;
|
|
|
|
PBD::ScopedConnection c;
|
|
|
|
e->Fred.connect_same_thread (c, boost::bind (&receiver));
|
|
|
|
|
|
|
|
N = 0;
|
|
|
|
e->emit ();
|
|
|
|
e->emit ();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2, N);
|
|
|
|
|
2012-05-23 12:22:51 -04:00
|
|
|
PBD::ScopedConnection d;
|
|
|
|
e->Fred.connect_same_thread (d, boost::bind (&receiver));
|
2012-05-14 13:18:48 -04:00
|
|
|
N = 0;
|
|
|
|
e->emit ();
|
|
|
|
CPPUNIT_ASSERT_EQUAL (2, N);
|
2011-03-04 12:35:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SignalsTest::testDestruction ()
|
|
|
|
{
|
|
|
|
Emitter* e = new Emitter;
|
|
|
|
PBD::ScopedConnection c;
|
|
|
|
e->Fred.connect_same_thread (c, boost::bind (&receiver));
|
|
|
|
e->emit ();
|
|
|
|
delete e;
|
|
|
|
c.disconnect ();
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT (true);
|
|
|
|
}
|
|
|
|
|
2014-06-25 08:41:31 -04:00
|
|
|
class AReceiver : public PBD::ScopedConnectionList
|
2012-05-14 13:18:48 -04:00
|
|
|
{
|
|
|
|
public:
|
2014-06-25 08:41:31 -04:00
|
|
|
AReceiver (Emitter* e) {
|
|
|
|
e->Fred.connect_same_thread (*this, boost::bind (&AReceiver::receiver, this));
|
2012-05-14 13:18:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void receiver () {
|
|
|
|
++N;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
SignalsTest::testScopedConnectionList ()
|
|
|
|
{
|
|
|
|
Emitter* e = new Emitter;
|
2014-06-25 08:41:31 -04:00
|
|
|
AReceiver* r = new AReceiver (e);
|
2012-05-14 13:18:48 -04:00
|
|
|
|
|
|
|
N = 0;
|
|
|
|
e->emit ();
|
|
|
|
delete r;
|
|
|
|
e->emit ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
CPPUNIT_ASSERT_EQUAL (1, N);
|
|
|
|
}
|