Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8ba116e5 authored by Issei Suzuki's avatar Issei Suzuki Committed by Android (Google) Code Review
Browse files

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

parents 41081d11 673d3f36
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -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;
        }
    }

+14 −0
Original line number Diff line number Diff line
@@ -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();