Commit 09b6ce89 authored by kelvinp@chromium.org's avatar kelvinp@chromium.org

Fix base_unittest.js

This CL addresses some feedback from Jamie regarding unit test naming conventions and organizations.
1. Renames test_eventSource.js to base_unittest.js.
2. Groups the modules by class instead of by function.
3. Adds extra test cases for base.js

Review URL: https://codereview.chromium.org/403663002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284252 0039d316-1c4b-4281-b951-d872f2087c98
parent e0ec8ba2
......@@ -135,7 +135,7 @@
],
# The unit test cases for the webapp
'remoting_webapp_unittest_cases': [
'webapp/unittests/test_eventSource.js',
'webapp/unittests/base_unittest.js',
],
'remoting_webapp_unittest_template_main':
'webapp/html/template_unittest.html',
......
......@@ -6,10 +6,83 @@
'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);
});
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));
});
var source = null;
var listener = null;
module('base.EventSource.raiseEvent', {
module('base.EventSource', {
setup: function() {
source = new base.EventSource();
source.defineEvents(['foo', 'bar']);
......@@ -22,20 +95,22 @@ module('base.EventSource.raiseEvent', {
}
});
test('should invoke the listener', function() {
test('raiseEvent() should invoke the listener', function() {
source.raiseEvent('foo');
sinon.assert.called(listener);
});
test('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 invoke the listener with the correct event data',
function() {
var data = {
field: 'foo'
};
source.raiseEvent('foo', data);
sinon.assert.calledWith(listener, data);
});
test('should not invoke listeners that are added during raiseEvent',
test(
'raiseEvent() should not invoke listeners that are added during raiseEvent',
function() {
source.addEventListener('foo', function() {
source.addEventListener('foo', function() {
......@@ -46,39 +121,28 @@ test('should not invoke listeners that are added during raiseEvent',
source.raiseEvent('foo');
});
test('should not invoke listeners of a different event',
test('raiseEvent() should not invoke listeners of a different event',
function() {
source.raiseEvent('bar');
sinon.assert.notCalled(listener);
});
test('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();
}
});
module('base.EventSource.removeEventListener', {
setup: function() {
source = new base.EventSource();
source.defineEvents(['foo', 'bar']);
},
teardown: function() {
source = null;
listener = null;
}
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('should not invoke the listener in subsequent calls to |raiseEvent|',
test(
'removeEventListener() should not invoke the listener in subsequent ' +
'calls to |raiseEvent|',
function() {
listener = sinon.spy();
source.addEventListener('foo', listener);
source.raiseEvent('foo');
sinon.assert.calledOnce(listener);
......@@ -87,7 +151,8 @@ test('should not invoke the listener in subsequent calls to |raiseEvent|',
sinon.assert.calledOnce(listener);
});
test('should work even if the listener is removed during |raiseEvent|',
test('removeEventListener() should work even if the listener ' +
'is removed during |raiseEvent|',
function() {
var sink = {};
sink.listener = sinon.spy(function() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment