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