scratch-vm/test/integration/complex.js

100 lines
3 KiB
JavaScript
Raw Normal View History

const fs = require('fs');
const path = require('path');
const test = require('tap').test;
const makeTestStorage = require('../fixtures/make-test-storage');
const extract = require('../fixtures/extract');
const VirtualMachine = require('../../src/index');
2017-01-05 16:58:31 -05:00
const projectUri = path.resolve(__dirname, '../fixtures/complex.sb2');
const project = extract(projectUri);
2017-01-05 16:58:31 -05:00
const spriteUri = path.resolve(__dirname, '../fixtures/sprite.json');
const sprite = fs.readFileSync(spriteUri, 'utf8');
2017-01-05 16:58:31 -05:00
test('complex', t => {
const vm = new VirtualMachine();
vm.attachStorage(makeTestStorage());
2017-01-05 16:58:31 -05:00
// Evaluate playground data and exit
vm.on('playgroundData', e => {
const threads = JSON.parse(e.threads);
2017-01-05 16:58:31 -05:00
t.ok(threads.length === 0);
t.end();
process.nextTick(process.exit);
});
// Manipulate each target
vm.on('targetsUpdate', data => {
const targets = data.targetList;
for (const i in targets) {
2017-01-05 16:58:31 -05:00
if (targets[i].isStage === true) continue;
if (targets[i].name.match(/test/)) continue;
2017-01-05 16:58:31 -05:00
vm.setEditingTarget(targets[i].id);
vm.renameSprite(targets[i].id, 'test');
vm.postSpriteInfo({
x: 0,
y: 10,
direction: 90,
draggable: true,
2017-01-05 16:58:31 -05:00
rotationStyle: 'all around',
visible: true
});
vm.addCostume(
'f9a1c175dbe2e5dee472858dd30d16bb.svg',
{
costumeName: 'costume1',
baseLayerID: 0,
baseLayerMD5: 'f9a1c175dbe2e5dee472858dd30d16bb.svg',
bitmapResolution: 1,
rotationCenterX: 47,
rotationCenterY: 55
}
);
2017-01-05 16:58:31 -05:00
}
});
// Start VM, load project, and run
t.doesNotThrow(() => {
2017-01-05 16:58:31 -05:00
vm.start();
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project).then(() => {
2017-04-18 11:55:38 -04:00
vm.greenFlag();
2017-01-05 16:58:31 -05:00
2017-04-18 11:55:38 -04:00
// Post IO data
vm.postIOData('mouse', {
isDown: true,
x: 0,
y: 10,
canvasWidth: 100,
canvasHeight: 100
});
2017-01-05 16:58:31 -05:00
2017-04-18 11:55:38 -04:00
// Add sprite
vm.addSprite2(sprite);
2017-01-05 16:58:31 -05:00
2017-04-18 11:55:38 -04:00
// Add backdrop
vm.addBackdrop(
'6b3d87ba2a7f89be703163b6c1d4c964.png',
{
costumeName: 'baseball-field',
baseLayerID: 26,
baseLayerMD5: '6b3d87ba2a7f89be703163b6c1d4c964.png',
bitmapResolution: 2,
rotationCenterX: 480,
rotationCenterY: 360
}
);
// After two seconds, get playground data and stop
setTimeout(() => {
2017-04-18 11:55:38 -04:00
vm.getPlaygroundData();
vm.stopAll();
}, 2000);
});
2017-01-05 16:58:31 -05:00
});
});