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#ifndef TST_QVARIANT_COMMON
30#define TST_QVARIANT_COMMON
31
32#include <QString>
33
34class MessageHandler {
35public:
36 MessageHandler(const int typeId, QtMessageHandler msgHandler = handler)
37 : oldMsgHandler(qInstallMessageHandler(msgHandler))
38 {
39 currentId = typeId;
40 }
41
42 ~MessageHandler()
43 {
44 qInstallMessageHandler(oldMsgHandler);
45 }
46
47 bool testPassed() const
48 {
49 return ok;
50 }
51protected:
52 static void handler(QtMsgType, const QMessageLogContext &, const QString &msg)
53 {
54 // Format itself is not important, but basic data as a type name should be included in the output
55 ok = msg.startsWith(s: "QVariant(");
56 QVERIFY2(ok, (QString::fromLatin1("Message is not started correctly: '") + msg + '\'').toLatin1().constData());
57 ok &= (currentId == QMetaType::UnknownType
58 ? msg.contains(s: "Invalid")
59 : msg.contains(s: QMetaType::typeName(type: currentId)));
60 QVERIFY2(ok, (QString::fromLatin1("Message doesn't contain type name: '") + msg + '\'').toLatin1().constData());
61 if (currentId == QMetaType::Char || currentId == QMetaType::QChar) {
62 // Chars insert '\0' into the qdebug stream, it is not possible to find a real string length
63 return;
64 }
65 if (QMetaType::typeFlags(type: currentId) & QMetaType::PointerToQObject) {
66 QByteArray currentName = QMetaType::typeName(type: currentId);
67 currentName.chop(n: 1);
68 ok &= (msg.contains(s: ", " + currentName) || msg.contains(s: ", 0x0"));
69 }
70 ok &= msg.endsWith(c: QLatin1Char(')'));
71 QVERIFY2(ok, (QString::fromLatin1("Message is not correctly finished: '") + msg + '\'').toLatin1().constData());
72
73 }
74
75 QtMessageHandler oldMsgHandler;
76 static int currentId;
77 static bool ok;
78};
79bool MessageHandler::ok;
80int MessageHandler::currentId;
81
82#define TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS \
83 QTest::addColumn<QVariant>("val"); \
84 QTest::addColumn<bool>("BitArrayCast"); \
85 QTest::addColumn<bool>("BitmapCast"); \
86 QTest::addColumn<bool>("BoolCast"); \
87 QTest::addColumn<bool>("BrushCast"); \
88 QTest::addColumn<bool>("ByteArrayCast"); \
89 QTest::addColumn<bool>("ColorCast"); \
90 QTest::addColumn<bool>("CursorCast"); \
91 QTest::addColumn<bool>("DateCast"); \
92 QTest::addColumn<bool>("DateTimeCast"); \
93 QTest::addColumn<bool>("DoubleCast"); \
94 QTest::addColumn<bool>("FontCast"); \
95 QTest::addColumn<bool>("ImageCast"); \
96 QTest::addColumn<bool>("IntCast"); \
97 QTest::addColumn<bool>("InvalidCast"); \
98 QTest::addColumn<bool>("KeySequenceCast"); \
99 QTest::addColumn<bool>("ListCast"); \
100 QTest::addColumn<bool>("LongLongCast"); \
101 QTest::addColumn<bool>("MapCast"); \
102 QTest::addColumn<bool>("PaletteCast"); \
103 QTest::addColumn<bool>("PenCast"); \
104 QTest::addColumn<bool>("PixmapCast"); \
105 QTest::addColumn<bool>("PointCast"); \
106 QTest::addColumn<bool>("RectCast"); \
107 QTest::addColumn<bool>("RegionCast"); \
108 QTest::addColumn<bool>("SizeCast"); \
109 QTest::addColumn<bool>("SizePolicyCast"); \
110 QTest::addColumn<bool>("StringCast"); \
111 QTest::addColumn<bool>("StringListCast"); \
112 QTest::addColumn<bool>("TimeCast"); \
113 QTest::addColumn<bool>("UIntCast"); \
114 QTest::addColumn<bool>("ULongLongCast");
115
116#define TST_QVARIANT_CANCONVERT_FETCH_DATA \
117 QFETCH(QVariant, val); \
118 QFETCH(bool, BitArrayCast); \
119 QFETCH(bool, BitmapCast); \
120 QFETCH(bool, BoolCast); \
121 QFETCH(bool, BrushCast); \
122 QFETCH(bool, ByteArrayCast); \
123 QFETCH(bool, ColorCast); \
124 QFETCH(bool, CursorCast); \
125 QFETCH(bool, DateCast); \
126 QFETCH(bool, DateTimeCast); \
127 QFETCH(bool, DoubleCast); \
128 QFETCH(bool, FontCast); \
129 QFETCH(bool, ImageCast); \
130 QFETCH(bool, IntCast); \
131 QFETCH(bool, InvalidCast); \
132 QFETCH(bool, KeySequenceCast); \
133 QFETCH(bool, ListCast); \
134 QFETCH(bool, LongLongCast); \
135 QFETCH(bool, MapCast); \
136 QFETCH(bool, PaletteCast); \
137 QFETCH(bool, PenCast); \
138 QFETCH(bool, PixmapCast); \
139 QFETCH(bool, PointCast); \
140 QFETCH(bool, RectCast); \
141 QFETCH(bool, RegionCast); \
142 QFETCH(bool, SizeCast); \
143 QFETCH(bool, SizePolicyCast); \
144 QFETCH(bool, StringCast); \
145 QFETCH(bool, StringListCast); \
146 QFETCH(bool, TimeCast); \
147 QFETCH(bool, UIntCast); \
148 QFETCH(bool, ULongLongCast);
149
150#define TST_QVARIANT_CANCONVERT_COMPARE_DATA \
151 QCOMPARE(val.canConvert(QVariant::BitArray), BitArrayCast); \
152 QCOMPARE(val.canConvert(QVariant::Bitmap), BitmapCast); \
153 QCOMPARE(val.canConvert(QVariant::Bool), BoolCast); \
154 QCOMPARE(val.canConvert(QVariant::Brush), BrushCast); \
155 QCOMPARE(val.canConvert(QVariant::ByteArray), ByteArrayCast); \
156 QCOMPARE(val.canConvert(QVariant::Color), ColorCast); \
157 QCOMPARE(val.canConvert(QVariant::Cursor), CursorCast); \
158 QCOMPARE(val.canConvert(QVariant::Date), DateCast); \
159 QCOMPARE(val.canConvert(QVariant::DateTime), DateTimeCast); \
160 QCOMPARE(val.canConvert(QVariant::Double), DoubleCast); \
161 QCOMPARE(val.canConvert(QVariant::Type(QMetaType::Float)), DoubleCast); \
162 QCOMPARE(val.canConvert(QVariant::Font), FontCast); \
163 QCOMPARE(val.canConvert(QVariant::Image), ImageCast); \
164 QCOMPARE(val.canConvert(QVariant::Int), IntCast); \
165 QCOMPARE(val.canConvert(QVariant::Invalid), InvalidCast); \
166 QCOMPARE(val.canConvert(QVariant::KeySequence), KeySequenceCast); \
167 QCOMPARE(val.canConvert(QVariant::List), ListCast); \
168 QCOMPARE(val.canConvert(QVariant::LongLong), LongLongCast); \
169 QCOMPARE(val.canConvert(QVariant::Map), MapCast); \
170 QCOMPARE(val.canConvert(QVariant::Palette), PaletteCast); \
171 QCOMPARE(val.canConvert(QVariant::Pen), PenCast); \
172 QCOMPARE(val.canConvert(QVariant::Pixmap), PixmapCast); \
173 QCOMPARE(val.canConvert(QVariant::Point), PointCast); \
174 QCOMPARE(val.canConvert(QVariant::Rect), RectCast); \
175 QCOMPARE(val.canConvert(QVariant::Region), RegionCast); \
176 QCOMPARE(val.canConvert(QVariant::Size), SizeCast); \
177 QCOMPARE(val.canConvert(QVariant::SizePolicy), SizePolicyCast); \
178 QCOMPARE(val.canConvert(QVariant::String), StringCast); \
179 QCOMPARE(val.canConvert(QVariant::StringList), StringListCast); \
180 QCOMPARE(val.canConvert(QVariant::Time), TimeCast); \
181 QCOMPARE(val.canConvert(QVariant::UInt), UIntCast); \
182 QCOMPARE(val.canConvert(QVariant::ULongLong), ULongLongCast);
183
184
185#endif
186

source code of qtbase/tests/auto/other/qvariant_common/tst_qvariant_common.h