idle-timer_test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. (function($) {
  2. /*
  3. ======== A Handy Little QUnit Reference ========
  4. http://docs.jquery.com/QUnit
  5. Test methods:
  6. expect(numAssertions)
  7. stop(increment)
  8. start(decrement)
  9. Test assertions:
  10. ok(value, [message])
  11. equal(actual, expected, [message])
  12. notEqual(actual, expected, [message])
  13. deepEqual(actual, expected, [message])
  14. notDeepEqual(actual, expected, [message])
  15. strictEqual(actual, expected, [message])
  16. notStrictEqual(actual, expected, [message])
  17. raises(block, [expected], [message])
  18. */
  19. //Name | expects | test
  20. module("Events Auto Binding");
  21. asyncTest("idle Event Triggered", 2, function () {
  22. $(document).on("idle.idleTimer", function (event, elem, obj) {
  23. ok(true, "idle fires at document");
  24. ok(obj.idle, "object returned properly");
  25. $.idleTimer("destroy");
  26. $(document).off();
  27. start();
  28. });
  29. $.idleTimer( 100 );
  30. });
  31. asyncTest( "active Event Triggered", 2, function() {
  32. $( document ).on( "active.idleTimer", function(event, elem, obj){
  33. ok(true, "active fires at document");
  34. ok(!obj.idle, "object returned properly");
  35. $.idleTimer("destroy");
  36. $(document).off();
  37. start();
  38. });
  39. $.idleTimer({idle:true});
  40. setTimeout( function(){
  41. $( "#qunit-fixture" ).trigger( "keydown" );
  42. }, 100 );
  43. });
  44. module("Events Element Binding");
  45. asyncTest("idle Triggered", 2, function () {
  46. $("#qunit-fixture").on("idle.idleTimer", function (event, elem, obj) {
  47. ok(true, "idle fires at document");
  48. ok(obj.idle, "object returned properly");
  49. $("#qunit-fixture").idleTimer("destroy");
  50. start();
  51. });
  52. $("#qunit-fixture").idleTimer(100);
  53. });
  54. asyncTest("active Triggered", 2, function () {
  55. $("#qunit-fixture").on("active.idleTimer", function (event, elem, obj) {
  56. ok(true, "active fires at document");
  57. ok(!obj.idle, "object returned properly");
  58. $("#qunit-fixture").idleTimer("destroy");
  59. start();
  60. });
  61. $("#qunit-fixture").idleTimer({ idle: true });
  62. setTimeout(function () {
  63. $("#qunit-fixture").trigger("keydown");
  64. }, 100);
  65. });
  66. /*
  67. Need to actually test pause/resume/reset, not just thier return type
  68. */
  69. module("Functional");
  70. asyncTest("Pause works and is a jQuery instance", 4, function () {
  71. $.idleTimer(100);
  72. equal(typeof $.idleTimer( "pause" ).jquery , "string", "pause should be jquery" );
  73. $.idleTimer("resume");
  74. equal(typeof $(document).idleTimer("pause").jquery, "string", "pause should be jquery");
  75. setTimeout(function () {
  76. ok(!$.idleTimer("isIdle"), "timer still active");
  77. ok(!$(document).idleTimer("isIdle"), "timer still active");
  78. $.idleTimer("destroy");
  79. $(document).off();
  80. start();
  81. }, 200);
  82. });
  83. asyncTest("Resume works and is a jQuery instance", 4, function () {
  84. $.idleTimer(100);
  85. $.idleTimer("pause");
  86. equal(typeof $.idleTimer("resume").jquery, "string", "resume should be jquery");
  87. $.idleTimer("pause");
  88. equal(typeof $(document).idleTimer("resume").jquery, "string", "resume should be jquery");
  89. setTimeout(function () {
  90. ok($.idleTimer("isIdle"), "timer inactive");
  91. ok($(document).idleTimer("isIdle"), "timer inactive");
  92. $.idleTimer("destroy");
  93. $(document).off();
  94. start();
  95. }, 200);
  96. });
  97. test("Elapsed time is a number", 2, function () {
  98. $.idleTimer(100);
  99. equal(typeof $.idleTimer("getElapsedTime"), "number", "Elapsed time should be a number");
  100. equal(typeof $(document).idleTimer("getElapsedTime"), "number", "Elapsed time should be a number");
  101. });
  102. test("Init works and is a jQuery instance", 4, function () {
  103. equal(typeof $.idleTimer(100).jquery, "string", "Init should be jquery");
  104. equal(typeof $("#qunit-fixture").idleTimer(100).jquery, "string", "Destroy should be jquery");
  105. equal(typeof $(document).data("idleTimerObj").idle, "boolean", "Init data added");
  106. equal(typeof $("#qunit-fixture").data("idleTimerObj").idle, "boolean", "Init data added");
  107. });
  108. test("Destroy works and is a jQuery instance", 4, function () {
  109. $.idleTimer(100);
  110. $("#qunit-fixture").idleTimer(100);
  111. equal(typeof $.idleTimer("destroy").jquery, "string", "Destroy should be jquery");
  112. equal(typeof $("#qunit-fixture").idleTimer("destroy").jquery, "string", "Destroy should be jquery");
  113. equal(typeof $(document).data("idleTimerObj"), "undefined", "destroy removed data");
  114. equal(typeof $("#qunit-fixture").data("idleTimerObj"), "undefined", "destroy removed data");
  115. });
  116. asyncTest("Reset is a jQuery instance", 6, function () {
  117. //start the timer
  118. $.idleTimer(200);
  119. $.idleTimer("pause");
  120. $("#qunit-fixture").idleTimer(200);
  121. $("#qunit-fixture").idleTimer("pause");
  122. //After a bit, reset it
  123. setTimeout(function () {
  124. equal(typeof $.idleTimer("reset").jquery, "string", "reset should be jquery");
  125. equal(typeof $("#qunit-fixture").idleTimer("reset").jquery, "string", "reset should be jquery");
  126. ok($(document).data("idleTimerObj").remaining===null, "reset remaining");
  127. ok($("#qunit-fixture").data("idleTimerObj").remaining === null, "reset remaining");
  128. }, 100);
  129. setTimeout(function () {
  130. ok($.idleTimer("isIdle"), "timer inactive");
  131. ok($("#qunit-fixture").idleTimer("isIdle"), "timer inactive");
  132. $.idleTimer("destroy");
  133. $("#qunit-fixture").idleTimer("destroy");
  134. $(document).off();
  135. start();
  136. }, 400);
  137. });
  138. test("Last Active time is a number", 2, function () {
  139. $.idleTimer(100);
  140. equal(typeof $.idleTimer("getLastActiveTime"), "number", "Last Active time should be a number");
  141. equal(typeof $(document).idleTimer("getLastActiveTime"), "number", "Last Active time should be a number");
  142. $.idleTimer("destroy");
  143. });
  144. test("Remaining time is a number", 2, function () {
  145. $.idleTimer(100);
  146. equal( typeof $.idleTimer( "getRemainingTime" ), "number", "Remaining time should be a number" );
  147. equal(typeof $(document).idleTimer("getRemainingTime"), "number", "Remaining time should be a number");
  148. $.idleTimer("destroy");
  149. });
  150. test("isIdle is a boolean", 2, function () {
  151. $.idleTimer(100);
  152. equal(typeof $.idleTimer("isIdle"), "boolean", "isIdle should be a boolean");
  153. equal(typeof $(document).idleTimer("isIdle"), "boolean", "isIdle should be a boolean");
  154. $.idleTimer("destroy");
  155. });
  156. }(jQuery));