From e337190c76a8fbfe44b93481f760d76c9dce0aa1 Mon Sep 17 00:00:00 2001 From: Heather <30523814+Heathermcx@users.noreply.github.com> Date: Tue, 9 Jan 2024 17:10:13 +1100 Subject: [PATCH] Account for diagram zoom when dragging resizers (#16) * add test * add fix * enable zoom for testing * finish writing tests * add fix for remaining resizers * Revert "enable zoom for testing" This reverts commit 69fa0aa301bedf49ddbfbb91e31c21d2f4016902. * fix formatting * make fields nullable * fix nullables * use file scoped namespaces --- .../Resizing/BottomLeftResizerProvider.cs | 32 +-- .../Resizing/BottomRightResizerProvider.cs | 25 ++- .../Resizing/TopLeftResizerProvider.cs | 33 +-- .../Resizing/TopRightResizerProvider.cs | 34 +-- .../Controls/ResizeControlTests.cs | 103 +++++----- .../BottomLeftResizerProviderTests.cs | 193 ++++++++++++------ .../BottomRightResizerProviderTests.cs | 189 +++++++++++------ .../Resizing/TopLeftResizerProviderTests.cs | 193 ++++++++++++------ .../Resizing/TopRightResizerProviderTests.cs | 193 ++++++++++++------ .../Controls/ResizeControlWidgetTests.cs | 25 ++- 10 files changed, 642 insertions(+), 378 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Positions/Resizing/BottomLeftResizerProvider.cs b/src/Blazor.Diagrams.Core/Positions/Resizing/BottomLeftResizerProvider.cs index c4e576c9..f3e8b10d 100644 --- a/src/Blazor.Diagrams.Core/Positions/Resizing/BottomLeftResizerProvider.cs +++ b/src/Blazor.Diagrams.Core/Positions/Resizing/BottomLeftResizerProvider.cs @@ -7,12 +7,14 @@ namespace Blazor.Diagrams.Core.Positions.Resizing { public class BottomLeftResizerProvider : IResizerProvider { - public string? Class => "bottomleft"; + public string? Class => "bottomleft"; + + private Size? _originalSize; + private Point? _originalPosition; + private Point? _originalMousePosition; + private NodeModel? _nodeModel; + private Diagram? _diagram; - private Size _originalSize = null!; - private Point _originalPosition = null!; - private Point _originalMousePosition = null!; - private NodeModel _nodeModel = null!; public Point? GetPosition(Model model) { @@ -29,22 +31,23 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr { _originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y); _originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); - _originalSize = nodeModel.Size!; + _originalSize = nodeModel.Size; _nodeModel = nodeModel; + _diagram = diagram; } } public void OnPointerMove(Model? model, PointerEventArgs args) { - if (_nodeModel is null) + if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null) { return; } - var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y); - var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X); + var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; + var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; - var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X); + var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; var positionY = _originalPosition.Y; if (width < _nodeModel.MinimumDimensions.Width) @@ -65,10 +68,11 @@ public void OnPointerMove(Model? model, PointerEventArgs args) public void OnResizeEnd(Model? model, PointerEventArgs args) { _nodeModel?.TriggerSizeChanged(); - _originalSize = null!; - _originalPosition = null!; - _originalMousePosition = null!; - _nodeModel = null!; + _originalSize = null; + _originalPosition = null; + _originalMousePosition = null; + _nodeModel = null; + _diagram = null; } } diff --git a/src/Blazor.Diagrams.Core/Positions/Resizing/BottomRightResizerProvider.cs b/src/Blazor.Diagrams.Core/Positions/Resizing/BottomRightResizerProvider.cs index f55e26d7..d5cad9e8 100644 --- a/src/Blazor.Diagrams.Core/Positions/Resizing/BottomRightResizerProvider.cs +++ b/src/Blazor.Diagrams.Core/Positions/Resizing/BottomRightResizerProvider.cs @@ -9,11 +9,12 @@ public class BottomRightResizerProvider : IResizerProvider { public string? Class => "bottomright"; - private Size _originalSize = null!; - private Point _originalMousePosition = null!; - private NodeModel _nodeModel = null!; + private Size? _originalSize; + private Point? _originalMousePosition; + private NodeModel? _nodeModel; + private Diagram? _diagram; - public Point? GetPosition(Model model) + public Point? GetPosition(Model model) { if (model is NodeModel nodeModel && nodeModel.Size is not null) { @@ -27,20 +28,21 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr if (model is NodeModel nodeModel) { _originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); - _originalSize = nodeModel.Size!; + _originalSize = nodeModel.Size; _nodeModel = nodeModel; + _diagram = diagram; } } public void OnPointerMove(Model? model, PointerEventArgs args) { - if (_nodeModel is null) + if (_originalSize is null || _originalMousePosition is null || _nodeModel is null || _diagram is null) { return; } - var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y); - var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X); + var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; + var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; if (width < _nodeModel.MinimumDimensions.Width) { @@ -57,9 +59,10 @@ public void OnPointerMove(Model? model, PointerEventArgs args) public void OnResizeEnd(Model? model, PointerEventArgs args) { _nodeModel?.TriggerSizeChanged(); - _originalSize = null!; - _originalMousePosition = null!; - _nodeModel = null!; + _originalSize = null; + _originalMousePosition = null; + _nodeModel = null; + _diagram = null; } } diff --git a/src/Blazor.Diagrams.Core/Positions/Resizing/TopLeftResizerProvider.cs b/src/Blazor.Diagrams.Core/Positions/Resizing/TopLeftResizerProvider.cs index 7c3afe5c..c9d7fbe6 100644 --- a/src/Blazor.Diagrams.Core/Positions/Resizing/TopLeftResizerProvider.cs +++ b/src/Blazor.Diagrams.Core/Positions/Resizing/TopLeftResizerProvider.cs @@ -7,12 +7,13 @@ namespace Blazor.Diagrams.Core.Positions.Resizing { public class TopLeftResizerProvider : IResizerProvider { - public string? Class => "topleft"; + public string? Class => "topleft"; - private Size _originalSize = null!; - private Point _originalPosition = null!; - private Point _originalMousePosition = null!; - private NodeModel _nodeModel = null!; + private Size? _originalSize; + private Point? _originalPosition; + private Point? _originalMousePosition; + private NodeModel? _nodeModel; + private Diagram? _diagram; public Point? GetPosition(Model model) { @@ -29,23 +30,24 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr { _originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y); _originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); - _originalSize = nodeModel.Size!; + _originalSize = nodeModel.Size; _nodeModel = nodeModel; + _diagram = diagram; } } public void OnPointerMove(Model? model, PointerEventArgs args) { - if (_nodeModel is null) + if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null) { return; } - var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y); - var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X); + var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; + var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; - var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X); - var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y); + var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; + var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; if (width < _nodeModel.MinimumDimensions.Width) { @@ -65,10 +67,11 @@ public void OnPointerMove(Model? model, PointerEventArgs args) public void OnResizeEnd(Model? model, PointerEventArgs args) { _nodeModel?.TriggerSizeChanged(); - _originalSize = null!; - _originalPosition = null!; - _originalMousePosition = null!; - _nodeModel = null!; + _originalSize = null; + _originalPosition = null; + _originalMousePosition = null; + _nodeModel = null; + _diagram = null; } } diff --git a/src/Blazor.Diagrams.Core/Positions/Resizing/TopRightResizerProvider.cs b/src/Blazor.Diagrams.Core/Positions/Resizing/TopRightResizerProvider.cs index e9ea4e14..8f19bc72 100644 --- a/src/Blazor.Diagrams.Core/Positions/Resizing/TopRightResizerProvider.cs +++ b/src/Blazor.Diagrams.Core/Positions/Resizing/TopRightResizerProvider.cs @@ -7,12 +7,13 @@ namespace Blazor.Diagrams.Core.Positions.Resizing { public class TopRightResizerProvider : IResizerProvider { - public string? Class => "topright"; + public string? Class => "topright"; - private Size _originalSize = null!; - private Point _originalPosition = null!; - private Point _originalMousePosition = null!; - private NodeModel _nodeModel = null!; + private Size? _originalSize; + private Point? _originalPosition; + private Point? _originalMousePosition; + private NodeModel? _nodeModel; + private Diagram? _diagram; public Point? GetPosition(Model model) { @@ -29,22 +30,24 @@ public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventAr { _originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y); _originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); - _originalSize = nodeModel.Size!; + _originalSize = nodeModel.Size; _nodeModel = nodeModel; + _diagram = diagram; } } public void OnPointerMove(Model? model, PointerEventArgs args) { - if (_nodeModel is null) + if (_originalSize is null || _originalPosition is null || _originalMousePosition is null || _nodeModel is null || _diagram is null) { return; } - var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y); - var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X); + + var height = _originalSize.Height - (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; + var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X) / _diagram.Zoom; var positionX = _originalPosition.X; - var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y); + var positionY = _originalPosition.Y + (args.ClientY - _originalMousePosition.Y) / _diagram.Zoom; if (width < _nodeModel.MinimumDimensions.Width) { @@ -64,11 +67,12 @@ public void OnPointerMove(Model? model, PointerEventArgs args) public void OnResizeEnd(Model? model, PointerEventArgs args) { _nodeModel?.TriggerSizeChanged(); - _originalSize = null!; - _originalPosition = null!; - _originalMousePosition = null!; - _nodeModel = null!; + _originalSize = null; + _originalPosition = null; + _originalMousePosition = null; + _nodeModel = null; + _diagram = null; } } -} +} \ No newline at end of file diff --git a/tests/Blazor.Diagrams.Core.Tests/Controls/ResizeControlTests.cs b/tests/Blazor.Diagrams.Core.Tests/Controls/ResizeControlTests.cs index dcd9bab1..5192be72 100644 --- a/tests/Blazor.Diagrams.Core.Tests/Controls/ResizeControlTests.cs +++ b/tests/Blazor.Diagrams.Core.Tests/Controls/ResizeControlTests.cs @@ -5,71 +5,70 @@ using Moq; using Xunit; -namespace Blazor.Diagrams.Core.Tests.Controls +namespace Blazor.Diagrams.Core.Tests.Controls; + +public class ResizeControlTests { - public class ResizeControlTests + [Fact] + public void GetPosition_ShouldUseResizeProviderGetPosition() { - [Fact] - public void GetPosition_ShouldUseResizeProviderGetPosition() - { - var resizeProvider = new Mock(); - var control = new ResizeControl(resizeProvider.Object); - var model = new Mock(); + var resizeProvider = new Mock(); + var control = new ResizeControl(resizeProvider.Object); + var model = new Mock(); - control.GetPosition(model.Object); + control.GetPosition(model.Object); - resizeProvider.Verify(m => m.GetPosition(model.Object), Times.Once); - } + resizeProvider.Verify(m => m.GetPosition(model.Object), Times.Once); + } - [Fact] - public void OnPointerDown_ShouldInvokeResizeStart() - { - var resizeProvider = new Mock(); - var control = new ResizeControl(resizeProvider.Object); - var diagram = Mock.Of(); - var model = Mock.Of(); - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); + [Fact] + public void OnPointerDown_ShouldInvokeResizeStart() + { + var resizeProvider = new Mock(); + var control = new ResizeControl(resizeProvider.Object); + var diagram = Mock.Of(); + var model = Mock.Of(); + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); - control.OnPointerDown(diagram, model, eventArgs); + control.OnPointerDown(diagram, model, eventArgs); - resizeProvider.Verify(m => m.OnResizeStart(diagram, model, eventArgs), Times.Once); - } + resizeProvider.Verify(m => m.OnResizeStart(diagram, model, eventArgs), Times.Once); + } - [Fact] - public void OnPointerDown_ShouldAddEventHandlers() - { - var resizeProvider = new Mock(); - var control = new ResizeControl(resizeProvider.Object); - var diagram = new TestDiagram(); - var model = Mock.Of(); - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); + [Fact] + public void OnPointerDown_ShouldAddEventHandlers() + { + var resizeProvider = new Mock(); + var control = new ResizeControl(resizeProvider.Object); + var diagram = new TestDiagram(); + var model = Mock.Of(); + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); - control.OnPointerDown(diagram, model, eventArgs); - - diagram.TriggerPointerMove(model, eventArgs); - resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Once); + control.OnPointerDown(diagram, model, eventArgs); + + diagram.TriggerPointerMove(model, eventArgs); + resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Once); - diagram.TriggerPointerUp(model, eventArgs); - resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once); - } + diagram.TriggerPointerUp(model, eventArgs); + resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once); + } - [Fact] - public void OnPointerUp_ShouldRemoveEventHandlers() - { - var resizeProvider = new Mock(); - var control = new ResizeControl(resizeProvider.Object); - var diagram = new TestDiagram(); - var model = Mock.Of(); - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); + [Fact] + public void OnPointerUp_ShouldRemoveEventHandlers() + { + var resizeProvider = new Mock(); + var control = new ResizeControl(resizeProvider.Object); + var diagram = new TestDiagram(); + var model = Mock.Of(); + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true); - control.OnPointerDown(diagram, model, eventArgs); - diagram.TriggerPointerUp(model, eventArgs); + control.OnPointerDown(diagram, model, eventArgs); + diagram.TriggerPointerUp(model, eventArgs); - diagram.TriggerPointerMove(model, eventArgs); - resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Never); + diagram.TriggerPointerMove(model, eventArgs); + resizeProvider.Verify(m => m.OnPointerMove(model, eventArgs), Times.Never); - diagram.TriggerPointerUp(model, eventArgs); - resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once); - } + diagram.TriggerPointerUp(model, eventArgs); + resizeProvider.Verify(m => m.OnResizeEnd(model, eventArgs), Times.Once); } } diff --git a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomLeftResizerProviderTests.cs b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomLeftResizerProviderTests.cs index 79ef6c17..1605cb14 100644 --- a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomLeftResizerProviderTests.cs +++ b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomLeftResizerProviderTests.cs @@ -11,72 +11,135 @@ using System.Threading.Tasks; using Xunit; -namespace Blazor.Diagrams.Core.Tests.Positions.Resizing +namespace Blazor.Diagrams.Core.Tests.Positions.Resizing; + +public class BottomLeftResizerProviderTests { - public class BottomLeftResizerProviderTests + [Fact] + public void DragResizer_ShouldResizeNode() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(10); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(90); + node.Size.Height.Should().Be(215); + } + + [Fact] + public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(99, -199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + eventArgs = new PointerEventArgs(300, -300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(99); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(0); + node.Size.Height.Should().Be(0); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedOut() { - [Fact] - public void DragResizer_ShouldResizeNode() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new BottomLeftResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(10); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(90); - node.Size.Height.Should().Be(215); - } - - [Fact] - public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new BottomLeftResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(99, -199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - eventArgs = new PointerEventArgs(300, -300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(99); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(0); - node.Size.Height.Should().Be(0); - } + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(0.5); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(20); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(80); + node.Size.Height.Should().Be(230); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedIn() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(2); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(5); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(95); + node.Size.Height.Should().Be(207.5); } } diff --git a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomRightResizerProviderTests.cs b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomRightResizerProviderTests.cs index 20ab2777..959724e6 100644 --- a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomRightResizerProviderTests.cs +++ b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/BottomRightResizerProviderTests.cs @@ -11,70 +11,133 @@ using System.Threading.Tasks; using Xunit; -namespace Blazor.Diagrams.Core.Tests.Positions.Resizing +namespace Blazor.Diagrams.Core.Tests.Positions.Resizing; + +public class BottomRightResizerProviderTests { - public class BottomRightResizerProviderTests + [Fact] + public void DragResizer_ShouldResizeNode() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(110); + node.Size.Height.Should().Be(215); + } + + [Fact] + public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(-300, -300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(0); + node.Size.Height.Should().Be(0); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedOut() { - [Fact] - public void DragResizer_ShouldResizeNode() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new BottomRightResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(110); - node.Size.Height.Should().Be(215); - } - - [Fact] - public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new BottomRightResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(-300, -300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(0); - node.Size.Height.Should().Be(0); - } + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(0.5); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(120); + node.Size.Height.Should().Be(230); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedIn() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new BottomRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(2); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(105); + node.Size.Height.Should().Be(207.5); } } diff --git a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopLeftResizerProviderTests.cs b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopLeftResizerProviderTests.cs index 1cc715d5..13d241d0 100644 --- a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopLeftResizerProviderTests.cs +++ b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopLeftResizerProviderTests.cs @@ -11,72 +11,135 @@ using System.Threading.Tasks; using Xunit; -namespace Blazor.Diagrams.Core.Tests.Positions.Resizing +namespace Blazor.Diagrams.Core.Tests.Positions.Resizing; + +public class TopLeftResizerProviderTests { - public class TopLeftResizerProviderTests + [Fact] + public void DragResizer_ShouldResizeNode() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(10); + node.Position.Y.Should().Be(15); + node.Size.Width.Should().Be(90); + node.Size.Height.Should().Be(185); + } + + [Fact] + public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(99, 199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + eventArgs = new PointerEventArgs(300, 300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(99); + node.Position.Y.Should().Be(199); + node.Size.Width.Should().Be(0); + node.Size.Height.Should().Be(0); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedOut() { - [Fact] - public void DragResizer_ShouldResizeNode() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new TopLeftResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(10); - node.Position.Y.Should().Be(15); - node.Size.Width.Should().Be(90); - node.Size.Height.Should().Be(185); - } - - [Fact] - public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new TopLeftResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(99, 199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - eventArgs = new PointerEventArgs(300, 300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(99); - node.Position.Y.Should().Be(199); - node.Size.Width.Should().Be(0); - node.Size.Height.Should().Be(0); - } + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(0.5); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(20); + node.Position.Y.Should().Be(30); + node.Size.Width.Should().Be(80); + node.Size.Height.Should().Be(170); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedIn() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopLeftResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(2); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(5); + node.Position.Y.Should().Be(7.5); + node.Size.Width.Should().Be(95); + node.Size.Height.Should().Be(192.5); } } diff --git a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopRightResizerProviderTests.cs b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopRightResizerProviderTests.cs index a138d524..5fcd3b82 100644 --- a/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopRightResizerProviderTests.cs +++ b/tests/Blazor.Diagrams.Core.Tests/Positions/Resizing/TopRightResizerProviderTests.cs @@ -11,72 +11,135 @@ using System.Threading.Tasks; using Xunit; -namespace Blazor.Diagrams.Core.Tests.Positions.Resizing +namespace Blazor.Diagrams.Core.Tests.Positions.Resizing; + +public class TopRightResizerProviderTests { - public class TopRightResizerProviderTests + [Fact] + public void DragResizer_ShouldResizeNode() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(15); + node.Size.Width.Should().Be(110); + node.Size.Height.Should().Be(185); + } + + [Fact] + public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(-99, 199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + eventArgs = new PointerEventArgs(-300, 300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(199); + node.Size.Width.Should().Be(0); + node.Size.Height.Should().Be(0); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedOut() { - [Fact] - public void DragResizer_ShouldResizeNode() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new TopRightResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(15); - node.Size.Width.Should().Be(110); - node.Size.Height.Should().Be(185); - } - - [Fact] - public void DragResizer_SmallerThanMinSize_SetsNodeToMinSize() - { - // setup - var diagram = new TestDiagram(); - diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); - var node = new NodeModel(position: new Point(0, 0)); - node.Size = new Size(100, 200); - var control = new ResizeControl(new TopRightResizerProvider()); - diagram.Controls.AddFor(node).Add(control); - diagram.SelectModel(node, false); - - // before resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(0); - node.Size.Width.Should().Be(100); - node.Size.Height.Should().Be(200); - - // resize - var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - control.OnPointerDown(diagram, node, eventArgs); - eventArgs = new PointerEventArgs(-99, 199, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - eventArgs = new PointerEventArgs(-300, 300, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); - diagram.TriggerPointerMove(null, eventArgs); - - // after resize - node.Position.X.Should().Be(0); - node.Position.Y.Should().Be(199); - node.Size.Width.Should().Be(0); - node.Size.Height.Should().Be(0); - } + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(0.5); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(30); + node.Size.Width.Should().Be(120); + node.Size.Height.Should().Be(170); + } + + [Fact] + public void DragResizer_ShouldResizeNode_WhenDiagramZoomedIn() + { + // setup + var diagram = new TestDiagram(); + diagram.SetContainer(new Rectangle(0, 0, 1000, 400)); + var node = new NodeModel(position: new Point(0, 0)); + node.Size = new Size(100, 200); + var control = new ResizeControl(new TopRightResizerProvider()); + diagram.Controls.AddFor(node).Add(control); + diagram.SelectModel(node, false); + diagram.SetZoom(2); + + // before resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(0); + node.Size.Width.Should().Be(100); + node.Size.Height.Should().Be(200); + + // resize + var eventArgs = new PointerEventArgs(0, 0, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + control.OnPointerDown(diagram, node, eventArgs); + eventArgs = new PointerEventArgs(10, 15, 0, 0, false, false, false, 1, 1, 1, 1, 1, 1, "arrow", true); + diagram.TriggerPointerMove(null, eventArgs); + + // after resize + node.Position.X.Should().Be(0); + node.Position.Y.Should().Be(7.5); + node.Size.Width.Should().Be(105); + node.Size.Height.Should().Be(192.5); } } diff --git a/tests/Blazor.Diagrams.Tests/Components/Controls/ResizeControlWidgetTests.cs b/tests/Blazor.Diagrams.Tests/Components/Controls/ResizeControlWidgetTests.cs index bf51e541..ebc758c4 100644 --- a/tests/Blazor.Diagrams.Tests/Components/Controls/ResizeControlWidgetTests.cs +++ b/tests/Blazor.Diagrams.Tests/Components/Controls/ResizeControlWidgetTests.cs @@ -5,21 +5,20 @@ using Moq; using Xunit; -namespace Blazor.Diagrams.Tests.Components.Controls -{ +namespace Blazor.Diagrams.Tests.Components.Controls; + public class ResizeControlWidgetTests +{ + [Fact] + public void ShouldRenderDiv() { - [Fact] - public void ShouldRenderDiv() - { - using var ctx = new TestContext(); - var providerMock = Mock.Of(); - - var cut = ctx.RenderComponent(parameters => - parameters.Add(w => w.Control, new ResizeControl(providerMock)) - ); + using var ctx = new TestContext(); + var providerMock = Mock.Of(); + + var cut = ctx.RenderComponent(parameters => + parameters.Add(w => w.Control, new ResizeControl(providerMock)) + ); - cut.MarkupMatches("
"); - } + cut.MarkupMatches("
"); } }