2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {NgZone} from '@angular/core/src/zone/ng_zone';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
|
2016-08-02 18:53:34 -04:00
|
|
|
import {DomEventsPlugin} from '@angular/platform-browser/src/dom/events/dom_events';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {EventManager, EventManagerPlugin} from '@angular/platform-browser/src/dom/events/event_manager';
|
2019-08-27 19:21:39 -04:00
|
|
|
|
|
|
|
import {createMouseEvent, el} from '../../../testing/src/browser_util';
|
2015-02-09 09:11:31 -05:00
|
|
|
|
2017-12-18 01:18:50 -05:00
|
|
|
(function() {
|
|
|
|
if (isNode) return;
|
2016-11-12 08:08:58 -05:00
|
|
|
let domEventPlugin: DomEventsPlugin;
|
2017-02-14 19:14:40 -05:00
|
|
|
let doc: any;
|
2017-07-13 18:36:11 -04:00
|
|
|
let zone: NgZone;
|
2015-02-19 22:10:16 -05:00
|
|
|
|
2015-02-09 09:11:31 -05:00
|
|
|
describe('EventManager', () => {
|
|
|
|
|
2017-02-14 19:14:40 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
doc = getDOM().supportsDOMEvents() ? document : getDOM().createHtmlDocument();
|
2017-07-13 18:36:11 -04:00
|
|
|
zone = new NgZone({});
|
2018-05-28 03:20:53 -04:00
|
|
|
domEventPlugin = new DomEventsPlugin(doc, zone, null);
|
2017-02-14 19:14:40 -05:00
|
|
|
});
|
2016-02-10 19:54:32 -05:00
|
|
|
|
2015-09-04 17:23:13 -04:00
|
|
|
it('should delegate event bindings to plugins that are passed in from the most generic one to the most specific one',
|
|
|
|
() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const element = el('<div></div>');
|
|
|
|
const handler = (e: any /** TODO #9100 */) => e;
|
2017-02-14 19:14:40 -05:00
|
|
|
const plugin = new FakeEventManagerPlugin(doc, ['click']);
|
2016-11-12 08:08:58 -05:00
|
|
|
const manager = new EventManager([domEventPlugin, plugin], new FakeNgZone());
|
2015-09-04 17:23:13 -04:00
|
|
|
manager.addEventListener(element, 'click', handler);
|
2016-09-19 20:15:57 -04:00
|
|
|
expect(plugin.eventHandler['click']).toBe(handler);
|
2015-09-04 17:23:13 -04:00
|
|
|
});
|
2015-02-09 09:11:31 -05:00
|
|
|
|
|
|
|
it('should delegate event bindings to the first plugin supporting the event', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const element = el('<div></div>');
|
|
|
|
const clickHandler = (e: any /** TODO #9100 */) => e;
|
|
|
|
const dblClickHandler = (e: any /** TODO #9100 */) => e;
|
2017-02-14 19:14:40 -05:00
|
|
|
const plugin1 = new FakeEventManagerPlugin(doc, ['dblclick']);
|
|
|
|
const plugin2 = new FakeEventManagerPlugin(doc, ['click', 'dblclick']);
|
2016-11-12 08:08:58 -05:00
|
|
|
const manager = new EventManager([plugin2, plugin1], new FakeNgZone());
|
2015-02-09 09:11:31 -05:00
|
|
|
manager.addEventListener(element, 'click', clickHandler);
|
|
|
|
manager.addEventListener(element, 'dblclick', dblClickHandler);
|
2016-09-19 20:15:57 -04:00
|
|
|
expect(plugin2.eventHandler['click']).toBe(clickHandler);
|
|
|
|
expect(plugin1.eventHandler['dblclick']).toBe(dblClickHandler);
|
2015-02-09 09:11:31 -05:00
|
|
|
});
|
|
|
|
|
2015-02-19 22:10:16 -05:00
|
|
|
it('should throw when no plugin can handle the event', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const element = el('<div></div>');
|
2017-02-14 19:14:40 -05:00
|
|
|
const plugin = new FakeEventManagerPlugin(doc, ['dblclick']);
|
2016-11-12 08:08:58 -05:00
|
|
|
const manager = new EventManager([plugin], new FakeNgZone());
|
2017-03-24 12:59:41 -04:00
|
|
|
expect(() => manager.addEventListener(element, 'click', null !))
|
2015-05-26 12:25:39 -04:00
|
|
|
.toThrowError('No event manager plugin found for event click');
|
2015-02-19 22:10:16 -05:00
|
|
|
});
|
|
|
|
|
2015-09-01 11:52:54 -04:00
|
|
|
it('events are caught when fired from a child', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const element = el('<div><div></div></div>');
|
2015-02-19 22:10:16 -05:00
|
|
|
// Workaround for https://bugs.webkit.org/show_bug.cgi?id=122755
|
2017-02-14 19:14:40 -05:00
|
|
|
getDOM().appendChild(doc.body, element);
|
2015-02-19 22:10:16 -05:00
|
|
|
|
2019-08-27 19:21:39 -04:00
|
|
|
const child = element.firstChild as Element;
|
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2016-11-12 08:08:58 -05:00
|
|
|
let receivedEvent: any /** TODO #9100 */ = null;
|
|
|
|
const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; };
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
2015-09-01 11:52:54 -04:00
|
|
|
manager.addEventListener(element, 'click', handler);
|
2016-04-28 20:50:03 -04:00
|
|
|
getDOM().dispatchEvent(child, dispatchedEvent);
|
2015-02-19 22:10:16 -05:00
|
|
|
|
2015-02-09 09:11:31 -05:00
|
|
|
expect(receivedEvent).toBe(dispatchedEvent);
|
|
|
|
});
|
2015-04-02 09:56:58 -04:00
|
|
|
|
2015-09-01 11:52:54 -04:00
|
|
|
it('should add and remove global event listeners', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const element = el('<div><div></div></div>');
|
2017-02-14 19:14:40 -05:00
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2016-11-12 08:08:58 -05:00
|
|
|
let receivedEvent: any /** TODO #9100 */ = null;
|
|
|
|
const handler = (e: any /** TODO #9100 */) => { receivedEvent = e; };
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
2015-04-02 09:56:58 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const remover = manager.addGlobalEventListener('document', 'click', handler);
|
2016-04-28 20:50:03 -04:00
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
2015-04-02 09:56:58 -04:00
|
|
|
expect(receivedEvent).toBe(dispatchedEvent);
|
|
|
|
|
|
|
|
receivedEvent = null;
|
|
|
|
remover();
|
2016-04-28 20:50:03 -04:00
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
2015-04-02 09:56:58 -04:00
|
|
|
expect(receivedEvent).toBe(null);
|
|
|
|
});
|
2017-08-31 02:52:51 -04:00
|
|
|
|
|
|
|
it('should keep zone when addEventListener', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-08-31 02:52:51 -04:00
|
|
|
let receivedEvent: any /** TODO #9100 */ = null;
|
|
|
|
let receivedZone: any = null;
|
|
|
|
const handler = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvent = e;
|
|
|
|
receivedZone = Zone.current;
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
2018-02-08 11:59:25 -05:00
|
|
|
let remover: any = null;
|
2017-08-31 02:52:51 -04:00
|
|
|
Zone.root.run(() => { remover = manager.addEventListener(element, 'click', handler); });
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
2017-09-01 13:30:37 -04:00
|
|
|
expect(receivedEvent).toBe(dispatchedEvent);
|
|
|
|
expect(receivedZone.name).toBe(Zone.root.name);
|
|
|
|
|
|
|
|
receivedEvent = null;
|
|
|
|
remover && remover();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvent).toBe(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should keep zone when addEventListener multiple times', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-09-01 13:30:37 -04:00
|
|
|
let receivedEvents: any[] /** TODO #9100 */ = [];
|
|
|
|
let receivedZones: any[] = [];
|
|
|
|
const handler1 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const handler2 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
2018-02-08 11:59:25 -05:00
|
|
|
let remover1: any = null;
|
|
|
|
let remover2: any = null;
|
2017-09-01 13:30:37 -04:00
|
|
|
Zone.root.run(() => { remover1 = manager.addEventListener(element, 'click', handler1); });
|
|
|
|
Zone.root.fork({name: 'test'}).run(() => {
|
|
|
|
remover2 = manager.addEventListener(element, 'click', handler2);
|
|
|
|
});
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]);
|
|
|
|
expect(receivedZones).toEqual([Zone.root.name, 'test']);
|
|
|
|
|
|
|
|
receivedEvents = [];
|
|
|
|
remover1 && remover1();
|
|
|
|
remover2 && remover2();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2017-11-15 21:43:53 -05:00
|
|
|
it('should support event.stopImmediatePropagation', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-11-15 21:43:53 -05:00
|
|
|
let receivedEvents: any[] /** TODO #9100 */ = [];
|
|
|
|
let receivedZones: any[] = [];
|
|
|
|
const handler1 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
};
|
|
|
|
const handler2 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
2018-02-08 11:59:25 -05:00
|
|
|
let remover1: any = null;
|
|
|
|
let remover2: any = null;
|
2017-11-15 21:43:53 -05:00
|
|
|
Zone.root.run(() => { remover1 = manager.addEventListener(element, 'click', handler1); });
|
|
|
|
Zone.root.fork({name: 'test'}).run(() => {
|
|
|
|
remover2 = manager.addEventListener(element, 'click', handler2);
|
|
|
|
});
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([dispatchedEvent]);
|
|
|
|
expect(receivedZones).toEqual([Zone.root.name]);
|
|
|
|
|
|
|
|
receivedEvents = [];
|
|
|
|
remover1 && remover1();
|
|
|
|
remover2 && remover2();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2017-09-01 13:30:37 -04:00
|
|
|
it('should handle event correctly when one handler remove itself ', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-09-01 13:30:37 -04:00
|
|
|
let receivedEvents: any[] /** TODO #9100 */ = [];
|
|
|
|
let receivedZones: any[] = [];
|
|
|
|
let remover1: any = null;
|
|
|
|
let remover2: any = null;
|
|
|
|
const handler1 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
remover1 && remover1();
|
|
|
|
};
|
|
|
|
const handler2 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
|
|
|
Zone.root.run(() => { remover1 = manager.addEventListener(element, 'click', handler1); });
|
|
|
|
Zone.root.fork({name: 'test'}).run(() => {
|
|
|
|
remover2 = manager.addEventListener(element, 'click', handler2);
|
|
|
|
});
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]);
|
|
|
|
expect(receivedZones).toEqual([Zone.root.name, 'test']);
|
|
|
|
|
|
|
|
receivedEvents = [];
|
|
|
|
remover1 && remover1();
|
|
|
|
remover2 && remover2();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should only add same callback once when addEventListener', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-09-01 13:30:37 -04:00
|
|
|
let receivedEvents: any[] /** TODO #9100 */ = [];
|
|
|
|
let receivedZones: any[] = [];
|
|
|
|
const handler = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
2018-02-08 11:59:25 -05:00
|
|
|
let remover1: any = null;
|
|
|
|
let remover2: any = null;
|
2017-09-01 13:30:37 -04:00
|
|
|
Zone.root.run(() => { remover1 = manager.addEventListener(element, 'click', handler); });
|
|
|
|
Zone.root.fork({name: 'test'}).run(() => {
|
|
|
|
remover2 = manager.addEventListener(element, 'click', handler);
|
|
|
|
});
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([dispatchedEvent]);
|
|
|
|
expect(receivedZones).toEqual([Zone.root.name]);
|
|
|
|
|
|
|
|
receivedEvents = [];
|
|
|
|
remover1 && remover1();
|
|
|
|
remover2 && remover2();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2017-11-20 02:33:37 -05:00
|
|
|
it('should be able to remove event listener which was added inside of ngZone', () => {
|
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('click');
|
2017-11-20 02:33:37 -05:00
|
|
|
let receivedEvents: any[] /** TODO #9100 */ = [];
|
|
|
|
let receivedZones: any[] = [];
|
|
|
|
const handler1 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const handler2 = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvents.push(e);
|
|
|
|
receivedZones.push(Zone.current.name);
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
2018-02-08 11:59:25 -05:00
|
|
|
let remover1: any = null;
|
|
|
|
let remover2: any = null;
|
2017-11-20 02:33:37 -05:00
|
|
|
// handler1 is added in root zone
|
|
|
|
Zone.root.run(() => { remover1 = manager.addEventListener(element, 'click', handler1); });
|
|
|
|
// handler2 is added in 'angular' zone
|
|
|
|
Zone.root.fork({name: 'fakeAngularZone', properties: {isAngularZone: true}}).run(() => {
|
|
|
|
remover2 = manager.addEventListener(element, 'click', handler2);
|
|
|
|
});
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvents).toEqual([dispatchedEvent, dispatchedEvent]);
|
2017-11-29 15:56:29 -05:00
|
|
|
expect(receivedZones).toEqual([Zone.root.name, 'fakeAngularZone']);
|
2017-11-20 02:33:37 -05:00
|
|
|
|
|
|
|
receivedEvents = [];
|
|
|
|
remover1 && remover1();
|
|
|
|
remover2 && remover2();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
// handler1 and handler2 are added in different zone
|
|
|
|
// one is angular zone, the other is not
|
|
|
|
// should still be able to remove them correctly
|
|
|
|
expect(receivedEvents).toEqual([]);
|
|
|
|
});
|
|
|
|
|
2019-02-05 13:48:18 -05:00
|
|
|
it('should run blockListedEvents handler outside of ngZone', () => {
|
2017-09-01 13:30:37 -04:00
|
|
|
const Zone = (window as any)['Zone'];
|
|
|
|
const element = el('<div><div></div></div>');
|
|
|
|
getDOM().appendChild(doc.body, element);
|
2019-08-27 19:21:39 -04:00
|
|
|
const dispatchedEvent = createMouseEvent('scroll');
|
2017-09-01 13:30:37 -04:00
|
|
|
let receivedEvent: any /** TODO #9100 */ = null;
|
|
|
|
let receivedZone: any = null;
|
|
|
|
const handler = (e: any /** TODO #9100 */) => {
|
|
|
|
receivedEvent = e;
|
|
|
|
receivedZone = Zone.current;
|
|
|
|
};
|
|
|
|
const manager = new EventManager([domEventPlugin], new FakeNgZone());
|
|
|
|
|
|
|
|
let remover = manager.addEventListener(element, 'scroll', handler);
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
2017-08-31 02:52:51 -04:00
|
|
|
expect(receivedEvent).toBe(dispatchedEvent);
|
|
|
|
expect(receivedZone.name).toBe(Zone.root.name);
|
|
|
|
|
|
|
|
receivedEvent = null;
|
|
|
|
remover && remover();
|
|
|
|
getDOM().dispatchEvent(element, dispatchedEvent);
|
|
|
|
expect(receivedEvent).toBe(null);
|
|
|
|
});
|
2015-02-09 09:11:31 -05:00
|
|
|
});
|
2017-12-18 01:18:50 -05:00
|
|
|
})();
|
2015-02-09 09:11:31 -05:00
|
|
|
|
2016-09-19 20:15:57 -04:00
|
|
|
/** @internal */
|
2015-02-09 09:11:31 -05:00
|
|
|
class FakeEventManagerPlugin extends EventManagerPlugin {
|
2016-09-19 20:15:57 -04:00
|
|
|
eventHandler: {[event: string]: Function} = {};
|
|
|
|
|
2017-02-14 19:14:40 -05:00
|
|
|
constructor(doc: any, public supportedEvents: string[]) { super(doc); }
|
2015-02-09 09:11:31 -05:00
|
|
|
|
2016-09-19 20:15:57 -04:00
|
|
|
supports(eventName: string): boolean { return this.supportedEvents.indexOf(eventName) > -1; }
|
2015-02-09 09:11:31 -05:00
|
|
|
|
2016-09-19 20:15:57 -04:00
|
|
|
addEventListener(element: any, eventName: string, handler: Function) {
|
|
|
|
this.eventHandler[eventName] = handler;
|
|
|
|
return () => { delete (this.eventHandler[eventName]); };
|
2015-02-09 09:11:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 16:33:22 -04:00
|
|
|
class FakeNgZone extends NgZone {
|
2015-05-26 12:25:39 -04:00
|
|
|
constructor() { super({enableLongStackTrace: false}); }
|
2017-08-02 19:30:50 -04:00
|
|
|
run<T>(fn: (...args: any[]) => T, applyThis?: any, applyArgs?: any[]): T { return fn(); }
|
2016-09-19 20:15:57 -04:00
|
|
|
runOutsideAngular(fn: Function) { return fn(); }
|
2015-02-09 09:11:31 -05:00
|
|
|
}
|