Update licenseheader text in source files for qtxmlpatterns Qt module
[profile/ivi/qtxmlpatterns.git] / tests / auto / qabstractmessagehandler / tst_qabstractmessagehandler.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QFile>
44 #include <QtTest/QtTest>
45
46 #ifdef QTEST_XMLPATTERNS
47 #include <QtXmlPatterns/QAbstractMessageHandler>
48
49 /* We expect these headers to be available. */
50 #include <QtXmlPatterns/QAbstractMessageHandler>
51 #include <QtXmlPatterns/qabstractmessagehandler.h>
52 #include <QAbstractMessageHandler>
53 #include <qabstractmessagehandler.h>
54
55 /*!
56  \class tst_QAbstractMessageHandler
57  \internal
58  \since 4.4
59  \brief Tests the QAbstractMessageHandler class.
60  */
61 class tst_QAbstractMessageHandler : public QObject
62 {
63     Q_OBJECT
64
65 private Q_SLOTS:
66     void constructor() const;
67     void constCorrectness() const;
68     void message() const;
69     void messageDefaultArguments() const;
70     void objectSize() const;
71     void hasQ_OBJECTMacro() const;
72 };
73
74 class Received
75 {
76 public:
77     QtMsgType       type;
78     QString         description;
79     QUrl            identifier;
80     QSourceLocation sourceLocation;
81
82     bool operator==(const Received &other) const
83     {
84         return type == other.type
85                && description == other.description
86                && identifier == other.identifier
87                && sourceLocation == other.sourceLocation;
88     }
89 };
90
91 class TestMessageHandler : public QAbstractMessageHandler
92 {
93 public:
94     QList<Received> received;
95
96 protected:
97     virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation);
98 };
99
100 void TestMessageHandler::handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation)
101 {
102     Received r;
103     r.type = type;
104     r.description = description;
105     r.identifier = identifier;
106     r.sourceLocation = sourceLocation;
107     received.append(r);
108 }
109
110 void tst_QAbstractMessageHandler::constructor() const
111 {
112     /* Allocate instance. */
113     {
114         TestMessageHandler instance;
115     }
116
117     {
118         TestMessageHandler instance1;
119         TestMessageHandler instance2;
120     }
121
122     {
123         TestMessageHandler instance1;
124         TestMessageHandler instance2;
125         TestMessageHandler instance3;
126     }
127 }
128
129 void tst_QAbstractMessageHandler::constCorrectness() const
130 {
131     /* No members are supposed to be const. */
132 }
133
134 void tst_QAbstractMessageHandler::objectSize() const
135 {
136     /* We shouldn't be adding anything. */
137     QCOMPARE(sizeof(QAbstractMessageHandler), sizeof(QObject));
138 }
139
140 void tst_QAbstractMessageHandler::message() const
141 {
142     TestMessageHandler handler;
143
144     /* Check that the arguments comes out as expected. */
145     handler.message(QtDebugMsg,
146                     QLatin1String("A description"),
147                     QUrl(QLatin1String("http://example.com/ID")),
148                     QSourceLocation(QUrl(QLatin1String("http://example.com/Location")), 4, 5));
149
150     Received expected;
151     expected.type = QtDebugMsg;
152     expected.description = QLatin1String("A description");
153     expected.identifier = QUrl(QLatin1String("http://example.com/ID"));
154     expected.sourceLocation = QSourceLocation(QUrl(QLatin1String("http://example.com/Location")), 4, 5);
155
156     QCOMPARE(expected, handler.received.first());
157 }
158
159 void tst_QAbstractMessageHandler::messageDefaultArguments() const
160 {
161     TestMessageHandler handler;
162
163     /* The three last arguments in message() are optional. Check that they are what we promise. */
164     handler.message(QtDebugMsg, QLatin1String("A description"));
165
166     Received expected;
167     expected.type = QtDebugMsg;
168     expected.description = QLatin1String("A description");
169     expected.identifier = QUrl();
170     expected.sourceLocation = QSourceLocation();
171
172     QCOMPARE(expected, handler.received.first());
173 }
174
175 void tst_QAbstractMessageHandler::hasQ_OBJECTMacro() const
176 {
177     TestMessageHandler messageHandler;
178     /* If this code fails to compile, the Q_OBJECT macro is missing in
179      * the class declaration. */
180     QAbstractMessageHandler *const secondPointer = qobject_cast<QAbstractMessageHandler *>(&messageHandler);
181     /* The static_cast is for compiling on broken compilers. */
182     QCOMPARE(static_cast<QAbstractMessageHandler *>(&messageHandler), secondPointer);
183 }
184
185 QTEST_MAIN(tst_QAbstractMessageHandler)
186
187 #include "tst_qabstractmessagehandler.moc"
188 #else
189 QTEST_NOOP_MAIN
190 #endif
191
192 // vim: et:ts=4:sw=4:sts=4