mockactivitymonitor_test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2013 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. /**
  15. * @fileoverview Tests for goog.ui.MockActivityMonitorTest.
  16. * @author nnaze@google.com (Nathan Naze)
  17. */
  18. /** @suppress {extraProvide} */
  19. goog.provide('goog.ui.MockActivityMonitorTest');
  20. goog.require('goog.events');
  21. goog.require('goog.functions');
  22. goog.require('goog.testing.jsunit');
  23. goog.require('goog.testing.recordFunction');
  24. goog.require('goog.ui.ActivityMonitor');
  25. goog.require('goog.ui.MockActivityMonitor');
  26. goog.setTestOnly('goog.ui.MockActivityMonitorTest');
  27. var googNow = goog.now;
  28. var monitor;
  29. var recordedFunction;
  30. var replacer;
  31. function setUp() {
  32. monitor = new goog.ui.MockActivityMonitor();
  33. recordedFunction = goog.testing.recordFunction();
  34. goog.events.listen(
  35. monitor, goog.ui.ActivityMonitor.Event.ACTIVITY, recordedFunction);
  36. }
  37. function tearDown() {
  38. goog.dispose(monitor);
  39. goog.now = googNow;
  40. }
  41. function testEventFireSameTime() {
  42. goog.now = goog.functions.constant(1000);
  43. monitor.simulateEvent();
  44. assertEquals(1, recordedFunction.getCallCount());
  45. monitor.simulateEvent();
  46. assertEquals(2, recordedFunction.getCallCount());
  47. }
  48. function testEventFireDifferingTime() {
  49. goog.now = goog.functions.constant(1000);
  50. monitor.simulateEvent();
  51. assertEquals(1, recordedFunction.getCallCount());
  52. goog.now = goog.functions.constant(1001);
  53. monitor.simulateEvent();
  54. assertEquals(2, recordedFunction.getCallCount());
  55. }
  56. function testDispatchEventReturnValue() {
  57. assertTrue(monitor.dispatchEvent(goog.ui.ActivityMonitor.Event.ACTIVITY));
  58. assertEquals(1, recordedFunction.getCallCount());
  59. }
  60. function testDispatchEventPreventDefault() {
  61. // Undo the listen call in setUp.
  62. goog.events.unlisten(
  63. monitor, goog.ui.ActivityMonitor.Event.ACTIVITY, recordedFunction);
  64. // Listen with a function that cancels the event.
  65. goog.events.listen(
  66. monitor, goog.ui.ActivityMonitor.Event.ACTIVITY,
  67. function(e) { e.preventDefault(); });
  68. assertFalse(monitor.dispatchEvent(goog.ui.ActivityMonitor.Event.ACTIVITY));
  69. }