1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Stephen Kelly <stephen.kelly@kdab.com> |
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 | #include <qwidget.h> |
32 | #include <qlabel.h> |
33 | |
34 | class tst_QWidgetMetaType : public QObject |
35 | { |
36 | Q_OBJECT |
37 | |
38 | public: |
39 | tst_QWidgetMetaType() {} |
40 | virtual ~tst_QWidgetMetaType() {} |
41 | |
42 | private slots: |
43 | void metaObject(); |
44 | void saveAndLoadBuiltin_data(); |
45 | void saveAndLoadBuiltin(); |
46 | }; |
47 | |
48 | class CustomWidget : public QWidget |
49 | { |
50 | Q_OBJECT |
51 | public: |
52 | CustomWidget(QWidget *parent = 0) |
53 | : QWidget(parent) |
54 | { |
55 | |
56 | } |
57 | }; |
58 | |
59 | Q_STATIC_ASSERT(( QMetaTypeId2<QSizePolicy>::IsBuiltIn)); |
60 | Q_STATIC_ASSERT((!QMetaTypeId2<QWidget*>::IsBuiltIn)); |
61 | Q_STATIC_ASSERT((!QMetaTypeId2<QList<QSizePolicy> >::IsBuiltIn)); |
62 | Q_STATIC_ASSERT((!QMetaTypeId2<QMap<QString,QSizePolicy> >::IsBuiltIn)); |
63 | |
64 | |
65 | void tst_QWidgetMetaType::metaObject() |
66 | { |
67 | QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QWidget*>()), &QWidget::staticMetaObject); |
68 | QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QLabel*>()), &QLabel::staticMetaObject); |
69 | QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<CustomWidget*>()), &CustomWidget::staticMetaObject); |
70 | QCOMPARE(QMetaType::metaObjectForType(qMetaTypeId<QSizePolicy>()), &QSizePolicy::staticMetaObject); |
71 | } |
72 | |
73 | template <typename T> |
74 | struct StreamingTraits |
75 | { |
76 | // Streamable by default, as currently all widgets built-in types are streamable |
77 | enum { isStreamable = 1 }; |
78 | }; |
79 | |
80 | void tst_QWidgetMetaType::saveAndLoadBuiltin_data() |
81 | { |
82 | QTest::addColumn<int>(name: "type" ); |
83 | QTest::addColumn<bool>(name: "isStreamable" ); |
84 | |
85 | #define ADD_METATYPE_TEST_ROW(MetaTypeName, MetaTypeId, RealType) \ |
86 | QTest::newRow(#RealType) << MetaTypeId << bool(StreamingTraits<RealType>::isStreamable); |
87 | QT_FOR_EACH_STATIC_WIDGETS_CLASS(ADD_METATYPE_TEST_ROW) |
88 | #undef ADD_METATYPE_TEST_ROW |
89 | } |
90 | |
91 | void tst_QWidgetMetaType::saveAndLoadBuiltin() |
92 | { |
93 | QFETCH(int, type); |
94 | QFETCH(bool, isStreamable); |
95 | |
96 | void *value = QMetaType::create(type); |
97 | |
98 | QByteArray ba; |
99 | QDataStream stream(&ba, QIODevice::ReadWrite); |
100 | QCOMPARE(QMetaType::save(stream, type, value), isStreamable); |
101 | QCOMPARE(stream.status(), QDataStream::Ok); |
102 | |
103 | if (isStreamable) |
104 | QVERIFY(QMetaType::load(stream, type, value)); |
105 | |
106 | stream.device()->seek(pos: 0); |
107 | stream.resetStatus(); |
108 | QCOMPARE(QMetaType::load(stream, type, value), isStreamable); |
109 | QCOMPARE(stream.status(), QDataStream::Ok); |
110 | |
111 | if (isStreamable) |
112 | QVERIFY(QMetaType::load(stream, type, value)); |
113 | |
114 | QMetaType::destroy(type, data: value); |
115 | } |
116 | |
117 | |
118 | QTEST_MAIN(tst_QWidgetMetaType) |
119 | #include "tst_qwidgetmetatype.moc" |
120 | |