Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/#359 should delete constraint not checked on remove control #371

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/Blazor.Diagrams.Core/Controls/Default/RemoveControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,44 @@ public RemoveControl(IPositionProvider positionProvider)

public override Point? GetPosition(Model model) => _positionProvider.GetPosition(model);

public override ValueTask OnPointerDown(Diagram diagram, Model model, PointerEventArgs _)
public override async ValueTask OnPointerDown(Diagram diagram, Model model, PointerEventArgs _)
{
switch (model)
if (await ShouldDeleteModel(diagram, model))
{
DeleteModel(diagram, model);
}
}

private static void DeleteModel(Diagram diagram, Model model)
{
switch (model)
{
case GroupModel group:
diagram.Groups.Delete(group);
return;
case NodeModel node:
diagram.Nodes.Remove(node);
break;
return;

case BaseLinkModel link:
diagram.Links.Remove(link);
break;
return;
}
}

private static async ValueTask<bool> ShouldDeleteModel(Diagram diagram, Model model)
{
if (model.Locked)
{
return false;
}

return ValueTask.CompletedTask;
return model switch
{
GroupModel group => await diagram.Options.Constraints.ShouldDeleteGroup.Invoke(group),
NodeModel node => await diagram.Options.Constraints.ShouldDeleteNode.Invoke(node),
BaseLinkModel link => await diagram.Options.Constraints.ShouldDeleteLink.Invoke(link),
_ => false,
};
}
}
272 changes: 272 additions & 0 deletions tests/Blazor.Diagrams.Core.Tests/Controls/RemoveControlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
using Blazor.Diagrams.Core.Controls.Default;
using Blazor.Diagrams.Core.Events;
using Blazor.Diagrams.Core.Geometry;
using Blazor.Diagrams.Core.Models;
using Blazor.Diagrams.Core.Models.Base;
using Moq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Blazor.Diagrams.Core.Tests.Controls
{
public class RemoveControlTests
{
public PointerEventArgs PointerEventArgs
=> new(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true);

[Fact]
public async Task OnPointerDown_NoConstraints_RemovesNode()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram();
var nodeMock = new Mock<NodeModel>(Point.Zero);
var node = diagram.Nodes.Add(nodeMock.Object);

// Act
await removeControl.OnPointerDown(diagram, node, PointerEventArgs);

// Assert
Assert.Empty(diagram.Nodes);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteNodeTrue_RemovesNode()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteNode = (node) => ValueTask.FromResult(true)
}
});
var nodeMock = new Mock<NodeModel>(Point.Zero);
var node = diagram.Nodes.Add(nodeMock.Object);

// Act
await removeControl.OnPointerDown(diagram, node, PointerEventArgs);

// Assert
Assert.Empty(diagram.Nodes);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteNodeFalse_KeepsNode()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteNode = (node) => ValueTask.FromResult(false)
}
});
var nodeMock = new Mock<NodeModel>(Point.Zero);
var node = diagram.Nodes.Add(nodeMock.Object);

// Act
await removeControl.OnPointerDown(diagram, node, PointerEventArgs);

// Assert
Assert.Contains(node, diagram.Nodes);
}

[Fact]
public async Task OnPointerDown_NoConstraints_RemovesLink()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram();

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var link = new LinkModel(
node1.GetPort(PortAlignment.Right)!,
node2.GetPort(PortAlignment.Left)!
);

diagram.Links.Add(link);

// Act
await removeControl.OnPointerDown(diagram, link, PointerEventArgs);

// Assert
Assert.Empty(diagram.Links);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteLinkTrue_RemovesLink()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteLink = (node) => ValueTask.FromResult(true)
}
});

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var link = new LinkModel(
node1.GetPort(PortAlignment.Right)!,
node2.GetPort(PortAlignment.Left)!
);

diagram.Links.Add(link);

// Act
await removeControl.OnPointerDown(diagram, link, PointerEventArgs);

// Assert
Assert.Empty(diagram.Links);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteLinkFalse_KeepsLink()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteLink = (node) => ValueTask.FromResult(false)
}
});

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var link = new LinkModel(
node1.GetPort(PortAlignment.Right)!,
node2.GetPort(PortAlignment.Left)!
);

diagram.Links.Add(link);

// Act
await removeControl.OnPointerDown(diagram, link, PointerEventArgs);

// Assert
Assert.Contains(link, diagram.Links);
}

[Fact]
public async Task OnPointerDown_NoConstraints_RemovesGroup()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram();

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var group = new GroupModel(new[] { node1, node2 });


diagram.Groups.Add(group);

// Act
await removeControl.OnPointerDown(diagram, group, PointerEventArgs);

// Assert
Assert.Empty(diagram.Groups);
Assert.Empty(diagram.Nodes);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteGroupTrue_RemovesGroup()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteGroup = (node) => ValueTask.FromResult(true)
}
});

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var group = new GroupModel(new[] { node1, node2 });


diagram.Groups.Add(group);

// Act
await removeControl.OnPointerDown(diagram, group, PointerEventArgs);

// Assert
Assert.Empty(diagram.Groups);
Assert.Empty(diagram.Nodes);
}

[Fact]
public async Task OnPointerDown_ShouldDeleteGroupFalse_KeepsGroup()
{
// Arrange
RemoveControl removeControl = new(0, 0);
Diagram diagram = new TestDiagram(
new Options.DiagramOptions
{
Constraints =
{
ShouldDeleteGroup = (node) => ValueTask.FromResult(false)
}
});

var node1 = new NodeModel(new Point(50, 50));
var node2 = new NodeModel(new Point(300, 300));
diagram.Nodes.Add(new[] { node1, node2 });
node1.AddPort(PortAlignment.Right);
node2.AddPort(PortAlignment.Left);

var group = new GroupModel(new[] { node1, node2 });


diagram.Groups.Add(group);

// Act
await removeControl.OnPointerDown(diagram, group, PointerEventArgs);

// Assert
Assert.Contains(group, diagram.Groups);
Assert.Contains(node1, diagram.Nodes);
Assert.Contains(node2, diagram.Nodes);
}

}
}
Loading