summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-05-07 10:40:38 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-05-07 10:41:11 (GMT)
commit81eebfbe01edb002d55c3504cc2558689cf0f936 (patch)
tree1cee50ade6cfd167cf503182b7f67afe5081fe56
parent1e404bcb6df5127a1d0e24ed11cce1c0260a57e9 (diff)
downloadQt-81eebfbe01edb002d55c3504cc2558689cf0f936.zip
Qt-81eebfbe01edb002d55c3504cc2558689cf0f936.tar.gz
Qt-81eebfbe01edb002d55c3504cc2558689cf0f936.tar.bz2
add QStateMachine::isRunning()
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp11
-rw-r--r--src/corelib/statemachine/qstatemachine.h2
-rw-r--r--tests/auto/qstatemachine/tst_qstatemachine.cpp42
3 files changed, 55 insertions, 0 deletions
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 4533115..21e564c 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -1728,6 +1728,17 @@ void QStateMachine::removeState(QAbstractState *state)
}
/*!
+ Returns whether this state machine is running.
+
+ start(), stop()
+*/
+bool QStateMachine::isRunning() const
+{
+ Q_D(const QStateMachine);
+ return (d->state == QStateMachinePrivate::Running);
+}
+
+/*!
Starts this state machine. The machine will reset its configuration and
transition to the initial state. When a final top-level state (QFinalState)
is entered, the machine will emit the finished() signal.
diff --git a/src/corelib/statemachine/qstatemachine.h b/src/corelib/statemachine/qstatemachine.h
index d73404a..5dc6c0b 100644
--- a/src/corelib/statemachine/qstatemachine.h
+++ b/src/corelib/statemachine/qstatemachine.h
@@ -108,6 +108,8 @@ public:
QString errorString() const;
void clearError();
+ bool isRunning() const;
+
#ifndef QT_NO_ANIMATION
bool animationsEnabled() const;
void setAnimationsEnabled(bool enabled);
diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp
index 3876a20..edd6459 100644
--- a/tests/auto/qstatemachine/tst_qstatemachine.cpp
+++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp
@@ -104,6 +104,7 @@ private slots:
void signalTransitions();
void eventTransitions();
void historyStates();
+ void startAndStop();
void transitionToRootState();
void transitionEntersParent();
@@ -1094,14 +1095,17 @@ void tst_QStateMachine::stateEntryAndExit()
s2->addTransition(s3);
QSignalSpy startedSpy(&machine, SIGNAL(started()));
+ QSignalSpy stoppedSpy(&machine, SIGNAL(stopped()));
QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
machine.setInitialState(s1);
QVERIFY(machine.configuration().isEmpty());
globalTick = 0;
+ QVERIFY(!machine.isRunning());
machine.start();
QTRY_COMPARE(startedSpy.count(), 1);
QTRY_COMPARE(finishedSpy.count(), 1);
+ QTRY_COMPARE(stoppedSpy.count(), 0);
QCOMPARE(machine.configuration().count(), 1);
QVERIFY(machine.configuration().contains(s3));
@@ -1826,6 +1830,44 @@ void tst_QStateMachine::historyStates()
QTRY_COMPARE(finishedSpy.count(), 1);
}
+void tst_QStateMachine::startAndStop()
+{
+ QStateMachine machine;
+ QSignalSpy startedSpy(&machine, SIGNAL(started()));
+ QSignalSpy stoppedSpy(&machine, SIGNAL(stopped()));
+ QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
+ QVERIFY(!machine.isRunning());
+ QTest::ignoreMessage(QtWarningMsg, "QStateMachine::start: No initial state set for machine. Refusing to start.");
+ machine.start();
+ QCOMPARE(startedSpy.count(), 0);
+ QCOMPARE(stoppedSpy.count(), 0);
+ QCOMPARE(finishedSpy.count(), 0);
+ QVERIFY(!machine.isRunning());
+ machine.stop();
+ QCOMPARE(startedSpy.count(), 0);
+ QCOMPARE(stoppedSpy.count(), 0);
+ QCOMPARE(finishedSpy.count(), 0);
+
+ QState *s1 = new QState(machine.rootState());
+ machine.setInitialState(s1);
+ machine.start();
+ QTRY_COMPARE(machine.isRunning(), true);
+ QTRY_COMPARE(startedSpy.count(), 1);
+ QCOMPARE(stoppedSpy.count(), 0);
+ QCOMPARE(finishedSpy.count(), 0);
+ QCOMPARE(machine.configuration().count(), 1);
+ QVERIFY(machine.configuration().contains(s1));
+
+ machine.stop();
+ QTRY_COMPARE(machine.isRunning(), false);
+ QTRY_COMPARE(stoppedSpy.count(), 1);
+ QCOMPARE(startedSpy.count(), 1);
+ QCOMPARE(finishedSpy.count(), 0);
+
+ QCOMPARE(machine.configuration().count(), 1);
+ QVERIFY(machine.configuration().contains(s1));
+}
+
void tst_QStateMachine::defaultGlobalRestorePolicy()
{
QStateMachine machine;