diff --git a/tests/mocha/.eslintrc.json b/tests/mocha/.eslintrc.json index f80d810dc..6caceffa4 100644 --- a/tests/mocha/.eslintrc.json +++ b/tests/mocha/.eslintrc.json @@ -13,6 +13,7 @@ "defineRowBlock": true, "defineStackBlock": true, "defineStatementBlock": true, + "createEventsFireStub": true, "testAWorkspace": true }, "extends": "../../.eslintrc.json" diff --git a/tests/mocha/event_test.js b/tests/mocha/event_test.js index 0a1b39109..ef04f3e67 100644 --- a/tests/mocha/event_test.js +++ b/tests/mocha/event_test.js @@ -555,22 +555,11 @@ suite('Events', function() { }); suite('Firing', function() { - function temporary_fireEvent(event) { - if (!Blockly.Events.isEnabled()) { - return; - } - Blockly.Events.FIRE_QUEUE_.push(event); - Blockly.Events.fireNow_(); - } - setup(function() { - this.savedFireFunc_ = Blockly.Events.fire; - Blockly.Events.fire = temporary_fireEvent; - temporary_fireEvent.firedEvents_ = []; + createEventsFireStub(); }); teardown(function() { - Blockly.Events.fire = this.savedFireFunc_; sinon.restore(); }); @@ -578,7 +567,7 @@ suite('Events', function() { try { var toolbox = document.getElementById('toolbox-categories'); var workspaceSvg = Blockly.inject('blocklyDiv', {toolbox: toolbox}); - temporary_fireEvent.firedEvents_ = []; + Blockly.Events.fire.firedEvents_ = []; var block = workspaceSvg.newBlock(''); block.initSvg(); diff --git a/tests/mocha/procedures_test.js b/tests/mocha/procedures_test.js index 8c6ce0e02..2b0047e47 100644 --- a/tests/mocha/procedures_test.js +++ b/tests/mocha/procedures_test.js @@ -295,6 +295,244 @@ suite('Procedures', function() { }, 'name'); }); }); + suite('Enable/Disable', function() { + setup(function() { + createEventsFireStub(); + var toolbox = document.getElementById('toolbox-categories'); + this.workspaceSvg = Blockly.inject('blocklyDiv', {toolbox: toolbox}); + }); + teardown(function() { + this.workspaceSvg.dispose(); + sinon.restore(); + }); + suite('Inherited disabled', function() { + setup(function() { + var dom = Blockly.Xml.textToDom( + '' + + '' + + 'bar' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + 'foo' + + '' + + '' + + 'baz' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + ''); + Blockly.Events.disable(); + Blockly.Xml.appendDomToWorkspace(dom, this.workspaceSvg); + Blockly.Events.enable(); + + this.barDef = this.workspaceSvg.getBlockById('bar-def'); + this.fooDef = this.workspaceSvg.getBlockById('foo-def'); + this.bazDef = this.workspaceSvg.getBlockById('baz-def'); + + this.barCalls = [ + this.workspaceSvg.getBlockById('bar-c1'), + this.workspaceSvg.getBlockById('bar-c2')]; + this.fooCalls = [ + this.workspaceSvg.getBlockById('foo-c1'), + this.workspaceSvg.getBlockById('foo-c2')]; + this.bazCall = this.workspaceSvg.getBlockById('baz-c1'); + }); + test('Nested caller', function() { + this.barDef.setEnabled(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isFalse(this.barCalls[i].isEnabled(), + 'Callers are disabled when their definition is disabled ' + + '(bar call ' + i + ')'); + } + chai.assert.isTrue(this.fooCalls[0].isEnabled(), + 'Callers in definitions are disabled by inheritance'); + chai.assert.isTrue(this.fooCalls[0].getInheritedDisabled(), + 'Callers in definitions are disabled by inheritance'); + + this.fooDef.setEnabled(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isFalse(this.fooCalls[i].isEnabled(), + 'Callers are disabled when their definition is disabled ' + + '(foo call ' + i + ')'); + } + + this.barDef.setEnabled(true); + + for (var i = 0; i < 2; i++) { + chai.assert.isTrue(this.barCalls[i].isEnabled(), + 'Callers are reenabled with their definition ' + + '(bar call ' + i + ')'); + } + chai.assert.isFalse(this.fooCalls[0].isEnabled(), + 'Nested disabled callers remain disabled'); + chai.assert.isFalse(this.fooCalls[0].getInheritedDisabled(), + 'Nested disabled callers remain disabled, not by inheritance'); + }); + test('Caller in return', function() { + this.bazDef.setEnabled(false); + + chai.assert.isFalse(this.bazCall.isEnabled(), + 'Caller is disabled with its definition'); + + chai.assert.isTrue(this.barCalls[1].isEnabled(), + 'Caller in the return is disabled by inheritance'); + chai.assert.isTrue(this.barCalls[1].getInheritedDisabled(), + 'Caller in the return is disabled by inheritance'); + + this.barDef.setEnabled(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isFalse(this.barCalls[i].isEnabled(), + 'Callers are disabled when their definition is disabled ' + + '(bar call ' + i + ')'); + } + + this.bazDef.setEnabled(true); + + chai.assert.isFalse(this.barCalls[1].isEnabled(), + 'Caller in return remains disabled'); + chai.assert.isFalse(this.barCalls[1].getInheritedDisabled(), + 'Caller in return remains disabled, not by inheritance'); + }); + }); + var testCases = [ + ['procedures_defnoreturn', + ('' + + '' + + 'bar' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '')], + ['procedures_defreturn', + ('' + + '' + + 'bar' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '')]]; + testCases.forEach(function(testCase) { + var suiteName = testCase[0]; + var domText = testCase[1]; + suite(suiteName, function() { + setup(function() { + var dom = Blockly.Xml.textToDom(domText); + + Blockly.Xml.appendDomToWorkspace(dom, this.workspaceSvg); + this.barDef = this.workspaceSvg.getBlockById('bar-def'); + this.barCalls = [ + this.workspaceSvg.getBlockById('bar-c1'), + this.workspaceSvg.getBlockById('bar-c2') + ]; + }); + + test('Set disabled updates callers', function() { + this.workspaceSvg.clearUndo(); + Blockly.Events.setGroup('g1'); + this.barDef.setEnabled(false); + Blockly.Events.setGroup(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isFalse(this.barCalls[i].isEnabled(), + 'Callers are disabled when their definition is disabled (call ' + + i + ')'); + } + var firedEvents = this.workspaceSvg.undoStack_; + chai.assert.equal(firedEvents.length, 3, + 'An event was fired for the definition and each caller'); + for (var i = 0; i < 3; i++) { + chai.assert.equal(firedEvents[i].group, 'g1', + 'Disable events are in the same group (event ' + i + ')'); + } + + this.workspaceSvg.clearUndo(); + Blockly.Events.setGroup('g2'); + this.barDef.setEnabled(true); + Blockly.Events.setGroup(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isTrue(this.barCalls[i].isEnabled(), + 'Callers are enabled when their definition is enabled (call ' + + i + ')'); + } + chai.assert.equal(firedEvents.length,3, + 'An event was fired for the definition and each caller'); + for (var i = 0; i < 3; i++) { + chai.assert.equal(firedEvents[i].group, 'g2', + 'Enable events are in the same group (event ' + i + ')'); + } + }); + test('Set disabled updates callers while remembering old caller state', function() { + this.barCalls[0].setEnabled(false); + this.workspaceSvg.clearUndo(); + Blockly.Events.setGroup('g1'); + this.barDef.setEnabled(false); + Blockly.Events.setGroup(false); + + for (var i = 0; i < 2; i++) { + chai.assert.isFalse(this.barCalls[i].isEnabled(), + 'Callers are disabled when their definition is disabled (call ' + + i + ')'); + } + var firedEvents = this.workspaceSvg.undoStack_; + chai.assert.equal(firedEvents.length, 2, + 'An event was fired for the definition and the enabled caller'); + for (var i = 0; i < 2; i++) { + chai.assert.equal(firedEvents[i].group, 'g1', + 'Disable events are in the same group (event ' + i + ')'); + } + + this.workspaceSvg.clearUndo(); + Blockly.Events.setGroup('g2'); + this.barDef.setEnabled(true); + Blockly.Events.setGroup(false); + + chai.assert.isFalse(this.barCalls[0].isEnabled(), + 'Caller remains in disabled state when the definition is enabled'); + chai.assert.isTrue(this.barCalls[1].isEnabled(), + 'Caller returns to previous enabled state when the definition is enabled'); + chai.assert.equal(firedEvents.length,2, + 'An event was fired for the definition and the enabled caller'); + for (var i = 0; i < 2; i++) { + chai.assert.equal(firedEvents[i].group, 'g2', + 'Enable events are in the same group (event ' + i + ')'); + } + }); + }); + }); + }); suite('Mutation', function() { setup(function() { this.findParentStub = sinon.stub(Blockly.Mutator, 'findParentWs') diff --git a/tests/mocha/test_helpers.js b/tests/mocha/test_helpers.js index c4a08c0d4..b18ab7fee 100644 --- a/tests/mocha/test_helpers.js +++ b/tests/mocha/test_helpers.js @@ -5,7 +5,8 @@ */ /* exported assertArrayEquals, assertVariableValues, captureWarnings - defineRowBlock, defineStackBlock, defineStatementBlock, createTestBlock */ + defineRowBlock, defineStackBlock, defineStatementBlock, createTestBlock, + createEventsFireStub */ /** * Check that two arrays have the same content. @@ -59,6 +60,25 @@ function captureWarnings(innerFunc) { return msgs; } + +/** + * Creates stub for Blockly.Events.fire that fires events immediately instead of + * with timeout. + * @return {sinon.stub} The created stub. + */ +function createEventsFireStub() { + var stub = sinon.stub(Blockly.Events, 'fire'); + stub.callsFake(function(event) { + if (!Blockly.Events.isEnabled()) { + return; + } + Blockly.Events.FIRE_QUEUE_.push(event); + Blockly.Events.fireNow_(); + }); + stub.firedEvents_ = []; + return stub; +} + function defineStackBlock() { Blockly.defineBlocksWithJsonArray([{ "type": "stack_block", diff --git a/tests/mocha/workspace_comment_test.js b/tests/mocha/workspace_comment_test.js index 8a354b918..4c42a816e 100644 --- a/tests/mocha/workspace_comment_test.js +++ b/tests/mocha/workspace_comment_test.js @@ -162,25 +162,15 @@ suite('Workspace comment', function() { }); suite('Content', function() { - function temporary_fireEvent(event) { - if (!Blockly.Events.isEnabled()) { - return; - } - Blockly.Events.FIRE_QUEUE_.push(event); - Blockly.Events.fireNow_(); - } - setup(function() { - this.savedFireFunc_ = Blockly.Events.fire; - Blockly.Events.fire = temporary_fireEvent; - temporary_fireEvent.firedEvents_ = []; + createEventsFireStub(); this.comment = new Blockly.WorkspaceComment( this.workspace, 'comment text', 0, 0, 'comment id'); }); teardown(function() { - Blockly.Events.fire = this.savedFireFunc_; + sinon.restore(); }); test('After creation', function() { diff --git a/tests/mocha/workspace_test.js b/tests/mocha/workspace_test.js index 74591b0f0..9693922ae 100644 --- a/tests/mocha/workspace_test.js +++ b/tests/mocha/workspace_test.js @@ -682,22 +682,8 @@ function testAWorkspace() { }); suite('Undo/Redo', function() { - function temporary_fireEvent(event) { - if (!Blockly.Events.isEnabled()) { - return; - } - Blockly.Events.FIRE_QUEUE_.push(event); - Blockly.Events.fireNow_(); - } - setup(function() { - this.savedFireFunc_ = Blockly.Events.fire; - Blockly.Events.fire = temporary_fireEvent; - temporary_fireEvent.firedEvents_ = []; - }); - - teardown(function() { - Blockly.Events.fire = this.savedFireFunc_; + createEventsFireStub(); }); function createTwoVarsDifferentTypes(workspace) { diff --git a/tests/workspace_svg/index.html b/tests/workspace_svg/index.html deleted file mode 100644 index 8a0b934f1..000000000 --- a/tests/workspace_svg/index.html +++ /dev/null @@ -1,419 +0,0 @@ - - - - -Blockly Workspace SVG testing - - - - - - - - - - - - - - - - -
- -

Blockly Workspace testing

- - - - - - - - - - - - - - - - diff --git a/tests/workspace_svg/procedure_svg_test.js b/tests/workspace_svg/procedure_svg_test.js deleted file mode 100644 index c3775dbb1..000000000 --- a/tests/workspace_svg/procedure_svg_test.js +++ /dev/null @@ -1,356 +0,0 @@ -/** - * @license - * Copyright 2018 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ -'use strict'; - -var savedFireFunc = Blockly.Events.fire; -var workspace; - -function procedureSvgTest_setup() { - Blockly.Events.fire = temporary_fireEvent; - temporary_fireEvent.firedEvents_ = []; - workspace = procedureSvgTest_createWorkspaceWithToolbox(); -} - -function procedureSvgTest_teardown() { - Blockly.Events.fire = savedFireFunc; - workspace.dispose(); -} - -function procedureSvgTest_createWorkspaceWithToolbox() { - var toolbox = document.getElementById('toolbox-categories'); - return Blockly.inject('blocklyDiv', {toolbox: toolbox}); -} - -function test_procedureReturnSetDisabledUpdatesCallers() { - procedureSvgTest_setup(); - try { - var dom = Blockly.Xml.textToDom( - '' + - '' + - 'bar' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''); - - - Blockly.Xml.appendDomToWorkspace(dom, workspace); - var barDef = workspace.getBlockById('bar-def'); - var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')]; - - workspace.clearUndo(); - Blockly.Events.setGroup('g1'); - barDef.setEnabled(false); - Blockly.Events.setGroup(false); - - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - - var firedEvents = workspace.undoStack_; - assertEquals('An event was fired for the definition and each caller.', - 3, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g1' == firedEvents[0].group - && 'g1' == firedEvents[1].group - && 'g1' == firedEvents[2].group); - - workspace.clearUndo(); - Blockly.Events.setGroup('g2'); - barDef.setEnabled(true); - Blockly.Events.setGroup(false); - - assertTrue('Callers are enabled when their definition is enabled.', - !barCalls[0].disabled && !barCalls[1].disabled); - - assertEquals('An event was fired for the definition and each caller.', - 3, firedEvents.length); - assertTrue('Enable events are in the same group.', - 'g2' == firedEvents[0].group - && 'g2' == firedEvents[1].group - && 'g2' == firedEvents[2].group); - - - } finally { - procedureSvgTest_teardown(); - } -} - - -function test_procedureReturnEnablingRemembersOldCallerState() { - procedureSvgTest_setup(); - try { - var dom = Blockly.Xml.textToDom( - '' + - '' + - 'bar' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''); - - - Blockly.Xml.appendDomToWorkspace(dom, workspace); - var barDef = workspace.getBlockById('bar-def'); - var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')]; - - barCalls[0].setEnabled(false); - workspace.clearUndo(); - Blockly.Events.setGroup('g1'); - barDef.setEnabled(false); - Blockly.Events.setGroup(false); - - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - - var firedEvents = workspace.undoStack_; - assertEquals('An event was fired for the definition and the enabled caller.', - 2, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g1' == firedEvents[0].group - && 'g1' == firedEvents[1].group); - - workspace.clearUndo(); - Blockly.Events.setGroup('g2'); - barDef.setEnabled(true); - Blockly.Events.setGroup(false); - - - assertTrue('Callers return to their previous state when the definition is enabled.', - barCalls[0].disabled && !barCalls[1].disabled); - - assertEquals('An event was fired for the definition and the enabled caller.', - 2, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g2' == firedEvents[0].group - && 'g2' == firedEvents[1].group); - - } finally { - procedureSvgTest_teardown(); - } -} - -function test_procedureNoReturnSetDisabledUpdatesCallers() { - procedureSvgTest_setup(); - try { - var dom = Blockly.Xml.textToDom( - '' + - '' + - 'bar' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''); - - - Blockly.Xml.appendDomToWorkspace(dom, workspace); - var barDef = workspace.getBlockById('bar-def'); - var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')]; - - workspace.clearUndo(); - Blockly.Events.setGroup('g1'); - barDef.setEnabled(false); - Blockly.Events.setGroup(false); - - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - - var firedEvents = workspace.undoStack_; - assertEquals('An event was fired for the definition and each caller.', - 3, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g1' == firedEvents[0].group - && 'g1' == firedEvents[1].group - && 'g1' == firedEvents[2].group); - - workspace.clearUndo(); - Blockly.Events.setGroup('g2'); - barDef.setEnabled(true); - Blockly.Events.setGroup(false); - - assertTrue('Callers are enabled when their definition is enabled.', - !barCalls[0].disabled && !barCalls[1].disabled); - - assertEquals('An event was fired for the definition and each caller.', - 3, firedEvents.length); - assertTrue('Enable events are in the same group.', - 'g2' == firedEvents[0].group - && 'g2' == firedEvents[1].group - && 'g2' == firedEvents[2].group); - - - } finally { - procedureSvgTest_teardown(); - } -} - - -function test_procedureNoReturnEnablingRemembersOldCallerState() { - procedureSvgTest_setup(); - try { - var dom = Blockly.Xml.textToDom( - '' + - '' + - 'bar' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''); - - - Blockly.Xml.appendDomToWorkspace(dom, workspace); - var barDef = workspace.getBlockById('bar-def'); - var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')]; - - barCalls[0].setEnabled(false); - workspace.clearUndo(); - Blockly.Events.setGroup('g1'); - barDef.setEnabled(false); - Blockly.Events.setGroup(false); - - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - - var firedEvents = workspace.undoStack_; - assertEquals('An event was fired for the definition and the enabled caller.', - 2, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g1' == firedEvents[0].group - && 'g1' == firedEvents[1].group); - - workspace.clearUndo(); - Blockly.Events.setGroup('g2'); - barDef.setEnabled(true); - Blockly.Events.setGroup(false); - - - assertTrue('Callers return to their previous state when the definition is enabled.', - barCalls[0].disabled && !barCalls[1].disabled); - - assertEquals('An event was fired for the definition and the enabled caller.', - 2, firedEvents.length); - assertTrue('Disable events are in the same group.', - 'g2' == firedEvents[0].group - && 'g2' == firedEvents[1].group); - - } finally { - procedureSvgTest_teardown(); - } -} - -/** -* This is a somewhat large test as it's checking interactions between -* three procedure definitions. -*/ -function test_procedureEnableDisableInteractions() { - procedureSvgTest_setup(); - try { - var dom = Blockly.Xml.textToDom( - '' + - '' + - 'bar' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - 'foo' + - '' + - '' + - 'baz' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''); - Blockly.Events.disable(); - Blockly.Xml.appendDomToWorkspace(dom, workspace); - Blockly.Events.enable(); - - var barDef = workspace.getBlockById('bar-def'); - var fooDef = workspace.getBlockById('foo-def'); - var bazDef = workspace.getBlockById('baz-def'); - - var barCalls = [workspace.getBlockById('bar-c1'), workspace.getBlockById('bar-c2')]; - var fooCalls = [workspace.getBlockById('foo-c1'), workspace.getBlockById('foo-c2')]; - var bazCall = workspace.getBlockById('baz-c1'); - - barDef.setEnabled(false); - - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - assertTrue('Callers in definitions are disabled by inheritance.', - !fooCalls[0].disabled && fooCalls[0].getInheritedDisabled()); - - fooDef.setEnabled(false); - - assertTrue('Callers are disabled when their definition is disabled', - fooCalls[0].disabled && fooCalls[1].disabled); - - barDef.setEnabled(true); - - assertTrue('Callers are reenabled with their definition', - !barCalls[0].disabled && !barCalls[0].disabled); - - assertTrue('Nested disabled callers remain disabled, not by inheritance.', - fooCalls[0].disabled && !fooCalls[0].getInheritedDisabled()); - - bazDef.setEnabled(false); - - assertTrue('Caller is disabled with its definition', - bazCall.disabled); - - assertTrue('Caller in the return is disabled by inheritance.', - !barCalls[1].disabled && barCalls[1].getInheritedDisabled()); - - barDef.setEnabled(false); - assertTrue('Callers are disabled when their definition is disabled.', - barCalls[0].disabled && barCalls[1].disabled); - - bazDef.setEnabled(true); - - assertTrue('Caller in the return remains disabled, not by inheritance.', - barCalls[1].disabled && !barCalls[1].getInheritedDisabled()); - - } finally { - procedureSvgTest_teardown(); - } - -}