jquery.testHelper.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * mobile support unit tests
  3. */
  4. (function( $ ) {
  5. $.testHelper = {
  6. excludeFileProtocol: function(callback){
  7. var message = "Tests require script reload and cannot be run via file: protocol";
  8. if (location.protocol == "file:") {
  9. test(message, function(){
  10. ok(false, message);
  11. });
  12. } else {
  13. callback();
  14. }
  15. },
  16. // TODO prevent test suite loads when the browser doesn't support push state
  17. // and push-state false is defined.
  18. setPushStateFor: function( libs ) {
  19. if( $.support.pushState && location.search.indexOf( "push-state" ) >= 0 ) {
  20. $.support.pushState = false;
  21. }
  22. $.each(libs, function(i, l) {
  23. $( "<script>", { src: l }).appendTo("head");
  24. });
  25. },
  26. reloads: {},
  27. reloadLib: function(libName){
  28. if(this.reloads[libName] === undefined) {
  29. this.reloads[libName] = {
  30. lib: $("script[src$='" + libName + "']"),
  31. count: 0
  32. };
  33. }
  34. var lib = this.reloads[libName].lib.clone(),
  35. src = lib.attr('src');
  36. //NOTE append "cache breaker" to force reload
  37. lib.attr('src', src + "?" + this.reloads[libName].count++);
  38. $("body").append(lib);
  39. },
  40. rerunQunit: function(){
  41. var self = this;
  42. QUnit.init();
  43. $("script:not([src*='.\/'])").each(function(i, elem){
  44. var src = elem.src.split("/");
  45. self.reloadLib(src[src.length - 1]);
  46. });
  47. QUnit.start();
  48. },
  49. alterExtend: function(extraExtension){
  50. var extendFn = $.extend;
  51. $.extend = function(object, extension){
  52. // NOTE extend the object as normal
  53. var result = extendFn.apply(this, arguments);
  54. // NOTE add custom extensions
  55. result = extendFn(result, extraExtension);
  56. return result;
  57. };
  58. },
  59. hideActivePageWhenComplete: function() {
  60. if( $('#qunit-testresult').length > 0 ) {
  61. $('.ui-page-active').css('display', 'none');
  62. } else {
  63. setTimeout($.testHelper.hideActivePageWhenComplete, 500);
  64. }
  65. },
  66. openPage: function(hash){
  67. location.href = location.href.split('#')[0] + hash;
  68. },
  69. sequence: function(fns, interval){
  70. $.each(fns, function(i, fn){
  71. setTimeout(fn, i * interval);
  72. });
  73. },
  74. pageSequence: function(fns){
  75. this.eventSequence("pagechange", fns);
  76. },
  77. eventSequence: function(event, fns, timedOut){
  78. var fn = fns.shift(),
  79. self = this;
  80. if( fn === undefined ) return;
  81. // if a pagechange or defined event is never triggered
  82. // continue in the sequence to alert possible failures
  83. var warnTimer = setTimeout(function(){
  84. self.eventSequence(event, fns, true);
  85. }, 2000);
  86. // bind the recursive call to the event
  87. $.mobile.pageContainer.one(event, function(){
  88. clearTimeout(warnTimer);
  89. // Let the current stack unwind before we fire off the next item in the sequence.
  90. // TODO setTimeout(self.pageSequence, 0, [fns, event]);
  91. setTimeout(function(){ self.eventSequence(event, fns); }, 0);
  92. });
  93. // invoke the function which should, in some fashion,
  94. // trigger the defined event
  95. fn(timedOut);
  96. },
  97. decorate: function(opts){
  98. var thisVal = opts.self || window;
  99. return function(){
  100. var returnVal;
  101. opts.before && opts.before.apply(thisVal, arguments);
  102. returnVal = opts.fn.apply(thisVal, arguments);
  103. opts.after && opts.after.apply(thisVal, arguments);
  104. return returnVal;
  105. };
  106. },
  107. assertUrlLocation: function( args ) {
  108. var parts = $.mobile.path.parseUrl( location.href ),
  109. pathnameOnward = location.href.replace( parts.domain, "" );
  110. if( $.support.pushState ) {
  111. same( pathnameOnward, args.hashOrPush || args.push, args.report );
  112. } else {
  113. same( parts.hash, "#" + (args.hashOrPush || args.hash), args.report );
  114. }
  115. }
  116. };
  117. })(jQuery);