tests.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. (function(){
  2. var
  3. History = window.History,
  4. document = window.document,
  5. test = window.test,
  6. deepEqual = window.deepEqual;
  7. // Check
  8. if ( !History.enabled ) {
  9. throw new Error('History.js is disabled');
  10. }
  11. // Prepare
  12. History.options.debug = false;
  13. // Variables
  14. var
  15. States = {
  16. // Home
  17. 0: {
  18. 'url': document.location.href.replace(/#.*$/,''),
  19. 'title': ''
  20. },
  21. // One
  22. 1: {
  23. 'data': {
  24. 'state': 1,
  25. 'rand': Math.random()
  26. },
  27. 'title': 'State 1',
  28. 'url': '?state=1'
  29. },
  30. // Two
  31. 2: {
  32. 'data': {
  33. 'state': 2,
  34. 'rand': Math.random()
  35. },
  36. 'title': 'State 2',
  37. 'url': '?state=2&asd=%20asd%2520asd'
  38. },
  39. // Three
  40. 3: {
  41. 'url': '?state=3'
  42. },
  43. // Four
  44. 4: {
  45. 'data': {
  46. 'state': 4,
  47. 'trick': true,
  48. 'rand': Math.random()
  49. },
  50. 'title': 'State 4',
  51. 'url': '?state=3'
  52. },
  53. // Log
  54. 5: {
  55. 'url': '?state=1#log'
  56. },
  57. // Six
  58. 6: {
  59. 'data': {
  60. 'state': 6,
  61. 'rand': Math.random()
  62. },
  63. 'url': 'six.html'
  64. },
  65. // Seven
  66. 7: {
  67. 'url': 'seven'
  68. },
  69. // Eight
  70. 8: {
  71. 'url': '/eight'
  72. }
  73. },
  74. stateOrder = [0,1,2,3,4,3,1,0,1,3,4,3,1,0,6,7,8,1,8,7,6,0],
  75. currentTest = 0;
  76. // Original Title
  77. var title = document.title;
  78. var banner;
  79. var checkStatus = function(){
  80. banner = banner || document.getElementById('qunit-banner');
  81. var status = banner.className !== 'qunit-fail';
  82. return status;
  83. };
  84. // Check State
  85. var checkState = function(){
  86. if ( !checkStatus() ) {
  87. throw new Error('A test has failed');
  88. }
  89. var
  90. stateIndex = stateOrder[currentTest],
  91. expectedState = History.normalizeState(States[stateIndex]),
  92. actualState = History.getState(false);
  93. ++currentTest;
  94. document.title = title+': '+actualState.url;
  95. var
  96. testName = 'Test '+currentTest,
  97. stateName = 'State '+stateIndex;
  98. test(testName,function(){
  99. History.log('Completed: '+testName +' / '+ stateName);
  100. deepEqual(actualState,expectedState,stateName);
  101. });
  102. // Image Load to Stress Test Safari and Opera
  103. (new Image()).src = "image.php";
  104. };
  105. // Check the Initial State
  106. checkState();
  107. // State Change
  108. History.Adapter.bind(window,'statechange',checkState);
  109. // Log
  110. var addLog = function(){
  111. var args = arguments;
  112. History.queue(function(){
  113. History.log.apply(History,args);
  114. });
  115. };
  116. // Dom Load
  117. History.Adapter.onDomLoad(function(){
  118. setTimeout(function(){
  119. // ----------------------------------------------------------------------
  120. // Test State Functionality: Adding
  121. // Test 2 / State 1 (0 -> 1)
  122. // Tests HTML4 -> HTML5 Graceful Upgrade
  123. addLog('Test 2',History.queues.length,History.busy.flag);
  124. History.setHash(History.getHashByState(States[1]));
  125. // Test 3 / State 2 (1 -> 2)
  126. addLog('Test 3',History.queues.length,History.busy.flag);
  127. History.pushState(States[2].data, States[2].title, States[2].url);
  128. // Test 3-2 / State 2 (2 -> 2) / No Change
  129. addLog('Test 3-2',History.queues.length,History.busy.flag);
  130. History.pushState(States[2].data, States[2].title, States[2].url);
  131. // Test 3-3 / State 2 (2 -> 2) / No Change
  132. addLog('Test 3-3',History.queues.length,History.busy.flag);
  133. History.replaceState(States[2].data, States[2].title, States[2].url);
  134. // Test 4 / State 3 (2 -> 3)
  135. addLog('Test 4',History.queues.length,History.busy.flag);
  136. History.replaceState(States[3].data, States[3].title, States[3].url);
  137. // Test 5 / State 4 (3 -> 4)
  138. addLog('Test 5',History.queues.length,History.busy.flag);
  139. History.pushState(States[4].data, States[4].title, States[4].url);
  140. // ----------------------------------------------------------------------
  141. // Test State Functionality: Traversing
  142. // Test 6 / State 3 (4 -> 3)
  143. // Test 7 / State 1 (3 -> 2 -> 1)
  144. addLog('Test 6,7',History.queues.length,History.busy.flag);
  145. History.go(-2);
  146. // Test 8 / State 0 (1 -> 0)
  147. // Tests Default State
  148. addLog('Test 8',History.queues.length,History.busy.flag);
  149. History.back();
  150. // Test 9 / State 1 (0 -> 1)
  151. // Test 10 / State 3 (1 -> 2 -> 3)
  152. addLog('Test 9,10',History.queues.length,History.busy.flag);
  153. History.go(2);
  154. // Test 11 / State 4 (3 -> 4)
  155. addLog('Test 11',History.queues.length,History.busy.flag);
  156. History.forward();
  157. // Test 12 / State 3 (4 -> 3)
  158. addLog('Test 12',History.queues.length,History.busy.flag);
  159. History.back();
  160. // Test 13 / State 1 (3 -> 2 -> 1)
  161. addLog('Test 13',History.queues.length,History.busy.flag);
  162. History.back();
  163. // ----------------------------------------------------------------------
  164. // Test State Functionality: Traditional Anchors
  165. // Test 13-2 / State 1 (1 -> #log) / No Change
  166. addLog('Test 13-2',History.queues.length,History.busy.flag);
  167. History.setHash('log');
  168. // Test 13-3 / State 1 (#log -> 1) / No Change
  169. addLog('Test 13-3',History.queues.length,History.busy.flag);
  170. History.back();
  171. // Test 14 / State 0 (1 -> 0)
  172. addLog('Test 14',History.queues.length,History.busy.flag);
  173. History.back();
  174. // ----------------------------------------------------------------------
  175. // Test URL Handling: Adding
  176. // Test 15 / State 6 (1 -> 6)
  177. // Also tests data with no title
  178. addLog('Test 15',History.queues.length,History.busy.flag);
  179. History.pushState(States[6].data, States[6].title, States[6].url);
  180. // Test 16 / State 7 (6 -> 7)
  181. addLog('Test 16',History.queues.length,History.busy.flag);
  182. History.pushState(States[7].data, States[7].title, States[7].url);
  183. // Test 17 / State 7 (7 -> 8)
  184. addLog('Test 17',History.queues.length,History.busy.flag);
  185. History.pushState(States[8].data, States[8].title, States[8].url);
  186. // Test 18 / State 1 (8 -> 1)
  187. // Should be /eight?state=1
  188. addLog('Test 18',History.queues.length,History.busy.flag);
  189. History.pushState(States[1].data, States[1].title, States[1].url);
  190. // ----------------------------------------------------------------------
  191. // Test URL Handling: Traversing
  192. // Test 19 / State 8 (1 -> 8)
  193. addLog('Test 19',History.queues.length,History.busy.flag);
  194. History.back();
  195. // Test 20 / State 7 (8 -> 7)
  196. addLog('Test 20',History.queues.length,History.busy.flag);
  197. History.back();
  198. // Test 21 / State 6 (7 -> 6)
  199. addLog('Test 21',History.queues.length,History.busy.flag);
  200. History.back();
  201. // Test 22 / State 0 (6 -> 0)
  202. addLog('Test 22',History.queues.length,History.busy.flag);
  203. History.back();
  204. },1000); // wait for test one to complete
  205. });
  206. })();