123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- goog.provide('goog.testing.events.EventObserverTest');
- goog.setTestOnly('goog.testing.events.EventObserverTest');
- goog.require('goog.array');
- goog.require('goog.events');
- goog.require('goog.events.Event');
- goog.require('goog.events.EventTarget');
- goog.require('goog.testing.events.EventObserver');
- goog.require('goog.testing.jsunit');
- function getEventType(e) {
- return e.type;
- }
- function testGetEvents() {
- var observer = new goog.testing.events.EventObserver();
- var target = new goog.events.EventTarget();
- goog.events.listen(target, ['foo', 'bar', 'baz'], observer);
- var eventTypes = ['bar', 'baz', 'foo', 'qux', 'quux', 'corge', 'foo', 'baz'];
- goog.array.forEach(eventTypes, goog.bind(target.dispatchEvent, target));
- var replayEvents = observer.getEvents();
- assertArrayEquals(
- 'Only the listened-for event types should be remembered',
- ['bar', 'baz', 'foo', 'foo', 'baz'],
- goog.array.map(observer.getEvents(), getEventType));
- assertArrayEquals(
- ['bar'], goog.array.map(observer.getEvents('bar'), getEventType));
- assertArrayEquals(
- ['baz', 'baz'], goog.array.map(observer.getEvents('baz'), getEventType));
- assertArrayEquals(
- ['foo', 'foo'], goog.array.map(observer.getEvents('foo'), getEventType));
- }
- function testHandleEvent() {
- var events = [
- new goog.events.Event('foo'), new goog.events.Event('bar'),
- new goog.events.Event('baz')
- ];
- var observer = new goog.testing.events.EventObserver();
- goog.array.forEach(events, goog.bind(observer.handleEvent, observer));
- assertArrayEquals(events, observer.getEvents());
- assertArrayEquals([events[0]], observer.getEvents('foo'));
- assertArrayEquals([events[1]], observer.getEvents('bar'));
- assertArrayEquals([events[2]], observer.getEvents('baz'));
- }
- function testClear() {
- var event = new goog.events.Event('foo');
- var observer = new goog.testing.events.EventObserver();
- observer.handleEvent(event);
- assertArrayEquals([event], observer.getEvents());
- observer.clear();
- assertArrayEquals([], observer.getEvents());
- var otherEvent = new goog.events.Event('baz');
- observer.handleEvent(otherEvent);
- assertArrayEquals([otherEvent], observer.getEvents());
- }
|