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('dispose(obj) should invoke the dispose method on |obj|',
50 sinon
.assert
.called(obj
.dispose
);
53 test('dispose(obj) should not crash if |obj| is null',
59 test('urljoin(url, opt_param) should return url if |opt_param| is missing',
62 base
.urlJoin('http://www.chromium.org'), 'http://www.chromium.org');
65 test('urljoin(url, opt_param) should urlencode |opt_param|',
67 var result
= base
.urlJoin('http://www.chromium.org', {
70 escapist
: ':/?#[]@$&+,;='
74 'http://www.chromium.org?a=a&foo=foo' +
75 '&escapist=%3A%2F%3F%23%5B%5D%40%24%26%2B%2C%3B%3D');
78 test('escapeHTML(str) should escape special characters', function() {
80 base
.escapeHTML('<script>alert("hello")</script>'),
81 '<script>alert("hello")</script>');
84 QUnit
.asyncTest('Promise.sleep(delay) should fulfill the promise after |delay|',
87 var clock
= this.clock
;
89 base
.Promise
.sleep(100).then(function(){
91 ok(true, 'Promise.sleep() is fulfilled after delay.');
95 // Tick the clock for 2 seconds and check if the promise is fulfilled.
98 // Promise fulfillment always occur on a new stack. Therefore, we will run
99 // the verification in a requestAnimationFrame.
100 window
.requestAnimationFrame(function(){
101 ok(!isCalled
, 'Promise.sleep() should not be fulfilled prematurely.');
106 QUnit
.asyncTest('Promise.negate should fulfill iff the promise does not.',
109 base
.Promise
.negate(Promise
.reject()).then(
111 ok
.bind(null, false));
112 base
.Promise
.negate(Promise
.resolve()).then(
113 ok
.bind(null, false),
114 ok
.bind(null, true));
115 window
.requestAnimationFrame(function(){
120 module('base.Deferred');
122 QUnit
.asyncTest('resolve() should fulfill the underlying promise.', function() {
124 var deferred
= new base
.Deferred();
125 deferred
.resolve('bar');
126 return deferred
.promise();
129 async().then(function(value
){
130 QUnit
.equal(value
, 'bar');
133 QUnit
.ok(false, 'The reject handler should not be invoked.');
137 QUnit
.asyncTest('reject() should fail the underlying promise.', function() {
139 var deferred
= new base
.Deferred();
140 deferred
.reject('bar');
141 return deferred
.promise();
144 async().then(function(){
145 QUnit
.ok(false, 'The then handler should not be invoked.');
147 QUnit
.equal(value
, 'bar');
156 module('base.EventSource', {
158 source
= new base
.EventSource();
159 source
.defineEvents(['foo', 'bar']);
160 listener
= sinon
.spy();
161 source
.addEventListener('foo', listener
);
163 teardown: function() {
169 test('raiseEvent() should invoke the listener', function() {
170 source
.raiseEvent('foo');
171 sinon
.assert
.called(listener
);
174 test('raiseEvent() should invoke the listener with the correct event data',
179 source
.raiseEvent('foo', data
);
180 sinon
.assert
.calledWith(listener
, data
);
184 'raiseEvent() should not invoke listeners that are added during raiseEvent',
186 source
.addEventListener('foo', function() {
187 source
.addEventListener('foo', function() {
192 source
.raiseEvent('foo');
195 test('raiseEvent() should not invoke listeners of a different event',
197 source
.raiseEvent('bar');
198 sinon
.assert
.notCalled(listener
);
201 test('raiseEvent() should assert when undeclared events are raised',
203 sinon
.spy(base
.debug
, 'assert');
205 source
.raiseEvent('undefined');
208 sinon
.assert
.called(base
.debug
.assert
);
209 base
.debug
.assert
.restore();
214 'removeEventListener() should not invoke the listener in subsequent ' +
215 'calls to |raiseEvent|',
217 source
.raiseEvent('foo');
218 sinon
.assert
.calledOnce(listener
);
220 source
.removeEventListener('foo', listener
);
221 source
.raiseEvent('foo');
222 sinon
.assert
.calledOnce(listener
);
225 test('removeEventListener() should work even if the listener ' +
226 'is removed during |raiseEvent|',
229 sink
.listener
= sinon
.spy(function() {
230 source
.removeEventListener('foo', sink
.listener
);
233 source
.addEventListener('foo', sink
.listener
);
234 source
.raiseEvent('foo');
235 sinon
.assert
.calledOnce(sink
.listener
);
237 source
.raiseEvent('foo');
238 sinon
.assert
.calledOnce(sink
.listener
);
241 test('encodeUtf8() can encode UTF8 strings', function() {
242 function toJsArray(arrayBuffer
) {
244 var array
= new Uint8Array(arrayBuffer
);
245 for (var i
= 0; i
< array
.length
; ++i
) {
246 result
.push(array
[i
]);
252 QUnit
.deepEqual(toJsArray(base
.encodeUtf8("ABC")), [0x41, 0x42, 0x43]);
254 // Some arbitrary characters from the basic Unicode plane.
256 toJsArray(base
.encodeUtf8("挂Ѓф")),
257 [/* 挂 */ 0xE6, 0x8C, 0x82, /* Ѓ */ 0xD0, 0x83, /* ф */ 0xD1, 0x84]);
259 // Unicode surrogate pair for U+1F603.
260 QUnit
.deepEqual(toJsArray(base
.encodeUtf8("😃")),
261 [0xF0, 0x9F, 0x98, 0x83]);
264 test('decodeUtf8() can decode UTF8 strings', function() {
266 QUnit
.equal(base
.decodeUtf8(new Uint8Array([0x41, 0x42, 0x43]).buffer
),
269 // Some arbitrary characters from the basic Unicode plane.
272 new Uint8Array([/* 挂 */ 0xE6, 0x8C, 0x82,
274 /* ф */ 0xD1, 0x84]).buffer
),
277 // Unicode surrogate pair for U+1F603.
278 QUnit
.equal(base
.decodeUtf8(new Uint8Array([0xF0, 0x9F, 0x98, 0x83]).buffer
),