diff options
Diffstat (limited to 'tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp')
-rw-r--r-- | tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp | 191 |
1 files changed, 187 insertions, 4 deletions
diff --git a/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp b/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp index 5a36c6edd3..da7e14411d 100644 --- a/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp +++ b/tests/auto/qml/qqmlvaluetypeproviders/tst_qqmlvaluetypeproviders.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qtest.h> #include <QQmlEngine> @@ -41,6 +41,7 @@ private slots: void structured(); void recursive(); void date(); + void constructors(); }; void tst_qqmlvaluetypeproviders::initTestCase() @@ -253,7 +254,6 @@ void tst_qqmlvaluetypeproviders::structured() " with value [object Object]", "Could not find any constructor for value type ConstructibleValueType to call" " with value QVariant(QJSValue, )", - "Could not convert QLocale(English, Latin, Australia) to double for property y", "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" @@ -292,8 +292,16 @@ void tst_qqmlvaluetypeproviders::structured() QCOMPARE(o->property("c3").value<ConstructibleValueType>(), ConstructibleValueType(99)); QCOMPARE(o->property("c4").value<ConstructibleValueType>(), ConstructibleValueType(0)); - QCOMPARE(o->property("ps").value<QList<QPointF>>(), - QList<QPointF>({QPointF(1, 2), QPointF(3, 4), QPointF(55, 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>>(), @@ -343,6 +351,112 @@ void tst_qqmlvaluetypeproviders::structured() 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)); + + QCOMPARE(o->property("listResult").toInt(), 12 + 67 + 68); + + + // You can store all kinds of insanity in a VariantObject, but we generally don't. + // Since we cannot rule out the possibility of there being such VariantObjects, we need to test + // their conversions. + + + QCOMPARE(o->property("fromInsanity").value<StructuredValueType>(), StructuredValueType()); + + QV4::Scope scope(e.handle()); + QV4::ScopedString name(scope, scope.engine->newString("insanity")); + + QObject *po = o.data(); + QV4::ScopedObject js( + scope, scope.engine->metaTypeToJS(QMetaType::fromType<MyTypeObject *>(), &po)); + + const QVariantHash hash { + {"i", 12}, + {"c", QUrl("http://example.com")}, + {"p", QVariantMap { + {"x", 17}, + {"y", 18} + }} + }; + QV4::ScopedValue hashValue( + scope, e.handle()->newVariantObject(QMetaType::fromType<QVariantHash>(), &hash)); + + js->put(name, hashValue); + + StructuredValueType fromHash; + fromHash.setI(12); + fromHash.setC(ConstructibleValueType(QUrl())); + fromHash.setP(QPointF(17, 18)); + + QCOMPARE(o->property("fromInsanity").value<StructuredValueType>(), fromHash); + + const QVariantMap map { + {"i", 13}, + {"c", QVariant::fromValue(po) }, + {"p", QVariantMap { + {"x", 19}, + {"y", 20} + }} + }; + QV4::ScopedValue mapValue( + scope, e.handle()->newVariantObject(QMetaType::fromType<QVariantMap>(), &map)); + js->put(name, mapValue); + + StructuredValueType fromMap; + fromMap.setI(13); + fromMap.setC(ConstructibleValueType(po)); + fromMap.setP(QPointF(19, 20)); + + QCOMPARE(o->property("fromInsanity").value<StructuredValueType>(), fromMap); + + BarrenValueType immediate; + immediate.setI(14); + QV4::ScopedValue immediateValue( + scope, e.handle()->newVariantObject(QMetaType::fromType<BarrenValueType>(), &immediate)); + js->put(name, immediateValue); + + StructuredValueType fromImmediate; + fromImmediate.setI(14); + + QCOMPARE(o->property("fromInsanity").value<StructuredValueType>(), fromImmediate); + + QQmlComponent c2(&e); + c2.setData( + "import QtQml; QtObject { property int i: 99; property point p: ({x: 3, y: 4}) }", QUrl()); + QVERIFY(c2.isReady()); + QScopedPointer<QObject> o2(c2.create()); + QVERIFY(!o2.isNull()); + QObject *object = o2.data(); + QV4::ScopedValue objectValue( + scope, e.handle()->newVariantObject(QMetaType::fromType<QObject *>(), &object)); + js->put(name, objectValue); + + StructuredValueType fromObject; + fromObject.setI(99); + fromObject.setP(QPointF(3, 4)); + + QCOMPARE(o->property("fromInsanity").value<StructuredValueType>(), fromObject); + + const MyTypeObject *m = static_cast<const MyTypeObject *>(po); + QVERIFY(!m->hasEffectPadding()); + QMetaObject::invokeMethod(po, "updatePadding"); + QVERIFY(m->hasEffectPadding()); + QCOMPARE(m->effectPadding(), QRectF()); + po->setProperty("newItemPadding", QRectF(1, 2, 3, 4)); + QMetaObject::invokeMethod(po, "updatePadding"); + QVERIFY(m->hasEffectPadding()); + QCOMPARE(m->effectPadding(), QRectF(1, 2, 3, 4)); } void tst_qqmlvaluetypeproviders::recursive() @@ -363,6 +477,9 @@ void tst_qqmlvaluetypeproviders::recursive() MyTypeObject *m = qobject_cast<MyTypeObject *>(o.data()); QCOMPARE(m->structured().p().x(), 76); + + // Recursive write back into a list detached from the property. + QCOMPARE(m->property("aa").toInt(), 12); } void tst_qqmlvaluetypeproviders::date() @@ -379,6 +496,72 @@ void tst_qqmlvaluetypeproviders::date() QCOMPARE(o->property("aVariant").value<QDateTime>().time().minute(), 44); } +void tst_qqmlvaluetypeproviders::constructors() +{ + + QQmlEngine e; + + { + const auto guard = qScopeGuard([]() { Padding::log.clear(); }); + QQmlComponent component(&e); + component.setData("import Test\nMyItem { padding : 50 }", QUrl()); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QScopedPointer<QObject> o(component.create()); + QVERIFY(!o.isNull()); + MyItem *item = qobject_cast<MyItem *>(o.data()); + QVERIFY(item); + QCOMPARE(item->padding().left(), 50); + QCOMPARE(item->padding().right(), 50); + + QCOMPARE(Padding::log.length(), 3); + + // Created by default ctor of MyItem + QCOMPARE(Padding::log[0].type, Padding::CustomCtor); + QCOMPARE(Padding::log[0].left, 17); + QCOMPARE(Padding::log[0].right, 17); + + // Created by assignment of integer + QCOMPARE(Padding::log[1].type, Padding::InvokableCtor); + QCOMPARE(Padding::log[1].left, 50); + QCOMPARE(Padding::log[1].right, 50); + + // In MyItem::setPadding() + QCOMPARE(Padding::log[2].type, Padding::CopyAssign); + QCOMPARE(Padding::log[2].left, 50); + QCOMPARE(Padding::log[2].right, 50); + } + + { + const auto guard = qScopeGuard([]() { Padding::log.clear(); }); + QQmlComponent component(&e); + component.setData("import Test\nMyItem { padding: ({ left: 10, right: 20 }) }", QUrl()); + QVERIFY2(component.isReady(), qPrintable(component.errorString())); + QScopedPointer<QObject> o(component.create()); + QVERIFY(!o.isNull()); + MyItem *item = qobject_cast<MyItem *>(o.data()); + QVERIFY(item); + QCOMPARE(item->padding().left(), 10); + QCOMPARE(item->padding().right(), 20); + + QCOMPARE(Padding::log.length(), 3); + + // Created by default ctor of MyItem + QCOMPARE(Padding::log[0].type, Padding::CustomCtor); + QCOMPARE(Padding::log[0].left, 17); + QCOMPARE(Padding::log[0].right, 17); + + // Preparing for setting properties of structured value + QCOMPARE(Padding::log[1].type, Padding::DefaultCtor); + QCOMPARE(Padding::log[1].left, 0); + QCOMPARE(Padding::log[1].right, 0); + + // In MyItem::setPadding() + QCOMPARE(Padding::log[2].type, Padding::CopyAssign); + QCOMPARE(Padding::log[2].left, 10); + QCOMPARE(Padding::log[2].right, 20); + } +} + QTEST_MAIN(tst_qqmlvaluetypeproviders) #include "tst_qqmlvaluetypeproviders.moc" |