StateMachineTestpublic class StateMachineTest extends TestCase
Fields Summary |
---|
private static final String | ENTER | private static final String | EXIT | private static final String | ON_QUITTING | private static final int | TEST_CMD_1 | private static final int | TEST_CMD_2 | private static final int | TEST_CMD_3 | private static final int | TEST_CMD_4 | private static final int | TEST_CMD_5 | private static final int | TEST_CMD_6 | private static final boolean | DBG | private static final boolean | WAIT_FOR_DEBUGGER | private static final String | TAG | private static int | sharedCounter | private static Object | waitObject |
Methods Summary |
---|
private void | dumpLogRecs(com.android.internal.util.StateMachine sm)
int size = sm.getLogRecSize();
tlog("size=" + size + " count=" + sm.getLogRecCount());
for (int i = 0; i < size; i++) {
LogRec lr = sm.getLogRec(i);
tlog(lr.toString());
}
| private void | dumpLogRecs(java.util.Collection clr)
int size = clr.size();
tlog("size=" + size);
for (LogRec lr : clr) {
tlog(lr.toString());
}
| private void | sleep(int millis)
try {
Thread.sleep(millis);
} catch(InterruptedException e) {
}
| public void | testHsm1()
if (DBG) tlog("testHsm1 E");
Hsm1 sm = Hsm1.makeHsm1();
// Send messages
sm.sendMessage(Hsm1.CMD_1);
sm.sendMessage(Hsm1.CMD_2);
synchronized (sm) {
// Wait for the last state machine to notify its done
try {
sm.wait();
} catch (InterruptedException e) {
tloge("testHsm1: exception while waiting " + e.getMessage());
}
}
dumpLogRecs(sm);
assertEquals(7, sm.getLogRecCount());
LogRec lr = sm.getLogRec(0);
assertEquals(Hsm1.CMD_1, lr.getWhat());
assertEquals(sm.mS1, lr.getState());
assertEquals(sm.mS1, lr.getOriginalState());
lr = sm.getLogRec(1);
assertEquals(Hsm1.CMD_2, lr.getWhat());
assertEquals(sm.mP1, lr.getState());
assertEquals(sm.mS1, lr.getOriginalState());
lr = sm.getLogRec(2);
assertEquals(Hsm1.CMD_2, lr.getWhat());
assertEquals(sm.mS2, lr.getState());
assertEquals(sm.mS2, lr.getOriginalState());
lr = sm.getLogRec(3);
assertEquals(Hsm1.CMD_3, lr.getWhat());
assertEquals(sm.mS2, lr.getState());
assertEquals(sm.mS2, lr.getOriginalState());
lr = sm.getLogRec(4);
assertEquals(Hsm1.CMD_3, lr.getWhat());
assertEquals(sm.mP2, lr.getState());
assertEquals(sm.mP2, lr.getOriginalState());
lr = sm.getLogRec(5);
assertEquals(Hsm1.CMD_4, lr.getWhat());
assertEquals(sm.mP2, lr.getState());
assertEquals(sm.mP2, lr.getOriginalState());
lr = sm.getLogRec(6);
assertEquals(Hsm1.CMD_5, lr.getWhat());
assertEquals(sm.mP2, lr.getState());
assertEquals(sm.mP2, lr.getOriginalState());
if (DBG) tlog("testStateMachineSharedThread X");
| public void | testStateMachine0()
//if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachine0 sm0 = new StateMachine0("sm0");
sm0.start();
if (sm0.isDbg()) tlog("testStateMachine0 E");
synchronized (sm0) {
// Send 6 messages
for (int i = 1; i <= 6; i++) {
sm0.sendMessage(sm0.obtainMessage(i));
}
try {
// wait for the messages to be handled
sm0.wait();
} catch (InterruptedException e) {
tloge("testStateMachine0: exception while waiting " + e.getMessage());
}
}
assertEquals(6, sm0.getLogRecCount());
assertEquals(3, sm0.getLogRecSize());
dumpLogRecs(sm0);
LogRec lr;
lr = sm0.getLogRec(0);
assertEquals(TEST_CMD_4, lr.getWhat());
assertEquals(sm0.mS1, lr.getState());
assertEquals(sm0.mS1, lr.getOriginalState());
lr = sm0.getLogRec(1);
assertEquals(TEST_CMD_5, lr.getWhat());
assertEquals(sm0.mS1, lr.getState());
assertEquals(sm0.mS1, lr.getOriginalState());
lr = sm0.getLogRec(2);
assertEquals(TEST_CMD_6, lr.getWhat());
assertEquals(sm0.mS1, lr.getState());
assertEquals(sm0.mS1, lr.getOriginalState());
if (sm0.isDbg()) tlog("testStateMachine0 X");
| public void | testStateMachine1()
StateMachine1 sm1 = new StateMachine1("sm1");
sm1.start();
if (sm1.isDbg()) tlog("testStateMachine1 E");
synchronized (sm1) {
// Send two messages
sm1.sendMessage(TEST_CMD_1);
sm1.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm1.wait();
} catch (InterruptedException e) {
tloge("testStateMachine1: exception while waiting " + e.getMessage());
}
}
assertEquals(2, sm1.mEnterCount);
assertEquals(2, sm1.mExitCount);
assertEquals(2, sm1.getLogRecSize());
LogRec lr;
lr = sm1.getLogRec(0);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm1.mS1, lr.getState());
assertEquals(sm1.mS1, lr.getOriginalState());
lr = sm1.getLogRec(1);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm1.mS1, lr.getState());
assertEquals(sm1.mS1, lr.getOriginalState());
assertEquals(2, sm1.mEnterCount);
assertEquals(2, sm1.mExitCount);
if (sm1.isDbg()) tlog("testStateMachine1 X");
| public void | testStateMachine2()
StateMachine2 sm2 = new StateMachine2("sm2");
sm2.start();
if (sm2.isDbg()) tlog("testStateMachine2 E");
synchronized (sm2) {
// Send two messages
sm2.sendMessage(TEST_CMD_1);
sm2.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm2.wait();
} catch (InterruptedException e) {
tloge("testStateMachine2: exception while waiting " + e.getMessage());
}
}
assertEquals(4, sm2.getLogRecSize());
LogRec lr;
lr = sm2.getLogRec(0);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm2.mS1, lr.getState());
lr = sm2.getLogRec(1);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm2.mS1, lr.getState());
lr = sm2.getLogRec(2);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm2.mS2, lr.getState());
lr = sm2.getLogRec(3);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm2.mS2, lr.getState());
assertTrue(sm2.mDidEnter);
assertTrue(sm2.mDidExit);
if (sm2.isDbg()) tlog("testStateMachine2 X");
| public void | testStateMachine3()
StateMachine3 sm3 = new StateMachine3("sm3");
sm3.start();
if (sm3.isDbg()) tlog("testStateMachine3 E");
synchronized (sm3) {
// Send two messages
sm3.sendMessage(TEST_CMD_1);
sm3.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm3.wait();
} catch (InterruptedException e) {
tloge("testStateMachine3: exception while waiting " + e.getMessage());
}
}
assertEquals(2, sm3.getLogRecSize());
LogRec lr;
lr = sm3.getLogRec(0);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm3.mParentState, lr.getState());
assertEquals(sm3.mChildState, lr.getOriginalState());
lr = sm3.getLogRec(1);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm3.mParentState, lr.getState());
assertEquals(sm3.mChildState, lr.getOriginalState());
if (sm3.isDbg()) tlog("testStateMachine3 X");
| public void | testStateMachine4()
StateMachine4 sm4 = new StateMachine4("sm4");
sm4.start();
if (sm4.isDbg()) tlog("testStateMachine4 E");
synchronized (sm4) {
// Send two messages
sm4.sendMessage(TEST_CMD_1);
sm4.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm4.wait();
} catch (InterruptedException e) {
tloge("testStateMachine4: exception while waiting " + e.getMessage());
}
}
assertEquals(2, sm4.getLogRecSize());
LogRec lr;
lr = sm4.getLogRec(0);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm4.mChildState1, lr.getState());
assertEquals(sm4.mChildState1, lr.getOriginalState());
lr = sm4.getLogRec(1);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm4.mParentState, lr.getState());
assertEquals(sm4.mChildState2, lr.getOriginalState());
if (sm4.isDbg()) tlog("testStateMachine4 X");
| public void | testStateMachine5()
StateMachine5 sm5 = new StateMachine5("sm5");
sm5.start();
if (sm5.isDbg()) tlog("testStateMachine5 E");
synchronized (sm5) {
// Send 6 messages
sm5.sendMessage(TEST_CMD_1);
sm5.sendMessage(TEST_CMD_2);
sm5.sendMessage(TEST_CMD_3);
sm5.sendMessage(TEST_CMD_4);
sm5.sendMessage(TEST_CMD_5);
sm5.sendMessage(TEST_CMD_6);
try {
// wait for the messages to be handled
sm5.wait();
} catch (InterruptedException e) {
tloge("testStateMachine5: exception while waiting " + e.getMessage());
}
}
assertEquals(6, sm5.getLogRecSize());
assertEquals(1, sm5.mParentState1EnterCount);
assertEquals(1, sm5.mParentState1ExitCount);
assertEquals(1, sm5.mChildState1EnterCount);
assertEquals(1, sm5.mChildState1ExitCount);
assertEquals(1, sm5.mChildState2EnterCount);
assertEquals(1, sm5.mChildState2ExitCount);
assertEquals(2, sm5.mParentState2EnterCount);
assertEquals(2, sm5.mParentState2ExitCount);
assertEquals(1, sm5.mChildState3EnterCount);
assertEquals(1, sm5.mChildState3ExitCount);
assertEquals(2, sm5.mChildState4EnterCount);
assertEquals(2, sm5.mChildState4ExitCount);
assertEquals(1, sm5.mChildState5EnterCount);
assertEquals(1, sm5.mChildState5ExitCount);
LogRec lr;
lr = sm5.getLogRec(0);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(sm5.mChildState1, lr.getState());
assertEquals(sm5.mChildState1, lr.getOriginalState());
lr = sm5.getLogRec(1);
assertEquals(TEST_CMD_2, lr.getWhat());
assertEquals(sm5.mChildState2, lr.getState());
assertEquals(sm5.mChildState2, lr.getOriginalState());
lr = sm5.getLogRec(2);
assertEquals(TEST_CMD_3, lr.getWhat());
assertEquals(sm5.mChildState5, lr.getState());
assertEquals(sm5.mChildState5, lr.getOriginalState());
lr = sm5.getLogRec(3);
assertEquals(TEST_CMD_4, lr.getWhat());
assertEquals(sm5.mChildState3, lr.getState());
assertEquals(sm5.mChildState3, lr.getOriginalState());
lr = sm5.getLogRec(4);
assertEquals(TEST_CMD_5, lr.getWhat());
assertEquals(sm5.mChildState4, lr.getState());
assertEquals(sm5.mChildState4, lr.getOriginalState());
lr = sm5.getLogRec(5);
assertEquals(TEST_CMD_6, lr.getWhat());
assertEquals(sm5.mParentState2, lr.getState());
assertEquals(sm5.mParentState2, lr.getOriginalState());
if (sm5.isDbg()) tlog("testStateMachine5 X");
| public void | testStateMachine6()
final int DELAY_TIME = 250;
final int DELAY_FUDGE = 20;
StateMachine6 sm6 = new StateMachine6("sm6");
sm6.start();
if (sm6.isDbg()) tlog("testStateMachine6 E");
synchronized (sm6) {
// Send a message
sm6.sendMessageDelayed(TEST_CMD_2, DELAY_TIME);
try {
// wait for the messages to be handled
sm6.wait();
} catch (InterruptedException e) {
tloge("testStateMachine6: exception while waiting " + e.getMessage());
}
}
/**
* TEST_CMD_1 was sent in enter and must always have been processed
* immediately after construction and hence the arrival time difference
* should always >= to the DELAY_TIME
*/
long arrivalTimeDiff = sm6.mArrivalTimeMsg2 - sm6.mArrivalTimeMsg1;
long expectedDelay = DELAY_TIME - DELAY_FUDGE;
if (sm6.isDbg()) tlog("testStateMachine6: expect " + arrivalTimeDiff
+ " >= " + expectedDelay);
assertTrue(arrivalTimeDiff >= expectedDelay);
if (sm6.isDbg()) tlog("testStateMachine6 X");
| public void | testStateMachine7()
final int SM7_DELAY_FUDGE = 20;
StateMachine7 sm7 = new StateMachine7("sm7");
sm7.start();
if (sm7.isDbg()) tlog("testStateMachine7 E");
synchronized (sm7) {
// Send a message
sm7.sendMessage(TEST_CMD_1);
try {
// wait for the messages to be handled
sm7.wait();
} catch (InterruptedException e) {
tloge("testStateMachine7: exception while waiting " + e.getMessage());
}
}
/**
* TEST_CMD_3 was sent in S2.enter with a delay and must always have been
* processed immediately after S1.exit. Since S1.exit sent TEST_CMD_2
* without a delay the arrival time difference should always >= to SM7_DELAY_TIME.
*/
long arrivalTimeDiff = sm7.mArrivalTimeMsg3 - sm7.mArrivalTimeMsg2;
long expectedDelay = sm7.SM7_DELAY_TIME - SM7_DELAY_FUDGE;
if (sm7.isDbg()) tlog("testStateMachine7: expect " + arrivalTimeDiff
+ " >= " + expectedDelay);
assertTrue(arrivalTimeDiff >= expectedDelay);
if (sm7.isDbg()) tlog("testStateMachine7 X");
| public void | testStateMachineEnterExitTransitionToTest()
//if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachineEnterExitTransitionToTest smEnterExitTranstionToTest =
new StateMachineEnterExitTransitionToTest("smEnterExitTranstionToTest");
smEnterExitTranstionToTest.start();
if (smEnterExitTranstionToTest.isDbg()) {
tlog("testStateMachineEnterExitTransitionToTest E");
}
synchronized (smEnterExitTranstionToTest) {
smEnterExitTranstionToTest.sendMessage(TEST_CMD_1);
try {
// wait for the messages to be handled
smEnterExitTranstionToTest.wait();
} catch (InterruptedException e) {
tloge("testStateMachineEnterExitTransitionToTest: exception while waiting "
+ e.getMessage());
}
}
dumpLogRecs(smEnterExitTranstionToTest);
assertEquals(9, smEnterExitTranstionToTest.getLogRecCount());
LogRec lr;
lr = smEnterExitTranstionToTest.getLogRec(0);
assertEquals(ENTER, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
lr = smEnterExitTranstionToTest.getLogRec(1);
assertEquals(EXIT, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
lr = smEnterExitTranstionToTest.getLogRec(2);
assertEquals(ENTER, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
lr = smEnterExitTranstionToTest.getLogRec(3);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS3, lr.getDestState());
lr = smEnterExitTranstionToTest.getLogRec(4);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
assertEquals(EXIT, lr.getInfo());
lr = smEnterExitTranstionToTest.getLogRec(5);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(ENTER, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
lr = smEnterExitTranstionToTest.getLogRec(6);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(EXIT, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
lr = smEnterExitTranstionToTest.getLogRec(7);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(ENTER, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
lr = smEnterExitTranstionToTest.getLogRec(8);
assertEquals(TEST_CMD_1, lr.getWhat());
assertEquals(EXIT, lr.getInfo());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
if (smEnterExitTranstionToTest.isDbg()) {
tlog("testStateMachineEnterExitTransitionToTest X");
}
| public void | testStateMachineQuit()
if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachineQuitTest smQuitTest = new StateMachineQuitTest("smQuitTest");
smQuitTest.start();
if (smQuitTest.isDbg()) tlog("testStateMachineQuit E");
synchronized (smQuitTest) {
// Send 6 message we'll quit on the first but all 6 should be processed before quitting.
for (int i = 1; i <= 6; i++) {
smQuitTest.sendMessage(smQuitTest.obtainMessage(i));
}
try {
// wait for the messages to be handled
smQuitTest.wait();
} catch (InterruptedException e) {
tloge("testStateMachineQuit: exception while waiting " + e.getMessage());
}
}
dumpLogRecs(smQuitTest.mLogRecs);
assertEquals(8, smQuitTest.mLogRecs.size());
LogRec lr;
Iterator<LogRec> itr = smQuitTest.mLogRecs.iterator();
for (int i = 1; i <= 6; i++) {
lr = itr.next();
assertEquals(i, lr.getWhat());
assertEquals(smQuitTest.mS1, lr.getState());
assertEquals(smQuitTest.mS1, lr.getOriginalState());
}
lr = itr.next();
assertEquals(EXIT, lr.getInfo());
assertEquals(smQuitTest.mS1, lr.getState());
lr = itr.next();
assertEquals(ON_QUITTING, lr.getInfo());
if (smQuitTest.isDbg()) tlog("testStateMachineQuit X");
| public void | testStateMachineQuitNow()
if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachineQuitNowTest smQuitNowTest = new StateMachineQuitNowTest("smQuitNowTest");
smQuitNowTest.start();
if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow E");
synchronized (smQuitNowTest) {
// Send 6 message we'll QuitNow on the first even though
// we send 6 only one will be processed.
for (int i = 1; i <= 6; i++) {
smQuitNowTest.sendMessage(smQuitNowTest.obtainMessage(i));
}
try {
// wait for the messages to be handled
smQuitNowTest.wait();
} catch (InterruptedException e) {
tloge("testStateMachineQuitNow: exception while waiting " + e.getMessage());
}
}
tlog("testStateMachineQuiteNow: logRecs=" + smQuitNowTest.mLogRecs);
assertEquals(3, smQuitNowTest.mLogRecs.size());
Iterator<LogRec> itr = smQuitNowTest.mLogRecs.iterator();
LogRec lr = itr.next();
assertEquals(1, lr.getWhat());
assertEquals(smQuitNowTest.mS1, lr.getState());
assertEquals(smQuitNowTest.mS1, lr.getOriginalState());
lr = itr.next();
assertEquals(EXIT, lr.getInfo());
assertEquals(smQuitNowTest.mS1, lr.getState());
lr = itr.next();
assertEquals(ON_QUITTING, lr.getInfo());
if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow X");
| public void | testStateMachineSharedThread()
if (DBG) tlog("testStateMachineSharedThread E");
// Create and start the handler thread
HandlerThread smThread = new HandlerThread("testStateMachineSharedThread");
smThread.start();
// Create the state machines
StateMachineSharedThread sms[] = new StateMachineSharedThread[10];
for (int i = 0; i < sms.length; i++) {
sms[i] = new StateMachineSharedThread("smSharedThread",
smThread.getLooper(), sms.length);
sms[i].start();
}
synchronized (waitObject) {
// Send messages to each of the state machines
for (StateMachineSharedThread sm : sms) {
for (int i = 1; i <= 4; i++) {
sm.sendMessage(i);
}
}
// Wait for the last state machine to notify its done
try {
waitObject.wait();
} catch (InterruptedException e) {
tloge("testStateMachineSharedThread: exception while waiting "
+ e.getMessage());
}
}
for (StateMachineSharedThread sm : sms) {
assertEquals(4, sm.getLogRecCount());
for (int i = 0; i < sm.getLogRecSize(); i++) {
LogRec lr = sm.getLogRec(i);
assertEquals(i+1, lr.getWhat());
assertEquals(sm.mS1, lr.getState());
assertEquals(sm.mS1, lr.getOriginalState());
}
}
if (DBG) tlog("testStateMachineSharedThread X");
| public void | testStateMachineUnhandledMessage()
StateMachineUnhandledMessage sm = new StateMachineUnhandledMessage("smUnhandledMessage");
sm.start();
if (sm.isDbg()) tlog("testStateMachineUnhandledMessage E");
synchronized (sm) {
// Send 2 messages
for (int i = 1; i <= 2; i++) {
sm.sendMessage(i);
}
try {
// wait for the messages to be handled
sm.wait();
} catch (InterruptedException e) {
tloge("testStateMachineUnhandledMessage: exception while waiting "
+ e.getMessage());
}
}
assertEquals(2, sm.getLogRecSize());
assertEquals(2, sm.mUnhandledMessageCount);
if (sm.isDbg()) tlog("testStateMachineUnhandledMessage X");
| private void | tlog(java.lang.String s)
Log.d(TAG, s);
| private void | tloge(java.lang.String s)
Log.e(TAG, s);
|
|