diff --git a/tests/mocha/.eslintrc.json b/tests/mocha/.eslintrc.json
index 17038e164..f80d810dc 100644
--- a/tests/mocha/.eslintrc.json
+++ b/tests/mocha/.eslintrc.json
@@ -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"
}
diff --git a/tests/mocha/index.html b/tests/mocha/index.html
index e97e1bfbc..ff94913bb 100644
--- a/tests/mocha/index.html
+++ b/tests/mocha/index.html
@@ -13,6 +13,7 @@
+
@@ -71,6 +72,7 @@
+
diff --git a/tests/mocha/json_test.js b/tests/mocha/json_test.js
index 290c7ced4..4e9ca9501 100644
--- a/tests/mocha/json_test.js
+++ b/tests/mocha/json_test.js
@@ -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. */
diff --git a/tests/mocha/test_helpers.js b/tests/mocha/test_helpers.js
index af4624174..c4a08c0d4 100644
--- a/tests/mocha/test_helpers.js
+++ b/tests/mocha/test_helpers.js
@@ -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",
diff --git a/tests/mocha/workspace_svg_test.js b/tests/mocha/workspace_svg_test.js
new file mode 100644
index 000000000..ad61e6d15
--- /dev/null
+++ b/tests/mocha/workspace_svg_test.js
@@ -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(
+ '' +
+ ' ' +
+ ' ' +
+ '');
+ 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(
+ '' +
+ '' +
+ '' +
+ '' +
+ '' +
+ '' +
+ '');
+
+ 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();
+ });
+});
diff --git a/tests/mocha/workspace_test.js b/tests/mocha/workspace_test.js
index 7a20a63aa..74591b0f0 100644
--- a/tests/mocha/workspace_test.js
+++ b/tests/mocha/workspace_test.js
@@ -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() {
});
});
});
-});
+}
diff --git a/tests/workspace_svg/index.html b/tests/workspace_svg/index.html
index 9443c68a4..9c7f4ad4a 100644
--- a/tests/workspace_svg/index.html
+++ b/tests/workspace_svg/index.html
@@ -57,7 +57,6 @@ h1 {
-
diff --git a/tests/workspace_svg/workspace_svg_test.js b/tests/workspace_svg/workspace_svg_test.js
deleted file mode 100644
index 1a5e9fa4b..000000000
--- a/tests/workspace_svg/workspace_svg_test.js
+++ /dev/null
@@ -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(
- '' +
- ' ' +
- ' ' +
- '');
- 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(
- '' +
- '' +
- '' +
- '' +
- '' +
- '' +
- '');
-
- 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();
- }
-}