2017-11-24 22:13:51 +00:00
|
|
|
define(["Test", "Tone/core/Draw", "Tone/core/Tone"],
|
2016-12-19 01:26:11 +00:00
|
|
|
function (Test, Draw, Tone) {
|
|
|
|
|
|
|
|
describe("Draw", function(){
|
|
|
|
|
2017-11-24 22:13:51 +00:00
|
|
|
//replace rAF since test is not executed in focus
|
|
|
|
var originalRAF = window.requestAnimationFrame;
|
|
|
|
|
|
|
|
window.requestAnimationFrame = function(cb){
|
|
|
|
setTimeout(cb, 16);
|
|
|
|
};
|
|
|
|
|
|
|
|
after(function(){
|
|
|
|
//re-set rAF
|
|
|
|
window.requestAnimationFrame = originalRAF;
|
|
|
|
});
|
|
|
|
|
2016-12-19 01:26:11 +00:00
|
|
|
it ("can schedule a callback at a AudioContext time", function(done){
|
|
|
|
var scheduledTime = Tone.now() + 0.2;
|
|
|
|
Draw.schedule(function(){
|
|
|
|
expect(Tone.now()).to.be.closeTo(scheduledTime, 0.05);
|
|
|
|
done();
|
|
|
|
}, scheduledTime);
|
|
|
|
});
|
|
|
|
|
|
|
|
it ("can schedule multiple callbacks", function(done){
|
|
|
|
var callbackCount = 0;
|
|
|
|
var firstEvent = Tone.now() + 0.1;
|
|
|
|
Draw.schedule(function(){
|
|
|
|
callbackCount++;
|
|
|
|
expect(Tone.now()).to.be.closeTo(firstEvent, 0.05);
|
|
|
|
}, firstEvent);
|
|
|
|
|
|
|
|
var thirdEvent = Tone.now() + 0.3;
|
|
|
|
Draw.schedule(function(){
|
|
|
|
callbackCount++;
|
|
|
|
expect(Tone.now()).to.be.closeTo(thirdEvent, 0.05);
|
|
|
|
expect(callbackCount).to.equal(3);
|
|
|
|
done();
|
|
|
|
}, thirdEvent);
|
|
|
|
|
|
|
|
var secondEvent = Tone.now() + 0.2;
|
|
|
|
Draw.schedule(function(){
|
|
|
|
callbackCount++;
|
|
|
|
expect(Tone.now()).to.be.closeTo(secondEvent, 0.05);
|
|
|
|
}, secondEvent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it ("can cancel scheduled events", function(done){
|
|
|
|
var callbackCount = 0;
|
|
|
|
Draw.schedule(function(){
|
|
|
|
callbackCount++;
|
|
|
|
}, Tone.now() + 0.1);
|
|
|
|
|
|
|
|
Draw.schedule(function(){
|
|
|
|
throw new Error("should not call this method");
|
|
|
|
}, Tone.now() + 0.2);
|
|
|
|
|
|
|
|
Draw.schedule(function(){
|
|
|
|
throw new Error("should not call this method");
|
|
|
|
}, Tone.now() + 0.25);
|
|
|
|
|
|
|
|
//cancel the second and third events
|
|
|
|
Draw.cancel(Tone.now() + 0.15);
|
|
|
|
|
|
|
|
//schedule another one after
|
|
|
|
Draw.schedule(function(){
|
|
|
|
callbackCount++;
|
|
|
|
expect(callbackCount).to.equal(2);
|
|
|
|
done();
|
|
|
|
}, Tone.now() + 0.3);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
2017-11-24 22:13:51 +00:00
|
|
|
});
|