Fix typos in QState sorting functions
authorKent Hansen <kent.hansen@nokia.com>
Thu, 31 May 2012 10:17:02 +0000 (12:17 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 4 Jun 2012 14:50:24 +0000 (16:50 +0200)
A QObject can't be a child of itself, so the comparison always
returned false. In practice, this was causing the entry/exit order
of parallel states to be random.

QObject::children() is documented to contain the children in the
order in which they were added, so this fix actually achieves
deterministic behavior.

Task-number: QTBUG-25959
Change-Id: Id3f12d6bfbc249f1d4fed0bafb7d0217093e458e
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
src/corelib/statemachine/qstatemachine.cpp
tests/auto/corelib/statemachine/qstatemachine/tst_qstatemachine.cpp

index 21c3ab5..fd190ab 100644 (file)
@@ -252,8 +252,8 @@ static int indexOfDescendant(QState *s, QAbstractState *desc)
 bool QStateMachinePrivate::stateEntryLessThan(QAbstractState *s1, QAbstractState *s2)
 {
     if (s1->parent() == s2->parent()) {
-        return s1->children().indexOf(s1)
-            < s2->children().indexOf(s2);
+        return s1->parent()->children().indexOf(s1)
+            < s2->parent()->children().indexOf(s2);
     } else if (isDescendantOf(s1, s2)) {
         return false;
     } else if (isDescendantOf(s2, s1)) {
@@ -270,8 +270,8 @@ bool QStateMachinePrivate::stateEntryLessThan(QAbstractState *s1, QAbstractState
 bool QStateMachinePrivate::stateExitLessThan(QAbstractState *s1, QAbstractState *s2)
 {
     if (s1->parent() == s2->parent()) {
-        return s1->children().indexOf(s1)
-            < s2->children().indexOf(s2);
+        return s1->parent()->children().indexOf(s1)
+            < s2->parent()->children().indexOf(s2);
     } else if (isDescendantOf(s1, s2)) {
         return true;
     } else if (isDescendantOf(s2, s1)) {
index beca75b..df6ac88 100644 (file)
@@ -1771,12 +1771,12 @@ void tst_QStateMachine::parallelStates()
 
     QState *s1 = new QState(QState::ParallelStates);
     QCOMPARE(s1->childMode(), QState::ParallelStates);
-      QState *s1_1 = new QState(s1);
+      TestState *s1_1 = new TestState(s1);
         QState *s1_1_1 = new QState(s1_1);
         QFinalState *s1_1_f = new QFinalState(s1_1);
         s1_1_1->addTransition(s1_1_f);
       s1_1->setInitialState(s1_1_1);
-      QState *s1_2 = new QState(s1);
+      TestState *s1_2 = new TestState(s1);
         QState *s1_2_1 = new QState(s1_2);
         QFinalState *s1_2_f = new QFinalState(s1_2);
         s1_2_1->addTransition(s1_2_f);
@@ -1797,10 +1797,25 @@ void tst_QStateMachine::parallelStates()
     machine.setInitialState(s1);
     QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
     QVERIFY(finishedSpy.isValid());
+    globalTick = 0;
     machine.start();
     QTRY_COMPARE(finishedSpy.count(), 1);
     QCOMPARE(machine.configuration().size(), 1);
     QVERIFY(machine.configuration().contains(s2));
+
+    // s1_1 is entered
+    QCOMPARE(s1_1->events.count(), 2);
+    QCOMPARE(s1_1->events.at(0).first, 0);
+    QCOMPARE(s1_1->events.at(0).second, TestState::Entry);
+    // s1_2 is entered
+    QCOMPARE(s1_2->events.at(0).first, 1);
+    QCOMPARE(s1_2->events.at(0).second, TestState::Entry);
+    // s1_1 is exited
+    QCOMPARE(s1_1->events.at(1).first, 2);
+    QCOMPARE(s1_1->events.at(1).second, TestState::Exit);
+    // s1_2 is exited
+    QCOMPARE(s1_2->events.at(1).first, 3);
+    QCOMPARE(s1_2->events.at(1).second, TestState::Exit);
 }
 
 void tst_QStateMachine::parallelRootState()