Skip to content

Commit

Permalink
chore: add tests for disabling blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Jul 11, 2023
1 parent cae721e commit e1306f9
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 31 deletions.
135 changes: 107 additions & 28 deletions tests/browser/test/basic_playground_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const {
testSetup,
testFileLocations,
dragNthBlockFromFlyout,
dragBlockTypeFromFlyout,
connect,
contextMenuSelect,
} = require('./test_setup');

Expand All @@ -22,14 +24,15 @@ async function getIsCollapsed(browser, blockId) {
}, blockId);
}

async function getIsEnabled(browser, blockId) {
async function getIsDisabled(browser, blockId) {
return await browser.execute((blockId) => {
return Blockly.getMainWorkspace().getBlockById(blockId).isEnabled();
const block = Blockly.getMainWorkspace().getBlockById(blockId);
return !block.isEnabled() || block.getInheritedDisabled();
}, blockId);
}

let browser;
suite('Testing Connecting Blocks', function (done) {
suite('Testing Connecting Blocks', function () {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

Expand Down Expand Up @@ -64,7 +67,7 @@ suite('Testing Connecting Blocks', function (done) {
});
});

suite('Right Clicking on Blocks', function (done) {
suite('Right Clicking on Blocks', function () {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

Expand All @@ -76,16 +79,8 @@ suite('Right Clicking on Blocks', function (done) {
test('Collapse', async function () {
await browser.refresh();
const block = await dragNthBlockFromFlyout(browser, 'Loops', 0, 20, 20);

const blockId = block.id;
let isCollapsed = await getIsCollapsed(browser, blockId);
chai.assert.isFalse(isCollapsed);

await contextMenuSelect(browser, block, 'Collapse Block');

isCollapsed = await getIsCollapsed(browser, blockId);

chai.assert.isTrue(isCollapsed);
chai.assert.isTrue(await getIsCollapsed(browser, block.id));
});

test('Expand', async function () {
Expand All @@ -95,42 +90,126 @@ suite('Right Clicking on Blocks', function (done) {
await contextMenuSelect(browser, block, 'Collapse Block');
await contextMenuSelect(browser, block, 'Expand Block');

// Verify.
const blockId = block.id;
const isCollapsed = await getIsCollapsed(browser, blockId);
chai.assert.isFalse(isCollapsed);
chai.assert.isFalse(await getIsCollapsed(browser, block.id));
});

test('Disable', async function () {
await browser.refresh();
const block = await dragNthBlockFromFlyout(browser, 'Loops', 0, 20, 20);

const blockId = block.id;
let isEnabled = await getIsEnabled(browser, blockId);
chai.assert.isTrue(isEnabled);

await contextMenuSelect(browser, block, 'Disable Block');

await new Promise((resolve) => setTimeout(resolve, 2000)); // 2 sec

isEnabled = await getIsEnabled(browser, blockId);
chai.assert.isFalse(isEnabled);
chai.assert.isTrue(await getIsDisabled(browser, block.id));
});

test('Enable', async function () {
await browser.refresh();
const block = await dragNthBlockFromFlyout(browser, 'Loops', 0, 20, 20);

const blockId = block.id;
await contextMenuSelect(browser, block, 'Disable Block');
await contextMenuSelect(browser, block, 'Enable Block');

const isEnabled = await getIsEnabled(browser, blockId);
chai.assert.isTrue(isEnabled);
chai.assert.isFalse(await getIsDisabled(browser, block.id));
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
await browser.deleteSession();
});
});

suite('Disabling', function () {
// Setting timeout to unlimited as the webdriver takes a longer
// time to run than most mocha tests.
this.timeout(0);

suiteSetup(async function () {
browser = await testSetup(testFileLocations.playground);
});

setup(async function () {
await browser.refresh();
});

test(
'children connected to value inputs are disabled when the ' +
'parent is diabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
10,
10
);
const child = await dragBlockTypeFromFlyout(
browser,
'Logic',
'logic_boolean',
110,
110
);
await connect(browser, child, 'OUTPUT', parent, 'IF0');

await contextMenuSelect(browser, parent, 'Disable Block');

chai.assert.isTrue(await getIsDisabled(browser, child.id));
}
);

test(
'children connected to statement inputs are disabled when the ' +
'parent is disabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
10,
10
);
const child = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
110,
110
);
await connect(browser, child, 'PREVIOUS', parent, 'IF0');

await contextMenuSelect(browser, parent, 'Disable Block');

chai.assert.isTrue(await getIsDisabled(browser, child.id));
}
);

test(
'children connected to next connections are not disabled when the ' +
'parent is disabled',
async function () {
const parent = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
10,
10
);
const child = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
110,
110
);
await connect(browser, child, 'PREVIOUS', parent, 'NEXT');

await contextMenuSelect(browser, parent, 'Disable Block');

chai.assert.isFalse(await getIsDisabled(browser, child.id));
}
);

suiteTeardown(async function () {
await browser.deleteSession();
});
});
15 changes: 12 additions & 3 deletions tests/browser/test/test_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,19 @@ async function dragBlockTypeFromFlyout(browser, categoryName, type, x, y) {
}

async function contextMenuSelect(browser, block, itemText) {
await block.click({button: 2});
await browser.pause(200);
// Clicking will always happen in the middle of the block's bounds
// (including children) by default, which causes problems if it has holes
// (e.g. statement inputs).
const xOffset = Math.round((await block.getSize('width')) * 0.3);
const yOffset = Math.round((await block.getSize('height')) * 0.45);

await block.click({button: 2, x: -xOffset, y: -yOffset});
await browser.pause(100);

const item = await browser.$(`div=${itemText}`);
await item.click();
await browser.pause(200);

await browser.pause(100);
}

module.exports = {
Expand All @@ -249,6 +257,7 @@ module.exports = {
getNthBlockOfCategory,
getBlockTypeFromCategory,
dragNthBlockFromFlyout,
dragBlockTypeFromFlyout,
connect,
switchRTL,
contextMenuSelect,
Expand Down

0 comments on commit e1306f9

Please sign in to comment.