Nils
Nils

Reputation: 13767

How to test Qt SCXML state machines

I'm trying to verify the behavior of a state machine using Qt test framework. I simply don't get how I am supposed to tests Qt SCXML implementation. Sure there is QSignalSpy, but that is only for signals/slops which do not require the event loop to run. What I essentially want to do is:

myStateMachine.submitEvent("MyEvent");
// Run event loop
// Check result

I tried to QCoreApplication::processEvents() this sometimes worked, but sometimes also got stuck when calling processEvents(). I guess I might triggered an infinite loop. Also googling did not help, but there must be a way to do this properly.

Upvotes: 5

Views: 991

Answers (2)

ymoreau
ymoreau

Reputation: 3996

We had the same issue and we took advantage of QScxmlStateMachine::reachedStableState signal.

// We declared a QTestEventLoop as m_eventLoop
connect(stateMachine, &QScxmlStateMachine::reachedStableState, 
        &m_eventLoop, &QTestEventLoop::exitLoop);

stateMachine->submitEvent("MyEvent");
m_eventLoop.enterLoopMSecs(3000);
// check the state, results etc

Upvotes: 0

Kevin Krammer
Kevin Krammer

Reputation: 5207

In a QtTest based test you can use QTest::qWait() to run the event loop for a given time.

You can also use QSignalSpy to wait for a signal to happen within a certain time, see QSignalSpy::wait()

If there are more than one signal that can trigger test continuation, you can also use a nested event loop, e.g. something like this

QEventLoop loop;
connect(sender1, SIGNAL(signal1()), &loop, SLOT(quit()));
connect(sender2, SIGNAL(signal2()), &loop, SLOT(quit()));
loop.exec();

Potentially combined with a timer to end the loop in case none of those signals arrive

Upvotes: 3

Related Questions