#include #include #include #include #include #include "../src/BaseQObject.h" #include "../src/DynamicQObject.h" // Templates that convers a T to a string template struct TypeName { static const char* Get() { return typeid(T).name(); } }; template <> struct TypeName { static const char* Get() { return "int"; } }; template <> struct TypeName { static const char* Get() { return "QString"; } }; template <> struct TypeName { static const char* Get() { return "bool"; } }; template <> struct TypeName { static const char* Get() { return "QVariant"; } }; class TestDynamicQObject : public QObject { Q_OBJECT private slots: void memoryLeakTest() { DynamicQObject dynamicQObject; BaseQObject baseQObject; } void testRegisterSignal() { DynamicQObject dynamicQObject; int index; dynamicQObject.registerSignal("fooSignal", {}, index); QCOMPARE(index != -1, true); QSignalSpy signalSpy(&dynamicQObject, SIGNAL(fooSignal())); dynamicQObject.emitSignal("fooSignal", {}); QCOMPARE(signalSpy.count(), 1); } void testSlotExecution() { testSlotExecutionForType(10); testSlotExecutionForType("foo"); testSlotExecutionForType(false); testSlotExecutionForType(QVariant(40)); } void testRegisterProperty() { DynamicQObject dynamicQObject; int index = -1; bool result = false; result = dynamicQObject.registerSlot("foo", QMetaType::Int, {}, index); QCOMPARE(index != -1, true); QCOMPARE(result, true); result = dynamicQObject.registerSlot("setFoo", QMetaType::Void, {QMetaType::Int}, index); QCOMPARE(index != -1, true); QCOMPARE(result, true); result = dynamicQObject.registerSignal("fooChanged", {QMetaType::Int}, index); QCOMPARE(index != -1, true); QCOMPARE(result, true); result = dynamicQObject.registerProperty("foo", QMetaType::Int, "foo", "setFoo", "fooChanged"); QCOMPARE(result, true); } private: template void testSlotExecutionForType(ReturnType expectedReturnValue) { DynamicQObject dynamicQObject; int index; dynamicQObject.registerSlot("fooSlot", (QMetaType::Type)qMetaTypeId(), {}, index); QCOMPARE(index != -1, true); // Call the slot and check return value bool called = false; auto handler = [&called, expectedReturnValue](const DynamicSlot &slot, const std::vector &args) -> QVariant { called = true; return expectedReturnValue; }; dynamicQObject.setOnSlotExecutedHandler(handler); ReturnType result; QMetaObject::invokeMethod(&dynamicQObject, "fooSlot", QReturnArgument(TypeName::Get(), result)); QCOMPARE(called, true); QCOMPARE(result, expectedReturnValue); } }; QTEST_MAIN(TestDynamicQObject) #include "test_dynamicqobject.moc"