summaryrefslogtreecommitdiffstats
path: root/src/testlib/qpropertytesthelper_p.h
blob: 45d665136d153de83339ea2ca81a2ec6db97951b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtTest module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QPROPERTYTESTHELPER_P_H
#define QPROPERTYTESTHELPER_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/QObject>
#include <QtCore/QProperty>
#include <QtTest/QSignalSpy>
#include <QTest>

QT_BEGIN_NAMESPACE

namespace QTestPrivate {

/*!
    \internal

    This helper macro is used as a wrapper around \l QVERIFY2() to provide a
    detailed error message in case of failure. It is intended to be used \e only
    in the helper functions below.

    The custom \a comparator method is used to check if the \a actual and
    \a expected values are equal or not.

    The macro uses a custom \a represent callback to generate the string
    representation of \a actual and \a expected.

    The error message is close to the one provided by the \l QCOMPARE() macro.
    Specifically the implementation is taken from the \c formatFailMessage()
    function, which is defined in the \c qtestresult.cpp file.
*/
#define QPROPERTY_TEST_COMPARISON_HELPER(actual, expected, comparator, represent)                  \
    do {                                                                                           \
        const size_t maxMsgLen = 1024;                                                             \
        char msg[maxMsgLen] = { '\0' };                                                            \
        auto actualStr = represent(actual);                                                        \
        auto expectedStr = represent(expected);                                                    \
        const size_t len1 = mbstowcs(nullptr, #actual, maxMsgLen);                                 \
        const size_t len2 = mbstowcs(nullptr, #expected, maxMsgLen);                               \
        qsnprintf(msg, maxMsgLen, "\n%s\n   Actual   (%s)%*s %s\n   Expected (%s)%*s %s\n",        \
                  "Comparison failed!", #actual, qMax(len1, len2) - len1 + 1, ":",                 \
                  actualStr ? actualStr : "<null>", #expected, qMax(len1, len2) - len2 + 1, ":",   \
                  expectedStr ? expectedStr : "<null>");                                           \
        delete[] actualStr;                                                                        \
        delete[] expectedStr;                                                                      \
        QVERIFY2(comparator(actual, expected), msg);                                               \
    } while (false)

/*!
    \internal
    Basic testing of a bindable property.

    This helper function tests the behavior of bindable read/write property
    \a propertyName, of type \c PropertyType, in class \c TestedClass.
    The caller must supply an \a instance of \c TestedClass and two distinct
    values, \a initial and \a changed, of \c PropertyType.

    Since the first part of the test sets the property to \a initial, it
    \e {must not} be the default value of the property, or the check that it
    was set will be vacuous.

    By default \c {operator==()} is used to compare values of the property and
    \c {QTest::toString()} is used to generate proper error messages.

    If such comparison is not supported for \c PropertyType, or the comparison
    it supports is not appropriate to this property, a custom \a comparator can
    be supplied.

    Apart from that, a custom \a represent callback can also be specified to
    generate a string representation of \c PropertyType. If supplied, it must
    allocate its returned string using \c {new char[]}, so that it can be used
    in place of \l {QTest::toString()}.

    \note Any test calling this method will need to call
    \code
    if (QTest::currentTestFailed())
        return;
    \endcode
    after doing so, if there is any later code in the test. If testing several
    properties in one test method, emitting a warning message saying which
    property failed, before returning, is a kindness to readers of the output.
*/
template<typename TestedClass, typename PropertyType>
void testReadWritePropertyBasics(
        TestedClass &instance, const PropertyType &initial, const PropertyType &changed,
        const char *propertyName,
        std::function<bool(const PropertyType &, const PropertyType &)> comparator =
                [](const PropertyType &lhs, const PropertyType &rhs) { return lhs == rhs; },
        std::function<char *(const PropertyType &)> represent =
                [](const PropertyType &val) { return QTest::toString(val); })
{
    // get the property
    const QMetaObject *metaObject = instance.metaObject();
    QMetaProperty metaProperty = metaObject->property(metaObject->indexOfProperty(propertyName));
    QVERIFY2(metaProperty.metaType() == QMetaType::fromType<PropertyType>(),
             QByteArray("Preconditions not met for ") +  propertyName  + "\n"
             "The type of initial and changed value does not match the type of the property.\n"
             "Please ensure that the types match exactly (convertability is not enough).\n"
             "You can provide the template types to the "
             "function explicitly to force a certain type.\n"
             "Expected was a " + metaProperty.metaType().name()
             + " but " + QMetaType::fromType<PropertyType>().name() + " was provided.");

    // in case the TestedClass has setProperty()/property() methods.
    QObject &testedObj = static_cast<QObject &>(instance);

    QVERIFY2(metaProperty.isBindable() && metaProperty.isWritable(),
             "Preconditions not met for " + QByteArray(propertyName));

    QScopedPointer<QSignalSpy> spy(nullptr);
    if (metaProperty.hasNotifySignal())
        spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal()));

    testedObj.setProperty(propertyName, QVariant::fromValue(initial));
    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), initial, comparator,
            represent);
    if (spy)
        QCOMPARE(spy->count(), 1);

    QUntypedBindable bindable = metaProperty.bindable(&instance);

    // Bind to the object's property (using both lambda and
    // Qt:makePropertyBinding).
    QProperty<PropertyType> propObserver(changed);
    propObserver.setBinding(bindable.makeBinding());
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), initial, comparator, represent);

    QProperty<PropertyType> propObserverLambda(changed);
    propObserverLambda.setBinding(
            [&]() { return testedObj.property(propertyName).template value<PropertyType>(); });
    QPROPERTY_TEST_COMPARISON_HELPER(propObserverLambda.value(), initial, comparator, represent);

    testedObj.setProperty(propertyName, QVariant::fromValue(changed));
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), changed, comparator, represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserverLambda.value(), changed, comparator, represent);
    if (spy)
        QCOMPARE(spy->count(), 2);

    // Bind object's property to other property
    QProperty<PropertyType> propSetter(initial);
    QVERIFY(!bindable.hasBinding());
    bindable.setBinding(Qt::makePropertyBinding(propSetter));

    QVERIFY(bindable.hasBinding());
    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), initial, comparator,
            represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), initial, comparator, represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserverLambda.value(), initial, comparator, represent);
    if (spy)
        QCOMPARE(spy->count(), 3);

    // Count notifications triggered; should only happen on actual change.
    int updateCount = 0;
    auto handler = bindable.onValueChanged([&updateCount]() { ++updateCount; });
    Q_UNUSED(handler)

    propSetter.setValue(changed);
    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), changed, comparator,
            represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), changed, comparator, represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserverLambda.value(), changed, comparator, represent);
    QCOMPARE(updateCount, 1);
    if (spy)
        QCOMPARE(spy->count(), 4);

    // Test that manually setting the value (even the same one) breaks the
    // binding.
    testedObj.setProperty(propertyName, QVariant::fromValue(changed));
    QVERIFY(!bindable.hasBinding());
    // Setting the same value should have no impact on udpateCount.
    QCOMPARE(updateCount, 1);

    // value didn't change -> the signal should not be emitted
    if (spy)
        QCOMPARE(spy->count(), 4);
}

/*!
    \internal
    Basic testing of a bindable property that is writable only once.

    The write-once properties are writable properties which accept only
    one valid setting of the value ("write"), after which later attempts
    are ignored.

    This helper function tests the behavior of bindable write-once property
    \a propertyName, of type \c PropertyType, in class \c TestedClass.
    The caller must supply an \a instance of \c TestedClass and two distinct
    values, \a initial and \a changed, of \c PropertyType.

    The property of \a instance must not yet have been set when this function
    is called. The value it has before being set should be passed as \a prior
    and a distinct value, that this test can set it to, as \a changed.

    The \a bindingPreservedOnWrite parameter controls whether this function
    expects the binding set by this function to be preserved when setting a value
    directly. The default value is 'true'.

    By default \c {operator==()} is used to compare values of the property and
    \c {QTest::toString()} is used to generate proper error messages.

    If such comparison is not supported for \c PropertyType, or the comparison
    it supports is not appropriate to this property, a custom \a comparator can
    be supplied.

    Apart from that, a custom \a represent callback can also be specified to
    generate a string representation of \c PropertyType. If supplied, it must
    allocate its returned string using \c {new char[]}, so that it can be used
    in place of \l {QTest::toString()}.

    \note Any test calling this method will need to call
    \code
    if (QTest::currentTestFailed())
        return;
    \endcode
    after doing so, if there is any later code in the test. If testing several
    properties in one test method, emitting a warning message saying which
    property failed, before returning, is a kindness to readers of the output.
*/

template<typename TestedClass, typename PropertyType>
void testWriteOncePropertyBasics(
        TestedClass &instance, const PropertyType &prior, const PropertyType &changed,
        const char *propertyName,
        bool bindingPreservedOnWrite = true,
        std::function<bool(const PropertyType &, const PropertyType &)> comparator =
                [](const PropertyType &lhs, const PropertyType &rhs) { return lhs == rhs; },
        std::function<char *(const PropertyType &)> represent =
                [](const PropertyType &val) { return QTest::toString(val); })
{
    // get the property
    const QMetaObject *metaObject = instance.metaObject();
    QMetaProperty metaProperty = metaObject->property(metaObject->indexOfProperty(propertyName));

    // in case the TestedClass has setProperty()/property() methods.
    QObject &testedObj = static_cast<QObject &>(instance);

    QVERIFY2(metaProperty.metaType() == QMetaType::fromType<PropertyType>(),
             QByteArray("Preconditions not met for ") +  propertyName + "\n"
             "The type of prior and changed value does not match the type of the property.\n"
             "Please ensure that the types match exactly (convertability is not enough).\n"
             "You can provide the template types to the "
             "function explicitly to force a certain type.\n"
             "Property is " + metaProperty.metaType().name()
             + " but parameters are " + QMetaType::fromType<PropertyType>().name() + ".\n");

    QVERIFY2(metaProperty.isBindable(), "Preconditions not met for " + QByteArray(propertyName));

    QUntypedBindable bindable = metaProperty.bindable(&instance);

    QScopedPointer<QSignalSpy> spy(nullptr);
    if (metaProperty.hasNotifySignal())
        spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal()));

    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), prior, comparator,
            represent);

    QProperty<PropertyType> propObserver;
    propObserver.setBinding(bindable.makeBinding());
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), prior, comparator, represent);

    // Create a binding that sets the 'changed' value to the property
    QProperty<PropertyType> propSetter(changed);
    QVERIFY(!bindable.hasBinding());
    bindable.setBinding(Qt::makePropertyBinding(propSetter));
    QVERIFY(bindable.hasBinding());

    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), changed, comparator,
            represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), changed, comparator, represent);
    if (spy)
        QCOMPARE(spy->count(), 1);

    // Attempt to set back the 'prior' value and verify that it has no effect
    testedObj.setProperty(propertyName, QVariant::fromValue(prior));
    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), changed, comparator,
            represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), changed, comparator, represent);
    if (spy)
        QCOMPARE(spy->count(), 1);
    if (bindingPreservedOnWrite)
        QVERIFY(bindable.hasBinding());
    else
        QVERIFY(!bindable.hasBinding());
}


/*!
    \internal
    Basic testing of a read-only bindable property.

    This helper function tests the behavior of bindable read-only property
    \a propertyName, of type \c PropertyType, in class \c TestedClass.
    The caller must supply an \a instance of \c TestedClass and two distinct
    values, \a initial and \a changed, of \c PropertyType.

    When this function is called, the property's value must be \a initial.
    The \a mutator must, when called, cause the property's value to be revised
    to \a changed.

    By default \c {operator==()} is used to compare values of the property and
    \c {QTest::toString()} is used to generate proper error messages.

    If such comparison is not supported for \c PropertyType, or the comparison
    it supports is not appropriate to this property, a custom \a comparator can
    be supplied.

    Apart from that, a custom \a represent callback can also be specified to
    generate a string representation of \c PropertyType. If supplied, it must
    allocate its returned string using \c {new char[]}, so that it can be used
    in place of \l {QTest::toString()}.

    \note Any test calling this method will need to call
    \code
    if (QTest::currentTestFailed())
        return;
    \endcode
    after doing so, if there is any later code in the test. If testing several
    properties in one test method, emitting a warning message saying which
    property failed, before returning, is a kindness to readers of the output.
*/
template<typename TestedClass, typename PropertyType>
void testReadOnlyPropertyBasics(
        TestedClass &instance, const PropertyType &initial, const PropertyType &changed,
        const char *propertyName,
        std::function<void()> mutator = []() { QFAIL("Data modifier function must be provided"); },
        std::function<bool(const PropertyType &, const PropertyType &)> comparator =
                [](const PropertyType &lhs, const PropertyType &rhs) { return lhs == rhs; },
        std::function<char *(const PropertyType &)> represent =
                [](const PropertyType &val) { return QTest::toString(val); })
{
    // get the property
    const QMetaObject *metaObject = instance.metaObject();
    QMetaProperty metaProperty = metaObject->property(metaObject->indexOfProperty(propertyName));

    // in case the TestedClass has setProperty()/property() methods.
    QObject &testedObj = static_cast<QObject &>(instance);

    QVERIFY2(metaProperty.metaType() == QMetaType::fromType<PropertyType>(),
             QByteArray("Preconditions not met for ") +  propertyName  + "\n"
             "The type of initial and changed value does not match the type of the property.\n"
             "Please ensure that the types match exactly (convertability is not enough).\n"
             "You can provide the template types to the "
             "function explicitly to force a certain type.\n"
             "Expected was a " + metaProperty.metaType().name()
             + " but " + QMetaType::fromType<PropertyType>().name() + " was provided.");

    QVERIFY2(metaProperty.isBindable(), "Preconditions not met for " + QByteArray(propertyName));

    QUntypedBindable bindable = metaProperty.bindable(&instance);

    QScopedPointer<QSignalSpy> spy(nullptr);
    if (metaProperty.hasNotifySignal())
        spy.reset(new QSignalSpy(&instance, metaProperty.notifySignal()));

    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), initial, comparator,
            represent);

    // Check that attempting to bind this read-only property to another property has no effect:
    QProperty<PropertyType> propSetter(initial);
    QVERIFY(!bindable.hasBinding());
    bindable.setBinding(Qt::makePropertyBinding(propSetter));
    QVERIFY(!bindable.hasBinding());
    propSetter.setValue(changed);
    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), initial, comparator,
            represent);
    if (spy)
        QCOMPARE(spy->count(), 0);

    QProperty<PropertyType> propObserver;
    propObserver.setBinding(bindable.makeBinding());

    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), initial, comparator, represent);

    // Invoke mutator function. Now property value should be changed.
    mutator();

    QPROPERTY_TEST_COMPARISON_HELPER(
            testedObj.property(propertyName).template value<PropertyType>(), changed, comparator,
            represent);
    QPROPERTY_TEST_COMPARISON_HELPER(propObserver.value(), changed, comparator, represent);

    if (spy)
        QCOMPARE(spy->count(), 1);
}

} // namespace QTestPrivate

QT_END_NAMESPACE

#endif // QPROPERTYTESTHELPER_P_H