/tests/integration/transitions/transitions_core.js

https://github.com/agcolom/jquery-mobile · JavaScript · 218 lines · 154 code · 49 blank · 15 comment · 4 complexity · 2a0de2605e0ffccb16a0bdab527aba28 MD5 · raw file

  1. /*
  2. * mobile navigation unit tests
  3. */
  4. (function($){
  5. var transitioning = "ui-mobile-viewport-transitioning",
  6. animationCompleteFn = $.fn.animationComplete,
  7. defaultMaxTrans = $.mobile.maxTransitionWidth,
  8. //TODO centralize class names?
  9. transitionTypes = "in out fade slide flip reverse pop",
  10. isTransitioning = function(page){
  11. return $.grep(transitionTypes.split(" "), function(className, i){
  12. return page.hasClass(className);
  13. }).length > 0;
  14. },
  15. isTransitioningIn = function(page){
  16. return page.hasClass("in") && isTransitioning(page);
  17. },
  18. disableMaxTransWidth = function(){
  19. $.mobile.maxTransitionWidth = false;
  20. },
  21. enableMaxTransWidth = function(){
  22. $.mobile.maxTransitionWidth = defaultMaxTrans;
  23. },
  24. //animationComplete callback queue
  25. fromQueue = [],
  26. toQueue = [],
  27. resetQueues = function(){
  28. fromQueue = [];
  29. toQueue = [];
  30. },
  31. onFromComplete = function( f ){
  32. fromQueue.push( f );
  33. },
  34. onToComplete = function( f ){
  35. toQueue.push( f );
  36. },
  37. //wipe all urls
  38. clearUrlHistory = function(){
  39. $.mobile.navigate.history.stack = [];
  40. $.mobile.navigate.history.activeIndex = 0;
  41. };
  42. module('jquery.mobile.navigation.js', {
  43. setup: function(){
  44. // disable this option so we can test transitions regardless of window width
  45. disableMaxTransWidth();
  46. //stub to allow callback before function is returned to transition handler
  47. $.fn.animationComplete = function( callback ){
  48. animationCompleteFn.call( this, function(){
  49. var queue = $(this).is(".out") ? fromQueue : toQueue;
  50. for( var i = 0, il = queue.length; i < il; i++ ){
  51. queue.pop()( this );
  52. }
  53. callback();
  54. });
  55. return this;
  56. };
  57. resetQueues();
  58. clearUrlHistory();
  59. if ( location.hash !== "#harmless-default-page" ) {
  60. stop();
  61. $(document).one("pagechange", function() {
  62. start();
  63. } );
  64. location.hash = "#harmless-default-page";
  65. }
  66. },
  67. teardown: function(){
  68. // unmock animation complete
  69. $.fn.animationComplete = animationCompleteFn;
  70. enableMaxTransWidth();
  71. }
  72. });
  73. /*
  74. NOTES:
  75. Our default transition handler now has either one or two animationComplete calls - two if there are two pages in play (from and to)
  76. To is required, so each async function must call start() onToComplete, not onFromComplete.
  77. */
  78. asyncTest( "changePage applies perspective class to mobile viewport for flip", function(){
  79. expect(1);
  80. $.testHelper.pageSequence([
  81. function() {
  82. $.mobile.changePage("#foo");
  83. },
  84. function() {
  85. onToComplete( function( el ) {
  86. ok($("body").hasClass("viewport-flip") || $("body").hasClass("viewport-fade"), "has viewport-flip or viewport-fade");
  87. start();
  88. });
  89. $("#foo > a").first().click();
  90. }
  91. ]);
  92. });
  93. asyncTest( "changePage applies transition class to mobile viewport for default transition", function(){
  94. expect(1);
  95. $.testHelper.pageSequence([
  96. function() {
  97. $.mobile.changePage("#baz");
  98. },
  99. function() {
  100. onToComplete( function( el ){
  101. ok($("body").hasClass(transitioning), "has transitioning class");
  102. start();
  103. });
  104. $("#baz > a").click();
  105. }
  106. ]);
  107. });
  108. asyncTest( "explicit transition preferred for page navigation reversal (ie back)", function(){
  109. expect( 1 );
  110. onToComplete(function(){
  111. $("#flip-trans > a").click();
  112. onToComplete(function(){
  113. $("#fade-trans > a").click();
  114. onToComplete(function(){
  115. ok($("#flip-trans").hasClass("fade"), "has fade class");
  116. start();
  117. });
  118. });
  119. });
  120. $("#fade-trans > a").click();
  121. });
  122. asyncTest( "default transition is fade", function(){
  123. onToComplete(function(){
  124. ok($("#no-trans").hasClass("fade"), "has fade class");
  125. start();
  126. });
  127. $("#default-trans > a").click();
  128. });
  129. asyncTest( "changePage queues requests", function(){
  130. expect(4)
  131. var firstPage = $("#foo"),
  132. secondPage = $("#bar");
  133. $.mobile.changePage(firstPage);
  134. $.mobile.changePage(secondPage);
  135. onToComplete(function(){
  136. ok(isTransitioningIn(firstPage), "first page begins transition");
  137. ok(!isTransitioningIn(secondPage), "second page doesn't transition yet");
  138. onToComplete(function(){
  139. ok(!isTransitioningIn(firstPage), "first page transition should be complete");
  140. ok(isTransitioningIn(secondPage), "second page should begin transitioning");
  141. start();
  142. });
  143. });
  144. });
  145. test( "animationComplete return value", function(){
  146. $.fn.animationComplete = animationCompleteFn;
  147. equal($("#foo").animationComplete(function(){})[0], $("#foo")[0]);
  148. });
  149. // reusable function for a few tests below
  150. function testTransitionMaxWidth( val, expected ){
  151. expect( 1 );
  152. $.mobile.maxTransitionWidth = val;
  153. var transitionOccurred = false;
  154. onToComplete(function(){
  155. transitionOccurred = true;
  156. });
  157. return setTimeout(function(){
  158. ok( transitionOccurred === expected, (expected ? "" : "no ") + "transition occurred" );
  159. start();
  160. }, 5000);
  161. $.mobile.changePage( $(".ui-page:not(.ui-page-active)").first() );
  162. }
  163. asyncTest( "maxTransitionWidth property disables transitions when value is less than browser width", function(){
  164. testTransitionMaxWidth( $( window ).width() - 1, false );
  165. });
  166. asyncTest( "maxTransitionWidth property disables transitions when value is false", function(){
  167. testTransitionMaxWidth( false, false );
  168. });
  169. })(jQuery);