| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 (function() { | |
| 6 | |
| 7 'use strict'; | |
| 8 | |
| 9 var source = null; | |
| 10 var listener = null; | |
| 11 | |
| 12 module('base.EventSource.raiseEvent', { | |
| 13 setup: function() { | |
| 14 source = new base.EventSource(); | |
| 15 source.defineEvents(['foo', 'bar']); | |
| 16 listener = sinon.spy(); | |
| 17 source.addEventListener('foo', listener); | |
| 18 }, | |
| 19 teardown: function() { | |
| 20 source = null; | |
| 21 listener = null; | |
| 22 } | |
| 23 }); | |
| 24 | |
| 25 test('should invoke the listener', function() { | |
| 26 source.raiseEvent('foo'); | |
| 27 sinon.assert.called(listener); | |
| 28 }); | |
| 29 | |
| 30 test('should invoke the listener with the correct event data', function() { | |
| 31 var data = { | |
| 32 field: 'foo' | |
| 33 }; | |
| 34 source.raiseEvent('foo', data); | |
| 35 sinon.assert.calledWith(listener, data); | |
| 36 }); | |
| 37 | |
| 38 test('should not invoke listeners that are added during raiseEvent', | |
| 39 function() { | |
| 40 source.addEventListener('foo', function() { | |
| 41 source.addEventListener('foo', function() { | |
| 42 ok(false); | |
| 43 }); | |
| 44 ok(true); | |
| 45 }); | |
| 46 source.raiseEvent('foo'); | |
| 47 }); | |
| 48 | |
| 49 test('should not invoke listeners of a different event', | |
| 50 function() { | |
| 51 source.raiseEvent('bar'); | |
| 52 sinon.assert.notCalled(listener); | |
| 53 }); | |
| 54 | |
| 55 test('should assert when undeclared events are raised', function() { | |
| 56 sinon.spy(base.debug, 'assert'); | |
| 57 try { | |
| 58 source.raiseEvent('undefined'); | |
| 59 } catch (e){ | |
| 60 } finally{ | |
| 61 sinon.assert.called(base.debug.assert); | |
| 62 base.debug.assert.restore(); | |
| 63 } | |
| 64 }); | |
| 65 | |
| 66 module('base.EventSource.removeEventListener', { | |
| 67 setup: function() { | |
| 68 source = new base.EventSource(); | |
| 69 source.defineEvents(['foo', 'bar']); | |
| 70 }, | |
| 71 teardown: function() { | |
| 72 source = null; | |
| 73 listener = null; | |
| 74 } | |
| 75 }); | |
| 76 | |
| 77 test('should not invoke the listener in subsequent calls to |raiseEvent|', | |
| 78 function() { | |
| 79 listener = sinon.spy(); | |
| 80 source.addEventListener('foo', listener); | |
| 81 | |
| 82 source.raiseEvent('foo'); | |
| 83 sinon.assert.calledOnce(listener); | |
| 84 | |
| 85 source.removeEventListener('foo', listener); | |
| 86 source.raiseEvent('foo'); | |
| 87 sinon.assert.calledOnce(listener); | |
| 88 }); | |
| 89 | |
| 90 test('should work even if the listener is removed during |raiseEvent|', | |
| 91 function() { | |
| 92 var sink = {}; | |
| 93 sink.listener = sinon.spy(function() { | |
| 94 source.removeEventListener('foo', sink.listener); | |
| 95 }); | |
| 96 | |
| 97 source.addEventListener('foo', sink.listener); | |
| 98 source.raiseEvent('foo'); | |
| 99 sinon.assert.calledOnce(sink.listener); | |
| 100 | |
| 101 source.raiseEvent('foo'); | |
| 102 sinon.assert.calledOnce(sink.listener); | |
| 103 }); | |
| 104 | |
| 105 })(); | |
| OLD | NEW |