1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 /** @type {base.EventSourceImpl} */
10 var eventSource
= null;
12 /** @type {HTMLElement} */
13 var domElement
= null;
15 /** @type {chromeMocks.Event} */
16 var myChromeEvent
= null;
18 /** @type {Listener} */
22 * @param {HTMLElement} element
25 var Listener = function(element
) {
26 /** @type {(sinon.Spy|function(...?))} */
27 this.onChromeEvent
= sinon
.spy();
28 /** @type {(sinon.Spy|function(...?))} */
29 this.onClickEvent
= sinon
.spy();
30 /** @type {(sinon.Spy|function(...?))} */
31 this.onCustomEvent
= sinon
.spy();
33 this.eventHooks_
= new base
.Disposables(
34 new base
.DomEventHook(element
, 'click', this.onClickEvent
.bind(this),
36 new base
.EventHook(eventSource
, 'customEvent',
37 this.onCustomEvent
.bind(this)),
38 new base
.ChromeEventHook(myChromeEvent
, this.onChromeEvent
.bind(this)));
41 Listener
.prototype.dispose = function() {
42 this.eventHooks_
.dispose();
45 function raiseAllEvents() {
47 myChromeEvent
.mock
$fire();
48 eventSource
.raiseEvent('customEvent');
51 QUnit
.module('base.EventHook', {
52 beforeEach: function() {
53 domElement
= /** @type {HTMLElement} */ (document
.createElement('div'));
54 eventSource
= new base
.EventSourceImpl();
55 eventSource
.defineEvents(['customEvent']);
56 myChromeEvent
= new chromeMocks
.Event();
57 listener
= new Listener(domElement
);
59 afterEach: function() {
67 QUnit
.test('EventHook should hook events when constructed', function() {
69 sinon
.assert
.calledOnce(listener
.onClickEvent
);
70 sinon
.assert
.calledOnce(listener
.onChromeEvent
);
71 sinon
.assert
.calledOnce(listener
.onCustomEvent
);
75 QUnit
.test('EventHook should unhook events when disposed', function() {
78 sinon
.assert
.notCalled(listener
.onClickEvent
);
79 sinon
.assert
.notCalled(listener
.onChromeEvent
);
80 sinon
.assert
.notCalled(listener
.onCustomEvent
);