/** * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: Apache-2.0 */ suite("Trashcan", function() { function fireDeleteEvent(workspace, xmlString) { var xml = Blockly.Xml.textToDom( '' + xmlString + ''); xml = xml.children[0]; var event = new Blockly.Events.Delete(); event.oldXml = xml; event.workspaceId = workspace.id; Blockly.Events.fire(event); } function fireNonDeleteEvent(workspace, oldXml) { var event = new Blockly.Events.Abstract(); event.type = 'dummy_type'; event.workspaceId = workspace.id; if (oldXml) { event.oldXml = oldXml; } Blockly.Events.fire(/** @type {Blockly.Events.Abstract} */ event); } setup(function() { sharedTestSetup.call(this); this.workspace = Blockly.inject('blocklyDiv', {'trashcan': true, 'maxTrashcanContents': Infinity}); this.trashcan = this.workspace.trashcan; }); teardown(function() { sharedTestTeardown.call(this); }); suite("Events", function() { test("Delete", function() { fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); }); test("Non-Delete", function() { fireNonDeleteEvent(this.workspace); chai.assert.equal(this.trashcan.contents_.length, 0); }); test("Non-Delete w/ oldXml", function() { var xml = Blockly.Xml.textToDom( '' + ' ' + '' ); xml = xml.children[0]; fireNonDeleteEvent(this.workspace, xml); chai.assert.equal(this.trashcan.contents_.length, 0); }); test("Shadow Delete", function() { fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 0); }); test("Click without contents - fires workspace click", function() { simulateClick(this.trashcan.svgGroup_); assertEventNotFired( this.eventsFireStub, Blockly.Events.TrashcanOpen, {}); assertEventFired( this.eventsFireStub, Blockly.Events.Click, {targetType: 'workspace'}, this.workspace.id, null); }); test("Click with contents - fires trashcanOpen", function() { fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); // Stub flyout interaction. var showFlyoutStub = sinon.stub(this.trashcan.flyout, "show"); simulateClick(this.trashcan.svgGroup_); sinon.assert.calledOnce(showFlyoutStub); assertEventFired( this.eventsFireStub, Blockly.Events.TrashcanOpen, {isOpen: true}, this.workspace.id); assertEventNotFired( this.eventsFireStub, Blockly.Events.Click, {}); }); test("Click outside trashcan - fires trashcanClose", function() { sinon.stub(this.trashcan.flyout, 'isVisible').returns(true); // Stub flyout interaction. var hideFlyoutStub = sinon.stub(this.trashcan.flyout, "hide"); simulateClick(this.workspace.svgGroup_); sinon.assert.calledOnce(hideFlyoutStub); assertEventFired( this.eventsFireStub, Blockly.Events.TrashcanOpen, {isOpen: false}, this.workspace.id); assertEventFired( this.eventsFireStub, Blockly.Events.Click, {targetType: 'workspace'}, this.workspace.id, null); }); }); suite("Unique Contents", function() { test("Simple", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); }); test("Different Coords", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); }); test("Different IDs", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); }); test("No Disabled - Disabled True", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); // Disabled tags get removed because disabled blocks aren't allowed to // be dragged from flyouts. See #2239 and #3243. chai.assert.equal(this.trashcan.contents_.length, 1); }); test("No Editable - Editable False", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("No Movable - Movable False", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Field Values", function() { fireDeleteEvent(this.workspace, '' + ' dummy_value1' + '' ); fireDeleteEvent(this.workspace, '' + ' dummy_value2' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("No Values - Values", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Value Blocks", function() { fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("No Statements - Statements", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Statement Blocks", function() { fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("No Next - Next", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Next Blocks", function() { fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); fireDeleteEvent(this.workspace, '' + ' ' + ' ' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("No Comment - Comment", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, '' + ' comment_text' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Comment Text", function() { fireDeleteEvent(this.workspace, '' + ' comment_text1' + '' ); fireDeleteEvent(this.workspace, '' + ' comment_text2' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Comment Size", function() { fireDeleteEvent(this.workspace, '' + ' comment_text' + '' ); fireDeleteEvent(this.workspace, '' + ' comment_text' + '' ); // h & w tags are removed b/c the blocks appear the same. chai.assert.equal(this.trashcan.contents_.length, 1); }); test("Different Comment Pinned", function() { fireDeleteEvent(this.workspace, '' + ' comment_text' + '' ); fireDeleteEvent(this.workspace, '' + ' comment_text' + '' ); // pinned tags are removed b/c the blocks appear the same. chai.assert.equal(this.trashcan.contents_.length, 1); }); test("No Mutator - Mutator", function() { fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, '' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); test("Different Mutator", function() { fireDeleteEvent(this.workspace, '' + ' ' + '' ); fireDeleteEvent(this.workspace, '' + ' ' + '' ); chai.assert.equal(this.trashcan.contents_.length, 2); }); }); suite("Max Contents", function() { test("Max 0", function() { this.workspace.options.maxTrashcanContents = 0; fireDeleteEvent(this.workspace, '' ); chai.assert.equal(this.trashcan.contents_.length, 0); this.workspace.options.maxTrashcanContents = Infinity; }); test("Last In First Out", function() { this.workspace.options.maxTrashcanContents = 1; fireDeleteEvent(this.workspace, ''); fireDeleteEvent(this.workspace, ''); chai.assert.equal(this.trashcan.contents_.length, 1); chai.assert.equal( Blockly.Xml.textToDom(this.trashcan.contents_[0]) .getAttribute('type'), 'dummy_type2' ); this.workspace.options.maxTrashcanContents = Infinity; }); }); });