diff options
author | Juha Vuolle <juha.vuolle@insta.fi> | 2021-03-11 10:49:32 +0200 |
---|---|---|
committer | Juha Vuolle <juha.vuolle@insta.fi> | 2021-05-03 13:52:14 +0300 |
commit | 26d9862c83d7f18eb4a23da342b6d3130494c4cb (patch) | |
tree | 10a28c4b0a4fca392083186e630cec9c6419689a /tests/auto | |
parent | d95938fc3356058b56de9297d915d38eab7e1dbf (diff) |
QtStateMachine QML-facing properties' bindable support part 5
This commit covers these QML-facing classes:
QAbstractTransition, QStateMachine
Task-number: QTBUG-91375
Change-Id: I3c7d4244a4113c6ea0b06fedde02d638fc558ccc
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/qstatemachine/qstatemachine/tst_qstatemachine.cpp | 36 | ||||
-rw-r--r-- | tests/auto/shared/bindableutils.h | 31 |
2 files changed, 55 insertions, 12 deletions
diff --git a/tests/auto/qstatemachine/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/qstatemachine/tst_qstatemachine.cpp index 0c1d4a8..374bdbe 100644 --- a/tests/auto/qstatemachine/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/qstatemachine/tst_qstatemachine.cpp @@ -6928,6 +6928,42 @@ void tst_QStateMachine::bindings() QByteArray arr2("arr2"); testWritableBindableBasics<QSignalTransition, QByteArray>( signalTransition, arr1, arr2, "signal"); + + // -- QAbstractTransition::transitionType + auto transitionType1 = QAbstractTransition::InternalTransition; + auto transitionType2 = QAbstractTransition::ExternalTransition; + testWritableBindableBasics<QAbstractTransition, QAbstractTransition::TransitionType>( + signalTransition, transitionType1, transitionType2, "transitionType"); + + // -- QStateMachine::errorString + QStateMachine brokenMachine; + brokenMachine.setErrorState(new QState(&machine)); // avoid warnings + QState* brokenState = new QState(&brokenMachine); + brokenMachine.setInitialState(brokenState); + new QState(brokenState); // make broken state a compound state that lacks the initial state + testReadableBindableBasics<QStateMachine, QString>( + brokenMachine, QString(), QStringLiteral("Missing initial state in compound state ''"), + "errorString", + [&](){ + brokenMachine.start(); + QTRY_VERIFY(brokenMachine.isRunning()); + }); + // test also clearing of the error above + testReadableBindableBasics<QStateMachine, QString>( + brokenMachine, QStringLiteral("Missing initial state in compound state ''"), QString(), + "errorString", + [&](){ brokenMachine.clearError(); }); + + // -- QStateMachine::globalRestorePolicy + testWritableBindableBasics<QStateMachine, QState::RestorePolicy>( + machine, QState::RestorePolicy::RestoreProperties, + QState::RestorePolicy::DontRestoreProperties, "globalRestorePolicy"); + +#if QT_CONFIG(animation) + // -- QStateMachine::animated + testWritableBindableBasics<QStateMachine, bool>( + machine, false, true, "animated"); +#endif } QTEST_MAIN(tst_QStateMachine) diff --git a/tests/auto/shared/bindableutils.h b/tests/auto/shared/bindableutils.h index 4a45f89..7c4f03f 100644 --- a/tests/auto/shared/bindableutils.h +++ b/tests/auto/shared/bindableutils.h @@ -31,6 +31,14 @@ #include <QtTest> #include <QObject> +#include <memory> + +#define DEFINE_CHANGE_SPY \ + std::unique_ptr<QSignalSpy> changeSpy(metaProperty.hasNotifySignal() ? new QSignalSpy(&testedClass, metaProperty.notifySignal()) : nullptr); + +#define VERIFY_CHANGE_COUNT(expected_count) \ + if (changeSpy) \ + QVERIFY2(changeSpy.get()->count() == expected_count, qPrintable(id + ", actual: " + QString::number(changeSpy.get()->count()))); // This is a helper function to test basics of typical bindable // properties that are read-only (but can change) Primarily ensure: @@ -61,10 +69,10 @@ void testReadableBindableBasics(TestedClass& testedClass, TestedData data0, Test id.append(propertyName); // Fail gracefully if preconditions to use this helper function are not met: - QVERIFY2(metaProperty.isBindable() && metaProperty.hasNotifySignal(), qPrintable(id)); + QVERIFY2(metaProperty.isBindable(), qPrintable(id)); QVERIFY2(modifierFunction, qPrintable(id)); - // Create a signal spy for the property changed -signal - QSignalSpy spy(&testedClass, metaProperty.notifySignal()); + // Create a signal spy for the property changed -signal (if the property has such signal) + DEFINE_CHANGE_SPY QUntypedBindable bindable = metaProperty.bindable(&testedClass); // Verify initial data is as expected @@ -79,7 +87,7 @@ void testReadableBindableBasics(TestedClass& testedClass, TestedData data0, Test modifierFunction(); QVERIFY2(data1Used == true, qPrintable(id)); QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1), qPrintable(id)); - QVERIFY2(spy.count() == 1, qPrintable(id + ", actual: " + QString::number(spy.count()))); + VERIFY_CHANGE_COUNT(1) } @@ -111,17 +119,16 @@ void testWritableBindableBasics(TestedClass& testedClass, TestedData data1, id.append(propertyName); // Fail gracefully if preconditions to use this helper function are not met: - QVERIFY2(metaProperty.isBindable() && metaProperty.isWritable() - && metaProperty.hasNotifySignal(), qPrintable(id)); - // Create a signal spy for the property changed -signal - QSignalSpy spy(&testedClass, metaProperty.notifySignal()); + QVERIFY2(metaProperty.isBindable() && metaProperty.isWritable(), qPrintable(id)); + // Create a signal change for the property changed -signal (if the property has such signal) + DEFINE_CHANGE_SPY QUntypedBindable bindable = metaProperty.bindable(&testedClass); // Test basic property read and write testedClass.setProperty(propertyName, QVariant::fromValue(data1)); QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1), qPrintable(id)); - QVERIFY2(spy.count() == 1, qPrintable(id + ", actual: " + QString::number(spy.count()))); + VERIFY_CHANGE_COUNT(1) // Test setting a binding as a source for the property QProperty<TestedData> property1(data1); @@ -131,20 +138,20 @@ void testWritableBindableBasics(TestedClass& testedClass, TestedData data1, QVERIFY2(bindable.hasBinding(), qPrintable(id)); // Check that the value also changed QVERIFY2(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data2), qPrintable(id)); - QVERIFY2(spy.count() == 2, qPrintable(id + ", actual: " + QString::number(spy.count()))); + VERIFY_CHANGE_COUNT(2) // 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(dataComparator(testedClass.property(propertyName).template value<TestedData>(), data1), qPrintable(id)); - QVERIFY2(spy.count() == 3, qPrintable(id + ", actual: " + QString::number(spy.count()))); + VERIFY_CHANGE_COUNT(3) // Remove binding by setting a value directly QVERIFY2(bindable.hasBinding(), qPrintable(id)); testedClass.setProperty(propertyName, QVariant::fromValue(data2)); 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()))); + VERIFY_CHANGE_COUNT(4) // Test using the property as the source in a binding QProperty<bool> data1Used([&](){ |