diff options
Diffstat (limited to 'tests')
4 files changed, 191 insertions, 9 deletions
diff --git a/tests/auto/corelib/global/global.pro b/tests/auto/corelib/global/global.pro index e9b547be68..a6c638f530 100644 --- a/tests/auto/corelib/global/global.pro +++ b/tests/auto/corelib/global/global.pro @@ -5,4 +5,5 @@ SUBDIRS=\ qgetputenv \ qglobal \ qnumeric \ - qrand + qrand \ + qmessagehandler diff --git a/tests/auto/corelib/global/qmessagehandler/qmessagehandler.pro b/tests/auto/corelib/global/qmessagehandler/qmessagehandler.pro new file mode 100644 index 0000000000..8bdba4bfc4 --- /dev/null +++ b/tests/auto/corelib/global/qmessagehandler/qmessagehandler.pro @@ -0,0 +1,4 @@ +CONFIG += testcase parallel_test +TARGET = tst_qmessagehandler +QT = core testlib +SOURCES = tst_qmessagehandler.cpp diff --git a/tests/auto/corelib/global/qmessagehandler/tst_qmessagehandler.cpp b/tests/auto/corelib/global/qmessagehandler/tst_qmessagehandler.cpp new file mode 100644 index 0000000000..39bd0986a5 --- /dev/null +++ b/tests/auto/corelib/global/qmessagehandler/tst_qmessagehandler.cpp @@ -0,0 +1,147 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qdebug.h> +#include <QtTest/QtTest> + +#include <qglobal.h> + +class tst_qmessagehandler : public QObject +{ + Q_OBJECT +private slots: + void cleanup(); + + void defaultHandler(); + void installMessageHandler(); + void installMsgHandler(); + void installBothHandler(); +}; + +static QtMsgType s_type; +const char *s_file; +int s_line; +const char *s_function; +static QString s_message; + +void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const char *msg) +{ + s_type = type; + s_file = context.file; + s_line = context.line; + s_function = context.function; + s_message = QString::fromLocal8Bit(msg); +} + +void customMsgHandler(QtMsgType type, const char *msg) +{ + s_type = type; + s_file = 0; + s_line = 0; + s_function = 0; + s_message = QString::fromLocal8Bit(msg); +} + +void tst_qmessagehandler::cleanup() +{ + qInstallMsgHandler(0); + qInstallMessageHandler(0); + s_type = QtFatalMsg; + s_file = 0; + s_line = 0; + s_function = 0; +} + +void tst_qmessagehandler::defaultHandler() +{ + // check that the default works + QTest::ignoreMessage(QtDebugMsg, "defaultHandler"); + qDebug("defaultHandler"); +} + +void tst_qmessagehandler::installMessageHandler() +{ + QMessageHandler oldHandler = qInstallMessageHandler(customMessageHandler); + + qDebug("installMessageHandler"); int line = __LINE__; + + QCOMPARE(s_type, QtDebugMsg); + QCOMPARE(s_message, QString::fromLocal8Bit("installMessageHandler")); + QCOMPARE(s_file, __FILE__); + QCOMPARE(s_function, Q_FUNC_INFO); + QCOMPARE(s_line, line); + + QMessageHandler myHandler = qInstallMessageHandler(oldHandler); + QCOMPARE((void*)myHandler, (void*)customMessageHandler); +} + +void tst_qmessagehandler::installMsgHandler() +{ + QtMsgHandler oldHandler = qInstallMsgHandler(customMsgHandler); + + qDebug("installMsgHandler"); + + QCOMPARE(s_type, QtDebugMsg); + QCOMPARE(s_message, QString::fromLocal8Bit("installMsgHandler")); + QCOMPARE(s_file, (const char*)0); + QCOMPARE(s_function, (const char*)0); + QCOMPARE(s_line, 0); + + QtMsgHandler myHandler = qInstallMsgHandler(oldHandler); + QCOMPARE((void*)myHandler, (void*)customMsgHandler); +} + +void tst_qmessagehandler::installBothHandler() +{ + qInstallMessageHandler(customMessageHandler); + qInstallMsgHandler(customMsgHandler); + + qDebug("installBothHandler"); int line = __LINE__; + + QCOMPARE(s_type, QtDebugMsg); + QCOMPARE(s_message, QString::fromLocal8Bit("installBothHandler")); + QCOMPARE(s_file, __FILE__); + QCOMPARE(s_function, Q_FUNC_INFO); + QCOMPARE(s_line, line); +} + +QTEST_MAIN(tst_qmessagehandler) +#include "tst_qmessagehandler.moc" diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 23043c634c..19f020f750 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -74,11 +74,17 @@ void tst_QDebug::assignment() const static QtMsgType s_msgType; static QByteArray s_msg; +static QByteArray s_file; +static int s_line; +static QByteArray s_function; -static void myMessageHandler(QtMsgType type, const char *msg) +static void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const char *msg) { s_msg = msg; s_msgType = type; + s_file = context.file; + s_line = context.line; + s_function = context.function; } // Helper class to ensure that the testlib message handler gets @@ -87,17 +93,17 @@ static void myMessageHandler(QtMsgType type, const char *msg) class MessageHandlerSetter { public: - MessageHandlerSetter(QtMsgHandler newMsgHandler) - : oldMsgHandler(qInstallMsgHandler(newMsgHandler)) + MessageHandlerSetter(QMessageHandler newMessageHandler) + : oldMessageHandler(qInstallMessageHandler(newMessageHandler)) { } ~MessageHandlerSetter() { - qInstallMsgHandler(oldMsgHandler); + qInstallMessageHandler(oldMessageHandler); } private: - QtMsgHandler oldMsgHandler; + QMessageHandler oldMessageHandler; }; /*! \internal @@ -107,8 +113,12 @@ void tst_QDebug::warningWithoutDebug() const { MessageHandlerSetter mhs(myMessageHandler); { qWarning() << "A qWarning() message"; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtWarningMsg); QCOMPARE(QString::fromLatin1(s_msg.data()), QString::fromLatin1("A qWarning() message ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } /*! \internal @@ -118,16 +128,24 @@ void tst_QDebug::criticalWithoutDebug() const { MessageHandlerSetter mhs(myMessageHandler); { qCritical() << "A qCritical() message"; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtCriticalMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("A qCritical() message ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } void tst_QDebug::debugWithBool() const { MessageHandlerSetter mhs(myMessageHandler); { qDebug() << false << true; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("false true ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } void tst_QDebug::veryLongWarningMessage() const @@ -140,8 +158,12 @@ void tst_QDebug::veryLongWarningMessage() const test.append(part); qWarning("Test output:\n%s\nend", qPrintable(test)); } + QString file = __FILE__; int line = __LINE__ - 2; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtWarningMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("Test output:\n")+test+QString::fromLatin1("\nend")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } void tst_QDebug::qDebugQStringRef() const @@ -153,8 +175,12 @@ void tst_QDebug::qDebugQStringRef() const MessageHandlerSetter mhs(myMessageHandler); { qDebug() << inRef; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"input\" ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } /* Use a null QStringRef. */ @@ -163,19 +189,23 @@ void tst_QDebug::qDebugQStringRef() const MessageHandlerSetter mhs(myMessageHandler); { qDebug() << inRef; } + QString file = __FILE__; int line = __LINE__ - 1; QString function = Q_FUNC_INFO; QCOMPARE(s_msgType, QtDebugMsg); QCOMPARE(QString::fromLatin1(s_msg), QString::fromLatin1("\"\" ")); + QCOMPARE(QString::fromLatin1(s_file), file); + QCOMPARE(s_line, line); + QCOMPARE(QString::fromLatin1(s_function), function); } } void tst_QDebug::defaultMessagehandler() const { MessageHandlerSetter mhs(0); - QtMsgHandler defaultMessageHandler1 = qInstallMsgHandler(0); - QtMsgHandler defaultMessageHandler2 = qInstallMsgHandler(myMessageHandler); + QMessageHandler defaultMessageHandler1 = qInstallMessageHandler(0); + QMessageHandler defaultMessageHandler2 = qInstallMessageHandler(myMessageHandler); bool same = (*defaultMessageHandler1 == *defaultMessageHandler2); QVERIFY(same); - QtMsgHandler messageHandler = qInstallMsgHandler(0); + QMessageHandler messageHandler = qInstallMessageHandler(0); same = (*messageHandler == *myMessageHandler); QVERIFY(same); } |