From 28e9a602cb35fc621dec36e28e8556ca0052551e Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Sat, 7 Jul 2012 05:43:11 +0200 Subject: [PATCH] statemachine: Emit finished() signal when the initial state is final It's legal to set a QFinalState as the initial state. The state machine should correctly emit the finished() signal upon entering such a state in the initial transition, and don't do any further processing. Change-Id: Ica8d3fadbbde604512ea1136624af54eb3b13b11 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/corelib/statemachine/qstatemachine.cpp | 13 ++++++++++++- .../qstatemachine/tst_qstatemachine.cpp | 12 ++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp index f1ca421de2..6412d7a0a9 100644 --- a/src/corelib/statemachine/qstatemachine.cpp +++ b/src/corelib/statemachine/qstatemachine.cpp @@ -1341,6 +1341,9 @@ void QStateMachinePrivate::_q_start() #ifndef QT_NO_ANIMATION QList selectedAnimations = selectAnimations(transitions); #endif + // enterStates() will set stopProcessingReason to Finished if a final + // state is entered. + stopProcessingReason = EventQueueEmpty; enterStates(&nullEvent, exitedStates, enteredStates, statesForDefaultEntry, assignmentsForEnteredStates #ifndef QT_NO_ANIMATION @@ -1355,7 +1358,15 @@ void QStateMachinePrivate::_q_start() emit q->started(); - _q_process(); + if (stopProcessingReason == Finished) { + // The state machine immediately reached a final state. + processingScheduled = false; + state = NotRunning; + unregisterAllTransitions(); + emit q->finished(); + } else { + _q_process(); + } } void QStateMachinePrivate::_q_process() diff --git a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp index b048f9c2f0..64f8d3aa9b 100644 --- a/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp @@ -193,6 +193,7 @@ private slots: void setPropertyAfterRestore(); void transitionWithNoTarget_data(); void transitionWithNoTarget(); + void initialStateIsFinal(); void restorePropertiesSimple(); void restoreProperties2(); @@ -4196,6 +4197,17 @@ void tst_QStateMachine::transitionWithNoTarget() delete object; } +void tst_QStateMachine::initialStateIsFinal() +{ + QStateMachine machine; + QFinalState *f = new QFinalState(&machine); + machine.setInitialState(f); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.start(); + QTRY_VERIFY(machine.configuration().contains(f)); + QTRY_COMPARE(finishedSpy.count(), 1); +} + class PropertyObject : public QObject { Q_OBJECT