test_initialize.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //-----------------------------------------------------------------------------
  2. QUnit.module("special initialization options", {
  3. setup: function() {
  4. this.called = [];
  5. this.onbeforeevent = function(event,from,to) { this.called.push('onbefore(' + event + ')'); },
  6. this.onafterevent = function(event,from,to) { this.called.push('onafter(' + event + ')'); },
  7. this.onleavestate = function(event,from,to) { this.called.push('onleave(' + from + ')'); },
  8. this.onenterstate = function(event,from,to) { this.called.push('onenter(' + to + ')'); },
  9. this.onchangestate = function(event,from,to) { this.called.push('onchange(' + from + ',' + to + ')'); };
  10. this.onbeforeinit = function() { this.called.push("onbeforeinit"); };
  11. this.onafterinit = function() { this.called.push("onafterinit"); };
  12. this.onbeforestartup = function() { this.called.push("onbeforestartup"); };
  13. this.onafterstartup = function() { this.called.push("onafterstartup"); };
  14. this.onbeforepanic = function() { this.called.push("onbeforepanic"); };
  15. this.onafterpanic = function() { this.called.push("onafterpanic"); };
  16. this.onbeforecalm = function() { this.called.push("onbeforecalm"); };
  17. this.onaftercalm = function() { this.called.push("onaftercalm"); };
  18. this.onenternone = function() { this.called.push("onenternone"); };
  19. this.onentergreen = function() { this.called.push("onentergreen"); };
  20. this.onenterred = function() { this.called.push("onenterred"); };
  21. this.onleavenone = function() { this.called.push("onleavenone"); };
  22. this.onleavegreen = function() { this.called.push("onleavegreen"); };
  23. this.onleavered = function() { this.called.push("onleavered"); };
  24. }
  25. });
  26. //-----------------------------------------------------------------------------
  27. test("initial state defaults to 'none'", function() {
  28. StateMachine.create({
  29. target: this,
  30. events: [
  31. { name: 'panic', from: 'green', to: 'red' },
  32. { name: 'calm', from: 'red', to: 'green' }
  33. ]});
  34. equal(this.current, 'none');
  35. deepEqual(this.called, []);
  36. });
  37. //-----------------------------------------------------------------------------
  38. test("initial state can be specified", function() {
  39. StateMachine.create({
  40. target: this,
  41. initial: 'green',
  42. events: [
  43. { name: 'panic', from: 'green', to: 'red' },
  44. { name: 'calm', from: 'red', to: 'green' }
  45. ]});
  46. equal(this.current, 'green');
  47. deepEqual(this.called, [
  48. "onbeforestartup",
  49. "onbefore(startup)",
  50. "onleavenone",
  51. "onleave(none)",
  52. "onentergreen",
  53. "onenter(green)",
  54. "onchange(none,green)",
  55. "onafterstartup",
  56. "onafter(startup)"
  57. ]);
  58. });
  59. //-----------------------------------------------------------------------------
  60. test("startup event name can be specified", function() {
  61. StateMachine.create({
  62. target: this,
  63. initial: { state: 'green', event: 'init' },
  64. events: [
  65. { name: 'panic', from: 'green', to: 'red' },
  66. { name: 'calm', from: 'red', to: 'green' }
  67. ]});
  68. equal(this.current, 'green');
  69. deepEqual(this.called, [
  70. "onbeforeinit",
  71. "onbefore(init)",
  72. "onleavenone",
  73. "onleave(none)",
  74. "onentergreen",
  75. "onenter(green)",
  76. "onchange(none,green)",
  77. "onafterinit",
  78. "onafter(init)"
  79. ]);
  80. });
  81. //-----------------------------------------------------------------------------
  82. test("startup event can be deferred", function() {
  83. StateMachine.create({
  84. target: this,
  85. initial: { state: 'green', event: 'init', defer: true },
  86. events: [
  87. { name: 'panic', from: 'green', to: 'red' },
  88. { name: 'calm', from: 'red', to: 'green' }
  89. ]});
  90. equal(this.current, 'none');
  91. deepEqual(this.called, []);
  92. this.init();
  93. equal(this.current, 'green');
  94. deepEqual(this.called, [
  95. "onbeforeinit",
  96. "onbefore(init)",
  97. "onleavenone",
  98. "onleave(none)",
  99. "onentergreen",
  100. "onenter(green)",
  101. "onchange(none,green)",
  102. "onafterinit",
  103. "onafter(init)"
  104. ]);
  105. });
  106. //-----------------------------------------------------------------------------