2020-11-09 16:41:55 -05:00
|
|
|
/* global vm, render */
|
2020-05-07 16:21:37 -04:00
|
|
|
const {chromium} = require('playwright-chromium');
|
2018-08-07 10:56:28 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const path = require('path');
|
|
|
|
|
|
|
|
const indexHTML = path.resolve(__dirname, 'index.html');
|
|
|
|
const testDir = (...args) => path.resolve(__dirname, 'pick-tests', ...args);
|
|
|
|
|
2020-01-07 14:46:34 -05:00
|
|
|
const runFile = async (file, action, page, script) => {
|
2018-08-07 10:56:28 -04:00
|
|
|
// start each test by going to the index.html, and loading the scratch file
|
2020-01-07 14:46:34 -05:00
|
|
|
await page.goto(`file://${indexHTML}`);
|
|
|
|
const fileInput = await page.$('#file');
|
2020-05-07 16:21:37 -04:00
|
|
|
await fileInput.setInputFiles(testDir(file));
|
2020-03-08 08:12:40 -04:00
|
|
|
|
|
|
|
await page.evaluate(() =>
|
|
|
|
// `loadFile` is defined on the page itself.
|
|
|
|
// eslint-disable-next-line no-undef
|
|
|
|
loadFile()
|
|
|
|
);
|
2020-01-07 14:46:34 -05:00
|
|
|
return page.evaluate(`(function () {return (${script})(${action});})()`);
|
|
|
|
};
|
2018-08-07 10:56:28 -04:00
|
|
|
|
|
|
|
// immediately invoked async function to let us wait for each test to finish before starting the next.
|
|
|
|
(async () => {
|
2020-05-07 16:21:37 -04:00
|
|
|
const browser = await chromium.launch();
|
2020-01-07 14:46:34 -05:00
|
|
|
const page = await browser.newPage();
|
2018-08-07 10:56:28 -04:00
|
|
|
|
2018-10-04 11:49:56 -04:00
|
|
|
const testOperation = async function (name, action, expect) {
|
|
|
|
await test(name, async t => {
|
|
|
|
|
2020-01-07 14:46:34 -05:00
|
|
|
const results = await runFile('test-mouse-touch.sb2', action, page, boundAction => {
|
2018-10-04 11:49:56 -04:00
|
|
|
vm.greenFlag();
|
|
|
|
const sendResults = [];
|
|
|
|
|
|
|
|
const idToTargetName = id => {
|
|
|
|
const target = vm.runtime.targets.find(tar => tar.drawableID === id);
|
|
|
|
if (!target) {
|
|
|
|
return `[Unknown drawableID: ${id}]`;
|
|
|
|
}
|
|
|
|
return target.sprite.name;
|
|
|
|
};
|
|
|
|
const sprite = vm.runtime.targets.find(target => target.sprite.name === 'Sprite1');
|
|
|
|
|
|
|
|
boundAction({
|
|
|
|
sendResults,
|
|
|
|
idToTargetName,
|
|
|
|
render,
|
|
|
|
sprite
|
|
|
|
});
|
|
|
|
return sendResults;
|
|
|
|
});
|
|
|
|
|
|
|
|
t.plan(expect.length);
|
|
|
|
for (let x = 0; x < expect.length; x++) {
|
|
|
|
t.deepEqual(results[x], expect[x], expect[x][0]);
|
|
|
|
}
|
|
|
|
t.end();
|
2018-08-07 10:56:28 -04:00
|
|
|
});
|
2018-10-04 11:49:56 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const tests = [
|
|
|
|
{
|
|
|
|
name: 'pick Sprite1',
|
|
|
|
action: ({sendResults, render, idToTargetName}) => {
|
2018-10-04 12:10:50 -04:00
|
|
|
sendResults.push(['center', idToTargetName(render.pick(360, 180))]);
|
2018-10-04 11:49:56 -04:00
|
|
|
},
|
|
|
|
expect: [['center', 'Sprite1']]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'pick Stage',
|
|
|
|
action: ({sendResults, render, idToTargetName}) => {
|
2018-10-04 12:10:50 -04:00
|
|
|
sendResults.push(['left', idToTargetName(render.pick(320, 180))]);
|
2018-10-04 11:49:56 -04:00
|
|
|
},
|
|
|
|
expect: [['left', 'Stage']]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'touching Sprite1',
|
|
|
|
action: ({sprite, sendResults, render}) => {
|
2018-10-04 12:10:50 -04:00
|
|
|
sendResults.push(['over', render.drawableTouching(sprite.drawableID, 360, 180)]);
|
2018-10-04 11:49:56 -04:00
|
|
|
},
|
|
|
|
expect: [['over', true]]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'pick Stage through hidden Sprite1',
|
|
|
|
action: ({sprite, sendResults, render, idToTargetName}) => {
|
|
|
|
sprite.setVisible(false);
|
2018-10-04 12:10:50 -04:00
|
|
|
sendResults.push(['hidden sprite pick center', idToTargetName(render.pick(360, 180))]);
|
2018-10-04 11:49:56 -04:00
|
|
|
},
|
|
|
|
expect: [['hidden sprite pick center', 'Stage']]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'touching hidden Sprite1',
|
|
|
|
action: ({sprite, sendResults, render}) => {
|
|
|
|
sprite.setVisible(false);
|
2018-10-04 12:10:50 -04:00
|
|
|
sendResults.push(['hidden over', render.drawableTouching(sprite.drawableID, 360, 180)]);
|
2018-10-04 11:49:56 -04:00
|
|
|
},
|
|
|
|
expect: [['hidden over', true]]
|
2018-08-07 10:56:28 -04:00
|
|
|
}
|
2018-10-04 11:49:56 -04:00
|
|
|
];
|
|
|
|
for (const {name, action, expect} of tests) {
|
|
|
|
await testOperation(name, action, expect);
|
|
|
|
}
|
2018-08-07 10:56:28 -04:00
|
|
|
|
|
|
|
// close the browser window we used
|
2020-01-07 14:46:34 -05:00
|
|
|
await browser.close();
|
2020-05-08 16:05:56 -04:00
|
|
|
})().catch(err => {
|
|
|
|
// Handle promise rejections by exiting with a nonzero code to ensure that tests don't erroneously pass
|
|
|
|
// eslint-disable-next-line no-console
|
2020-05-11 15:38:11 -04:00
|
|
|
console.error(err.message);
|
2020-05-08 16:05:56 -04:00
|
|
|
process.exit(1);
|
|
|
|
});
|