Add sound and costume reordering methods and tests to top-level API

This commit is contained in:
Paul Kaplan 2018-06-05 12:01:24 -04:00
parent d3fd3ff806
commit e6ebc33e9e
2 changed files with 92 additions and 0 deletions

View file

@ -1033,6 +1033,38 @@ class VirtualMachine extends EventEmitter {
return null;
}
/**
* Reorder the costumes of a target if it exists. Return whether it succeeded.
* @param {!string} targetId ID of the target which owns the costumes.
* @param {!number} costumeIndex index of the costume to move.
* @param {!number} newIndex index that the costume should be moved to.
* @returns {boolean} whether the target was found for reordering.
*/
reorderCostume (targetId, costumeIndex, newIndex) {
const target = this.runtime.getTargetById(targetId);
if (target) {
target.reorderCostume(costumeIndex, newIndex);
return true;
}
return false;
}
/**
* Reorder the sounds of a target if it exists. Return whether it succeeded.
* @param {!string} targetId ID of the target which owns the sounds.
* @param {!number} soundIndex index of the sound to move.
* @param {!number} newIndex index that the sound should be moved to.
* @returns {boolean} whether the target was found for reordering.
*/
reorderSound (targetId, soundIndex, newIndex) {
const target = this.runtime.getTargetById(targetId);
if (target) {
target.reorderSound(soundIndex, newIndex);
return true;
}
return false;
}
/**
* Put a target into a "drag" state, during which its X/Y positions will be unaffected
* by blocks.

View file

@ -306,6 +306,66 @@ test('duplicateSprite assigns duplicated sprite a fresh name', t => {
});
test('reorderCostume', t => {
const vm = new VirtualMachine();
vm.emitTargetsUpdate = () => {};
const spr = new Sprite(null, vm.runtime);
spr.name = 'foo';
const target = spr.createClone();
// Stub out reorder on target, tested in rendered-target tests.
// Just want to know if it is called with the right params.
let costumeIndex = null;
let newIndex = null;
target.reorderCostume = (_costumeIndex, _newIndex) => {
costumeIndex = _costumeIndex;
newIndex = _newIndex;
};
vm.runtime.targets = [target];
t.equal(vm.reorderCostume('not-a-target', 0, 3), false);
t.equal(costumeIndex, null);
t.equal(newIndex, null);
t.equal(vm.reorderCostume(target.id, 0, 3), true);
t.equal(costumeIndex, 0);
t.equal(newIndex, 3);
t.end();
});
test('reorderSound', t => {
const vm = new VirtualMachine();
vm.emitTargetsUpdate = () => {};
const spr = new Sprite(null, vm.runtime);
spr.name = 'foo';
const target = spr.createClone();
// Stub out reorder on target, tested in rendered-target tests.
// Just want to know if it is called with the right params.
let soundIndex = null;
let newIndex = null;
target.reorderSound = (_soundIndex, _newIndex) => {
soundIndex = _soundIndex;
newIndex = _newIndex;
};
vm.runtime.targets = [target];
t.equal(vm.reorderSound('not-a-target', 0, 3), false);
t.equal(soundIndex, null); // Make sure reorder function was not called somehow
t.equal(newIndex, null);
t.equal(vm.reorderSound(target.id, 0, 3), true);
t.equal(soundIndex, 0); // Make sure reorder function was called correctly
t.equal(newIndex, 3);
t.end();
});
test('emitWorkspaceUpdate', t => {
const vm = new VirtualMachine();
const blocksToXML = comments => {