mirror of
https://github.com/google/blockly.git
synced 2026-01-25 09:40:10 +01:00
Workspace svg tests (#3857)
* Converting workspace svg tests to mocha and calling workspace tests. * Removing jsunit style test for workspace_svg. * Fix eslint error.
This commit is contained in:
@@ -8,10 +8,12 @@
|
||||
"sinon": false,
|
||||
"assertArrayEquals": true,
|
||||
"assertVariableValues": true,
|
||||
"captureWarnings": true,
|
||||
"createTestBlock": true,
|
||||
"defineRowBlock": true,
|
||||
"defineStackBlock": true,
|
||||
"defineStatementBlock": true
|
||||
"defineStatementBlock": true,
|
||||
"testAWorkspace": true
|
||||
},
|
||||
"extends": "../../.eslintrc.json"
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<script src="../../generators/python.js"></script>
|
||||
<script src="../../msg/messages.js"></script>
|
||||
<script src="../../blocks/procedures.js"></script>
|
||||
<script src="../../blocks/math.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -71,6 +72,7 @@
|
||||
<script src="variables_test.js"></script>
|
||||
<script src="widget_div_test.js"></script>
|
||||
<script src="workspace_test.js"></script>
|
||||
<script src="workspace_svg_test.js"></script>
|
||||
<script src="workspace_comment_test.js"></script>
|
||||
<script src="xml_procedures_test.js"></script>
|
||||
<script src="xml_test.js"></script>
|
||||
|
||||
@@ -26,26 +26,6 @@ suite('JSON Block Definitions', function() {
|
||||
this.workspace_.dispose();
|
||||
});
|
||||
|
||||
/**
|
||||
* Captures the strings sent to console.warn() when calling a function.
|
||||
* @param {function} innerFunc The function where warnings may called.
|
||||
* @return {string[]} The warning messages (only the first arguments).
|
||||
*/
|
||||
function captureWarnings(innerFunc) {
|
||||
var msgs = [];
|
||||
var nativeConsoleWarn = console.warn;
|
||||
try {
|
||||
console.warn = function(msg) {
|
||||
msgs.push(msg);
|
||||
nativeConsoleWarn.apply(console, arguments);
|
||||
};
|
||||
innerFunc();
|
||||
} finally {
|
||||
console.warn = nativeConsoleWarn;
|
||||
}
|
||||
return msgs;
|
||||
}
|
||||
|
||||
suite('defineBlocksWithJsonArray', function() {
|
||||
test('Basic block', function() {
|
||||
/** Ensure a block can be instantiated from a JSON definition. */
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/* exported assertArrayEquals, assertVariableValues, defineRowBlock,
|
||||
defineStackBlock, defineStatementBlock, createTestBlock */
|
||||
/* exported assertArrayEquals, assertVariableValues, captureWarnings
|
||||
defineRowBlock, defineStackBlock, defineStatementBlock, createTestBlock */
|
||||
|
||||
/**
|
||||
* Check that two arrays have the same content.
|
||||
@@ -40,6 +40,25 @@ function assertVariableValues(container, name, type, id) {
|
||||
chai.assert.equal(variable.getId(), id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Captures the strings sent to console.warn() when calling a function.
|
||||
* @param {function} innerFunc The function where warnings may called.
|
||||
* @return {string[]} The warning messages (only the first arguments).
|
||||
*/
|
||||
function captureWarnings(innerFunc) {
|
||||
var msgs = [];
|
||||
var nativeConsoleWarn = console.warn;
|
||||
try {
|
||||
console.warn = function(msg) {
|
||||
msgs.push(msg);
|
||||
};
|
||||
innerFunc();
|
||||
} finally {
|
||||
console.warn = nativeConsoleWarn;
|
||||
}
|
||||
return msgs;
|
||||
}
|
||||
|
||||
function defineStackBlock() {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "stack_block",
|
||||
|
||||
88
tests/mocha/workspace_svg_test.js
Normal file
88
tests/mocha/workspace_svg_test.js
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2020 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
suite('WorkspaceSvg', function() {
|
||||
setup(function() {
|
||||
var toolbox = document.getElementById('toolbox-categories');
|
||||
this.workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
'type': 'simple_test_block',
|
||||
'message0': 'simple test block',
|
||||
'output': null
|
||||
},
|
||||
{
|
||||
'type': 'test_val_in',
|
||||
'message0': 'test in %1',
|
||||
'args0': [
|
||||
{
|
||||
'type': 'input_value',
|
||||
'name': 'NAME'
|
||||
}
|
||||
]
|
||||
}]);
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
delete Blockly.Blocks['simple_test_block'];
|
||||
delete Blockly.Blocks['test_val_in'];
|
||||
this.workspace.dispose();
|
||||
});
|
||||
|
||||
test('appendDomToWorkspace alignment', function() {
|
||||
var dom = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="math_random_float" inline="true" x="21" y="23">' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
Blockly.Xml.appendDomToWorkspace(dom, this.workspace);
|
||||
chai.assert.equal(this.workspace.getAllBlocks(false).length, 1,
|
||||
'Block count');
|
||||
Blockly.Xml.appendDomToWorkspace(dom, this.workspace);
|
||||
chai.assert.equal(this.workspace.getAllBlocks(false).length, 2,
|
||||
'Block count');
|
||||
var blocks = this.workspace.getAllBlocks(false);
|
||||
chai.assert.equal(blocks[0].getRelativeToSurfaceXY().x, 21,
|
||||
'Block 1 position x');
|
||||
chai.assert.equal(blocks[0].getRelativeToSurfaceXY().y, 23,
|
||||
'Block 1 position y');
|
||||
chai.assert.equal(blocks[1].getRelativeToSurfaceXY().x, 21,
|
||||
'Block 2 position x');
|
||||
// Y separation value defined in appendDomToWorkspace as 10
|
||||
chai.assert.equal(blocks[1].getRelativeToSurfaceXY().y,
|
||||
23 + blocks[0].getHeightWidth().height + 10,
|
||||
'Block 2 position y');
|
||||
});
|
||||
|
||||
test('Replacing shadow disposes svg', function() {
|
||||
var dom = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="test_val_in">' +
|
||||
'<value name="NAME">' +
|
||||
'<shadow type="simple_test_block"></shadow>' +
|
||||
'</value>' +
|
||||
'</block>' +
|
||||
'</xml>');
|
||||
|
||||
Blockly.Xml.appendDomToWorkspace(dom, this.workspace);
|
||||
var blocks = this.workspace.getAllBlocks(false);
|
||||
chai.assert.equal(blocks.length, 2,'Block count');
|
||||
var shadowBlock = blocks[1];
|
||||
chai.assert.exists(shadowBlock.getSvgRoot());
|
||||
|
||||
var block = this.workspace.newBlock('simple_test_block');
|
||||
block.initSvg();
|
||||
|
||||
var inputConnection =
|
||||
this.workspace.getTopBlocks()[0].getInput('NAME').connection;
|
||||
inputConnection.connect(block.outputConnection);
|
||||
chai.assert.exists(block.getSvgRoot());
|
||||
chai.assert.notExists(shadowBlock.getSvgRoot());
|
||||
});
|
||||
|
||||
suite('Workspace Base class', function() {
|
||||
testAWorkspace();
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,18 @@
|
||||
suite('Workspace', function() {
|
||||
setup(function() {
|
||||
this.workspace = new Blockly.Workspace();
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
this.workspace.dispose();
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
testAWorkspace();
|
||||
});
|
||||
|
||||
function testAWorkspace() {
|
||||
setup(function() {
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
"type": "get_var_block",
|
||||
"message0": "%1",
|
||||
@@ -22,7 +34,6 @@ suite('Workspace', function() {
|
||||
|
||||
teardown(function() {
|
||||
delete Blockly.Blocks['get_var_block'];
|
||||
this.workspace.dispose();
|
||||
// Clear Blockly.Event state.
|
||||
Blockly.Events.setGroup(false);
|
||||
Blockly.Events.disabled_ = 0;
|
||||
@@ -826,7 +837,12 @@ suite('Workspace', function() {
|
||||
test('Delete same variable twice no usages', function() {
|
||||
this.workspace.createVariable('name1', 'type1', 'id1');
|
||||
this.workspace.deleteVariableById('id1');
|
||||
this.workspace.deleteVariableById('id1');
|
||||
var workspace = this.workspace;
|
||||
var warnings = captureWarnings(function() {
|
||||
workspace.deleteVariableById('id1');
|
||||
});
|
||||
chai.assert.equal(warnings.length, 1,
|
||||
'Expected 1 warning for second deleteVariableById call.');
|
||||
|
||||
// Check the undoStack only recorded one delete event.
|
||||
var undoStack = this.workspace.undoStack_;
|
||||
@@ -850,7 +866,12 @@ suite('Workspace', function() {
|
||||
this.workspace.createVariable('name1', 'type1', 'id1');
|
||||
createVarBlocksNoEvents(this.workspace, ['id1']);
|
||||
this.workspace.deleteVariableById('id1');
|
||||
this.workspace.deleteVariableById('id1');
|
||||
var workspace = this.workspace;
|
||||
var warnings = captureWarnings(function() {
|
||||
workspace.deleteVariableById('id1');
|
||||
});
|
||||
chai.assert.equal(warnings.length, 1,
|
||||
'Expected 1 warning for second deleteVariableById call.');
|
||||
|
||||
// Check the undoStack only recorded one delete event.
|
||||
var undoStack = this.workspace.undoStack_;
|
||||
@@ -1052,4 +1073,4 @@ suite('Workspace', function() {
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ h1 {
|
||||
<script src="../mocha/test_helpers.js"></script>
|
||||
|
||||
<script src="../jsunit/test_utilities.js"></script>
|
||||
<script src="workspace_svg_test.js"></script>
|
||||
<script src="procedure_svg_test.js"></script>
|
||||
<script src="event_svg_test.js"></script>
|
||||
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2017 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
function helper_setUpMockBlocks() {
|
||||
// TODO: Replace with defineGetVarBlock();
|
||||
Blockly.defineBlocksWithJsonArray([{
|
||||
'type': 'field_variable_test_block',
|
||||
'message0': '%1',
|
||||
'args0': [
|
||||
{
|
||||
'type': 'field_variable',
|
||||
'name': 'VAR',
|
||||
'variable': 'item'
|
||||
}
|
||||
],
|
||||
},
|
||||
{
|
||||
'type': 'simple_test_block',
|
||||
'message0': 'simple test block',
|
||||
'output': null
|
||||
},
|
||||
{
|
||||
'type': 'test_val_in',
|
||||
'message0': 'test in %1',
|
||||
'args0': [
|
||||
{
|
||||
'type': 'input_value',
|
||||
'name': 'NAME'
|
||||
}
|
||||
]
|
||||
}]);
|
||||
}
|
||||
|
||||
function helper_tearDownMockBlocks() {
|
||||
delete Blockly.Blocks['field_variable_test_block'];
|
||||
delete Blockly.Blocks['simple_test_block'];
|
||||
delete Blockly.Blocks['test_val_in'];
|
||||
}
|
||||
|
||||
function helper_createWorkspaceWithToolbox() {
|
||||
var toolbox = document.getElementById('toolbox-categories');
|
||||
return Blockly.inject('blocklyDiv', {toolbox: toolbox});
|
||||
}
|
||||
|
||||
function helper_createNewBlock(workspace, type) {
|
||||
var block = workspace.newBlock(type);
|
||||
block.initSvg();
|
||||
return block;
|
||||
}
|
||||
|
||||
function test_createWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
workspace.dispose();
|
||||
}
|
||||
|
||||
function test_emptyWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
try {
|
||||
assertEquals('Empty workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (3).', 0, workspace.getAllBlocks(false).length);
|
||||
workspace.clear();
|
||||
assertEquals('Empty workspace (4).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Empty workspace (5).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Empty workspace (6).', 0, workspace.getAllBlocks(false).length);
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_flatWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
var blockA, blockB;
|
||||
try {
|
||||
blockA = helper_createNewBlock(workspace, '');
|
||||
assertEquals('One block workspace (1).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (2).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (3).', 1, workspace.getAllBlocks(false).length);
|
||||
blockB = helper_createNewBlock(workspace, '');
|
||||
assertEquals('Two block workspace (1).', 2, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Two block workspace (2).', 2, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Two block workspace (3).', 2, workspace.getAllBlocks(false).length);
|
||||
try {
|
||||
blockA.dispose();
|
||||
} catch (e) {
|
||||
fail('Failed to delete blockA ' + e);
|
||||
}
|
||||
|
||||
assertEquals('One block workspace (4).', 1, workspace.getTopBlocks(true).length);
|
||||
assertEquals('One block workspace (5).', 1, workspace.getTopBlocks(false).length);
|
||||
assertEquals('One block workspace (6).', 1, workspace.getAllBlocks(false).length);
|
||||
workspace.clear();
|
||||
assertEquals('Cleared workspace (1).', 0, workspace.getTopBlocks(true).length);
|
||||
assertEquals('Cleared workspace (2).', 0, workspace.getTopBlocks(false).length);
|
||||
assertEquals('Cleared workspace (3).', 0, workspace.getAllBlocks(false).length);
|
||||
} finally {
|
||||
blockB && blockB.dispose();
|
||||
blockA && blockA.dispose();
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/** Tests the alignment of appendDomToWorkspace with WorkspaceSvg. */
|
||||
function test_appendDomToWorkspace() {
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
try {
|
||||
var dom = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
' <block type="math_random_float" inline="true" x="21" y="23">' +
|
||||
' </block>' +
|
||||
'</xml>');
|
||||
Blockly.Xml.appendDomToWorkspace(dom, workspace);
|
||||
assertEquals('Block count', 1, workspace.getAllBlocks(false).length);
|
||||
Blockly.Xml.appendDomToWorkspace(dom, workspace);
|
||||
assertEquals('Block count', 2, workspace.getAllBlocks(false).length);
|
||||
var blocks = workspace.getAllBlocks(false);
|
||||
assertEquals('Block 1 position x',21,blocks[0].getRelativeToSurfaceXY().x);
|
||||
assertEquals('Block 1 position y',23,blocks[0].getRelativeToSurfaceXY().y);
|
||||
assertEquals('Block 2 position x',21,blocks[1].getRelativeToSurfaceXY().x);
|
||||
assertEquals('Block 2 position y',23 + blocks[0].getHeightWidth().height + Blockly.BlockSvg.SEP_SPACE_Y,blocks[1].getRelativeToSurfaceXY().y);
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
function test_svgDisposeWithShadow() {
|
||||
helper_setUpMockBlocks();
|
||||
var workspace = helper_createWorkspaceWithToolbox();
|
||||
var blockNew;
|
||||
try {
|
||||
var dom = Blockly.Xml.textToDom(
|
||||
'<xml xmlns="https://developers.google.com/blockly/xml">' +
|
||||
'<block type="test_val_in">' +
|
||||
'<value name="NAME">' +
|
||||
'<shadow type="simple_test_block"></shadow>' +
|
||||
'</value>' +
|
||||
'</block>' +
|
||||
'</xml>');
|
||||
|
||||
Blockly.Xml.appendDomToWorkspace(dom, workspace);
|
||||
assertEquals('Block count', 2, workspace.getAllBlocks(false).length);
|
||||
var inputConnection = workspace.getTopBlocks()[0].getInput('NAME').connection;
|
||||
|
||||
blockNew = helper_createNewBlock(workspace, 'simple_test_block');
|
||||
inputConnection.connect(blockNew.outputConnection);
|
||||
|
||||
} finally {
|
||||
workspace.dispose();
|
||||
blockNew && blockNew.dispose();
|
||||
helper_tearDownMockBlocks();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user