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

Commit 81bf8802 authored by Mitchell Wills's avatar Mitchell Wills Committed by Android (Google) Code Review
Browse files

Merge "Reenable the unit tests for StateMachine"

parents fc00de50 36afe5b5
Loading
Loading
Loading
Loading
+11 −11
Original line number Original line Diff line number Diff line
@@ -1305,7 +1305,7 @@ public class StateMachine {
     * @param state the state to add
     * @param state the state to add
     * @param parent the parent of state
     * @param parent the parent of state
     */
     */
    protected final void addState(State state, State parent) {
    public final void addState(State state, State parent) {
        mSmHandler.addState(state, parent);
        mSmHandler.addState(state, parent);
    }
    }


@@ -1313,7 +1313,7 @@ public class StateMachine {
     * Add a new state to the state machine, parent will be null
     * Add a new state to the state machine, parent will be null
     * @param state to add
     * @param state to add
     */
     */
    protected final void addState(State state) {
    public final void addState(State state) {
        mSmHandler.addState(state, null);
        mSmHandler.addState(state, null);
    }
    }


@@ -1323,14 +1323,14 @@ public class StateMachine {
     *
     *
     * @param initialState is the state which will receive the first message.
     * @param initialState is the state which will receive the first message.
     */
     */
    protected final void setInitialState(State initialState) {
    public final void setInitialState(State initialState) {
        mSmHandler.setInitialState(initialState);
        mSmHandler.setInitialState(initialState);
    }
    }


    /**
    /**
     * @return current message
     * @return current message
     */
     */
    protected final Message getCurrentMessage() {
    public final Message getCurrentMessage() {
        // mSmHandler can be null if the state machine has quit.
        // mSmHandler can be null if the state machine has quit.
        SmHandler smh = mSmHandler;
        SmHandler smh = mSmHandler;
        if (smh == null) return null;
        if (smh == null) return null;
@@ -1340,7 +1340,7 @@ public class StateMachine {
    /**
    /**
     * @return current state
     * @return current state
     */
     */
    protected final IState getCurrentState() {
    public final IState getCurrentState() {
        // mSmHandler can be null if the state machine has quit.
        // mSmHandler can be null if the state machine has quit.
        SmHandler smh = mSmHandler;
        SmHandler smh = mSmHandler;
        if (smh == null) return null;
        if (smh == null) return null;
@@ -1361,7 +1361,7 @@ public class StateMachine {
     *
     *
     * @param destState will be the state that receives the next message.
     * @param destState will be the state that receives the next message.
     */
     */
    protected final void transitionTo(IState destState) {
    public final void transitionTo(IState destState) {
        mSmHandler.transitionTo(destState);
        mSmHandler.transitionTo(destState);
    }
    }


@@ -1372,7 +1372,7 @@ public class StateMachine {
     * for all subsequent messages haltedProcessMessage
     * for all subsequent messages haltedProcessMessage
     * will be called.
     * will be called.
     */
     */
    protected final void transitionToHaltingState() {
    public final void transitionToHaltingState() {
        mSmHandler.transitionTo(mSmHandler.mHaltingState);
        mSmHandler.transitionTo(mSmHandler.mHaltingState);
    }
    }


@@ -1385,7 +1385,7 @@ public class StateMachine {
     *
     *
     * @param msg is deferred until the next transition.
     * @param msg is deferred until the next transition.
     */
     */
    protected final void deferMessage(Message msg) {
    public final void deferMessage(Message msg) {
        mSmHandler.deferMessage(msg);
        mSmHandler.deferMessage(msg);
    }
    }


@@ -1496,7 +1496,7 @@ public class StateMachine {
     *
     *
     * @param string
     * @param string
     */
     */
    protected void addLogRec(String string) {
    public void addLogRec(String string) {
        // mSmHandler can be null if the state machine has quit.
        // mSmHandler can be null if the state machine has quit.
        SmHandler smh = mSmHandler;
        SmHandler smh = mSmHandler;
        if (smh == null) return;
        if (smh == null) return;
@@ -1947,7 +1947,7 @@ public class StateMachine {
    /**
    /**
     * Quit the state machine after all currently queued up messages are processed.
     * Quit the state machine after all currently queued up messages are processed.
     */
     */
    protected final void quit() {
    public final void quit() {
        // mSmHandler can be null if the state machine is already stopped.
        // mSmHandler can be null if the state machine is already stopped.
        SmHandler smh = mSmHandler;
        SmHandler smh = mSmHandler;
        if (smh == null) return;
        if (smh == null) return;
@@ -1958,7 +1958,7 @@ public class StateMachine {
    /**
    /**
     * Quit the state machine immediately all currently queued messages will be discarded.
     * Quit the state machine immediately all currently queued messages will be discarded.
     */
     */
    protected final void quitNow() {
    public final void quitNow() {
        // mSmHandler can be null if the state machine is already stopped.
        // mSmHandler can be null if the state machine is already stopped.
        SmHandler smh = mSmHandler;
        SmHandler smh = mSmHandler;
        if (smh == null) return;
        if (smh == null) return;
+41 −42
Original line number Original line Diff line number Diff line
@@ -39,7 +39,6 @@ import junit.framework.TestCase;
/**
/**
 * Test for StateMachine.
 * Test for StateMachine.
 */
 */
@Suppress // Failing
public class StateMachineTest extends TestCase {
public class StateMachineTest extends TestCase {
    private static final String ENTER = "enter";
    private static final String ENTER = "enter";
    private static final String EXIT = "exit";
    private static final String EXIT = "exit";
@@ -100,7 +99,7 @@ public class StateMachineTest extends TestCase {


        @Override
        @Override
        public void onQuitting() {
        public void onQuitting() {
            log("onQuitting");
            tlog("onQuitting");
            addLogRec(ON_QUITTING);
            addLogRec(ON_QUITTING);
            mLogRecs = mThisSm.copyLogRecs();
            mLogRecs = mThisSm.copyLogRecs();
            synchronized (mThisSm) {
            synchronized (mThisSm) {
@@ -111,7 +110,7 @@ public class StateMachineTest extends TestCase {
        class S1 extends State {
        class S1 extends State {
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("S1.exit");
                tlog("S1.exit");
                addLogRec(EXIT);
                addLogRec(EXIT);
            }
            }
            @Override
            @Override
@@ -119,13 +118,13 @@ public class StateMachineTest extends TestCase {
                switch(message.what) {
                switch(message.what) {
                    // Sleep and assume the other messages will be queued up.
                    // Sleep and assume the other messages will be queued up.
                    case TEST_CMD_1: {
                    case TEST_CMD_1: {
                        log("TEST_CMD_1");
                        tlog("TEST_CMD_1");
                        sleep(500);
                        sleep(500);
                        quit();
                        quit();
                        break;
                        break;
                    }
                    }
                    default: {
                    default: {
                        log("default what=" + message.what);
                        tlog("default what=" + message.what);
                        break;
                        break;
                    }
                    }
                }
                }
@@ -201,7 +200,7 @@ public class StateMachineTest extends TestCase {


        @Override
        @Override
        public void onQuitting() {
        public void onQuitting() {
            log("onQuitting");
            tlog("onQuitting");
            addLogRec(ON_QUITTING);
            addLogRec(ON_QUITTING);
            // Get a copy of the log records since we're quitting and they will disappear
            // Get a copy of the log records since we're quitting and they will disappear
            mLogRecs = mThisSm.copyLogRecs();
            mLogRecs = mThisSm.copyLogRecs();
@@ -214,7 +213,7 @@ public class StateMachineTest extends TestCase {
        class S1 extends State {
        class S1 extends State {
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("S1.exit");
                tlog("S1.exit");
                addLogRec(EXIT);
                addLogRec(EXIT);
            }
            }
            @Override
            @Override
@@ -222,13 +221,13 @@ public class StateMachineTest extends TestCase {
                switch(message.what) {
                switch(message.what) {
                    // Sleep and assume the other messages will be queued up.
                    // Sleep and assume the other messages will be queued up.
                    case TEST_CMD_1: {
                    case TEST_CMD_1: {
                        log("TEST_CMD_1");
                        tlog("TEST_CMD_1");
                        sleep(500);
                        sleep(500);
                        quitNow();
                        quitNow();
                        break;
                        break;
                    }
                    }
                    default: {
                    default: {
                        log("default what=" + message.what);
                        tlog("default what=" + message.what);
                        break;
                        break;
                    }
                    }
                }
                }
@@ -309,12 +308,12 @@ public class StateMachineTest extends TestCase {
                // Test transitions in enter on the initial state work
                // Test transitions in enter on the initial state work
                addLogRec(ENTER);
                addLogRec(ENTER);
                transitionTo(mS2);
                transitionTo(mS2);
                log("S1.enter");
                tlog("S1.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                addLogRec(EXIT);
                addLogRec(EXIT);
                log("S1.exit");
                tlog("S1.exit");
            }
            }
        }
        }


@@ -322,7 +321,7 @@ public class StateMachineTest extends TestCase {
            @Override
            @Override
            public void enter() {
            public void enter() {
                addLogRec(ENTER);
                addLogRec(ENTER);
                log("S2.enter");
                tlog("S2.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
@@ -332,14 +331,14 @@ public class StateMachineTest extends TestCase {
                assertEquals(TEST_CMD_1, getCurrentMessage().what);
                assertEquals(TEST_CMD_1, getCurrentMessage().what);
                addLogRec(EXIT);
                addLogRec(EXIT);


                log("S2.exit");
                tlog("S2.exit");
            }
            }
            @Override
            @Override
            public boolean processMessage(Message message) {
            public boolean processMessage(Message message) {
                // Start a transition to S3 but it will be
                // Start a transition to S3 but it will be
                // changed to a transition to S4 in exit
                // changed to a transition to S4 in exit
                transitionTo(mS3);
                transitionTo(mS3);
                log("S2.processMessage");
                tlog("S2.processMessage");
                return HANDLED;
                return HANDLED;
            }
            }
        }
        }
@@ -348,12 +347,12 @@ public class StateMachineTest extends TestCase {
            @Override
            @Override
            public void enter() {
            public void enter() {
                addLogRec(ENTER);
                addLogRec(ENTER);
                log("S3.enter");
                tlog("S3.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                addLogRec(EXIT);
                addLogRec(EXIT);
                log("S3.exit");
                tlog("S3.exit");
            }
            }
        }
        }


@@ -363,12 +362,12 @@ public class StateMachineTest extends TestCase {
                addLogRec(ENTER);
                addLogRec(ENTER);
                // Test that we can do halting in an enter/exit
                // Test that we can do halting in an enter/exit
                transitionToHaltingState();
                transitionToHaltingState();
                log("S4.enter");
                tlog("S4.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                addLogRec(EXIT);
                addLogRec(EXIT);
                log("S4.exit");
                tlog("S4.exit");
            }
            }
        }
        }


@@ -572,7 +571,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state
            // Set the initial state
            setInitialState(mS1);
            setInitialState(mS1);
            if (DBG) log("StateMachine1: ctor X");
            if (DBG) tlog("StateMachine1: ctor X");
        }
        }


        class S1 extends State {
        class S1 extends State {
@@ -673,7 +672,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state
            // Set the initial state
            setInitialState(mS1);
            setInitialState(mS1);
            if (DBG) log("StateMachine2: ctor X");
            if (DBG) tlog("StateMachine2: ctor X");
        }
        }


        class S1 extends State {
        class S1 extends State {
@@ -782,7 +781,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state will be the child
            // Set the initial state will be the child
            setInitialState(mChildState);
            setInitialState(mChildState);
            if (DBG) log("StateMachine3: ctor X");
            if (DBG) tlog("StateMachine3: ctor X");
        }
        }


        class ParentState extends State {
        class ParentState extends State {
@@ -869,7 +868,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state will be child 1
            // Set the initial state will be child 1
            setInitialState(mChildState1);
            setInitialState(mChildState1);
            if (DBG) log("StateMachine4: ctor X");
            if (DBG) tlog("StateMachine4: ctor X");
        }
        }


        class ParentState extends State {
        class ParentState extends State {
@@ -969,7 +968,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state will be the child
            // Set the initial state will be the child
            setInitialState(mChildState1);
            setInitialState(mChildState1);
            if (DBG) log("StateMachine5: ctor X");
            if (DBG) tlog("StateMachine5: ctor X");
        }
        }


        class ParentState1 extends State {
        class ParentState1 extends State {
@@ -1296,7 +1295,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state
            // Set the initial state
            setInitialState(mS1);
            setInitialState(mS1);
            if (DBG) log("StateMachine6: ctor X");
            if (DBG) tlog("StateMachine6: ctor X");
        }
        }


        class S1 extends State {
        class S1 extends State {
@@ -1383,7 +1382,7 @@ public class StateMachineTest extends TestCase {


            // Set the initial state
            // Set the initial state
            setInitialState(mS1);
            setInitialState(mS1);
            if (DBG) log("StateMachine7: ctor X");
            if (DBG) tlog("StateMachine7: ctor X");
        }
        }


        class S1 extends State {
        class S1 extends State {
@@ -1654,7 +1653,7 @@ public class StateMachineTest extends TestCase {


        Hsm1(String name) {
        Hsm1(String name) {
            super(name);
            super(name);
            log("ctor E");
            tlog("ctor E");


            // Add states, use indentation to show hierarchy
            // Add states, use indentation to show hierarchy
            addState(mP1);
            addState(mP1);
@@ -1664,22 +1663,22 @@ public class StateMachineTest extends TestCase {


            // Set the initial state
            // Set the initial state
            setInitialState(mS1);
            setInitialState(mS1);
            log("ctor X");
            tlog("ctor X");
        }
        }


        class P1 extends State {
        class P1 extends State {
            @Override
            @Override
            public void enter() {
            public void enter() {
                log("P1.enter");
                tlog("P1.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("P1.exit");
                tlog("P1.exit");
            }
            }
            @Override
            @Override
            public boolean processMessage(Message message) {
            public boolean processMessage(Message message) {
                boolean retVal;
                boolean retVal;
                log("P1.processMessage what=" + message.what);
                tlog("P1.processMessage what=" + message.what);
                switch(message.what) {
                switch(message.what) {
                case CMD_2:
                case CMD_2:
                    // CMD_2 will arrive in mS2 before CMD_3
                    // CMD_2 will arrive in mS2 before CMD_3
@@ -1700,15 +1699,15 @@ public class StateMachineTest extends TestCase {
        class S1 extends State {
        class S1 extends State {
            @Override
            @Override
            public void enter() {
            public void enter() {
                log("S1.enter");
                tlog("S1.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("S1.exit");
                tlog("S1.exit");
            }
            }
            @Override
            @Override
            public boolean processMessage(Message message) {
            public boolean processMessage(Message message) {
                log("S1.processMessage what=" + message.what);
                tlog("S1.processMessage what=" + message.what);
                if (message.what == CMD_1) {
                if (message.what == CMD_1) {
                    // Transition to ourself to show that enter/exit is called
                    // Transition to ourself to show that enter/exit is called
                    transitionTo(mS1);
                    transitionTo(mS1);
@@ -1723,16 +1722,16 @@ public class StateMachineTest extends TestCase {
        class S2 extends State {
        class S2 extends State {
            @Override
            @Override
            public void enter() {
            public void enter() {
                log("S2.enter");
                tlog("S2.enter");
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("S2.exit");
                tlog("S2.exit");
            }
            }
            @Override
            @Override
            public boolean processMessage(Message message) {
            public boolean processMessage(Message message) {
                boolean retVal;
                boolean retVal;
                log("S2.processMessage what=" + message.what);
                tlog("S2.processMessage what=" + message.what);
                switch(message.what) {
                switch(message.what) {
                case(CMD_2):
                case(CMD_2):
                    sendMessage(CMD_4);
                    sendMessage(CMD_4);
@@ -1754,16 +1753,16 @@ public class StateMachineTest extends TestCase {
        class P2 extends State {
        class P2 extends State {
            @Override
            @Override
            public void enter() {
            public void enter() {
                log("P2.enter");
                tlog("P2.enter");
                sendMessage(CMD_5);
                sendMessage(CMD_5);
            }
            }
            @Override
            @Override
            public void exit() {
            public void exit() {
                log("P2.exit");
                tlog("P2.exit");
            }
            }
            @Override
            @Override
            public boolean processMessage(Message message) {
            public boolean processMessage(Message message) {
                log("P2.processMessage what=" + message.what);
                tlog("P2.processMessage what=" + message.what);
                switch(message.what) {
                switch(message.what) {
                case(CMD_3):
                case(CMD_3):
                    break;
                    break;
@@ -1779,7 +1778,7 @@ public class StateMachineTest extends TestCase {


        @Override
        @Override
        protected void onHalting() {
        protected void onHalting() {
            log("halting");
            tlog("halting");
            synchronized (this) {
            synchronized (this) {
                this.notifyAll();
                this.notifyAll();
            }
            }
@@ -1852,11 +1851,11 @@ public class StateMachineTest extends TestCase {
        if (DBG) tlog("testStateMachineSharedThread X");
        if (DBG) tlog("testStateMachineSharedThread X");
    }
    }


    private void tlog(String s) {
    private static void tlog(String s) {
        Log.d(TAG, s);
        Log.d(TAG, s);
    }
    }


    private void tloge(String s) {
    private static void tloge(String s) {
        Log.e(TAG, s);
        Log.e(TAG, s);
    }
    }
}
}