Fix pen tests

This commit is contained in:
Christopher Willis-Ford 2017-10-04 15:40:25 -07:00
parent dd20e09774
commit b6727a766f
4 changed files with 47 additions and 20 deletions

View file

@ -91,6 +91,16 @@ class SharedDispatch {
}
}
/**
* Check if a particular service lives on another worker.
* @param {string} service - the service to check.
* @returns {boolean} - true if the service is remote (calls must cross a Worker boundary), false otherwise.
* @private
*/
_isRemoteService (service) {
return this._getServiceProvider(service).isRemote;
}
/**
* Like {@link call}, but force the call to be posted through a particular communication channel.
* @param {object} provider - send the call through this object's `postMessage` function.

View file

@ -198,7 +198,17 @@ class ExtensionManager {
blockInfo.opcode = this._sanitizeID(blockInfo.opcode);
blockInfo.func = blockInfo.func ? this._sanitizeID(blockInfo.func) : blockInfo.opcode;
blockInfo.text = blockInfo.text || blockInfo.opcode;
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func);
/**
* This is only here because the VM performs poorly when blocks return promises.
* @TODO make it possible for the VM to resolve a promise and continue during the same frame.
*/
if (dispatch._isRemoteService(serviceName)) {
blockInfo.func = dispatch.call.bind(dispatch, serviceName, blockInfo.func);
} else {
const serviceObject = dispatch.services[serviceName];
blockInfo.func = serviceObject[blockInfo.func].bind(serviceObject);
}
return blockInfo;
}
}

View file

@ -500,27 +500,27 @@ const specMap = {
]
},
'clearPenTrails': {
opcode: 'pen_clear',
opcode: 'pen.clear',
argMap: [
]
},
'stampCostume': {
opcode: 'pen_stamp',
opcode: 'pen.stamp',
argMap: [
]
},
'putPenDown': {
opcode: 'pen_pendown',
opcode: 'pen.penDown',
argMap: [
]
},
'putPenUp': {
opcode: 'pen_penup',
opcode: 'pen.penUp',
argMap: [
]
},
'penColor:': {
opcode: 'pen_setpencolortocolor',
opcode: 'pen.setPenColorToColor',
argMap: [
{
type: 'input',
@ -530,7 +530,7 @@ const specMap = {
]
},
'changePenHueBy:': {
opcode: 'pen_changepencolorby',
opcode: 'pen.changePenHueBy',
argMap: [
{
type: 'input',
@ -540,7 +540,7 @@ const specMap = {
]
},
'setPenHueTo:': {
opcode: 'pen_setpencolortonum',
opcode: 'pen.setPenHueToNumber',
argMap: [
{
type: 'input',
@ -550,7 +550,7 @@ const specMap = {
]
},
'changePenShadeBy:': {
opcode: 'pen_changepenshadeby',
opcode: 'pen.changePenShadeBy',
argMap: [
{
type: 'input',
@ -560,7 +560,7 @@ const specMap = {
]
},
'setPenShadeTo:': {
opcode: 'pen_setpenshadeto',
opcode: 'pen.setPenShadeToNumber',
argMap: [
{
type: 'input',
@ -570,7 +570,7 @@ const specMap = {
]
},
'changePenSizeBy:': {
opcode: 'pen_changepensizeby',
opcode: 'pen.changePenSizeBy',
argMap: [
{
type: 'input',
@ -580,7 +580,7 @@ const specMap = {
]
},
'penSize:': {
opcode: 'pen_setpensizeto',
opcode: 'pen.setPenSizeTo',
argMap: [
{
type: 'input',

View file

@ -1,8 +1,10 @@
const Worker = require('tiny-worker');
const path = require('path');
const test = require('tap').test;
const Scratch3PenBlocks = require('../../src/blocks/scratch3_pen');
const VirtualMachine = require('../../src/index');
const dispatch = require('../../src/dispatch/central-dispatch');
const makeTestStorage = require('../fixtures/make-test-storage');
const extract = require('../fixtures/extract');
@ -10,6 +12,9 @@ const extract = require('../fixtures/extract');
const uri = path.resolve(__dirname, '../fixtures/pen.sb2');
const project = extract(uri);
// By default Central Dispatch works with the Worker class built into the browser. Tell it to use TinyWorker instead.
dispatch.workerClass = Worker;
test('pen', t => {
const vm = new VirtualMachine();
vm.attachStorage(makeTestStorage());
@ -42,14 +47,16 @@ test('pen', t => {
vm.clear();
vm.setCompatibilityMode(false);
vm.setTurboMode(false);
vm.loadProject(project).then(() => {
vm.greenFlag();
vm.loadProject(project)
.then(() => vm.extensionManager.loadExtensionURL('pen')) /** @TODO: loadProject should load extensions */
.then(() => {
vm.greenFlag();
// After two seconds, get playground data and stop
setTimeout(() => {
vm.getPlaygroundData();
vm.stopAll();
}, 2000);
});
// After two seconds, get playground data and stop
setTimeout(() => {
vm.getPlaygroundData();
vm.stopAll();
}, 2000);
});
});
});