mirror of
https://github.com/scratchfoundation/scratch-render.git
synced 2025-07-29 15:40:29 -04:00
Fix file input in tests
This commit is contained in:
parent
fdd02a6bd6
commit
34072d2f53
5 changed files with 79 additions and 65 deletions
test/helper
53
test/helper/page-util.js
Normal file
53
test/helper/page-util.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
/* global window, VirtualMachine, ScratchStorage, ScratchSVGRenderer */
|
||||
/* eslint-env browser */
|
||||
|
||||
// Wait for all SVG skins to be loaded.
|
||||
// TODO: this is extremely janky and should be removed once vm.loadProject waits for SVG skins to load
|
||||
window.waitForSVGSkinLoad = renderer => new Promise(resolve => {
|
||||
// eslint-disable-next-line prefer-const
|
||||
let interval;
|
||||
|
||||
const waitInner = () => {
|
||||
let numSVGSkins = 0;
|
||||
let numLoadedSVGSkins = 0;
|
||||
for (const skin of renderer._allSkins) {
|
||||
if (skin.constructor.name !== 'SVGSkin') continue;
|
||||
numSVGSkins++;
|
||||
if (skin._svgRenderer.loaded) numLoadedSVGSkins++;
|
||||
}
|
||||
|
||||
if (numSVGSkins === numLoadedSVGSkins) {
|
||||
clearInterval(interval);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
|
||||
interval = setInterval(waitInner, 1);
|
||||
});
|
||||
|
||||
window.loadFileInputIntoVM = (fileInput, vm, render) => {
|
||||
const reader = new FileReader();
|
||||
return new Promise(resolve => {
|
||||
reader.onload = () => {
|
||||
vm.start();
|
||||
vm.loadProject(reader.result)
|
||||
.then(() => window.waitForSVGSkinLoad(render))
|
||||
.then(() => {
|
||||
resolve();
|
||||
});
|
||||
};
|
||||
reader.readAsArrayBuffer(fileInput.files[0]);
|
||||
});
|
||||
};
|
||||
|
||||
window.initVM = render => {
|
||||
const vm = new VirtualMachine();
|
||||
const storage = new ScratchStorage();
|
||||
|
||||
vm.attachStorage(storage);
|
||||
vm.attachRenderer(render);
|
||||
vm.attachV2SVGAdapter(new ScratchSVGRenderer.SVGRenderer());
|
||||
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
|
||||
|
||||
return vm;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue