Merge "Make StateMachine#handle call handler defined in root state."
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user