Merge pull request #432 from thisandagain/bugfix/coverage

Add basic test coverage for the sound blocks
This commit is contained in:
Eric Rosenbaum 2017-02-03 17:58:33 -05:00 committed by GitHub
commit 92affb05f0
3 changed files with 50 additions and 0 deletions

View file

@ -79,6 +79,7 @@ Scratch3SoundBlocks.prototype.playSound = function (args, util) {
var index = this._getSoundIndex(args.SOUND_MENU, util); var index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) { if (index >= 0) {
var md5 = util.target.sprite.sounds[index].md5; var md5 = util.target.sprite.sounds[index].md5;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.playSound(md5); util.target.audioPlayer.playSound(md5);
} }
}; };
@ -87,6 +88,7 @@ Scratch3SoundBlocks.prototype.playSoundAndWait = function (args, util) {
var index = this._getSoundIndex(args.SOUND_MENU, util); var index = this._getSoundIndex(args.SOUND_MENU, util);
if (index >= 0) { if (index >= 0) {
var md5 = util.target.sprite.sounds[index].md5; var md5 = util.target.sprite.sounds[index].md5;
if (util.target.audioPlayer === null) return;
return util.target.audioPlayer.playSound(md5); return util.target.audioPlayer.playSound(md5);
} }
}; };
@ -124,6 +126,7 @@ Scratch3SoundBlocks.prototype.getSoundIndexByName = function (soundName, util) {
}; };
Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) { Scratch3SoundBlocks.prototype.stopAllSounds = function (args, util) {
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.stopAllSounds(); util.target.audioPlayer.stopAllSounds();
}; };
@ -132,19 +135,23 @@ Scratch3SoundBlocks.prototype.playNoteForBeats = function (args, util) {
var beats = Cast.toNumber(args.BEATS); var beats = Cast.toNumber(args.BEATS);
var soundState = this._getSoundState(util.target); var soundState = this._getSoundState(util.target);
var inst = soundState.currentInstrument; var inst = soundState.currentInstrument;
if (typeof this.runtime.audioEngine === 'undefined') return;
return this.runtime.audioEngine.playNoteForBeatsWithInst(note, beats, inst); return this.runtime.audioEngine.playNoteForBeatsWithInst(note, beats, inst);
}; };
Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) { Scratch3SoundBlocks.prototype.playDrumForBeats = function (args, util) {
var drum = Cast.toNumber(args.DRUM); var drum = Cast.toNumber(args.DRUM);
drum -= 1; // drums are one-indexed drum -= 1; // drums are one-indexed
if (typeof this.runtime.audioEngine === 'undefined') return;
drum = MathUtil.wrapClamp(drum, 0, this.runtime.audioEngine.numDrums); drum = MathUtil.wrapClamp(drum, 0, this.runtime.audioEngine.numDrums);
var beats = Cast.toNumber(args.BEATS); var beats = Cast.toNumber(args.BEATS);
if (util.target.audioPlayer === null) return;
return util.target.audioPlayer.playDrumForBeats(drum, beats); return util.target.audioPlayer.playDrumForBeats(drum, beats);
}; };
Scratch3SoundBlocks.prototype.restForBeats = function (args) { Scratch3SoundBlocks.prototype.restForBeats = function (args) {
var beats = Cast.toNumber(args.BEATS); var beats = Cast.toNumber(args.BEATS);
if (typeof this.runtime.audioEngine === 'undefined') return;
return this.runtime.audioEngine.waitForBeats(beats); return this.runtime.audioEngine.waitForBeats(beats);
}; };
@ -152,6 +159,7 @@ Scratch3SoundBlocks.prototype.setInstrument = function (args, util) {
var soundState = this._getSoundState(util.target); var soundState = this._getSoundState(util.target);
var instNum = Cast.toNumber(args.INSTRUMENT); var instNum = Cast.toNumber(args.INSTRUMENT);
instNum -= 1; // instruments are one-indexed instNum -= 1; // instruments are one-indexed
if (typeof this.runtime.audioEngine === 'undefined') return;
instNum = MathUtil.wrapClamp(instNum, 0, this.runtime.audioEngine.numInstruments); instNum = MathUtil.wrapClamp(instNum, 0, this.runtime.audioEngine.numInstruments);
soundState.currentInstrument = instNum; soundState.currentInstrument = instNum;
return this.runtime.audioEngine.instrumentPlayer.loadInstrument(soundState.currentInstrument); return this.runtime.audioEngine.instrumentPlayer.loadInstrument(soundState.currentInstrument);
@ -165,6 +173,7 @@ Scratch3SoundBlocks.prototype.setEffect = function (args, util) {
if (!soundState.effects.hasOwnProperty(effect)) return; if (!soundState.effects.hasOwnProperty(effect)) return;
soundState.effects[effect] = value; soundState.effects[effect] = value;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]); util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
}; };
@ -176,6 +185,7 @@ Scratch3SoundBlocks.prototype.changeEffect = function (args, util) {
if (!soundState.effects.hasOwnProperty(effect)) return; if (!soundState.effects.hasOwnProperty(effect)) return;
soundState.effects[effect] += value; soundState.effects[effect] += value;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setEffect(effect, soundState.effects[effect]); util.target.audioPlayer.setEffect(effect, soundState.effects[effect]);
}; };
@ -184,6 +194,7 @@ Scratch3SoundBlocks.prototype.clearEffects = function (args, util) {
for (var effect in soundState.effects) { for (var effect in soundState.effects) {
soundState.effects[effect] = 0; soundState.effects[effect] = 0;
} }
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.clearEffects(); util.target.audioPlayer.clearEffects();
}; };
@ -202,6 +213,7 @@ Scratch3SoundBlocks.prototype._updateVolume = function (volume, util) {
var soundState = this._getSoundState(util.target); var soundState = this._getSoundState(util.target);
volume = MathUtil.clamp(volume, 0, 100); volume = MathUtil.clamp(volume, 0, 100);
soundState.volume = volume; soundState.volume = volume;
if (util.target.audioPlayer === null) return;
util.target.audioPlayer.setVolume(soundState.volume); util.target.audioPlayer.setVolume(soundState.volume);
}; };
@ -212,15 +224,18 @@ Scratch3SoundBlocks.prototype.getVolume = function (args, util) {
Scratch3SoundBlocks.prototype.setTempo = function (args) { Scratch3SoundBlocks.prototype.setTempo = function (args) {
var value = Cast.toNumber(args.TEMPO); var value = Cast.toNumber(args.TEMPO);
if (typeof this.runtime.audioEngine === 'undefined') return;
this.runtime.audioEngine.setTempo(value); this.runtime.audioEngine.setTempo(value);
}; };
Scratch3SoundBlocks.prototype.changeTempo = function (args) { Scratch3SoundBlocks.prototype.changeTempo = function (args) {
var value = Cast.toNumber(args.TEMPO); var value = Cast.toNumber(args.TEMPO);
if (typeof this.runtime.audioEngine === 'undefined') return;
this.runtime.audioEngine.changeTempo(value); this.runtime.audioEngine.changeTempo(value);
}; };
Scratch3SoundBlocks.prototype.getTempo = function () { Scratch3SoundBlocks.prototype.getTempo = function () {
if (typeof this.runtime.audioEngine === 'undefined') return;
return this.runtime.audioEngine.currentTempo; return this.runtime.audioEngine.currentTempo;
}; };

BIN
test/fixtures/sound.sb2 vendored Normal file

Binary file not shown.

35
test/integration/sound.js Normal file
View file

@ -0,0 +1,35 @@
var path = require('path');
var test = require('tap').test;
var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index');
var uri = path.resolve(__dirname, '../fixtures/sound.sb2');
var project = extract(uri);
test('sound', function (t) {
var vm = new VirtualMachine();
// Evaluate playground data and exit
vm.on('playgroundData', function (e) {
var threads = JSON.parse(e.threads);
t.ok(threads.length > 0);
t.end();
process.nextTick(process.exit);
});
// Start VM, load project, and run
t.doesNotThrow(function () {
vm.start();
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project);
vm.greenFlag();
});
// After two seconds, get playground data and stop
setTimeout(function () {
vm.getPlaygroundData();
vm.stopAll();
}, 2000);
});