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 <QtTest/QtTest>
31
32#include <qvariant.h>
33
34#include "tst_qvariant_common.h"
35
36
37class tst_QWidgetsVariant : public QObject
38{
39 Q_OBJECT
40
41private slots:
42
43 void constructor_invalid_data();
44 void constructor_invalid();
45
46 void canConvert_data();
47 void canConvert();
48
49 void writeToReadFromDataStream_data();
50 void writeToReadFromDataStream();
51
52 void qvariant_cast_QObject_data();
53 void qvariant_cast_QObject();
54 void qvariant_cast_QObject_derived();
55
56 void debugStream_data();
57 void debugStream();
58
59 void implicitConstruction();
60
61 void widgetsVariantAtExit();
62};
63
64void tst_QWidgetsVariant::constructor_invalid_data()
65{
66 QTest::addColumn<uint>(name: "typeId");
67
68 QTest::newRow(dataTag: "LastGuiType + 1") << uint(QMetaType::LastGuiType + 1);
69 QVERIFY(!QMetaType::isRegistered(QMetaType::LastGuiType + 1));
70 QTest::newRow(dataTag: "LastWidgetsType + 1") << uint(QMetaType::LastWidgetsType + 1);
71 QVERIFY(!QMetaType::isRegistered(QMetaType::LastWidgetsType + 1));
72}
73
74void tst_QWidgetsVariant::constructor_invalid()
75{
76
77 QFETCH(uint, typeId);
78 {
79 QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression("^Trying to construct an instance of an invalid type, type id:"));
80 QVariant variant(static_cast<QVariant::Type>(typeId));
81 QVERIFY(!variant.isValid());
82 QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
83 }
84 {
85 QTest::ignoreMessage(type: QtWarningMsg, messagePattern: QRegularExpression("^Trying to construct an instance of an invalid type, type id:"));
86 QVariant variant(typeId, /* copy */ 0);
87 QVERIFY(!variant.isValid());
88 QCOMPARE(variant.userType(), int(QMetaType::UnknownType));
89 }
90}
91
92void tst_QWidgetsVariant::canConvert_data()
93{
94 TST_QVARIANT_CANCONVERT_DATATABLE_HEADERS
95
96#ifdef Y
97#undef Y
98#endif
99#ifdef N
100#undef N
101#endif
102#define Y true
103#define N false
104
105 QVariant var;
106
107 // bita bitm bool brsh byta col curs date dt dbl font img int inv kseq list ll map pal pen pix pnt rect reg size sp str strl time uint ull
108
109
110 var = QVariant::fromValue(value: QSizePolicy());
111 QTest::newRow(dataTag: "SizePolicy")
112 << var << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << N << Y << N << N << N << N << N;
113
114#undef N
115#undef Y
116}
117
118void tst_QWidgetsVariant::canConvert()
119{
120 TST_QVARIANT_CANCONVERT_FETCH_DATA
121
122 TST_QVARIANT_CANCONVERT_COMPARE_DATA
123}
124
125
126void tst_QWidgetsVariant::writeToReadFromDataStream_data()
127{
128 QTest::addColumn<QVariant>(name: "writeVariant");
129 QTest::addColumn<bool>(name: "isNull");
130
131 QTest::newRow( dataTag: "sizepolicy_valid" ) << QVariant::fromValue( value: QSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed ) ) << false;
132}
133
134void tst_QWidgetsVariant::writeToReadFromDataStream()
135{
136 QFETCH( QVariant, writeVariant );
137 QFETCH( bool, isNull );
138 QByteArray data;
139
140 QDataStream writeStream( &data, QIODevice::WriteOnly );
141 writeStream << writeVariant;
142
143 QVariant readVariant;
144 QDataStream readStream( &data, QIODevice::ReadOnly );
145 readStream >> readVariant;
146 QVERIFY( readVariant.isNull() == isNull );
147}
148
149class CustomQWidget : public QWidget {
150 Q_OBJECT
151public:
152 CustomQWidget(QWidget *parent = 0) : QWidget(parent) {}
153};
154
155void tst_QWidgetsVariant::qvariant_cast_QObject_data()
156{
157 QTest::addColumn<QVariant>(name: "data");
158 QTest::addColumn<bool>(name: "success");
159
160 QWidget *widget = new QWidget;
161 widget->setObjectName(QString::fromLatin1(str: "Hello"));
162 QTest::newRow(dataTag: "from QWidget") << QVariant::fromValue(value: widget) << true;
163
164 CustomQWidget *customWidget = new CustomQWidget;
165 customWidget->setObjectName(QString::fromLatin1(str: "Hello"));
166 QTest::newRow(dataTag: "from Derived QWidget") << QVariant::fromValue(value: customWidget) << true;
167}
168
169void tst_QWidgetsVariant::qvariant_cast_QObject()
170{
171 QFETCH(QVariant, data);
172 QFETCH(bool, success);
173
174 QObject *o = qvariant_cast<QObject *>(v: data);
175 QCOMPARE(o != 0, success);
176 if (success) {
177 QCOMPARE(o->objectName(), QString::fromLatin1("Hello"));
178 QVERIFY(data.canConvert<QObject*>());
179 QVERIFY(data.canConvert(QMetaType::QObjectStar));
180 QVERIFY(data.canConvert(::qMetaTypeId<QObject*>()));
181 QVERIFY(data.value<QObject*>());
182 QVERIFY(data.convert(QMetaType::QObjectStar));
183 QCOMPARE(data.userType(), int(QMetaType::QObjectStar));
184
185 QVERIFY(data.canConvert<QWidget*>());
186 QVERIFY(data.canConvert(::qMetaTypeId<QWidget*>()));
187 QVERIFY(data.value<QWidget*>());
188 QVERIFY(data.convert(qMetaTypeId<QWidget*>()));
189 QCOMPARE(data.userType(), qMetaTypeId<QWidget*>());
190 } else {
191 QVERIFY(!data.canConvert<QObject*>());
192 QVERIFY(!data.canConvert(QMetaType::QObjectStar));
193 QVERIFY(!data.canConvert(::qMetaTypeId<QObject*>()));
194 QVERIFY(!data.value<QObject*>());
195 QVERIFY(!data.convert(QMetaType::QObjectStar));
196 QVERIFY(data.userType() != QMetaType::QObjectStar);
197 }
198 delete o;
199}
200
201void tst_QWidgetsVariant::qvariant_cast_QObject_derived()
202{
203 CustomQWidget customWidget;
204 QWidget *widget = &customWidget;
205 QVariant data = QVariant::fromValue(value: widget);
206 QCOMPARE(data.userType(), qMetaTypeId<QWidget*>());
207
208 QCOMPARE(data.value<QObject*>(), widget);
209 QCOMPARE(data.value<QWidget*>(), widget);
210 QCOMPARE(data.value<CustomQWidget*>(), widget);
211}
212
213void tst_QWidgetsVariant::debugStream_data()
214{
215 QTest::addColumn<QVariant>(name: "variant");
216 QTest::addColumn<int>(name: "typeId");
217 for (int id = QMetaType::LastGuiType + 1; id < QMetaType::User; ++id) {
218 const char *tagName = QMetaType::typeName(type: id);
219 if (!tagName)
220 continue;
221 QTest::newRow(dataTag: tagName) << QVariant(static_cast<QVariant::Type>(id)) << id;
222 }
223}
224
225void tst_QWidgetsVariant::debugStream()
226{
227 QFETCH(QVariant, variant);
228 QFETCH(int, typeId);
229
230 MessageHandler msgHandler(typeId);
231 qDebug() << variant;
232 QVERIFY(msgHandler.testPassed());
233}
234
235void tst_QWidgetsVariant::widgetsVariantAtExit()
236{
237 // crash test, it should not crash at QApplication exit
238 static QVariant sizePolicy = QSizePolicy();
239 Q_UNUSED(sizePolicy);
240 QVERIFY(true);
241}
242
243
244void tst_QWidgetsVariant::implicitConstruction()
245{
246 // This is a compile-time test
247 QVariant v;
248
249#define FOR_EACH_WIDGETS_CLASS(F) \
250 F(SizePolicy) \
251
252#define CONSTRUCT(TYPE) \
253 { \
254 Q##TYPE t; \
255 v = t; \
256 QVERIFY(true); \
257 }
258
259 FOR_EACH_WIDGETS_CLASS(CONSTRUCT)
260
261#undef CONSTRUCT
262#undef FOR_EACH_WIDGETS_CLASS
263}
264
265QTEST_MAIN(tst_QWidgetsVariant)
266#include "tst_qwidgetsvariant.moc"
267

source code of qtbase/tests/auto/widgets/kernel/qwidgetsvariant/tst_qwidgetsvariant.cpp