1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
44 #include <QtTest/QtTest>
46 /* We expect these headers to be available. */
47 #include <QtXmlPatterns/QAbstractMessageHandler>
48 #include <QtXmlPatterns/qabstractmessagehandler.h>
49 #include <QAbstractMessageHandler>
50 #include <qabstractmessagehandler.h>
53 \class tst_QAbstractMessageHandler
56 \brief Tests the QAbstractMessageHandler class.
58 class tst_QAbstractMessageHandler : public QObject
63 void constructor() const;
64 void constCorrectness() const;
66 void messageDefaultArguments() const;
67 void objectSize() const;
68 void hasQ_OBJECTMacro() const;
77 QSourceLocation sourceLocation;
79 bool operator==(const Received &other) const
81 return type == other.type
82 && description == other.description
83 && identifier == other.identifier
84 && sourceLocation == other.sourceLocation;
88 class TestMessageHandler : public QAbstractMessageHandler
91 QList<Received> received;
94 virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation);
97 void TestMessageHandler::handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation)
101 r.description = description;
102 r.identifier = identifier;
103 r.sourceLocation = sourceLocation;
107 void tst_QAbstractMessageHandler::constructor() const
109 /* Allocate instance. */
111 TestMessageHandler instance;
115 TestMessageHandler instance1;
116 TestMessageHandler instance2;
120 TestMessageHandler instance1;
121 TestMessageHandler instance2;
122 TestMessageHandler instance3;
126 void tst_QAbstractMessageHandler::constCorrectness() const
128 /* No members are supposed to be const. */
131 void tst_QAbstractMessageHandler::objectSize() const
133 /* We shouldn't be adding anything. */
134 QCOMPARE(sizeof(QAbstractMessageHandler), sizeof(QObject));
137 void tst_QAbstractMessageHandler::message() const
139 TestMessageHandler handler;
141 /* Check that the arguments comes out as expected. */
142 handler.message(QtDebugMsg,
143 QLatin1String("A description"),
144 QUrl(QLatin1String("http://example.com/ID")),
145 QSourceLocation(QUrl(QLatin1String("http://example.com/Location")), 4, 5));
148 expected.type = QtDebugMsg;
149 expected.description = QLatin1String("A description");
150 expected.identifier = QUrl(QLatin1String("http://example.com/ID"));
151 expected.sourceLocation = QSourceLocation(QUrl(QLatin1String("http://example.com/Location")), 4, 5);
153 QCOMPARE(expected, handler.received.first());
156 void tst_QAbstractMessageHandler::messageDefaultArguments() const
158 TestMessageHandler handler;
160 /* The three last arguments in message() are optional. Check that they are what we promise. */
161 handler.message(QtDebugMsg, QLatin1String("A description"));
164 expected.type = QtDebugMsg;
165 expected.description = QLatin1String("A description");
166 expected.identifier = QUrl();
167 expected.sourceLocation = QSourceLocation();
169 QCOMPARE(expected, handler.received.first());
172 void tst_QAbstractMessageHandler::hasQ_OBJECTMacro() const
174 TestMessageHandler messageHandler;
175 /* If this code fails to compile, the Q_OBJECT macro is missing in
176 * the class declaration. */
177 QAbstractMessageHandler *const secondPointer = qobject_cast<QAbstractMessageHandler *>(&messageHandler);
178 /* The static_cast is for compiling on broken compilers. */
179 QCOMPARE(static_cast<QAbstractMessageHandler *>(&messageHandler), secondPointer);
182 QTEST_MAIN(tst_QAbstractMessageHandler)
184 #include "tst_qabstractmessagehandler.moc"
186 // vim: et:ts=4:sw=4:sts=4