From 73efc9a2cd9839c75eb8bd1449ce23232ac0d306 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sat, 5 Oct 2013 20:01:36 +0200 Subject: Test exceptions in signals and slots Only DirectConnection is tested Change-Id: I525c6a65428489c34e58d1d9600b4e5ebda733b9 Reviewed-by: Thiago Macieira Reviewed-by: Marc Mutz --- tests/auto/corelib/kernel/qobject/tst_qobject.cpp | 97 +++++++++++++++++++++++ 1 file changed, 97 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 05d81c2bd1..ece88d47e3 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -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::Value); Q_STATIC_ASSERT(!QtPrivate::HasQ_OBJECT_Macro::Value); -- cgit v1.2.3