Export QStateMachine running property.

It is needed to control a QStateMachine object from QML.

Change-Id: I19271d97718af2d688c477647d6341f70fdef3ea
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
This commit is contained in:
BogDan Vatra 2014-06-24 13:09:59 +03:00 committed by BogDan Vatra
parent d8062f117b
commit 6302d6eef7
3 changed files with 311 additions and 4 deletions

View File

@ -163,6 +163,13 @@ QT_BEGIN_NAMESPACE
QState::DontRestoreProperties. QState::DontRestoreProperties.
*/ */
/*!
\property QStateMachine::running
\since 5.4
\brief the running state of this state machine
*/
#ifndef QT_NO_ANIMATION #ifndef QT_NO_ANIMATION
/*! /*!
\property QStateMachine::animated \property QStateMachine::animated
@ -1409,6 +1416,7 @@ void QStateMachinePrivate::_q_start()
#endif #endif
emit q->started(QStateMachine::QPrivateSignal()); emit q->started(QStateMachine::QPrivateSignal());
emit q->runningChanged(true);
if (stopProcessingReason == Finished) { if (stopProcessingReason == Finished) {
// The state machine immediately reached a final state. // The state machine immediately reached a final state.
@ -1416,6 +1424,7 @@ void QStateMachinePrivate::_q_start()
state = NotRunning; state = NotRunning;
unregisterAllTransitions(); unregisterAllTransitions();
emitFinished(); emitFinished();
emit q->runningChanged(false);
} else { } else {
_q_process(); _q_process();
} }
@ -1498,12 +1507,14 @@ void QStateMachinePrivate::_q_process()
cancelAllDelayedEvents(); cancelAllDelayedEvents();
unregisterAllTransitions(); unregisterAllTransitions();
emitFinished(); emitFinished();
emit q->runningChanged(false);
break; break;
case Stopped: case Stopped:
state = NotRunning; state = NotRunning;
cancelAllDelayedEvents(); cancelAllDelayedEvents();
unregisterAllTransitions(); unregisterAllTransitions();
emit q->stopped(QStateMachine::QPrivateSignal()); emit q->stopped(QStateMachine::QPrivateSignal());
emit q->runningChanged(false);
break; break;
} }
} }
@ -2117,7 +2128,7 @@ bool QStateMachine::isRunning() const
the main application event loop started with QCoreApplication::exec() or the main application event loop started with QCoreApplication::exec() or
QApplication::exec(). QApplication::exec().
\sa started(), finished(), stop(), initialState() \sa started(), finished(), stop(), initialState(), setRunning()
*/ */
void QStateMachine::start() void QStateMachine::start()
{ {
@ -2145,7 +2156,7 @@ void QStateMachine::start()
Stops this state machine. The state machine will stop processing events and Stops this state machine. The state machine will stop processing events and
then emit the stopped() signal. then emit the stopped() signal.
\sa stopped(), start() \sa stopped(), start(), setRunning()
*/ */
void QStateMachine::stop() void QStateMachine::stop()
{ {
@ -2164,6 +2175,19 @@ void QStateMachine::stop()
} }
} }
/*!
Convenience functions to start/stop this state machine.
\sa start(), stop(), started(), finished(), stopped()
*/
void QStateMachine::setRunning(bool running)
{
if (running)
start();
else
stop();
}
/*! /*!
\threadsafe \threadsafe
@ -2712,6 +2736,15 @@ QStateMachine::WrappedEvent::~WrappedEvent()
Returns a clone of the original event. Returns a clone of the original event.
*/ */
/*!
\fn QStateMachine::runningChanged(bool running)
\since 5.4
This signal is emitted when the running property is changed.
\sa QStateMachine::running
*/
QT_END_NAMESPACE QT_END_NAMESPACE
#include "qstatemachine.moc" #include "qstatemachine.moc"

View File

@ -62,6 +62,7 @@ class Q_CORE_EXPORT QStateMachine : public QState
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString errorString READ errorString) Q_PROPERTY(QString errorString READ errorString)
Q_PROPERTY(QState::RestorePolicy globalRestorePolicy READ globalRestorePolicy WRITE setGlobalRestorePolicy) Q_PROPERTY(QState::RestorePolicy globalRestorePolicy READ globalRestorePolicy WRITE setGlobalRestorePolicy)
Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
#ifndef QT_NO_ANIMATION #ifndef QT_NO_ANIMATION
Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated) Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated)
#endif #endif
@ -149,6 +150,7 @@ public:
public Q_SLOTS: public Q_SLOTS:
void start(); void start();
void stop(); void stop();
void setRunning(bool running);
Q_SIGNALS: Q_SIGNALS:
void started( void started(
@ -161,6 +163,8 @@ Q_SIGNALS:
QPrivateSignal QPrivateSignal
#endif #endif
); );
void runningChanged(bool running);
protected: protected:
void onEntry(QEvent *event); void onEntry(QEvent *event);

View File

@ -71,6 +71,27 @@ static int globalTick;
QCoreApplication::exec(); \ QCoreApplication::exec(); \
} }
#define TEST_RUNNING_CHANGED(RUNNING) \
{ \
QTRY_COMPARE(runningSpy.count(), 1); \
QList<QVariant> runningArgs = runningSpy.takeFirst(); \
QVERIFY(runningArgs.at(0).type() == QVariant::Bool); \
QVERIFY(runningArgs.at(0).toBool() == RUNNING); \
QCOMPARE(machine.isRunning(), runningArgs.at(0).toBool()); \
}
#define TEST_RUNNING_CHANGED_STARTED_STOPPED \
{ \
QTRY_COMPARE(runningSpy.count(), 2); \
QList<QVariant> runningArgs = runningSpy.takeFirst(); \
QVERIFY(runningArgs.at(0).type() == QVariant::Bool); \
QVERIFY(runningArgs.at(0).toBool() == true); \
runningArgs = runningSpy.takeFirst(); \
QVERIFY(runningArgs.at(0).type() == QVariant::Bool); \
QVERIFY(runningArgs.at(0).toBool() == false); \
QCOMPARE(machine.isRunning(), runningArgs.at(0).toBool()); \
}
class SignalEmitter : public QObject class SignalEmitter : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -120,6 +141,7 @@ private slots:
#endif #endif
void historyStates(); void historyStates();
void startAndStop(); void startAndStop();
void setRunning();
void targetStateWithNoParent(); void targetStateWithNoParent();
void targetStateDeleted(); void targetStateDeleted();
void transitionToRootState(); void transitionToRootState();
@ -916,10 +938,13 @@ void tst_QStateMachine::historyStateAfterRestart()
s2->addTransition(new EventTransition(QEvent::User, s1)); s2->addTransition(new EventTransition(QEvent::User, s1));
for (int x = 0; x < 2; ++x) { for (int x = 0; x < 2; ++x) {
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().count(), 1); QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -956,6 +981,7 @@ void tst_QStateMachine::historyStateAfterRestart()
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
machine.stop(); machine.stop();
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
} }
@ -1235,10 +1261,12 @@ void tst_QStateMachine::stateEntryAndExit()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.setInitialState(s1); machine.setInitialState(s1);
QCOMPARE(machine.initialState(), (QAbstractState*)s1); QCOMPARE(machine.initialState(), (QAbstractState*)s1);
@ -1269,6 +1297,7 @@ void tst_QStateMachine::stateEntryAndExit()
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
QTRY_COMPARE(stoppedSpy.count(), 0); QTRY_COMPARE(stoppedSpy.count(), 0);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().count(), 1); QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s3)); QVERIFY(machine.configuration().contains(s3));
@ -1314,14 +1343,17 @@ void tst_QStateMachine::stateEntryAndExit()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.setInitialState(s1); machine.setInitialState(s1);
globalTick = 0; globalTick = 0;
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().count(), 1); QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s3)); QVERIFY(machine.configuration().contains(s3));
@ -1444,10 +1476,13 @@ void tst_QStateMachine::assignPropertyWithAnimation()
s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3); s2->addTransition(s2, SIGNAL(propertiesAssigned()), s3);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(obj.property("foo").toInt(), 456); QCOMPARE(obj.property("foo").toInt(), 456);
QCOMPARE(obj.property("bar").toInt(), 789); QCOMPARE(obj.property("bar").toInt(), 789);
} }
@ -1474,9 +1509,12 @@ void tst_QStateMachine::assignPropertyWithAnimation()
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(obj.property("foo").toInt(), 456); QCOMPARE(obj.property("foo").toInt(), 456);
QCOMPARE(obj.property("bar").toInt(), 789); QCOMPARE(obj.property("bar").toInt(), 789);
} }
@ -1503,9 +1541,12 @@ void tst_QStateMachine::assignPropertyWithAnimation()
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(obj.property("foo").toInt(), 456); QCOMPARE(obj.property("foo").toInt(), 456);
QCOMPARE(obj.property("bar").toInt(), 654); QCOMPARE(obj.property("bar").toInt(), 654);
QCOMPARE(obj.property("baz").toInt(), 789); QCOMPARE(obj.property("baz").toInt(), 789);
@ -1553,9 +1594,12 @@ void tst_QStateMachine::assignPropertyWithAnimation()
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(obj.property("foo").toInt(), 321); QCOMPARE(obj.property("foo").toInt(), 321);
QCOMPARE(obj.property("bar").toInt(), 789); QCOMPARE(obj.property("bar").toInt(), 789);
} }
@ -1676,9 +1720,12 @@ void tst_QStateMachine::postEvent()
machine.addState(s2); machine.addState(s2);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
@ -1706,9 +1753,12 @@ void tst_QStateMachine::cancelDelayedEvent()
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -1728,6 +1778,7 @@ void tst_QStateMachine::cancelDelayedEvent()
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
} }
@ -1740,10 +1791,13 @@ void tst_QStateMachine::postDelayedEventAndStop()
s1->addTransition(new StringTransition("a", s2)); s1->addTransition(new StringTransition("a", s2));
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -1753,11 +1807,13 @@ void tst_QStateMachine::postDelayedEventAndStop()
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
machine.stop(); machine.stop();
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 2); QTRY_COMPARE(startedSpy.count(), 2);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -1765,8 +1821,10 @@ void tst_QStateMachine::postDelayedEventAndStop()
QVERIFY(id2 != -1); QVERIFY(id2 != -1);
machine.stop(); machine.stop();
QTRY_COMPARE(stoppedSpy.count(), 2); QTRY_COMPARE(stoppedSpy.count(), 2);
TEST_RUNNING_CHANGED(false);
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 3); QTRY_COMPARE(startedSpy.count(), 3);
TEST_RUNNING_CHANGED(true);
QTestEventLoop::instance().enterLoop(2); QTestEventLoop::instance().enterLoop(2);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -1813,11 +1871,13 @@ void tst_QStateMachine::postDelayedEventFromThread()
DelayedEventPosterThread poster(&machine); DelayedEventPosterThread poster(&machine);
poster.start(); poster.start();
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QVERIFY(poster.firstEventWasCancelled); QVERIFY(poster.firstEventWasCancelled);
} }
@ -1826,16 +1886,20 @@ void tst_QStateMachine::stopAndPostEvent()
QStateMachine machine; QStateMachine machine;
QState *s1 = new QState(&machine); QState *s1 = new QState(&machine);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
machine.stop(); machine.stop();
QCOMPARE(stoppedSpy.count(), 0); QCOMPARE(stoppedSpy.count(), 0);
machine.postEvent(new QEvent(QEvent::User)); machine.postEvent(new QEvent(QEvent::User));
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
} }
@ -1850,10 +1914,13 @@ void tst_QStateMachine::stateFinished()
QFinalState *s2 = new QFinalState(&machine); QFinalState *s2 = new QFinalState(&machine);
s1->addTransition(s1, SIGNAL(finished()), s2); s1->addTransition(s1, SIGNAL(finished()), s2);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
} }
@ -1888,11 +1955,14 @@ void tst_QStateMachine::parallelStates()
s1->addTransition(s1, SIGNAL(finished()), s2); s1->addTransition(s1, SIGNAL(finished()), s2);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
globalTick = 0; globalTick = 0;
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
@ -1933,6 +2003,8 @@ void tst_QStateMachine::parallelRootState()
QFinalState *s2_f = new QFinalState(s2); QFinalState *s2_f = new QFinalState(s2);
s2->setInitialState(s2_f); s2->setInitialState(s2_f);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
@ -1945,6 +2017,7 @@ void tst_QStateMachine::parallelRootState()
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
QVERIFY(machine.configuration().contains(s2_f)); QVERIFY(machine.configuration().contains(s2_f));
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
void tst_QStateMachine::allSourceToTargetConfigurations() void tst_QStateMachine::allSourceToTargetConfigurations()
@ -1984,6 +2057,8 @@ void tst_QStateMachine::allSourceToTargetConfigurations()
s2->addTransition(new StringTransition("f", s11)); s2->addTransition(new StringTransition("f", s11));
s0->addTransition(new StringTransition("e", s211)); s0->addTransition(new StringTransition("e", s211));
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2008,6 +2083,7 @@ void tst_QStateMachine::allSourceToTargetConfigurations()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
class TestSignalTransition : public QSignalTransition class TestSignalTransition : public QSignalTransition
@ -2094,6 +2170,8 @@ void tst_QStateMachine::signalTransitions()
QCOMPARE(trans->senderObject(), (QObject*)&emitter); QCOMPARE(trans->senderObject(), (QObject*)&emitter);
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg())));
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2103,6 +2181,7 @@ void tst_QStateMachine::signalTransitions()
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
@ -2112,6 +2191,7 @@ void tst_QStateMachine::signalTransitions()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
emitter.emitSignalWithIntArg(123); emitter.emitSignalWithIntArg(123);
QTRY_COMPARE(finishedSpy.count(), 2); QTRY_COMPARE(finishedSpy.count(), 2);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
@ -2119,6 +2199,7 @@ void tst_QStateMachine::signalTransitions()
QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg()))); QCOMPARE(trans->signal(), QByteArray(SIGNAL(signalWithNoArg())));
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 3); QTRY_COMPARE(finishedSpy.count(), 3);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
SignalEmitter emitter2; SignalEmitter emitter2;
machine.start(); machine.start();
@ -2126,12 +2207,14 @@ void tst_QStateMachine::signalTransitions()
trans->setSenderObject(&emitter2); trans->setSenderObject(&emitter2);
emitter2.emitSignalWithNoArg(); emitter2.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 4); QTRY_COMPARE(finishedSpy.count(), 4);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTest::ignoreMessage(QtWarningMsg, "QSignalTransition: no such signal: SignalEmitter::noSuchSignal()"); QTest::ignoreMessage(QtWarningMsg, "QSignalTransition: no such signal: SignalEmitter::noSuchSignal()");
trans->setSignal(SIGNAL(noSuchSignal())); trans->setSignal(SIGNAL(noSuchSignal()));
QCOMPARE(trans->signal(), QByteArray(SIGNAL(noSuchSignal()))); QCOMPARE(trans->signal(), QByteArray(SIGNAL(noSuchSignal())));
TEST_RUNNING_CHANGED(true);
} }
{ {
QStateMachine machine; QStateMachine machine;
@ -2145,6 +2228,8 @@ void tst_QStateMachine::signalTransitions()
QCOMPARE(trans->senderObject(), (QObject*)&emitter); QCOMPARE(trans->senderObject(), (QObject*)&emitter);
QCOMPARE(trans->signal(), QByteArray("signalWithNoArg()")); QCOMPARE(trans->signal(), QByteArray("signalWithNoArg()"));
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2154,6 +2239,7 @@ void tst_QStateMachine::signalTransitions()
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
trans->setSignal("signalWithIntArg(int)"); trans->setSignal("signalWithIntArg(int)");
QCOMPARE(trans->signal(), QByteArray("signalWithIntArg(int)")); QCOMPARE(trans->signal(), QByteArray("signalWithIntArg(int)"));
@ -2161,6 +2247,7 @@ void tst_QStateMachine::signalTransitions()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
emitter.emitSignalWithIntArg(123); emitter.emitSignalWithIntArg(123);
QTRY_COMPARE(finishedSpy.count(), 2); QTRY_COMPARE(finishedSpy.count(), 2);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
{ {
QStateMachine machine; QStateMachine machine;
@ -2170,6 +2257,8 @@ void tst_QStateMachine::signalTransitions()
TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithIntArg(int)), s1); TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithIntArg(int)), s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2179,6 +2268,7 @@ void tst_QStateMachine::signalTransitions()
emitter.emitSignalWithIntArg(123); emitter.emitSignalWithIntArg(123);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(trans->eventTestSenderReceived(), (QObject*)&emitter); QCOMPARE(trans->eventTestSenderReceived(), (QObject*)&emitter);
QCOMPARE(trans->eventTestSignalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithIntArg(int)")); QCOMPARE(trans->eventTestSignalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithIntArg(int)"));
QCOMPARE(trans->eventTestArgumentsReceived().size(), 1); QCOMPARE(trans->eventTestArgumentsReceived().size(), 1);
@ -2196,6 +2286,8 @@ void tst_QStateMachine::signalTransitions()
TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s1); TestSignalTransition *trans = new TestSignalTransition(&emitter, SIGNAL(signalWithStringArg(QString)), s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2206,6 +2298,7 @@ void tst_QStateMachine::signalTransitions()
emitter.emitSignalWithStringArg(testString); emitter.emitSignalWithStringArg(testString);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(trans->eventTestSenderReceived(), (QObject*)&emitter); QCOMPARE(trans->eventTestSenderReceived(), (QObject*)&emitter);
QCOMPARE(trans->eventTestSignalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithStringArg(QString)")); QCOMPARE(trans->eventTestSignalIndexReceived(), emitter.metaObject()->indexOfSignal("signalWithStringArg(QString)"));
QCOMPARE(trans->eventTestArgumentsReceived().size(), 1); QCOMPARE(trans->eventTestArgumentsReceived().size(), 1);
@ -2232,6 +2325,8 @@ void tst_QStateMachine::signalTransitions()
trans->setTargetState(s1); trans->setTargetState(s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2241,6 +2336,7 @@ void tst_QStateMachine::signalTransitions()
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
// Multiple transitions for same (object,signal) // Multiple transitions for same (object,signal)
{ {
@ -2302,28 +2398,36 @@ void tst_QStateMachine::signalTransitions()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 2); QTRY_COMPARE(startedSpy.count(), 2);
TEST_RUNNING_CHANGED(true);
emitter.emitSignalWithIntArg(123); emitter.emitSignalWithIntArg(123);
QTRY_COMPARE(finishedSpy.count(), 2); QTRY_COMPARE(finishedSpy.count(), 2);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 3); QTRY_COMPARE(startedSpy.count(), 3);
TEST_RUNNING_CHANGED(true);
emitter.emitSignalWithStringArg("hello"); emitter.emitSignalWithStringArg("hello");
QTRY_COMPARE(finishedSpy.count(), 3); QTRY_COMPARE(finishedSpy.count(), 3);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s3)); QVERIFY(machine.configuration().contains(s3));
} }
@ -2343,16 +2447,20 @@ void tst_QStateMachine::signalTransitions()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(true);
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
} }
@ -2406,6 +2514,8 @@ void tst_QStateMachine::eventTransitions()
QCOMPARE(trans->targetState(), (QAbstractState*)s1); QCOMPARE(trans->targetState(), (QAbstractState*)s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2414,6 +2524,7 @@ void tst_QStateMachine::eventTransitions()
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
@ -2423,12 +2534,14 @@ void tst_QStateMachine::eventTransitions()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTest::mouseRelease(&button, Qt::LeftButton); QTest::mouseRelease(&button, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 2); QTRY_COMPARE(finishedSpy.count(), 2);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
trans->setEventType(QEvent::MouseButtonPress); trans->setEventType(QEvent::MouseButtonPress);
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 3); QTRY_COMPARE(finishedSpy.count(), 3);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QPushButton button2; QPushButton button2;
machine.start(); machine.start();
@ -2436,6 +2549,7 @@ void tst_QStateMachine::eventTransitions()
trans->setEventSource(&button2); trans->setEventSource(&button2);
QTest::mousePress(&button2, Qt::LeftButton); QTest::mousePress(&button2, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 4); QTRY_COMPARE(finishedSpy.count(), 4);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
for (int x = 0; x < 2; ++x) { for (int x = 0; x < 2; ++x) {
QStateMachine machine; QStateMachine machine;
@ -2459,6 +2573,8 @@ void tst_QStateMachine::eventTransitions()
QCOMPARE(trans->targetState(), (QAbstractState*)s1); QCOMPARE(trans->targetState(), (QAbstractState*)s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
@ -2469,6 +2585,7 @@ void tst_QStateMachine::eventTransitions()
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
{ {
QStateMachine machine; QStateMachine machine;
@ -2485,16 +2602,19 @@ void tst_QStateMachine::eventTransitions()
trans->setTargetState(s1); trans->setTargetState(s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
TEST_RUNNING_CHANGED(true);
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
{ {
@ -2508,16 +2628,20 @@ void tst_QStateMachine::eventTransitions()
trans->setTargetState(s1); trans->setTargetState(s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
TEST_RUNNING_CHANGED(true);
QTest::keyPress(&button, Qt::Key_A); QTest::keyPress(&button, Qt::Key_A);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
{ {
QStateMachine machine; QStateMachine machine;
@ -2534,16 +2658,20 @@ void tst_QStateMachine::eventTransitions()
trans->setTargetState(s1); trans->setTargetState(s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
TEST_RUNNING_CHANGED(true);
QTest::keyPress(&button, Qt::Key_A); QTest::keyPress(&button, Qt::Key_A);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
// Multiple transitions for same (object,event) // Multiple transitions for same (object,event)
{ {
@ -2609,21 +2737,27 @@ void tst_QStateMachine::eventTransitions()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 2); QTRY_COMPARE(startedSpy.count(), 2);
TEST_RUNNING_CHANGED(true);
QTest::mouseRelease(&button, Qt::LeftButton); QTest::mouseRelease(&button, Qt::LeftButton);
QTRY_COMPARE(finishedSpy.count(), 2); QTRY_COMPARE(finishedSpy.count(), 2);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
} }
@ -2637,12 +2771,15 @@ void tst_QStateMachine::eventTransitions()
trans->setTargetState(s1); trans->setTargetState(s1);
s0->addTransition(trans); s0->addTransition(trans);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QTest::ignoreMessage(QtWarningMsg, "QObject event transitions are not supported for custom types"); QTest::ignoreMessage(QtWarningMsg, "QObject event transitions are not supported for custom types");
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
} }
// custom transition // custom transition
{ {
@ -2655,16 +2792,20 @@ void tst_QStateMachine::eventTransitions()
QCOMPARE(trans->eventSourceReceived(), (QObject*)0); QCOMPARE(trans->eventSourceReceived(), (QObject*)0);
QCOMPARE(trans->eventTypeReceived(), QEvent::None); QCOMPARE(trans->eventTypeReceived(), QEvent::None);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.setInitialState(s0); machine.setInitialState(s0);
machine.start(); machine.start();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
TEST_RUNNING_CHANGED(true);
QTest::mousePress(&button, Qt::LeftButton); QTest::mousePress(&button, Qt::LeftButton);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(trans->eventSourceReceived(), (QObject*)&button); QCOMPARE(trans->eventSourceReceived(), (QObject*)&button);
QCOMPARE(trans->eventTypeReceived(), QEvent::MouseButtonPress); QCOMPARE(trans->eventTypeReceived(), QEvent::MouseButtonPress);
@ -2686,14 +2827,18 @@ void tst_QStateMachine::graphicsSceneEventTransitions()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
QVERIFY(finishedSpy.count() == 0); QVERIFY(finishedSpy.count() == 0);
TEST_RUNNING_CHANGED(true);
QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove); QGraphicsSceneMouseEvent mouseEvent(QEvent::GraphicsSceneMouseMove);
scene.sendEvent(textItem, &mouseEvent); scene.sendEvent(textItem, &mouseEvent);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
#endif #endif
@ -2734,6 +2879,8 @@ void tst_QStateMachine::historyStates()
root->setInitialState(s0); root->setInitialState(s0);
s0->setInitialState(s00); s0->setInitialState(s00);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.start(); machine.start();
@ -2765,6 +2912,7 @@ void tst_QStateMachine::historyStates()
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
} }
@ -2774,10 +2922,12 @@ void tst_QStateMachine::startAndStop()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
QVERIFY(!machine.isRunning()); QVERIFY(!machine.isRunning());
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start."); QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start.");
@ -2785,11 +2935,13 @@ void tst_QStateMachine::startAndStop()
QCOMPARE(startedSpy.count(), 0); QCOMPARE(startedSpy.count(), 0);
QCOMPARE(stoppedSpy.count(), 0); QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
QCOMPARE(runningSpy.count(), 0);
QVERIFY(!machine.isRunning()); QVERIFY(!machine.isRunning());
machine.stop(); machine.stop();
QCOMPARE(startedSpy.count(), 0); QCOMPARE(startedSpy.count(), 0);
QCOMPARE(stoppedSpy.count(), 0); QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
QCOMPARE(runningSpy.count(), 0);
QState *s1 = new QState(&machine); QState *s1 = new QState(&machine);
machine.setInitialState(s1); machine.setInitialState(s1);
@ -2798,17 +2950,20 @@ void tst_QStateMachine::startAndStop()
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
QCOMPARE(stoppedSpy.count(), 0); QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().count(), 1); QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start(): already running"); QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start(): already running");
machine.start(); machine.start();
QCOMPARE(runningSpy.count(), 0);
machine.stop(); machine.stop();
QTRY_COMPARE(machine.isRunning(), false); QTRY_COMPARE(machine.isRunning(), false);
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
QCOMPARE(startedSpy.count(), 1); QCOMPARE(startedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().count(), 1); QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
@ -2817,6 +2972,81 @@ void tst_QStateMachine::startAndStop()
machine.stop(); machine.stop();
QTRY_COMPARE(startedSpy.count(), 2); QTRY_COMPARE(startedSpy.count(), 2);
QCOMPARE(stoppedSpy.count(), 2); QCOMPARE(stoppedSpy.count(), 2);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
}
void tst_QStateMachine::setRunning()
{
QStateMachine machine;
QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid());
QVERIFY(stoppedSpy.isValid());
QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
QVERIFY(!machine.isRunning());
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start.");
machine.setRunning(true);
QCOMPARE(startedSpy.count(), 0);
QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0);
QCOMPARE(runningSpy.count(), 0);
QVERIFY(!machine.isRunning());
machine.setRunning(false);
QCOMPARE(startedSpy.count(), 0);
QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0);
QCOMPARE(runningSpy.count(), 0);
QState *s1 = new QState(&machine);
machine.setInitialState(s1);
machine.setRunning(true);
QTRY_COMPARE(machine.isRunning(), true);
QTRY_COMPARE(startedSpy.count(), 1);
QCOMPARE(stoppedSpy.count(), 0);
QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(true);
QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s1));
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start(): already running");
machine.setRunning(true);
QCOMPARE(runningSpy.count(), 0);
machine.setRunning(false);
QTRY_COMPARE(machine.isRunning(), false);
QTRY_COMPARE(stoppedSpy.count(), 1);
QCOMPARE(startedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s1));
machine.setRunning(false);
QCOMPARE(runningSpy.count(), 0);
machine.start();
machine.setRunning(false);
QTRY_COMPARE(startedSpy.count(), 2);
QCOMPARE(stoppedSpy.count(), 2);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QState *s1_1 = new QState(s1);
QFinalState *s1_2 = new QFinalState(s1);
s1_1->addTransition(s1_2);
s1->setInitialState(s1_1);
QFinalState *s2 = new QFinalState(&machine);
s1->addTransition(s1, SIGNAL(finished()), s2);
machine.setRunning(false);
QCOMPARE(runningSpy.count(), 0);
machine.setRunning(true);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QTRY_COMPARE(startedSpy.count(), 3);
QCOMPARE(stoppedSpy.count(), 2);
QCOMPARE(finishedSpy.count(), 1);
} }
void tst_QStateMachine::targetStateWithNoParent() void tst_QStateMachine::targetStateWithNoParent()
@ -2830,10 +3060,12 @@ void tst_QStateMachine::targetStateWithNoParent()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 's1'"); QTest::ignoreMessage(QtWarningMsg, "Unrecoverable error detected in running state machine: No common ancestor for targets and source of transition from state 's1'");
@ -2841,6 +3073,7 @@ void tst_QStateMachine::targetStateWithNoParent()
QCOMPARE(machine.isRunning(), false); QCOMPARE(machine.isRunning(), false);
QCOMPARE(stoppedSpy.count(), 1); QCOMPARE(stoppedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.error(), QStateMachine::NoCommonAncestorForTransitionError); QCOMPARE(machine.error(), QStateMachine::NoCommonAncestorForTransitionError);
} }
@ -3270,10 +3503,13 @@ void tst_QStateMachine::propertiesAssignedSignalTransitionsReuseAnimationGroup()
s3->addTransition(s3, SIGNAL(propertiesAssigned()), s4); s3->addTransition(s3, SIGNAL(propertiesAssigned()), s4);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy machineFinishedSpy(&machine, &QStateMachine::finished); QSignalSpy machineFinishedSpy(&machine, &QStateMachine::finished);
QVERIFY(machineFinishedSpy.isValid()); QVERIFY(machineFinishedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(machineFinishedSpy.count(), 1); QTRY_COMPARE(machineFinishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s4)); QVERIFY(machine.configuration().contains(s4));
QCOMPARE(object->property("foo").toInt(), 789); QCOMPARE(object->property("foo").toInt(), 789);
@ -3772,10 +4008,13 @@ void tst_QStateMachine::nestedStateMachines()
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QTRY_COMPARE(machine.configuration().count(), 1+2*3); QTRY_COMPARE(machine.configuration().count(), 1+2*3);
QVERIFY(machine.configuration().contains(group)); QVERIFY(machine.configuration().contains(group));
for (int i = 0; i < 3; ++i) for (int i = 0; i < 3; ++i)
@ -3787,6 +4026,7 @@ void tst_QStateMachine::nestedStateMachines()
subMachines[i]->postEvent(new QEvent(QEvent::User)); subMachines[i]->postEvent(new QEvent(QEvent::User));
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
} }
void tst_QStateMachine::goToState() void tst_QStateMachine::goToState()
@ -3797,8 +4037,11 @@ void tst_QStateMachine::goToState()
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QStateMachinePrivate::get(&machine)->goToState(s2); QStateMachinePrivate::get(&machine)->goToState(s2);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
@ -3838,10 +4081,13 @@ void tst_QStateMachine::goToStateFromSourceWithTransition()
s1->addTransition(new QSignalTransition); s1->addTransition(new QSignalTransition);
QState *s2 = new QState(&machine); QState *s2 = new QState(&machine);
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QStateMachinePrivate::get(&machine)->goToState(s2); QStateMachinePrivate::get(&machine)->goToState(s2);
QCoreApplication::processEvents(); QCoreApplication::processEvents();
@ -3928,10 +4174,13 @@ void tst_QStateMachine::postEventFromOtherThread()
poster.start(); poster.start();
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
#ifndef QT_NO_WIDGETS #ifndef QT_NO_WIDGETS
@ -3987,15 +4236,18 @@ void tst_QStateMachine::stopInTransitionToFinalState()
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
QSignalSpy s2EnteredSpy(s2, &QFinalState::entered); QSignalSpy s2EnteredSpy(s2, &QFinalState::entered);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(stoppedSpy.isValid()); QVERIFY(stoppedSpy.isValid());
QVERIFY(finishedSpy.isValid()); QVERIFY(finishedSpy.isValid());
QVERIFY(s2EnteredSpy.isValid()); QVERIFY(s2EnteredSpy.isValid());
QVERIFY(runningSpy.isValid());
machine.start(); machine.start();
// Stopping should take precedence over finished. // Stopping should take precedence over finished.
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
QCOMPARE(s2EnteredSpy.count(), 1); QCOMPARE(s2EnteredSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s2)); QVERIFY(machine.configuration().contains(s2));
} }
@ -4029,10 +4281,13 @@ void tst_QStateMachine::stopInEventTest()
s1->addTransition(new StopInEventTestTransition()); s1->addTransition(new StopInEventTestTransition());
machine.setInitialState(s1); machine.setInitialState(s1);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QVERIFY(startedSpy.isValid()); QVERIFY(startedSpy.isValid());
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped); QSignalSpy stoppedSpy(&machine, &QStateMachine::stopped);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
@ -4042,6 +4297,7 @@ void tst_QStateMachine::stopInEventTest()
QTRY_COMPARE(stoppedSpy.count(), 1); QTRY_COMPARE(stoppedSpy.count(), 1);
QCOMPARE(finishedSpy.count(), 0); QCOMPARE(finishedSpy.count(), 0);
TEST_RUNNING_CHANGED(false);
QCOMPARE(machine.configuration().size(), 1); QCOMPARE(machine.configuration().size(), 1);
QVERIFY(machine.configuration().contains(s1)); QVERIFY(machine.configuration().contains(s1));
} }
@ -4070,12 +4326,16 @@ void tst_QStateMachine::testIncrementReceivers()
IncrementReceiversTest testObject; IncrementReceiversTest testObject;
s1->addTransition(&testObject, SIGNAL(mySignal()), s2); s1->addTransition(&testObject, SIGNAL(mySignal()), s2);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
machine.start(); machine.start();
TEST_RUNNING_CHANGED(true);
QMetaObject::invokeMethod(&testObject, "mySignal", Qt::QueuedConnection); QMetaObject::invokeMethod(&testObject, "mySignal", Qt::QueuedConnection);
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
QCOMPARE(testObject.signalList.size(), 1); QCOMPARE(testObject.signalList.size(), 1);
QCOMPARE(testObject.signalList.at(0), QMetaMethod::fromSignal(&IncrementReceiversTest::mySignal)); QCOMPARE(testObject.signalList.at(0), QMetaMethod::fromSignal(&IncrementReceiversTest::mySignal));
} }
@ -4091,9 +4351,12 @@ void tst_QStateMachine::initialStateIsEnteredBeforeStartedEmitted()
// transition should trigger. // transition should trigger.
s1->addTransition(&machine, SIGNAL(started()), s2); s1->addTransition(&machine, SIGNAL(started()), s2);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
machine.start(); machine.start();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
void tst_QStateMachine::deletePropertyAssignmentObjectBeforeEntry() void tst_QStateMachine::deletePropertyAssignmentObjectBeforeEntry()
@ -4261,10 +4524,13 @@ void tst_QStateMachine::initialStateIsFinal()
QStateMachine machine; QStateMachine machine;
QFinalState *f = new QFinalState(&machine); QFinalState *f = new QFinalState(&machine);
machine.setInitialState(f); machine.setInitialState(f);
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
machine.start(); machine.start();
QTRY_VERIFY(machine.configuration().contains(f)); QTRY_VERIFY(machine.configuration().contains(f));
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED_STARTED_STOPPED;
} }
class PropertyObject : public QObject class PropertyObject : public QObject
@ -4921,15 +5187,19 @@ void tst_QStateMachine::signalTransitionSenderInDifferentThread2()
thread.start(); thread.start();
QTRY_VERIFY(thread.isRunning()); QTRY_VERIFY(thread.isRunning());
QSignalSpy runningSpy(&machine, &QStateMachine::runningChanged);
QVERIFY(runningSpy.isValid());
QSignalSpy startedSpy(&machine, &QStateMachine::started); QSignalSpy startedSpy(&machine, &QStateMachine::started);
QSignalSpy finishedSpy(&machine, &QStateMachine::finished); QSignalSpy finishedSpy(&machine, &QStateMachine::finished);
machine.start(); machine.start();
QTRY_COMPARE(startedSpy.count(), 1); QTRY_COMPARE(startedSpy.count(), 1);
TEST_RUNNING_CHANGED(true);
emitter.emitSignalWithNoArg(); emitter.emitSignalWithNoArg();
// The second emission should not get "lost". // The second emission should not get "lost".
emitter.emitSignalWithDefaultArg(); emitter.emitSignalWithDefaultArg();
QTRY_COMPARE(finishedSpy.count(), 1); QTRY_COMPARE(finishedSpy.count(), 1);
TEST_RUNNING_CHANGED(false);
thread.quit(); thread.quit();
QTRY_VERIFY(thread.wait()); QTRY_VERIFY(thread.wait());