aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp
blob: 1100827f34fffcac30740eb854e938c1e17d77f4 (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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <qtest.h>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlContext>
#include <QDebug>
#include <QScopedPointer>
#include <private/qqmlglobal_p.h>
#include <private/qquickvaluetypes_p.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>
#include "testtypes.h"

QT_BEGIN_NAMESPACE
extern int qt_defaultDpi(void);
QT_END_NAMESPACE

// There is some overlap between the qqmllanguage and qqmlvaluetypes
// test here, but it needs to be separate to ensure that no QML plugins
// are loaded prior to these tests, which could contaminate the type
// system with more providers.

class tst_qqmlvaluetypeproviders : public QQmlDataTest
{
    Q_OBJECT
public:
    tst_qqmlvaluetypeproviders() : QQmlDataTest(QT_QMLTEST_DATADIR) {}

private slots:
    void initTestCase() override;

    void qtqmlValueTypes();   // This test function _must_ be the first test function run.
    void qtquickValueTypes();
    void comparisonSemantics();
    void cppIntegration();
    void jsObjectConversion();
    void invokableFunctions();
    void userType();
    void changedSignal();
    void structured();
    void recursive();
    void date();
};

void tst_qqmlvaluetypeproviders::initTestCase()
{
    QQmlDataTest::initTestCase();
    registerTypes();
}

void tst_qqmlvaluetypeproviders::qtqmlValueTypes()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("qtqmlValueTypes.qml"));
    QVERIFY2(!component.isError(), qPrintable(component.errorString()));
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("qtqmlTypeSuccess").toBool());
    QVERIFY(object->property("qtquickTypeSuccess").toBool());
}

void tst_qqmlvaluetypeproviders::qtquickValueTypes()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("qtquickValueTypes.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("qtqmlTypeSuccess").toBool());
    QVERIFY(object->property("qtquickTypeSuccess").toBool());
}

void tst_qqmlvaluetypeproviders::comparisonSemantics()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("comparisonSemantics.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("comparisonSuccess").toBool());
}

void tst_qqmlvaluetypeproviders::cppIntegration()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("cppIntegration.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);

    // ensure accessing / comparing / assigning cpp-defined props
    // and qml-defined props works in QML.
    QVERIFY(object->property("success").toBool());

    // ensure types match
    QCOMPARE(object->property("g").userType(), object->property("rectf").userType());
    QCOMPARE(object->property("p").userType(), object->property("pointf").userType());
    QCOMPARE(object->property("z").userType(), object->property("sizef").userType());
    QCOMPARE(object->property("v2").userType(), object->property("vector2").userType());
    QCOMPARE(object->property("v3").userType(), object->property("vector").userType());
    QCOMPARE(object->property("v4").userType(), object->property("vector4").userType());
    QCOMPARE(object->property("q").userType(), object->property("quaternion").userType());
    QCOMPARE(object->property("m").userType(), object->property("matrix").userType());
    QCOMPARE(object->property("c").userType(), object->property("color").userType());
    QCOMPARE(object->property("f").userType(), object->property("font").userType());

    // ensure values match
    QCOMPARE(object->property("g").value<QRectF>(), object->property("rectf").value<QRectF>());
    QCOMPARE(object->property("p").value<QPointF>(), object->property("pointf").value<QPointF>());
    QCOMPARE(object->property("z").value<QSizeF>(), object->property("sizef").value<QSizeF>());
    QCOMPARE(object->property("v2").value<QVector2D>(), object->property("vector2").value<QVector2D>());
    QCOMPARE(object->property("v3").value<QVector3D>(), object->property("vector").value<QVector3D>());
    QCOMPARE(object->property("v4").value<QVector4D>(), object->property("vector4").value<QVector4D>());
    QCOMPARE(object->property("q").value<QQuaternion>(), object->property("quaternion").value<QQuaternion>());
    QCOMPARE(object->property("m").value<QMatrix4x4>(), object->property("matrix").value<QMatrix4x4>());
    QCOMPARE(object->property("c").value<QColor>(), object->property("color").value<QColor>());
    QCOMPARE(object->property("f").value<QFont>(), object->property("font").value<QFont>());
}

void tst_qqmlvaluetypeproviders::jsObjectConversion()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("jsObjectConversion.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("qtquickTypeSuccess").toBool());
}

void tst_qqmlvaluetypeproviders::invokableFunctions()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("invokableFunctions.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("complete").toBool());
    QVERIFY(object->property("success").toBool());
}

namespace {

// A value-type class to export to QML
class TestValue
{
public:
    TestValue() : m_p1(0), m_p2(0.0) {}
    TestValue(int p1, double p2) : m_p1(p1), m_p2(p2) {}
    TestValue(const TestValue &other) : m_p1(other.m_p1), m_p2(other.m_p2) {}
    ~TestValue() {}

    TestValue &operator=(const TestValue &other) { m_p1 = other.m_p1; m_p2 = other.m_p2; return *this; }

    int property1() const { return m_p1; }
    void setProperty1(int p1) { m_p1 = p1; }

    double property2() const { return m_p2; }
    void setProperty2(double p2) { m_p2 = p2; }

    bool operator==(const TestValue &other) const { return (m_p1 == other.m_p1) && (m_p2 == other.m_p2); }
    bool operator!=(const TestValue &other) const { return !operator==(other); }

    bool operator<(const TestValue &other) const { if (m_p1 < other.m_p1) return true; return m_p2 < other.m_p2; }

private:
    int m_p1;
    double m_p2;
};

}

Q_DECLARE_METATYPE(TestValue);

namespace {

class TestValueType
{
    TestValue v;
    Q_GADGET
    Q_PROPERTY(int property1 READ property1 WRITE setProperty1)
    Q_PROPERTY(double property2 READ property2 WRITE setProperty2)
public:
    Q_INVOKABLE QString toString() const { return QString::number(property1()) + QLatin1Char(',') + QString::number(property2()); }

    int property1() const { return v.property1(); }
    void setProperty1(int p1) { v.setProperty1(p1); }

    double property2() const { return v.property2(); }
    void setProperty2(double p2) { v.setProperty2(p2); }
};

class TestValueExporter : public QObject
{
    Q_OBJECT
    Q_PROPERTY(TestValue testValue READ testValue WRITE setTestValue)
    QML_NAMED_ELEMENT(TestValueExporter)
public:
    TestValue testValue() const { return m_testValue; }
    void setTestValue(const TestValue &v) { m_testValue = v; }

    Q_INVOKABLE TestValue getTestValue() const { return TestValue(333, 666.999); }

private:
    TestValue m_testValue;
};

}

void tst_qqmlvaluetypeproviders::userType()
{
    qmlRegisterExtendedType<TestValue, TestValueType>("Test", 1, 0, "test_value");
    qmlRegisterTypesAndRevisions<TestValueExporter>("Test", 1);
    Q_ASSERT(qMetaTypeId<TestValue>() >= QMetaType::User);

    TestValueExporter exporter;

    QQmlEngine e;

    QQmlComponent component(&e, testFileUrl("userType.qml"));
    QScopedPointer<QObject> obj(component.createWithInitialProperties({{"testValueExporter", QVariant::fromValue(&exporter)}}));
    QVERIFY(obj != nullptr);
    QCOMPARE(obj->property("success").toBool(), true);
}

void tst_qqmlvaluetypeproviders::changedSignal()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("changedSignal.qml"));
    QVERIFY(!component.isError());
    QVERIFY(component.errors().isEmpty());
    QScopedPointer<QObject> object(component.create());
    QVERIFY(object != nullptr);
    QVERIFY(object->property("complete").toBool());
    QVERIFY(object->property("success").toBool());
}

void tst_qqmlvaluetypeproviders::structured()
{
    QQmlEngine e;
    const QUrl url = testFileUrl("structuredValueTypes.qml");
    QQmlComponent component(&e, url);
    QVERIFY2(!component.isError(), qPrintable(component.errorString()));

    const char *warnings[] = {
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value [object Object]",
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value QVariant(QJSValue, )",
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value [object Object]",
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value QVariant(QVariantMap, QMap())",
        "Could not convert array value at position 5 from QVariantMap to ConstructibleValueType",
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value [object Object]",
        "Could not find any constructor for value type ConstructibleValueType to call"
            " with value QVariant(QJSValue, )"
    };

    for (const auto warning : warnings)
        QTest::ignoreMessage(QtWarningMsg, warning);

    QTest::ignoreMessage(QtWarningMsg, qPrintable(
                             url.toString()  + QStringLiteral(":44: Error: Cannot assign QJSValue "
                                                              "to ConstructibleValueType")));

    QTest::ignoreMessage(QtWarningMsg, qPrintable(
                             url.toString()  + QStringLiteral(":14:5: Unable to assign QJSValue "
                                                              "to ConstructibleValueType")));

    QScopedPointer<QObject> o(component.create());
    QVERIFY2(!o.isNull(), qPrintable(component.errorString()));

    QCOMPARE(o->property("p").value<QPointF>(), QPointF(7, 77));
    QCOMPARE(o->property("s").value<QSizeF>(), QSizeF(7, 77));
    QCOMPARE(o->property("r").value<QRectF>(), QRectF(5, 55, 7, 77));

    QCOMPARE(o->property("p2").value<QPointF>(), QPointF(4, 5));
    QCOMPARE(o->property("s2").value<QSizeF>(), QSizeF(7, 8));
    QCOMPARE(o->property("r2").value<QRectF>(), QRectF(9, 10, 11, 12));

    QCOMPARE(o->property("c1").value<ConstructibleValueType>(), ConstructibleValueType(5));
    QCOMPARE(o->property("c2").value<ConstructibleValueType>(), ConstructibleValueType(0));
    QCOMPARE(o->property("c3").value<ConstructibleValueType>(), ConstructibleValueType(99));
    QCOMPARE(o->property("c4").value<ConstructibleValueType>(), ConstructibleValueType(0));

    const QList<QPointF> actual = o->property("ps").value<QList<QPointF>>();
    const QList<QPointF> expected = {
        QPointF(1, 2), QPointF(3, 4), QPointF(55, std::numeric_limits<double>::quiet_NaN())
    };
    QCOMPARE(actual.size(), expected.size());
    QCOMPARE(actual[0], expected[0]);
    QCOMPARE(actual[1], expected[1]);
    QCOMPARE(actual[2].x(), expected[2].x());
    QVERIFY(std::isnan(actual[2].y()));

    QCOMPARE(o->property("ss").value<QList<QSizeF>>(),
             QList<QSizeF>({QSizeF(5, 6), QSizeF(7, 8), QSizeF(-1, 99)}));
    QCOMPARE(o->property("cs").value<QList<ConstructibleValueType>>(),
             QList<ConstructibleValueType>({1, 2, 3, 4, 5, 0}));

    StructuredValueType b1;
    b1.setI(10);
    b1.setC(14);
    b1.setP(QPointF(1, 44));
    QCOMPARE(o->property("b1").value<StructuredValueType>(), b1);

    StructuredValueType b2;
    b2.setI(11);
    b2.setC(15);
    b2.setP(QPointF(4, 0));
    QCOMPARE(o->property("b2").value<StructuredValueType>(), b2);


    QList<StructuredValueType> bb(3);
    bb[0].setI(21);
    bb[1].setC(22);
    bb[2].setP(QPointF(199, 222));
    QCOMPARE(o->property("bb").value<QList<StructuredValueType>>(), bb);

    MyTypeObject *t = qobject_cast<MyTypeObject *>(o.data());
    QVERIFY(t);

    QCOMPARE(t->constructible(), ConstructibleValueType(47));

    StructuredValueType structured;
    structured.setI(11);
    structured.setC(12);
    structured.setP(QPointF(7, 8));
    QCOMPARE(t->structured(), structured);

    QCOMPARE(o->property("cr1").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(11.25));
    QCOMPARE(o->property("cr2").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(std::numeric_limits<qreal>::infinity()));
    QCOMPARE(o->property("cr3").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(-std::numeric_limits<qreal>::infinity()));
    QCOMPARE(o->property("cr4").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(std::numeric_limits<qreal>::quiet_NaN()));
    QCOMPARE(o->property("cr5").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(0));
    QCOMPARE(o->property("cr6").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(-112.5));
    QCOMPARE(o->property("cr7").value<ConstructibleFromQReal>(),
             ConstructibleFromQReal(50));

    BarrenValueType barren;
    barren.setI(17);
    QCOMPARE(o->property("barren").value<BarrenValueType>(), barren);

    QMetaObject::invokeMethod(o.data(), "changeBarren");
    QCOMPARE(o->property("barren").value<BarrenValueType>(), BarrenValueType(QString()));

    QCOMPARE(o->property("fromObject").value<ConstructibleValueType>(),
             ConstructibleValueType(nullptr));
    QCOMPARE(o->property("aVariant").value<ConstructibleValueType>(),
             ConstructibleValueType(nullptr));
}

void tst_qqmlvaluetypeproviders::recursive()
{
    QQmlEngine e;
    const QUrl url = testFileUrl("recursiveWriteBack.qml");
    QQmlComponent component(&e, url);
    QVERIFY2(!component.isError(), qPrintable(component.errorString()));

    QScopedPointer<QObject> o(component.create());
    QVERIFY(!o.isNull());

    const QList<StructuredValueType> l = o->property("l").value<QList<StructuredValueType>>();
    QCOMPARE(l.size(), 3);
    QCOMPARE(l[2].i(), 4);
    QCOMPARE(l[1].p().x(), 88);
    QCOMPARE(l[0].sizes()[1].width(), 19);

    MyTypeObject *m = qobject_cast<MyTypeObject *>(o.data());
    QCOMPARE(m->structured().p().x(), 76);
}

void tst_qqmlvaluetypeproviders::date()
{
    QQmlEngine e;
    QQmlComponent component(&e, testFileUrl("dateWriteBack.qml"));
    QVERIFY2(component.isReady(), qPrintable(component.errorString()));
    QScopedPointer<QObject> o(component.create());
    QVERIFY(!o.isNull());

    QCOMPARE(o->property("aDateTime").value<QDateTime>().date().day(), 14);
    QCOMPARE(o->property("aDate").value<QDate>().month(), 9);
    QCOMPARE(o->property("aTime").value<QTime>().hour(), 5);
    QCOMPARE(o->property("aVariant").value<QDateTime>().time().minute(), 44);
}

QTEST_MAIN(tst_qqmlvaluetypeproviders)

#include "tst_qqmlvaluetypeproviders.moc"