Tone.js/test/helper/Test.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

/* global mocha*/
2015-08-16 18:58:25 +00:00
define(["Tone/core/Tone", "chai", "Tone/core/Context", "Tone/core/Transport", "Tone/core/Buffer", "@babel/polyfill"],
function(Tone, chai, Context, Transport, Buffer, babelPolyfill){
2015-08-16 18:58:25 +00:00
//add a chai test
2017-12-30 16:26:29 +00:00
chai.Assertion.addMethod("percentageFrom", function(val, percent){
new chai.Assertion(this._obj).to.be.closeTo(val, val * percent);
});
2018-05-18 22:04:04 +00:00
//silence the logging
window.TONE_SILENCE_VERSION_LOGGING = true;
2017-12-30 16:26:29 +00:00
//testing setup
window.expect = chai.expect;
mocha.setup({
ui : "bdd",
// make this very long cause sometimes the travis CI server is slow
timeout : 30000
});
2015-08-16 18:58:25 +00:00
2018-05-18 16:07:20 +00:00
//point to the relative path of the audio files
if (window.__karma__){
Buffer.baseUrl = "/base/test/";
} else {
Buffer.baseUrl = "../test/";
}
2017-12-30 16:26:29 +00:00
beforeEach(function(){
if (Transport.bpm.value !== 120){
Transport.bpm.value = 120;
}
if (Transport.timeSignature !== 4){
Transport.timeSignature = 4;
}
});
2017-12-30 16:26:29 +00:00
Context.on("init", function(){
Test.input = Tone.context.createGain();
});
2017-12-30 16:26:29 +00:00
/**
2018-05-18 16:07:20 +00:00
* The Test object
*/
2017-12-30 16:26:29 +00:00
var Test = {
input : Tone.context.createGain()
};
2015-08-16 18:58:25 +00:00
2017-12-30 16:26:29 +00:00
Test.run = function(){
mocha.run();
};
2015-08-16 18:58:25 +00:00
2017-12-30 16:26:29 +00:00
Test.wasDisposed = function(obj){
for (var prop in obj){
var member = obj[prop];
if (typeof member !== "function" &&
2017-11-21 17:40:32 +00:00
typeof member !== "string" &&
2015-08-16 18:58:25 +00:00
typeof member !== "number" &&
typeof member !== "boolean" &&
2015-08-17 02:22:38 +00:00
typeof member !== "undefined" &&
2017-11-21 17:40:32 +00:00
prop !== "preset" &&
!(member instanceof AudioContext) &&
!obj.constructor.prototype[prop]){
2017-12-30 16:26:29 +00:00
if (member !== null){
throw Error("property was not completely disposed: "+prop);
}
2015-08-16 18:58:25 +00:00
}
}
2017-12-30 16:26:29 +00:00
};
2015-08-16 18:58:25 +00:00
2017-12-30 16:26:29 +00:00
Test.connect = function(node, inputNumber){
this.input.connect(node, 0, inputNumber);
this.input.disconnect();
};
2017-12-30 16:26:29 +00:00
Test.whenBetween = function(value, start, stop, callback){
if (value >= start && value < stop){
callback();
}
};
2017-12-30 16:26:29 +00:00
//invoked only once
Test.atTime = function(when, callback){
var wasInvoked = false;
return function(time){
if (time >= when && !wasInvoked){
callback(time);
wasInvoked = true;
}
};
};
return Test;
});