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 module('base'); |
| 10 |
| 11 test('mix(dest, src) should copy properties from |src| to |dest|', |
| 12 function() { |
| 13 var src = { a: 'a', b: 'b'}; |
| 14 var dest = { c: 'c'}; |
| 15 |
| 16 base.mix(dest, src); |
| 17 deepEqual(dest, {a: 'a', b: 'b', c: 'c'}); |
| 18 }); |
| 19 |
| 20 test('mix(dest, src) should assert if properties are overwritten', |
| 21 function() { |
| 22 var src = { a: 'a', b: 'b'}; |
| 23 var dest = { a: 'a'}; |
| 24 |
| 25 sinon.spy(base.debug, 'assert'); |
| 26 |
| 27 try { |
| 28 base.mix(dest, src); |
| 29 } catch (e) { |
| 30 } finally { |
| 31 sinon.assert.called(base.debug.assert); |
| 32 base.debug.assert.restore(); |
| 33 } |
| 34 }); |
| 35 |
| 36 test('values(obj) should return an array containing the values of |obj|', |
| 37 function() { |
| 38 var output = base.values({ a: 'a', b: 'b'}); |
| 39 |
| 40 notEqual(output.indexOf('a'), -1, '"a" should be in the output'); |
| 41 notEqual(output.indexOf('b'), -1, '"b" should be in the output'); |
| 42 }); |
| 43 |
| 44 test('dispose(obj) should invoke the dispose method on |obj|', |
| 45 function() { |
| 46 var obj = { |
| 47 dispose: sinon.spy() |
| 48 }; |
| 49 base.dispose(obj); |
| 50 sinon.assert.called(obj.dispose); |
| 51 }); |
| 52 |
| 53 test('dispose(obj) should not crash if |obj| is null', |
| 54 function() { |
| 55 expect(0); |
| 56 base.dispose(null); |
| 57 }); |
| 58 |
| 59 QUnit.asyncTest('Promise.sleep(delay) should fulfill the promise after |delay|', |
| 60 function() { |
| 61 var isCalled = false; |
| 62 var clock = this.clock; |
| 63 |
| 64 base.Promise.sleep(100).then(function(){ |
| 65 isCalled = true; |
| 66 ok(true, 'Promise.sleep() is fulfilled after delay.'); |
| 67 QUnit.start(); |
| 68 }); |
| 69 |
| 70 // Tick the clock for 2 seconds and check if the promise is fulfilled. |
| 71 clock.tick(2); |
| 72 |
| 73 // Promise fulfillment always occur on a new stack. Therefore, we will run |
| 74 // the verification in a requestAnimationFrame. |
| 75 window.requestAnimationFrame(function(){ |
| 76 ok(!isCalled, 'Promise.sleep() should not be fulfilled prematurely.'); |
| 77 clock.tick(101); |
| 78 }.bind(this)); |
| 79 }); |
| 80 |
| 81 |
| 82 var source = null; |
| 83 var listener = null; |
| 84 |
| 85 module('base.EventSource', { |
| 86 setup: function() { |
| 87 source = new base.EventSource(); |
| 88 source.defineEvents(['foo', 'bar']); |
| 89 listener = sinon.spy(); |
| 90 source.addEventListener('foo', listener); |
| 91 }, |
| 92 teardown: function() { |
| 93 source = null; |
| 94 listener = null; |
| 95 } |
| 96 }); |
| 97 |
| 98 test('raiseEvent() should invoke the listener', function() { |
| 99 source.raiseEvent('foo'); |
| 100 sinon.assert.called(listener); |
| 101 }); |
| 102 |
| 103 test('raiseEvent() should invoke the listener with the correct event data', |
| 104 function() { |
| 105 var data = { |
| 106 field: 'foo' |
| 107 }; |
| 108 source.raiseEvent('foo', data); |
| 109 sinon.assert.calledWith(listener, data); |
| 110 }); |
| 111 |
| 112 test( |
| 113 'raiseEvent() should not invoke listeners that are added during raiseEvent', |
| 114 function() { |
| 115 source.addEventListener('foo', function() { |
| 116 source.addEventListener('foo', function() { |
| 117 ok(false); |
| 118 }); |
| 119 ok(true); |
| 120 }); |
| 121 source.raiseEvent('foo'); |
| 122 }); |
| 123 |
| 124 test('raiseEvent() should not invoke listeners of a different event', |
| 125 function() { |
| 126 source.raiseEvent('bar'); |
| 127 sinon.assert.notCalled(listener); |
| 128 }); |
| 129 |
| 130 test('raiseEvent() should assert when undeclared events are raised', |
| 131 function() { |
| 132 sinon.spy(base.debug, 'assert'); |
| 133 try { |
| 134 source.raiseEvent('undefined'); |
| 135 } catch (e) { |
| 136 } finally { |
| 137 sinon.assert.called(base.debug.assert); |
| 138 base.debug.assert.restore(); |
| 139 } |
| 140 }); |
| 141 |
| 142 test( |
| 143 'removeEventListener() should not invoke the listener in subsequent ' + |
| 144 'calls to |raiseEvent|', |
| 145 function() { |
| 146 source.raiseEvent('foo'); |
| 147 sinon.assert.calledOnce(listener); |
| 148 |
| 149 source.removeEventListener('foo', listener); |
| 150 source.raiseEvent('foo'); |
| 151 sinon.assert.calledOnce(listener); |
| 152 }); |
| 153 |
| 154 test('removeEventListener() should work even if the listener ' + |
| 155 'is removed during |raiseEvent|', |
| 156 function() { |
| 157 var sink = {}; |
| 158 sink.listener = sinon.spy(function() { |
| 159 source.removeEventListener('foo', sink.listener); |
| 160 }); |
| 161 |
| 162 source.addEventListener('foo', sink.listener); |
| 163 source.raiseEvent('foo'); |
| 164 sinon.assert.calledOnce(sink.listener); |
| 165 |
| 166 source.raiseEvent('foo'); |
| 167 sinon.assert.calledOnce(sink.listener); |
| 168 }); |
| 169 |
| 170 })(); |
OLD | NEW |