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.
11 test('mix(dest, src) should copy properties from |src| to |dest|',
13 var src = { a: 'a', b: 'b'};
17 deepEqual(dest, {a: 'a', b: 'b', c: 'c'});
20 test('mix(dest, src) should assert if properties are overwritten',
22 var src = { a: 'a', b: 'b'};
25 sinon.spy(base.debug, 'assert');
31 sinon.assert.called(base.debug.assert);
32 base.debug.assert.restore();
36 test('values(obj) should return an array containing the values of |obj|',
38 var output = base.values({ a: 'a', b: 'b'});
40 notEqual(output.indexOf('a'), -1, '"a" should be in the output');
41 notEqual(output.indexOf('b'), -1, '"b" should be in the output');
44 test('deepCopy(obj) should return null on NaN and undefined',
46 QUnit.equal(base.deepCopy(NaN), null);
47 QUnit.equal(base.deepCopy(undefined), null);
50 test('deepCopy(obj) should copy primitive types recursively',
52 QUnit.equal(base.deepCopy(1), 1);
53 QUnit.equal(base.deepCopy('hello'), 'hello');
54 QUnit.equal(base.deepCopy(false), false);
55 QUnit.equal(base.deepCopy(null), null);
56 QUnit.deepEqual(base.deepCopy([1, 2]), [1, 2]);
57 QUnit.deepEqual(base.deepCopy({'key': 'value'}), {'key': 'value'});
58 QUnit.deepEqual(base.deepCopy(
59 {'key': {'key_nested': 'value_nested'}}),
60 {'key': {'key_nested': 'value_nested'}}
62 QUnit.deepEqual(base.deepCopy([1, [2, [3]]]), [1, [2, [3]]]);
65 test('modify the original after deepCopy(obj) should not affect the copy',
67 var original = [1, 2, 3, 4];
68 var copy = base.deepCopy(original);
70 QUnit.deepEqual(copy, [1, 2, 3, 4]);
73 test('dispose(obj) should invoke the dispose method on |obj|',
79 sinon.assert.called(obj.dispose);
82 test('dispose(obj) should not crash if |obj| is null',
88 test('urljoin(url, opt_param) should return url if |opt_param| is missing',
91 base.urlJoin('http://www.chromium.org'), 'http://www.chromium.org');
94 test('urljoin(url, opt_param) should urlencode |opt_param|',
96 var result = base.urlJoin('http://www.chromium.org', {
99 escapist: ':/?#[]@$&+,;='
103 'http://www.chromium.org?a=a&foo=foo' +
104 '&escapist=%3A%2F%3F%23%5B%5D%40%24%26%2B%2C%3B%3D');
107 test('escapeHTML(str) should escape special characters', function() {
109 base.escapeHTML('<script>alert("hello")</script>'),
110 '<script>alert("hello")</script>');
113 QUnit.asyncTest('Promise.sleep(delay) should fulfill the promise after |delay|',
115 var isCalled = false;
116 var clock = this.clock;
118 base.Promise.sleep(100).then(function(){
120 ok(true, 'Promise.sleep() is fulfilled after delay.');
124 // Tick the clock for 2 seconds and check if the promise is fulfilled.
127 // Promise fulfillment always occur on a new stack. Therefore, we will run
128 // the verification in a requestAnimationFrame.
129 window.requestAnimationFrame(function(){
130 ok(!isCalled, 'Promise.sleep() should not be fulfilled prematurely.');
135 QUnit.asyncTest('Promise.negate should fulfill iff the promise does not.',
138 base.Promise.negate(Promise.reject()).then(
140 ok.bind(null, false));
141 base.Promise.negate(Promise.resolve()).then(
142 ok.bind(null, false),
143 ok.bind(null, true));
144 window.requestAnimationFrame(function(){
149 module('base.Deferred');
151 QUnit.asyncTest('resolve() should fulfill the underlying promise.', function() {
153 var deferred = new base.Deferred();
154 deferred.resolve('bar');
155 return deferred.promise();
158 async().then(function(value){
159 QUnit.equal(value, 'bar');
162 QUnit.ok(false, 'The reject handler should not be invoked.');
166 QUnit.asyncTest('reject() should fail the underlying promise.', function() {
168 var deferred = new base.Deferred();
169 deferred.reject('bar');
170 return deferred.promise();
173 async().then(function(){
174 QUnit.ok(false, 'The then handler should not be invoked.');
176 QUnit.equal(value, 'bar');
185 module('base.EventSource', {
187 source = new base.EventSourceImpl();
188 source.defineEvents(['foo', 'bar']);
189 listener = sinon.spy();
190 source.addEventListener('foo', listener);
192 teardown: function() {
198 test('raiseEvent() should invoke the listener', function() {
199 source.raiseEvent('foo');
200 sinon.assert.called(listener);
203 test('raiseEvent() should invoke the listener with the correct event data',
208 source.raiseEvent('foo', data);
209 sinon.assert.calledWith(listener, data);
213 'raiseEvent() should not invoke listeners that are added during raiseEvent',
215 source.addEventListener('foo', function() {
216 source.addEventListener('foo', function() {
221 source.raiseEvent('foo');
224 test('raiseEvent() should not invoke listeners of a different event',
226 source.raiseEvent('bar');
227 sinon.assert.notCalled(listener);
230 test('raiseEvent() should assert when undeclared events are raised',
232 sinon.spy(base.debug, 'assert');
234 source.raiseEvent('undefined');
237 sinon.assert.called(base.debug.assert);
238 base.debug.assert.restore();
243 'removeEventListener() should not invoke the listener in subsequent ' +
244 'calls to |raiseEvent|',
246 source.raiseEvent('foo');
247 sinon.assert.calledOnce(listener);
249 source.removeEventListener('foo', listener);
250 source.raiseEvent('foo');
251 sinon.assert.calledOnce(listener);
254 test('removeEventListener() should work even if the listener ' +
255 'is removed during |raiseEvent|',
258 sink.listener = sinon.spy(function() {
259 source.removeEventListener('foo', sink.listener);
262 source.addEventListener('foo', sink.listener);
263 source.raiseEvent('foo');
264 sinon.assert.calledOnce(sink.listener);
266 source.raiseEvent('foo');
267 sinon.assert.calledOnce(sink.listener);
270 test('encodeUtf8() can encode UTF8 strings', function() {
271 function toJsArray(arrayBuffer) {
273 var array = new Uint8Array(arrayBuffer);
274 for (var i = 0; i < array.length; ++i) {
275 result.push(array[i]);
281 QUnit.deepEqual(toJsArray(base.encodeUtf8("ABC")), [0x41, 0x42, 0x43]);
283 // Some arbitrary characters from the basic Unicode plane.
285 toJsArray(base.encodeUtf8("挂Ѓф")),
286 [/* 挂 */ 0xE6, 0x8C, 0x82, /* Ѓ */ 0xD0, 0x83, /* ф */ 0xD1, 0x84]);
288 // Unicode surrogate pair for U+1F603.
289 QUnit.deepEqual(toJsArray(base.encodeUtf8("😃")),
290 [0xF0, 0x9F, 0x98, 0x83]);
293 test('decodeUtf8() can decode UTF8 strings', function() {
295 QUnit.equal(base.decodeUtf8(new Uint8Array([0x41, 0x42, 0x43]).buffer),
298 // Some arbitrary characters from the basic Unicode plane.
301 new Uint8Array([/* 挂 */ 0xE6, 0x8C, 0x82,
303 /* ф */ 0xD1, 0x84]).buffer),
306 // Unicode surrogate pair for U+1F603.
307 QUnit.equal(base.decodeUtf8(new Uint8Array([0xF0, 0x9F, 0x98, 0x83]).buffer),