activitymonitor_test.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2006 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.ActivityMonitorTest');
  15. goog.setTestOnly('goog.ui.ActivityMonitorTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events');
  18. goog.require('goog.events.Event');
  19. goog.require('goog.events.EventType');
  20. goog.require('goog.testing.MockClock');
  21. goog.require('goog.testing.PropertyReplacer');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.testing.recordFunction');
  25. goog.require('goog.ui.ActivityMonitor');
  26. var mockClock;
  27. var stubs = new goog.testing.PropertyReplacer();
  28. var mydiv;
  29. function setUp() {
  30. mydiv = document.getElementById('mydiv');
  31. mockClock = new goog.testing.MockClock(true);
  32. // ActivityMonitor initializes a private to 0 which it compares to now(),
  33. // so tests fail unless we start the mock clock with something besides 0.
  34. mockClock.tick(1);
  35. }
  36. function tearDown() {
  37. mockClock.dispose();
  38. stubs.reset();
  39. }
  40. function testIdle() {
  41. var activityMonitor = new goog.ui.ActivityMonitor();
  42. assertEquals(
  43. 'Upon creation, last event time should be creation time',
  44. mockClock.getCurrentTime(), activityMonitor.getLastEventTime());
  45. mockClock.tick(1000);
  46. activityMonitor.resetTimer();
  47. var resetTime = mockClock.getCurrentTime();
  48. assertEquals(
  49. 'Upon reset, last event time should be reset time', resetTime,
  50. activityMonitor.getLastEventTime());
  51. assertEquals(
  52. 'Upon reset, idle time should be zero', 0, activityMonitor.getIdleTime());
  53. mockClock.tick(1000);
  54. assertEquals(
  55. '1s after reset, last event time should be reset time', resetTime,
  56. activityMonitor.getLastEventTime());
  57. assertEquals(
  58. '1s after reset, idle time should be 1s', 1000,
  59. activityMonitor.getIdleTime());
  60. }
  61. function testEventFired() {
  62. var activityMonitor = new goog.ui.ActivityMonitor();
  63. var listener = goog.testing.recordFunction();
  64. goog.events.listen(
  65. activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener);
  66. mockClock.tick(1000);
  67. goog.testing.events.fireClickEvent(mydiv);
  68. assertEquals(
  69. 'Activity event should fire when click happens after creation', 1,
  70. listener.getCallCount());
  71. mockClock.tick(3000);
  72. goog.testing.events.fireClickEvent(mydiv);
  73. assertEquals(
  74. 'Activity event should not fire when click happens 3s or ' +
  75. 'less since the last activity',
  76. 1, listener.getCallCount());
  77. mockClock.tick(1);
  78. goog.testing.events.fireClickEvent(mydiv);
  79. assertEquals(
  80. 'Activity event should fire when click happens more than ' +
  81. '3s since the last activity',
  82. 2, listener.getCallCount());
  83. }
  84. function testEventFiredWhenPropagationStopped() {
  85. var activityMonitor = new goog.ui.ActivityMonitor();
  86. var listener = goog.testing.recordFunction();
  87. goog.events.listen(
  88. activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener);
  89. goog.events.listenOnce(
  90. mydiv, goog.events.EventType.CLICK, goog.events.Event.stopPropagation);
  91. goog.testing.events.fireClickEvent(mydiv);
  92. assertEquals(
  93. 'Activity event should fire despite click propagation ' +
  94. 'stopped because listening on capture',
  95. 1, listener.getCallCount());
  96. }
  97. function testEventNotFiredWhenPropagationStopped() {
  98. var activityMonitor = new goog.ui.ActivityMonitor(undefined, true);
  99. var listener = goog.testing.recordFunction();
  100. goog.events.listen(
  101. activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener);
  102. goog.events.listenOnce(
  103. mydiv, goog.events.EventType.CLICK, goog.events.Event.stopPropagation);
  104. goog.testing.events.fireClickEvent(mydiv);
  105. assertEquals(
  106. 'Activity event should not fire since click propagation ' +
  107. 'stopped and listening on bubble',
  108. 0, listener.getCallCount());
  109. }
  110. function testTouchSequenceFired() {
  111. var activityMonitor = new goog.ui.ActivityMonitor();
  112. var listener = goog.testing.recordFunction();
  113. goog.events.listen(
  114. activityMonitor, goog.ui.ActivityMonitor.Event.ACTIVITY, listener);
  115. mockClock.tick(1000);
  116. goog.testing.events.fireTouchSequence(mydiv);
  117. assertEquals(
  118. 'Activity event should fire when touch happens after creation', 1,
  119. listener.getCallCount());
  120. mockClock.tick(3000);
  121. goog.testing.events.fireTouchSequence(mydiv);
  122. assertEquals(
  123. 'Activity event should not fire when touch happens 3s or ' +
  124. 'less since the last activity',
  125. 1, listener.getCallCount());
  126. mockClock.tick(1);
  127. goog.testing.events.fireTouchSequence(mydiv);
  128. assertEquals(
  129. 'Activity event should fire when touch happens more than ' +
  130. '3s since the last activity',
  131. 2, listener.getCallCount());
  132. }
  133. function testAddDocument_duplicate() {
  134. var defaultDoc = goog.dom.getDomHelper().getDocument();
  135. var activityMonitor = new goog.ui.ActivityMonitor();
  136. assertEquals(1, activityMonitor.documents_.length);
  137. assertEquals(defaultDoc, activityMonitor.documents_[0]);
  138. var listenerCount = activityMonitor.eventHandler_.getListenerCount();
  139. activityMonitor.addDocument(defaultDoc);
  140. assertEquals(1, activityMonitor.documents_.length);
  141. assertEquals(defaultDoc, activityMonitor.documents_[0]);
  142. assertEquals(listenerCount, activityMonitor.eventHandler_.getListenerCount());
  143. }