<commit_before>/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

suite("Trashcan", function() {
  function fireDeleteEvent(workspace, xmlString) {
    var xml = Blockly.Xml.textToDom(
        '<xml xmlns="https://developers.google.com/blockly/xml">' +
        xmlString + '</xml>');
    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);
    var options = new Blockly.Options(
        {'trashcan': true, 'maxTrashcanContents': Infinity});
    this.workspace = new Blockly.WorkspaceSvg(options);
    this.trashcan = new Blockly.Trashcan(this.workspace);
    // Stub the trashcan dom.
    this.trashcan.svgLid_ = sinon.createStubInstance(SVGElement);
  });
  teardown(function() {
    sharedTestTeardown.call(this);
  });

  suite("Events", function() {
    test("Delete", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      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 xmlns="https://developers.google.com/blockly/xml">' +
          '  <block type="dummy_type"/>' +
          '</xml>'
      );
      xml = xml.children[0];
      fireNonDeleteEvent(this.workspace, xml);
      chai.assert.equal(this.trashcan.contents_.length, 0);
    });
    test("Shadow Delete", function() {
      fireDeleteEvent(this.workspace, '<shadow type="dummy_type"/>');
      chai.assert.equal(this.trashcan.contents_.length, 0);
    });
    test("Click without contents - fires no events", function() {
      this.trashcan.click();
      var lastFireCall = this.eventsFireStub.lastCall;
      chai.assert.notExists(lastFireCall);
    });
    test("Click with contents - fires trashcanOpen", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
      // Stub flyout interaction.
      var showFlyoutStub = sinon.stub(this.trashcan.flyout, "show");
      this.trashcan.click();

      assertEventFired(
          this.eventsFireStub, Blockly.Events.Ui,
          {element: 'trashcanOpen', oldValue: null, newValue: true},
          this.workspace.id, null);
      sinon.assert.calledOnce(showFlyoutStub);
    });
  });
  suite("Unique Contents", function() {
    test("Simple", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("Different Coords", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type" x="10" y="10"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" x="20" y="20"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("Different IDs", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type" id="id1"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" id="id2"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("No Disabled - Disabled True", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" disabled="true"/>');
      // 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, '<block type="dummy_type"/>');<commit_msg>Use event dispatching to test trashcan click. (#4156)<commit_after>/**
 * @license
 * Copyright 2019 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

suite("Trashcan", function() {
  function fireDeleteEvent(workspace, xmlString) {
    var xml = Blockly.Xml.textToDom(
        '<xml xmlns="https://developers.google.com/blockly/xml">' +
        xmlString + '</xml>');
    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, '<block type="dummy_type"/>');
      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 xmlns="https://developers.google.com/blockly/xml">' +
          '  <block type="dummy_type"/>' +
          '</xml>'
      );
      xml = xml.children[0];
      fireNonDeleteEvent(this.workspace, xml);
      chai.assert.equal(this.trashcan.contents_.length, 0);
    });
    test("Shadow Delete", function() {
      fireDeleteEvent(this.workspace, '<shadow type="dummy_type"/>');
      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.Ui, {element: 'trashcanOpen'});
      assertEventFired(
          this.eventsFireStub, Blockly.Events.Ui,
          {element: 'click', oldValue: null, newValue: 'workspace'},
          this.workspace.id, null);
    });
    test("Click with contents - fires trashcanOpen", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      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.Ui,
          {element: 'trashcanOpen', oldValue: null, newValue: true},
          this.workspace.id, null);
      assertEventNotFired(
          this.eventsFireStub, Blockly.Events.Ui, {element: '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.Ui,
          {element: 'trashcanOpen', oldValue: null, newValue: false},
          this.workspace.id, null);
      assertEventFired(
          this.eventsFireStub, Blockly.Events.Ui,
          {element: 'click', oldValue: null, newValue: 'workspace'},
          this.workspace.id, null);
    });
  });
  suite("Unique Contents", function() {
    test("Simple", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("Different Coords", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type" x="10" y="10"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" x="20" y="20"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("Different IDs", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type" id="id1"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" id="id2"/>');
      chai.assert.equal(this.trashcan.contents_.length, 1);
    });
    test("No Disabled - Disabled True", function() {
      fireDeleteEvent(this.workspace, '<block type="dummy_type"/>');
      fireDeleteEvent(this.workspace, '<block type="dummy_type" disabled="true"/>');
      // 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, '<block type="dummy_type"/>');
{'fraction_non_alphanumeric': 0.15842636895268475, 'fraction_numerical': 0.00786815523657629, 'mean_word_length': 3.3166590178981186, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 36}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3369731', 'n_tokens_mistral': 2996, 'n_tokens_neox': 2966, 'n_words': 526}