From: Jason McDonald Date: Fri, 30 Dec 2011 00:58:14 +0000 (+1000) Subject: Prevent qDebug test bypassing the testlib message handler. X-Git-Tag: qt-v5.0.0-alpha1~2101 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14e139f39187a6e87d6eed332b11b8a3f8521906;p=profile%2Fivi%2Fqtbase.git Prevent qDebug test bypassing the testlib message handler. Several of the test functions in the QDebug autotest call qInstallMsgHandler() to temporarily use a custom message handler. Unfortunately, these test functions were then resetting the message handler back to Qt's default handler at the end of the test. QTestLib also calls qInstallMsgHandler() to set a message handler that redirects debug/warning/fatal messages into the test log. When the test resets the message handler back to Qt's default handler, testlib's message handler is bypassed for the rest of the test, preventing any subsequent debug/warning/fatal messages from being visible in the test log or subject to testlib's ignoreMessage() function. This error also caused several of the test functions to fail if they were run manually. The "defaultMessagehandler" test would fail if it was run before any other test function and the "assignment" test would fail if it was run after any other test function. This commit fixes these failures by using a helper class to ensure that the previously active message handler is restored at the end of each test function, even if the test function fails or throws an exception. Change-Id: I51376724d164c8ad126e5b9be76890bf3e6a9fb0 Reviewed-by: Rohan McGovern --- diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index f33a7ee..86872a4 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -81,16 +81,34 @@ static void myMessageHandler(QtMsgType type, const char *msg) s_msgType = type; } +// Helper class to ensure that the testlib message handler gets +// restored at the end of each test function, even if the test +// fails or throws an exception. +class MessageHandlerSetter +{ +public: + MessageHandlerSetter(QtMsgHandler newMsgHandler) + : oldMsgHandler(qInstallMsgHandler(newMsgHandler)) + { } + + ~MessageHandlerSetter() + { + qInstallMsgHandler(oldMsgHandler); + } + +private: + QtMsgHandler oldMsgHandler; +}; + /*! \internal The qWarning() stream should be usable even if QT_NO_DEBUG is defined. */ void tst_QDebug::warningWithoutDebug() const { - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); { qWarning() << "A qWarning() message"; } QCOMPARE(s_msgType, QtWarningMsg); QCOMPARE(QString::fromLatin1(s_msg.data()), QString::fromLatin1("A qWarning() message ")); - qInstallMsgHandler(0); } /*! \internal @@ -98,25 +116,23 @@ void tst_QDebug::warningWithoutDebug() const */ void tst_QDebug::criticalWithoutDebug() const { - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); { qCritical() << "A qCritical() message"; } QCOMPARE(s_msgType, QtCriticalMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("A qCritical() message ")); - qInstallMsgHandler(0); } void tst_QDebug::debugWithQBool() const { - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); { qDebug() << QBool(false) << QBool(true); } QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("false true ")); - qInstallMsgHandler(0); } void tst_QDebug::veryLongWarningMessage() const { - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); QString test; { QString part("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n"); @@ -126,7 +142,6 @@ void tst_QDebug::veryLongWarningMessage() const } QCOMPARE(s_msgType, QtWarningMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend")); - qInstallMsgHandler(0); } void tst_QDebug::qDebugQStringRef() const @@ -136,27 +151,26 @@ void tst_QDebug::qDebugQStringRef() const const QString in(QLatin1String("input")); const QStringRef inRef(&in); - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); { qDebug() << inRef; } QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"input\" ")); - qInstallMsgHandler(0); } /* Use a null QStringRef. */ { const QStringRef inRef; - qInstallMsgHandler(myMessageHandler); + MessageHandlerSetter mhs(myMessageHandler); { qDebug() << inRef; } QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"\" ")); - qInstallMsgHandler(0); } } void tst_QDebug::defaultMessagehandler() const { + MessageHandlerSetter mhs(0); QtMsgHandler defaultMessageHandler1 = qInstallMsgHandler(0); QtMsgHandler defaultMessageHandler2 = qInstallMsgHandler(myMessageHandler); bool same = (*defaultMessageHandler1 == *defaultMessageHandler2);