diff options
author | Kent Hansen <kent.hansen@nokia.com> | 2011-01-26 11:01:19 (GMT) |
---|---|---|
committer | Kent Hansen <kent.hansen@nokia.com> | 2011-01-26 11:01:19 (GMT) |
commit | 1c79a42d6b0f6c15a0472e0cde3742d0c504ead3 (patch) | |
tree | 793c6b796de08f62bd0bbc1b372b12fda5f85b93 | |
parent | e881b19ba3f2f4bfda460e1a043f461fb0517d70 (diff) | |
download | Qt-1c79a42d6b0f6c15a0472e0cde3742d0c504ead3.zip Qt-1c79a42d6b0f6c15a0472e0cde3742d0c504ead3.tar.gz Qt-1c79a42d6b0f6c15a0472e0cde3742d0c504ead3.tar.bz2 |
Make sure QStateMachine stops when it's told to
If QStateMachine::stop() was called by an event test or while
transitioning to another state, the state machine could end up
in an inconsistent internal state (stop==true, but the stopped()
signal was not emitted). This also caused the machine to behave
incorrectly if it was then restarted (it would immediately stop
upon receiving the first event).
Solution: Move the stop-handling after the event processing loop,
so that it always takes precedence over other possible reasons
for exiting the loop (event queues exhausted, final state
entered).
Task-number: QTBUG-16463
Reviewed-by: Eskil Abrahamsen Blomfeldt
-rw-r--r-- | src/corelib/statemachine/qstatemachine.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qstatemachine/tst_qstatemachine.cpp | 68 |
2 files changed, 73 insertions, 2 deletions
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp index 392c45d..b2af5ca 100644 --- a/src/corelib/statemachine/qstatemachine.cpp +++ b/src/corelib/statemachine/qstatemachine.cpp @@ -1245,9 +1245,7 @@ void QStateMachinePrivate::_q_process() #endif while (processing) { if (stop) { - stop = false; processing = false; - stopProcessingReason = Stopped; break; } QSet<QAbstractTransition*> enabledTransitions; @@ -1299,6 +1297,11 @@ void QStateMachinePrivate::_q_process() #ifdef QSTATEMACHINE_DEBUG qDebug() << q << ": finished the event processing loop"; #endif + if (stop) { + stop = false; + stopProcessingReason = Stopped; + } + switch (stopProcessingReason) { case EventQueueEmpty: break; diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp index a03657f..231cab0 100644 --- a/tests/auto/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -211,6 +211,9 @@ private slots: void postEventFromOtherThread(); void eventFilterForApplication(); void eventClassesExported(); + void stopInTransitionToFinalState(); + void stopInEventTest_data(); + void stopInEventTest(); }; tst_QStateMachine::tst_QStateMachine() @@ -4374,5 +4377,70 @@ void tst_QStateMachine::eventClassesExported() QStateMachine::SignalEvent *signalEvent = new QStateMachine::SignalEvent(0, 0, QList<QVariant>()); } +void tst_QStateMachine::stopInTransitionToFinalState() +{ + QStateMachine machine; + QState *s1 = new QState(&machine); + QFinalState *s2 = new QFinalState(&machine); + QAbstractTransition *t1 = s1->addTransition(s2); + machine.setInitialState(s1); + + QObject::connect(t1, SIGNAL(triggered()), &machine, SLOT(stop())); + QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + QSignalSpy s2EnteredSpy(s2, SIGNAL(entered())); + machine.start(); + + // Stopping should take precedence over finished. + QTRY_COMPARE(stoppedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(s2EnteredSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); +} + +class StopInEventTestTransition : public QAbstractTransition +{ +public: + bool eventTest(QEvent *e) + { + if (e->type() == QEvent::User) + machine()->stop(); + return false; + } + void onTransition(QEvent *) + { } +}; + +void tst_QStateMachine::stopInEventTest_data() +{ + QTest::addColumn<int>("eventPriority"); + QTest::newRow("NormalPriority") << int(QStateMachine::NormalPriority); + QTest::newRow("HighPriority") << int(QStateMachine::HighPriority); +} + +void tst_QStateMachine::stopInEventTest() +{ + QFETCH(int, eventPriority); + + QStateMachine machine; + QState *s1 = new QState(&machine); + s1->addTransition(new StopInEventTestTransition()); + machine.setInitialState(s1); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + + QSignalSpy stoppedSpy(&machine, SIGNAL(stopped())); + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + machine.postEvent(new QEvent(QEvent::User), QStateMachine::EventPriority(eventPriority)); + + QTRY_COMPARE(stoppedSpy.count(), 1); + QCOMPARE(finishedSpy.count(), 0); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); +} + QTEST_MAIN(tst_QStateMachine) #include "tst_qstatemachine.moc" |