1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QFile> |
31 | #include <QtTest/QtTest> |
32 | |
33 | /* We expect these headers to be available. */ |
34 | #include <QtXmlPatterns/QAbstractMessageHandler> |
35 | #include <QtXmlPatterns/qabstractmessagehandler.h> |
36 | #include <QAbstractMessageHandler> |
37 | #include <qabstractmessagehandler.h> |
38 | |
39 | /*! |
40 | \class tst_QAbstractMessageHandler |
41 | \internal |
42 | \since 4.4 |
43 | \brief Tests the QAbstractMessageHandler class. |
44 | */ |
45 | class tst_QAbstractMessageHandler : public QObject |
46 | { |
47 | Q_OBJECT |
48 | |
49 | private Q_SLOTS: |
50 | void constructor() const; |
51 | void constCorrectness() const; |
52 | void message() const; |
53 | void messageDefaultArguments() const; |
54 | void objectSize() const; |
55 | void hasQ_OBJECTMacro() const; |
56 | }; |
57 | |
58 | class Received |
59 | { |
60 | public: |
61 | QtMsgType type; |
62 | QString description; |
63 | QUrl identifier; |
64 | QSourceLocation sourceLocation; |
65 | |
66 | bool operator==(const Received &other) const |
67 | { |
68 | return type == other.type |
69 | && description == other.description |
70 | && identifier == other.identifier |
71 | && sourceLocation == other.sourceLocation; |
72 | } |
73 | }; |
74 | |
75 | class TestMessageHandler : public QAbstractMessageHandler |
76 | { |
77 | public: |
78 | QList<Received> received; |
79 | |
80 | protected: |
81 | virtual void handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation); |
82 | }; |
83 | |
84 | void TestMessageHandler::handleMessage(QtMsgType type, const QString &description, const QUrl &identifier, const QSourceLocation &sourceLocation) |
85 | { |
86 | Received r; |
87 | r.type = type; |
88 | r.description = description; |
89 | r.identifier = identifier; |
90 | r.sourceLocation = sourceLocation; |
91 | received.append(t: r); |
92 | } |
93 | |
94 | void tst_QAbstractMessageHandler::constructor() const |
95 | { |
96 | /* Allocate instance. */ |
97 | { |
98 | TestMessageHandler instance; |
99 | } |
100 | |
101 | { |
102 | TestMessageHandler instance1; |
103 | TestMessageHandler instance2; |
104 | } |
105 | |
106 | { |
107 | TestMessageHandler instance1; |
108 | TestMessageHandler instance2; |
109 | TestMessageHandler instance3; |
110 | } |
111 | } |
112 | |
113 | void tst_QAbstractMessageHandler::constCorrectness() const |
114 | { |
115 | /* No members are supposed to be const. */ |
116 | } |
117 | |
118 | void tst_QAbstractMessageHandler::objectSize() const |
119 | { |
120 | /* We shouldn't be adding anything. */ |
121 | QCOMPARE(sizeof(QAbstractMessageHandler), sizeof(QObject)); |
122 | } |
123 | |
124 | void tst_QAbstractMessageHandler::message() const |
125 | { |
126 | TestMessageHandler handler; |
127 | |
128 | /* Check that the arguments comes out as expected. */ |
129 | handler.message(type: QtDebugMsg, |
130 | description: QLatin1String("A description" ), |
131 | identifier: QUrl(QLatin1String("http://example.com/ID" )), |
132 | sourceLocation: QSourceLocation(QUrl(QLatin1String("http://example.com/Location" )), 4, 5)); |
133 | |
134 | Received expected; |
135 | expected.type = QtDebugMsg; |
136 | expected.description = QLatin1String("A description" ); |
137 | expected.identifier = QUrl(QLatin1String("http://example.com/ID" )); |
138 | expected.sourceLocation = QSourceLocation(QUrl(QLatin1String("http://example.com/Location" )), 4, 5); |
139 | |
140 | QCOMPARE(expected, handler.received.first()); |
141 | } |
142 | |
143 | void tst_QAbstractMessageHandler::messageDefaultArguments() const |
144 | { |
145 | TestMessageHandler handler; |
146 | |
147 | /* The three last arguments in message() are optional. Check that they are what we promise. */ |
148 | handler.message(type: QtDebugMsg, description: QLatin1String("A description" )); |
149 | |
150 | Received expected; |
151 | expected.type = QtDebugMsg; |
152 | expected.description = QLatin1String("A description" ); |
153 | expected.identifier = QUrl(); |
154 | expected.sourceLocation = QSourceLocation(); |
155 | |
156 | QCOMPARE(expected, handler.received.first()); |
157 | } |
158 | |
159 | void tst_QAbstractMessageHandler::hasQ_OBJECTMacro() const |
160 | { |
161 | TestMessageHandler messageHandler; |
162 | /* If this code fails to compile, the Q_OBJECT macro is missing in |
163 | * the class declaration. */ |
164 | QAbstractMessageHandler *const secondPointer = qobject_cast<QAbstractMessageHandler *>(object: &messageHandler); |
165 | /* The static_cast is for compiling on broken compilers. */ |
166 | QCOMPARE(static_cast<QAbstractMessageHandler *>(&messageHandler), secondPointer); |
167 | } |
168 | |
169 | QTEST_MAIN(tst_QAbstractMessageHandler) |
170 | |
171 | #include "tst_qabstractmessagehandler.moc" |
172 | |
173 | // vim: et:ts=4:sw=4:sts=4 |
174 | |