test.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // These tests expect the DOM to contain a presentation
  2. // with the following slide structure:
  3. //
  4. // 1
  5. // 2 - Three sub-slides
  6. // 3 - Three fragment elements
  7. // 3 - Two fragments with same data-fragment-index
  8. // 4
  9. Reveal.addEventListener( 'ready', function() {
  10. // ---------------------------------------------------------------
  11. // DOM TESTS
  12. QUnit.module( 'DOM' );
  13. test( 'Initial slides classes', function() {
  14. var horizontalSlides = document.querySelectorAll( '.reveal .slides>section' )
  15. strictEqual( document.querySelectorAll( '.reveal .slides section.past' ).length, 0, 'no .past slides' );
  16. strictEqual( document.querySelectorAll( '.reveal .slides section.present' ).length, 1, 'one .present slide' );
  17. strictEqual( document.querySelectorAll( '.reveal .slides>section.future' ).length, horizontalSlides.length - 1, 'remaining horizontal slides are .future' );
  18. strictEqual( document.querySelectorAll( '.reveal .slides section.stack' ).length, 2, 'two .stacks' );
  19. ok( document.querySelectorAll( '.reveal .slides section.stack' )[0].querySelectorAll( '.future' ).length > 0, 'vertical slides are given .future' );
  20. });
  21. // ---------------------------------------------------------------
  22. // API TESTS
  23. QUnit.module( 'API' );
  24. test( 'Reveal.isReady', function() {
  25. strictEqual( Reveal.isReady(), true, 'returns true' );
  26. });
  27. test( 'Reveal.isOverview', function() {
  28. strictEqual( Reveal.isOverview(), false, 'false by default' );
  29. Reveal.toggleOverview();
  30. strictEqual( Reveal.isOverview(), true, 'true after toggling on' );
  31. Reveal.toggleOverview();
  32. strictEqual( Reveal.isOverview(), false, 'false after toggling off' );
  33. });
  34. test( 'Reveal.isPaused', function() {
  35. strictEqual( Reveal.isPaused(), false, 'false by default' );
  36. Reveal.togglePause();
  37. strictEqual( Reveal.isPaused(), true, 'true after pausing' );
  38. Reveal.togglePause();
  39. strictEqual( Reveal.isPaused(), false, 'false after resuming' );
  40. });
  41. test( 'Reveal.isFirstSlide', function() {
  42. Reveal.slide( 0, 0 );
  43. strictEqual( Reveal.isFirstSlide(), true, 'true after Reveal.slide( 0, 0 )' );
  44. Reveal.slide( 1, 0 );
  45. strictEqual( Reveal.isFirstSlide(), false, 'false after Reveal.slide( 1, 0 )' );
  46. Reveal.slide( 0, 0 );
  47. strictEqual( Reveal.isFirstSlide(), true, 'true after Reveal.slide( 0, 0 )' );
  48. });
  49. test( 'Reveal.isLastSlide', function() {
  50. Reveal.slide( 0, 0 );
  51. strictEqual( Reveal.isLastSlide(), false, 'false after Reveal.slide( 0, 0 )' );
  52. var lastSlideIndex = document.querySelectorAll( '.reveal .slides>section' ).length - 1;
  53. Reveal.slide( lastSlideIndex, 0 );
  54. strictEqual( Reveal.isLastSlide(), true, 'true after Reveal.slide( ', 0+ lastSlideIndex +' )' );
  55. Reveal.slide( 0, 0 );
  56. strictEqual( Reveal.isLastSlide(), false, 'false after Reveal.slide( 0, 0 )' );
  57. });
  58. test( 'Reveal.getTotalSlides', function() {
  59. strictEqual( Reveal.getTotalSlides(), 8, 'eight slides in total' );
  60. });
  61. test( 'Reveal.getIndices', function() {
  62. var indices = Reveal.getIndices();
  63. ok( typeof indices.hasOwnProperty( 'h' ), 'h exists' );
  64. ok( typeof indices.hasOwnProperty( 'v' ), 'v exists' );
  65. ok( typeof indices.hasOwnProperty( 'f' ), 'f exists' );
  66. Reveal.slide( 1, 0 );
  67. ok( Reveal.getIndices().h === 1 && Reveal.getIndices().v === 0, 'h 1, v 0' );
  68. Reveal.slide( 1, 2 );
  69. ok( Reveal.getIndices().h === 1 && Reveal.getIndices().v === 2, 'h 1, v 2' );
  70. Reveal.slide( 0, 0 );
  71. });
  72. test( 'Reveal.getSlide', function() {
  73. var firstSlide = document.querySelector( '.reveal .slides>section:first-child' );
  74. equal( Reveal.getSlide( 0 ), firstSlide, 'gets correct first slide' );
  75. strictEqual( Reveal.getSlide( 100 ), undefined, 'returns undefined when slide can\'t be found' );
  76. });
  77. test( 'Reveal.getPreviousSlide/getCurrentSlide', function() {
  78. Reveal.slide( 0, 0 );
  79. Reveal.slide( 1, 0 );
  80. var firstSlide = document.querySelector( '.reveal .slides>section:first-child' );
  81. var secondSlide = document.querySelector( '.reveal .slides>section:nth-child(2)>section' );
  82. equal( Reveal.getPreviousSlide(), firstSlide, 'previous is slide #0' );
  83. equal( Reveal.getCurrentSlide(), secondSlide, 'current is slide #1' );
  84. });
  85. test( 'Reveal.getProgress', function() {
  86. Reveal.slide( 0, 0 );
  87. strictEqual( Reveal.getProgress(), 0, 'progress is 0 on first slide' );
  88. var lastSlideIndex = document.querySelectorAll( '.reveal .slides>section' ).length - 1;
  89. Reveal.slide( lastSlideIndex, 0 );
  90. strictEqual( Reveal.getProgress(), 1, 'progress is 1 on last slide' );
  91. });
  92. test( 'Reveal.getScale', function() {
  93. ok( typeof Reveal.getScale() === 'number', 'has scale' );
  94. });
  95. test( 'Reveal.getConfig', function() {
  96. ok( typeof Reveal.getConfig() === 'object', 'has config' );
  97. });
  98. test( 'Reveal.configure', function() {
  99. strictEqual( Reveal.getConfig().loop, false, '"loop" is false to start with' );
  100. Reveal.configure({ loop: true });
  101. strictEqual( Reveal.getConfig().loop, true, '"loop" has changed to true' );
  102. Reveal.configure({ loop: false, customTestValue: 1 });
  103. strictEqual( Reveal.getConfig().customTestValue, 1, 'supports custom values' );
  104. });
  105. test( 'Reveal.availableRoutes', function() {
  106. Reveal.slide( 0, 0 );
  107. deepEqual( Reveal.availableRoutes(), { left: false, up: false, down: false, right: true }, 'correct for first slide' );
  108. Reveal.slide( 1, 0 );
  109. deepEqual( Reveal.availableRoutes(), { left: true, up: false, down: true, right: true }, 'correct for vertical slide' );
  110. });
  111. test( 'Reveal.next', function() {
  112. Reveal.slide( 0, 0 );
  113. // Step through vertical child slides
  114. Reveal.next();
  115. deepEqual( Reveal.getIndices(), { h: 1, v: 0, f: undefined } );
  116. Reveal.next();
  117. deepEqual( Reveal.getIndices(), { h: 1, v: 1, f: undefined } );
  118. Reveal.next();
  119. deepEqual( Reveal.getIndices(), { h: 1, v: 2, f: undefined } );
  120. // Step through fragments
  121. Reveal.next();
  122. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: -1 } );
  123. Reveal.next();
  124. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 0 } );
  125. Reveal.next();
  126. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 1 } );
  127. Reveal.next();
  128. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 2 } );
  129. });
  130. test( 'Reveal.next at end', function() {
  131. Reveal.slide( 3 );
  132. // We're at the end, this should have no effect
  133. Reveal.next();
  134. deepEqual( Reveal.getIndices(), { h: 3, v: 0, f: undefined } );
  135. Reveal.next();
  136. deepEqual( Reveal.getIndices(), { h: 3, v: 0, f: undefined } );
  137. });
  138. // ---------------------------------------------------------------
  139. // FRAGMENT TESTS
  140. QUnit.module( 'Fragments' );
  141. test( 'Sliding to fragments', function() {
  142. Reveal.slide( 2, 0, -1 );
  143. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: -1 }, 'Reveal.slide( 2, 0, -1 )' );
  144. Reveal.slide( 2, 0, 0 );
  145. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 0 }, 'Reveal.slide( 2, 0, 0 )' );
  146. Reveal.slide( 2, 0, 2 );
  147. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 2 }, 'Reveal.slide( 2, 0, 2 )' );
  148. Reveal.slide( 2, 0, 1 );
  149. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 1 }, 'Reveal.slide( 2, 0, 1 )' );
  150. });
  151. test( 'Hiding all fragments', function() {
  152. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(1)' );
  153. Reveal.slide( 2, 0, 0 );
  154. strictEqual( fragmentSlide.querySelectorAll( '.fragment.visible' ).length, 1, 'one fragment visible when index is 0' );
  155. Reveal.slide( 2, 0, -1 );
  156. strictEqual( fragmentSlide.querySelectorAll( '.fragment.visible' ).length, 0, 'no fragments visible when index is -1' );
  157. });
  158. test( 'Current fragment', function() {
  159. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(1)' );
  160. Reveal.slide( 2, 0 );
  161. strictEqual( fragmentSlide.querySelectorAll( '.fragment.current-fragment' ).length, 0, 'no current fragment at index -1' );
  162. Reveal.slide( 2, 0, 0 );
  163. strictEqual( fragmentSlide.querySelectorAll( '.fragment.current-fragment' ).length, 1, 'one current fragment at index 0' );
  164. Reveal.slide( 1, 0, 0 );
  165. strictEqual( fragmentSlide.querySelectorAll( '.fragment.current-fragment' ).length, 0, 'no current fragment when navigating to previous slide' );
  166. Reveal.slide( 3, 0, 0 );
  167. strictEqual( fragmentSlide.querySelectorAll( '.fragment.current-fragment' ).length, 0, 'no current fragment when navigating to next slide' );
  168. });
  169. test( 'Stepping through fragments', function() {
  170. Reveal.slide( 2, 0, -1 );
  171. // forwards:
  172. Reveal.next();
  173. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 0 }, 'next() goes to next fragment' );
  174. Reveal.right();
  175. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 1 }, 'right() goes to next fragment' );
  176. Reveal.down();
  177. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 2 }, 'down() goes to next fragment' );
  178. Reveal.down(); // moves to f #3
  179. // backwards:
  180. Reveal.prev();
  181. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 2 }, 'prev() goes to prev fragment' );
  182. Reveal.left();
  183. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 1 }, 'left() goes to prev fragment' );
  184. Reveal.up();
  185. deepEqual( Reveal.getIndices(), { h: 2, v: 0, f: 0 }, 'up() goes to prev fragment' );
  186. });
  187. test( 'Stepping past fragments', function() {
  188. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(1)' );
  189. Reveal.slide( 0, 0, 0 );
  190. equal( fragmentSlide.querySelectorAll( '.fragment.visible' ).length, 0, 'no fragments visible when on previous slide' );
  191. Reveal.slide( 3, 0, 0 );
  192. equal( fragmentSlide.querySelectorAll( '.fragment.visible' ).length, 3, 'all fragments visible when on future slide' );
  193. });
  194. test( 'Fragment indices', function() {
  195. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(2)' );
  196. Reveal.slide( 3, 0, 0 );
  197. equal( fragmentSlide.querySelectorAll( '.fragment.visible' ).length, 2, 'both fragments of same index are shown' );
  198. });
  199. test( 'Index generation', function() {
  200. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(1)' );
  201. // These have no indices defined to start with
  202. equal( fragmentSlide.querySelectorAll( '.fragment' )[0].getAttribute( 'data-fragment-index' ), '0' );
  203. equal( fragmentSlide.querySelectorAll( '.fragment' )[1].getAttribute( 'data-fragment-index' ), '1' );
  204. equal( fragmentSlide.querySelectorAll( '.fragment' )[2].getAttribute( 'data-fragment-index' ), '2' );
  205. });
  206. test( 'Index normalization', function() {
  207. var fragmentSlide = document.querySelector( '#fragment-slides>section:nth-child(3)' );
  208. // These start out as 1-4-4 and should normalize to 0-1-1
  209. equal( fragmentSlide.querySelectorAll( '.fragment' )[0].getAttribute( 'data-fragment-index' ), '0' );
  210. equal( fragmentSlide.querySelectorAll( '.fragment' )[1].getAttribute( 'data-fragment-index' ), '1' );
  211. equal( fragmentSlide.querySelectorAll( '.fragment' )[2].getAttribute( 'data-fragment-index' ), '1' );
  212. });
  213. asyncTest( 'fragmentshown event', function() {
  214. expect( 2 );
  215. var _onEvent = function( event ) {
  216. ok( true, 'event fired' );
  217. }
  218. Reveal.addEventListener( 'fragmentshown', _onEvent );
  219. Reveal.slide( 2, 0 );
  220. Reveal.slide( 2, 0 ); // should do nothing
  221. Reveal.slide( 2, 0, 0 ); // should do nothing
  222. Reveal.next();
  223. Reveal.next();
  224. Reveal.prev(); // shouldn't fire fragmentshown
  225. start();
  226. Reveal.removeEventListener( 'fragmentshown', _onEvent );
  227. });
  228. asyncTest( 'fragmenthidden event', function() {
  229. expect( 2 );
  230. var _onEvent = function( event ) {
  231. ok( true, 'event fired' );
  232. }
  233. Reveal.addEventListener( 'fragmenthidden', _onEvent );
  234. Reveal.slide( 2, 0, 2 );
  235. Reveal.slide( 2, 0, 2 ); // should do nothing
  236. Reveal.prev();
  237. Reveal.prev();
  238. Reveal.next(); // shouldn't fire fragmenthidden
  239. start();
  240. Reveal.removeEventListener( 'fragmenthidden', _onEvent );
  241. });
  242. // ---------------------------------------------------------------
  243. // AUTO-SLIDE TESTS
  244. QUnit.module( 'Auto Sliding' );
  245. test( 'Reveal.isAutoSliding', function() {
  246. strictEqual( Reveal.isAutoSliding(), false, 'false by default' );
  247. Reveal.configure({ autoSlide: 10000 });
  248. strictEqual( Reveal.isAutoSliding(), true, 'true after starting' );
  249. Reveal.configure({ autoSlide: 0 });
  250. strictEqual( Reveal.isAutoSliding(), false, 'false after setting to 0' );
  251. });
  252. test( 'Reveal.toggleAutoSlide', function() {
  253. Reveal.configure({ autoSlide: 10000 });
  254. Reveal.toggleAutoSlide();
  255. strictEqual( Reveal.isAutoSliding(), false, 'false after first toggle' );
  256. Reveal.toggleAutoSlide();
  257. strictEqual( Reveal.isAutoSliding(), true, 'true after second toggle' );
  258. Reveal.configure({ autoSlide: 0 });
  259. });
  260. asyncTest( 'autoslidepaused', function() {
  261. expect( 1 );
  262. var _onEvent = function( event ) {
  263. ok( true, 'event fired' );
  264. }
  265. Reveal.addEventListener( 'autoslidepaused', _onEvent );
  266. Reveal.configure({ autoSlide: 10000 });
  267. Reveal.toggleAutoSlide();
  268. start();
  269. // cleanup
  270. Reveal.configure({ autoSlide: 0 });
  271. Reveal.removeEventListener( 'autoslidepaused', _onEvent );
  272. });
  273. asyncTest( 'autoslideresumed', function() {
  274. expect( 1 );
  275. var _onEvent = function( event ) {
  276. ok( true, 'event fired' );
  277. }
  278. Reveal.addEventListener( 'autoslideresumed', _onEvent );
  279. Reveal.configure({ autoSlide: 10000 });
  280. Reveal.toggleAutoSlide();
  281. Reveal.toggleAutoSlide();
  282. start();
  283. // cleanup
  284. Reveal.configure({ autoSlide: 0 });
  285. Reveal.removeEventListener( 'autoslideresumed', _onEvent );
  286. });
  287. // ---------------------------------------------------------------
  288. // CONFIGURATION VALUES
  289. QUnit.module( 'Configuration' );
  290. test( 'Controls', function() {
  291. var controlsElement = document.querySelector( '.reveal>.controls' );
  292. Reveal.configure({ controls: false });
  293. equal( controlsElement.style.display, 'none', 'controls are hidden' );
  294. Reveal.configure({ controls: true });
  295. equal( controlsElement.style.display, 'block', 'controls are visible' );
  296. });
  297. test( 'Progress', function() {
  298. var progressElement = document.querySelector( '.reveal>.progress' );
  299. Reveal.configure({ progress: false });
  300. equal( progressElement.style.display, 'none', 'progress are hidden' );
  301. Reveal.configure({ progress: true });
  302. equal( progressElement.style.display, 'block', 'progress are visible' );
  303. });
  304. test( 'Loop', function() {
  305. Reveal.configure({ loop: true });
  306. Reveal.slide( 0, 0 );
  307. Reveal.left();
  308. notEqual( Reveal.getIndices().h, 0, 'looped from start to end' );
  309. Reveal.right();
  310. equal( Reveal.getIndices().h, 0, 'looped from end to start' );
  311. Reveal.configure({ loop: false });
  312. });
  313. // ---------------------------------------------------------------
  314. // LAZY-LOADING TESTS
  315. QUnit.module( 'Lazy-Loading' );
  316. test( 'img with data-src', function() {
  317. strictEqual( document.querySelectorAll( '.reveal section img[src]' ).length, 1, 'Image source has been set' );
  318. });
  319. // ---------------------------------------------------------------
  320. // EVENT TESTS
  321. QUnit.module( 'Events' );
  322. asyncTest( 'slidechanged', function() {
  323. expect( 3 );
  324. var _onEvent = function( event ) {
  325. ok( true, 'event fired' );
  326. }
  327. Reveal.addEventListener( 'slidechanged', _onEvent );
  328. Reveal.slide( 1, 0 ); // should trigger
  329. Reveal.slide( 1, 0 ); // should do nothing
  330. Reveal.next(); // should trigger
  331. Reveal.slide( 3, 0 ); // should trigger
  332. Reveal.next(); // should do nothing
  333. start();
  334. Reveal.removeEventListener( 'slidechanged', _onEvent );
  335. });
  336. asyncTest( 'paused', function() {
  337. expect( 1 );
  338. var _onEvent = function( event ) {
  339. ok( true, 'event fired' );
  340. }
  341. Reveal.addEventListener( 'paused', _onEvent );
  342. Reveal.togglePause();
  343. Reveal.togglePause();
  344. start();
  345. Reveal.removeEventListener( 'paused', _onEvent );
  346. });
  347. asyncTest( 'resumed', function() {
  348. expect( 1 );
  349. var _onEvent = function( event ) {
  350. ok( true, 'event fired' );
  351. }
  352. Reveal.addEventListener( 'resumed', _onEvent );
  353. Reveal.togglePause();
  354. Reveal.togglePause();
  355. start();
  356. Reveal.removeEventListener( 'resumed', _onEvent );
  357. });
  358. } );
  359. Reveal.initialize();