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 var getRandomValuesStub
= null;
11 QUnit
.module('base', {
12 afterEach: function() {
13 if (getRandomValuesStub
) {
14 getRandomValuesStub
.restore();
15 getRandomValuesStub
= null;
20 QUnit
.test('mix(dest, src) should copy properties from |src| to |dest|',
22 var src
= { a
: 'a', b
: 'b'};
26 assert
.deepEqual(dest
, {a
: 'a', b
: 'b', c
: 'c'});
29 QUnit
.test('mix(dest, src) should not override property.', function(assert
) {
30 var src
= { a
: 'a', b
: 'b'};
31 var dest
= { a
: 'a2'};
33 assert
.equal(dest
['a'], 'a2');
34 assert
.equal(dest
['b'], 'b');
37 QUnit
.test('values(obj) should return an array containing the values of |obj|',
39 var output
= base
.values({ a
: 'a', b
: 'b'});
41 assert
.notEqual(output
.indexOf('a'), -1, '"a" should be in the output');
42 assert
.notEqual(output
.indexOf('b'), -1, '"b" should be in the output');
45 QUnit
.test('deepCopy(obj) should return null on NaN and undefined',
47 assert
.equal(base
.deepCopy(NaN
), null);
48 assert
.equal(base
.deepCopy(undefined), null);
51 QUnit
.test('deepCopy(obj) should copy primitive types recursively',
53 assert
.equal(base
.deepCopy(1), 1);
54 assert
.equal(base
.deepCopy('hello'), 'hello');
55 assert
.equal(base
.deepCopy(false), false);
56 assert
.equal(base
.deepCopy(null), null);
57 assert
.deepEqual(base
.deepCopy([1, 2]), [1, 2]);
58 assert
.deepEqual(base
.deepCopy({'key': 'value'}), {'key': 'value'});
59 assert
.deepEqual(base
.deepCopy(
60 {'key': {'key_nested': 'value_nested'}}),
61 {'key': {'key_nested': 'value_nested'}}
63 assert
.deepEqual(base
.deepCopy([1, [2, [3]]]), [1, [2, [3]]]);
66 QUnit
.test('copyWithoutNullFields returns a new object',
72 var copy
= base
.copyWithoutNullFields(obj
);
73 assert
.notEqual(obj
, copy
);
74 assert
.deepEqual(obj
, copy
);
77 QUnit
.test('copyWithoutNullFields removes null and undefined fields',
86 undefinedField
: undefined
88 var copy
= base
.copyWithoutNullFields(obj
);
89 assert
.equal(copy
['a'], obj
['a']);
90 assert
.equal(copy
['b'], obj
['b']);
91 assert
.equal(copy
['zero'], 0);
92 assert
.equal(copy
['emptyString'], '');
93 assert
.ok(!('nullField' in copy
));
94 assert
.ok(!('undefinedField' in copy
));
97 QUnit
.test('copyWithoutNullFields(null) returns a new empty bject',
100 base
.copyWithoutNullFields(null),
103 base
.copyWithoutNullFields(null),
104 base
.copyWithoutNullFields(null));
106 base
.copyWithoutNullFields(undefined),
109 base
.copyWithoutNullFields(undefined),
110 base
.copyWithoutNullFields(undefined));
113 QUnit
.test('modify the original after deepCopy(obj) should not affect the copy',
115 var original
= [1, 2, 3, 4];
116 var copy
= base
.deepCopy(original
);
118 assert
.deepEqual(copy
, [1, 2, 3, 4]);
121 QUnit
.test('dispose(obj) should invoke the dispose method on |obj|',
125 * @implements {base.Disposable}
127 base
.MockDisposable = function() {};
128 base
.MockDisposable
.prototype.dispose
= sinon
.spy();
130 var obj
= new base
.MockDisposable();
132 sinon
.assert
.called(obj
.dispose
);
135 QUnit
.test('dispose(obj) should not crash if |obj| is null',
142 'urljoin(url, opt_param) should return url if |opt_param| is missing',
145 base
.urlJoin('http://www.chromium.org'), 'http://www.chromium.org');
148 QUnit
.test('urljoin(url, opt_param) should urlencode |opt_param|',
150 var result
= base
.urlJoin('http://www.chromium.org', {
153 escapist
: ':/?#[]@$&+,;='
157 'http://www.chromium.org?a=a&foo=foo' +
158 '&escapist=%3A%2F%3F%23%5B%5D%40%24%26%2B%2C%3B%3D');
161 QUnit
.test('escapeHTML(str) should escape special characters', function(assert
){
163 base
.escapeHTML('<script>alert("hello")</script>'),
164 '<script>alert("hello")</script>');
167 QUnit
.test('Promise.sleep(delay) should fulfill the promise after |delay|',
169 * 'this' is not defined for jscompile, so it can't figure out the type of
171 * @suppress {reportUnknownTypes|checkVars|checkTypes}
174 var isCalled
= false;
175 var clock
= /** @type {QUnit.Clock} */ (this.clock
);
177 var promise
= base
.Promise
.sleep(100).then(function(){
179 assert
.ok(true, 'Promise.sleep() is fulfilled after delay.');
182 // Tick the clock for 2 seconds and check if the promise is fulfilled.
185 // Promise fulfillment always occur on a new stack. Therefore, we will run
186 // the verification in a requestAnimationFrame.
187 window
.requestAnimationFrame(function(){
189 !isCalled
, 'Promise.sleep() should not be fulfilled prematurely.');
196 QUnit
.test('Promise.negate should fulfill iff the promise does not.',
198 return base
.Promise
.negate(Promise
.reject())
201 }).catch(function() {
204 return base
.Promise
.negate(Promise
.resolve());
207 }).catch(function() {
212 QUnit
.test('generateUuid generates a UUID', function(assert
) {
213 getRandomValuesStub
= sinon
.stub(
214 window
.crypto
, 'getRandomValues', function(/** Uint16Array*/ out
) {
215 for (var i
= 0; i
< out
.length
; i
++) {
219 assert
.equal(base
.generateUuid(), '00000001-0002-0003-0004-000500060007');
222 QUnit
.module('base.Deferred');
224 QUnit
.test('resolve() should fulfill the underlying promise.', function(assert
){
225 /** @returns {Promise} */
227 var deferred
= new base
.Deferred();
228 deferred
.resolve('bar');
229 return deferred
.promise();
232 return async().then(function(/** string */ value
){
233 assert
.equal(value
, 'bar');
235 assert
.ok(false, 'The reject handler should not be invoked.');
239 QUnit
.test('reject() should fail the underlying promise.', function(assert
) {
240 /** @returns {Promise} */
242 var deferred
= new base
.Deferred();
243 deferred
.reject('bar');
244 return deferred
.promise();
247 return async().then(function(){
248 assert
.ok(false, 'The then handler should not be invoked.');
250 assert
.equal(value
, 'bar');
255 /** @type {base.EventSourceImpl} */
259 QUnit
.module('base.EventSource', {
260 beforeEach: function() {
261 source
= new base
.EventSourceImpl();
262 source
.defineEvents(['foo', 'bar']);
263 listener
= sinon
.spy();
264 source
.addEventListener('foo', listener
);
266 afterEach: function() {
272 QUnit
.test('raiseEvent() should invoke the listener', function() {
273 source
.raiseEvent('foo');
274 sinon
.assert
.called(listener
);
278 'raiseEvent() should invoke the listener with the correct event data',
283 source
.raiseEvent('foo', data
);
284 sinon
.assert
.calledWith(listener
, data
);
288 'raiseEvent() should not invoke listeners that are added during raiseEvent',
290 source
.addEventListener('foo', function() {
291 source
.addEventListener('foo', function() {
296 source
.raiseEvent('foo');
299 QUnit
.test('raiseEvent() should not invoke listeners of a different event',
301 source
.raiseEvent('bar');
302 sinon
.assert
.notCalled(listener
);
305 QUnit
.test('raiseEvent() should assert when undeclared events are raised',
307 sinon
.stub(base
.debug
, 'assert');
309 source
.raiseEvent('undefined');
312 sinon
.assert
.called(base
.debug
.assert
);
313 $testStub(base
.debug
.assert
).restore();
318 'removeEventListener() should not invoke the listener in subsequent ' +
319 'calls to |raiseEvent|',
321 source
.raiseEvent('foo');
322 sinon
.assert
.calledOnce(listener
);
324 source
.removeEventListener('foo', listener
);
325 source
.raiseEvent('foo');
326 sinon
.assert
.calledOnce(listener
);
329 QUnit
.test('removeEventListener() should work even if the listener ' +
330 'is removed during |raiseEvent|',
333 sink
.listener
= sinon
.spy(function() {
334 source
.removeEventListener('foo', sink
.listener
);
337 source
.addEventListener('foo', sink
.listener
);
338 source
.raiseEvent('foo');
339 sinon
.assert
.calledOnce(sink
.listener
);
341 source
.raiseEvent('foo');
342 sinon
.assert
.calledOnce(sink
.listener
);
345 QUnit
.test('encodeUtf8() can encode UTF8 strings', function(assert
) {
346 /** @type {function(ArrayBuffer):Array} */
347 function toJsArray(arrayBuffer
) {
349 var array
= new Uint8Array(arrayBuffer
);
350 for (var i
= 0; i
< array
.length
; ++i
) {
351 result
.push(array
[i
]);
357 assert
.deepEqual(toJsArray(base
.encodeUtf8("ABC")), [0x41, 0x42, 0x43]);
359 // Some arbitrary characters from the basic Unicode plane.
361 toJsArray(base
.encodeUtf8("挂Ѓф")),
362 [/* 挂 */ 0xE6, 0x8C, 0x82, /* Ѓ */ 0xD0, 0x83, /* ф */ 0xD1, 0x84]);
364 // Unicode surrogate pair for U+1F603.
365 assert
.deepEqual(toJsArray(base
.encodeUtf8("😃")),
366 [0xF0, 0x9F, 0x98, 0x83]);
369 QUnit
.test('decodeUtf8() can decode UTF8 strings', function(assert
) {
371 assert
.equal(base
.decodeUtf8(new Uint8Array([0x41, 0x42, 0x43]).buffer
),
374 // Some arbitrary characters from the basic Unicode plane.
377 new Uint8Array([/* 挂 */ 0xE6, 0x8C, 0x82,
379 /* ф */ 0xD1, 0x84]).buffer
),
382 // Unicode surrogate pair for U+1F603.
383 assert
.equal(base
.decodeUtf8(new Uint8Array([0xF0, 0x9F, 0x98, 0x83]).buffer
),