eventobserver_test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. goog.provide('goog.testing.events.EventObserverTest');
  15. goog.setTestOnly('goog.testing.events.EventObserverTest');
  16. goog.require('goog.array');
  17. goog.require('goog.events');
  18. goog.require('goog.events.Event');
  19. goog.require('goog.events.EventTarget');
  20. goog.require('goog.testing.events.EventObserver');
  21. goog.require('goog.testing.jsunit');
  22. // Return an event's type
  23. function getEventType(e) {
  24. return e.type;
  25. }
  26. function testGetEvents() {
  27. var observer = new goog.testing.events.EventObserver();
  28. var target = new goog.events.EventTarget();
  29. goog.events.listen(target, ['foo', 'bar', 'baz'], observer);
  30. var eventTypes = ['bar', 'baz', 'foo', 'qux', 'quux', 'corge', 'foo', 'baz'];
  31. goog.array.forEach(eventTypes, goog.bind(target.dispatchEvent, target));
  32. var replayEvents = observer.getEvents();
  33. assertArrayEquals(
  34. 'Only the listened-for event types should be remembered',
  35. ['bar', 'baz', 'foo', 'foo', 'baz'],
  36. goog.array.map(observer.getEvents(), getEventType));
  37. assertArrayEquals(
  38. ['bar'], goog.array.map(observer.getEvents('bar'), getEventType));
  39. assertArrayEquals(
  40. ['baz', 'baz'], goog.array.map(observer.getEvents('baz'), getEventType));
  41. assertArrayEquals(
  42. ['foo', 'foo'], goog.array.map(observer.getEvents('foo'), getEventType));
  43. }
  44. function testHandleEvent() {
  45. var events = [
  46. new goog.events.Event('foo'), new goog.events.Event('bar'),
  47. new goog.events.Event('baz')
  48. ];
  49. var observer = new goog.testing.events.EventObserver();
  50. goog.array.forEach(events, goog.bind(observer.handleEvent, observer));
  51. assertArrayEquals(events, observer.getEvents());
  52. assertArrayEquals([events[0]], observer.getEvents('foo'));
  53. assertArrayEquals([events[1]], observer.getEvents('bar'));
  54. assertArrayEquals([events[2]], observer.getEvents('baz'));
  55. }
  56. function testClear() {
  57. var event = new goog.events.Event('foo');
  58. var observer = new goog.testing.events.EventObserver();
  59. observer.handleEvent(event);
  60. assertArrayEquals([event], observer.getEvents());
  61. observer.clear();
  62. assertArrayEquals([], observer.getEvents());
  63. var otherEvent = new goog.events.Event('baz');
  64. observer.handleEvent(otherEvent);
  65. assertArrayEquals([otherEvent], observer.getEvents());
  66. }