Run finalize on "done.invoke." events


The current implementation seems to specifically avoid looking for
finalize calls on receiving a "done.invoke." event.

Nonetheless, one possible reading of the specification doesn't necessary
seem to imply that this event should not be run through a "<finalize>"
element of the invoking state.

Some external tools that were tried seems to agree with running finalize
on a "done.invoke" state.

Hence, align the code this reading of the spec, and the external tools
that were tried, by removing the exception for "done.invoke." events and
the processing of a "<finalize>" element.

When the above is done, a test fails in our compliance test suite, test
233.
In the test, a "<finalize>" handler tries to manipulate some data that is
supposed to be in the manipulated event.
With the change, it will do so on a produced "done.invoke." state that
has doesn't have that data, thus producing an error and consequently
failing the test.

While this is a direct effect of the performed change, it is not a fault
of the new behavior that the test is failing.
Instead, the failure was hidden by the previous behavior.

Normally, the test produces an event from the invoked service to the
parent state machine.
This event contains the data that the "<finalize>" handler expects.
When it will be processed, right before any transition is performed due
to the processing, the "<finalize>" handler will be run, enabling a
condition that will allow for a transition to a final state that
indicates that the test passed.

The invoked service should produce a "done.invoke." event, as part of
exiting the interpreter while being in a final top-level state.
But since the event will be processed after the parent state machine has
transitioned to the final state, it will never pass by the "<finalized>"
handler, which is not part of the active state anymore.

Nonetheless, the current implementation runs "<finalize>" handlers as
soon as the event is posted and not right before processing.

This goes against the spec which, in the specification of the
children, which are executable content, of a <finalize> element, states
that:

```
The Processor MUST execute them right before the event is pulled off the
external event queue for processing. The Processor MUST NOT execute them
at any other time or in response to any other events.
```

Following the form presented in appendix D of the specification and
considering the above, the code that processes "<finalize>"
handlers (and autoforwards which is linked to the same timing
restrictions) was transplanted from it's original position under
`postEvent` to a relevant position under `processEvents`.

The code was generally transplanted as-is, with the sole exception of
the removal of a `setEvent`/`resetEvent` pair in the moved code, under
the understanding that it was necessary in its original context, due to
the finalize handler expecting the processed event to be part of the
running state of the processor, but is subsumed in the new context due
to the processor already being put in the relevant state during the
processing of the event.

Hence, aligning with the required timing restrictions and ensuring that
the now failing test passes correctly.

An additional test case was added to "tst_statemachine" to ensure that
"<finalize>" handlers now run on "done.invoke." events.

Fixes: QTBUG-135906
Change-Id: I8ff4af2c2909418d4e23ad1dcdb0d2cc0c6269f1
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
commit766c27270b18f6bb10ffb6726f62b7c28b286401
parent0bcc9cc5e5a701805ba020c47fa2341c60953bae
authorLuca Di Sera <luca.disera@qt.io>
committerLuca Di Sera <luca.disera@qt.io>
commit timeMon, 16 Jun 2025 11:38:20 +0200