blob: d29b5723065b8b0342b71c3e9bbf093df031aa21 [file] [log] [blame]
Wink Savillefc5b4802009-12-08 21:22:24 -08001/**
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Wink Saville6b888d92010-11-15 12:16:52 -080017package com.android.internal.util;
18
Wink Savilleefcc3d32013-01-30 11:21:22 -080019import java.util.Collection;
20import java.util.Iterator;
21
Wink Saville6b888d92010-11-15 12:16:52 -080022import android.os.Debug;
23import android.os.HandlerThread;
24import android.os.Looper;
25import android.os.Message;
26import android.os.SystemClock;
Wink Savillefc5b4802009-12-08 21:22:24 -080027
Abodunrinwa Tokif9017762015-07-31 18:13:30 -070028import android.test.suitebuilder.annotation.Suppress;
Wink Saville64c42ca2011-04-18 14:55:10 -070029import com.android.internal.util.State;
30import com.android.internal.util.StateMachine;
Wink Savillebbf30dfd2012-05-29 12:40:46 -070031import com.android.internal.util.StateMachine.LogRec;
Wink Savillefc5b4802009-12-08 21:22:24 -080032
Brett Chabotf76c56b2010-07-26 17:28:17 -070033import android.test.suitebuilder.annotation.MediumTest;
34import android.test.suitebuilder.annotation.SmallTest;
35import android.util.Log;
36
37import junit.framework.TestCase;
Wink Savillefc5b4802009-12-08 21:22:24 -080038
39/**
Wink Saville64c42ca2011-04-18 14:55:10 -070040 * Test for StateMachine.
Wink Savillefc5b4802009-12-08 21:22:24 -080041 */
Wink Saville64c42ca2011-04-18 14:55:10 -070042public class StateMachineTest extends TestCase {
Wink Savillebbf30dfd2012-05-29 12:40:46 -070043 private static final String ENTER = "enter";
44 private static final String EXIT = "exit";
45 private static final String ON_QUITTING = "ON_QUITTING";
46
Wink Savillefc5b4802009-12-08 21:22:24 -080047 private static final int TEST_CMD_1 = 1;
48 private static final int TEST_CMD_2 = 2;
49 private static final int TEST_CMD_3 = 3;
50 private static final int TEST_CMD_4 = 4;
51 private static final int TEST_CMD_5 = 5;
52 private static final int TEST_CMD_6 = 6;
53
54 private static final boolean DBG = true;
Wink Saville6b888d92010-11-15 12:16:52 -080055 private static final boolean WAIT_FOR_DEBUGGER = false;
Wink Saville64c42ca2011-04-18 14:55:10 -070056 private static final String TAG = "StateMachineTest";
Wink Savillefc5b4802009-12-08 21:22:24 -080057
Wink Savillebbf30dfd2012-05-29 12:40:46 -070058 private void sleep(int millis) {
59 try {
60 Thread.sleep(millis);
61 } catch(InterruptedException e) {
62 }
63 }
64
Wink Savilleefcc3d32013-01-30 11:21:22 -080065 private void dumpLogRecs(StateMachine sm) {
66 int size = sm.getLogRecSize();
67 tlog("size=" + size + " count=" + sm.getLogRecCount());
68 for (int i = 0; i < size; i++) {
69 LogRec lr = sm.getLogRec(i);
70 tlog(lr.toString());
71 }
72 }
73
74 private void dumpLogRecs(Collection<LogRec> clr) {
75 int size = clr.size();
76 tlog("size=" + size);
77 for (LogRec lr : clr) {
78 tlog(lr.toString());
79 }
80 }
81
Wink Savillefc5b4802009-12-08 21:22:24 -080082 /**
Wink Savillebbf30dfd2012-05-29 12:40:46 -070083 * Tests {@link StateMachine#quit()}.
Wink Saville1b8b98b2010-03-11 11:49:54 -080084 */
Wink Saville64c42ca2011-04-18 14:55:10 -070085 class StateMachineQuitTest extends StateMachine {
Wink Savilleefcc3d32013-01-30 11:21:22 -080086 Collection<LogRec> mLogRecs;
Wink Saville1b8b98b2010-03-11 11:49:54 -080087
88 StateMachineQuitTest(String name) {
89 super(name);
90 mThisSm = this;
91 setDbg(DBG);
92
93 // Setup state machine with 1 state
94 addState(mS1);
95
96 // Set the initial state
97 setInitialState(mS1);
98 }
99
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700100 @Override
101 public void onQuitting() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700102 tlog("onQuitting");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700103 addLogRec(ON_QUITTING);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800104 mLogRecs = mThisSm.copyLogRecs();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700105 synchronized (mThisSm) {
106 mThisSm.notifyAll();
107 }
Wink Saville1b8b98b2010-03-11 11:49:54 -0800108 }
109
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700110 class S1 extends State {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800111 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700112 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700113 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -0800114 addLogRec(EXIT);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700115 }
116 @Override
117 public boolean processMessage(Message message) {
118 switch(message.what) {
119 // Sleep and assume the other messages will be queued up.
120 case TEST_CMD_1: {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700121 tlog("TEST_CMD_1");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700122 sleep(500);
123 quit();
124 break;
125 }
126 default: {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700127 tlog("default what=" + message.what);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700128 break;
129 }
130 }
131 return HANDLED;
Wink Saville1b8b98b2010-03-11 11:49:54 -0800132 }
133 }
134
135 private StateMachineQuitTest mThisSm;
136 private S1 mS1 = new S1();
137 }
138
139 @SmallTest
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700140 public void testStateMachineQuit() throws Exception {
Wink Saville6b888d92010-11-15 12:16:52 -0800141 if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
Wink Saville1b8b98b2010-03-11 11:49:54 -0800142
143 StateMachineQuitTest smQuitTest = new StateMachineQuitTest("smQuitTest");
144 smQuitTest.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800145 if (smQuitTest.isDbg()) tlog("testStateMachineQuit E");
Wink Saville1b8b98b2010-03-11 11:49:54 -0800146
147 synchronized (smQuitTest) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700148
149 // Send 6 message we'll quit on the first but all 6 should be processed before quitting.
Wink Saville1b8b98b2010-03-11 11:49:54 -0800150 for (int i = 1; i <= 6; i++) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700151 smQuitTest.sendMessage(smQuitTest.obtainMessage(i));
Wink Saville1b8b98b2010-03-11 11:49:54 -0800152 }
153
Wink Saville1b8b98b2010-03-11 11:49:54 -0800154 try {
155 // wait for the messages to be handled
156 smQuitTest.wait();
157 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800158 tloge("testStateMachineQuit: exception while waiting " + e.getMessage());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800159 }
160 }
161
Wink Savilleefcc3d32013-01-30 11:21:22 -0800162 dumpLogRecs(smQuitTest.mLogRecs);
163 assertEquals(8, smQuitTest.mLogRecs.size());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800164
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700165 LogRec lr;
Wink Savilleefcc3d32013-01-30 11:21:22 -0800166 Iterator<LogRec> itr = smQuitTest.mLogRecs.iterator();
167 for (int i = 1; i <= 6; i++) {
168 lr = itr.next();
169 assertEquals(i, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700170 assertEquals(smQuitTest.mS1, lr.getState());
171 assertEquals(smQuitTest.mS1, lr.getOriginalState());
172 }
Wink Savilleefcc3d32013-01-30 11:21:22 -0800173 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700174 assertEquals(EXIT, lr.getInfo());
175 assertEquals(smQuitTest.mS1, lr.getState());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800176
Wink Savilleefcc3d32013-01-30 11:21:22 -0800177 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700178 assertEquals(ON_QUITTING, lr.getInfo());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800179
Wink Savilleefcc3d32013-01-30 11:21:22 -0800180 if (smQuitTest.isDbg()) tlog("testStateMachineQuit X");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700181 }
Wink Saville1b8b98b2010-03-11 11:49:54 -0800182
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700183 /**
184 * Tests {@link StateMachine#quitNow()}
185 */
186 class StateMachineQuitNowTest extends StateMachine {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800187 public Collection<LogRec> mLogRecs = null;
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700188
189 StateMachineQuitNowTest(String name) {
190 super(name);
191 mThisSm = this;
192 setDbg(DBG);
193
194 // Setup state machine with 1 state
195 addState(mS1);
196
197 // Set the initial state
198 setInitialState(mS1);
199 }
200
201 @Override
202 public void onQuitting() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700203 tlog("onQuitting");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700204 addLogRec(ON_QUITTING);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800205 // Get a copy of the log records since we're quitting and they will disappear
206 mLogRecs = mThisSm.copyLogRecs();
207
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700208 synchronized (mThisSm) {
209 mThisSm.notifyAll();
210 }
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700211 }
212
213 class S1 extends State {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800214 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700215 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700216 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -0800217 addLogRec(EXIT);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700218 }
219 @Override
220 public boolean processMessage(Message message) {
221 switch(message.what) {
222 // Sleep and assume the other messages will be queued up.
223 case TEST_CMD_1: {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700224 tlog("TEST_CMD_1");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700225 sleep(500);
226 quitNow();
227 break;
228 }
229 default: {
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700230 tlog("default what=" + message.what);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700231 break;
232 }
233 }
234 return HANDLED;
235 }
236 }
237
238 private StateMachineQuitNowTest mThisSm;
239 private S1 mS1 = new S1();
240 }
241
242 @SmallTest
243 public void testStateMachineQuitNow() throws Exception {
244 if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
245
246 StateMachineQuitNowTest smQuitNowTest = new StateMachineQuitNowTest("smQuitNowTest");
247 smQuitNowTest.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800248 if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow E");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700249
250 synchronized (smQuitNowTest) {
251
Wink Savilleefcc3d32013-01-30 11:21:22 -0800252 // Send 6 message we'll QuitNow on the first even though
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700253 // we send 6 only one will be processed.
254 for (int i = 1; i <= 6; i++) {
255 smQuitNowTest.sendMessage(smQuitNowTest.obtainMessage(i));
256 }
257
258 try {
259 // wait for the messages to be handled
260 smQuitNowTest.wait();
261 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800262 tloge("testStateMachineQuitNow: exception while waiting " + e.getMessage());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700263 }
264 }
265
Wink Savilleefcc3d32013-01-30 11:21:22 -0800266 tlog("testStateMachineQuiteNow: logRecs=" + smQuitNowTest.mLogRecs);
267 assertEquals(3, smQuitNowTest.mLogRecs.size());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700268
Wink Savilleefcc3d32013-01-30 11:21:22 -0800269 Iterator<LogRec> itr = smQuitNowTest.mLogRecs.iterator();
270 LogRec lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700271 assertEquals(1, lr.getWhat());
272 assertEquals(smQuitNowTest.mS1, lr.getState());
273 assertEquals(smQuitNowTest.mS1, lr.getOriginalState());
274
Wink Savilleefcc3d32013-01-30 11:21:22 -0800275 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700276 assertEquals(EXIT, lr.getInfo());
277 assertEquals(smQuitNowTest.mS1, lr.getState());
278
Wink Savilleefcc3d32013-01-30 11:21:22 -0800279 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700280 assertEquals(ON_QUITTING, lr.getInfo());
281
Wink Savilleefcc3d32013-01-30 11:21:22 -0800282 if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow X");
Wink Saville1b8b98b2010-03-11 11:49:54 -0800283 }
284
285 /**
Wink Savillee7be6a82010-03-18 17:03:30 -0700286 * Test enter/exit can use transitionTo
287 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700288 class StateMachineEnterExitTransitionToTest extends StateMachine {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700289
Wink Savillee7be6a82010-03-18 17:03:30 -0700290 StateMachineEnterExitTransitionToTest(String name) {
291 super(name);
292 mThisSm = this;
293 setDbg(DBG);
294
295 // Setup state machine with 1 state
296 addState(mS1);
297 addState(mS2);
298 addState(mS3);
299 addState(mS4);
300
301 // Set the initial state
302 setInitialState(mS1);
303 }
304
Wink Saville64c42ca2011-04-18 14:55:10 -0700305 class S1 extends State {
306 @Override
307 public void enter() {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700308 // Test transitions in enter on the initial state work
Wink Savilleefcc3d32013-01-30 11:21:22 -0800309 addLogRec(ENTER);
Wink Savillee7be6a82010-03-18 17:03:30 -0700310 transitionTo(mS2);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700311 tlog("S1.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700312 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700313 @Override
314 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800315 addLogRec(EXIT);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700316 tlog("S1.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700317 }
318 }
319
Wink Saville64c42ca2011-04-18 14:55:10 -0700320 class S2 extends State {
321 @Override
322 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800323 addLogRec(ENTER);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700324 tlog("S2.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700325 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700326 @Override
327 public void exit() {
Wink Savillee7be6a82010-03-18 17:03:30 -0700328 // Test transition in exit work
Wink Savillee7be6a82010-03-18 17:03:30 -0700329 transitionTo(mS4);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800330
331 assertEquals(TEST_CMD_1, getCurrentMessage().what);
332 addLogRec(EXIT);
333
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700334 tlog("S2.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700335 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700336 @Override
337 public boolean processMessage(Message message) {
Wink Savillee7be6a82010-03-18 17:03:30 -0700338 // Start a transition to S3 but it will be
Wink Savillea4f3bec2010-05-19 09:11:38 -0700339 // changed to a transition to S4 in exit
Wink Savillee7be6a82010-03-18 17:03:30 -0700340 transitionTo(mS3);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700341 tlog("S2.processMessage");
Wink Savillea4f3bec2010-05-19 09:11:38 -0700342 return HANDLED;
Wink Savillee7be6a82010-03-18 17:03:30 -0700343 }
344 }
345
Wink Saville64c42ca2011-04-18 14:55:10 -0700346 class S3 extends State {
347 @Override
348 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800349 addLogRec(ENTER);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700350 tlog("S3.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700351 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700352 @Override
353 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800354 addLogRec(EXIT);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700355 tlog("S3.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700356 }
357 }
358
Wink Saville64c42ca2011-04-18 14:55:10 -0700359 class S4 extends State {
360 @Override
361 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800362 addLogRec(ENTER);
Wink Savillee7be6a82010-03-18 17:03:30 -0700363 // Test that we can do halting in an enter/exit
364 transitionToHaltingState();
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700365 tlog("S4.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700366 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700367 @Override
368 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800369 addLogRec(EXIT);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700370 tlog("S4.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700371 }
372 }
373
374 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700375 protected void onHalting() {
Wink Savillee7be6a82010-03-18 17:03:30 -0700376 synchronized (mThisSm) {
377 mThisSm.notifyAll();
378 }
379 }
380
381 private StateMachineEnterExitTransitionToTest mThisSm;
382 private S1 mS1 = new S1();
383 private S2 mS2 = new S2();
384 private S3 mS3 = new S3();
385 private S4 mS4 = new S4();
Wink Savillee7be6a82010-03-18 17:03:30 -0700386 }
387
388 @SmallTest
389 public void testStateMachineEnterExitTransitionToTest() throws Exception {
390 //if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
391
392 StateMachineEnterExitTransitionToTest smEnterExitTranstionToTest =
393 new StateMachineEnterExitTransitionToTest("smEnterExitTranstionToTest");
394 smEnterExitTranstionToTest.start();
395 if (smEnterExitTranstionToTest.isDbg()) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800396 tlog("testStateMachineEnterExitTransitionToTest E");
Wink Savillee7be6a82010-03-18 17:03:30 -0700397 }
398
399 synchronized (smEnterExitTranstionToTest) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700400 smEnterExitTranstionToTest.sendMessage(TEST_CMD_1);
Wink Savillee7be6a82010-03-18 17:03:30 -0700401
402 try {
403 // wait for the messages to be handled
404 smEnterExitTranstionToTest.wait();
405 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800406 tloge("testStateMachineEnterExitTransitionToTest: exception while waiting "
Wink Savillee7be6a82010-03-18 17:03:30 -0700407 + e.getMessage());
408 }
409 }
410
Wink Savilleefcc3d32013-01-30 11:21:22 -0800411 dumpLogRecs(smEnterExitTranstionToTest);
Wink Savillee7be6a82010-03-18 17:03:30 -0700412
Wink Savilleefcc3d32013-01-30 11:21:22 -0800413 assertEquals(9, smEnterExitTranstionToTest.getLogRecCount());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700414 LogRec lr;
Wink Savillee7be6a82010-03-18 17:03:30 -0700415
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700416 lr = smEnterExitTranstionToTest.getLogRec(0);
417 assertEquals(ENTER, lr.getInfo());
418 assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
Wink Savillee7be6a82010-03-18 17:03:30 -0700419
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700420 lr = smEnterExitTranstionToTest.getLogRec(1);
421 assertEquals(EXIT, lr.getInfo());
422 assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
423
424 lr = smEnterExitTranstionToTest.getLogRec(2);
425 assertEquals(ENTER, lr.getInfo());
426 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
427
428 lr = smEnterExitTranstionToTest.getLogRec(3);
429 assertEquals(TEST_CMD_1, lr.getWhat());
430 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
431 assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800432 assertEquals(smEnterExitTranstionToTest.mS3, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700433
434 lr = smEnterExitTranstionToTest.getLogRec(4);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800435 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700436 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800437 assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
438 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
439 assertEquals(EXIT, lr.getInfo());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700440
441 lr = smEnterExitTranstionToTest.getLogRec(5);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800442 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700443 assertEquals(ENTER, lr.getInfo());
444 assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800445 assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
446 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700447
448 lr = smEnterExitTranstionToTest.getLogRec(6);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800449 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700450 assertEquals(EXIT, lr.getInfo());
451 assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800452 assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
453 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700454
455 lr = smEnterExitTranstionToTest.getLogRec(7);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800456 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700457 assertEquals(ENTER, lr.getInfo());
458 assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800459 assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
460 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700461
462 lr = smEnterExitTranstionToTest.getLogRec(8);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800463 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700464 assertEquals(EXIT, lr.getInfo());
465 assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800466 assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
Wink Savillee7be6a82010-03-18 17:03:30 -0700467
468 if (smEnterExitTranstionToTest.isDbg()) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800469 tlog("testStateMachineEnterExitTransitionToTest X");
Wink Savillee7be6a82010-03-18 17:03:30 -0700470 }
471 }
472
473 /**
Wink Savillefc5b4802009-12-08 21:22:24 -0800474 * Tests that ProcessedMessage works as a circular buffer.
475 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700476 class StateMachine0 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800477 StateMachine0(String name) {
478 super(name);
479 mThisSm = this;
480 setDbg(DBG);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700481 setLogRecSize(3);
Wink Savillefc5b4802009-12-08 21:22:24 -0800482
483 // Setup state machine with 1 state
484 addState(mS1);
485
486 // Set the initial state
487 setInitialState(mS1);
488 }
489
Wink Saville64c42ca2011-04-18 14:55:10 -0700490 class S1 extends State {
491 @Override
492 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800493 if (message.what == TEST_CMD_6) {
494 transitionToHaltingState();
495 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700496 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800497 }
498 }
499
500 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700501 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800502 synchronized (mThisSm) {
503 mThisSm.notifyAll();
504 }
505 }
506
507 private StateMachine0 mThisSm;
508 private S1 mS1 = new S1();
509 }
510
511 @SmallTest
512 public void testStateMachine0() throws Exception {
Wink Saville1b8b98b2010-03-11 11:49:54 -0800513 //if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
Wink Savillefc5b4802009-12-08 21:22:24 -0800514
515 StateMachine0 sm0 = new StateMachine0("sm0");
516 sm0.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800517 if (sm0.isDbg()) tlog("testStateMachine0 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800518
519 synchronized (sm0) {
520 // Send 6 messages
521 for (int i = 1; i <= 6; i++) {
522 sm0.sendMessage(sm0.obtainMessage(i));
523 }
524
525 try {
526 // wait for the messages to be handled
527 sm0.wait();
528 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800529 tloge("testStateMachine0: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800530 }
531 }
532
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700533 assertEquals(6, sm0.getLogRecCount());
534 assertEquals(3, sm0.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800535
Wink Savilleefcc3d32013-01-30 11:21:22 -0800536 dumpLogRecs(sm0);
537
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700538 LogRec lr;
539 lr = sm0.getLogRec(0);
540 assertEquals(TEST_CMD_4, lr.getWhat());
541 assertEquals(sm0.mS1, lr.getState());
542 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800543
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700544 lr = sm0.getLogRec(1);
545 assertEquals(TEST_CMD_5, lr.getWhat());
546 assertEquals(sm0.mS1, lr.getState());
547 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800548
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700549 lr = sm0.getLogRec(2);
550 assertEquals(TEST_CMD_6, lr.getWhat());
551 assertEquals(sm0.mS1, lr.getState());
552 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800553
Wink Savilleefcc3d32013-01-30 11:21:22 -0800554 if (sm0.isDbg()) tlog("testStateMachine0 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800555 }
556
557 /**
558 * This tests enter/exit and transitions to the same state.
559 * The state machine has one state, it receives two messages
560 * in state mS1. With the first message it transitions to
561 * itself which causes it to be exited and reentered.
562 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700563 class StateMachine1 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800564 StateMachine1(String name) {
565 super(name);
566 mThisSm = this;
567 setDbg(DBG);
568
569 // Setup state machine with 1 state
570 addState(mS1);
571
572 // Set the initial state
573 setInitialState(mS1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700574 if (DBG) tlog("StateMachine1: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800575 }
576
Wink Saville64c42ca2011-04-18 14:55:10 -0700577 class S1 extends State {
578 @Override
579 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800580 mEnterCount++;
581 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700582 @Override
583 public void exit() {
584 mExitCount++;
585 }
586 @Override
587 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800588 if (message.what == TEST_CMD_1) {
589 assertEquals(1, mEnterCount);
590 assertEquals(0, mExitCount);
591 transitionTo(mS1);
592 } else if (message.what == TEST_CMD_2) {
593 assertEquals(2, mEnterCount);
594 assertEquals(1, mExitCount);
595 transitionToHaltingState();
596 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700597 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800598 }
Wink Savillefc5b4802009-12-08 21:22:24 -0800599 }
600
601 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700602 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800603 synchronized (mThisSm) {
604 mThisSm.notifyAll();
605 }
606 }
607
608 private StateMachine1 mThisSm;
609 private S1 mS1 = new S1();
610
611 private int mEnterCount;
612 private int mExitCount;
613 }
614
Brett Chabotf76c56b2010-07-26 17:28:17 -0700615 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800616 public void testStateMachine1() throws Exception {
617 StateMachine1 sm1 = new StateMachine1("sm1");
618 sm1.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800619 if (sm1.isDbg()) tlog("testStateMachine1 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800620
621 synchronized (sm1) {
622 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700623 sm1.sendMessage(TEST_CMD_1);
624 sm1.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800625
626 try {
627 // wait for the messages to be handled
628 sm1.wait();
629 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800630 tloge("testStateMachine1: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800631 }
632 }
633
634 assertEquals(2, sm1.mEnterCount);
635 assertEquals(2, sm1.mExitCount);
636
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700637 assertEquals(2, sm1.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800638
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700639 LogRec lr;
640 lr = sm1.getLogRec(0);
641 assertEquals(TEST_CMD_1, lr.getWhat());
642 assertEquals(sm1.mS1, lr.getState());
643 assertEquals(sm1.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800644
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700645 lr = sm1.getLogRec(1);
646 assertEquals(TEST_CMD_2, lr.getWhat());
647 assertEquals(sm1.mS1, lr.getState());
648 assertEquals(sm1.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800649
650 assertEquals(2, sm1.mEnterCount);
651 assertEquals(2, sm1.mExitCount);
652
Wink Savilleefcc3d32013-01-30 11:21:22 -0800653 if (sm1.isDbg()) tlog("testStateMachine1 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800654 }
655
656 /**
657 * Test deferring messages and states with no parents. The state machine
658 * has two states, it receives two messages in state mS1 deferring them
659 * until what == TEST_CMD_2 and then transitions to state mS2. State
660 * mS2 then receives both of the deferred messages first TEST_CMD_1 and
661 * then TEST_CMD_2.
662 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700663 class StateMachine2 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800664 StateMachine2(String name) {
665 super(name);
666 mThisSm = this;
667 setDbg(DBG);
668
669 // Setup the hierarchy
670 addState(mS1);
671 addState(mS2);
672
673 // Set the initial state
674 setInitialState(mS1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700675 if (DBG) tlog("StateMachine2: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800676 }
677
Wink Saville64c42ca2011-04-18 14:55:10 -0700678 class S1 extends State {
679 @Override
680 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800681 mDidEnter = true;
682 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700683 @Override
684 public void exit() {
685 mDidExit = true;
686 }
687 @Override
688 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800689 deferMessage(message);
690 if (message.what == TEST_CMD_2) {
691 transitionTo(mS2);
692 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700693 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800694 }
Wink Savillefc5b4802009-12-08 21:22:24 -0800695 }
696
Wink Saville64c42ca2011-04-18 14:55:10 -0700697 class S2 extends State {
698 @Override
699 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800700 if (message.what == TEST_CMD_2) {
701 transitionToHaltingState();
702 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700703 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800704 }
705 }
706
707 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700708 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800709 synchronized (mThisSm) {
710 mThisSm.notifyAll();
711 }
712 }
713
714 private StateMachine2 mThisSm;
715 private S1 mS1 = new S1();
716 private S2 mS2 = new S2();
717
718 private boolean mDidEnter = false;
719 private boolean mDidExit = false;
720 }
721
Brett Chabotf76c56b2010-07-26 17:28:17 -0700722 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800723 public void testStateMachine2() throws Exception {
724 StateMachine2 sm2 = new StateMachine2("sm2");
725 sm2.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800726 if (sm2.isDbg()) tlog("testStateMachine2 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800727
728 synchronized (sm2) {
729 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700730 sm2.sendMessage(TEST_CMD_1);
731 sm2.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800732
733 try {
734 // wait for the messages to be handled
735 sm2.wait();
736 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800737 tloge("testStateMachine2: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800738 }
739 }
740
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700741 assertEquals(4, sm2.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800742
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700743 LogRec lr;
744 lr = sm2.getLogRec(0);
745 assertEquals(TEST_CMD_1, lr.getWhat());
746 assertEquals(sm2.mS1, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800747
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700748 lr = sm2.getLogRec(1);
749 assertEquals(TEST_CMD_2, lr.getWhat());
750 assertEquals(sm2.mS1, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800751
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700752 lr = sm2.getLogRec(2);
753 assertEquals(TEST_CMD_1, lr.getWhat());
754 assertEquals(sm2.mS2, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800755
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700756 lr = sm2.getLogRec(3);
757 assertEquals(TEST_CMD_2, lr.getWhat());
758 assertEquals(sm2.mS2, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800759
760 assertTrue(sm2.mDidEnter);
761 assertTrue(sm2.mDidExit);
762
Wink Savilleefcc3d32013-01-30 11:21:22 -0800763 if (sm2.isDbg()) tlog("testStateMachine2 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800764 }
765
766 /**
767 * Test that unhandled messages in a child are handled by the parent.
768 * When TEST_CMD_2 is received.
769 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700770 class StateMachine3 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800771 StateMachine3(String name) {
772 super(name);
773 mThisSm = this;
774 setDbg(DBG);
775
776 // Setup the simplest hierarchy of two states
777 // mParentState and mChildState.
778 // (Use indentation to help visualize hierarchy)
779 addState(mParentState);
780 addState(mChildState, mParentState);
781
782 // Set the initial state will be the child
783 setInitialState(mChildState);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700784 if (DBG) tlog("StateMachine3: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800785 }
786
Wink Saville64c42ca2011-04-18 14:55:10 -0700787 class ParentState extends State {
788 @Override
789 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800790 if (message.what == TEST_CMD_2) {
791 transitionToHaltingState();
792 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700793 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800794 }
795 }
796
Wink Saville64c42ca2011-04-18 14:55:10 -0700797 class ChildState extends State {
798 @Override
799 public boolean processMessage(Message message) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700800 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800801 }
802 }
803
804 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700805 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800806 synchronized (mThisSm) {
807 mThisSm.notifyAll();
808 }
809 }
810
811 private StateMachine3 mThisSm;
812 private ParentState mParentState = new ParentState();
813 private ChildState mChildState = new ChildState();
814 }
815
Brett Chabotf76c56b2010-07-26 17:28:17 -0700816 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800817 public void testStateMachine3() throws Exception {
818 StateMachine3 sm3 = new StateMachine3("sm3");
819 sm3.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800820 if (sm3.isDbg()) tlog("testStateMachine3 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800821
822 synchronized (sm3) {
823 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700824 sm3.sendMessage(TEST_CMD_1);
825 sm3.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800826
827 try {
828 // wait for the messages to be handled
829 sm3.wait();
830 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800831 tloge("testStateMachine3: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800832 }
833 }
834
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700835 assertEquals(2, sm3.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800836
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700837 LogRec lr;
838 lr = sm3.getLogRec(0);
839 assertEquals(TEST_CMD_1, lr.getWhat());
840 assertEquals(sm3.mParentState, lr.getState());
841 assertEquals(sm3.mChildState, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800842
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700843 lr = sm3.getLogRec(1);
844 assertEquals(TEST_CMD_2, lr.getWhat());
845 assertEquals(sm3.mParentState, lr.getState());
846 assertEquals(sm3.mChildState, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800847
Wink Savilleefcc3d32013-01-30 11:21:22 -0800848 if (sm3.isDbg()) tlog("testStateMachine3 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800849 }
850
851 /**
852 * Test a hierarchy of 3 states a parent and two children
853 * with transition from child 1 to child 2 and child 2
854 * lets the parent handle the messages.
855 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700856 class StateMachine4 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800857 StateMachine4(String name) {
858 super(name);
859 mThisSm = this;
860 setDbg(DBG);
861
862 // Setup a hierarchy of three states
863 // mParentState, mChildState1 & mChildState2
864 // (Use indentation to help visualize hierarchy)
865 addState(mParentState);
866 addState(mChildState1, mParentState);
867 addState(mChildState2, mParentState);
868
869 // Set the initial state will be child 1
870 setInitialState(mChildState1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700871 if (DBG) tlog("StateMachine4: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800872 }
873
Wink Saville64c42ca2011-04-18 14:55:10 -0700874 class ParentState extends State {
875 @Override
876 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800877 if (message.what == TEST_CMD_2) {
878 transitionToHaltingState();
879 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700880 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800881 }
882 }
883
Wink Saville64c42ca2011-04-18 14:55:10 -0700884 class ChildState1 extends State {
885 @Override
886 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800887 transitionTo(mChildState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -0700888 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800889 }
890 }
891
Wink Saville64c42ca2011-04-18 14:55:10 -0700892 class ChildState2 extends State {
893 @Override
894 public boolean processMessage(Message message) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700895 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800896 }
897 }
898
899 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700900 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800901 synchronized (mThisSm) {
902 mThisSm.notifyAll();
903 }
904 }
905
906 private StateMachine4 mThisSm;
907 private ParentState mParentState = new ParentState();
908 private ChildState1 mChildState1 = new ChildState1();
909 private ChildState2 mChildState2 = new ChildState2();
910 }
911
Brett Chabotf76c56b2010-07-26 17:28:17 -0700912 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800913 public void testStateMachine4() throws Exception {
914 StateMachine4 sm4 = new StateMachine4("sm4");
915 sm4.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800916 if (sm4.isDbg()) tlog("testStateMachine4 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800917
918 synchronized (sm4) {
919 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700920 sm4.sendMessage(TEST_CMD_1);
921 sm4.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800922
923 try {
924 // wait for the messages to be handled
925 sm4.wait();
926 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800927 tloge("testStateMachine4: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800928 }
929 }
930
931
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700932 assertEquals(2, sm4.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800933
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700934 LogRec lr;
935 lr = sm4.getLogRec(0);
936 assertEquals(TEST_CMD_1, lr.getWhat());
937 assertEquals(sm4.mChildState1, lr.getState());
938 assertEquals(sm4.mChildState1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800939
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700940 lr = sm4.getLogRec(1);
941 assertEquals(TEST_CMD_2, lr.getWhat());
942 assertEquals(sm4.mParentState, lr.getState());
943 assertEquals(sm4.mChildState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800944
Wink Savilleefcc3d32013-01-30 11:21:22 -0800945 if (sm4.isDbg()) tlog("testStateMachine4 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800946 }
947
948 /**
949 * Test transition from one child to another of a "complex"
950 * hierarchy with two parents and multiple children.
951 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700952 class StateMachine5 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800953 StateMachine5(String name) {
954 super(name);
955 mThisSm = this;
956 setDbg(DBG);
957
958 // Setup a hierarchy with two parents and some children.
959 // (Use indentation to help visualize hierarchy)
960 addState(mParentState1);
961 addState(mChildState1, mParentState1);
962 addState(mChildState2, mParentState1);
963
964 addState(mParentState2);
965 addState(mChildState3, mParentState2);
966 addState(mChildState4, mParentState2);
967 addState(mChildState5, mChildState4);
968
969 // Set the initial state will be the child
970 setInitialState(mChildState1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -0700971 if (DBG) tlog("StateMachine5: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800972 }
973
Wink Saville64c42ca2011-04-18 14:55:10 -0700974 class ParentState1 extends State {
975 @Override
976 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800977 mParentState1EnterCount += 1;
978 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700979 @Override
980 public void exit() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800981 mParentState1ExitCount += 1;
982 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700983 @Override
984 public boolean processMessage(Message message) {
985 return HANDLED;
986 }
Wink Savillefc5b4802009-12-08 21:22:24 -0800987 }
988
Wink Saville64c42ca2011-04-18 14:55:10 -0700989 class ChildState1 extends State {
990 @Override
991 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800992 mChildState1EnterCount += 1;
993 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700994 @Override
995 public void exit() {
996 mChildState1ExitCount += 1;
997 }
998 @Override
999 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001000 assertEquals(1, mParentState1EnterCount);
1001 assertEquals(0, mParentState1ExitCount);
1002 assertEquals(1, mChildState1EnterCount);
1003 assertEquals(0, mChildState1ExitCount);
1004 assertEquals(0, mChildState2EnterCount);
1005 assertEquals(0, mChildState2ExitCount);
1006 assertEquals(0, mParentState2EnterCount);
1007 assertEquals(0, mParentState2ExitCount);
1008 assertEquals(0, mChildState3EnterCount);
1009 assertEquals(0, mChildState3ExitCount);
1010 assertEquals(0, mChildState4EnterCount);
1011 assertEquals(0, mChildState4ExitCount);
1012 assertEquals(0, mChildState5EnterCount);
1013 assertEquals(0, mChildState5ExitCount);
1014
1015 transitionTo(mChildState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001016 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001017 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001018 }
1019
Wink Saville64c42ca2011-04-18 14:55:10 -07001020 class ChildState2 extends State {
1021 @Override
1022 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001023 mChildState2EnterCount += 1;
1024 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001025 @Override
1026 public void exit() {
1027 mChildState2ExitCount += 1;
1028 }
1029 @Override
1030 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001031 assertEquals(1, mParentState1EnterCount);
1032 assertEquals(0, mParentState1ExitCount);
1033 assertEquals(1, mChildState1EnterCount);
1034 assertEquals(1, mChildState1ExitCount);
1035 assertEquals(1, mChildState2EnterCount);
1036 assertEquals(0, mChildState2ExitCount);
1037 assertEquals(0, mParentState2EnterCount);
1038 assertEquals(0, mParentState2ExitCount);
1039 assertEquals(0, mChildState3EnterCount);
1040 assertEquals(0, mChildState3ExitCount);
1041 assertEquals(0, mChildState4EnterCount);
1042 assertEquals(0, mChildState4ExitCount);
1043 assertEquals(0, mChildState5EnterCount);
1044 assertEquals(0, mChildState5ExitCount);
1045
1046 transitionTo(mChildState5);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001047 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001048 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001049 }
1050
Wink Saville64c42ca2011-04-18 14:55:10 -07001051 class ParentState2 extends State {
1052 @Override
1053 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001054 mParentState2EnterCount += 1;
1055 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001056 @Override
1057 public void exit() {
1058 mParentState2ExitCount += 1;
1059 }
1060 @Override
1061 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001062 assertEquals(1, mParentState1EnterCount);
1063 assertEquals(1, mParentState1ExitCount);
1064 assertEquals(1, mChildState1EnterCount);
1065 assertEquals(1, mChildState1ExitCount);
1066 assertEquals(1, mChildState2EnterCount);
1067 assertEquals(1, mChildState2ExitCount);
1068 assertEquals(2, mParentState2EnterCount);
1069 assertEquals(1, mParentState2ExitCount);
1070 assertEquals(1, mChildState3EnterCount);
1071 assertEquals(1, mChildState3ExitCount);
1072 assertEquals(2, mChildState4EnterCount);
1073 assertEquals(2, mChildState4ExitCount);
1074 assertEquals(1, mChildState5EnterCount);
1075 assertEquals(1, mChildState5ExitCount);
1076
1077 transitionToHaltingState();
Wink Savillea4f3bec2010-05-19 09:11:38 -07001078 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001079 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001080 }
1081
Wink Saville64c42ca2011-04-18 14:55:10 -07001082 class ChildState3 extends State {
1083 @Override
1084 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001085 mChildState3EnterCount += 1;
1086 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001087 @Override
1088 public void exit() {
1089 mChildState3ExitCount += 1;
1090 }
1091 @Override
1092 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001093 assertEquals(1, mParentState1EnterCount);
1094 assertEquals(1, mParentState1ExitCount);
1095 assertEquals(1, mChildState1EnterCount);
1096 assertEquals(1, mChildState1ExitCount);
1097 assertEquals(1, mChildState2EnterCount);
1098 assertEquals(1, mChildState2ExitCount);
1099 assertEquals(1, mParentState2EnterCount);
1100 assertEquals(0, mParentState2ExitCount);
1101 assertEquals(1, mChildState3EnterCount);
1102 assertEquals(0, mChildState3ExitCount);
1103 assertEquals(1, mChildState4EnterCount);
1104 assertEquals(1, mChildState4ExitCount);
1105 assertEquals(1, mChildState5EnterCount);
1106 assertEquals(1, mChildState5ExitCount);
1107
1108 transitionTo(mChildState4);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001109 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001110 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001111 }
1112
Wink Saville64c42ca2011-04-18 14:55:10 -07001113 class ChildState4 extends State {
1114 @Override
1115 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001116 mChildState4EnterCount += 1;
1117 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001118 @Override
1119 public void exit() {
1120 mChildState4ExitCount += 1;
1121 }
1122 @Override
1123 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001124 assertEquals(1, mParentState1EnterCount);
1125 assertEquals(1, mParentState1ExitCount);
1126 assertEquals(1, mChildState1EnterCount);
1127 assertEquals(1, mChildState1ExitCount);
1128 assertEquals(1, mChildState2EnterCount);
1129 assertEquals(1, mChildState2ExitCount);
1130 assertEquals(1, mParentState2EnterCount);
1131 assertEquals(0, mParentState2ExitCount);
1132 assertEquals(1, mChildState3EnterCount);
1133 assertEquals(1, mChildState3ExitCount);
1134 assertEquals(2, mChildState4EnterCount);
1135 assertEquals(1, mChildState4ExitCount);
1136 assertEquals(1, mChildState5EnterCount);
1137 assertEquals(1, mChildState5ExitCount);
1138
1139 transitionTo(mParentState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001140 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001141 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001142 }
1143
Wink Saville64c42ca2011-04-18 14:55:10 -07001144 class ChildState5 extends State {
1145 @Override
1146 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001147 mChildState5EnterCount += 1;
1148 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001149 @Override
1150 public void exit() {
1151 mChildState5ExitCount += 1;
1152 }
1153 @Override
1154 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001155 assertEquals(1, mParentState1EnterCount);
1156 assertEquals(1, mParentState1ExitCount);
1157 assertEquals(1, mChildState1EnterCount);
1158 assertEquals(1, mChildState1ExitCount);
1159 assertEquals(1, mChildState2EnterCount);
1160 assertEquals(1, mChildState2ExitCount);
1161 assertEquals(1, mParentState2EnterCount);
1162 assertEquals(0, mParentState2ExitCount);
1163 assertEquals(0, mChildState3EnterCount);
1164 assertEquals(0, mChildState3ExitCount);
1165 assertEquals(1, mChildState4EnterCount);
1166 assertEquals(0, mChildState4ExitCount);
1167 assertEquals(1, mChildState5EnterCount);
1168 assertEquals(0, mChildState5ExitCount);
1169
1170 transitionTo(mChildState3);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001171 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001172 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001173 }
1174
1175 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001176 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001177 synchronized (mThisSm) {
1178 mThisSm.notifyAll();
1179 }
1180 }
1181
1182 private StateMachine5 mThisSm;
1183 private ParentState1 mParentState1 = new ParentState1();
1184 private ChildState1 mChildState1 = new ChildState1();
1185 private ChildState2 mChildState2 = new ChildState2();
1186 private ParentState2 mParentState2 = new ParentState2();
1187 private ChildState3 mChildState3 = new ChildState3();
1188 private ChildState4 mChildState4 = new ChildState4();
1189 private ChildState5 mChildState5 = new ChildState5();
1190
1191 private int mParentState1EnterCount = 0;
1192 private int mParentState1ExitCount = 0;
1193 private int mChildState1EnterCount = 0;
1194 private int mChildState1ExitCount = 0;
1195 private int mChildState2EnterCount = 0;
1196 private int mChildState2ExitCount = 0;
1197 private int mParentState2EnterCount = 0;
1198 private int mParentState2ExitCount = 0;
1199 private int mChildState3EnterCount = 0;
1200 private int mChildState3ExitCount = 0;
1201 private int mChildState4EnterCount = 0;
1202 private int mChildState4ExitCount = 0;
1203 private int mChildState5EnterCount = 0;
1204 private int mChildState5ExitCount = 0;
1205 }
1206
Brett Chabotf76c56b2010-07-26 17:28:17 -07001207 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001208 public void testStateMachine5() throws Exception {
1209 StateMachine5 sm5 = new StateMachine5("sm5");
1210 sm5.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001211 if (sm5.isDbg()) tlog("testStateMachine5 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001212
1213 synchronized (sm5) {
1214 // Send 6 messages
Wink Saville91fbd562010-03-17 17:12:43 -07001215 sm5.sendMessage(TEST_CMD_1);
1216 sm5.sendMessage(TEST_CMD_2);
1217 sm5.sendMessage(TEST_CMD_3);
1218 sm5.sendMessage(TEST_CMD_4);
1219 sm5.sendMessage(TEST_CMD_5);
1220 sm5.sendMessage(TEST_CMD_6);
Wink Savillefc5b4802009-12-08 21:22:24 -08001221
1222 try {
1223 // wait for the messages to be handled
1224 sm5.wait();
1225 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001226 tloge("testStateMachine5: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001227 }
1228 }
1229
1230
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001231 assertEquals(6, sm5.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -08001232
1233 assertEquals(1, sm5.mParentState1EnterCount);
1234 assertEquals(1, sm5.mParentState1ExitCount);
1235 assertEquals(1, sm5.mChildState1EnterCount);
1236 assertEquals(1, sm5.mChildState1ExitCount);
1237 assertEquals(1, sm5.mChildState2EnterCount);
1238 assertEquals(1, sm5.mChildState2ExitCount);
1239 assertEquals(2, sm5.mParentState2EnterCount);
1240 assertEquals(2, sm5.mParentState2ExitCount);
1241 assertEquals(1, sm5.mChildState3EnterCount);
1242 assertEquals(1, sm5.mChildState3ExitCount);
1243 assertEquals(2, sm5.mChildState4EnterCount);
1244 assertEquals(2, sm5.mChildState4ExitCount);
1245 assertEquals(1, sm5.mChildState5EnterCount);
1246 assertEquals(1, sm5.mChildState5ExitCount);
1247
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001248 LogRec lr;
1249 lr = sm5.getLogRec(0);
1250 assertEquals(TEST_CMD_1, lr.getWhat());
1251 assertEquals(sm5.mChildState1, lr.getState());
1252 assertEquals(sm5.mChildState1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001253
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001254 lr = sm5.getLogRec(1);
1255 assertEquals(TEST_CMD_2, lr.getWhat());
1256 assertEquals(sm5.mChildState2, lr.getState());
1257 assertEquals(sm5.mChildState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001258
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001259 lr = sm5.getLogRec(2);
1260 assertEquals(TEST_CMD_3, lr.getWhat());
1261 assertEquals(sm5.mChildState5, lr.getState());
1262 assertEquals(sm5.mChildState5, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001263
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001264 lr = sm5.getLogRec(3);
1265 assertEquals(TEST_CMD_4, lr.getWhat());
1266 assertEquals(sm5.mChildState3, lr.getState());
1267 assertEquals(sm5.mChildState3, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001268
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001269 lr = sm5.getLogRec(4);
1270 assertEquals(TEST_CMD_5, lr.getWhat());
1271 assertEquals(sm5.mChildState4, lr.getState());
1272 assertEquals(sm5.mChildState4, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001273
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001274 lr = sm5.getLogRec(5);
1275 assertEquals(TEST_CMD_6, lr.getWhat());
1276 assertEquals(sm5.mParentState2, lr.getState());
1277 assertEquals(sm5.mParentState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001278
Wink Savilleefcc3d32013-01-30 11:21:22 -08001279 if (sm5.isDbg()) tlog("testStateMachine5 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001280 }
1281
1282 /**
1283 * Test that the initial state enter is invoked immediately
1284 * after construction and before any other messages arrive and that
1285 * sendMessageDelayed works.
1286 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001287 class StateMachine6 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001288 StateMachine6(String name) {
1289 super(name);
1290 mThisSm = this;
1291 setDbg(DBG);
1292
1293 // Setup state machine with 1 state
1294 addState(mS1);
1295
1296 // Set the initial state
1297 setInitialState(mS1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001298 if (DBG) tlog("StateMachine6: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001299 }
1300
Wink Saville64c42ca2011-04-18 14:55:10 -07001301 class S1 extends State {
1302 @Override
1303 public void enter() {
Wink Saville91fbd562010-03-17 17:12:43 -07001304 sendMessage(TEST_CMD_1);
Wink Savillefc5b4802009-12-08 21:22:24 -08001305 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001306 @Override
1307 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001308 if (message.what == TEST_CMD_1) {
1309 mArrivalTimeMsg1 = SystemClock.elapsedRealtime();
1310 } else if (message.what == TEST_CMD_2) {
1311 mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
1312 transitionToHaltingState();
1313 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001314 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001315 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001316 }
1317
1318 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001319 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001320 synchronized (mThisSm) {
1321 mThisSm.notifyAll();
1322 }
1323 }
1324
1325 private StateMachine6 mThisSm;
1326 private S1 mS1 = new S1();
1327
1328 private long mArrivalTimeMsg1;
1329 private long mArrivalTimeMsg2;
1330 }
1331
Brett Chabotf76c56b2010-07-26 17:28:17 -07001332 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001333 public void testStateMachine6() throws Exception {
Wink Savillefc5b4802009-12-08 21:22:24 -08001334 final int DELAY_TIME = 250;
1335 final int DELAY_FUDGE = 20;
1336
1337 StateMachine6 sm6 = new StateMachine6("sm6");
1338 sm6.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001339 if (sm6.isDbg()) tlog("testStateMachine6 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001340
1341 synchronized (sm6) {
1342 // Send a message
Wink Saville91fbd562010-03-17 17:12:43 -07001343 sm6.sendMessageDelayed(TEST_CMD_2, DELAY_TIME);
Wink Savillefc5b4802009-12-08 21:22:24 -08001344
1345 try {
1346 // wait for the messages to be handled
1347 sm6.wait();
1348 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001349 tloge("testStateMachine6: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001350 }
1351 }
1352
1353 /**
1354 * TEST_CMD_1 was sent in enter and must always have been processed
1355 * immediately after construction and hence the arrival time difference
1356 * should always >= to the DELAY_TIME
1357 */
1358 long arrivalTimeDiff = sm6.mArrivalTimeMsg2 - sm6.mArrivalTimeMsg1;
1359 long expectedDelay = DELAY_TIME - DELAY_FUDGE;
Wink Savilleefcc3d32013-01-30 11:21:22 -08001360 if (sm6.isDbg()) tlog("testStateMachine6: expect " + arrivalTimeDiff
Wink Savillefc5b4802009-12-08 21:22:24 -08001361 + " >= " + expectedDelay);
1362 assertTrue(arrivalTimeDiff >= expectedDelay);
1363
Wink Savilleefcc3d32013-01-30 11:21:22 -08001364 if (sm6.isDbg()) tlog("testStateMachine6 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001365 }
1366
1367 /**
1368 * Test that enter is invoked immediately after exit. This validates
1369 * that enter can be used to send a watch dog message for its state.
1370 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001371 class StateMachine7 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001372 private final int SM7_DELAY_TIME = 250;
1373
1374 StateMachine7(String name) {
1375 super(name);
1376 mThisSm = this;
1377 setDbg(DBG);
1378
1379 // Setup state machine with 1 state
1380 addState(mS1);
1381 addState(mS2);
1382
1383 // Set the initial state
1384 setInitialState(mS1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001385 if (DBG) tlog("StateMachine7: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001386 }
1387
Wink Saville64c42ca2011-04-18 14:55:10 -07001388 class S1 extends State {
1389 @Override
1390 public void exit() {
1391 sendMessage(TEST_CMD_2);
1392 }
1393 @Override
1394 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001395 transitionTo(mS2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001396 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001397 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001398 }
1399
Wink Saville64c42ca2011-04-18 14:55:10 -07001400 class S2 extends State {
1401 @Override
1402 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001403 // Send a delayed message as a watch dog
Wink Saville91fbd562010-03-17 17:12:43 -07001404 sendMessageDelayed(TEST_CMD_3, SM7_DELAY_TIME);
Wink Savillefc5b4802009-12-08 21:22:24 -08001405 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001406 @Override
1407 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001408 if (message.what == TEST_CMD_2) {
1409 mMsgCount += 1;
1410 mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
1411 } else if (message.what == TEST_CMD_3) {
1412 mMsgCount += 1;
1413 mArrivalTimeMsg3 = SystemClock.elapsedRealtime();
1414 }
1415
1416 if (mMsgCount == 2) {
1417 transitionToHaltingState();
1418 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001419 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001420 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001421 }
1422
1423 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001424 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001425 synchronized (mThisSm) {
1426 mThisSm.notifyAll();
1427 }
1428 }
1429
1430 private StateMachine7 mThisSm;
1431 private S1 mS1 = new S1();
1432 private S2 mS2 = new S2();
1433
1434 private int mMsgCount = 0;
1435 private long mArrivalTimeMsg2;
1436 private long mArrivalTimeMsg3;
1437 }
1438
Brett Chabotf76c56b2010-07-26 17:28:17 -07001439 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001440 public void testStateMachine7() throws Exception {
Wink Savillefc5b4802009-12-08 21:22:24 -08001441 final int SM7_DELAY_FUDGE = 20;
1442
1443 StateMachine7 sm7 = new StateMachine7("sm7");
1444 sm7.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001445 if (sm7.isDbg()) tlog("testStateMachine7 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001446
1447 synchronized (sm7) {
1448 // Send a message
Wink Saville91fbd562010-03-17 17:12:43 -07001449 sm7.sendMessage(TEST_CMD_1);
Wink Savillefc5b4802009-12-08 21:22:24 -08001450
1451 try {
1452 // wait for the messages to be handled
1453 sm7.wait();
1454 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001455 tloge("testStateMachine7: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001456 }
1457 }
1458
1459 /**
1460 * TEST_CMD_3 was sent in S2.enter with a delay and must always have been
1461 * processed immediately after S1.exit. Since S1.exit sent TEST_CMD_2
1462 * without a delay the arrival time difference should always >= to SM7_DELAY_TIME.
1463 */
1464 long arrivalTimeDiff = sm7.mArrivalTimeMsg3 - sm7.mArrivalTimeMsg2;
1465 long expectedDelay = sm7.SM7_DELAY_TIME - SM7_DELAY_FUDGE;
Wink Savilleefcc3d32013-01-30 11:21:22 -08001466 if (sm7.isDbg()) tlog("testStateMachine7: expect " + arrivalTimeDiff
Wink Savillefc5b4802009-12-08 21:22:24 -08001467 + " >= " + expectedDelay);
1468 assertTrue(arrivalTimeDiff >= expectedDelay);
1469
Wink Savilleefcc3d32013-01-30 11:21:22 -08001470 if (sm7.isDbg()) tlog("testStateMachine7 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001471 }
1472
1473 /**
1474 * Test unhandledMessage.
1475 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001476 class StateMachineUnhandledMessage extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001477 StateMachineUnhandledMessage(String name) {
1478 super(name);
1479 mThisSm = this;
1480 setDbg(DBG);
1481
1482 // Setup state machine with 1 state
1483 addState(mS1);
1484
1485 // Set the initial state
1486 setInitialState(mS1);
1487 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001488 @Override
1489 public void unhandledMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001490 mUnhandledMessageCount += 1;
1491 }
1492
Wink Saville64c42ca2011-04-18 14:55:10 -07001493 class S1 extends State {
1494 @Override
1495 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001496 if (message.what == TEST_CMD_2) {
1497 transitionToHaltingState();
1498 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001499 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001500 }
1501 }
1502
1503 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001504 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001505 synchronized (mThisSm) {
1506 mThisSm.notifyAll();
1507 }
1508 }
1509
1510 private StateMachineUnhandledMessage mThisSm;
1511 private int mUnhandledMessageCount;
1512 private S1 mS1 = new S1();
1513 }
1514
1515 @SmallTest
1516 public void testStateMachineUnhandledMessage() throws Exception {
1517
Wink Savilleefcc3d32013-01-30 11:21:22 -08001518 StateMachineUnhandledMessage sm = new StateMachineUnhandledMessage("smUnhandledMessage");
Wink Savillefc5b4802009-12-08 21:22:24 -08001519 sm.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001520 if (sm.isDbg()) tlog("testStateMachineUnhandledMessage E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001521
1522 synchronized (sm) {
1523 // Send 2 messages
1524 for (int i = 1; i <= 2; i++) {
Wink Saville91fbd562010-03-17 17:12:43 -07001525 sm.sendMessage(i);
Wink Savillefc5b4802009-12-08 21:22:24 -08001526 }
1527
1528 try {
1529 // wait for the messages to be handled
1530 sm.wait();
1531 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001532 tloge("testStateMachineUnhandledMessage: exception while waiting "
Wink Savillefc5b4802009-12-08 21:22:24 -08001533 + e.getMessage());
1534 }
1535 }
1536
Wink Savilleefcc3d32013-01-30 11:21:22 -08001537 assertEquals(2, sm.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -08001538 assertEquals(2, sm.mUnhandledMessageCount);
1539
Wink Savilleefcc3d32013-01-30 11:21:22 -08001540 if (sm.isDbg()) tlog("testStateMachineUnhandledMessage X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001541 }
1542
1543 /**
1544 * Test state machines sharing the same thread/looper. Multiple instances
1545 * of the same state machine will be created. They will all share the
1546 * same thread and thus each can update <code>sharedCounter</code> which
1547 * will be used to notify testStateMachineSharedThread that the test is
1548 * complete.
1549 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001550 class StateMachineSharedThread extends StateMachine {
Wink Savillef0f566e2010-03-11 14:03:50 -08001551 StateMachineSharedThread(String name, Looper looper, int maxCount) {
1552 super(name, looper);
Wink Savillefc5b4802009-12-08 21:22:24 -08001553 mMaxCount = maxCount;
1554 setDbg(DBG);
1555
1556 // Setup state machine with 1 state
1557 addState(mS1);
1558
1559 // Set the initial state
1560 setInitialState(mS1);
1561 }
1562
Wink Saville64c42ca2011-04-18 14:55:10 -07001563 class S1 extends State {
1564 @Override
1565 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001566 if (message.what == TEST_CMD_4) {
1567 transitionToHaltingState();
1568 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001569 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001570 }
1571 }
1572
1573 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001574 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001575 // Update the shared counter, which is OK since all state
1576 // machines are using the same thread.
1577 sharedCounter += 1;
1578 if (sharedCounter == mMaxCount) {
1579 synchronized (waitObject) {
1580 waitObject.notifyAll();
1581 }
1582 }
1583 }
1584
1585 private int mMaxCount;
1586 private S1 mS1 = new S1();
1587 }
1588 private static int sharedCounter = 0;
1589 private static Object waitObject = new Object();
1590
Brett Chabotf76c56b2010-07-26 17:28:17 -07001591 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001592 public void testStateMachineSharedThread() throws Exception {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001593 if (DBG) tlog("testStateMachineSharedThread E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001594
1595 // Create and start the handler thread
1596 HandlerThread smThread = new HandlerThread("testStateMachineSharedThread");
1597 smThread.start();
1598
1599 // Create the state machines
1600 StateMachineSharedThread sms[] = new StateMachineSharedThread[10];
1601 for (int i = 0; i < sms.length; i++) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001602 sms[i] = new StateMachineSharedThread("smSharedThread",
1603 smThread.getLooper(), sms.length);
Wink Savillefc5b4802009-12-08 21:22:24 -08001604 sms[i].start();
1605 }
1606
1607 synchronized (waitObject) {
1608 // Send messages to each of the state machines
1609 for (StateMachineSharedThread sm : sms) {
1610 for (int i = 1; i <= 4; i++) {
Wink Saville91fbd562010-03-17 17:12:43 -07001611 sm.sendMessage(i);
Wink Savillefc5b4802009-12-08 21:22:24 -08001612 }
1613 }
1614
1615 // Wait for the last state machine to notify its done
1616 try {
1617 waitObject.wait();
1618 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001619 tloge("testStateMachineSharedThread: exception while waiting "
Wink Savillefc5b4802009-12-08 21:22:24 -08001620 + e.getMessage());
1621 }
1622 }
1623
1624 for (StateMachineSharedThread sm : sms) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001625 assertEquals(4, sm.getLogRecCount());
1626 for (int i = 0; i < sm.getLogRecSize(); i++) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001627 LogRec lr = sm.getLogRec(i);
1628 assertEquals(i+1, lr.getWhat());
1629 assertEquals(sm.mS1, lr.getState());
1630 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001631 }
1632 }
1633
Wink Savilleefcc3d32013-01-30 11:21:22 -08001634 if (DBG) tlog("testStateMachineSharedThread X");
1635 }
1636
1637 static class Hsm1 extends StateMachine {
1638 private static final String HSM1_TAG = "hsm1";
1639
1640 public static final int CMD_1 = 1;
1641 public static final int CMD_2 = 2;
1642 public static final int CMD_3 = 3;
1643 public static final int CMD_4 = 4;
1644 public static final int CMD_5 = 5;
1645
1646 public static Hsm1 makeHsm1() {
1647 Log.d(HSM1_TAG, "makeHsm1 E");
1648 Hsm1 sm = new Hsm1(HSM1_TAG);
1649 sm.start();
1650 Log.d(HSM1_TAG, "makeHsm1 X");
1651 return sm;
1652 }
1653
1654 Hsm1(String name) {
1655 super(name);
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001656 tlog("ctor E");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001657
1658 // Add states, use indentation to show hierarchy
1659 addState(mP1);
1660 addState(mS1, mP1);
1661 addState(mS2, mP1);
1662 addState(mP2);
1663
1664 // Set the initial state
1665 setInitialState(mS1);
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001666 tlog("ctor X");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001667 }
1668
1669 class P1 extends State {
1670 @Override
1671 public void enter() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001672 tlog("P1.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001673 }
1674 @Override
1675 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001676 tlog("P1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001677 }
1678 @Override
1679 public boolean processMessage(Message message) {
1680 boolean retVal;
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001681 tlog("P1.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001682 switch(message.what) {
1683 case CMD_2:
1684 // CMD_2 will arrive in mS2 before CMD_3
1685 sendMessage(CMD_3);
1686 deferMessage(message);
1687 transitionTo(mS2);
1688 retVal = true;
1689 break;
1690 default:
1691 // Any message we don't understand in this state invokes unhandledMessage
1692 retVal = false;
1693 break;
1694 }
1695 return retVal;
1696 }
1697 }
1698
1699 class S1 extends State {
1700 @Override
1701 public void enter() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001702 tlog("S1.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001703 }
1704 @Override
1705 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001706 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001707 }
1708 @Override
1709 public boolean processMessage(Message message) {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001710 tlog("S1.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001711 if (message.what == CMD_1) {
1712 // Transition to ourself to show that enter/exit is called
1713 transitionTo(mS1);
1714 return HANDLED;
1715 } else {
1716 // Let parent process all other messages
1717 return NOT_HANDLED;
1718 }
1719 }
1720 }
1721
1722 class S2 extends State {
1723 @Override
1724 public void enter() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001725 tlog("S2.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001726 }
1727 @Override
1728 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001729 tlog("S2.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001730 }
1731 @Override
1732 public boolean processMessage(Message message) {
1733 boolean retVal;
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001734 tlog("S2.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001735 switch(message.what) {
1736 case(CMD_2):
1737 sendMessage(CMD_4);
1738 retVal = true;
1739 break;
1740 case(CMD_3):
1741 deferMessage(message);
1742 transitionTo(mP2);
1743 retVal = true;
1744 break;
1745 default:
1746 retVal = false;
1747 break;
1748 }
1749 return retVal;
1750 }
1751 }
1752
1753 class P2 extends State {
1754 @Override
1755 public void enter() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001756 tlog("P2.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001757 sendMessage(CMD_5);
1758 }
1759 @Override
1760 public void exit() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001761 tlog("P2.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001762 }
1763 @Override
1764 public boolean processMessage(Message message) {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001765 tlog("P2.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001766 switch(message.what) {
1767 case(CMD_3):
1768 break;
1769 case(CMD_4):
1770 break;
1771 case(CMD_5):
1772 transitionToHaltingState();
1773 break;
1774 }
1775 return HANDLED;
1776 }
1777 }
1778
1779 @Override
1780 protected void onHalting() {
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001781 tlog("halting");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001782 synchronized (this) {
1783 this.notifyAll();
1784 }
1785 }
1786
1787 P1 mP1 = new P1();
1788 S1 mS1 = new S1();
1789 S2 mS2 = new S2();
1790 P2 mP2 = new P2();
Wink Savillefc5b4802009-12-08 21:22:24 -08001791 }
1792
Brett Chabotf76c56b2010-07-26 17:28:17 -07001793 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001794 public void testHsm1() throws Exception {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001795 if (DBG) tlog("testHsm1 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001796
1797 Hsm1 sm = Hsm1.makeHsm1();
1798
1799 // Send messages
Wink Saville91fbd562010-03-17 17:12:43 -07001800 sm.sendMessage(Hsm1.CMD_1);
1801 sm.sendMessage(Hsm1.CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -08001802
1803 synchronized (sm) {
1804 // Wait for the last state machine to notify its done
1805 try {
1806 sm.wait();
1807 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001808 tloge("testHsm1: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001809 }
1810 }
1811
Wink Savilleefcc3d32013-01-30 11:21:22 -08001812 dumpLogRecs(sm);
1813
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001814 assertEquals(7, sm.getLogRecCount());
Wink Savilleefcc3d32013-01-30 11:21:22 -08001815
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001816 LogRec lr = sm.getLogRec(0);
1817 assertEquals(Hsm1.CMD_1, lr.getWhat());
1818 assertEquals(sm.mS1, lr.getState());
1819 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001820
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001821 lr = sm.getLogRec(1);
1822 assertEquals(Hsm1.CMD_2, lr.getWhat());
1823 assertEquals(sm.mP1, lr.getState());
1824 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001825
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001826 lr = sm.getLogRec(2);
1827 assertEquals(Hsm1.CMD_2, lr.getWhat());
1828 assertEquals(sm.mS2, lr.getState());
1829 assertEquals(sm.mS2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001830
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001831 lr = sm.getLogRec(3);
1832 assertEquals(Hsm1.CMD_3, lr.getWhat());
1833 assertEquals(sm.mS2, lr.getState());
1834 assertEquals(sm.mS2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001835
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001836 lr = sm.getLogRec(4);
1837 assertEquals(Hsm1.CMD_3, lr.getWhat());
1838 assertEquals(sm.mP2, lr.getState());
1839 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001840
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001841 lr = sm.getLogRec(5);
1842 assertEquals(Hsm1.CMD_4, lr.getWhat());
1843 assertEquals(sm.mP2, lr.getState());
1844 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001845
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001846 lr = sm.getLogRec(6);
1847 assertEquals(Hsm1.CMD_5, lr.getWhat());
1848 assertEquals(sm.mP2, lr.getState());
1849 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001850
Wink Savilleefcc3d32013-01-30 11:21:22 -08001851 if (DBG) tlog("testStateMachineSharedThread X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001852 }
1853
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001854 private static void tlog(String s) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001855 Log.d(TAG, s);
Wink Savillefc5b4802009-12-08 21:22:24 -08001856 }
1857
Mitchell Wills36afe5b2016-08-09 13:33:20 -07001858 private static void tloge(String s) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001859 Log.e(TAG, s);
Wink Savillefc5b4802009-12-08 21:22:24 -08001860 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001861}