35 lines
747 B
C++
35 lines
747 B
C++
|
|
#include <QtTest/QtTest>
|
||
|
|
#include "../../src/core/eventbus.h"
|
||
|
|
#include "../../src/core/events.h"
|
||
|
|
|
||
|
|
class TestEventBus : public QObject
|
||
|
|
{
|
||
|
|
Q_OBJECT
|
||
|
|
private slots:
|
||
|
|
void testPublishAndSubscribe();
|
||
|
|
};
|
||
|
|
|
||
|
|
void TestEventBus::testPublishAndSubscribe()
|
||
|
|
{
|
||
|
|
bool received = false;
|
||
|
|
int value = 0;
|
||
|
|
|
||
|
|
// Subscribe to an event of type int
|
||
|
|
auto subscription = EventBus::instance().subscribe<int>([&](int data) {
|
||
|
|
received = true;
|
||
|
|
value = data;
|
||
|
|
});
|
||
|
|
|
||
|
|
// Publish an event
|
||
|
|
EventBus::instance().publish<int>(42);
|
||
|
|
|
||
|
|
// Check that we received the event
|
||
|
|
QVERIFY(received);
|
||
|
|
QCOMPARE(value, 42);
|
||
|
|
|
||
|
|
// Unsubscribe (optional, subscription goes out of scope)
|
||
|
|
}
|
||
|
|
|
||
|
|
QTEST_MAIN(TestEventBus)
|
||
|
|
#include "test_eventbus.moc"
|