diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-09-29 13:04:09 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-09-29 13:09:36 (GMT) |
commit | f6a6e30eb16616b90d90fd6e20f9d840da41b9d1 (patch) | |
tree | 5e3a5ca259a06d78cb7cc2777084bc4118724322 /tests/auto | |
parent | 1fd189619149c6d3489a35e42c4034d4145237dc (diff) | |
download | Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.zip Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.gz Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.bz2 |
Introduce state machine event priority, make it possible to cancel events
The priority specifies whether the event should be posted to what the
SCXML spec refers to as the "external" (NormalPriority) queue, or the
"internal" (HighPriority) queue.
Delayed events are now posted through a separate function,
postDelayedEvent(). That function returns an id that can be passed to
cancelDelayedEvent() to cancel it.
Reviewed-by: Eskil Abrahamsen Blomfeldt
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/qstatemachine/tst_qstatemachine.cpp | 49 |
1 files changed, 44 insertions, 5 deletions
diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp index 37b34bf..463dbf6 100644 --- a/tests/auto/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -119,6 +119,7 @@ private slots: void assignProperty(); void assignPropertyWithAnimation(); void postEvent(); + void cancelDelayedEvent(); void stateFinished(); void parallelStates(); void parallelRootState(); @@ -1543,8 +1544,8 @@ private: class StringEventPoster : public QState { public: - StringEventPoster(QStateMachine *machine, const QString &value, QState *parent = 0) - : QState(parent), m_machine(machine), m_value(value), m_delay(0) {} + StringEventPoster(const QString &value, QState *parent = 0) + : QState(parent), m_value(value), m_delay(-1) {} void setString(const QString &value) { m_value = value; } @@ -1554,12 +1555,14 @@ public: protected: virtual void onEntry(QEvent *) { - m_machine->postEvent(new StringEvent(m_value), m_delay); + if (m_delay == -1) + machine()->postEvent(new StringEvent(m_value)); + else + machine()->postDelayedEvent(new StringEvent(m_value), m_delay); } virtual void onExit(QEvent *) {} private: - QStateMachine *m_machine; QString m_value; int m_delay; }; @@ -1573,7 +1576,7 @@ void tst_QStateMachine::postEvent() QTest::ignoreMessage(QtWarningMsg, "QStateMachine::postEvent: cannot post event when the state machine is not running"); machine.postEvent(&e); } - StringEventPoster *s1 = new StringEventPoster(&machine, "a"); + StringEventPoster *s1 = new StringEventPoster("a"); if (x == 1) s1->setDelay(100); QFinalState *s2 = new QFinalState; @@ -1599,6 +1602,42 @@ void tst_QStateMachine::postEvent() } } +void tst_QStateMachine::cancelDelayedEvent() +{ + QStateMachine machine; + QTest::ignoreMessage(QtWarningMsg, "QStateMachine::cancelDelayedEvent: the machine is not running"); + QVERIFY(!machine.cancelDelayedEvent(-1)); + + QState *s1 = new QState(&machine); + QFinalState *s2 = new QFinalState(&machine); + s1->addTransition(new StringTransition("a", s2)); + machine.setInitialState(s1); + + QSignalSpy startedSpy(&machine, SIGNAL(started())); + machine.start(); + QTRY_COMPARE(startedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s1)); + + int id1 = machine.postDelayedEvent(new StringEvent("c"), 50000); + QVERIFY(id1 != -1); + int id2 = machine.postDelayedEvent(new StringEvent("b"), 25000); + QVERIFY(id2 != -1); + QVERIFY(id2 != id1); + int id3 = machine.postDelayedEvent(new StringEvent("a"), 100); + QVERIFY(id3 != -1); + QVERIFY(id3 != id2); + QVERIFY(machine.cancelDelayedEvent(id1)); + QVERIFY(!machine.cancelDelayedEvent(id1)); + QVERIFY(machine.cancelDelayedEvent(id2)); + QVERIFY(!machine.cancelDelayedEvent(id2)); + + QSignalSpy finishedSpy(&machine, SIGNAL(finished())); + QTRY_COMPARE(finishedSpy.count(), 1); + QCOMPARE(machine.configuration().size(), 1); + QVERIFY(machine.configuration().contains(s2)); +} + void tst_QStateMachine::stateFinished() { QStateMachine machine; |