1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2015 Olivier Goffart <ogoffart@woboq.com> |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the test suite of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | |
31 | #include <QtTest/QtTest> |
32 | |
33 | #include <qobject.h> |
34 | #include <qmetaobject.h> |
35 | #include <wobjectimpl.h> |
36 | |
37 | struct CustomType |
38 | { |
39 | int padding; |
40 | QString str; |
41 | CustomType(const QString &str = QString()) : str(str) {} |
42 | operator QString() const { return str; } |
43 | friend bool operator!=(const CustomType &a, const CustomType &b) |
44 | { return a.str != b.str; } |
45 | }; |
46 | |
47 | Q_DECLARE_METATYPE(CustomType) |
48 | |
49 | class tst_QMetaProperty : public QObject |
50 | { |
51 | W_OBJECT(tst_QMetaProperty) |
52 | |
53 | private slots: |
54 | #define DECLARE_TEST(NAME) void NAME(); W_SLOT(NAME) |
55 | DECLARE_TEST(hasStdCppSet) |
56 | DECLARE_TEST(isConstant) |
57 | DECLARE_TEST(isFinal) |
58 | DECLARE_TEST(gadget) |
59 | DECLARE_TEST(readAndWriteWithLazyRegistration) |
60 | DECLARE_TEST(mapProperty) |
61 | DECLARE_TEST(conversion) |
62 | #undef DECLARE_TEST |
63 | |
64 | public: |
65 | enum EnumType { EnumType1 }; |
66 | |
67 | void setValue(EnumType) {} |
68 | EnumType getValue() const { return EnumType1; } |
69 | void set_value(EnumType) {} |
70 | EnumType get_value() const { return EnumType1; } |
71 | |
72 | void resetValue7() { value7 = QStringLiteral("reset" ); } |
73 | int value8() const { return 1; } |
74 | int value9() const { return 1; } |
75 | int value10() const { return 1; } |
76 | |
77 | QString value7; |
78 | QMap<int, int> map; |
79 | CustomType custom; |
80 | |
81 | W_PROPERTY(EnumType, value WRITE setValue READ getValue) |
82 | W_PROPERTY(EnumType, value2 WRITE set_value READ get_value) |
83 | W_PROPERTY(QString, value7 MEMBER value7 RESET resetValue7) |
84 | W_PROPERTY(int, value8 READ value8) |
85 | W_PROPERTY(int, value9 READ value9 CONSTANT) |
86 | W_PROPERTY(int, value10 READ value10 FINAL) |
87 | W_PROPERTY((QMap<int, int>), map MEMBER map) |
88 | W_PROPERTY(CustomType, custom MEMBER custom) |
89 | |
90 | }; |
91 | |
92 | W_OBJECT_IMPL(tst_QMetaProperty) |
93 | |
94 | void tst_QMetaProperty::hasStdCppSet() |
95 | { |
96 | QSKIP("Not supported by W_PROPERTY" ); |
97 | const QMetaObject *mo = metaObject(); |
98 | |
99 | QMetaProperty prop = mo->property(mo->indexOfProperty("value" )); |
100 | QVERIFY(prop.isValid()); |
101 | QVERIFY(prop.hasStdCppSet()); |
102 | |
103 | prop = mo->property(mo->indexOfProperty("value2" )); |
104 | QVERIFY(prop.isValid()); |
105 | QVERIFY(!prop.hasStdCppSet()); |
106 | } |
107 | |
108 | void tst_QMetaProperty::isConstant() |
109 | { |
110 | const QMetaObject *mo = metaObject(); |
111 | |
112 | QMetaProperty prop = mo->property(mo->indexOfProperty("value8" )); |
113 | QVERIFY(prop.isValid()); |
114 | QVERIFY(!prop.isConstant()); |
115 | |
116 | prop = mo->property(mo->indexOfProperty("value9" )); |
117 | QVERIFY(prop.isValid()); |
118 | QVERIFY(prop.isConstant()); |
119 | } |
120 | |
121 | void tst_QMetaProperty::isFinal() |
122 | { |
123 | const QMetaObject *mo = metaObject(); |
124 | |
125 | QMetaProperty prop = mo->property(mo->indexOfProperty("value10" )); |
126 | QVERIFY(prop.isValid()); |
127 | QVERIFY(prop.isFinal()); |
128 | |
129 | prop = mo->property(mo->indexOfProperty("value9" )); |
130 | QVERIFY(prop.isValid()); |
131 | QVERIFY(!prop.isFinal()); |
132 | } |
133 | |
134 | class MyGadget { |
135 | public: |
136 | QString m_value; |
137 | void setValue(const QString &value) { m_value = value; } |
138 | QString getValue() { return m_value; } |
139 | void resetValue() { m_value = QLatin1Literal("reset" ); } |
140 | W_GADGET(MyGadget) |
141 | W_PROPERTY(QString, value READ getValue WRITE setValue RESET resetValue) |
142 | }; |
143 | |
144 | W_GADGET_IMPL(MyGadget) |
145 | |
146 | void tst_QMetaProperty::gadget() |
147 | { |
148 | const QMetaObject *mo = &MyGadget::staticMetaObject; |
149 | QMetaProperty valueProp = mo->property(mo->indexOfProperty("value" )); |
150 | QVERIFY(valueProp.isValid()); |
151 | { |
152 | MyGadget g; |
153 | QString hello = QLatin1Literal("hello" ); |
154 | QVERIFY(valueProp.writeOnGadget(&g, hello)); |
155 | QCOMPARE(g.m_value, QLatin1String("hello" )); |
156 | QCOMPARE(valueProp.readOnGadget(&g), QVariant(hello)); |
157 | QVERIFY(valueProp.resetOnGadget(&g)); |
158 | QCOMPARE(valueProp.readOnGadget(&g), QVariant(QLatin1String("reset" ))); |
159 | } |
160 | } |
161 | |
162 | struct CustomReadObject : QObject |
163 | { |
164 | W_OBJECT(CustomReadObject) |
165 | }; |
166 | |
167 | struct CustomWriteObject : QObject |
168 | { |
169 | W_OBJECT(CustomWriteObject) |
170 | }; |
171 | |
172 | struct CustomWriteObjectChild : CustomWriteObject |
173 | { |
174 | W_OBJECT(CustomWriteObjectChild) |
175 | }; |
176 | |
177 | struct TypeLazyRegistration : QObject |
178 | { |
179 | W_OBJECT(TypeLazyRegistration) |
180 | CustomReadObject *_read; |
181 | CustomWriteObject *_write; |
182 | W_PROPERTY(CustomReadObject *,read MEMBER _read) |
183 | W_PROPERTY(CustomWriteObject *,write MEMBER _write) |
184 | public: |
185 | TypeLazyRegistration() |
186 | : _read() |
187 | , _write() |
188 | {} |
189 | }; |
190 | |
191 | W_OBJECT_IMPL(CustomReadObject) |
192 | W_OBJECT_IMPL(CustomWriteObject) |
193 | W_OBJECT_IMPL(CustomWriteObjectChild) |
194 | W_OBJECT_IMPL(TypeLazyRegistration) |
195 | |
196 | |
197 | void tst_QMetaProperty::readAndWriteWithLazyRegistration() |
198 | { |
199 | QCOMPARE(QMetaType::type("CustomReadObject*" ), int(QMetaType::UnknownType)); |
200 | QCOMPARE(QMetaType::type("CustomWriteObject*" ), int(QMetaType::UnknownType)); |
201 | |
202 | TypeLazyRegistration o; |
203 | QVERIFY(o.property("read" ).isValid()); |
204 | QVERIFY(QMetaType::type("CustomReadObject*" ) != QMetaType::UnknownType); |
205 | QCOMPARE(QMetaType::type("CustomWriteObject*" ), int(QMetaType::UnknownType)); |
206 | |
207 | CustomWriteObjectChild data; |
208 | QVariant value = QVariant::fromValue(&data); // this register CustomWriteObjectChild |
209 | // check if base classes are not registered automatically, otherwise this test would be meaningless |
210 | QCOMPARE(QMetaType::type("CustomWriteObject*" ), int(QMetaType::UnknownType)); |
211 | QVERIFY(o.setProperty("write" , value)); |
212 | QVERIFY(QMetaType::type("CustomWriteObject*" ) != QMetaType::UnknownType); |
213 | QCOMPARE(o.property("write" ).value<CustomWriteObjectChild*>(), &data); |
214 | } |
215 | |
216 | void tst_QMetaProperty::mapProperty() |
217 | { |
218 | map.insert(5, 9); |
219 | QVariant v1 = QVariant::fromValue(map); |
220 | QVariant v = property("map" ); |
221 | QVERIFY(v.isValid()); |
222 | QCOMPARE(map, (v.value<QMap<int,int> >())); |
223 | QCOMPARE(v.typeName(), "QMap<int,int>" ); |
224 | } |
225 | |
226 | void tst_QMetaProperty::conversion() |
227 | { |
228 | if (qVersion() < QByteArray("5.6.0" )) |
229 | QSKIP("this is a test for a bug in 5.5" ); |
230 | |
231 | |
232 | QMetaType::registerConverter<QString, CustomType>(); |
233 | QMetaType::registerConverter<CustomType, QString>(); |
234 | |
235 | QString hello = QStringLiteral("Hello" ); |
236 | |
237 | // Write to a QString property using a CustomType in a QVariant |
238 | QMetaProperty value7P = metaObject()->property(metaObject()->indexOfProperty("value7" )); |
239 | QVERIFY(value7P.isValid()); |
240 | QVERIFY(value7P.write(this, QVariant::fromValue(CustomType(hello)))); |
241 | QCOMPARE(value7, hello); |
242 | |
243 | // Write to a CustomType property using a QString in a QVariant |
244 | QMetaProperty customP = metaObject()->property(metaObject()->indexOfProperty("custom" )); |
245 | QVERIFY(customP.isValid()); |
246 | QVERIFY(customP.write(this, hello)); |
247 | QCOMPARE(custom.str, hello); |
248 | |
249 | // Something that cannot be converted should fail |
250 | QVERIFY(!customP.write(this, 45)); |
251 | QVERIFY(!customP.write(this, QVariant::fromValue(this))); |
252 | QVERIFY(!value7P.write(this, QVariant::fromValue(this))); |
253 | QVERIFY(!value7P.write(this, QVariant::fromValue<QObject*>(this))); |
254 | |
255 | // none of this should have changed the values |
256 | QCOMPARE(value7, hello); |
257 | QCOMPARE(custom.str, hello); |
258 | |
259 | // Empty variant should be converted to default object |
260 | QVERIFY(customP.write(this, QVariant())); |
261 | QCOMPARE(custom.str, QString()); |
262 | // or reset resetable |
263 | QVERIFY(value7P.write(this, QVariant())); |
264 | QCOMPARE(value7, QLatin1Literal("reset" )); |
265 | } |
266 | |
267 | QTEST_MAIN(tst_QMetaProperty) |
268 | |