1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(function() {
'use strict';
module('base');
test('mix(dest, src) should copy properties from |src| to |dest|',
function() {
var src = { a: 'a', b: 'b'};
var dest = { c: 'c'};
base.mix(dest, src);
deepEqual(dest, {a: 'a', b: 'b', c: 'c'});
});
test('mix(dest, src) should assert if properties are overwritten',
function() {
var src = { a: 'a', b: 'b'};
var dest = { a: 'a'};
sinon.spy(base.debug, 'assert');
try {
base.mix(dest, src);
} catch (e) {
} finally {
sinon.assert.called(base.debug.assert);
base.debug.assert.restore();
}
});
test('values(obj) should return an array containing the values of |obj|',
function() {
var output = base.values({ a: 'a', b: 'b'});
notEqual(output.indexOf('a'), -1, '"a" should be in the output');
notEqual(output.indexOf('b'), -1, '"b" should be in the output');
});
test('dispose(obj) should invoke the dispose method on |obj|',
function() {
var obj = {
dispose: sinon.spy()
};
base.dispose(obj);
sinon.assert.called(obj.dispose);
});
test('dispose(obj) should not crash if |obj| is null',
function() {
expect(0);
base.dispose(null);
});
test('urljoin(url, opt_param) should return url if |opt_param| is missing',
function() {
QUnit.equal(
base.urlJoin('http://www.chromium.org'), 'http://www.chromium.org');
});
test('urljoin(url, opt_param) should urlencode |opt_param|',
function() {
var result = base.urlJoin('http://www.chromium.org', {
a: 'a',
foo: 'foo',
escapist: ':/?#[]@$&+,;='
});
QUnit.equal(
result,
'http://www.chromium.org?a=a&foo=foo' +
'&escapist=%3A%2F%3F%23%5B%5D%40%24%26%2B%2C%3B%3D');
});
QUnit.asyncTest('Promise.sleep(delay) should fulfill the promise after |delay|',
function() {
var isCalled = false;
var clock = this.clock;
base.Promise.sleep(100).then(function(){
isCalled = true;
ok(true, 'Promise.sleep() is fulfilled after delay.');
QUnit.start();
});
// Tick the clock for 2 seconds and check if the promise is fulfilled.
clock.tick(2);
// Promise fulfillment always occur on a new stack. Therefore, we will run
// the verification in a requestAnimationFrame.
window.requestAnimationFrame(function(){
ok(!isCalled, 'Promise.sleep() should not be fulfilled prematurely.');
clock.tick(101);
}.bind(this));
});
QUnit.asyncTest('Promise.negate should fulfill iff the promise does not.',
function() {
base.Promise.negate(Promise.reject()).then(
ok.bind(null, true),
ok.bind(null, false));
base.Promise.negate(Promise.resolve()).then(
ok.bind(null, false),
ok.bind(null, true));
window.requestAnimationFrame(function(){
QUnit.start();
});
});
module('base.Deferred');
QUnit.asyncTest('resolve() should fulfill the underlying promise.', function() {
function async() {
var deferred = new base.Deferred();
deferred.resolve('bar');
return deferred.promise();
}
async().then(function(value){
QUnit.equal(value, 'bar');
QUnit.start();
}, function() {
QUnit.ok(false, 'The reject handler should not be invoked.');
});
});
QUnit.asyncTest('reject() should fail the underlying promise.', function() {
function async() {
var deferred = new base.Deferred();
deferred.reject('bar');
return deferred.promise();
}
async().then(function(){
QUnit.ok(false, 'The then handler should not be invoked.');
}, function(value) {
QUnit.equal(value, 'bar');
QUnit.start();
});
});
var source = null;
var listener = null;
module('base.EventSource', {
setup: function() {
source = new base.EventSource();
source.defineEvents(['foo', 'bar']);
listener = sinon.spy();
source.addEventListener('foo', listener);
},
teardown: function() {
source = null;
listener = null;
}
});
test('raiseEvent() should invoke the listener', function() {
source.raiseEvent('foo');
sinon.assert.called(listener);
});
test('raiseEvent() should invoke the listener with the correct event data',
function() {
var data = {
field: 'foo'
};
source.raiseEvent('foo', data);
sinon.assert.calledWith(listener, data);
});
test(
'raiseEvent() should not invoke listeners that are added during raiseEvent',
function() {
source.addEventListener('foo', function() {
source.addEventListener('foo', function() {
ok(false);
});
ok(true);
});
source.raiseEvent('foo');
});
test('raiseEvent() should not invoke listeners of a different event',
function() {
source.raiseEvent('bar');
sinon.assert.notCalled(listener);
});
test('raiseEvent() should assert when undeclared events are raised',
function() {
sinon.spy(base.debug, 'assert');
try {
source.raiseEvent('undefined');
} catch (e) {
} finally {
sinon.assert.called(base.debug.assert);
base.debug.assert.restore();
}
});
test(
'removeEventListener() should not invoke the listener in subsequent ' +
'calls to |raiseEvent|',
function() {
source.raiseEvent('foo');
sinon.assert.calledOnce(listener);
source.removeEventListener('foo', listener);
source.raiseEvent('foo');
sinon.assert.calledOnce(listener);
});
test('removeEventListener() should work even if the listener ' +
'is removed during |raiseEvent|',
function() {
var sink = {};
sink.listener = sinon.spy(function() {
source.removeEventListener('foo', sink.listener);
});
source.addEventListener('foo', sink.listener);
source.raiseEvent('foo');
sinon.assert.calledOnce(sink.listener);
source.raiseEvent('foo');
sinon.assert.calledOnce(sink.listener);
});
test('encodeUtf8() can encode UTF8 strings', function() {
function toJsArray(arrayBuffer) {
var result = [];
var array = new Uint8Array(arrayBuffer);
for (var i = 0; i < array.length; ++i) {
result.push(array[i]);
}
return result;
}
// ASCII.
QUnit.deepEqual(toJsArray(base.encodeUtf8("ABC")), [0x41, 0x42, 0x43]);
// Some arbitrary characters from the basic Unicode plane.
QUnit.deepEqual(
toJsArray(base.encodeUtf8("挂Ѓф")),
[/* 挂 */ 0xE6, 0x8C, 0x82, /* Ѓ */ 0xD0, 0x83, /* ф */ 0xD1, 0x84]);
// Unicode surrogate pair for U+1F603.
QUnit.deepEqual(toJsArray(base.encodeUtf8("😃")),
[0xF0, 0x9F, 0x98, 0x83]);
});
test('decodeUtf8() can decode UTF8 strings', function() {
// ASCII.
QUnit.equal(base.decodeUtf8(new Uint8Array([0x41, 0x42, 0x43]).buffer),
"ABC");
// Some arbitrary characters from the basic Unicode plane.
QUnit.equal(
base.decodeUtf8(
new Uint8Array([/* 挂 */ 0xE6, 0x8C, 0x82,
/* Ѓ */ 0xD0, 0x83,
/* ф */ 0xD1, 0x84]).buffer),
"挂Ѓф");
// Unicode surrogate pair for U+1F603.
QUnit.equal(base.decodeUtf8(new Uint8Array([0xF0, 0x9F, 0x98, 0x83]).buffer),
"😃");
});
})();
|