idletimer_test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.IdleTimerTest');
  15. goog.setTestOnly('goog.ui.IdleTimerTest');
  16. goog.require('goog.events');
  17. goog.require('goog.testing.MockClock');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.ui.IdleTimer');
  20. goog.require('goog.ui.MockActivityMonitor');
  21. var clock;
  22. function setUp() {
  23. clock = new goog.testing.MockClock(true);
  24. goog.now = goog.bind(clock.getCurrentTime, clock);
  25. }
  26. function tearDown() {
  27. clock.dispose();
  28. }
  29. /**
  30. * Tests whether an event is fired when the user becomes idle
  31. */
  32. function testBecomeIdle() {
  33. var idleThreshold = 1000;
  34. var mockActivityMonitor = new goog.ui.MockActivityMonitor();
  35. var idleTimer = new goog.ui.IdleTimer(idleThreshold, mockActivityMonitor);
  36. mockActivityMonitor.simulateEvent();
  37. assertFalse('Precondition: user should be active', idleTimer.isIdle());
  38. var onBecomeIdleCount = 0;
  39. var onBecomeIdle = function() { onBecomeIdleCount += 1; };
  40. goog.events.listen(
  41. idleTimer, goog.ui.IdleTimer.Event.BECOME_IDLE, onBecomeIdle);
  42. clock.tick(idleThreshold);
  43. mockActivityMonitor.simulateEvent();
  44. clock.tick(idleThreshold);
  45. assert('The BECOME_IDLE event fired too early', onBecomeIdleCount == 0);
  46. assertFalse('The user should still be active', idleTimer.isIdle());
  47. clock.tick(1);
  48. assert('The BECOME_IDLE event fired too late', onBecomeIdleCount == 1);
  49. assert('The user should be idle', idleTimer.isIdle());
  50. idleTimer.dispose();
  51. }
  52. /**
  53. * Tests whether an event is fired when the user becomes active
  54. */
  55. function testBecomeActive() {
  56. var idleThreshold = 1000;
  57. var mockActivityMonitor = new goog.ui.MockActivityMonitor();
  58. var idleTimer = new goog.ui.IdleTimer(idleThreshold, mockActivityMonitor);
  59. clock.tick(idleThreshold + 1);
  60. assert('Precondition: user should be idle', idleTimer.isIdle());
  61. var onBecomeActiveCount = 0;
  62. var onBecomeActive = function() { onBecomeActiveCount += 1; };
  63. goog.events.listen(
  64. idleTimer, goog.ui.IdleTimer.Event.BECOME_ACTIVE, onBecomeActive);
  65. clock.tick(idleThreshold);
  66. assert('The BECOME_ACTIVE event fired too early', onBecomeActiveCount == 0);
  67. assert('The user should still be idle', idleTimer.isIdle());
  68. mockActivityMonitor.simulateEvent();
  69. assert('The BECOME_ACTIVE event fired too late', onBecomeActiveCount == 1);
  70. assertFalse('The user should be active', idleTimer.isIdle());
  71. idleTimer.dispose();
  72. }