eventobserver.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2010 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 Event observer.
  16. *
  17. * Provides an event observer that holds onto events that it handles. This
  18. * can be used in unit testing to verify an event target's events --
  19. * that the order count, types, etc. are correct.
  20. *
  21. * Example usage:
  22. * <pre>
  23. * var observer = new goog.testing.events.EventObserver();
  24. * var widget = new foo.Widget();
  25. * goog.events.listen(widget, ['select', 'submit'], observer);
  26. * // Simulate user action of 3 select events and 2 submit events.
  27. * assertEquals(3, observer.getEvents('select').length);
  28. * assertEquals(2, observer.getEvents('submit').length);
  29. * </pre>
  30. *
  31. * @author nnaze@google.com (Nathan Naze)
  32. */
  33. goog.setTestOnly('goog.testing.events.EventObserver');
  34. goog.provide('goog.testing.events.EventObserver');
  35. goog.require('goog.array');
  36. /**
  37. * Event observer. Implements a handleEvent interface so it may be used as
  38. * a listener in listening functions and methods.
  39. * @see goog.events.listen
  40. * @see goog.events.EventHandler
  41. * @constructor
  42. * @final
  43. */
  44. goog.testing.events.EventObserver = function() {
  45. /**
  46. * A list of events handled by the observer in order of handling, oldest to
  47. * newest.
  48. * @type {!Array<!goog.events.Event>}
  49. * @private
  50. */
  51. this.events_ = [];
  52. };
  53. /**
  54. * Handles an event and remembers it. Event listening functions and methods
  55. * will call this method when this observer is used as a listener.
  56. * @see goog.events.listen
  57. * @see goog.events.EventHandler
  58. * @param {!goog.events.Event} e Event to handle.
  59. */
  60. goog.testing.events.EventObserver.prototype.handleEvent = function(e) {
  61. this.events_.push(e);
  62. };
  63. /**
  64. * @param {string=} opt_type If given, only return events of this type.
  65. * @return {!Array<!goog.events.Event>} The events handled, oldest to newest.
  66. */
  67. goog.testing.events.EventObserver.prototype.getEvents = function(opt_type) {
  68. var events = goog.array.clone(this.events_);
  69. if (opt_type) {
  70. events = goog.array.filter(
  71. events, function(event) { return event.type == opt_type; });
  72. }
  73. return events;
  74. };
  75. /** Clears the list of events seen by this observer. */
  76. goog.testing.events.EventObserver.prototype.clear = function() {
  77. this.events_ = [];
  78. };