Merge "Make StateMachine#handle call handler defined in root state."

This commit is contained in:
Issei Suzuki
2022-12-21 11:34:52 +00:00
committed by Android (Google) Code Review
2 changed files with 19 additions and 6 deletions

View File

@@ -177,19 +177,18 @@ public class StateMachine {
}
/**
* Process an event. Search handler for a given event and {@link Handler#handle(int)}. If the
* handler cannot handle the event, delegate it to a handler for a parent of the given state.
* Process an event. Search handler for a given event and {@link Handler#handle(int, Object)}.
* If the handler cannot handle the event, delegate it to a handler for a parent of the given
* state.
*
* @param event Type of an event.
*/
public void handle(int event, @Nullable Object param) {
int state = mState;
while (state != 0) {
for (int state = mState;; state >>= 4) {
final Handler h = mStateHandlers.get(state);
if (h != null && h.handle(event, param)) {
if ((h != null && h.handle(event, param)) || state == 0) {
return;
}
state >>= 4;
}
}

View File

@@ -214,6 +214,20 @@ public class StateMachineTest {
assertEquals("h25;", log.toString());
}
@Test
public void testStateMachineTriggerStateActionDelegateRoot() {
final StringBuffer log = new StringBuffer();
StateMachine stateMachine = new StateMachine(0x2);
stateMachine.addStateHandler(0x0, new LoggingHandler(0x0, log));
stateMachine.addStateHandler(0x2,
new LoggingHandler(0x2, log, false /* handleSelf */));
// state 0x2 delegate the message handling to its parent state
stateMachine.handle(0, null);
assertEquals("h0;", log.toString());
}
@Test
public void testStateMachineNestedTransition() {
final StringBuffer log = new StringBuffer();