OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function() { | 5 (function() { |
6 | 6 |
7 'use strict'; | 7 'use strict'; |
8 | 8 |
9 /** @type {base.EventSourceImpl} */ | |
9 var eventSource = null; | 10 var eventSource = null; |
11 | |
12 /** @type {HTMLElement} */ | |
10 var domElement = null; | 13 var domElement = null; |
14 | |
15 /** @type {chrome.Event} */ | |
11 var myChromeEvent = null; | 16 var myChromeEvent = null; |
17 | |
18 /** @type {Listener} */ | |
12 var listener = null; | 19 var listener = null; |
13 | 20 |
21 /** @constructor */ | |
14 var Listener = function(element) { | 22 var Listener = function(element) { |
15 this.onChromeEvent = sinon.stub(); | 23 /** @type {(sinon.$spy|function(...?))} */ |
16 this.onClickEvent = sinon.stub(); | 24 this.onChromeEvent = sinon.spy(); |
17 this.onCustomEvent = sinon.stub(); | 25 /** @type {(sinon.$spy|function(...?))} */ |
26 this.onClickEvent = sinon.spy(); | |
27 /** @type {(sinon.$spy|function(...?))} */ | |
28 this.onCustomEvent = sinon.spy(); | |
29 | |
18 this.eventHooks_ = new base.Disposables( | 30 this.eventHooks_ = new base.Disposables( |
19 new base.DomEventHook(element, 'click', this.onClickEvent.bind(this), | 31 new base.DomEventHook(domElement, 'click', this.onClickEvent.bind(this), |
kelvinp
2015/02/26 00:31:30
should be element as it is passed through the cons
garykac
2015/02/28 02:33:33
I didn't see the 'element' param and thought it wa
| |
20 false), | 32 false), |
21 new base.EventHook(eventSource, 'customEvent', | 33 new base.EventHook(eventSource, 'customEvent', |
22 this.onCustomEvent.bind(this)), | 34 this.onCustomEvent.bind(this)), |
23 new base.ChromeEventHook(myChromeEvent, this.onChromeEvent.bind(this))); | 35 new base.ChromeEventHook(myChromeEvent, this.onChromeEvent.bind(this))); |
24 }; | 36 }; |
25 | 37 |
26 Listener.prototype.dispose = function() { | 38 Listener.prototype.dispose = function() { |
27 this.eventHooks_.dispose(); | 39 this.eventHooks_.dispose(); |
28 }; | 40 }; |
29 | 41 |
30 function raiseAllEvents() { | 42 function raiseAllEvents() { |
31 domElement.click(); | 43 domElement.click(); |
32 myChromeEvent.mock$fire(); | 44 myChromeEvent.mock$fire(); |
33 eventSource.raiseEvent('customEvent'); | 45 eventSource.raiseEvent('customEvent'); |
34 } | 46 } |
35 | 47 |
36 module('base.EventHook', { | 48 module('base.EventHook', { |
37 setup: function() { | 49 setup: function() { |
38 domElement = document.createElement('div'); | 50 domElement = /** @type {HTMLElement} */ (document.createElement('div')); |
39 eventSource = new base.EventSourceImpl(); | 51 eventSource = new base.EventSourceImpl(); |
40 eventSource.defineEvents(['customEvent']); | 52 eventSource.defineEvents(['customEvent']); |
41 myChromeEvent = new chromeMocks.Event(); | 53 myChromeEvent = new chrome.Event(); |
42 listener = new Listener(domElement); | 54 listener = new Listener(domElement); |
43 }, | 55 }, |
44 tearDown: function() { | 56 tearDown: function() { |
45 domElement = null; | 57 domElement = null; |
46 eventSource = null; | 58 eventSource = null; |
47 myChromeEvent = null; | 59 myChromeEvent = null; |
48 listener = null; | 60 listener = null; |
49 } | 61 } |
50 }); | 62 }); |
51 | 63 |
52 test('EventHook should hook events when constructed', function() { | 64 test('EventHook should hook events when constructed', function() { |
53 raiseAllEvents(); | 65 raiseAllEvents(); |
54 sinon.assert.calledOnce(listener.onClickEvent); | 66 sinon.assert.calledOnce(listener.onClickEvent); |
55 sinon.assert.calledOnce(listener.onChromeEvent); | 67 sinon.assert.calledOnce(listener.onChromeEvent); |
56 sinon.assert.calledOnce(listener.onCustomEvent); | 68 sinon.assert.calledOnce(listener.onCustomEvent); |
57 listener.dispose(); | 69 listener.dispose(); |
58 }); | 70 }); |
59 | 71 |
60 test('EventHook should unhook events when disposed', function() { | 72 test('EventHook should unhook events when disposed', function() { |
61 listener.dispose(); | 73 listener.dispose(); |
62 raiseAllEvents(); | 74 raiseAllEvents(); |
63 sinon.assert.notCalled(listener.onClickEvent); | 75 sinon.assert.notCalled(listener.onClickEvent); |
64 sinon.assert.notCalled(listener.onChromeEvent); | 76 sinon.assert.notCalled(listener.onChromeEvent); |
65 sinon.assert.notCalled(listener.onCustomEvent); | 77 sinon.assert.notCalled(listener.onCustomEvent); |
66 }); | 78 }); |
67 | 79 |
68 })(); | 80 })(); |
OLD | NEW |