1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
|
/**
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.internal.util;
import android.os.Debug;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import com.android.internal.util.State;
import com.android.internal.util.StateMachine;
import com.android.internal.util.StateMachine.ProcessedMessageInfo;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import junit.framework.TestCase;
/**
* Test for StateMachine.
*/
public class StateMachineTest extends TestCase {
private static final int TEST_CMD_1 = 1;
private static final int TEST_CMD_2 = 2;
private static final int TEST_CMD_3 = 3;
private static final int TEST_CMD_4 = 4;
private static final int TEST_CMD_5 = 5;
private static final int TEST_CMD_6 = 6;
private static final boolean DBG = true;
private static final boolean WAIT_FOR_DEBUGGER = false;
private static final String TAG = "StateMachineTest";
/**
* Tests that we can quit the state machine.
*/
class StateMachineQuitTest extends StateMachine {
private int mQuitCount = 0;
StateMachineQuitTest(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
}
class S1 extends State {
@Override
public boolean processMessage(Message message) {
if (isQuit(message)) {
mQuitCount += 1;
if (mQuitCount > 2) {
// Returning NOT_HANDLED to actually quit
return NOT_HANDLED;
} else {
// Do NOT quit
return HANDLED;
}
} else {
// All other message are handled
return HANDLED;
}
}
}
@Override
protected void quitting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachineQuitTest mThisSm;
private S1 mS1 = new S1();
}
@SmallTest
public void testStateMachineQuitTest() throws Exception {
if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachineQuitTest smQuitTest = new StateMachineQuitTest("smQuitTest");
smQuitTest.start();
if (smQuitTest.isDbg()) Log.d(TAG, "testStateMachineQuitTest E");
synchronized (smQuitTest) {
// Send 6 messages
for (int i = 1; i <= 6; i++) {
smQuitTest.sendMessage(i);
}
// First two are ignored
smQuitTest.quit();
smQuitTest.quit();
// Now we will quit
smQuitTest.quit();
try {
// wait for the messages to be handled
smQuitTest.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachineQuitTest: exception while waiting " + e.getMessage());
}
}
assertTrue(smQuitTest.getProcessedMessagesCount() == 9);
ProcessedMessageInfo pmi;
// The first two message didn't quit and were handled by mS1
pmi = smQuitTest.getProcessedMessageInfo(6);
assertEquals(StateMachine.SM_QUIT_CMD, pmi.getWhat());
assertEquals(smQuitTest.mS1, pmi.getState());
assertEquals(smQuitTest.mS1, pmi.getOriginalState());
pmi = smQuitTest.getProcessedMessageInfo(7);
assertEquals(StateMachine.SM_QUIT_CMD, pmi.getWhat());
assertEquals(smQuitTest.mS1, pmi.getState());
assertEquals(smQuitTest.mS1, pmi.getOriginalState());
// The last message was never handled so the states are null
pmi = smQuitTest.getProcessedMessageInfo(8);
assertEquals(StateMachine.SM_QUIT_CMD, pmi.getWhat());
assertEquals(null, pmi.getState());
assertEquals(null, pmi.getOriginalState());
if (smQuitTest.isDbg()) Log.d(TAG, "testStateMachineQuitTest X");
}
/**
* Test enter/exit can use transitionTo
*/
class StateMachineEnterExitTransitionToTest extends StateMachine {
StateMachineEnterExitTransitionToTest(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
addState(mS2);
addState(mS3);
addState(mS4);
// Set the initial state
setInitialState(mS1);
}
class S1 extends State {
@Override
public void enter() {
// Test that message is HSM_INIT_CMD
assertEquals(SM_INIT_CMD, getCurrentMessage().what);
// Test that a transition in enter and the initial state works
mS1EnterCount += 1;
transitionTo(mS2);
Log.d(TAG, "S1.enter");
}
@Override
public void exit() {
// Test that message is HSM_INIT_CMD
assertEquals(SM_INIT_CMD, getCurrentMessage().what);
mS1ExitCount += 1;
Log.d(TAG, "S1.exit");
}
}
class S2 extends State {
@Override
public void enter() {
// Test that message is HSM_INIT_CMD
assertEquals(SM_INIT_CMD, getCurrentMessage().what);
mS2EnterCount += 1;
Log.d(TAG, "S2.enter");
}
@Override
public void exit() {
// Test that message is TEST_CMD_1
assertEquals(TEST_CMD_1, getCurrentMessage().what);
// Test transition in exit work
mS2ExitCount += 1;
transitionTo(mS4);
Log.d(TAG, "S2.exit");
}
@Override
public boolean processMessage(Message message) {
// Start a transition to S3 but it will be
// changed to a transition to S4 in exit
transitionTo(mS3);
Log.d(TAG, "S2.processMessage");
return HANDLED;
}
}
class S3 extends State {
@Override
public void enter() {
// Test that we can do halting in an enter/exit
transitionToHaltingState();
mS3EnterCount += 1;
Log.d(TAG, "S3.enter");
}
@Override
public void exit() {
mS3ExitCount += 1;
Log.d(TAG, "S3.exit");
}
}
class S4 extends State {
@Override
public void enter() {
// Test that we can do halting in an enter/exit
transitionToHaltingState();
mS4EnterCount += 1;
Log.d(TAG, "S4.enter");
}
@Override
public void exit() {
mS4ExitCount += 1;
Log.d(TAG, "S4.exit");
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachineEnterExitTransitionToTest mThisSm;
private S1 mS1 = new S1();
private S2 mS2 = new S2();
private S3 mS3 = new S3();
private S4 mS4 = new S4();
private int mS1EnterCount = 0;
private int mS1ExitCount = 0;
private int mS2EnterCount = 0;
private int mS2ExitCount = 0;
private int mS3EnterCount = 0;
private int mS3ExitCount = 0;
private int mS4EnterCount = 0;
private int mS4ExitCount = 0;
}
@SmallTest
public void testStateMachineEnterExitTransitionToTest() throws Exception {
//if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachineEnterExitTransitionToTest smEnterExitTranstionToTest =
new StateMachineEnterExitTransitionToTest("smEnterExitTranstionToTest");
smEnterExitTranstionToTest.start();
if (smEnterExitTranstionToTest.isDbg()) {
Log.d(TAG, "testStateMachineEnterExitTransitionToTest E");
}
synchronized (smEnterExitTranstionToTest) {
smEnterExitTranstionToTest.sendMessage(TEST_CMD_1);
try {
// wait for the messages to be handled
smEnterExitTranstionToTest.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachineEnterExitTransitionToTest: exception while waiting "
+ e.getMessage());
}
}
assertTrue(smEnterExitTranstionToTest.getProcessedMessagesCount() == 1);
ProcessedMessageInfo pmi;
// Message should be handled by mS2.
pmi = smEnterExitTranstionToTest.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(smEnterExitTranstionToTest.mS2, pmi.getState());
assertEquals(smEnterExitTranstionToTest.mS2, pmi.getOriginalState());
assertEquals(smEnterExitTranstionToTest.mS1EnterCount, 1);
assertEquals(smEnterExitTranstionToTest.mS1ExitCount, 1);
assertEquals(smEnterExitTranstionToTest.mS2EnterCount, 1);
assertEquals(smEnterExitTranstionToTest.mS2ExitCount, 1);
assertEquals(smEnterExitTranstionToTest.mS3EnterCount, 1);
assertEquals(smEnterExitTranstionToTest.mS3ExitCount, 1);
assertEquals(smEnterExitTranstionToTest.mS3EnterCount, 1);
assertEquals(smEnterExitTranstionToTest.mS3ExitCount, 1);
if (smEnterExitTranstionToTest.isDbg()) {
Log.d(TAG, "testStateMachineEnterExitTransitionToTest X");
}
}
/**
* Tests that ProcessedMessage works as a circular buffer.
*/
class StateMachine0 extends StateMachine {
StateMachine0(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
setProcessedMessagesSize(3);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
}
class S1 extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_6) {
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine0 mThisSm;
private S1 mS1 = new S1();
}
@SmallTest
public void testStateMachine0() throws Exception {
//if (WAIT_FOR_DEBUGGER) Debug.waitForDebugger();
StateMachine0 sm0 = new StateMachine0("sm0");
sm0.start();
if (sm0.isDbg()) Log.d(TAG, "testStateMachine0 E");
synchronized (sm0) {
// Send 6 messages
for (int i = 1; i <= 6; i++) {
sm0.sendMessage(sm0.obtainMessage(i));
}
try {
// wait for the messages to be handled
sm0.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine0: exception while waiting " + e.getMessage());
}
}
assertTrue(sm0.getProcessedMessagesCount() == 6);
assertTrue(sm0.getProcessedMessagesSize() == 3);
ProcessedMessageInfo pmi;
pmi = sm0.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_4, pmi.getWhat());
assertEquals(sm0.mS1, pmi.getState());
assertEquals(sm0.mS1, pmi.getOriginalState());
pmi = sm0.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_5, pmi.getWhat());
assertEquals(sm0.mS1, pmi.getState());
assertEquals(sm0.mS1, pmi.getOriginalState());
pmi = sm0.getProcessedMessageInfo(2);
assertEquals(TEST_CMD_6, pmi.getWhat());
assertEquals(sm0.mS1, pmi.getState());
assertEquals(sm0.mS1, pmi.getOriginalState());
if (sm0.isDbg()) Log.d(TAG, "testStateMachine0 X");
}
/**
* This tests enter/exit and transitions to the same state.
* The state machine has one state, it receives two messages
* in state mS1. With the first message it transitions to
* itself which causes it to be exited and reentered.
*/
class StateMachine1 extends StateMachine {
StateMachine1(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
if (DBG) Log.d(TAG, "StateMachine1: ctor X");
}
class S1 extends State {
@Override
public void enter() {
mEnterCount++;
}
@Override
public void exit() {
mExitCount++;
}
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_1) {
assertEquals(1, mEnterCount);
assertEquals(0, mExitCount);
transitionTo(mS1);
} else if (message.what == TEST_CMD_2) {
assertEquals(2, mEnterCount);
assertEquals(1, mExitCount);
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine1 mThisSm;
private S1 mS1 = new S1();
private int mEnterCount;
private int mExitCount;
}
@MediumTest
public void testStateMachine1() throws Exception {
StateMachine1 sm1 = new StateMachine1("sm1");
sm1.start();
if (sm1.isDbg()) Log.d(TAG, "testStateMachine1 E");
synchronized (sm1) {
// Send two messages
sm1.sendMessage(TEST_CMD_1);
sm1.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm1.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine1: exception while waiting " + e.getMessage());
}
}
assertEquals(2, sm1.mEnterCount);
assertEquals(2, sm1.mExitCount);
assertTrue(sm1.getProcessedMessagesSize() == 2);
ProcessedMessageInfo pmi;
pmi = sm1.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm1.mS1, pmi.getState());
assertEquals(sm1.mS1, pmi.getOriginalState());
pmi = sm1.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm1.mS1, pmi.getState());
assertEquals(sm1.mS1, pmi.getOriginalState());
assertEquals(2, sm1.mEnterCount);
assertEquals(2, sm1.mExitCount);
if (sm1.isDbg()) Log.d(TAG, "testStateMachine1 X");
}
/**
* Test deferring messages and states with no parents. The state machine
* has two states, it receives two messages in state mS1 deferring them
* until what == TEST_CMD_2 and then transitions to state mS2. State
* mS2 then receives both of the deferred messages first TEST_CMD_1 and
* then TEST_CMD_2.
*/
class StateMachine2 extends StateMachine {
StateMachine2(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup the hierarchy
addState(mS1);
addState(mS2);
// Set the initial state
setInitialState(mS1);
if (DBG) Log.d(TAG, "StateMachine2: ctor X");
}
class S1 extends State {
@Override
public void enter() {
mDidEnter = true;
}
@Override
public void exit() {
mDidExit = true;
}
@Override
public boolean processMessage(Message message) {
deferMessage(message);
if (message.what == TEST_CMD_2) {
transitionTo(mS2);
}
return HANDLED;
}
}
class S2 extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_2) {
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine2 mThisSm;
private S1 mS1 = new S1();
private S2 mS2 = new S2();
private boolean mDidEnter = false;
private boolean mDidExit = false;
}
@MediumTest
public void testStateMachine2() throws Exception {
StateMachine2 sm2 = new StateMachine2("sm2");
sm2.start();
if (sm2.isDbg()) Log.d(TAG, "testStateMachine2 E");
synchronized (sm2) {
// Send two messages
sm2.sendMessage(TEST_CMD_1);
sm2.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm2.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine2: exception while waiting " + e.getMessage());
}
}
assertTrue(sm2.getProcessedMessagesSize() == 4);
ProcessedMessageInfo pmi;
pmi = sm2.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm2.mS1, pmi.getState());
pmi = sm2.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm2.mS1, pmi.getState());
pmi = sm2.getProcessedMessageInfo(2);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm2.mS2, pmi.getState());
pmi = sm2.getProcessedMessageInfo(3);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm2.mS2, pmi.getState());
assertTrue(sm2.mDidEnter);
assertTrue(sm2.mDidExit);
if (sm2.isDbg()) Log.d(TAG, "testStateMachine2 X");
}
/**
* Test that unhandled messages in a child are handled by the parent.
* When TEST_CMD_2 is received.
*/
class StateMachine3 extends StateMachine {
StateMachine3(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup the simplest hierarchy of two states
// mParentState and mChildState.
// (Use indentation to help visualize hierarchy)
addState(mParentState);
addState(mChildState, mParentState);
// Set the initial state will be the child
setInitialState(mChildState);
if (DBG) Log.d(TAG, "StateMachine3: ctor X");
}
class ParentState extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_2) {
transitionToHaltingState();
}
return HANDLED;
}
}
class ChildState extends State {
@Override
public boolean processMessage(Message message) {
return NOT_HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine3 mThisSm;
private ParentState mParentState = new ParentState();
private ChildState mChildState = new ChildState();
}
@MediumTest
public void testStateMachine3() throws Exception {
StateMachine3 sm3 = new StateMachine3("sm3");
sm3.start();
if (sm3.isDbg()) Log.d(TAG, "testStateMachine3 E");
synchronized (sm3) {
// Send two messages
sm3.sendMessage(TEST_CMD_1);
sm3.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm3.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine3: exception while waiting " + e.getMessage());
}
}
assertTrue(sm3.getProcessedMessagesSize() == 2);
ProcessedMessageInfo pmi;
pmi = sm3.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm3.mParentState, pmi.getState());
assertEquals(sm3.mChildState, pmi.getOriginalState());
pmi = sm3.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm3.mParentState, pmi.getState());
assertEquals(sm3.mChildState, pmi.getOriginalState());
if (sm3.isDbg()) Log.d(TAG, "testStateMachine3 X");
}
/**
* Test a hierarchy of 3 states a parent and two children
* with transition from child 1 to child 2 and child 2
* lets the parent handle the messages.
*/
class StateMachine4 extends StateMachine {
StateMachine4(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup a hierarchy of three states
// mParentState, mChildState1 & mChildState2
// (Use indentation to help visualize hierarchy)
addState(mParentState);
addState(mChildState1, mParentState);
addState(mChildState2, mParentState);
// Set the initial state will be child 1
setInitialState(mChildState1);
if (DBG) Log.d(TAG, "StateMachine4: ctor X");
}
class ParentState extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_2) {
transitionToHaltingState();
}
return HANDLED;
}
}
class ChildState1 extends State {
@Override
public boolean processMessage(Message message) {
transitionTo(mChildState2);
return HANDLED;
}
}
class ChildState2 extends State {
@Override
public boolean processMessage(Message message) {
return NOT_HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine4 mThisSm;
private ParentState mParentState = new ParentState();
private ChildState1 mChildState1 = new ChildState1();
private ChildState2 mChildState2 = new ChildState2();
}
@MediumTest
public void testStateMachine4() throws Exception {
StateMachine4 sm4 = new StateMachine4("sm4");
sm4.start();
if (sm4.isDbg()) Log.d(TAG, "testStateMachine4 E");
synchronized (sm4) {
// Send two messages
sm4.sendMessage(TEST_CMD_1);
sm4.sendMessage(TEST_CMD_2);
try {
// wait for the messages to be handled
sm4.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine4: exception while waiting " + e.getMessage());
}
}
assertTrue(sm4.getProcessedMessagesSize() == 2);
ProcessedMessageInfo pmi;
pmi = sm4.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm4.mChildState1, pmi.getState());
assertEquals(sm4.mChildState1, pmi.getOriginalState());
pmi = sm4.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm4.mParentState, pmi.getState());
assertEquals(sm4.mChildState2, pmi.getOriginalState());
if (sm4.isDbg()) Log.d(TAG, "testStateMachine4 X");
}
/**
* Test transition from one child to another of a "complex"
* hierarchy with two parents and multiple children.
*/
class StateMachine5 extends StateMachine {
StateMachine5(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup a hierarchy with two parents and some children.
// (Use indentation to help visualize hierarchy)
addState(mParentState1);
addState(mChildState1, mParentState1);
addState(mChildState2, mParentState1);
addState(mParentState2);
addState(mChildState3, mParentState2);
addState(mChildState4, mParentState2);
addState(mChildState5, mChildState4);
// Set the initial state will be the child
setInitialState(mChildState1);
if (DBG) Log.d(TAG, "StateMachine5: ctor X");
}
class ParentState1 extends State {
@Override
public void enter() {
mParentState1EnterCount += 1;
}
@Override
public void exit() {
mParentState1ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
return HANDLED;
}
}
class ChildState1 extends State {
@Override
public void enter() {
mChildState1EnterCount += 1;
}
@Override
public void exit() {
mChildState1ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(0, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(0, mChildState1ExitCount);
assertEquals(0, mChildState2EnterCount);
assertEquals(0, mChildState2ExitCount);
assertEquals(0, mParentState2EnterCount);
assertEquals(0, mParentState2ExitCount);
assertEquals(0, mChildState3EnterCount);
assertEquals(0, mChildState3ExitCount);
assertEquals(0, mChildState4EnterCount);
assertEquals(0, mChildState4ExitCount);
assertEquals(0, mChildState5EnterCount);
assertEquals(0, mChildState5ExitCount);
transitionTo(mChildState2);
return HANDLED;
}
}
class ChildState2 extends State {
@Override
public void enter() {
mChildState2EnterCount += 1;
}
@Override
public void exit() {
mChildState2ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(0, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(1, mChildState1ExitCount);
assertEquals(1, mChildState2EnterCount);
assertEquals(0, mChildState2ExitCount);
assertEquals(0, mParentState2EnterCount);
assertEquals(0, mParentState2ExitCount);
assertEquals(0, mChildState3EnterCount);
assertEquals(0, mChildState3ExitCount);
assertEquals(0, mChildState4EnterCount);
assertEquals(0, mChildState4ExitCount);
assertEquals(0, mChildState5EnterCount);
assertEquals(0, mChildState5ExitCount);
transitionTo(mChildState5);
return HANDLED;
}
}
class ParentState2 extends State {
@Override
public void enter() {
mParentState2EnterCount += 1;
}
@Override
public void exit() {
mParentState2ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(1, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(1, mChildState1ExitCount);
assertEquals(1, mChildState2EnterCount);
assertEquals(1, mChildState2ExitCount);
assertEquals(2, mParentState2EnterCount);
assertEquals(1, mParentState2ExitCount);
assertEquals(1, mChildState3EnterCount);
assertEquals(1, mChildState3ExitCount);
assertEquals(2, mChildState4EnterCount);
assertEquals(2, mChildState4ExitCount);
assertEquals(1, mChildState5EnterCount);
assertEquals(1, mChildState5ExitCount);
transitionToHaltingState();
return HANDLED;
}
}
class ChildState3 extends State {
@Override
public void enter() {
mChildState3EnterCount += 1;
}
@Override
public void exit() {
mChildState3ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(1, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(1, mChildState1ExitCount);
assertEquals(1, mChildState2EnterCount);
assertEquals(1, mChildState2ExitCount);
assertEquals(1, mParentState2EnterCount);
assertEquals(0, mParentState2ExitCount);
assertEquals(1, mChildState3EnterCount);
assertEquals(0, mChildState3ExitCount);
assertEquals(1, mChildState4EnterCount);
assertEquals(1, mChildState4ExitCount);
assertEquals(1, mChildState5EnterCount);
assertEquals(1, mChildState5ExitCount);
transitionTo(mChildState4);
return HANDLED;
}
}
class ChildState4 extends State {
@Override
public void enter() {
mChildState4EnterCount += 1;
}
@Override
public void exit() {
mChildState4ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(1, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(1, mChildState1ExitCount);
assertEquals(1, mChildState2EnterCount);
assertEquals(1, mChildState2ExitCount);
assertEquals(1, mParentState2EnterCount);
assertEquals(0, mParentState2ExitCount);
assertEquals(1, mChildState3EnterCount);
assertEquals(1, mChildState3ExitCount);
assertEquals(2, mChildState4EnterCount);
assertEquals(1, mChildState4ExitCount);
assertEquals(1, mChildState5EnterCount);
assertEquals(1, mChildState5ExitCount);
transitionTo(mParentState2);
return HANDLED;
}
}
class ChildState5 extends State {
@Override
public void enter() {
mChildState5EnterCount += 1;
}
@Override
public void exit() {
mChildState5ExitCount += 1;
}
@Override
public boolean processMessage(Message message) {
assertEquals(1, mParentState1EnterCount);
assertEquals(1, mParentState1ExitCount);
assertEquals(1, mChildState1EnterCount);
assertEquals(1, mChildState1ExitCount);
assertEquals(1, mChildState2EnterCount);
assertEquals(1, mChildState2ExitCount);
assertEquals(1, mParentState2EnterCount);
assertEquals(0, mParentState2ExitCount);
assertEquals(0, mChildState3EnterCount);
assertEquals(0, mChildState3ExitCount);
assertEquals(1, mChildState4EnterCount);
assertEquals(0, mChildState4ExitCount);
assertEquals(1, mChildState5EnterCount);
assertEquals(0, mChildState5ExitCount);
transitionTo(mChildState3);
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine5 mThisSm;
private ParentState1 mParentState1 = new ParentState1();
private ChildState1 mChildState1 = new ChildState1();
private ChildState2 mChildState2 = new ChildState2();
private ParentState2 mParentState2 = new ParentState2();
private ChildState3 mChildState3 = new ChildState3();
private ChildState4 mChildState4 = new ChildState4();
private ChildState5 mChildState5 = new ChildState5();
private int mParentState1EnterCount = 0;
private int mParentState1ExitCount = 0;
private int mChildState1EnterCount = 0;
private int mChildState1ExitCount = 0;
private int mChildState2EnterCount = 0;
private int mChildState2ExitCount = 0;
private int mParentState2EnterCount = 0;
private int mParentState2ExitCount = 0;
private int mChildState3EnterCount = 0;
private int mChildState3ExitCount = 0;
private int mChildState4EnterCount = 0;
private int mChildState4ExitCount = 0;
private int mChildState5EnterCount = 0;
private int mChildState5ExitCount = 0;
}
@MediumTest
public void testStateMachine5() throws Exception {
StateMachine5 sm5 = new StateMachine5("sm5");
sm5.start();
if (sm5.isDbg()) Log.d(TAG, "testStateMachine5 E");
synchronized (sm5) {
// Send 6 messages
sm5.sendMessage(TEST_CMD_1);
sm5.sendMessage(TEST_CMD_2);
sm5.sendMessage(TEST_CMD_3);
sm5.sendMessage(TEST_CMD_4);
sm5.sendMessage(TEST_CMD_5);
sm5.sendMessage(TEST_CMD_6);
try {
// wait for the messages to be handled
sm5.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine5: exception while waiting " + e.getMessage());
}
}
assertTrue(sm5.getProcessedMessagesSize() == 6);
assertEquals(1, sm5.mParentState1EnterCount);
assertEquals(1, sm5.mParentState1ExitCount);
assertEquals(1, sm5.mChildState1EnterCount);
assertEquals(1, sm5.mChildState1ExitCount);
assertEquals(1, sm5.mChildState2EnterCount);
assertEquals(1, sm5.mChildState2ExitCount);
assertEquals(2, sm5.mParentState2EnterCount);
assertEquals(2, sm5.mParentState2ExitCount);
assertEquals(1, sm5.mChildState3EnterCount);
assertEquals(1, sm5.mChildState3ExitCount);
assertEquals(2, sm5.mChildState4EnterCount);
assertEquals(2, sm5.mChildState4ExitCount);
assertEquals(1, sm5.mChildState5EnterCount);
assertEquals(1, sm5.mChildState5ExitCount);
ProcessedMessageInfo pmi;
pmi = sm5.getProcessedMessageInfo(0);
assertEquals(TEST_CMD_1, pmi.getWhat());
assertEquals(sm5.mChildState1, pmi.getState());
assertEquals(sm5.mChildState1, pmi.getOriginalState());
pmi = sm5.getProcessedMessageInfo(1);
assertEquals(TEST_CMD_2, pmi.getWhat());
assertEquals(sm5.mChildState2, pmi.getState());
assertEquals(sm5.mChildState2, pmi.getOriginalState());
pmi = sm5.getProcessedMessageInfo(2);
assertEquals(TEST_CMD_3, pmi.getWhat());
assertEquals(sm5.mChildState5, pmi.getState());
assertEquals(sm5.mChildState5, pmi.getOriginalState());
pmi = sm5.getProcessedMessageInfo(3);
assertEquals(TEST_CMD_4, pmi.getWhat());
assertEquals(sm5.mChildState3, pmi.getState());
assertEquals(sm5.mChildState3, pmi.getOriginalState());
pmi = sm5.getProcessedMessageInfo(4);
assertEquals(TEST_CMD_5, pmi.getWhat());
assertEquals(sm5.mChildState4, pmi.getState());
assertEquals(sm5.mChildState4, pmi.getOriginalState());
pmi = sm5.getProcessedMessageInfo(5);
assertEquals(TEST_CMD_6, pmi.getWhat());
assertEquals(sm5.mParentState2, pmi.getState());
assertEquals(sm5.mParentState2, pmi.getOriginalState());
if (sm5.isDbg()) Log.d(TAG, "testStateMachine5 X");
}
/**
* Test that the initial state enter is invoked immediately
* after construction and before any other messages arrive and that
* sendMessageDelayed works.
*/
class StateMachine6 extends StateMachine {
StateMachine6(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
if (DBG) Log.d(TAG, "StateMachine6: ctor X");
}
class S1 extends State {
@Override
public void enter() {
sendMessage(TEST_CMD_1);
}
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_1) {
mArrivalTimeMsg1 = SystemClock.elapsedRealtime();
} else if (message.what == TEST_CMD_2) {
mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine6 mThisSm;
private S1 mS1 = new S1();
private long mArrivalTimeMsg1;
private long mArrivalTimeMsg2;
}
@MediumTest
public void testStateMachine6() throws Exception {
long sentTimeMsg2;
final int DELAY_TIME = 250;
final int DELAY_FUDGE = 20;
StateMachine6 sm6 = new StateMachine6("sm6");
sm6.start();
if (sm6.isDbg()) Log.d(TAG, "testStateMachine6 E");
synchronized (sm6) {
// Send a message
sentTimeMsg2 = SystemClock.elapsedRealtime();
sm6.sendMessageDelayed(TEST_CMD_2, DELAY_TIME);
try {
// wait for the messages to be handled
sm6.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine6: exception while waiting " + e.getMessage());
}
}
/**
* TEST_CMD_1 was sent in enter and must always have been processed
* immediately after construction and hence the arrival time difference
* should always >= to the DELAY_TIME
*/
long arrivalTimeDiff = sm6.mArrivalTimeMsg2 - sm6.mArrivalTimeMsg1;
long expectedDelay = DELAY_TIME - DELAY_FUDGE;
if (sm6.isDbg()) Log.d(TAG, "testStateMachine6: expect " + arrivalTimeDiff
+ " >= " + expectedDelay);
assertTrue(arrivalTimeDiff >= expectedDelay);
if (sm6.isDbg()) Log.d(TAG, "testStateMachine6 X");
}
/**
* Test that enter is invoked immediately after exit. This validates
* that enter can be used to send a watch dog message for its state.
*/
class StateMachine7 extends StateMachine {
private final int SM7_DELAY_TIME = 250;
StateMachine7(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
addState(mS2);
// Set the initial state
setInitialState(mS1);
if (DBG) Log.d(TAG, "StateMachine7: ctor X");
}
class S1 extends State {
@Override
public void exit() {
sendMessage(TEST_CMD_2);
}
@Override
public boolean processMessage(Message message) {
transitionTo(mS2);
return HANDLED;
}
}
class S2 extends State {
@Override
public void enter() {
// Send a delayed message as a watch dog
sendMessageDelayed(TEST_CMD_3, SM7_DELAY_TIME);
}
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_2) {
mMsgCount += 1;
mArrivalTimeMsg2 = SystemClock.elapsedRealtime();
} else if (message.what == TEST_CMD_3) {
mMsgCount += 1;
mArrivalTimeMsg3 = SystemClock.elapsedRealtime();
}
if (mMsgCount == 2) {
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachine7 mThisSm;
private S1 mS1 = new S1();
private S2 mS2 = new S2();
private int mMsgCount = 0;
private long mArrivalTimeMsg2;
private long mArrivalTimeMsg3;
}
@MediumTest
public void testStateMachine7() throws Exception {
long sentTimeMsg2;
final int SM7_DELAY_FUDGE = 20;
StateMachine7 sm7 = new StateMachine7("sm7");
sm7.start();
if (sm7.isDbg()) Log.d(TAG, "testStateMachine7 E");
synchronized (sm7) {
// Send a message
sentTimeMsg2 = SystemClock.elapsedRealtime();
sm7.sendMessage(TEST_CMD_1);
try {
// wait for the messages to be handled
sm7.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachine7: exception while waiting " + e.getMessage());
}
}
/**
* TEST_CMD_3 was sent in S2.enter with a delay and must always have been
* processed immediately after S1.exit. Since S1.exit sent TEST_CMD_2
* without a delay the arrival time difference should always >= to SM7_DELAY_TIME.
*/
long arrivalTimeDiff = sm7.mArrivalTimeMsg3 - sm7.mArrivalTimeMsg2;
long expectedDelay = sm7.SM7_DELAY_TIME - SM7_DELAY_FUDGE;
if (sm7.isDbg()) Log.d(TAG, "testStateMachine7: expect " + arrivalTimeDiff
+ " >= " + expectedDelay);
assertTrue(arrivalTimeDiff >= expectedDelay);
if (sm7.isDbg()) Log.d(TAG, "testStateMachine7 X");
}
/**
* Test unhandledMessage.
*/
class StateMachineUnhandledMessage extends StateMachine {
StateMachineUnhandledMessage(String name) {
super(name);
mThisSm = this;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
}
@Override
public void unhandledMessage(Message message) {
mUnhandledMessageCount += 1;
}
class S1 extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_2) {
transitionToHaltingState();
}
return NOT_HANDLED;
}
}
@Override
protected void halting() {
synchronized (mThisSm) {
mThisSm.notifyAll();
}
}
private StateMachineUnhandledMessage mThisSm;
private int mUnhandledMessageCount;
private S1 mS1 = new S1();
}
@SmallTest
public void testStateMachineUnhandledMessage() throws Exception {
StateMachineUnhandledMessage sm = new StateMachineUnhandledMessage("sm");
sm.start();
if (sm.isDbg()) Log.d(TAG, "testStateMachineUnhandledMessage E");
synchronized (sm) {
// Send 2 messages
for (int i = 1; i <= 2; i++) {
sm.sendMessage(i);
}
try {
// wait for the messages to be handled
sm.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachineUnhandledMessage: exception while waiting "
+ e.getMessage());
}
}
assertTrue(sm.getProcessedMessagesCount() == 2);
assertEquals(2, sm.mUnhandledMessageCount);
if (sm.isDbg()) Log.d(TAG, "testStateMachineUnhandledMessage X");
}
/**
* Test state machines sharing the same thread/looper. Multiple instances
* of the same state machine will be created. They will all share the
* same thread and thus each can update <code>sharedCounter</code> which
* will be used to notify testStateMachineSharedThread that the test is
* complete.
*/
class StateMachineSharedThread extends StateMachine {
StateMachineSharedThread(String name, Looper looper, int maxCount) {
super(name, looper);
mMaxCount = maxCount;
setDbg(DBG);
// Setup state machine with 1 state
addState(mS1);
// Set the initial state
setInitialState(mS1);
}
class S1 extends State {
@Override
public boolean processMessage(Message message) {
if (message.what == TEST_CMD_4) {
transitionToHaltingState();
}
return HANDLED;
}
}
@Override
protected void halting() {
// Update the shared counter, which is OK since all state
// machines are using the same thread.
sharedCounter += 1;
if (sharedCounter == mMaxCount) {
synchronized (waitObject) {
waitObject.notifyAll();
}
}
}
private int mMaxCount;
private S1 mS1 = new S1();
}
private static int sharedCounter = 0;
private static Object waitObject = new Object();
@MediumTest
public void testStateMachineSharedThread() throws Exception {
if (DBG) Log.d(TAG, "testStateMachineSharedThread E");
// Create and start the handler thread
HandlerThread smThread = new HandlerThread("testStateMachineSharedThread");
smThread.start();
// Create the state machines
StateMachineSharedThread sms[] = new StateMachineSharedThread[10];
for (int i = 0; i < sms.length; i++) {
sms[i] = new StateMachineSharedThread("sm", smThread.getLooper(), sms.length);
sms[i].start();
}
synchronized (waitObject) {
// Send messages to each of the state machines
for (StateMachineSharedThread sm : sms) {
for (int i = 1; i <= 4; i++) {
sm.sendMessage(i);
}
}
// Wait for the last state machine to notify its done
try {
waitObject.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testStateMachineSharedThread: exception while waiting "
+ e.getMessage());
}
}
for (StateMachineSharedThread sm : sms) {
assertTrue(sm.getProcessedMessagesCount() == 4);
for (int i = 0; i < sm.getProcessedMessagesCount(); i++) {
ProcessedMessageInfo pmi = sm.getProcessedMessageInfo(i);
assertEquals(i+1, pmi.getWhat());
assertEquals(sm.mS1, pmi.getState());
assertEquals(sm.mS1, pmi.getOriginalState());
}
}
if (DBG) Log.d(TAG, "testStateMachineSharedThread X");
}
@MediumTest
public void testHsm1() throws Exception {
if (DBG) Log.d(TAG, "testHsm1 E");
Hsm1 sm = Hsm1.makeHsm1();
// Send messages
sm.sendMessage(Hsm1.CMD_1);
sm.sendMessage(Hsm1.CMD_2);
synchronized (sm) {
// Wait for the last state machine to notify its done
try {
sm.wait();
} catch (InterruptedException e) {
Log.e(TAG, "testHsm1: exception while waiting " + e.getMessage());
}
}
assertEquals(7, sm.getProcessedMessagesCount());
ProcessedMessageInfo pmi = sm.getProcessedMessageInfo(0);
assertEquals(Hsm1.CMD_1, pmi.getWhat());
assertEquals(sm.mS1, pmi.getState());
assertEquals(sm.mS1, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(1);
assertEquals(Hsm1.CMD_2, pmi.getWhat());
assertEquals(sm.mP1, pmi.getState());
assertEquals(sm.mS1, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(2);
assertEquals(Hsm1.CMD_2, pmi.getWhat());
assertEquals(sm.mS2, pmi.getState());
assertEquals(sm.mS2, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(3);
assertEquals(Hsm1.CMD_3, pmi.getWhat());
assertEquals(sm.mS2, pmi.getState());
assertEquals(sm.mS2, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(4);
assertEquals(Hsm1.CMD_3, pmi.getWhat());
assertEquals(sm.mP2, pmi.getState());
assertEquals(sm.mP2, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(5);
assertEquals(Hsm1.CMD_4, pmi.getWhat());
assertEquals(sm.mP2, pmi.getState());
assertEquals(sm.mP2, pmi.getOriginalState());
pmi = sm.getProcessedMessageInfo(6);
assertEquals(Hsm1.CMD_5, pmi.getWhat());
assertEquals(sm.mP2, pmi.getState());
assertEquals(sm.mP2, pmi.getOriginalState());
if (DBG) Log.d(TAG, "testStateMachineSharedThread X");
}
}
class Hsm1 extends StateMachine {
private static final String TAG = "hsm1";
public static final int CMD_1 = 1;
public static final int CMD_2 = 2;
public static final int CMD_3 = 3;
public static final int CMD_4 = 4;
public static final int CMD_5 = 5;
public static Hsm1 makeHsm1() {
Log.d(TAG, "makeHsm1 E");
Hsm1 sm = new Hsm1("hsm1");
sm.start();
Log.d(TAG, "makeHsm1 X");
return sm;
}
Hsm1(String name) {
super(name);
Log.d(TAG, "ctor E");
// Add states, use indentation to show hierarchy
addState(mP1);
addState(mS1, mP1);
addState(mS2, mP1);
addState(mP2);
// Set the initial state
setInitialState(mS1);
Log.d(TAG, "ctor X");
}
class P1 extends State {
@Override
public void enter() {
Log.d(TAG, "P1.enter");
}
@Override
public void exit() {
Log.d(TAG, "P1.exit");
}
@Override
public boolean processMessage(Message message) {
boolean retVal;
Log.d(TAG, "P1.processMessage what=" + message.what);
switch(message.what) {
case CMD_2:
// CMD_2 will arrive in mS2 before CMD_3
sendMessage(CMD_3);
deferMessage(message);
transitionTo(mS2);
retVal = true;
break;
default:
// Any message we don't understand in this state invokes unhandledMessage
retVal = false;
break;
}
return retVal;
}
}
class S1 extends State {
@Override
public void enter() {
Log.d(TAG, "S1.enter");
}
@Override
public void exit() {
Log.d(TAG, "S1.exit");
}
@Override
public boolean processMessage(Message message) {
Log.d(TAG, "S1.processMessage what=" + message.what);
if (message.what == CMD_1) {
// Transition to ourself to show that enter/exit is called
transitionTo(mS1);
return HANDLED;
} else {
// Let parent process all other messages
return NOT_HANDLED;
}
}
}
class S2 extends State {
@Override
public void enter() {
Log.d(TAG, "S2.enter");
}
@Override
public void exit() {
Log.d(TAG, "S2.exit");
}
@Override
public boolean processMessage(Message message) {
boolean retVal;
Log.d(TAG, "S2.processMessage what=" + message.what);
switch(message.what) {
case(CMD_2):
sendMessage(CMD_4);
retVal = true;
break;
case(CMD_3):
deferMessage(message);
transitionTo(mP2);
retVal = true;
break;
default:
retVal = false;
break;
}
return retVal;
}
}
class P2 extends State {
@Override
public void enter() {
Log.d(TAG, "P2.enter");
sendMessage(CMD_5);
}
@Override
public void exit() {
Log.d(TAG, "P2.exit");
}
@Override
public boolean processMessage(Message message) {
Log.d(TAG, "P2.processMessage what=" + message.what);
switch(message.what) {
case(CMD_3):
break;
case(CMD_4):
break;
case(CMD_5):
transitionToHaltingState();
break;
}
return HANDLED;
}
}
@Override
protected void halting() {
Log.d(TAG, "halting");
synchronized (this) {
this.notifyAll();
}
}
P1 mP1 = new P1();
S1 mS1 = new S1();
S2 mS2 = new S2();
P2 mP2 = new P2();
}
|