diff options
author | Juha Vuolle <juha.vuolle@insta.fi> | 2021-03-01 14:41:06 +0200 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@insta.fi> | 2021-05-03 13:51:56 +0300 |
commit | 28c1702e69800dc89748c184d449b775a049968f (patch) | |
tree | 34a849d89ac7d09359aa4bdd4d8a6f06f5adb0b8 /tests | |
parent | 00f3ec2b5c11aa399d72f8daaf0a45150905123e (diff) |
QtStateMachine QML-facing properties' bindable support part 2
This commit covers these QML-facing classes:
SignalTransition
Task-number: QTBUG-91375
Change-Id: I58bca7b4599b420aa99492233f9d88ea4af7e877
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/qml/qqmlstatemachine/CMakeLists.txt | 10 | ||||
-rw-r--r-- | tests/auto/qml/qqmlstatemachine/data/signaltransition.qml | 51 | ||||
-rw-r--r-- | tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp | 33 | ||||
-rw-r--r-- | tests/auto/shared/bindableutils.h | 14 |
4 files changed, 101 insertions, 7 deletions
diff --git a/tests/auto/qml/qqmlstatemachine/CMakeLists.txt b/tests/auto/qml/qqmlstatemachine/CMakeLists.txt index 5772a55..fd14ad7 100644 --- a/tests/auto/qml/qqmlstatemachine/CMakeLists.txt +++ b/tests/auto/qml/qqmlstatemachine/CMakeLists.txt @@ -9,11 +9,12 @@ qt_internal_add_test(tst_qqmlstatemachine tst_qqmlstatemachine.cpp INCLUDE_DIRECTORIES ../../shared - PUBLIC_LIBRARIES + LIBRARIES Qt::CorePrivate Qt::Gui Qt::GuiPrivate Qt::QmlPrivate + Qt::StateMachineQmlPrivate ) ## Scopes: @@ -28,3 +29,10 @@ qt_extend_target(tst_qqmlstatemachine CONDITION NOT ANDROID AND NOT IOS DEFINES QT_QMLTEST_DATADIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}/data\\\" ) + +qt_internal_add_resource(tst_qqmlstatemachine "tst_qqmlstatemachine" + PREFIX + "/" + FILES + "data/signaltransition.qml" +) diff --git a/tests/auto/qml/qqmlstatemachine/data/signaltransition.qml b/tests/auto/qml/qqmlstatemachine/data/signaltransition.qml new file mode 100644 index 0000000..52c62a7 --- /dev/null +++ b/tests/auto/qml/qqmlstatemachine/data/signaltransition.qml @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2021 The Qt Company +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite module 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$ +** +****************************************************************************/ + +import QtQuick +import QtQml.StateMachine + +Item { + id: root + + signal signal1() + signal signal2() + + function getSignal1() { return root.signal1 } + function getSignal2() { return root.signal2 } + + SignalTransition { + objectName: "st1" + guard: 1 + 1 + } + + SignalTransition { + objectName: "st2" + guard: 2 + 2 + } + +} diff --git a/tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp b/tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp index a6f0d65..80d75a8 100644 --- a/tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp +++ b/tests/auto/qml/qqmlstatemachine/tst_qqmlstatemachine.cpp @@ -28,8 +28,13 @@ #include <QQmlComponent> #include <QQmlContext> #include <QQmlEngine> +#include <QtQuick/QQuickItem> +#include <QtStateMachineQml/private/signaltransition_p.h> +#include <QtQml/qqmlscriptstring.h> + #include <QTest> #include "../../shared/util.h" +#include "../../shared/bindableutils.h" class tst_qqmlstatemachine : public QQmlDataTest { @@ -39,6 +44,7 @@ public: private slots: void tst_cppObjectSignal(); + void tst_bindings(); }; @@ -104,6 +110,33 @@ void tst_qqmlstatemachine::tst_cppObjectSignal() QTRY_VERIFY(!rootObject->property("running").toBool()); } +void tst_qqmlstatemachine::tst_bindings() +{ + // -- SignalTransition::guard + SignalTransition signalTransition; + // Generating QQmlScriptString requires proper qml context setup, and here we + // use same the element that we are testing to create the testing material + QQmlEngine engine; + QQmlComponent component(&engine, QUrl(QLatin1String("qrc:///data/signaltransition.qml"))); + + std::unique_ptr<QObject> obj(component.create()); + SignalTransition *st1 = qobject_cast<SignalTransition*>(obj->findChild<QObject*>("st1")); + SignalTransition *st2 = qobject_cast<SignalTransition*>(obj->findChild<QObject*>("st2")); + QVERIFY(st1 && st2 && (st1->guard() != st2->guard())); + testWritableBindableBasics<SignalTransition, QQmlScriptString>( + signalTransition, st1->guard(), st2->guard(), "guard"); + + // -- SignalTransition::signal + // We use QML to create the test material (QJSValues that contain valid methods) + QVariant signal1; + QVariant signal2; + QMetaObject::invokeMethod(obj.get(), "getSignal1", Q_RETURN_ARG(QVariant, signal1)); + QMetaObject::invokeMethod(obj.get(), "getSignal2", Q_RETURN_ARG(QVariant, signal2)); + // QJSValue does not implement operator== so we supply own comparator + testWritableBindableBasics<SignalTransition, QJSValue>( + *st1, signal1.value<QJSValue>(), signal2.value<QJSValue>(), "signal", + [](QJSValue d1, QJSValue d2) { return d1.strictlyEquals(d2); }); +} QTEST_MAIN(tst_qqmlstatemachine) diff --git a/tests/auto/shared/bindableutils.h b/tests/auto/shared/bindableutils.h index 3e93752..6e9f657 100644 --- a/tests/auto/shared/bindableutils.h +++ b/tests/auto/shared/bindableutils.h @@ -46,7 +46,8 @@ // "propertyName" is the name of the property we are interested in testing template<typename TestedClass, typename TestedData> void testWritableBindableBasics(TestedClass& testedClass, TestedData data1, - TestedData data2, const char* propertyName) + TestedData data2, const char* propertyName, + std::function<bool(TestedData,TestedData)> dataComparator = [](TestedData d1, TestedData d2) { return d1 == d2; }) { // Get the property we are testing const QMetaObject *metaObject = testedClass.metaObject(); @@ -66,7 +67,8 @@ void testWritableBindableBasics(TestedClass& testedClass, TestedData data1, // Test basic property read and write testedClass.setProperty(propertyName, QVariant::fromValue(data1)); - QVERIFY2(testedClass.property(propertyName).template value<TestedData>() == data1, qPrintable(id)); + + QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1), qPrintable(id)); QVERIFY2(spy.count() == 1, qPrintable(id + ", actual: " + QString::number(spy.count()))); // Test setting a binding as a source for the property @@ -76,25 +78,25 @@ void testWritableBindableBasics(TestedClass& testedClass, TestedData data1, bindable.setBinding(Qt::makePropertyBinding(property2)); QVERIFY2(bindable.hasBinding(), qPrintable(id)); // Check that the value also changed - QVERIFY2(testedClass.property(propertyName).template value<TestedData>() == data2, qPrintable(id)); + QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data2), qPrintable(id)); QVERIFY2(spy.count() == 2, qPrintable(id + ", actual: " + QString::number(spy.count()))); // Same test but with a lambda binding (cast to be able to set the lambda directly) QBindable<TestedData> *typedBindable = static_cast<QBindable<TestedData>*>(&bindable); typedBindable->setBinding([&](){ return property1.value(); }); QVERIFY2(typedBindable->hasBinding(), qPrintable(id)); - QVERIFY2(testedClass.property(propertyName).template value<TestedData>() == data1, qPrintable(id)); + QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1), qPrintable(id)); QVERIFY2(spy.count() == 3, qPrintable(id + ", actual: " + QString::number(spy.count()))); // Remove binding by setting a value directly QVERIFY2(bindable.hasBinding(), qPrintable(id)); testedClass.setProperty(propertyName, QVariant::fromValue(data2)); - QVERIFY2(testedClass.property(propertyName).template value<TestedData>() == data2, qPrintable(id)); + QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data2), qPrintable(id)); QVERIFY2(!bindable.hasBinding(), qPrintable(id)); QVERIFY2(spy.count() == 4, qPrintable(id + ", actual: " + QString::number(spy.count()))); // Test using the property as the source in a binding QProperty<bool> data1Used([&](){ - return testedClass.property(propertyName).template value<TestedData>() == data1; + return dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1); }); QVERIFY2(data1Used == false, qPrintable(id)); testedClass.setProperty(propertyName, QVariant::fromValue(data1)); |