diff options
author | Andrei Golubev <andrei.golubev@qt.io> | 2021-12-17 11:49:46 +0100 |
---|---|---|
committer | Andrei Golubev <andrei.golubev@qt.io> | 2021-12-23 15:56:16 +0100 |
commit | 71084db1df9453aa9c657b91f2dab6766a56903b (patch) | |
tree | f524bfb5397601f7b78ae525381eee21d2e6a2c1 /tests/auto/qml | |
parent | 2dc1080f5f14c0b15a40e210d17a403c7490a18e (diff) |
qmltc: Support different property change handlers
qmltc must be able to compile property change handlers:
- for bindable properties (with no notify)
- for notifiable properties (with no bindable)
- for both bindable and notifiable properties (preferring
notify signal)
Test the aforementioned cases and some signal handling cases
along the way
Task-number: QTBUG-84368
Pick-to: 6.3
Change-Id: I2cd2d0ad6407889942c806e03831dec4c7ce265a
Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qmltc/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/cpptypes/typewithproperties.cpp | 81 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/cpptypes/typewithproperties.h | 72 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/data/propertyChangeAndSignalHandlers.qml | 56 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/tst_qmltc.cpp | 66 | ||||
-rw-r--r-- | tests/auto/qml/qmltc/tst_qmltc.h | 1 |
6 files changed, 279 insertions, 0 deletions
diff --git a/tests/auto/qml/qmltc/CMakeLists.txt b/tests/auto/qml/qmltc/CMakeLists.txt index fc1d88e56a..f104b723a0 100644 --- a/tests/auto/qml/qmltc/CMakeLists.txt +++ b/tests/auto/qml/qmltc/CMakeLists.txt @@ -10,6 +10,8 @@ set(cpp_sources # private properties: cpptypes/testprivateproperty.h cpptypes/testprivateproperty.cpp cpptypes/private/testprivateproperty_p.h + + cpptypes/typewithproperties.h cpptypes/typewithproperties.cpp ) set(qml_sources @@ -64,6 +66,7 @@ set(qml_sources data/PrivateProperty.qml data/privatePropertySubclass.qml data/calqlatrBits.qml + data/propertyChangeAndSignalHandlers.qml # support types: data/DefaultPropertySingleChild.qml diff --git a/tests/auto/qml/qmltc/cpptypes/typewithproperties.cpp b/tests/auto/qml/qmltc/cpptypes/typewithproperties.cpp new file mode 100644 index 0000000000..fe4fc41020 --- /dev/null +++ b/tests/auto/qml/qmltc/cpptypes/typewithproperties.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "typewithproperties.h" + +double TypeWithProperties::a() const +{ + return m_a; +} +QString TypeWithProperties::b() const +{ + return m_b; +} +QVariant TypeWithProperties::c() const +{ + return m_c; +} +int TypeWithProperties::d() const +{ + return m_d; +} + +void TypeWithProperties::setA(double a_) +{ + m_a = a_; +} +void TypeWithProperties::setB(const QString &b_) +{ + if (m_b != b_) { + m_b = b_; + Q_EMIT bChanged(); + } +} +void TypeWithProperties::setC(const QVariant &c_) +{ + if (m_c != c_) { + m_c = c_; + Q_EMIT cWeirdSignal(c_); + } +} +void TypeWithProperties::setD(int d_) +{ + if (m_d != d_) { + m_d = d_; + Q_EMIT dSignal(u"d changed"_qs, d_); + } +} + +QBindable<double> TypeWithProperties::bindableA() +{ + return QBindable<double>(&m_a); +} +QBindable<int> TypeWithProperties::bindableD() +{ + return QBindable<int>(&m_d); +} diff --git a/tests/auto/qml/qmltc/cpptypes/typewithproperties.h b/tests/auto/qml/qmltc/cpptypes/typewithproperties.h new file mode 100644 index 0000000000..51d7eca015 --- /dev/null +++ b/tests/auto/qml/qmltc/cpptypes/typewithproperties.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/qobject.h> +#include <QtCore/qproperty.h> +#include <QtCore/qstring.h> +#include <QtCore/qvariant.h> +#include <QtQml/qqmlregistration.h> + +class TypeWithProperties : public QObject +{ + Q_OBJECT + QML_ELEMENT + + Q_PROPERTY(double a READ a WRITE setA BINDABLE bindableA) + Q_PROPERTY(QString b READ b WRITE setB NOTIFY bChanged) + Q_PROPERTY(QVariant c READ c WRITE setC NOTIFY cWeirdSignal) + Q_PROPERTY(int d READ d WRITE setD NOTIFY dSignal BINDABLE bindableD) + + QProperty<double> m_a { 0.0 }; + QString m_b; + QProperty<QVariant> m_c; + QProperty<int> m_d; + +public: + TypeWithProperties(QObject *parent = nullptr) : QObject(parent) { } + + double a() const; + QString b() const; + QVariant c() const; + int d() const; + + void setA(double); + void setB(const QString &); + void setC(const QVariant &); + void setD(int); + + QBindable<double> bindableA(); + QBindable<int> bindableD(); + +Q_SIGNALS: + void bChanged(); + // ### QTBUG-99317 + // void cWeirdSignal(const QVariant &); + void cWeirdSignal(QVariant); + void dSignal(QString, int); +}; diff --git a/tests/auto/qml/qmltc/data/propertyChangeAndSignalHandlers.qml b/tests/auto/qml/qmltc/data/propertyChangeAndSignalHandlers.qml new file mode 100644 index 0000000000..60f1b7a994 --- /dev/null +++ b/tests/auto/qml/qmltc/data/propertyChangeAndSignalHandlers.qml @@ -0,0 +1,56 @@ +import QtQuick +import QmltcTests 1.0 +Item { + function changeProperties1() { + one.a++; + one.b = one.b + "1"; + one.c = one.c + 1; + } + function changeProperties2() { two.c = two.c + 1; } + function changeProperties3(value: real) { three.c = value; } + + property int aChangedCount1: 0 + property int bChangedCount1: 0 + property int cChangedCount1: 0 + property int dChangedCount1: 0 + property int cChangedCount2: 0 + property int dChangedCount2: 0 + property int cChangedCount3: 0 + property int dChangedCount3: 0 + property string dChangedStr3: "" + property int cChangedCount4: 0 + property int dChangedCount4: 0 + property string dChangedStr4: "" + + TypeWithProperties { + id: one + onAChanged: { aChangedCount1++; } + onBChanged: { bChangedCount1++; } + onCChanged: { cChangedCount1++; } + onDChanged: { dChangedCount1++; } + } + + TypeWithProperties { + id: two + onCWeirdSignal: { cChangedCount2++; } + onDSignal: { dChangedCount2++; } + } + + TypeWithProperties { + id: three + onCChanged: function(value) { cChangedCount3 = value; } + onDChanged: function(str, value) { + dChangedStr3 = str; + dChangedCount3 = value; + } + } + + TypeWithProperties { + id: four + onCWeirdSignal: function(value) { cChangedCount4 = value + 1; } + onDSignal: function(str, value) { + dChangedStr4 = str + "!"; + dChangedCount4 = value / 2; + } + } +} diff --git a/tests/auto/qml/qmltc/tst_qmltc.cpp b/tests/auto/qml/qmltc/tst_qmltc.cpp index e93fd3bf24..a6b2b75843 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.cpp +++ b/tests/auto/qml/qmltc/tst_qmltc.cpp @@ -78,6 +78,7 @@ #include "keyevents.h" #include "privatepropertysubclass.h" #include "calqlatrbits.h" +#include "propertychangeandsignalhandlers.h" // Qt: #include <QtCore/qstring.h> @@ -1705,4 +1706,69 @@ void tst_qmltc::calqlatrBits() QTRY_VERIFY(scaleChangedSpy.count() > 0); } +void tst_qmltc::trickyPropertyChangeAndSignalHandlers() +{ + QQmlEngine e; + PREPEND_NAMESPACE(propertyChangeAndSignalHandlers) created(&e); + + // sanity + QCOMPARE(created.aChangedCount1(), 0); + QCOMPARE(created.bChangedCount1(), 0); + QCOMPARE(created.cChangedCount1(), 0); + QCOMPARE(created.dChangedCount1(), 0); + QCOMPARE(created.cChangedCount2(), 0); + QCOMPARE(created.dChangedCount2(), 0); + QCOMPARE(created.cChangedCount3(), 0); + QCOMPARE(created.dChangedCount3(), 0); + QCOMPARE(created.dChangedStr3(), QString()); + QCOMPARE(created.cChangedCount4(), 0); + QCOMPARE(created.dChangedCount4(), 0); + QCOMPARE(created.dChangedStr4(), QString()); + + QQmlContext *ctx = e.contextForObject(&created); + QVERIFY(ctx); + TypeWithProperties *one = qobject_cast<TypeWithProperties *>(ctx->objectForName("one")); + QVERIFY(one); + TypeWithProperties *two = qobject_cast<TypeWithProperties *>(ctx->objectForName("two")); + QVERIFY(two); + TypeWithProperties *three = qobject_cast<TypeWithProperties *>(ctx->objectForName("three")); + QVERIFY(three); + TypeWithProperties *four = qobject_cast<TypeWithProperties *>(ctx->objectForName("four")); + QVERIFY(four); + + one->setA(10); + QCOMPARE(created.aChangedCount1(), 1); + one->setB("1"); + QCOMPARE(created.bChangedCount1(), 1); + one->setC(2.5); + QCOMPARE(created.cChangedCount1(), 1); + two->setC(44.5); + QCOMPARE(created.cChangedCount2(), 1); + three->setC(42.0); + QCOMPARE(created.cChangedCount3(), 42); + three->setD(10); + QCOMPARE(created.dChangedCount3(), 10); + QCOMPARE(created.dChangedStr3(), u"d changed"_qs); + four->setC(1.5); + QCOMPARE(created.cChangedCount4(), 2); // cChangedCount4 is int, so 0.5 is truncated + four->setD(84); + // Note: due to signal-over-property-change-handler preference, we bind to + // signal in the case when the property is both bindable and notifiable. in + // this test, it would mean that we get proper dChanged*4 values intead of + // `undefined` junk + QCOMPARE(created.dChangedCount4(), 42); + QCOMPARE(created.dChangedStr4(), u"d changed!"_qs); + + created.changeProperties1(); + QCOMPARE(created.aChangedCount1(), 2); + QCOMPARE(created.bChangedCount1(), 2); + QCOMPARE(created.cChangedCount1(), 2); + + created.changeProperties2(); + QCOMPARE(created.cChangedCount2(), 2); + + created.changeProperties3(22); + QCOMPARE(created.cChangedCount3(), 22); +} + QTEST_MAIN(tst_qmltc) diff --git a/tests/auto/qml/qmltc/tst_qmltc.h b/tests/auto/qml/qmltc/tst_qmltc.h index 78a66cf8e5..be5ab2850e 100644 --- a/tests/auto/qml/qmltc/tst_qmltc.h +++ b/tests/auto/qml/qmltc/tst_qmltc.h @@ -93,4 +93,5 @@ private slots: void keyEvents(); void privateProperties(); void calqlatrBits(); // corner cases from calqlatr demo + void trickyPropertyChangeAndSignalHandlers(); }; |