Fix tests involving scratch-storage

- Attach the storage module to the VM for tests which load projects or
  other assets.
- Move `import_sb2.js` from `test/unit/ into `test/integration` since it
  now depends heavily on `scratch-storage`.
- Skip loading costumes when there is no renderer.
This commit is contained in:
Christopher Willis-Ford 2017-03-09 15:50:43 -08:00
parent c23e9c6bf8
commit e4fd9d57a2
15 changed files with 77 additions and 5 deletions

View file

@ -51,7 +51,9 @@ var parseScratchObject = function (object, runtime, topLevel) {
rotationCenterY: costumeSource.rotationCenterY, rotationCenterY: costumeSource.rotationCenterY,
skinId: null skinId: null
}; };
if (runtime.renderer) {
costumePromises.push(loadCostume(costumeSource.baseLayerMD5, costume, runtime)); costumePromises.push(loadCostume(costumeSource.baseLayerMD5, costume, runtime));
}
sprite.costumes.push(costume); sprite.costumes.push(costume);
} }
} }
@ -147,6 +149,7 @@ var parseScratchObject = function (object, runtime, topLevel) {
/** /**
* Load a costume's asset into memory asynchronously. * Load a costume's asset into memory asynchronously.
* Do not call this unless there is a renderer attached.
* @param {string} md5ext - the MD5 and extension of the costume to be loaded. * @param {string} md5ext - the MD5 and extension of the costume to be loaded.
* @param {!object} costume - the Scratch costume object. * @param {!object} costume - the Scratch costume object.
* @property {int} skinId - the ID of the costume's render skin, once installed. * @property {int} skinId - the ID of the costume's render skin, once installed.
@ -171,9 +174,7 @@ var loadCostume = function (md5ext, costume, runtime) {
if (assetType === AssetType.ImageVector) { if (assetType === AssetType.ImageVector) {
promise = promise.then(function (costumeAsset) { promise = promise.then(function (costumeAsset) {
if (runtime.renderer) {
costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter); costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
}
}); });
} else { } else {
promise = promise.then(function (costumeAsset) { promise = promise.then(function (costumeAsset) {

View file

@ -171,7 +171,7 @@ VirtualMachine.prototype.downloadProjectId = function (id) {
/** /**
* Add a single sprite from the "Sprite2" (i.e., SB2 sprite) format. * Add a single sprite from the "Sprite2" (i.e., SB2 sprite) format.
* @param {?string} json JSON string representing the sprite. * @param {string} json JSON string representing the sprite.
*/ */
VirtualMachine.prototype.addSprite2 = function (json) { VirtualMachine.prototype.addSprite2 = function (json) {
// Select new sprite. // Select new sprite.

47
test/fixtures/attach-test-storage.js vendored Normal file
View file

@ -0,0 +1,47 @@
var ScratchStorage = require('scratch-storage');
var ASSET_SERVER = 'https://cdn.assets.scratch.mit.edu/';
var PROJECT_SERVER = 'https://cdn.projects.scratch.mit.edu/';
/**
* @param {Asset} asset - calculate a URL for this asset.
* @returns {string} a URL to download a project file.
*/
var getProjectUrl = function (asset) {
var assetIdParts = asset.assetId.split('.');
var assetUrlParts = [PROJECT_SERVER, 'internalapi/project/', assetIdParts[0], '/get/'];
if (assetIdParts[1]) {
assetUrlParts.push(assetIdParts[1]);
}
return assetUrlParts.join('');
};
/**
* @param {Asset} asset - calculate a URL for this asset.
* @returns {string} a URL to download a project asset (PNG, WAV, etc.)
*/
var getAssetUrl = function (asset) {
var assetUrlParts = [
ASSET_SERVER,
'internalapi/asset/',
asset.assetId,
'.',
asset.assetType.runtimeFormat,
'/get/'
];
return assetUrlParts.join('');
};
/**
* Construct a new instance of ScratchStorage, provide it with default web sources, and attach it to the provided VM.
* @param {VirtualMachine} vm - the VM which will own the new ScratchStorage instance.
*/
var attachTestStorage = function (vm) {
var storage = new ScratchStorage();
var AssetType = ScratchStorage.AssetType;
storage.addWebSource([AssetType.Project], getProjectUrl);
storage.addWebSource([AssetType.ImageVector, AssetType.ImageBitmap, AssetType.Sound], getAssetUrl);
vm.attachStorage(storage);
};
module.exports = attachTestStorage;

View file

@ -1,6 +1,7 @@
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -12,6 +13,7 @@ var sprite = fs.readFileSync(spriteUri, 'utf8');
test('complex', function (t) { test('complex', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('control', function (t) { test('control', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('data', function (t) { test('data', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function () { vm.on('playgroundData', function () {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('event', function (t) { test('event', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(projectUri);
test('complex', function (t) { test('complex', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var renderedTarget = require('../../src/sprites/rendered-target'); var renderedTarget = require('../../src/sprites/rendered-target');
@ -18,6 +19,7 @@ test('default', function (t) {
// Create runtime instance & load SB2 into it // Create runtime instance & load SB2 into it
var rt = new runtime(); var rt = new runtime();
attachTestStorage(rt);
sb2(file, rt); sb2(file, rt);
// Test // Test

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('looks', function (t) { test('looks', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('motion', function (t) { test('motion', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('pen', function (t) { test('pen', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function () { vm.on('playgroundData', function () {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('procedure', function (t) { test('procedure', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('sensing', function (t) { test('sensing', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {

View file

@ -1,5 +1,6 @@
var path = require('path'); var path = require('path');
var test = require('tap').test; var test = require('tap').test;
var attachTestStorage = require('../fixtures/attach-test-storage');
var extract = require('../fixtures/extract'); var extract = require('../fixtures/extract');
var VirtualMachine = require('../../src/index'); var VirtualMachine = require('../../src/index');
@ -8,6 +9,7 @@ var project = extract(uri);
test('sound', function (t) { test('sound', function (t) {
var vm = new VirtualMachine(); var vm = new VirtualMachine();
attachTestStorage(vm);
// Evaluate playground data and exit // Evaluate playground data and exit
vm.on('playgroundData', function (e) { vm.on('playgroundData', function (e) {