Skip to content

Commit 3f68e18

Browse files
authored
Merge pull request #603 from LLK/revert-597-playwright
Revert "Switch from Chromeless to Playwright for tests"
2 parents 7349c19 + 8137923 commit 3f68e18

File tree

7 files changed

+119
-135
lines changed

7 files changed

+119
-135
lines changed

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
language: node_js
22
dist: trusty
3+
addons:
4+
chrome: stable
35
node_js:
4-
- 10
6+
- 8
57
- node
68
env:
79
- NODE_ENV=production
10+
before_install:
11+
- google-chrome-stable --headless --no-sandbox --remote-debugging-port=9222 &
812
install:
913
- npm --production=false install
1014
- npm --production=false update
@@ -20,7 +24,7 @@ jobs:
2024
- npm run docs
2125
- npm run tap
2226
- stage: deploy
23-
node_js: 10
27+
node_js: 8
2428
script: npm run build
2529
before_deploy:
2630
- VPKG=$($(npm bin)/json -f package.json version)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
"babel-loader": "^7.1.4",
3030
"babel-polyfill": "^6.22.0",
3131
"babel-preset-env": "^1.6.1",
32+
"chromeless": "^1.5.1",
3233
"copy-webpack-plugin": "^4.5.1",
3334
"docdash": "^0.4.0",
3435
"eslint": "^4.6.1",
3536
"eslint-config-scratch": "^5.0.0",
3637
"gh-pages": "^1.0.0",
3738
"jsdoc": "^3.5.5",
3839
"json": "^9.0.4",
39-
"playwright-chromium": "^1.0.1",
4040
"scratch-vm": "0.2.0-prerelease.20191227164934",
4141
"tap": "^11.0.0",
4242
"travis-after-all": "^1.4.4",

test/helper/page-util.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/integration/cpu-render.html

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<script src="../../node_modules/scratch-vm/dist/web/scratch-vm.js"></script>
33
<script src="../../node_modules/scratch-storage/dist/web/scratch-storage.js"></script>
44
<script src="../../node_modules/scratch-svg-renderer/dist/web/scratch-svg-renderer.js"></script>
5-
<script src="../helper/page-util.js"></script>
65
<!-- note: this uses the BUILT version of scratch-render! make sure to npm run build -->
76
<script src="../../dist/web/scratch-render.js"></script>
87

@@ -18,18 +17,38 @@
1817
window.devicePixelRatio = 1;
1918
const gpuCanvas = document.getElementById('test');
2019
var render = new ScratchRender(gpuCanvas);
21-
var vm = initVM(render);
20+
var vm = new VirtualMachine();
21+
var storage = new ScratchStorage();
2222

23-
const fileInput = document.getElementById('file');
24-
const loadFile = loadFileInputIntoVM.bind(null, fileInput, vm, render);
25-
fileInput.addEventListener('change', e => {
26-
loadFile()
27-
.then(() => {
28-
vm.greenFlag();
29-
setTimeout(() => {
30-
renderCpu();
31-
}, 1000);
32-
});
23+
vm.attachStorage(storage);
24+
vm.attachRenderer(render);
25+
vm.attachV2SVGAdapter(new ScratchSVGRenderer.SVGRenderer());
26+
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
27+
28+
document.getElementById('file').addEventListener('click', e => {
29+
document.body.removeChild(document.getElementById('loaded'));
30+
});
31+
32+
document.getElementById('file').addEventListener('change', e => {
33+
const reader = new FileReader();
34+
const thisFileInput = e.target;
35+
reader.onload = () => {
36+
vm.start();
37+
vm.loadProject(reader.result)
38+
.then(() => {
39+
// we add a `#loaded` div to our document, the integration suite
40+
// waits for that element to show up to assume the vm is ready
41+
// to play!
42+
const div = document.createElement('div');
43+
div.id='loaded';
44+
document.body.appendChild(div);
45+
vm.greenFlag();
46+
setTimeout(() => {
47+
renderCpu();
48+
}, 1000);
49+
});
50+
};
51+
reader.readAsArrayBuffer(thisFileInput.files[0]);
3352
});
3453

3554
const cpuCanvas = document.getElementById('cpu');

test/integration/index.html

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<script src="../../node_modules/scratch-vm/dist/web/scratch-vm.js"></script>
33
<script src="../../node_modules/scratch-storage/dist/web/scratch-storage.js"></script>
44
<script src="../../node_modules/scratch-svg-renderer/dist/web/scratch-svg-renderer.js"></script>
5-
<script src="../helper/page-util.js"></script>
65
<!-- note: this uses the BUILT version of scratch-render! make sure to npm run build -->
76
<script src="../../dist/web/scratch-render.js"></script>
87

@@ -16,13 +15,39 @@
1615

1716
var canvas = document.getElementById('test');
1817
var render = new ScratchRender(canvas);
19-
var vm = initVM(render);
18+
var vm = new VirtualMachine();
19+
var storage = new ScratchStorage();
2020
var mockMouse = data => vm.runtime.postIOData('mouse', {
2121
canvasWidth: canvas.width,
2222
canvasHeight: canvas.height,
2323
...data,
2424
});
2525

26-
const loadFile = loadFileInputIntoVM.bind(null, document.getElementById('file'), vm, render);
26+
vm.attachStorage(storage);
27+
vm.attachRenderer(render);
28+
vm.attachV2SVGAdapter(new ScratchSVGRenderer.SVGRenderer());
29+
vm.attachV2BitmapAdapter(new ScratchSVGRenderer.BitmapAdapter());
30+
31+
document.getElementById('file').addEventListener('click', e => {
32+
document.body.removeChild(document.getElementById('loaded'));
33+
});
34+
35+
document.getElementById('file').addEventListener('change', e => {
36+
const reader = new FileReader();
37+
const thisFileInput = e.target;
38+
reader.onload = () => {
39+
vm.start();
40+
vm.loadProject(reader.result)
41+
.then(() => {
42+
// we add a `#loaded` div to our document, the integration suite
43+
// waits for that element to show up to assume the vm is ready
44+
// to play!
45+
const div = document.createElement('div');
46+
div.id='loaded';
47+
document.body.appendChild(div);
48+
});
49+
};
50+
reader.readAsArrayBuffer(thisFileInput.files[0]);
51+
});
2752
</script>
2853
</body>

test/integration/pick-tests.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
/* global vm, render, Promise */
2-
const {chromium} = require('playwright-chromium');
2+
const {Chromeless} = require('chromeless');
33
const test = require('tap').test;
44
const path = require('path');
5+
const chromeless = new Chromeless();
56

67
const indexHTML = path.resolve(__dirname, 'index.html');
78
const testDir = (...args) => path.resolve(__dirname, 'pick-tests', ...args);
89

9-
const runFile = async (file, action, page, script) => {
10+
const runFile = (file, action, script) =>
1011
// start each test by going to the index.html, and loading the scratch file
11-
await page.goto(`file://${indexHTML}`);
12-
const fileInput = await page.$('#file');
13-
await fileInput.setInputFiles(testDir(file));
14-
15-
await page.evaluate(() =>
16-
// `loadFile` is defined on the page itself.
17-
// eslint-disable-next-line no-undef
18-
loadFile()
19-
);
20-
return page.evaluate(`(function () {return (${script})(${action});})()`);
21-
};
12+
chromeless.goto(`file://${indexHTML}`)
13+
.setFileInput('#file', testDir(file))
14+
// the index.html handler for file input will add a #loaded element when it
15+
// finishes.
16+
.wait('#loaded')
17+
.evaluate(`function () {return (${script})(${action});}`)
18+
;
2219

2320
// immediately invoked async function to let us wait for each test to finish before starting the next.
2421
(async () => {
25-
const browser = await chromium.launch();
26-
const page = await browser.newPage();
2722

2823
const testOperation = async function (name, action, expect) {
2924
await test(name, async t => {
3025

31-
const results = await runFile('test-mouse-touch.sb2', action, page, boundAction => {
26+
const results = await runFile('test-mouse-touch.sb2', action, boundAction => {
3227
vm.greenFlag();
3328
const sendResults = [];
3429

@@ -102,5 +97,5 @@ const runFile = async (file, action, page, script) => {
10297
}
10398

10499
// close the browser window we used
105-
await browser.close();
100+
await chromeless.end();
106101
})();

test/integration/scratch-tests.js

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
11
/* global vm, Promise */
2-
const {chromium} = require('playwright-chromium');
2+
const {Chromeless} = require('chromeless');
33
const test = require('tap').test;
44
const path = require('path');
55
const fs = require('fs');
6+
const chromeless = new Chromeless();
67

78
const indexHTML = path.resolve(__dirname, 'index.html');
89
const testDir = (...args) => path.resolve(__dirname, 'scratch-tests', ...args);
910

10-
const testFile = (file, page) => test(file, async t => {
11+
const testFile = file => test(file, async t => {
1112
// start each test by going to the index.html, and loading the scratch file
12-
await page.goto(`file://${indexHTML}`);
13-
const fileInput = await page.$('#file');
14-
await fileInput.setInputFiles(testDir(file));
15-
await page.evaluate(() =>
16-
// `loadFile` is defined on the page itself.
17-
// eslint-disable-next-line no-undef
18-
loadFile()
19-
);
20-
const says = await page.evaluate(() => {
21-
// This function is run INSIDE the integration chrome browser via some
22-
// injection and .toString() magic. We can return some "simple data"
23-
// back across as a promise, so we will just log all the says that happen
24-
// for parsing after.
25-
26-
// this becomes the `says` in the outer scope
27-
const messages = [];
28-
const TIMEOUT = 5000;
29-
30-
vm.runtime.on('SAY', (_, __, message) => {
31-
messages.push(message);
32-
});
13+
const says = await chromeless.goto(`file://${indexHTML}`)
14+
.setFileInput('#file', testDir(file))
15+
// the index.html handler for file input will add a #loaded element when it
16+
// finishes.
17+
.wait('#loaded')
18+
.evaluate(() => {
19+
// This function is run INSIDE the integration chrome browser via some
20+
// injection and .toString() magic. We can return some "simple data"
21+
// back across as a promise, so we will just log all the says that happen
22+
// for parsing after.
23+
24+
// this becomes the `says` in the outer scope
25+
const messages = [];
26+
const TIMEOUT = 5000;
27+
28+
vm.runtime.on('SAY', (_, __, message) => {
29+
messages.push(message);
30+
});
3331

34-
vm.greenFlag();
35-
const startTime = Date.now();
36-
37-
return Promise.resolve()
38-
.then(async () => {
39-
// waiting for all threads to complete, then we return
40-
while (vm.runtime.threads.some(thread => vm.runtime.isActiveThread(thread))) {
41-
if ((Date.now() - startTime) >= TIMEOUT) {
42-
// if we push the message after end, the failure from tap is not very useful:
43-
// "not ok test after end() was called"
44-
messages.unshift(`fail Threads still running after ${TIMEOUT}ms`);
45-
break;
32+
vm.greenFlag();
33+
const startTime = Date.now();
34+
35+
return Promise.resolve()
36+
.then(async () => {
37+
// waiting for all threads to complete, then we return
38+
while (vm.runtime.threads.some(thread => vm.runtime.isActiveThread(thread))) {
39+
if ((Date.now() - startTime) >= TIMEOUT) {
40+
// if we push the message after end, the failure from tap is not very useful:
41+
// "not ok test after end() was called"
42+
messages.unshift(`fail Threads still running after ${TIMEOUT}ms`);
43+
break;
44+
}
45+
46+
await new Promise(resolve => setTimeout(resolve, 50));
4647
}
4748

48-
await new Promise(resolve => setTimeout(resolve, 50));
49-
}
50-
51-
return messages;
52-
});
53-
});
49+
return messages;
50+
});
51+
});
5452

5553
// Map string messages to tap reporting methods. This will be used
5654
// with events from scratch's runtime emitted on block instructions.
@@ -105,16 +103,13 @@ const testFile = (file, page) => test(file, async t => {
105103

106104
// immediately invoked async function to let us wait for each test to finish before starting the next.
107105
(async () => {
108-
const browser = await chromium.launch();
109-
const page = await browser.newPage();
110-
111106
const files = fs.readdirSync(testDir())
112107
.filter(uri => uri.endsWith('.sb2') || uri.endsWith('.sb3'));
113108

114109
for (const file of files) {
115-
await testFile(file, page);
110+
await testFile(file);
116111
}
117112

118113
// close the browser window we used
119-
await browser.close();
114+
await chromeless.end();
120115
})();

0 commit comments

Comments
 (0)