blob: eb2a5165dc4f414e9cb8da9715a202f229052dac [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 /**
mukesh agrawal47e1c7a2017-03-27 20:26:38 -070083 * Tests {@link StateMachine#toString()}.
84 */
85 class StateMachineToStringTest extends StateMachine {
86 StateMachineToStringTest(String name) {
87 super(name);
88 }
89 }
90
91 class ExampleState extends State {
92 String mName;
93
94 ExampleState(String name) {
95 mName = name;
96 }
97
98 @Override
99 public String getName() {
100 return mName;
101 }
102 }
103
104 @SmallTest
105 public void testToStringSucceedsEvenIfMachineHasNoStates() throws Exception {
106 StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
107 assertTrue(stateMachine.toString().contains("TestStateMachine"));
108 }
109
110 @SmallTest
111 public void testToStringSucceedsEvenIfStateHasNoName() throws Exception {
112 StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
113 State exampleState = new ExampleState(null);
114 stateMachine.addState(exampleState);
115 stateMachine.setInitialState(exampleState);
116 stateMachine.start();
117 assertTrue(stateMachine.toString().contains("TestStateMachine"));
118 assertTrue(stateMachine.toString().contains("(null)"));
119 }
120
121 @SmallTest
122 public void testToStringIncludesMachineAndStateNames() throws Exception {
123 StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
124 State exampleState = new ExampleState("exampleState");
125 stateMachine.addState(exampleState);
126 stateMachine.setInitialState(exampleState);
127 stateMachine.start();
128 assertTrue(stateMachine.toString().contains("TestStateMachine"));
129 assertTrue(stateMachine.toString().contains("exampleState"));
130 }
131
132 @SmallTest
133 public void testToStringDoesNotContainMultipleLines() throws Exception {
134 StateMachine stateMachine = new StateMachineToStringTest("TestStateMachine");
135 State exampleState = new ExampleState("exampleState");
136 stateMachine.addState(exampleState);
137 stateMachine.setInitialState(exampleState);
138 stateMachine.start();
139 assertFalse(stateMachine.toString().contains("\n"));
140 }
141
142 /**
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700143 * Tests {@link StateMachine#quit()}.
Wink Saville1b8b98b2010-03-11 11:49:54 -0800144 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700145 class StateMachineQuitTest extends StateMachine {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800146 Collection<LogRec> mLogRecs;
Wink Saville1b8b98b2010-03-11 11:49:54 -0800147
148 StateMachineQuitTest(String name) {
149 super(name);
150 mThisSm = this;
151 setDbg(DBG);
152
153 // Setup state machine with 1 state
154 addState(mS1);
155
156 // Set the initial state
157 setInitialState(mS1);
158 }
159
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700160 @Override
161 public void onQuitting() {
Mitchell Wills009ae992016-08-09 13:33:20 -0700162 tlog("onQuitting");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700163 addLogRec(ON_QUITTING);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800164 mLogRecs = mThisSm.copyLogRecs();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700165 synchronized (mThisSm) {
166 mThisSm.notifyAll();
167 }
Wink Saville1b8b98b2010-03-11 11:49:54 -0800168 }
169
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700170 class S1 extends State {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800171 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700172 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -0700173 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -0800174 addLogRec(EXIT);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700175 }
176 @Override
177 public boolean processMessage(Message message) {
178 switch(message.what) {
179 // Sleep and assume the other messages will be queued up.
180 case TEST_CMD_1: {
Mitchell Wills009ae992016-08-09 13:33:20 -0700181 tlog("TEST_CMD_1");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700182 sleep(500);
183 quit();
184 break;
185 }
186 default: {
Mitchell Wills009ae992016-08-09 13:33:20 -0700187 tlog("default what=" + message.what);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700188 break;
189 }
190 }
191 return HANDLED;
Wink Saville1b8b98b2010-03-11 11:49:54 -0800192 }
193 }
194
195 private StateMachineQuitTest mThisSm;
196 private S1 mS1 = new S1();
197 }
198
199 @SmallTest
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700200 public void testStateMachineQuit() throws Exception {
Wink Saville6b888d92010-11-15 12:16:52 -0800201 if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
Wink Saville1b8b98b2010-03-11 11:49:54 -0800202
203 StateMachineQuitTest smQuitTest = new StateMachineQuitTest("smQuitTest");
204 smQuitTest.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800205 if (smQuitTest.isDbg()) tlog("testStateMachineQuit E");
Wink Saville1b8b98b2010-03-11 11:49:54 -0800206
207 synchronized (smQuitTest) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700208
209 // Send 6 message we'll quit on the first but all 6 should be processed before quitting.
Wink Saville1b8b98b2010-03-11 11:49:54 -0800210 for (int i = 1; i <= 6; i++) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700211 smQuitTest.sendMessage(smQuitTest.obtainMessage(i));
Wink Saville1b8b98b2010-03-11 11:49:54 -0800212 }
213
Wink Saville1b8b98b2010-03-11 11:49:54 -0800214 try {
215 // wait for the messages to be handled
216 smQuitTest.wait();
217 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800218 tloge("testStateMachineQuit: exception while waiting " + e.getMessage());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800219 }
220 }
221
Wink Savilleefcc3d32013-01-30 11:21:22 -0800222 dumpLogRecs(smQuitTest.mLogRecs);
223 assertEquals(8, smQuitTest.mLogRecs.size());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800224
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700225 LogRec lr;
Wink Savilleefcc3d32013-01-30 11:21:22 -0800226 Iterator<LogRec> itr = smQuitTest.mLogRecs.iterator();
227 for (int i = 1; i <= 6; i++) {
228 lr = itr.next();
229 assertEquals(i, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700230 assertEquals(smQuitTest.mS1, lr.getState());
231 assertEquals(smQuitTest.mS1, lr.getOriginalState());
232 }
Wink Savilleefcc3d32013-01-30 11:21:22 -0800233 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700234 assertEquals(EXIT, lr.getInfo());
235 assertEquals(smQuitTest.mS1, lr.getState());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800236
Wink Savilleefcc3d32013-01-30 11:21:22 -0800237 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700238 assertEquals(ON_QUITTING, lr.getInfo());
Wink Saville1b8b98b2010-03-11 11:49:54 -0800239
Wink Savilleefcc3d32013-01-30 11:21:22 -0800240 if (smQuitTest.isDbg()) tlog("testStateMachineQuit X");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700241 }
Wink Saville1b8b98b2010-03-11 11:49:54 -0800242
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700243 /**
244 * Tests {@link StateMachine#quitNow()}
245 */
246 class StateMachineQuitNowTest extends StateMachine {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800247 public Collection<LogRec> mLogRecs = null;
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700248
249 StateMachineQuitNowTest(String name) {
250 super(name);
251 mThisSm = this;
252 setDbg(DBG);
253
254 // Setup state machine with 1 state
255 addState(mS1);
256
257 // Set the initial state
258 setInitialState(mS1);
259 }
260
261 @Override
262 public void onQuitting() {
Mitchell Wills009ae992016-08-09 13:33:20 -0700263 tlog("onQuitting");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700264 addLogRec(ON_QUITTING);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800265 // Get a copy of the log records since we're quitting and they will disappear
266 mLogRecs = mThisSm.copyLogRecs();
267
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700268 synchronized (mThisSm) {
269 mThisSm.notifyAll();
270 }
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700271 }
272
273 class S1 extends State {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800274 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700275 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -0700276 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -0800277 addLogRec(EXIT);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700278 }
279 @Override
280 public boolean processMessage(Message message) {
281 switch(message.what) {
282 // Sleep and assume the other messages will be queued up.
283 case TEST_CMD_1: {
Mitchell Wills009ae992016-08-09 13:33:20 -0700284 tlog("TEST_CMD_1");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700285 sleep(500);
286 quitNow();
287 break;
288 }
289 default: {
Mitchell Wills009ae992016-08-09 13:33:20 -0700290 tlog("default what=" + message.what);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700291 break;
292 }
293 }
294 return HANDLED;
295 }
296 }
297
298 private StateMachineQuitNowTest mThisSm;
299 private S1 mS1 = new S1();
300 }
301
302 @SmallTest
303 public void testStateMachineQuitNow() throws Exception {
304 if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
305
306 StateMachineQuitNowTest smQuitNowTest = new StateMachineQuitNowTest("smQuitNowTest");
307 smQuitNowTest.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800308 if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow E");
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700309
310 synchronized (smQuitNowTest) {
311
Wink Savilleefcc3d32013-01-30 11:21:22 -0800312 // Send 6 message we'll QuitNow on the first even though
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700313 // we send 6 only one will be processed.
314 for (int i = 1; i <= 6; i++) {
315 smQuitNowTest.sendMessage(smQuitNowTest.obtainMessage(i));
316 }
317
318 try {
319 // wait for the messages to be handled
320 smQuitNowTest.wait();
321 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800322 tloge("testStateMachineQuitNow: exception while waiting " + e.getMessage());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700323 }
324 }
325
Wink Savilleefcc3d32013-01-30 11:21:22 -0800326 tlog("testStateMachineQuiteNow: logRecs=" + smQuitNowTest.mLogRecs);
327 assertEquals(3, smQuitNowTest.mLogRecs.size());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700328
Wink Savilleefcc3d32013-01-30 11:21:22 -0800329 Iterator<LogRec> itr = smQuitNowTest.mLogRecs.iterator();
330 LogRec lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700331 assertEquals(1, lr.getWhat());
332 assertEquals(smQuitNowTest.mS1, lr.getState());
333 assertEquals(smQuitNowTest.mS1, lr.getOriginalState());
334
Wink Savilleefcc3d32013-01-30 11:21:22 -0800335 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700336 assertEquals(EXIT, lr.getInfo());
337 assertEquals(smQuitNowTest.mS1, lr.getState());
338
Wink Savilleefcc3d32013-01-30 11:21:22 -0800339 lr = itr.next();
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700340 assertEquals(ON_QUITTING, lr.getInfo());
341
Wink Savilleefcc3d32013-01-30 11:21:22 -0800342 if (smQuitNowTest.isDbg()) tlog("testStateMachineQuitNow X");
Wink Saville1b8b98b2010-03-11 11:49:54 -0800343 }
344
345 /**
Wink Savillee7be6a82010-03-18 17:03:30 -0700346 * Test enter/exit can use transitionTo
347 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700348 class StateMachineEnterExitTransitionToTest extends StateMachine {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700349
Wink Savillee7be6a82010-03-18 17:03:30 -0700350 StateMachineEnterExitTransitionToTest(String name) {
351 super(name);
352 mThisSm = this;
353 setDbg(DBG);
354
355 // Setup state machine with 1 state
356 addState(mS1);
357 addState(mS2);
358 addState(mS3);
359 addState(mS4);
360
361 // Set the initial state
362 setInitialState(mS1);
363 }
364
Wink Saville64c42ca2011-04-18 14:55:10 -0700365 class S1 extends State {
366 @Override
367 public void enter() {
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700368 // Test transitions in enter on the initial state work
Wink Savilleefcc3d32013-01-30 11:21:22 -0800369 addLogRec(ENTER);
Wink Savillee7be6a82010-03-18 17:03:30 -0700370 transitionTo(mS2);
Mitchell Wills009ae992016-08-09 13:33:20 -0700371 tlog("S1.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700372 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700373 @Override
374 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800375 addLogRec(EXIT);
Mitchell Wills009ae992016-08-09 13:33:20 -0700376 tlog("S1.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700377 }
378 }
379
Wink Saville64c42ca2011-04-18 14:55:10 -0700380 class S2 extends State {
381 @Override
382 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800383 addLogRec(ENTER);
Mitchell Wills009ae992016-08-09 13:33:20 -0700384 tlog("S2.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700385 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700386 @Override
387 public void exit() {
Wink Savillee7be6a82010-03-18 17:03:30 -0700388 // Test transition in exit work
Wink Savillee7be6a82010-03-18 17:03:30 -0700389 transitionTo(mS4);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800390
391 assertEquals(TEST_CMD_1, getCurrentMessage().what);
392 addLogRec(EXIT);
393
Mitchell Wills009ae992016-08-09 13:33:20 -0700394 tlog("S2.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700395 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700396 @Override
397 public boolean processMessage(Message message) {
Wink Savillee7be6a82010-03-18 17:03:30 -0700398 // Start a transition to S3 but it will be
Wink Savillea4f3bec2010-05-19 09:11:38 -0700399 // changed to a transition to S4 in exit
Wink Savillee7be6a82010-03-18 17:03:30 -0700400 transitionTo(mS3);
Mitchell Wills009ae992016-08-09 13:33:20 -0700401 tlog("S2.processMessage");
Wink Savillea4f3bec2010-05-19 09:11:38 -0700402 return HANDLED;
Wink Savillee7be6a82010-03-18 17:03:30 -0700403 }
404 }
405
Wink Saville64c42ca2011-04-18 14:55:10 -0700406 class S3 extends State {
407 @Override
408 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800409 addLogRec(ENTER);
Mitchell Wills009ae992016-08-09 13:33:20 -0700410 tlog("S3.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700411 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700412 @Override
413 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800414 addLogRec(EXIT);
Mitchell Wills009ae992016-08-09 13:33:20 -0700415 tlog("S3.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700416 }
417 }
418
Wink Saville64c42ca2011-04-18 14:55:10 -0700419 class S4 extends State {
420 @Override
421 public void enter() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800422 addLogRec(ENTER);
Wink Savillee7be6a82010-03-18 17:03:30 -0700423 // Test that we can do halting in an enter/exit
424 transitionToHaltingState();
Mitchell Wills009ae992016-08-09 13:33:20 -0700425 tlog("S4.enter");
Wink Savillee7be6a82010-03-18 17:03:30 -0700426 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700427 @Override
428 public void exit() {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800429 addLogRec(EXIT);
Mitchell Wills009ae992016-08-09 13:33:20 -0700430 tlog("S4.exit");
Wink Savillee7be6a82010-03-18 17:03:30 -0700431 }
432 }
433
434 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700435 protected void onHalting() {
Wink Savillee7be6a82010-03-18 17:03:30 -0700436 synchronized (mThisSm) {
437 mThisSm.notifyAll();
438 }
439 }
440
441 private StateMachineEnterExitTransitionToTest mThisSm;
442 private S1 mS1 = new S1();
443 private S2 mS2 = new S2();
444 private S3 mS3 = new S3();
445 private S4 mS4 = new S4();
Wink Savillee7be6a82010-03-18 17:03:30 -0700446 }
447
448 @SmallTest
449 public void testStateMachineEnterExitTransitionToTest() throws Exception {
450 //if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
451
452 StateMachineEnterExitTransitionToTest smEnterExitTranstionToTest =
453 new StateMachineEnterExitTransitionToTest("smEnterExitTranstionToTest");
454 smEnterExitTranstionToTest.start();
455 if (smEnterExitTranstionToTest.isDbg()) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800456 tlog("testStateMachineEnterExitTransitionToTest E");
Wink Savillee7be6a82010-03-18 17:03:30 -0700457 }
458
459 synchronized (smEnterExitTranstionToTest) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700460 smEnterExitTranstionToTest.sendMessage(TEST_CMD_1);
Wink Savillee7be6a82010-03-18 17:03:30 -0700461
462 try {
463 // wait for the messages to be handled
464 smEnterExitTranstionToTest.wait();
465 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800466 tloge("testStateMachineEnterExitTransitionToTest: exception while waiting "
Wink Savillee7be6a82010-03-18 17:03:30 -0700467 + e.getMessage());
468 }
469 }
470
Wink Savilleefcc3d32013-01-30 11:21:22 -0800471 dumpLogRecs(smEnterExitTranstionToTest);
Wink Savillee7be6a82010-03-18 17:03:30 -0700472
Wink Savilleefcc3d32013-01-30 11:21:22 -0800473 assertEquals(9, smEnterExitTranstionToTest.getLogRecCount());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700474 LogRec lr;
Wink Savillee7be6a82010-03-18 17:03:30 -0700475
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700476 lr = smEnterExitTranstionToTest.getLogRec(0);
477 assertEquals(ENTER, lr.getInfo());
478 assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
Wink Savillee7be6a82010-03-18 17:03:30 -0700479
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700480 lr = smEnterExitTranstionToTest.getLogRec(1);
481 assertEquals(EXIT, lr.getInfo());
482 assertEquals(smEnterExitTranstionToTest.mS1, lr.getState());
483
484 lr = smEnterExitTranstionToTest.getLogRec(2);
485 assertEquals(ENTER, lr.getInfo());
486 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
487
488 lr = smEnterExitTranstionToTest.getLogRec(3);
489 assertEquals(TEST_CMD_1, lr.getWhat());
490 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
491 assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800492 assertEquals(smEnterExitTranstionToTest.mS3, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700493
494 lr = smEnterExitTranstionToTest.getLogRec(4);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800495 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700496 assertEquals(smEnterExitTranstionToTest.mS2, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800497 assertEquals(smEnterExitTranstionToTest.mS2, lr.getOriginalState());
498 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
499 assertEquals(EXIT, lr.getInfo());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700500
501 lr = smEnterExitTranstionToTest.getLogRec(5);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800502 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700503 assertEquals(ENTER, lr.getInfo());
504 assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800505 assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
506 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700507
508 lr = smEnterExitTranstionToTest.getLogRec(6);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800509 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700510 assertEquals(EXIT, lr.getInfo());
511 assertEquals(smEnterExitTranstionToTest.mS3, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800512 assertEquals(smEnterExitTranstionToTest.mS3, lr.getOriginalState());
513 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700514
515 lr = smEnterExitTranstionToTest.getLogRec(7);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800516 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700517 assertEquals(ENTER, lr.getInfo());
518 assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800519 assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
520 assertEquals(smEnterExitTranstionToTest.mS4, lr.getDestState());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700521
522 lr = smEnterExitTranstionToTest.getLogRec(8);
Wink Savilleefcc3d32013-01-30 11:21:22 -0800523 assertEquals(TEST_CMD_1, lr.getWhat());
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700524 assertEquals(EXIT, lr.getInfo());
525 assertEquals(smEnterExitTranstionToTest.mS4, lr.getState());
Wink Savilleefcc3d32013-01-30 11:21:22 -0800526 assertEquals(smEnterExitTranstionToTest.mS4, lr.getOriginalState());
Wink Savillee7be6a82010-03-18 17:03:30 -0700527
528 if (smEnterExitTranstionToTest.isDbg()) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800529 tlog("testStateMachineEnterExitTransitionToTest X");
Wink Savillee7be6a82010-03-18 17:03:30 -0700530 }
531 }
532
533 /**
Wink Savillefc5b4802009-12-08 21:22:24 -0800534 * Tests that ProcessedMessage works as a circular buffer.
535 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700536 class StateMachine0 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800537 StateMachine0(String name) {
538 super(name);
539 mThisSm = this;
540 setDbg(DBG);
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700541 setLogRecSize(3);
Wink Savillefc5b4802009-12-08 21:22:24 -0800542
543 // Setup state machine with 1 state
544 addState(mS1);
545
546 // Set the initial state
547 setInitialState(mS1);
548 }
549
Wink Saville64c42ca2011-04-18 14:55:10 -0700550 class S1 extends State {
551 @Override
552 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800553 if (message.what == TEST_CMD_6) {
554 transitionToHaltingState();
555 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700556 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800557 }
558 }
559
560 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700561 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800562 synchronized (mThisSm) {
563 mThisSm.notifyAll();
564 }
565 }
566
567 private StateMachine0 mThisSm;
568 private S1 mS1 = new S1();
569 }
570
571 @SmallTest
572 public void testStateMachine0() throws Exception {
Wink Saville1b8b98b2010-03-11 11:49:54 -0800573 //if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
Wink Savillefc5b4802009-12-08 21:22:24 -0800574
575 StateMachine0 sm0 = new StateMachine0("sm0");
576 sm0.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800577 if (sm0.isDbg()) tlog("testStateMachine0 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800578
579 synchronized (sm0) {
580 // Send 6 messages
581 for (int i = 1; i <= 6; i++) {
582 sm0.sendMessage(sm0.obtainMessage(i));
583 }
584
585 try {
586 // wait for the messages to be handled
587 sm0.wait();
588 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800589 tloge("testStateMachine0: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800590 }
591 }
592
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700593 assertEquals(6, sm0.getLogRecCount());
594 assertEquals(3, sm0.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800595
Wink Savilleefcc3d32013-01-30 11:21:22 -0800596 dumpLogRecs(sm0);
597
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700598 LogRec lr;
599 lr = sm0.getLogRec(0);
600 assertEquals(TEST_CMD_4, lr.getWhat());
601 assertEquals(sm0.mS1, lr.getState());
602 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800603
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700604 lr = sm0.getLogRec(1);
605 assertEquals(TEST_CMD_5, lr.getWhat());
606 assertEquals(sm0.mS1, lr.getState());
607 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800608
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700609 lr = sm0.getLogRec(2);
610 assertEquals(TEST_CMD_6, lr.getWhat());
611 assertEquals(sm0.mS1, lr.getState());
612 assertEquals(sm0.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800613
Wink Savilleefcc3d32013-01-30 11:21:22 -0800614 if (sm0.isDbg()) tlog("testStateMachine0 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800615 }
616
617 /**
618 * This tests enter/exit and transitions to the same state.
619 * The state machine has one state, it receives two messages
620 * in state mS1. With the first message it transitions to
621 * itself which causes it to be exited and reentered.
622 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700623 class StateMachine1 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800624 StateMachine1(String name) {
625 super(name);
626 mThisSm = this;
627 setDbg(DBG);
628
629 // Setup state machine with 1 state
630 addState(mS1);
631
632 // Set the initial state
633 setInitialState(mS1);
Mitchell Wills009ae992016-08-09 13:33:20 -0700634 if (DBG) tlog("StateMachine1: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800635 }
636
Wink Saville64c42ca2011-04-18 14:55:10 -0700637 class S1 extends State {
638 @Override
639 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800640 mEnterCount++;
641 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700642 @Override
643 public void exit() {
644 mExitCount++;
645 }
646 @Override
647 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800648 if (message.what == TEST_CMD_1) {
649 assertEquals(1, mEnterCount);
650 assertEquals(0, mExitCount);
651 transitionTo(mS1);
652 } else if (message.what == TEST_CMD_2) {
653 assertEquals(2, mEnterCount);
654 assertEquals(1, mExitCount);
655 transitionToHaltingState();
656 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700657 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800658 }
Wink Savillefc5b4802009-12-08 21:22:24 -0800659 }
660
661 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700662 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800663 synchronized (mThisSm) {
664 mThisSm.notifyAll();
665 }
666 }
667
668 private StateMachine1 mThisSm;
669 private S1 mS1 = new S1();
670
671 private int mEnterCount;
672 private int mExitCount;
673 }
674
Brett Chabotf76c56b2010-07-26 17:28:17 -0700675 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800676 public void testStateMachine1() throws Exception {
677 StateMachine1 sm1 = new StateMachine1("sm1");
678 sm1.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800679 if (sm1.isDbg()) tlog("testStateMachine1 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800680
681 synchronized (sm1) {
682 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700683 sm1.sendMessage(TEST_CMD_1);
684 sm1.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800685
686 try {
687 // wait for the messages to be handled
688 sm1.wait();
689 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800690 tloge("testStateMachine1: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800691 }
692 }
693
694 assertEquals(2, sm1.mEnterCount);
695 assertEquals(2, sm1.mExitCount);
696
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700697 assertEquals(2, sm1.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800698
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700699 LogRec lr;
700 lr = sm1.getLogRec(0);
701 assertEquals(TEST_CMD_1, lr.getWhat());
702 assertEquals(sm1.mS1, lr.getState());
703 assertEquals(sm1.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800704
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700705 lr = sm1.getLogRec(1);
706 assertEquals(TEST_CMD_2, lr.getWhat());
707 assertEquals(sm1.mS1, lr.getState());
708 assertEquals(sm1.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800709
710 assertEquals(2, sm1.mEnterCount);
711 assertEquals(2, sm1.mExitCount);
712
Wink Savilleefcc3d32013-01-30 11:21:22 -0800713 if (sm1.isDbg()) tlog("testStateMachine1 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800714 }
715
716 /**
717 * Test deferring messages and states with no parents. The state machine
718 * has two states, it receives two messages in state mS1 deferring them
719 * until what == TEST_CMD_2 and then transitions to state mS2. State
720 * mS2 then receives both of the deferred messages first TEST_CMD_1 and
721 * then TEST_CMD_2.
722 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700723 class StateMachine2 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800724 StateMachine2(String name) {
725 super(name);
726 mThisSm = this;
727 setDbg(DBG);
728
729 // Setup the hierarchy
730 addState(mS1);
731 addState(mS2);
732
733 // Set the initial state
734 setInitialState(mS1);
Mitchell Wills009ae992016-08-09 13:33:20 -0700735 if (DBG) tlog("StateMachine2: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800736 }
737
Wink Saville64c42ca2011-04-18 14:55:10 -0700738 class S1 extends State {
739 @Override
740 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800741 mDidEnter = true;
742 }
Wink Saville64c42ca2011-04-18 14:55:10 -0700743 @Override
744 public void exit() {
745 mDidExit = true;
746 }
747 @Override
748 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800749 deferMessage(message);
750 if (message.what == TEST_CMD_2) {
751 transitionTo(mS2);
752 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700753 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800754 }
Wink Savillefc5b4802009-12-08 21:22:24 -0800755 }
756
Wink Saville64c42ca2011-04-18 14:55:10 -0700757 class S2 extends State {
758 @Override
759 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800760 if (message.what == TEST_CMD_2) {
761 transitionToHaltingState();
762 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700763 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800764 }
765 }
766
767 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700768 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800769 synchronized (mThisSm) {
770 mThisSm.notifyAll();
771 }
772 }
773
774 private StateMachine2 mThisSm;
775 private S1 mS1 = new S1();
776 private S2 mS2 = new S2();
777
778 private boolean mDidEnter = false;
779 private boolean mDidExit = false;
780 }
781
Brett Chabotf76c56b2010-07-26 17:28:17 -0700782 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800783 public void testStateMachine2() throws Exception {
784 StateMachine2 sm2 = new StateMachine2("sm2");
785 sm2.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800786 if (sm2.isDbg()) tlog("testStateMachine2 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800787
788 synchronized (sm2) {
789 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700790 sm2.sendMessage(TEST_CMD_1);
791 sm2.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800792
793 try {
794 // wait for the messages to be handled
795 sm2.wait();
796 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800797 tloge("testStateMachine2: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800798 }
799 }
800
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700801 assertEquals(4, sm2.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800802
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700803 LogRec lr;
804 lr = sm2.getLogRec(0);
805 assertEquals(TEST_CMD_1, lr.getWhat());
806 assertEquals(sm2.mS1, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800807
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700808 lr = sm2.getLogRec(1);
809 assertEquals(TEST_CMD_2, lr.getWhat());
810 assertEquals(sm2.mS1, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800811
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700812 lr = sm2.getLogRec(2);
813 assertEquals(TEST_CMD_1, lr.getWhat());
814 assertEquals(sm2.mS2, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800815
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700816 lr = sm2.getLogRec(3);
817 assertEquals(TEST_CMD_2, lr.getWhat());
818 assertEquals(sm2.mS2, lr.getState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800819
820 assertTrue(sm2.mDidEnter);
821 assertTrue(sm2.mDidExit);
822
Wink Savilleefcc3d32013-01-30 11:21:22 -0800823 if (sm2.isDbg()) tlog("testStateMachine2 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800824 }
825
826 /**
827 * Test that unhandled messages in a child are handled by the parent.
828 * When TEST_CMD_2 is received.
829 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700830 class StateMachine3 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800831 StateMachine3(String name) {
832 super(name);
833 mThisSm = this;
834 setDbg(DBG);
835
836 // Setup the simplest hierarchy of two states
837 // mParentState and mChildState.
838 // (Use indentation to help visualize hierarchy)
839 addState(mParentState);
840 addState(mChildState, mParentState);
841
842 // Set the initial state will be the child
843 setInitialState(mChildState);
Mitchell Wills009ae992016-08-09 13:33:20 -0700844 if (DBG) tlog("StateMachine3: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800845 }
846
Wink Saville64c42ca2011-04-18 14:55:10 -0700847 class ParentState extends State {
848 @Override
849 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800850 if (message.what == TEST_CMD_2) {
851 transitionToHaltingState();
852 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700853 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800854 }
855 }
856
Wink Saville64c42ca2011-04-18 14:55:10 -0700857 class ChildState extends State {
858 @Override
859 public boolean processMessage(Message message) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700860 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800861 }
862 }
863
864 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700865 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800866 synchronized (mThisSm) {
867 mThisSm.notifyAll();
868 }
869 }
870
871 private StateMachine3 mThisSm;
872 private ParentState mParentState = new ParentState();
873 private ChildState mChildState = new ChildState();
874 }
875
Brett Chabotf76c56b2010-07-26 17:28:17 -0700876 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800877 public void testStateMachine3() throws Exception {
878 StateMachine3 sm3 = new StateMachine3("sm3");
879 sm3.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800880 if (sm3.isDbg()) tlog("testStateMachine3 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800881
882 synchronized (sm3) {
883 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700884 sm3.sendMessage(TEST_CMD_1);
885 sm3.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800886
887 try {
888 // wait for the messages to be handled
889 sm3.wait();
890 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800891 tloge("testStateMachine3: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800892 }
893 }
894
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700895 assertEquals(2, sm3.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800896
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700897 LogRec lr;
898 lr = sm3.getLogRec(0);
899 assertEquals(TEST_CMD_1, lr.getWhat());
900 assertEquals(sm3.mParentState, lr.getState());
901 assertEquals(sm3.mChildState, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800902
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700903 lr = sm3.getLogRec(1);
904 assertEquals(TEST_CMD_2, lr.getWhat());
905 assertEquals(sm3.mParentState, lr.getState());
906 assertEquals(sm3.mChildState, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800907
Wink Savilleefcc3d32013-01-30 11:21:22 -0800908 if (sm3.isDbg()) tlog("testStateMachine3 X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800909 }
910
911 /**
912 * Test a hierarchy of 3 states a parent and two children
913 * with transition from child 1 to child 2 and child 2
914 * lets the parent handle the messages.
915 */
Wink Saville64c42ca2011-04-18 14:55:10 -0700916 class StateMachine4 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -0800917 StateMachine4(String name) {
918 super(name);
919 mThisSm = this;
920 setDbg(DBG);
921
922 // Setup a hierarchy of three states
923 // mParentState, mChildState1 & mChildState2
924 // (Use indentation to help visualize hierarchy)
925 addState(mParentState);
926 addState(mChildState1, mParentState);
927 addState(mChildState2, mParentState);
928
929 // Set the initial state will be child 1
930 setInitialState(mChildState1);
Mitchell Wills009ae992016-08-09 13:33:20 -0700931 if (DBG) tlog("StateMachine4: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -0800932 }
933
Wink Saville64c42ca2011-04-18 14:55:10 -0700934 class ParentState extends State {
935 @Override
936 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800937 if (message.what == TEST_CMD_2) {
938 transitionToHaltingState();
939 }
Wink Savillea4f3bec2010-05-19 09:11:38 -0700940 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800941 }
942 }
943
Wink Saville64c42ca2011-04-18 14:55:10 -0700944 class ChildState1 extends State {
945 @Override
946 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -0800947 transitionTo(mChildState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -0700948 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800949 }
950 }
951
Wink Saville64c42ca2011-04-18 14:55:10 -0700952 class ChildState2 extends State {
953 @Override
954 public boolean processMessage(Message message) {
Wink Savillea4f3bec2010-05-19 09:11:38 -0700955 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -0800956 }
957 }
958
959 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700960 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -0800961 synchronized (mThisSm) {
962 mThisSm.notifyAll();
963 }
964 }
965
966 private StateMachine4 mThisSm;
967 private ParentState mParentState = new ParentState();
968 private ChildState1 mChildState1 = new ChildState1();
969 private ChildState2 mChildState2 = new ChildState2();
970 }
971
Brett Chabotf76c56b2010-07-26 17:28:17 -0700972 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -0800973 public void testStateMachine4() throws Exception {
974 StateMachine4 sm4 = new StateMachine4("sm4");
975 sm4.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -0800976 if (sm4.isDbg()) tlog("testStateMachine4 E");
Wink Savillefc5b4802009-12-08 21:22:24 -0800977
978 synchronized (sm4) {
979 // Send two messages
Wink Saville91fbd562010-03-17 17:12:43 -0700980 sm4.sendMessage(TEST_CMD_1);
981 sm4.sendMessage(TEST_CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -0800982
983 try {
984 // wait for the messages to be handled
985 sm4.wait();
986 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -0800987 tloge("testStateMachine4: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -0800988 }
989 }
990
991
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700992 assertEquals(2, sm4.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -0800993
Wink Savillebbf30dfd2012-05-29 12:40:46 -0700994 LogRec lr;
995 lr = sm4.getLogRec(0);
996 assertEquals(TEST_CMD_1, lr.getWhat());
997 assertEquals(sm4.mChildState1, lr.getState());
998 assertEquals(sm4.mChildState1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -0800999
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001000 lr = sm4.getLogRec(1);
1001 assertEquals(TEST_CMD_2, lr.getWhat());
1002 assertEquals(sm4.mParentState, lr.getState());
1003 assertEquals(sm4.mChildState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001004
Wink Savilleefcc3d32013-01-30 11:21:22 -08001005 if (sm4.isDbg()) tlog("testStateMachine4 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001006 }
1007
1008 /**
1009 * Test transition from one child to another of a "complex"
1010 * hierarchy with two parents and multiple children.
1011 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001012 class StateMachine5 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001013 StateMachine5(String name) {
1014 super(name);
1015 mThisSm = this;
1016 setDbg(DBG);
1017
1018 // Setup a hierarchy with two parents and some children.
1019 // (Use indentation to help visualize hierarchy)
1020 addState(mParentState1);
1021 addState(mChildState1, mParentState1);
1022 addState(mChildState2, mParentState1);
1023
1024 addState(mParentState2);
1025 addState(mChildState3, mParentState2);
1026 addState(mChildState4, mParentState2);
1027 addState(mChildState5, mChildState4);
1028
1029 // Set the initial state will be the child
1030 setInitialState(mChildState1);
Mitchell Wills009ae992016-08-09 13:33:20 -07001031 if (DBG) tlog("StateMachine5: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001032 }
1033
Wink Saville64c42ca2011-04-18 14:55:10 -07001034 class ParentState1 extends State {
1035 @Override
1036 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001037 mParentState1EnterCount += 1;
1038 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001039 @Override
1040 public void exit() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001041 mParentState1ExitCount += 1;
1042 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001043 @Override
1044 public boolean processMessage(Message message) {
1045 return HANDLED;
1046 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001047 }
1048
Wink Saville64c42ca2011-04-18 14:55:10 -07001049 class ChildState1 extends State {
1050 @Override
1051 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001052 mChildState1EnterCount += 1;
1053 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001054 @Override
1055 public void exit() {
1056 mChildState1ExitCount += 1;
1057 }
1058 @Override
1059 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001060 assertEquals(1, mParentState1EnterCount);
1061 assertEquals(0, mParentState1ExitCount);
1062 assertEquals(1, mChildState1EnterCount);
1063 assertEquals(0, mChildState1ExitCount);
1064 assertEquals(0, mChildState2EnterCount);
1065 assertEquals(0, mChildState2ExitCount);
1066 assertEquals(0, mParentState2EnterCount);
1067 assertEquals(0, mParentState2ExitCount);
1068 assertEquals(0, mChildState3EnterCount);
1069 assertEquals(0, mChildState3ExitCount);
1070 assertEquals(0, mChildState4EnterCount);
1071 assertEquals(0, mChildState4ExitCount);
1072 assertEquals(0, mChildState5EnterCount);
1073 assertEquals(0, mChildState5ExitCount);
1074
1075 transitionTo(mChildState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001076 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001077 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001078 }
1079
Wink Saville64c42ca2011-04-18 14:55:10 -07001080 class ChildState2 extends State {
1081 @Override
1082 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001083 mChildState2EnterCount += 1;
1084 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001085 @Override
1086 public void exit() {
1087 mChildState2ExitCount += 1;
1088 }
1089 @Override
1090 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001091 assertEquals(1, mParentState1EnterCount);
1092 assertEquals(0, mParentState1ExitCount);
1093 assertEquals(1, mChildState1EnterCount);
1094 assertEquals(1, mChildState1ExitCount);
1095 assertEquals(1, mChildState2EnterCount);
1096 assertEquals(0, mChildState2ExitCount);
1097 assertEquals(0, mParentState2EnterCount);
1098 assertEquals(0, mParentState2ExitCount);
1099 assertEquals(0, mChildState3EnterCount);
1100 assertEquals(0, mChildState3ExitCount);
1101 assertEquals(0, mChildState4EnterCount);
1102 assertEquals(0, mChildState4ExitCount);
1103 assertEquals(0, mChildState5EnterCount);
1104 assertEquals(0, mChildState5ExitCount);
1105
1106 transitionTo(mChildState5);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001107 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001108 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001109 }
1110
Wink Saville64c42ca2011-04-18 14:55:10 -07001111 class ParentState2 extends State {
1112 @Override
1113 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001114 mParentState2EnterCount += 1;
1115 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001116 @Override
1117 public void exit() {
1118 mParentState2ExitCount += 1;
1119 }
1120 @Override
1121 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001122 assertEquals(1, mParentState1EnterCount);
1123 assertEquals(1, mParentState1ExitCount);
1124 assertEquals(1, mChildState1EnterCount);
1125 assertEquals(1, mChildState1ExitCount);
1126 assertEquals(1, mChildState2EnterCount);
1127 assertEquals(1, mChildState2ExitCount);
1128 assertEquals(2, mParentState2EnterCount);
1129 assertEquals(1, mParentState2ExitCount);
1130 assertEquals(1, mChildState3EnterCount);
1131 assertEquals(1, mChildState3ExitCount);
1132 assertEquals(2, mChildState4EnterCount);
1133 assertEquals(2, mChildState4ExitCount);
1134 assertEquals(1, mChildState5EnterCount);
1135 assertEquals(1, mChildState5ExitCount);
1136
1137 transitionToHaltingState();
Wink Savillea4f3bec2010-05-19 09:11:38 -07001138 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001139 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001140 }
1141
Wink Saville64c42ca2011-04-18 14:55:10 -07001142 class ChildState3 extends State {
1143 @Override
1144 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001145 mChildState3EnterCount += 1;
1146 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001147 @Override
1148 public void exit() {
1149 mChildState3ExitCount += 1;
1150 }
1151 @Override
1152 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001153 assertEquals(1, mParentState1EnterCount);
1154 assertEquals(1, mParentState1ExitCount);
1155 assertEquals(1, mChildState1EnterCount);
1156 assertEquals(1, mChildState1ExitCount);
1157 assertEquals(1, mChildState2EnterCount);
1158 assertEquals(1, mChildState2ExitCount);
1159 assertEquals(1, mParentState2EnterCount);
1160 assertEquals(0, mParentState2ExitCount);
1161 assertEquals(1, mChildState3EnterCount);
1162 assertEquals(0, mChildState3ExitCount);
1163 assertEquals(1, mChildState4EnterCount);
1164 assertEquals(1, mChildState4ExitCount);
1165 assertEquals(1, mChildState5EnterCount);
1166 assertEquals(1, mChildState5ExitCount);
1167
1168 transitionTo(mChildState4);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001169 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001170 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001171 }
1172
Wink Saville64c42ca2011-04-18 14:55:10 -07001173 class ChildState4 extends State {
1174 @Override
1175 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001176 mChildState4EnterCount += 1;
1177 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001178 @Override
1179 public void exit() {
1180 mChildState4ExitCount += 1;
1181 }
1182 @Override
1183 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001184 assertEquals(1, mParentState1EnterCount);
1185 assertEquals(1, mParentState1ExitCount);
1186 assertEquals(1, mChildState1EnterCount);
1187 assertEquals(1, mChildState1ExitCount);
1188 assertEquals(1, mChildState2EnterCount);
1189 assertEquals(1, mChildState2ExitCount);
1190 assertEquals(1, mParentState2EnterCount);
1191 assertEquals(0, mParentState2ExitCount);
1192 assertEquals(1, mChildState3EnterCount);
1193 assertEquals(1, mChildState3ExitCount);
1194 assertEquals(2, mChildState4EnterCount);
1195 assertEquals(1, mChildState4ExitCount);
1196 assertEquals(1, mChildState5EnterCount);
1197 assertEquals(1, mChildState5ExitCount);
1198
1199 transitionTo(mParentState2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001200 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001201 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001202 }
1203
Wink Saville64c42ca2011-04-18 14:55:10 -07001204 class ChildState5 extends State {
1205 @Override
1206 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001207 mChildState5EnterCount += 1;
1208 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001209 @Override
1210 public void exit() {
1211 mChildState5ExitCount += 1;
1212 }
1213 @Override
1214 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001215 assertEquals(1, mParentState1EnterCount);
1216 assertEquals(1, mParentState1ExitCount);
1217 assertEquals(1, mChildState1EnterCount);
1218 assertEquals(1, mChildState1ExitCount);
1219 assertEquals(1, mChildState2EnterCount);
1220 assertEquals(1, mChildState2ExitCount);
1221 assertEquals(1, mParentState2EnterCount);
1222 assertEquals(0, mParentState2ExitCount);
1223 assertEquals(0, mChildState3EnterCount);
1224 assertEquals(0, mChildState3ExitCount);
1225 assertEquals(1, mChildState4EnterCount);
1226 assertEquals(0, mChildState4ExitCount);
1227 assertEquals(1, mChildState5EnterCount);
1228 assertEquals(0, mChildState5ExitCount);
1229
1230 transitionTo(mChildState3);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001231 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001232 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001233 }
1234
1235 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001236 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001237 synchronized (mThisSm) {
1238 mThisSm.notifyAll();
1239 }
1240 }
1241
1242 private StateMachine5 mThisSm;
1243 private ParentState1 mParentState1 = new ParentState1();
1244 private ChildState1 mChildState1 = new ChildState1();
1245 private ChildState2 mChildState2 = new ChildState2();
1246 private ParentState2 mParentState2 = new ParentState2();
1247 private ChildState3 mChildState3 = new ChildState3();
1248 private ChildState4 mChildState4 = new ChildState4();
1249 private ChildState5 mChildState5 = new ChildState5();
1250
1251 private int mParentState1EnterCount = 0;
1252 private int mParentState1ExitCount = 0;
1253 private int mChildState1EnterCount = 0;
1254 private int mChildState1ExitCount = 0;
1255 private int mChildState2EnterCount = 0;
1256 private int mChildState2ExitCount = 0;
1257 private int mParentState2EnterCount = 0;
1258 private int mParentState2ExitCount = 0;
1259 private int mChildState3EnterCount = 0;
1260 private int mChildState3ExitCount = 0;
1261 private int mChildState4EnterCount = 0;
1262 private int mChildState4ExitCount = 0;
1263 private int mChildState5EnterCount = 0;
1264 private int mChildState5ExitCount = 0;
1265 }
1266
Brett Chabotf76c56b2010-07-26 17:28:17 -07001267 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001268 public void testStateMachine5() throws Exception {
1269 StateMachine5 sm5 = new StateMachine5("sm5");
1270 sm5.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001271 if (sm5.isDbg()) tlog("testStateMachine5 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001272
1273 synchronized (sm5) {
1274 // Send 6 messages
Wink Saville91fbd562010-03-17 17:12:43 -07001275 sm5.sendMessage(TEST_CMD_1);
1276 sm5.sendMessage(TEST_CMD_2);
1277 sm5.sendMessage(TEST_CMD_3);
1278 sm5.sendMessage(TEST_CMD_4);
1279 sm5.sendMessage(TEST_CMD_5);
1280 sm5.sendMessage(TEST_CMD_6);
Wink Savillefc5b4802009-12-08 21:22:24 -08001281
1282 try {
1283 // wait for the messages to be handled
1284 sm5.wait();
1285 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001286 tloge("testStateMachine5: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001287 }
1288 }
1289
1290
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001291 assertEquals(6, sm5.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -08001292
1293 assertEquals(1, sm5.mParentState1EnterCount);
1294 assertEquals(1, sm5.mParentState1ExitCount);
1295 assertEquals(1, sm5.mChildState1EnterCount);
1296 assertEquals(1, sm5.mChildState1ExitCount);
1297 assertEquals(1, sm5.mChildState2EnterCount);
1298 assertEquals(1, sm5.mChildState2ExitCount);
1299 assertEquals(2, sm5.mParentState2EnterCount);
1300 assertEquals(2, sm5.mParentState2ExitCount);
1301 assertEquals(1, sm5.mChildState3EnterCount);
1302 assertEquals(1, sm5.mChildState3ExitCount);
1303 assertEquals(2, sm5.mChildState4EnterCount);
1304 assertEquals(2, sm5.mChildState4ExitCount);
1305 assertEquals(1, sm5.mChildState5EnterCount);
1306 assertEquals(1, sm5.mChildState5ExitCount);
1307
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001308 LogRec lr;
1309 lr = sm5.getLogRec(0);
1310 assertEquals(TEST_CMD_1, lr.getWhat());
1311 assertEquals(sm5.mChildState1, lr.getState());
1312 assertEquals(sm5.mChildState1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001313
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001314 lr = sm5.getLogRec(1);
1315 assertEquals(TEST_CMD_2, lr.getWhat());
1316 assertEquals(sm5.mChildState2, lr.getState());
1317 assertEquals(sm5.mChildState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001318
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001319 lr = sm5.getLogRec(2);
1320 assertEquals(TEST_CMD_3, lr.getWhat());
1321 assertEquals(sm5.mChildState5, lr.getState());
1322 assertEquals(sm5.mChildState5, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001323
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001324 lr = sm5.getLogRec(3);
1325 assertEquals(TEST_CMD_4, lr.getWhat());
1326 assertEquals(sm5.mChildState3, lr.getState());
1327 assertEquals(sm5.mChildState3, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001328
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001329 lr = sm5.getLogRec(4);
1330 assertEquals(TEST_CMD_5, lr.getWhat());
1331 assertEquals(sm5.mChildState4, lr.getState());
1332 assertEquals(sm5.mChildState4, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001333
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001334 lr = sm5.getLogRec(5);
1335 assertEquals(TEST_CMD_6, lr.getWhat());
1336 assertEquals(sm5.mParentState2, lr.getState());
1337 assertEquals(sm5.mParentState2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001338
Wink Savilleefcc3d32013-01-30 11:21:22 -08001339 if (sm5.isDbg()) tlog("testStateMachine5 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001340 }
1341
1342 /**
1343 * Test that the initial state enter is invoked immediately
1344 * after construction and before any other messages arrive and that
1345 * sendMessageDelayed works.
1346 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001347 class StateMachine6 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001348 StateMachine6(String name) {
1349 super(name);
1350 mThisSm = this;
1351 setDbg(DBG);
1352
1353 // Setup state machine with 1 state
1354 addState(mS1);
1355
1356 // Set the initial state
1357 setInitialState(mS1);
Mitchell Wills009ae992016-08-09 13:33:20 -07001358 if (DBG) tlog("StateMachine6: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001359 }
1360
Wink Saville64c42ca2011-04-18 14:55:10 -07001361 class S1 extends State {
1362 @Override
1363 public void enter() {
Wink Saville91fbd562010-03-17 17:12:43 -07001364 sendMessage(TEST_CMD_1);
Wink Savillefc5b4802009-12-08 21:22:24 -08001365 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001366 @Override
1367 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001368 if (message.what == TEST_CMD_1) {
1369 mArrivalTimeMsg1 = SystemClock.elapsedRealtime();
1370 } else if (message.what == TEST_CMD_2) {
1371 mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
1372 transitionToHaltingState();
1373 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001374 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001375 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001376 }
1377
1378 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001379 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001380 synchronized (mThisSm) {
1381 mThisSm.notifyAll();
1382 }
1383 }
1384
1385 private StateMachine6 mThisSm;
1386 private S1 mS1 = new S1();
1387
1388 private long mArrivalTimeMsg1;
1389 private long mArrivalTimeMsg2;
1390 }
1391
Brett Chabotf76c56b2010-07-26 17:28:17 -07001392 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001393 public void testStateMachine6() throws Exception {
Wink Savillefc5b4802009-12-08 21:22:24 -08001394 final int DELAY_TIME = 250;
1395 final int DELAY_FUDGE = 20;
1396
1397 StateMachine6 sm6 = new StateMachine6("sm6");
1398 sm6.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001399 if (sm6.isDbg()) tlog("testStateMachine6 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001400
1401 synchronized (sm6) {
1402 // Send a message
Wink Saville91fbd562010-03-17 17:12:43 -07001403 sm6.sendMessageDelayed(TEST_CMD_2, DELAY_TIME);
Wink Savillefc5b4802009-12-08 21:22:24 -08001404
1405 try {
1406 // wait for the messages to be handled
1407 sm6.wait();
1408 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001409 tloge("testStateMachine6: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001410 }
1411 }
1412
1413 /**
1414 * TEST_CMD_1 was sent in enter and must always have been processed
1415 * immediately after construction and hence the arrival time difference
1416 * should always >= to the DELAY_TIME
1417 */
1418 long arrivalTimeDiff = sm6.mArrivalTimeMsg2 - sm6.mArrivalTimeMsg1;
1419 long expectedDelay = DELAY_TIME - DELAY_FUDGE;
Wink Savilleefcc3d32013-01-30 11:21:22 -08001420 if (sm6.isDbg()) tlog("testStateMachine6: expect " + arrivalTimeDiff
Wink Savillefc5b4802009-12-08 21:22:24 -08001421 + " >= " + expectedDelay);
1422 assertTrue(arrivalTimeDiff >= expectedDelay);
1423
Wink Savilleefcc3d32013-01-30 11:21:22 -08001424 if (sm6.isDbg()) tlog("testStateMachine6 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001425 }
1426
1427 /**
1428 * Test that enter is invoked immediately after exit. This validates
1429 * that enter can be used to send a watch dog message for its state.
1430 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001431 class StateMachine7 extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001432 private final int SM7_DELAY_TIME = 250;
1433
1434 StateMachine7(String name) {
1435 super(name);
1436 mThisSm = this;
1437 setDbg(DBG);
1438
1439 // Setup state machine with 1 state
1440 addState(mS1);
1441 addState(mS2);
1442
1443 // Set the initial state
1444 setInitialState(mS1);
Mitchell Wills009ae992016-08-09 13:33:20 -07001445 if (DBG) tlog("StateMachine7: ctor X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001446 }
1447
Wink Saville64c42ca2011-04-18 14:55:10 -07001448 class S1 extends State {
1449 @Override
1450 public void exit() {
1451 sendMessage(TEST_CMD_2);
1452 }
1453 @Override
1454 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001455 transitionTo(mS2);
Wink Savillea4f3bec2010-05-19 09:11:38 -07001456 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001457 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001458 }
1459
Wink Saville64c42ca2011-04-18 14:55:10 -07001460 class S2 extends State {
1461 @Override
1462 public void enter() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001463 // Send a delayed message as a watch dog
Wink Saville91fbd562010-03-17 17:12:43 -07001464 sendMessageDelayed(TEST_CMD_3, SM7_DELAY_TIME);
Wink Savillefc5b4802009-12-08 21:22:24 -08001465 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001466 @Override
1467 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001468 if (message.what == TEST_CMD_2) {
1469 mMsgCount += 1;
1470 mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
1471 } else if (message.what == TEST_CMD_3) {
1472 mMsgCount += 1;
1473 mArrivalTimeMsg3 = SystemClock.elapsedRealtime();
1474 }
1475
1476 if (mMsgCount == 2) {
1477 transitionToHaltingState();
1478 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001479 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001480 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001481 }
1482
1483 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001484 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001485 synchronized (mThisSm) {
1486 mThisSm.notifyAll();
1487 }
1488 }
1489
1490 private StateMachine7 mThisSm;
1491 private S1 mS1 = new S1();
1492 private S2 mS2 = new S2();
1493
1494 private int mMsgCount = 0;
1495 private long mArrivalTimeMsg2;
1496 private long mArrivalTimeMsg3;
1497 }
1498
Brett Chabotf76c56b2010-07-26 17:28:17 -07001499 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001500 public void testStateMachine7() throws Exception {
Wink Savillefc5b4802009-12-08 21:22:24 -08001501 final int SM7_DELAY_FUDGE = 20;
1502
1503 StateMachine7 sm7 = new StateMachine7("sm7");
1504 sm7.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001505 if (sm7.isDbg()) tlog("testStateMachine7 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001506
1507 synchronized (sm7) {
1508 // Send a message
Wink Saville91fbd562010-03-17 17:12:43 -07001509 sm7.sendMessage(TEST_CMD_1);
Wink Savillefc5b4802009-12-08 21:22:24 -08001510
1511 try {
1512 // wait for the messages to be handled
1513 sm7.wait();
1514 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001515 tloge("testStateMachine7: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001516 }
1517 }
1518
1519 /**
1520 * TEST_CMD_3 was sent in S2.enter with a delay and must always have been
1521 * processed immediately after S1.exit. Since S1.exit sent TEST_CMD_2
1522 * without a delay the arrival time difference should always >= to SM7_DELAY_TIME.
1523 */
1524 long arrivalTimeDiff = sm7.mArrivalTimeMsg3 - sm7.mArrivalTimeMsg2;
1525 long expectedDelay = sm7.SM7_DELAY_TIME - SM7_DELAY_FUDGE;
Wink Savilleefcc3d32013-01-30 11:21:22 -08001526 if (sm7.isDbg()) tlog("testStateMachine7: expect " + arrivalTimeDiff
Wink Savillefc5b4802009-12-08 21:22:24 -08001527 + " >= " + expectedDelay);
1528 assertTrue(arrivalTimeDiff >= expectedDelay);
1529
Wink Savilleefcc3d32013-01-30 11:21:22 -08001530 if (sm7.isDbg()) tlog("testStateMachine7 X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001531 }
1532
1533 /**
1534 * Test unhandledMessage.
1535 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001536 class StateMachineUnhandledMessage extends StateMachine {
Wink Savillefc5b4802009-12-08 21:22:24 -08001537 StateMachineUnhandledMessage(String name) {
1538 super(name);
1539 mThisSm = this;
1540 setDbg(DBG);
1541
1542 // Setup state machine with 1 state
1543 addState(mS1);
1544
1545 // Set the initial state
1546 setInitialState(mS1);
1547 }
Wink Saville64c42ca2011-04-18 14:55:10 -07001548 @Override
1549 public void unhandledMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001550 mUnhandledMessageCount += 1;
1551 }
1552
Wink Saville64c42ca2011-04-18 14:55:10 -07001553 class S1 extends State {
1554 @Override
1555 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001556 if (message.what == TEST_CMD_2) {
1557 transitionToHaltingState();
1558 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001559 return NOT_HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001560 }
1561 }
1562
1563 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001564 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001565 synchronized (mThisSm) {
1566 mThisSm.notifyAll();
1567 }
1568 }
1569
1570 private StateMachineUnhandledMessage mThisSm;
1571 private int mUnhandledMessageCount;
1572 private S1 mS1 = new S1();
1573 }
1574
1575 @SmallTest
1576 public void testStateMachineUnhandledMessage() throws Exception {
1577
Wink Savilleefcc3d32013-01-30 11:21:22 -08001578 StateMachineUnhandledMessage sm = new StateMachineUnhandledMessage("smUnhandledMessage");
Wink Savillefc5b4802009-12-08 21:22:24 -08001579 sm.start();
Wink Savilleefcc3d32013-01-30 11:21:22 -08001580 if (sm.isDbg()) tlog("testStateMachineUnhandledMessage E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001581
1582 synchronized (sm) {
1583 // Send 2 messages
1584 for (int i = 1; i <= 2; i++) {
Wink Saville91fbd562010-03-17 17:12:43 -07001585 sm.sendMessage(i);
Wink Savillefc5b4802009-12-08 21:22:24 -08001586 }
1587
1588 try {
1589 // wait for the messages to be handled
1590 sm.wait();
1591 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001592 tloge("testStateMachineUnhandledMessage: exception while waiting "
Wink Savillefc5b4802009-12-08 21:22:24 -08001593 + e.getMessage());
1594 }
1595 }
1596
Wink Savilleefcc3d32013-01-30 11:21:22 -08001597 assertEquals(2, sm.getLogRecSize());
Wink Savillefc5b4802009-12-08 21:22:24 -08001598 assertEquals(2, sm.mUnhandledMessageCount);
1599
Wink Savilleefcc3d32013-01-30 11:21:22 -08001600 if (sm.isDbg()) tlog("testStateMachineUnhandledMessage X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001601 }
1602
1603 /**
1604 * Test state machines sharing the same thread/looper. Multiple instances
1605 * of the same state machine will be created. They will all share the
1606 * same thread and thus each can update <code>sharedCounter</code> which
1607 * will be used to notify testStateMachineSharedThread that the test is
1608 * complete.
1609 */
Wink Saville64c42ca2011-04-18 14:55:10 -07001610 class StateMachineSharedThread extends StateMachine {
Wink Savillef0f566e2010-03-11 14:03:50 -08001611 StateMachineSharedThread(String name, Looper looper, int maxCount) {
1612 super(name, looper);
Wink Savillefc5b4802009-12-08 21:22:24 -08001613 mMaxCount = maxCount;
1614 setDbg(DBG);
1615
1616 // Setup state machine with 1 state
1617 addState(mS1);
1618
1619 // Set the initial state
1620 setInitialState(mS1);
1621 }
1622
Wink Saville64c42ca2011-04-18 14:55:10 -07001623 class S1 extends State {
1624 @Override
1625 public boolean processMessage(Message message) {
Wink Savillefc5b4802009-12-08 21:22:24 -08001626 if (message.what == TEST_CMD_4) {
1627 transitionToHaltingState();
1628 }
Wink Savillea4f3bec2010-05-19 09:11:38 -07001629 return HANDLED;
Wink Savillefc5b4802009-12-08 21:22:24 -08001630 }
1631 }
1632
1633 @Override
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001634 protected void onHalting() {
Wink Savillefc5b4802009-12-08 21:22:24 -08001635 // Update the shared counter, which is OK since all state
1636 // machines are using the same thread.
1637 sharedCounter += 1;
1638 if (sharedCounter == mMaxCount) {
1639 synchronized (waitObject) {
1640 waitObject.notifyAll();
1641 }
1642 }
1643 }
1644
1645 private int mMaxCount;
1646 private S1 mS1 = new S1();
1647 }
1648 private static int sharedCounter = 0;
1649 private static Object waitObject = new Object();
1650
Brett Chabotf76c56b2010-07-26 17:28:17 -07001651 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001652 public void testStateMachineSharedThread() throws Exception {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001653 if (DBG) tlog("testStateMachineSharedThread E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001654
1655 // Create and start the handler thread
1656 HandlerThread smThread = new HandlerThread("testStateMachineSharedThread");
1657 smThread.start();
1658
1659 // Create the state machines
1660 StateMachineSharedThread sms[] = new StateMachineSharedThread[10];
1661 for (int i = 0; i < sms.length; i++) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001662 sms[i] = new StateMachineSharedThread("smSharedThread",
1663 smThread.getLooper(), sms.length);
Wink Savillefc5b4802009-12-08 21:22:24 -08001664 sms[i].start();
1665 }
1666
1667 synchronized (waitObject) {
1668 // Send messages to each of the state machines
1669 for (StateMachineSharedThread sm : sms) {
1670 for (int i = 1; i <= 4; i++) {
Wink Saville91fbd562010-03-17 17:12:43 -07001671 sm.sendMessage(i);
Wink Savillefc5b4802009-12-08 21:22:24 -08001672 }
1673 }
1674
1675 // Wait for the last state machine to notify its done
1676 try {
1677 waitObject.wait();
1678 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001679 tloge("testStateMachineSharedThread: exception while waiting "
Wink Savillefc5b4802009-12-08 21:22:24 -08001680 + e.getMessage());
1681 }
1682 }
1683
1684 for (StateMachineSharedThread sm : sms) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001685 assertEquals(4, sm.getLogRecCount());
1686 for (int i = 0; i < sm.getLogRecSize(); i++) {
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001687 LogRec lr = sm.getLogRec(i);
1688 assertEquals(i+1, lr.getWhat());
1689 assertEquals(sm.mS1, lr.getState());
1690 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001691 }
1692 }
1693
Wink Savilleefcc3d32013-01-30 11:21:22 -08001694 if (DBG) tlog("testStateMachineSharedThread X");
1695 }
1696
1697 static class Hsm1 extends StateMachine {
1698 private static final String HSM1_TAG = "hsm1";
1699
1700 public static final int CMD_1 = 1;
1701 public static final int CMD_2 = 2;
1702 public static final int CMD_3 = 3;
1703 public static final int CMD_4 = 4;
1704 public static final int CMD_5 = 5;
1705
1706 public static Hsm1 makeHsm1() {
1707 Log.d(HSM1_TAG, "makeHsm1 E");
1708 Hsm1 sm = new Hsm1(HSM1_TAG);
1709 sm.start();
1710 Log.d(HSM1_TAG, "makeHsm1 X");
1711 return sm;
1712 }
1713
1714 Hsm1(String name) {
1715 super(name);
Mitchell Wills009ae992016-08-09 13:33:20 -07001716 tlog("ctor E");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001717
1718 // Add states, use indentation to show hierarchy
1719 addState(mP1);
1720 addState(mS1, mP1);
1721 addState(mS2, mP1);
1722 addState(mP2);
1723
1724 // Set the initial state
1725 setInitialState(mS1);
Mitchell Wills009ae992016-08-09 13:33:20 -07001726 tlog("ctor X");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001727 }
1728
1729 class P1 extends State {
1730 @Override
1731 public void enter() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001732 tlog("P1.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001733 }
1734 @Override
1735 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001736 tlog("P1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001737 }
1738 @Override
1739 public boolean processMessage(Message message) {
1740 boolean retVal;
Mitchell Wills009ae992016-08-09 13:33:20 -07001741 tlog("P1.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001742 switch(message.what) {
1743 case CMD_2:
1744 // CMD_2 will arrive in mS2 before CMD_3
1745 sendMessage(CMD_3);
1746 deferMessage(message);
1747 transitionTo(mS2);
1748 retVal = true;
1749 break;
1750 default:
1751 // Any message we don't understand in this state invokes unhandledMessage
1752 retVal = false;
1753 break;
1754 }
1755 return retVal;
1756 }
1757 }
1758
1759 class S1 extends State {
1760 @Override
1761 public void enter() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001762 tlog("S1.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001763 }
1764 @Override
1765 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001766 tlog("S1.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001767 }
1768 @Override
1769 public boolean processMessage(Message message) {
Mitchell Wills009ae992016-08-09 13:33:20 -07001770 tlog("S1.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001771 if (message.what == CMD_1) {
1772 // Transition to ourself to show that enter/exit is called
1773 transitionTo(mS1);
1774 return HANDLED;
1775 } else {
1776 // Let parent process all other messages
1777 return NOT_HANDLED;
1778 }
1779 }
1780 }
1781
1782 class S2 extends State {
1783 @Override
1784 public void enter() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001785 tlog("S2.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001786 }
1787 @Override
1788 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001789 tlog("S2.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001790 }
1791 @Override
1792 public boolean processMessage(Message message) {
1793 boolean retVal;
Mitchell Wills009ae992016-08-09 13:33:20 -07001794 tlog("S2.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001795 switch(message.what) {
1796 case(CMD_2):
1797 sendMessage(CMD_4);
1798 retVal = true;
1799 break;
1800 case(CMD_3):
1801 deferMessage(message);
1802 transitionTo(mP2);
1803 retVal = true;
1804 break;
1805 default:
1806 retVal = false;
1807 break;
1808 }
1809 return retVal;
1810 }
1811 }
1812
1813 class P2 extends State {
1814 @Override
1815 public void enter() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001816 tlog("P2.enter");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001817 sendMessage(CMD_5);
1818 }
1819 @Override
1820 public void exit() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001821 tlog("P2.exit");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001822 }
1823 @Override
1824 public boolean processMessage(Message message) {
Mitchell Wills009ae992016-08-09 13:33:20 -07001825 tlog("P2.processMessage what=" + message.what);
Wink Savilleefcc3d32013-01-30 11:21:22 -08001826 switch(message.what) {
1827 case(CMD_3):
1828 break;
1829 case(CMD_4):
1830 break;
1831 case(CMD_5):
1832 transitionToHaltingState();
1833 break;
1834 }
1835 return HANDLED;
1836 }
1837 }
1838
1839 @Override
1840 protected void onHalting() {
Mitchell Wills009ae992016-08-09 13:33:20 -07001841 tlog("halting");
Wink Savilleefcc3d32013-01-30 11:21:22 -08001842 synchronized (this) {
1843 this.notifyAll();
1844 }
1845 }
1846
1847 P1 mP1 = new P1();
1848 S1 mS1 = new S1();
1849 S2 mS2 = new S2();
1850 P2 mP2 = new P2();
Wink Savillefc5b4802009-12-08 21:22:24 -08001851 }
1852
Brett Chabotf76c56b2010-07-26 17:28:17 -07001853 @MediumTest
Wink Savillefc5b4802009-12-08 21:22:24 -08001854 public void testHsm1() throws Exception {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001855 if (DBG) tlog("testHsm1 E");
Wink Savillefc5b4802009-12-08 21:22:24 -08001856
1857 Hsm1 sm = Hsm1.makeHsm1();
1858
1859 // Send messages
Wink Saville91fbd562010-03-17 17:12:43 -07001860 sm.sendMessage(Hsm1.CMD_1);
1861 sm.sendMessage(Hsm1.CMD_2);
Wink Savillefc5b4802009-12-08 21:22:24 -08001862
1863 synchronized (sm) {
1864 // Wait for the last state machine to notify its done
1865 try {
1866 sm.wait();
1867 } catch (InterruptedException e) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001868 tloge("testHsm1: exception while waiting " + e.getMessage());
Wink Savillefc5b4802009-12-08 21:22:24 -08001869 }
1870 }
1871
Wink Savilleefcc3d32013-01-30 11:21:22 -08001872 dumpLogRecs(sm);
1873
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001874 assertEquals(7, sm.getLogRecCount());
Wink Savilleefcc3d32013-01-30 11:21:22 -08001875
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001876 LogRec lr = sm.getLogRec(0);
1877 assertEquals(Hsm1.CMD_1, lr.getWhat());
1878 assertEquals(sm.mS1, lr.getState());
1879 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001880
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001881 lr = sm.getLogRec(1);
1882 assertEquals(Hsm1.CMD_2, lr.getWhat());
1883 assertEquals(sm.mP1, lr.getState());
1884 assertEquals(sm.mS1, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001885
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001886 lr = sm.getLogRec(2);
1887 assertEquals(Hsm1.CMD_2, lr.getWhat());
1888 assertEquals(sm.mS2, lr.getState());
1889 assertEquals(sm.mS2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001890
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001891 lr = sm.getLogRec(3);
1892 assertEquals(Hsm1.CMD_3, lr.getWhat());
1893 assertEquals(sm.mS2, lr.getState());
1894 assertEquals(sm.mS2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001895
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001896 lr = sm.getLogRec(4);
1897 assertEquals(Hsm1.CMD_3, lr.getWhat());
1898 assertEquals(sm.mP2, lr.getState());
1899 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001900
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001901 lr = sm.getLogRec(5);
1902 assertEquals(Hsm1.CMD_4, lr.getWhat());
1903 assertEquals(sm.mP2, lr.getState());
1904 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001905
Wink Savillebbf30dfd2012-05-29 12:40:46 -07001906 lr = sm.getLogRec(6);
1907 assertEquals(Hsm1.CMD_5, lr.getWhat());
1908 assertEquals(sm.mP2, lr.getState());
1909 assertEquals(sm.mP2, lr.getOriginalState());
Wink Savillefc5b4802009-12-08 21:22:24 -08001910
Wink Savilleefcc3d32013-01-30 11:21:22 -08001911 if (DBG) tlog("testStateMachineSharedThread X");
Wink Savillefc5b4802009-12-08 21:22:24 -08001912 }
1913
Mitchell Wills009ae992016-08-09 13:33:20 -07001914 private static void tlog(String s) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001915 Log.d(TAG, s);
Wink Savillefc5b4802009-12-08 21:22:24 -08001916 }
1917
Mitchell Wills009ae992016-08-09 13:33:20 -07001918 private static void tloge(String s) {
Wink Savilleefcc3d32013-01-30 11:21:22 -08001919 Log.e(TAG, s);
Wink Savillefc5b4802009-12-08 21:22:24 -08001920 }
Wink Savillefc5b4802009-12-08 21:22:24 -08001921}