PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/unit/fixed-toolbar/fixedToolbar.js

https://github.com/Intai/go-jquery-mobile
JavaScript | 360 lines | 345 code | 9 blank | 6 comment | 0 complexity | b4b30bd35d1941cb8d83ef2f3aae3217 MD5 | raw file
  1. /*
  2. * mobile Fixed Toolbar unit tests
  3. */
  4. (function($){
  5. module('jquery.mobile.fixedToolbar.js');
  6. $( "html" ).height( screen.height * 3 );
  7. function scrollDown(){
  8. window.scrollTo(0,screen.height );
  9. }
  10. function scrollUp(){
  11. window.scrollTo(0,0);
  12. }
  13. module("jquery.mobile.fixedToolbar.js", {setup: function() {
  14. var startTimeout;
  15. // swallow the inital page change
  16. stop();
  17. $(document).one("pagechange", function() {
  18. clearTimeout(startTimeout);
  19. });
  20. startTimeout = setTimeout(start, 1000);
  21. }});
  22. test( "Fixed Header Structural Classes are applied correctly", function(){
  23. //footer
  24. ok( !$('#classes-test-a').hasClass('ui-header-fixed'), 'An ordinary header should not have fixed classes');
  25. ok( $('#classes-test-b').hasClass('ui-header-fixed'), 'An header with data-position=fixed should have ui-header-fixed class');
  26. ok( $('#classes-test-c').hasClass('ui-header-fullscreen'), 'An header with data-position=fixed and data-fullscreen should have ui-header-fullscreen class');
  27. //footer
  28. ok( !$('#classes-test-d').hasClass('ui-footer-fixed'), 'An ordinary footer should not have fixed classes');
  29. ok( $('#classes-test-e').hasClass('ui-footer-fixed'), 'A footer with data-position=fixed should have ui-footer-fixed class"');
  30. ok( $('#classes-test-f').hasClass('ui-footer-fullscreen'), 'A footer with data-position=fixed and data-fullscreen should have ui-footer-fullscreen class');
  31. //parent
  32. ok( $('#classes-test-b').closest( ".ui-page" ).hasClass( "ui-page-header-fixed" ), "Parent page of a fixed header has class ui-page-header-fixed" );
  33. ok( $('#classes-test-e').closest( ".ui-page" ).hasClass( "ui-page-footer-fixed" ), "Parent page of a fixed footer has class ui-page-header-fixed" );
  34. ok( $('#classes-test-c').closest( ".ui-page" ).hasClass( "ui-page-header-fullscreen" ), "Parent page of a fullscreen header has class ui-page-header-fullscreen" );
  35. ok( $('#classes-test-f').closest( ".ui-page" ).hasClass( "ui-page-footer-fullscreen" ), "Parent page of a fullscreen footer has class ui-page-header-fullscreen" );
  36. });
  37. asyncTest( "Fixed header and footer transition classes are applied correctly", function(){
  38. expect( 5 );
  39. $.testHelper.sequence([
  40. function(){
  41. $( '#classes-test-b, #classes-test-g, #classes-test-e,#classes-test-h,#classes-test-i,#classes-test-j, #classes-test-k' ).fixedtoolbar( "hide" );
  42. scrollDown();
  43. },
  44. function(){
  45. //show first
  46. $( '#classes-test-b, #classes-test-g, #classes-test-e,#classes-test-h,#classes-test-i,#classes-test-j, #classes-test-k' ).fixedtoolbar( "show" );
  47. },
  48. function() {
  49. ok( $( '#classes-test-g' ).hasClass('slidedown'), 'The slidedown class should be applied by default');
  50. ok( !$( '#classes-test-h' ).hasClass('slidedown'), 'The slidedown class should not be applied when the header has a data-transition of "none"');
  51. ok( !$( '#classes-test-h' ).hasClass('in'), 'The "in" class should not be applied when the header has a data-transition of "none"');
  52. ok( $( '#classes-test-i' ).hasClass('slidedown'), 'The "slidedown" class should be applied when the header has a data-transition of "slide"');
  53. ok( $( '#classes-test-j' ).hasClass('slideup'), 'The "slideup" class should be applied when the footer has a data-transition of "slide"');
  54. },
  55. function(){
  56. scrollUp();
  57. start();
  58. }
  59. ], 1000);
  60. });
  61. test( "User zooming is disabled when the header is visible and disablePageZoom is true", function(){
  62. $.mobile.zoom.enable();
  63. var defaultZoom = $.mobile.fixedtoolbar.prototype.options.disablePageZoom;
  64. $( ".ui-page-active .ui-header-fixed" ).fixedtoolbar("option", "disablePageZoom", true );
  65. $( ".ui-page-active" ).trigger( "pagebeforeshow" );
  66. ok( !$.mobile.zoom.enabled, "Viewport scaling is disabled before page show." );
  67. $( ".ui-page-active .ui-header-fixed" ).fixedtoolbar("option", "disablePageZoom", defaultZoom );
  68. $.mobile.zoom.enable();
  69. });
  70. test( "Meta viewport content is restored to previous state, and zooming renabled, after pagebeforehide", function(){
  71. $.mobile.zoom.enable( true );
  72. var defaultZoom = $.mobile.fixedtoolbar.prototype.options.disablePageZoom;
  73. $( ".ui-page-active .ui-header-fixed" ).fixedtoolbar("option", "disablePageZoom", true );
  74. $( ".ui-page-active" ).trigger( "pagebeforeshow" );
  75. ok( !$.mobile.zoom.enabled, "Viewport scaling is disabled before page show." );
  76. $( ".ui-page-active" ).trigger( "pagebeforehide" );
  77. ok( $.mobile.zoom.enabled, "Viewport scaling is enabled." );
  78. $( ".ui-page-active .ui-header-fixed" ).fixedtoolbar("option", "disablePageZoom", defaultZoom );
  79. $.mobile.zoom.enable( true );
  80. });
  81. test( "User zooming is not disabled when the header is visible and disablePageZoom is false", function(){
  82. $.mobile.zoom.enable( true );
  83. var defaultZoom = $.mobile.fixedtoolbar.prototype.options.disablePageZoom;
  84. $( ".ui-page :jqmData(position='fixed')" ).fixedtoolbar( "option", "disablePageZoom", false );
  85. $( ".ui-page-active" ).trigger( "pagebeforeshow" );
  86. ok( $.mobile.zoom.enabled, "Viewport scaling is not disabled before page show." );
  87. $( ".ui-page :jqmData(position='fixed')" ).fixedtoolbar( "option", "disablePageZoom", defaultZoom );
  88. $.mobile.zoom.enable( true );
  89. });
  90. asyncTest( "The hide method is working properly", function() {
  91. expect( 2 );
  92. $.testHelper.sequence([
  93. function(){
  94. $( '#classes-test-g' ).fixedtoolbar( "show" );
  95. scrollDown();
  96. },
  97. function() {
  98. $( '#classes-test-g' ).fixedtoolbar( "hide" );
  99. ok( $( '#classes-test-g' ).hasClass('out'), 'The out class should be applied when hide is called');
  100. },
  101. function() {
  102. ok( $( '#classes-test-g' ).hasClass('ui-fixed-hidden'), 'The toolbar has the ui-fixed-hidden class applied after hide');
  103. $( '#classes-test-g' ).fixedtoolbar( "show" );
  104. },
  105. function(){
  106. scrollUp();
  107. start();
  108. }
  109. ], 700);
  110. });
  111. asyncTest( "The show method is working properly", function() {
  112. expect( 2 );
  113. $.testHelper.sequence([
  114. function(){
  115. scrollDown();
  116. },
  117. function() {
  118. $( '#classes-test-g' ).fixedtoolbar( "hide" );
  119. },
  120. function() {
  121. $( '#classes-test-g' ).fixedtoolbar( "show" );
  122. ok( $( '#classes-test-g' ).hasClass('in'), 'The in class should be applied when show is called');
  123. },
  124. function() {
  125. ok( !$( '#classes-test-g' ).hasClass('ui-fixed-hidden'), 'The toolbar does not have the ui-fixed-hidden class applied after show');
  126. },
  127. function(){
  128. scrollUp();
  129. start();
  130. }
  131. ], 700);
  132. });
  133. asyncTest( "The toggle method is working properly", function() {
  134. expect( 3 );
  135. $.testHelper.sequence([
  136. function(){
  137. scrollDown();
  138. },
  139. function(){
  140. $( '#classes-test-g' ).fixedtoolbar( "show" );
  141. },
  142. function() {
  143. ok( !$( '#classes-test-g' ).hasClass('ui-fixed-hidden'), 'The toolbar does not have the ui-fixed-hidden class');
  144. },
  145. function() {
  146. $( '#classes-test-g' ).fixedtoolbar( "toggle" );
  147. },
  148. function() {
  149. ok( $( '#classes-test-g' ).hasClass('ui-fixed-hidden'), 'The toolbar does have the ui-fixed-hidden class');
  150. },
  151. function() {
  152. $( '#classes-test-g' ).fixedtoolbar( "toggle" );
  153. },
  154. function() {
  155. ok( !$( '#classes-test-g' ).hasClass('ui-fixed-hidden'), 'The toolbar does not have the ui-fixed-hidden class');
  156. },
  157. function(){
  158. scrollUp();
  159. start();
  160. }
  161. ], 500);
  162. });
  163. asyncTest( "The persistent headers and footers are working properly", function() {
  164. expect( 3 );
  165. $( "#persist-test-b, #persist-test-a" ).page();
  166. var nextpageheader = $( "#persist-test-b .ui-header-fixed" ),
  167. nextpagefooter = $( "#persist-test-b .ui-footer-fixed" );
  168. $.testHelper.pageSequence([
  169. function(){
  170. ok( nextpageheader.length && nextpagefooter.length, "next page has fixed header and fixed footer" );
  171. $.mobile.changePage( "#persist-test-a" );
  172. },
  173. function(){
  174. $( "#persist-test-b" )
  175. .one( "pagebeforeshow", function(){
  176. ok( nextpageheader.parent( ".ui-mobile-viewport" ).length, "fixed header and footer are now a child of page container" );
  177. });
  178. $.mobile.changePage( "#persist-test-b" );
  179. },
  180. function() {
  181. ok( nextpageheader.parent( ".ui-page" ).length, "fixed header and footer are now a child of page again" );
  182. $.mobile.changePage( "#default" );
  183. },
  184. start
  185. ]);
  186. });
  187. asyncTest( "The persistent headers should work without a footer", function() {
  188. expect( 3 );
  189. $( "#persist-test-c, #persist-test-d" ).page();
  190. var nextpageheader = $( "#persist-test-d .ui-header-fixed" );
  191. $.testHelper.pageSequence([
  192. function(){
  193. ok( nextpageheader.length, "next page has fixed header and fixed footer" );
  194. $.mobile.changePage( "#persist-test-c" );
  195. },
  196. function(){
  197. $( "#persist-test-d" )
  198. .one( "pagebeforeshow", function(){
  199. deepEqual( nextpageheader.parent()[0], $.mobile.pageContainer[0], "fixed header is now a child of page container" );
  200. });
  201. $.mobile.changePage( "#persist-test-d" );
  202. },
  203. function() {
  204. deepEqual( nextpageheader.parent()[0], $.mobile.activePage[0], "fixed header is now a child of page again" );
  205. $.mobile.changePage( "#default" );
  206. },
  207. start
  208. ]);
  209. });
  210. asyncTest( "The persistent footers should work without a header", function() {
  211. expect( 3 );
  212. $( "#persist-test-e, #persist-test-f" ).page();
  213. var nextpagefooter = $( "#persist-test-f .ui-footer-fixed" );
  214. $.testHelper.pageSequence([
  215. function(){
  216. ok( nextpagefooter.length, "next page has fixed footer and fixed footer" );
  217. $.mobile.changePage( "#persist-test-e" );
  218. },
  219. function(){
  220. $( "#persist-test-f" )
  221. .one( "pagebeforeshow", function(){
  222. deepEqual( nextpagefooter.parent()[0], $.mobile.pageContainer[0], "fixed footer is now a child of page container" );
  223. });
  224. $.mobile.changePage( "#persist-test-f" );
  225. },
  226. function() {
  227. deepEqual( nextpagefooter.parent()[0], $.mobile.activePage[0], "fixed footer is now a child of page again" );
  228. $.mobile.changePage( "#default" );
  229. },
  230. start
  231. ]);
  232. });
  233. var asyncTestFooterAndHeader = function( pageSelector, visible ) {
  234. $.testHelper.pageSequence([
  235. function() {
  236. $.mobile.changePage( pageSelector );
  237. },
  238. function() {
  239. var $footer = $.mobile.activePage.find( ".ui-footer" ),
  240. $header = $.mobile.activePage.find( ".ui-header" ),
  241. hiddenStr = visible ? "hidden" : "visible";
  242. equal( $footer.length, 1, "there should be one footer" );
  243. equal( $header.length, 1, "there should be one header" );
  244. equal( !$footer.hasClass( "ui-fixed-hidden" ), visible, "the footer should be " + hiddenStr );
  245. equal( !$header.hasClass( "ui-fixed-hidden" ), visible, "the header should be " + hiddenStr );
  246. $.mobile.changePage( "#default" );
  247. },
  248. start
  249. ]);
  250. };
  251. asyncTest( "data-visible-on-page-show hides toolbars when false", function() {
  252. asyncTestFooterAndHeader( "#page-show-visible-false", false );
  253. });
  254. asyncTest( "data-visible-on-page-show shows toolbars when explicitly true", function() {
  255. asyncTestFooterAndHeader( "#page-show-visible-true", true );
  256. });
  257. asyncTest( "data-visible-on-page-show shows toolbars when undefined", function() {
  258. asyncTestFooterAndHeader( "#page-show-visible-undefined", true );
  259. });
  260. })(jQuery);