core tests

This commit is contained in:
Yotam Mann 2015-08-16 22:21:14 -04:00
parent 9e7066f4ce
commit 2da9badb96
9 changed files with 392 additions and 119 deletions

113
test/core/Buffer.js Normal file
View file

@ -0,0 +1,113 @@
define(["Test", "Tone/core/Buffer"], function (Test, Buffer) {
describe("Buffer", function(){
it ("can be created and disposed", function(){
var buff = new Buffer("./audio/kick.mp3");
buff.dispose();
Test.wasDisposed(buff);
});
it("loads a file from a url string", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(buff){
expect(buff).to.be.instanceof(Buffer);
buffer.dispose();
done();
});
});
it("has a duration", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(){
expect(buffer.duration).to.be.closeTo(0.23, 0.01);
buffer.dispose();
done();
});
});
it("the static onload method is invoked", function(done){
var buffer = new Buffer("./audio/hh.mp3");
Buffer.onload = function(){
buffer.dispose();
done();
//reset this method for the next one
Buffer.onload = function(){};
};
});
it("can be constructed with an options object", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"reverse" : true,
"onload" : function(){
buffer.dispose();
done();
}
});
});
it("takes an AudioBuffer in the constructor method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("takes Tone.Buffer in the set method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer("./audio/hh.mp3");
testOne.set(buffer);
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("takes AudioBuffer in the set method", function(done){
var buffer = new Buffer({
"url" : "./audio/hh.mp3",
"onload" : function(){
var testOne = new Buffer("./audio/hh.mp3");
testOne.set(buffer.get());
expect(testOne.get()).to.equal(buffer.get());
testOne.dispose();
buffer.dispose();
done();
}
});
});
it("the static onprogress method is invoked", function(done){
var progressWasInvoked = false;
var buffer = new Buffer("./audio/hh.mp3", function(){
buffer.dispose();
expect(progressWasInvoked).to.be.true;
done();
});
Buffer.onprogress = function(){
progressWasInvoked = true;
//reset this method for the next one
Buffer.onprogress = function(){};
};
});
it("can reverse a buffer", function(done){
var buffer = new Buffer("./audio/kick.mp3", function(){
var buffArray = buffer.get();
var lastSample = buffArray[buffArray.length - 1];
buffer.reverse = true;
expect(buffer.get()[0]).to.equal(lastSample);
buffer.dispose();
done();
});
});
});
});

111
test/core/Schedulable.js Normal file
View file

@ -0,0 +1,111 @@
define(["Test", "Tone/core/Schedulable"], function (Test, Schedulable) {
describe("Schedulable", function(){
it ("can be created and disposed", function(){
var sched = new Schedulable();
sched.dispose();
Test.wasDisposed(sched);
});
it ("has a private timeline array", function(){
var sched = new Schedulable();
expect(sched).has.property("_timeline").that.is.an("array")
});
it ("accepts events into the timeline", function(){
var sched = new Schedulable();
sched._insertEvent({
"state" : "A",
"time" : 0
});
sched._insertEvent({
"state" : "B",
"time" : 1
});
sched._insertEvent({
"state" : "C",
"time" : 2
});
sched.dispose();
});
it ("can insert events in the timeline in the right order", function(){
var sched = new Schedulable();
sched._insertEvent({
"time" : 0
});
sched._insertEvent({
"time" : 2
});
sched._insertEvent({
"time" : 1
});
expect(sched._timeline[0].time).to.equal(0);
expect(sched._timeline[1].time).to.equal(1);
expect(sched._timeline[2].time).to.equal(2);
sched.dispose();
});
it ("can search for events in the timeline by time", function(){
var sched = new Schedulable();
sched._insertEvent({
"time" : 0
});
sched._insertEvent({
"time" : 2
});
sched._insertEvent({
"time" : 1
});
expect(sched._search(0)).to.equal(0);
expect(sched._search(0.01)).to.equal(0);
expect(sched._search(1)).to.equal(1);
expect(sched._search(1.01)).to.equal(1);
expect(sched._search(20000)).to.equal(2);
expect(sched._search(-1)).to.equal(-1);
sched.dispose();
});
it ("can get the scheduled event at the given time", function(){
var sched = new Schedulable();
sched._insertEvent({
"state" : "A",
"time" : 2
});
sched._insertEvent({
"state" : "C",
"time" : 9.4
});
sched._insertEvent({
"state" : "B",
"time" : 6
});
expect(sched._getEvent(0)).is.null
expect(sched._getEvent(2).state).is.equal("A");
expect(sched._getEvent(5.9).state).is.equal("A");
expect(sched._getEvent(6.1).state).is.equal("B");
expect(sched._getEvent(12).state).is.equal("C");
sched.dispose();
});
it ("puts the second scheduled event after if two events are scheduled at the same time", function(){
var sched = new Schedulable();
sched._insertEvent({
"name" : "A",
"time" : 0
});
sched._insertEvent({
"name" : "B",
"time" : 0
});
expect(sched._getEvent(0).name).is.equal("B");
sched._insertEvent({
"name" : "C",
"time" : 0
});
expect(sched._getEvent(0).name).is.equal("C");
sched.dispose();
});
});
});

View file

@ -0,0 +1,48 @@
define(["Test", "Tone/core/SchedulableState"], function (Test, SchedulableState) {
describe("SchedulableState", function(){
it ("can be created and disposed", function(){
var sched = new SchedulableState();
sched.dispose();
Test.wasDisposed(sched);
});
it ("can schedule a state at a given time", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 1);
sched.dispose();
});
it ("can get a state at a given time", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 2);
expect(sched.getStateAtTime(1)).to.equal("B");
expect(sched.getStateAtTime(0.999)).to.equal("A");
sched.dispose();
});
it ("returns undefined if it's before any scheduled states", function(){
var sched = new SchedulableState();
sched.setStateAtTime("A", 0);
sched.setStateAtTime("B", 1);
sched.setStateAtTime("C", 2);
expect(sched.getStateAtTime(-11)).is.undefined;
sched.dispose();
});
it ("returns initial state if defined and query time is before any scheduled states", function(){
var sched = new SchedulableState("initial");
sched.setStateAtTime("A", 20);
sched.setStateAtTime("B", 21);
sched.setStateAtTime("C", 22);
expect(sched.getStateAtTime(0)).is.equal("initial");
sched.dispose();
});
});
});

View file

@ -30,24 +30,6 @@ define(["Test", "Tone/core/Tone", "PassAudio", "Tone/source/Oscillator", "Tone/i
});
describe("Tone.setContext", function(){
it ("can set a new context", function(){
var origCtx = Tone.context;
var ctx = new OfflineAudioContext(2, 44100, 44100);
Tone.setContext(ctx);
expect(Tone.context).to.equal(ctx);
expect(Tone.prototype.context).to.equal(ctx);
//then set it back
Tone.setContext(origCtx);
expect(Tone.context).to.equal(origCtx);
expect(Tone.prototype.context).to.equal(origCtx);
//and a saftey check
expect(ctx).to.not.equal(origCtx);
});
});
describe("Tone", function(){
var tone = new Tone();
@ -136,9 +118,26 @@ define(["Test", "Tone/core/Tone", "PassAudio", "Tone/source/Oscillator", "Tone/i
done();
});
});
context("Tone.setContext", function(){
it ("can set a new context", function(){
var origCtx = Tone.context;
var ctx = new OfflineAudioContext(2, 44100, 44100);
Tone.setContext(ctx);
expect(Tone.context).to.equal(ctx);
expect(Tone.prototype.context).to.equal(ctx);
//then set it back
Tone.setContext(origCtx);
expect(Tone.context).to.equal(origCtx);
expect(Tone.prototype.context).to.equal(origCtx);
//and a saftey check
expect(ctx).to.not.equal(origCtx);
});
describe("Tone.prototype.set / get", function(){
});
context("Tone.prototype.set / get", function(){
it("sets a value given an object", function(){
var osc = new Oscillator(0);
@ -254,3 +253,5 @@ define(["Test", "Tone/core/Tone", "PassAudio", "Tone/source/Oscillator", "Tone/i
});
});
});