Test exceptions in signals and slots

Only DirectConnection is tested

Change-Id: I525c6a65428489c34e58d1d9600b4e5ebda733b9
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Olivier Goffart 2013-10-05 20:01:36 +02:00 committed by The Qt Project
parent 46a8885ae4
commit 73efc9a2cd
1 changed files with 97 additions and 0 deletions

View File

@ -151,6 +151,7 @@ private slots:
void contextDoesNotLeakFunctor();
void connectBase();
void qmlConnect();
void exceptions();
};
struct QObjectCreatedOnShutdown
@ -6136,6 +6137,102 @@ void tst_QObject::qmlConnect()
#endif
}
#ifndef QT_NO_EXCEPTIONS
class ObjectException : public std::exception { };
struct ThrowFunctor
{
CountedStruct operator()(const CountedStruct &, CountedStruct s2) const
{
throw ObjectException();
return s2;
}
CountedStruct s;
};
#endif
class ExceptionThrower : public QObject
{
Q_OBJECT
public slots:
CountedStruct throwException(const CountedStruct &, CountedStruct s2)
{
#ifndef QT_NO_EXCEPTIONS
throw ObjectException();
#endif
return s2;
}
signals:
CountedStruct mySignal(const CountedStruct &s1, CountedStruct s2);
};
void tst_QObject::exceptions()
{
#ifndef QT_NO_EXCEPTIONS
ReceiverObject receiver;
// String based syntax
{
QCOMPARE(countedStructObjectsCount, 0);
ExceptionThrower thrower;
receiver.reset();
connect(&thrower, SIGNAL(mySignal(CountedStruct,CountedStruct)), &receiver, SLOT(slot1()));
connect(&thrower, SIGNAL(mySignal(CountedStruct,CountedStruct)), &thrower, SLOT(throwException(CountedStruct,CountedStruct)));
connect(&thrower, SIGNAL(mySignal(CountedStruct,CountedStruct)), &receiver, SLOT(slot2()));
try {
CountedStruct s;
emit thrower.mySignal(s, s);
QFAIL("Exception not thrown?");
} catch (ObjectException&) {}
QCOMPARE(receiver.count_slot1, 1);
QCOMPARE(receiver.count_slot2, 0);
QCOMPARE(countedStructObjectsCount, 0);
}
// Pointer to member function
{
QCOMPARE(countedStructObjectsCount, 0);
ExceptionThrower thrower;
receiver.reset();
connect(&thrower, &ExceptionThrower::mySignal, &receiver, &ReceiverObject::slot1);
connect(&thrower, &ExceptionThrower::mySignal, &thrower, &ExceptionThrower::throwException);
connect(&thrower, &ExceptionThrower::mySignal, &receiver, &ReceiverObject::slot2);
try {
CountedStruct s;
emit thrower.mySignal(s, s);
QFAIL("Exception not thrown?");
} catch (ObjectException&) {}
QCOMPARE(receiver.count_slot1, 1);
QCOMPARE(receiver.count_slot2, 0);
QCOMPARE(countedStructObjectsCount, 0);
}
// Functor
{
QCOMPARE(countedStructObjectsCount, 0);
ExceptionThrower thrower;
receiver.reset();
connect(&thrower, &ExceptionThrower::mySignal, &receiver, &ReceiverObject::slot1);
connect(&thrower, &ExceptionThrower::mySignal, ThrowFunctor());
connect(&thrower, &ExceptionThrower::mySignal, &receiver, &ReceiverObject::slot2);
try {
CountedStruct s;
emit thrower.mySignal(s, s);
QFAIL("Exception not thrown?");
} catch (ObjectException&) {}
QCOMPARE(receiver.count_slot1, 1);
QCOMPARE(receiver.count_slot2, 0);
QCOMPARE(countedStructObjectsCount, 1); // the Functor
}
QCOMPARE(countedStructObjectsCount, 0);
#else
QSKIP("Needs exceptions");
#endif
}
// Test for QtPrivate::HasQ_OBJECT_Macro
Q_STATIC_ASSERT(QtPrivate::HasQ_OBJECT_Macro<tst_QObject>::Value);
Q_STATIC_ASSERT(!QtPrivate::HasQ_OBJECT_Macro<SiblingDeleter>::Value);