Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create test for mutator #7278

Merged
merged 15 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions tests/browser/test/mutator_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Node.js script to run Automated tests in Chrome, via webdriver.
*/

const chai = require('chai');
const {
testSetup,
testFileLocations,
connect,
switchRTL,
dragBlockTypeFromFlyout,
getSelectedBlockId,
screenDirection,
} = require('./test_setup');

let browser;
suite('This tests mutating a Blockly block', function (done) {
// Setting timeout to unlimited as the webdriver takes a longer time to run than most mocha test
this.timeout(0);

// Setup Selenium for all of the tests
suiteSetup(async function () {
browser = await testSetup(testFileLocations.PLAYGROUND);
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
});

test('This test mutating a block creates more inputs', async function () {
await testingMutator(screenDirection.LTR);
});

// Teardown entire suite after test are done running
suiteTeardown(async function () {
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
await browser.deleteSession();
});
});

async function testingMutator(delta) {
// Drag out print from flyout.
const controlIfFlyout = await dragBlockTypeFromFlyout(
browser,
'Logic',
'controls_if',
delta * 50,
50
);
// Click on the mutator and drag out else ig block
const mutatorWheel = await browser.$(
'#blocklyDiv > div > svg.blocklySvg > g > g.blocklyBlockCanvas > g.blocklyDraggable.blocklySelected > g.blocklyIconGroup'
);
await mutatorWheel.click();
await browser.pause(100);
const elseIfFlyout = await browser.$(
'#blocklyDiv > div > svg.blocklySvg > g > g.blocklyBubbleCanvas > g > g:nth-child(2) > svg:nth-child(1) > g > g.blocklyFlyout > g > g.blocklyBlockCanvas > g:nth-child(3)'
);
await elseIfFlyout.dragAndDrop({x: delta * 50, y: 42});
// Get the original number of mutator inputs
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
await browser.pause(100);

// Get the ids for block before mutating
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
const originalInputs = await browser.execute(() => {
const originalInputs =
Blockly.getMainWorkspace().getAllBlocks()[0].inputList.length;
return originalInputs;
});

await browser.pause(100);
// Get the ids for the blocks in the mutator
const blockIds = await browser.execute(() => {
const mutatorBlock = Blockly.getMainWorkspace().getAllBlocks()[0];
// Adding the first element in the array is the original block id, the second is the first mutator block, and the third is the second mutator block
const blockIds = [
mutatorBlock.id,
mutatorBlock.mutator.getWorkspace().getAllBlocks()[0].id,
mutatorBlock.mutator.getWorkspace().getAllBlocks()[1].id,
];
return blockIds;
});

// The flyout block and the workspace block have the same id, so to get around that I pass in the selector to the connect function
const dragBlockSelector = await browser.$(
'#blocklyDiv > div > svg.blocklySvg > g > g.blocklyBubbleCanvas > g > g:nth-child(2) > svg:nth-child(1) > g > g.blocklyBlockCanvas > g.blocklyDraggable'
);
// For some reason this needs a lot more time
await browser.pause(2000);
// Connect the mutator blocks
await connect(
browser,
blockIds[2],
'PREVIOUS',
blockIds[1],
'NEXT',
blockIds[0],
dragBlockSelector
);

// For some reason this needs a lot more time
await browser.pause(200);

// Get the ids for block after mutating
const afterInputs = await browser.execute(() => {
const afterInputs =
Blockly.getMainWorkspace().getAllBlocks()[0].inputList.length;
return afterInputs;
});

chai.assert.isTrue(afterInputs > originalInputs);
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
}
74 changes: 57 additions & 17 deletions tests/browser/test/test_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,27 @@ async function getBlockTypeFromWorkspace(browser, blockType, position) {
* @param connectionName Which connection to return. An input name to
* get a value or statement connection, and otherwise the type of
* the connection.
* @param mutatorBlockId The block that holds the mutator icon
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
* @return A Promise that resolves to the location of the specific
* connection in screen coordinates.
*/
async function getLocationOfBlockConnection(browser, id, connectionName) {
async function getLocationOfBlockConnection(
browser,
id,
connectionName,
mutatorBlockId
) {
return await browser.execute(
(id, connectionName) => {
const block = Blockly.getMainWorkspace().getBlockById(id);
(id, connectionName, mutatorBlockId) => {
let block;
if (mutatorBlockId) {
block = Blockly.getMainWorkspace()
.getBlockById(mutatorBlockId)
.mutator.getWorkspace()
.getBlockById(id);
} else {
block = Blockly.getMainWorkspace().getBlockById(id);
}

let connection;
switch (connectionName) {
Expand Down Expand Up @@ -237,7 +251,8 @@ async function getLocationOfBlockConnection(browser, id, connectionName) {
);
},
id,
connectionName
connectionName,
mutatorBlockId
);
}

Expand All @@ -249,32 +264,57 @@ async function getLocationOfBlockConnection(browser, id, connectionName) {
* @param draggedConnection The active connection on the block being dragged.
* @param targetBlock The block to drag to.
* @param targetConnection The connection to connect to on the target block.
* @param mutatorBlockId The block that holds the mutator icon
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
* @param dragBlockSelector The selector of the block to drag
ericblackmonGoogle marked this conversation as resolved.
Show resolved Hide resolved
* @return A Promise that resolves when the actions are completed.
*/
async function connect(
browser,
draggedBlock,
draggedConnection,
targetBlock,
targetConnection
targetConnection,
mutatorBlockId,
dragBlockSelector
) {
const draggedLocation = await getLocationOfBlockConnection(
browser,
draggedBlock.id,
draggedConnection
);
const targetLocation = await getLocationOfBlockConnection(
browser,
targetBlock.id,
targetConnection
);
let draggedLocation;
let targetLocation;

if (mutatorBlockId) {
draggedLocation = await getLocationOfBlockConnection(
browser,
draggedBlock,
draggedConnection,
mutatorBlockId
);
targetLocation = await getLocationOfBlockConnection(
browser,
targetBlock,
targetConnection,
mutatorBlockId
);
} else {
draggedLocation = await getLocationOfBlockConnection(
browser,
draggedBlock.id,
draggedConnection
);
targetLocation = await getLocationOfBlockConnection(
browser,
targetBlock.id,
targetConnection
);
}

const delta = {
x: targetLocation.x - draggedLocation.x,
y: targetLocation.y - draggedLocation.y,
};
await draggedBlock.dragAndDrop(delta);
await browser.pause(200);
if (mutatorBlockId) {
await dragBlockSelector.dragAndDrop(delta);
} else {
await draggedBlock.dragAndDrop(delta);
}
}

/**
Expand Down