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

Commit 47e1c7a0 authored by mukesh agrawal's avatar mukesh agrawal
Browse files

StateMachine: make toString() terser

The current implementation of the toString()
method calls dump(). This causes two problems:

1. toString() may return a large string. This
   is at odds with the advice in the documentation
   for Object.toString(), which says that the
   returned String should be concise, and easy
   to read.
2. The dump() method is overriden by many of the
   StateMachine subclasses. Some of those subclasses
   have dump() implementations that are expensive,
   and/or have dependencies on other objects.

To resolve these problems, we simpify
StateMachine.toString().

Along the way: remove a stale comment about
implementing dump() using toString().

Note: only ran the StateMachine tests, since some
other tests are already failing.

Bug: 36661851
Test: tests/utiltests/runtests.sh \
      -e class com.android.internal.util.StateMachineTest
Change-Id: I5c16c650f01178c4d018b6a65e4aa95fb905aff6
parent 28882ed5
Loading
Loading
Loading
Loading
+9 −8
Original line number Diff line number Diff line
@@ -2070,8 +2070,6 @@ public class StateMachine {
     * @param args
     */
    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
        // Cannot just invoke pw.println(this.toString()) because if the
        // resulting string is to long it won't be displayed.
        pw.println(getName() + ":");
        pw.println(" total records=" + getLogRecCount());
        for (int i = 0; i < getLogRecSize(); i++) {
@@ -2083,12 +2081,15 @@ public class StateMachine {

    @Override
    public String toString() {
        StringWriter sr = new StringWriter();
        PrintWriter pr = new PrintWriter(sr);
        dump(null, pr, null);
        pr.flush();
        pr.close();
        return sr.toString();
        String name = "(null)";
        String state = "(null)";
        try {
            name = mName.toString();
            state = mSmHandler.getCurrentState().getName().toString();
        } catch (NullPointerException npe) {
            // Will use default(s) initialized above.
        }
        return "name=" + name + " state=" + state;
    }

    /**
+60 −0
Original line number Diff line number Diff line
@@ -79,6 +79,66 @@ public class StateMachineTest extends TestCase {
        }
    }

    /**
     * Tests {@link StateMachine#toString()}.
     */
    class StateMachineToStringTest extends StateMachine {
        StateMachineToStringTest(String name) {
            super(name);
        }
    }

    class ExampleState extends State {
        String mName;

        ExampleState(String name) {
            mName = name;
        }

        @Override
        public String getName() {
            return mName;
        }
    }

    @SmallTest
    public void testToStringSucceedsEvenIfMachineHasNoStates() throws Exception {
        StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
        assertTrue(stateMachine.toString().contains("TestStateMachine"));
    }

    @SmallTest
    public void testToStringSucceedsEvenIfStateHasNoName() throws Exception {
        StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
        State exampleState = new ExampleState(null);
        stateMachine.addState(exampleState);
        stateMachine.setInitialState(exampleState);
        stateMachine.start();
        assertTrue(stateMachine.toString().contains("TestStateMachine"));
        assertTrue(stateMachine.toString().contains("(null)"));
    }

    @SmallTest
    public void testToStringIncludesMachineAndStateNames() throws Exception {
        StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
        State exampleState = new ExampleState("exampleState");
        stateMachine.addState(exampleState);
        stateMachine.setInitialState(exampleState);
        stateMachine.start();
        assertTrue(stateMachine.toString().contains("TestStateMachine"));
        assertTrue(stateMachine.toString().contains("exampleState"));
    }

    @SmallTest
    public void testToStringDoesNotContainMultipleLines() throws Exception {
        StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
        State exampleState = new ExampleState("exampleState");
        stateMachine.addState(exampleState);
        stateMachine.setInitialState(exampleState);
        stateMachine.start();
        assertFalse(stateMachine.toString().contains("\n"));
    }

    /**
     * Tests {@link StateMachine#quit()}.
     */